blob: df1ca6990444173a3cf83331b12f8086a035fdfb [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
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010054// This is VTermScreenCell without the characters, thus much smaller.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020055typedef 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 Moolenaar0d6f5d92019-12-05 21:33:15 +010086// typedef term_T in structs.h
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020087struct 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)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010094 int tl_system; // when non-zero used for :!cmd output
95 int tl_toprow; // row with first line of system terminal
Bram Moolenaar13568252018-03-16 20:46:58 +010096#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020097
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010098 // Set when setting the size of a vterm, reset after redrawing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020099 int tl_vterm_size_changed;
100
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100101 int tl_normal_mode; // TRUE: Terminal-Normal mode
Bram Moolenaareea32af2021-11-21 14:51:13 +0000102 int tl_channel_closing;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200103 int tl_channel_closed;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200104 int tl_channel_recently_closed; // still need to handle tl_finish
105
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100106 int tl_finish;
107#define TL_FINISH_UNSET NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100108#define TL_FINISH_CLOSE 'c' // ++close or :terminal without argument
109#define TL_FINISH_NOCLOSE 'n' // ++noclose
110#define TL_FINISH_OPEN 'o' // ++open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200111 char_u *tl_opencmd;
112 char_u *tl_eof_chars;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200113 char_u *tl_api; // prefix for terminal API function
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200114
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100115 char_u *tl_arg0_cmd; // To format the status bar
116
Bram Moolenaar4f974752019-02-17 17:44:42 +0100117#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200118 void *tl_winpty_config;
119 void *tl_winpty;
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200120
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100121 HPCON tl_conpty;
122 DYN_STARTUPINFOEXW tl_siex; // Structure that always needs to be hold
123
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200124 FILE *tl_out_fd;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200125#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100126#if defined(FEAT_SESSION)
127 char_u *tl_command;
128#endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100129 char_u *tl_kill;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200130
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100131 // last known vterm size
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200132 int tl_rows;
133 int tl_cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200134
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100135 char_u *tl_title; // NULL or allocated
136 char_u *tl_status_text; // NULL or allocated
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200137
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100138 // Range of screen rows to update. Zero based.
139 int tl_dirty_row_start; // MAX_ROW if nothing dirty
140 int tl_dirty_row_end; // row below last one to update
141 int tl_dirty_snapshot; // text updated after making snapshot
Bram Moolenaar56bc8e22018-05-10 18:05:56 +0200142#ifdef FEAT_TIMERS
143 int tl_timer_set;
144 proftime_T tl_timer_due;
145#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100146 int tl_postponed_scroll; // to be scrolled up
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200147
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200148 garray_T tl_scrollback;
149 int tl_scrollback_scrolled;
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100150 garray_T tl_scrollback_postponed;
151
Bram Moolenaar83d47902020-03-26 20:34:00 +0100152 char_u *tl_highlight_name; // replaces "Terminal"; allocated
153
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200154 cellattr_T tl_default_color;
155
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100156 linenr_T tl_top_diff_rows; // rows of top diff file or zero
157 linenr_T tl_bot_diff_rows; // rows of bottom diff file
Bram Moolenaard96ff162018-02-18 22:13:29 +0100158
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200159 VTermPos tl_cursor_pos;
160 int tl_cursor_visible;
161 int tl_cursor_blink;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100162 int tl_cursor_shape; // 1: block, 2: underline, 3: bar
163 char_u *tl_cursor_color; // NULL or allocated
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200164
165 int tl_using_altscreen;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200166 garray_T tl_osc_buf; // incomplete OSC string
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200167};
168
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100169#define TMODE_ONCE 1 // CTRL-\ CTRL-N used
170#define TMODE_LOOP 2 // CTRL-W N used
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200171
172/*
173 * List of all active terminals.
174 */
175static term_T *first_term = NULL;
176
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100177// Terminal active in terminal_loop().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200178static term_T *in_terminal_loop = NULL;
179
Bram Moolenaar4f974752019-02-17 17:44:42 +0100180#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100181static BOOL has_winpty = FALSE;
182static BOOL has_conpty = FALSE;
183#endif
184
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100185#define MAX_ROW 999999 // used for tl_dirty_row_end to update all rows
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200186#define KEY_BUF_LEN 200
187
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200188#define FOR_ALL_TERMS(term) \
189 for ((term) = first_term; (term) != NULL; (term) = (term)->tl_next)
190
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200191/*
192 * Functions with separate implementation for MS-Windows and Unix-like systems.
193 */
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200194static 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 +0200195static int create_pty_only(term_T *term, jobopt_T *opt);
196static void term_report_winsize(term_T *term, int rows, int cols);
197static void term_free_vterm(term_T *term);
Bram Moolenaar13568252018-03-16 20:46:58 +0100198#ifdef FEAT_GUI
199static void update_system_term(term_T *term);
200#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200201
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100202static void handle_postponed_scrollback(term_T *term);
203
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100204// The character that we know (or assume) that the terminal expects for the
205// backspace key.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200206static int term_backspace_char = BS;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100208// Store the last set and the desired cursor properties, so that we only update
209// them when needed. Doing it unnecessary may result in flicker.
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200210static char_u *last_set_cursor_color = NULL;
211static char_u *desired_cursor_color = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +0100212static int last_set_cursor_shape = -1;
213static int desired_cursor_shape = -1;
214static int last_set_cursor_blink = -1;
215static int desired_cursor_blink = -1;
216
217
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100218///////////////////////////////////////
219// 1. Generic code for all systems.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200220
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200221 static int
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200222cursor_color_equal(char_u *lhs_color, char_u *rhs_color)
223{
224 if (lhs_color != NULL && rhs_color != NULL)
225 return STRCMP(lhs_color, rhs_color) == 0;
226 return lhs_color == NULL && rhs_color == NULL;
227}
228
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200229 static void
230cursor_color_copy(char_u **to_color, char_u *from_color)
231{
232 // Avoid a free & alloc if the value is already right.
233 if (cursor_color_equal(*to_color, from_color))
234 return;
235 vim_free(*to_color);
236 *to_color = (from_color == NULL) ? NULL : vim_strsave(from_color);
237}
238
239 static char_u *
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200240cursor_color_get(char_u *color)
241{
242 return (color == NULL) ? (char_u *)"" : color;
243}
244
245
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200246/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200247 * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
Bram Moolenaar498c2562018-04-15 23:45:15 +0200248 * current window.
249 * Sets "rows" and/or "cols" to zero when it should follow the window size.
250 * Return TRUE if the size is the minimum size: "24*80".
251 */
252 static int
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200253parse_termwinsize(win_T *wp, int *rows, int *cols)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200254{
255 int minsize = FALSE;
256
257 *rows = 0;
258 *cols = 0;
259
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200260 if (*wp->w_p_tws != NUL)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200261 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200262 char_u *p = vim_strchr(wp->w_p_tws, 'x');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200263
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100264 // Syntax of value was already checked when it's set.
Bram Moolenaar498c2562018-04-15 23:45:15 +0200265 if (p == NULL)
266 {
267 minsize = TRUE;
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200268 p = vim_strchr(wp->w_p_tws, '*');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200269 }
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200270 *rows = atoi((char *)wp->w_p_tws);
Bram Moolenaar498c2562018-04-15 23:45:15 +0200271 *cols = atoi((char *)p + 1);
272 }
273 return minsize;
274}
275
276/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200277 * Determine the terminal size from 'termwinsize' and the current window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200278 */
279 static void
Bram Moolenaarb936b792020-09-04 18:34:09 +0200280set_term_and_win_size(term_T *term, jobopt_T *opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200281{
Bram Moolenaarb936b792020-09-04 18:34:09 +0200282 int rows, cols;
283 int minsize;
284
Bram Moolenaar13568252018-03-16 20:46:58 +0100285#ifdef FEAT_GUI
286 if (term->tl_system)
287 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100288 // Use the whole screen for the system command. However, it will start
289 // at the command line and scroll up as needed, using tl_toprow.
Bram Moolenaar13568252018-03-16 20:46:58 +0100290 term->tl_rows = Rows;
291 term->tl_cols = Columns;
Bram Moolenaar07b46af2018-04-10 14:56:18 +0200292 return;
Bram Moolenaar13568252018-03-16 20:46:58 +0100293 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100294#endif
Bram Moolenaarb936b792020-09-04 18:34:09 +0200295 term->tl_rows = curwin->w_height;
296 term->tl_cols = curwin->w_width;
297
298 minsize = parse_termwinsize(curwin, &rows, &cols);
299 if (minsize)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200300 {
Bram Moolenaarb936b792020-09-04 18:34:09 +0200301 if (term->tl_rows < rows)
302 term->tl_rows = rows;
303 if (term->tl_cols < cols)
304 term->tl_cols = cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200305 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200306 if ((opt->jo_set2 & JO2_TERM_ROWS))
307 term->tl_rows = opt->jo_term_rows;
308 else if (rows != 0)
309 term->tl_rows = rows;
310 if ((opt->jo_set2 & JO2_TERM_COLS))
311 term->tl_cols = opt->jo_term_cols;
312 else if (cols != 0)
313 term->tl_cols = cols;
314
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200315 if (!opt->jo_hidden)
Bram Moolenaarb936b792020-09-04 18:34:09 +0200316 {
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200317 if (term->tl_rows != curwin->w_height)
318 win_setheight_win(term->tl_rows, curwin);
319 if (term->tl_cols != curwin->w_width)
320 win_setwidth_win(term->tl_cols, curwin);
Bram Moolenaarb936b792020-09-04 18:34:09 +0200321
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200322 // Set 'winsize' now to avoid a resize at the next redraw.
323 if (!minsize && *curwin->w_p_tws != NUL)
324 {
325 char_u buf[100];
326
327 vim_snprintf((char *)buf, 100, "%dx%d",
328 term->tl_rows, term->tl_cols);
329 set_option_value((char_u *)"termwinsize", 0L, buf, OPT_LOCAL);
330 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200331 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200332}
333
334/*
335 * Initialize job options for a terminal job.
336 * Caller may overrule some of them.
337 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100338 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200339init_job_options(jobopt_T *opt)
340{
341 clear_job_options(opt);
342
343 opt->jo_mode = MODE_RAW;
344 opt->jo_out_mode = MODE_RAW;
345 opt->jo_err_mode = MODE_RAW;
346 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
347}
348
349/*
350 * Set job options mandatory for a terminal job.
351 */
352 static void
353setup_job_options(jobopt_T *opt, int rows, int cols)
354{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100355#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100356 // Win32: Redirecting the job output won't work, thus always connect stdout
357 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200358 if (!(opt->jo_set & JO_OUT_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200359#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200360 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100361 // Connect stdout to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200362 opt->jo_io[PART_OUT] = JIO_BUFFER;
363 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
364 opt->jo_modifiable[PART_OUT] = 0;
365 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
366 }
367
Bram Moolenaar4f974752019-02-17 17:44:42 +0100368#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100369 // Win32: Redirecting the job output won't work, thus always connect stderr
370 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200371 if (!(opt->jo_set & JO_ERR_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200372#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200373 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100374 // Connect stderr to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200375 opt->jo_io[PART_ERR] = JIO_BUFFER;
376 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
377 opt->jo_modifiable[PART_ERR] = 0;
378 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
379 }
380
381 opt->jo_pty = TRUE;
382 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
383 opt->jo_term_rows = rows;
384 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
385 opt->jo_term_cols = cols;
386}
387
388/*
Bram Moolenaar5c381eb2019-06-25 06:50:31 +0200389 * Flush messages on channels.
390 */
391 static void
392term_flush_messages()
393{
394 mch_check_messages();
395 parse_queued_messages();
396}
397
398/*
Bram Moolenaard96ff162018-02-18 22:13:29 +0100399 * Close a terminal buffer (and its window). Used when creating the terminal
400 * fails.
401 */
402 static void
403term_close_buffer(buf_T *buf, buf_T *old_curbuf)
404{
405 free_terminal(buf);
406 if (old_curbuf != NULL)
407 {
408 --curbuf->b_nwindows;
409 curbuf = old_curbuf;
410 curwin->w_buffer = curbuf;
411 ++curbuf->b_nwindows;
412 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100413 CHECK_CURBUF;
Bram Moolenaard96ff162018-02-18 22:13:29 +0100414
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100415 // Wiping out the buffer will also close the window and call
416 // free_terminal().
Bram Moolenaard96ff162018-02-18 22:13:29 +0100417 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
418}
419
420/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200421 * Start a terminal window and return its buffer.
Bram Moolenaar13568252018-03-16 20:46:58 +0100422 * Use either "argvar" or "argv", the other must be NULL.
423 * When "flags" has TERM_START_NOJOB only create the buffer, b_term and open
424 * the window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200425 * Returns NULL when failed.
426 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100427 buf_T *
428term_start(
429 typval_T *argvar,
430 char **argv,
431 jobopt_T *opt,
432 int flags)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200433{
434 exarg_T split_ea;
435 win_T *old_curwin = curwin;
436 term_T *term;
437 buf_T *old_curbuf = NULL;
438 int res;
439 buf_T *newbuf;
Bram Moolenaare1004402020-10-24 20:49:43 +0200440 int vertical = opt->jo_vertical || (cmdmod.cmod_split & WSP_VERT);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200441 jobopt_T orig_opt; // only partly filled
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200442
443 if (check_restricted() || check_secure())
444 return NULL;
Bram Moolenaare5b44862021-05-30 13:54:03 +0200445#ifdef FEAT_CMDWIN
446 if (cmdwin_type != 0)
447 {
448 emsg(_(e_cannot_open_terminal_from_command_line_window));
449 return NULL;
450 }
451#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200452
453 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
454 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
455 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
Bram Moolenaarb0992022020-01-30 14:55:42 +0100456 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))
457 || (argvar != NULL
458 && argvar->v_type == VAR_LIST
459 && argvar->vval.v_list != NULL
460 && argvar->vval.v_list->lv_first == &range_list_item))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100462 emsg(_(e_invarg));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200463 return NULL;
464 }
465
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200466 term = ALLOC_CLEAR_ONE(term_T);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200467 if (term == NULL)
468 return NULL;
469 term->tl_dirty_row_end = MAX_ROW;
470 term->tl_cursor_visible = TRUE;
471 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
472 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100473#ifdef FEAT_GUI
474 term->tl_system = (flags & TERM_START_SYSTEM);
475#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200476 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100477 ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200478 ga_init2(&term->tl_osc_buf, sizeof(char), 300);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200479
Bram Moolenaaraeed2a62021-04-29 20:18:45 +0200480 setpcmark();
Bram Moolenaara80faa82020-04-12 19:37:17 +0200481 CLEAR_FIELD(split_ea);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200482 if (opt->jo_curwin)
483 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100484 // Create a new buffer in the current window.
Bram Moolenaar13568252018-03-16 20:46:58 +0100485 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200486 {
487 no_write_message();
488 vim_free(term);
489 return NULL;
490 }
491 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaarb1009092020-05-31 16:04:42 +0200492 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
493 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
494 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200495 {
496 vim_free(term);
497 return NULL;
498 }
499 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100500 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200501 {
502 buf_T *buf;
503
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100504 // Create a new buffer without a window. Make it the current buffer for
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100505 // a moment to be able to do the initializations.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200506 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
507 BLN_NEW | BLN_LISTED);
508 if (buf == NULL || ml_open(buf) == FAIL)
509 {
510 vim_free(term);
511 return NULL;
512 }
513 old_curbuf = curbuf;
514 --curbuf->b_nwindows;
515 curbuf = buf;
516 curwin->w_buffer = buf;
517 ++curbuf->b_nwindows;
518 }
519 else
520 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100521 // Open a new window or tab.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200522 split_ea.cmdidx = CMD_new;
523 split_ea.cmd = (char_u *)"new";
524 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100525 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200526 {
527 split_ea.line2 = opt->jo_term_rows;
528 split_ea.addr_count = 1;
529 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100530 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200531 {
532 split_ea.line2 = opt->jo_term_cols;
533 split_ea.addr_count = 1;
534 }
535
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100536 if (vertical)
Bram Moolenaare1004402020-10-24 20:49:43 +0200537 cmdmod.cmod_split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200538 ex_splitview(&split_ea);
539 if (curwin == old_curwin)
540 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100541 // split failed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200542 vim_free(term);
543 return NULL;
544 }
545 }
546 term->tl_buffer = curbuf;
547 curbuf->b_term = term;
548
549 if (!opt->jo_hidden)
550 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100551 // Only one size was taken care of with :new, do the other one. With
552 // "curwin" both need to be done.
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100553 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200554 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100555 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200556 win_setwidth(opt->jo_term_cols);
557 }
558
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100559 // Link the new terminal in the list of active terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200560 term->tl_next = first_term;
561 first_term = term;
562
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100563 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
564
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200565 if (opt->jo_term_name != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100566 {
567 vim_free(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200568 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100569 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100570 else if (argv != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100571 {
572 vim_free(curbuf->b_ffname);
Bram Moolenaar13568252018-03-16 20:46:58 +0100573 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100574 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200575 else
576 {
577 int i;
578 size_t len;
579 char_u *cmd, *p;
580
581 if (argvar->v_type == VAR_STRING)
582 {
583 cmd = argvar->vval.v_string;
584 if (cmd == NULL)
585 cmd = (char_u *)"";
586 else if (STRCMP(cmd, "NONE") == 0)
587 cmd = (char_u *)"pty";
588 }
589 else if (argvar->v_type != VAR_LIST
590 || argvar->vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +0100591 || argvar->vval.v_list->lv_len == 0
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100592 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200593 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
594 cmd = (char_u*)"";
595
596 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200597 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200598
599 for (i = 0; p != NULL; ++i)
600 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100601 // Prepend a ! to the command name to avoid the buffer name equals
602 // the executable, otherwise ":w!" would overwrite it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200603 if (i == 0)
604 vim_snprintf((char *)p, len, "!%s", cmd);
605 else
606 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
607 if (buflist_findname(p) == NULL)
608 {
609 vim_free(curbuf->b_ffname);
610 curbuf->b_ffname = p;
611 break;
612 }
613 }
614 }
Bram Moolenaare010c722020-02-24 21:37:54 +0100615 vim_free(curbuf->b_sfname);
616 curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200617 curbuf->b_fname = curbuf->b_ffname;
618
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100619 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
620
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200621 if (opt->jo_term_opencmd != NULL)
622 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
623
624 if (opt->jo_eof_chars != NULL)
625 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
626
627 set_string_option_direct((char_u *)"buftype", -1,
628 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200629 // Avoid that 'buftype' is reset when this buffer is entered.
630 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200631
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100632 // Mark the buffer as not modifiable. It can only be made modifiable after
633 // the job finished.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200634 curbuf->b_p_ma = FALSE;
635
Bram Moolenaarb936b792020-09-04 18:34:09 +0200636 set_term_and_win_size(term, opt);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100637#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200638 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
639#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200640 setup_job_options(opt, term->tl_rows, term->tl_cols);
641
Bram Moolenaar13568252018-03-16 20:46:58 +0100642 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100643 return curbuf;
644
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100645#if defined(FEAT_SESSION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100646 // Remember the command for the session file.
Bram Moolenaar13568252018-03-16 20:46:58 +0100647 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100648 term->tl_command = vim_strsave((char_u *)"NONE");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100649 else if (argvar->v_type == VAR_STRING)
650 {
651 char_u *cmd = argvar->vval.v_string;
652
653 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
654 term->tl_command = vim_strsave(cmd);
655 }
656 else if (argvar->v_type == VAR_LIST
657 && argvar->vval.v_list != NULL
658 && argvar->vval.v_list->lv_len > 0)
659 {
660 garray_T ga;
661 listitem_T *item;
662
663 ga_init2(&ga, 1, 100);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200664 FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100665 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100666 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100667 char_u *p;
668
669 if (s == NULL)
670 break;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100671 p = vim_strsave_fnameescape(s, VSE_NONE);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100672 if (p == NULL)
673 break;
674 ga_concat(&ga, p);
675 vim_free(p);
676 ga_append(&ga, ' ');
677 }
678 if (item == NULL)
679 {
680 ga_append(&ga, NUL);
681 term->tl_command = ga.ga_data;
682 }
683 else
684 ga_clear(&ga);
685 }
686#endif
687
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100688 if (opt->jo_term_kill != NULL)
689 {
690 char_u *p = skiptowhite(opt->jo_term_kill);
691
692 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
693 }
694
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200695 if (opt->jo_term_api != NULL)
Bram Moolenaar21109272020-01-30 16:27:20 +0100696 {
697 char_u *p = skiptowhite(opt->jo_term_api);
698
699 term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
700 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200701 else
702 term->tl_api = vim_strsave((char_u *)"Tapi_");
703
Bram Moolenaar83d47902020-03-26 20:34:00 +0100704 if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
705 term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
706
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100707 // System dependent: setup the vterm and maybe start the job in it.
Bram Moolenaar13568252018-03-16 20:46:58 +0100708 if (argv == NULL
709 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200710 && argvar->vval.v_string != NULL
711 && STRCMP(argvar->vval.v_string, "NONE") == 0)
712 res = create_pty_only(term, opt);
713 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200714 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200715
716 newbuf = curbuf;
717 if (res == OK)
718 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100719 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200720 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
721 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100722#ifdef FEAT_GUI
723 if (term->tl_system)
724 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100725 // display first line below typed command
Bram Moolenaar13568252018-03-16 20:46:58 +0100726 term->tl_toprow = msg_row + 1;
727 term->tl_dirty_row_end = 0;
728 }
729#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200730
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100731 // Make sure we don't get stuck on sending keys to the job, it leads to
732 // a deadlock if the job is waiting for Vim to read.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200733 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
734
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200735 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200736 {
737 --curbuf->b_nwindows;
738 curbuf = old_curbuf;
739 curwin->w_buffer = curbuf;
740 ++curbuf->b_nwindows;
741 }
742 }
743 else
744 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100745 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200746 return NULL;
747 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100748
Bram Moolenaar13568252018-03-16 20:46:58 +0100749 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar28ed4df2019-10-26 16:21:40 +0200750 if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
751 apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200752 return newbuf;
753}
754
755/*
756 * ":terminal": open a terminal window and execute a job in it.
757 */
758 void
759ex_terminal(exarg_T *eap)
760{
761 typval_T argvar[2];
762 jobopt_T opt;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100763 int opt_shell = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200764 char_u *cmd;
765 char_u *tofree = NULL;
766
767 init_job_options(&opt);
768
769 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100770 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200771 {
772 char_u *p, *ep;
773
774 cmd += 2;
775 p = skiptowhite(cmd);
776 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200777 if (ep != NULL)
778 {
779 if (ep < p)
780 p = ep;
781 else
782 ep = NULL;
783 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200784
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200785# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
786 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
787 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200788 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200789 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100790 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200791 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200792 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200793 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200794 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200795 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200796 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200797 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100798 opt.jo_term_norestore = 1;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100799 else if (OPTARG_HAS("shell"))
800 opt_shell = TRUE;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200801 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100802 {
803 opt.jo_set2 |= JO2_TERM_KILL;
804 opt.jo_term_kill = ep + 1;
805 p = skiptowhite(cmd);
806 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200807 else if (OPTARG_HAS("api"))
808 {
809 opt.jo_set2 |= JO2_TERM_API;
810 if (ep != NULL)
811 {
812 opt.jo_term_api = ep + 1;
813 p = skiptowhite(cmd);
814 }
815 else
816 opt.jo_term_api = NULL;
817 }
818 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200819 {
820 opt.jo_set2 |= JO2_TERM_ROWS;
821 opt.jo_term_rows = atoi((char *)ep + 1);
822 p = skiptowhite(cmd);
823 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200824 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200825 {
826 opt.jo_set2 |= JO2_TERM_COLS;
827 opt.jo_term_cols = atoi((char *)ep + 1);
828 p = skiptowhite(cmd);
829 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200830 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200831 {
832 char_u *buf = NULL;
833 char_u *keys;
834
Bram Moolenaar21109272020-01-30 16:27:20 +0100835 vim_free(opt.jo_eof_chars);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200836 p = skiptowhite(cmd);
837 *p = NUL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200838 keys = replace_termcodes(ep + 1, &buf,
839 REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200840 opt.jo_set2 |= JO2_EOF_CHARS;
841 opt.jo_eof_chars = vim_strsave(keys);
842 vim_free(buf);
843 *p = ' ';
844 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100845#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100846 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
847 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100848 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100849 int tty_type = NUL;
850
851 p = skiptowhite(cmd);
852 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
853 tty_type = 'w';
854 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
855 tty_type = 'c';
856 else
857 {
858 semsg(e_invargval, "type");
859 goto theend;
860 }
861 opt.jo_set2 |= JO2_TTY_TYPE;
862 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100863 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100864#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200865 else
866 {
867 if (*p)
868 *p = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100869 semsg(_("E181: Invalid attribute: %s"), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100870 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200871 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200872# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200873 cmd = skipwhite(p);
874 }
875 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100876 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100877 // Make a copy of 'shell', an autocommand may change the option.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200878 tofree = cmd = vim_strsave(p_sh);
879
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100880 // default to close when the shell exits
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100881 if (opt.jo_term_finish == NUL)
Bram Moolenaare2978022020-04-26 14:47:44 +0200882 opt.jo_term_finish = TL_FINISH_CLOSE;
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100883 }
884
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200885 if (eap->addr_count > 0)
886 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100887 // Write lines from current buffer to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200888 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
889 opt.jo_io[PART_IN] = JIO_BUFFER;
890 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
891 opt.jo_in_top = eap->line1;
892 opt.jo_in_bot = eap->line2;
893 }
894
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100895 if (opt_shell && tofree == NULL)
896 {
897#ifdef UNIX
898 char **argv = NULL;
899 char_u *tofree1 = NULL;
900 char_u *tofree2 = NULL;
901
902 // :term ++shell command
903 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
904 term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaaradf4aa22019-11-10 22:36:44 +0100905 vim_free(argv);
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100906 vim_free(tofree1);
907 vim_free(tofree2);
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100908 goto theend;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100909#else
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100910# ifdef MSWIN
911 long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
912 char_u *newcmd;
913
914 newcmd = alloc(cmdlen);
915 if (newcmd == NULL)
916 goto theend;
917 tofree = newcmd;
918 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
919 cmd = newcmd;
920# else
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100921 emsg(_("E279: Sorry, ++shell is not supported on this system"));
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100922 goto theend;
923# endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100924#endif
925 }
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100926 argvar[0].v_type = VAR_STRING;
927 argvar[0].vval.v_string = cmd;
928 argvar[1].v_type = VAR_UNKNOWN;
929 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100930
931theend:
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100932 vim_free(tofree);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200933 vim_free(opt.jo_eof_chars);
934}
935
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100936#if defined(FEAT_SESSION) || defined(PROTO)
937/*
938 * Write a :terminal command to the session file to restore the terminal in
939 * window "wp".
940 * Return FAIL if writing fails.
941 */
942 int
Bram Moolenaar0e655112020-09-11 20:36:36 +0200943term_write_session(FILE *fd, win_T *wp, hashtab_T *terminal_bufs)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100944{
Bram Moolenaar0e655112020-09-11 20:36:36 +0200945 const int bufnr = wp->w_buffer->b_fnum;
946 term_T *term = wp->w_buffer->b_term;
947
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200948 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200949 {
950 // There are multiple views into this terminal buffer. We don't want to
951 // create the terminal multiple times. If it's the first time, create,
952 // otherwise link to the first buffer.
953 char id_as_str[NUMBUFLEN];
954 hashitem_T *entry;
955
956 vim_snprintf(id_as_str, sizeof(id_as_str), "%d", bufnr);
957
958 entry = hash_find(terminal_bufs, (char_u *)id_as_str);
959 if (!HASHITEM_EMPTY(entry))
960 {
961 // we've already opened this terminal buffer
962 if (fprintf(fd, "execute 'buffer ' . s:term_buf_%d", bufnr) < 0)
963 return FAIL;
964 return put_eol(fd);
965 }
966 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100967
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100968 // Create the terminal and run the command. This is not without
969 // risk, but let's assume the user only creates a session when this
970 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100971 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
972 term->tl_cols, term->tl_rows) < 0)
973 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100974#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100975 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
976 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100977#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100978 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
979 return FAIL;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200980 if (put_eol(fd) != OK)
981 return FAIL;
982
983 if (fprintf(fd, "let s:term_buf_%d = bufnr()", bufnr) < 0)
984 return FAIL;
985
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200986 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200987 {
988 char *hash_key = alloc(NUMBUFLEN);
989
990 vim_snprintf(hash_key, NUMBUFLEN, "%d", bufnr);
991 hash_add(terminal_bufs, (char_u *)hash_key);
992 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100993
994 return put_eol(fd);
995}
996
997/*
998 * Return TRUE if "buf" has a terminal that should be restored.
999 */
1000 int
1001term_should_restore(buf_T *buf)
1002{
1003 term_T *term = buf->b_term;
1004
1005 return term != NULL && (term->tl_command == NULL
1006 || STRCMP(term->tl_command, "NONE") != 0);
1007}
1008#endif
1009
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001010/*
1011 * Free the scrollback buffer for "term".
1012 */
1013 static void
1014free_scrollback(term_T *term)
1015{
1016 int i;
1017
1018 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
1019 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
1020 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001021 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
1022 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
1023 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001024}
1025
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001026
1027// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +02001028static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001029
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001030/*
1031 * Free a terminal and everything it refers to.
1032 * Kills the job if there is one.
1033 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001034 * The actual terminal structure is freed later in free_unused_terminals(),
1035 * because callbacks may wipe out a buffer while the terminal is still
1036 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001037 */
1038 void
1039free_terminal(buf_T *buf)
1040{
1041 term_T *term = buf->b_term;
1042 term_T *tp;
1043
1044 if (term == NULL)
1045 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001046
1047 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001048 if (first_term == term)
1049 first_term = term->tl_next;
1050 else
1051 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
1052 if (tp->tl_next == term)
1053 {
1054 tp->tl_next = term->tl_next;
1055 break;
1056 }
1057
1058 if (term->tl_job != NULL)
1059 {
1060 if (term->tl_job->jv_status != JOB_ENDED
1061 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +01001062 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001063 job_stop(term->tl_job, NULL, "kill");
1064 job_unref(term->tl_job);
1065 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001066 term->tl_next = terminals_to_free;
1067 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001068
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001069 buf->b_term = NULL;
1070 if (in_terminal_loop == term)
1071 in_terminal_loop = NULL;
1072}
1073
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001074 void
1075free_unused_terminals()
1076{
1077 while (terminals_to_free != NULL)
1078 {
1079 term_T *term = terminals_to_free;
1080
1081 terminals_to_free = term->tl_next;
1082
1083 free_scrollback(term);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02001084 ga_clear(&term->tl_osc_buf);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001085
1086 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001087 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001088 vim_free(term->tl_title);
1089#ifdef FEAT_SESSION
1090 vim_free(term->tl_command);
1091#endif
1092 vim_free(term->tl_kill);
1093 vim_free(term->tl_status_text);
1094 vim_free(term->tl_opencmd);
1095 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001096 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001097#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001098 if (term->tl_out_fd != NULL)
1099 fclose(term->tl_out_fd);
1100#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001101 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001102 vim_free(term->tl_cursor_color);
1103 vim_free(term);
1104 }
1105}
1106
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001107/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001108 * Get the part that is connected to the tty. Normally this is PART_IN, but
1109 * when writing buffer lines to the job it can be another. This makes it
1110 * possible to do "1,5term vim -".
1111 */
1112 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001113get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001114{
1115#ifdef UNIX
1116 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1117 int i;
1118
1119 for (i = 0; i < 3; ++i)
1120 {
1121 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1122
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001123 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001124 return parts[i];
1125 }
1126#endif
1127 return PART_IN;
1128}
1129
1130/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001131 * Write job output "msg[len]" to the vterm.
1132 */
1133 static void
Bram Moolenaar36968af2021-11-15 17:13:11 +00001134term_write_job_output(term_T *term, char_u *msg_arg, size_t len_arg)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001135{
Bram Moolenaar36968af2021-11-15 17:13:11 +00001136 char_u *msg = msg_arg;
1137 size_t len = len_arg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001138 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001139 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar36968af2021-11-15 17:13:11 +00001140 size_t limit = term->tl_buffer->b_p_twsl * term->tl_cols * 3;
1141
1142 // Limit the length to 'termwinscroll' * cols * 3 bytes. Keep the text at
1143 // the end.
1144 if (len > limit)
1145 {
1146 char_u *p = msg + len - limit;
1147
1148 p -= (*mb_head_off)(msg, p);
1149 len -= p - msg;
1150 msg = p;
1151 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001152
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001153 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001154
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001155 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001156 if (prevlen != vterm_output_get_buffer_current(vterm))
1157 {
1158 char buf[KEY_BUF_LEN];
1159 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1160
1161 if (curlen > 0)
1162 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1163 (char_u *)buf, (int)curlen, NULL);
1164 }
1165
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001166 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001167 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1168}
1169
1170 static void
1171update_cursor(term_T *term, int redraw)
1172{
1173 if (term->tl_normal_mode)
1174 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001175#ifdef FEAT_GUI
1176 if (term->tl_system)
1177 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1178 term->tl_cursor_pos.col);
1179 else
1180#endif
1181 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001182 if (redraw)
1183 {
1184 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1185 cursor_on();
1186 out_flush();
1187#ifdef FEAT_GUI
1188 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001189 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001190 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001191 gui_mch_flush();
1192 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001193#endif
1194 }
1195}
1196
1197/*
1198 * Invoked when "msg" output from a job was received. Write it to the terminal
1199 * of "buffer".
1200 */
1201 void
1202write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1203{
1204 size_t len = STRLEN(msg);
1205 term_T *term = buffer->b_term;
1206
Bram Moolenaar4f974752019-02-17 17:44:42 +01001207#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001208 // Win32: Cannot redirect output of the job, intercept it here and write to
1209 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001210 if (term->tl_out_fd != NULL)
1211 {
1212 ch_log(channel, "Writing %d bytes to output file", (int)len);
1213 fwrite(msg, len, 1, term->tl_out_fd);
1214 return;
1215 }
1216#endif
1217
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001218 if (term->tl_vterm == NULL)
1219 {
1220 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1221 return;
1222 }
1223 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01001224 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001225 term_write_job_output(term, msg, len);
1226
Bram Moolenaar13568252018-03-16 20:46:58 +01001227#ifdef FEAT_GUI
1228 if (term->tl_system)
1229 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001230 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001231 update_system_term(term);
1232 update_cursor(term, TRUE);
1233 }
1234 else
1235#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001236 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1237 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001238 if (!term->tl_normal_mode)
1239 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001240 // Don't use update_screen() when editing the command line, it gets
1241 // cleared.
1242 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001243 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001244 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001245 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001246 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001247 // update_screen() can be slow, check the terminal wasn't closed
1248 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001249 if (buffer == curbuf && curbuf->b_term != NULL)
1250 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001251 }
1252 else
1253 redraw_after_callback(TRUE);
1254 }
1255}
1256
1257/*
1258 * Send a mouse position and click to the vterm
1259 */
1260 static int
1261term_send_mouse(VTerm *vterm, int button, int pressed)
1262{
1263 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001264 int row = mouse_row - W_WINROW(curwin);
1265 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001266
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001267#ifdef FEAT_PROP_POPUP
1268 if (popup_is_popup(curwin))
1269 {
1270 row -= popup_top_extra(curwin);
1271 col -= popup_left_extra(curwin);
1272 }
1273#endif
1274 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001275 if (button != 0)
1276 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001277 return TRUE;
1278}
1279
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001280static int enter_mouse_col = -1;
1281static int enter_mouse_row = -1;
1282
1283/*
1284 * Handle a mouse click, drag or release.
1285 * Return TRUE when a mouse event is sent to the terminal.
1286 */
1287 static int
1288term_mouse_click(VTerm *vterm, int key)
1289{
1290#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001291 // For modeless selection mouse drag and release events are ignored, unless
1292 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001293 static int ignore_drag_release = TRUE;
1294 VTermMouseState mouse_state;
1295
1296 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1297 if (mouse_state.flags == 0)
1298 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001299 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001300 switch (key)
1301 {
1302 case K_LEFTDRAG:
1303 case K_LEFTRELEASE:
1304 case K_RIGHTDRAG:
1305 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001306 // Ignore drag and release events when the button-down wasn't
1307 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001308 if (ignore_drag_release)
1309 {
1310 int save_mouse_col, save_mouse_row;
1311
1312 if (enter_mouse_col < 0)
1313 break;
1314
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001315 // mouse click in the window gave us focus, handle that
1316 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001317 save_mouse_col = mouse_col;
1318 save_mouse_row = mouse_row;
1319 mouse_col = enter_mouse_col;
1320 mouse_row = enter_mouse_row;
1321 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1322 mouse_col = save_mouse_col;
1323 mouse_row = save_mouse_row;
1324 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001325 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001326 case K_LEFTMOUSE:
1327 case K_RIGHTMOUSE:
1328 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1329 ignore_drag_release = TRUE;
1330 else
1331 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001332 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001333 if (clip_star.available)
1334 {
1335 int button, is_click, is_drag;
1336
1337 button = get_mouse_button(KEY2TERMCAP1(key),
1338 &is_click, &is_drag);
1339 if (mouse_model_popup() && button == MOUSE_LEFT
1340 && (mod_mask & MOD_MASK_SHIFT))
1341 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001342 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001343 button = MOUSE_RIGHT;
1344 mod_mask &= ~MOD_MASK_SHIFT;
1345 }
1346 clip_modeless(button, is_click, is_drag);
1347 }
1348 break;
1349
1350 case K_MIDDLEMOUSE:
1351 if (clip_star.available)
1352 insert_reg('*', TRUE);
1353 break;
1354 }
1355 enter_mouse_col = -1;
1356 return FALSE;
1357 }
1358#endif
1359 enter_mouse_col = -1;
1360
1361 switch (key)
1362 {
1363 case K_LEFTMOUSE:
1364 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1365 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1366 case K_LEFTRELEASE:
1367 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1368 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1369 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1370 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1371 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1372 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1373 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1374 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1375 }
1376 return TRUE;
1377}
1378
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001379/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001380 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1381 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001382 * Return the number of bytes in "buf".
1383 */
1384 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001385term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001386{
1387 VTerm *vterm = term->tl_vterm;
1388 VTermKey key = VTERM_KEY_NONE;
1389 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001390 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001391
1392 switch (c)
1393 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001394 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001395
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001396 // don't use VTERM_KEY_BACKSPACE, it always
1397 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001398 case K_BS: c = term_backspace_char; break;
1399
1400 case ESC: key = VTERM_KEY_ESCAPE; break;
1401 case K_DEL: key = VTERM_KEY_DEL; break;
1402 case K_DOWN: key = VTERM_KEY_DOWN; break;
1403 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1404 key = VTERM_KEY_DOWN; break;
1405 case K_END: key = VTERM_KEY_END; break;
1406 case K_S_END: mod = VTERM_MOD_SHIFT;
1407 key = VTERM_KEY_END; break;
1408 case K_C_END: mod = VTERM_MOD_CTRL;
1409 key = VTERM_KEY_END; break;
1410 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1411 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1412 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1413 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1414 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1415 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1416 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1417 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1418 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1419 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1420 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1421 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1422 case K_HOME: key = VTERM_KEY_HOME; break;
1423 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1424 key = VTERM_KEY_HOME; break;
1425 case K_C_HOME: mod = VTERM_MOD_CTRL;
1426 key = VTERM_KEY_HOME; break;
1427 case K_INS: key = VTERM_KEY_INS; break;
1428 case K_K0: key = VTERM_KEY_KP_0; break;
1429 case K_K1: key = VTERM_KEY_KP_1; break;
1430 case K_K2: key = VTERM_KEY_KP_2; break;
1431 case K_K3: key = VTERM_KEY_KP_3; break;
1432 case K_K4: key = VTERM_KEY_KP_4; break;
1433 case K_K5: key = VTERM_KEY_KP_5; break;
1434 case K_K6: key = VTERM_KEY_KP_6; break;
1435 case K_K7: key = VTERM_KEY_KP_7; break;
1436 case K_K8: key = VTERM_KEY_KP_8; break;
1437 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001438 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001439 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001440 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001441 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001442 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1443 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001444 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1445 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001446 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1447 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001448 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1449 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1450 case K_LEFT: key = VTERM_KEY_LEFT; break;
1451 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1452 key = VTERM_KEY_LEFT; break;
1453 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1454 key = VTERM_KEY_LEFT; break;
1455 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1456 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1457 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1458 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1459 key = VTERM_KEY_RIGHT; break;
1460 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1461 key = VTERM_KEY_RIGHT; break;
1462 case K_UP: key = VTERM_KEY_UP; break;
1463 case K_S_UP: mod = VTERM_MOD_SHIFT;
1464 key = VTERM_KEY_UP; break;
1465 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001466 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1467 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001468
Bram Moolenaara42ad572017-11-16 13:08:04 +01001469 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1470 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001471 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1472 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001473
1474 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001475 case K_LEFTMOUSE_NM:
1476 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001477 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001478 case K_LEFTRELEASE_NM:
1479 case K_MOUSEMOVE:
1480 case K_MIDDLEMOUSE:
1481 case K_MIDDLEDRAG:
1482 case K_MIDDLERELEASE:
1483 case K_RIGHTMOUSE:
1484 case K_RIGHTDRAG:
1485 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1486 return 0;
1487 other = TRUE;
1488 break;
1489
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001490 case K_X1MOUSE: /* TODO */ return 0;
1491 case K_X1DRAG: /* TODO */ return 0;
1492 case K_X1RELEASE: /* TODO */ return 0;
1493 case K_X2MOUSE: /* TODO */ return 0;
1494 case K_X2DRAG: /* TODO */ return 0;
1495 case K_X2RELEASE: /* TODO */ return 0;
1496
1497 case K_IGNORE: return 0;
1498 case K_NOP: return 0;
1499 case K_UNDO: return 0;
1500 case K_HELP: return 0;
1501 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1502 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1503 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1504 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1505 case K_SELECT: return 0;
1506#ifdef FEAT_GUI
1507 case K_VER_SCROLLBAR: return 0;
1508 case K_HOR_SCROLLBAR: return 0;
1509#endif
1510#ifdef FEAT_GUI_TABLINE
1511 case K_TABLINE: return 0;
1512 case K_TABMENU: return 0;
1513#endif
1514#ifdef FEAT_NETBEANS_INTG
1515 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1516#endif
1517#ifdef FEAT_DND
1518 case K_DROP: return 0;
1519#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001520 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001521 case K_PS: vterm_keyboard_start_paste(vterm);
1522 other = TRUE;
1523 break;
1524 case K_PE: vterm_keyboard_end_paste(vterm);
1525 other = TRUE;
1526 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001527 }
1528
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001529 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001530 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001531 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001532 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001533 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001534 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001535 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001536
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001537 /*
1538 * Convert special keys to vterm keys:
1539 * - Write keys to vterm: vterm_keyboard_key()
1540 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001541 */
1542 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001543 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001544 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001545 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001546 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001547 vterm_keyboard_unichar(vterm, c, mod);
1548
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001549 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001550 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1551}
1552
1553/*
1554 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001555 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001556 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001557 */
1558 static int
1559term_job_running_check(term_T *term, int check_job_status)
1560{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001561 // Also consider the job finished when the channel is closed, to avoid a
1562 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001563 if (term != NULL
1564 && term->tl_job != NULL
1565 && channel_is_open(term->tl_job->jv_channel))
1566 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001567 job_T *job = term->tl_job;
1568
1569 // Careful: Checking the job status may invoked callbacks, which close
1570 // the buffer and terminate "term". However, "job" will not be freed
1571 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001572 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001573 job_status(job);
1574 return (job->jv_status == JOB_STARTED
1575 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001576 }
1577 return FALSE;
1578}
1579
1580/*
1581 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001582 */
1583 int
1584term_job_running(term_T *term)
1585{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001586 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001587}
1588
1589/*
1590 * Return TRUE if "term" has an active channel and used ":term NONE".
1591 */
1592 int
1593term_none_open(term_T *term)
1594{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001595 // Also consider the job finished when the channel is closed, to avoid a
1596 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001597 return term != NULL
1598 && term->tl_job != NULL
1599 && channel_is_open(term->tl_job->jv_channel)
1600 && term->tl_job->jv_channel->ch_keep_open;
1601}
1602
1603/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001604 * Used when exiting: kill the job in "buf" if so desired.
1605 * Return OK when the job finished.
1606 * Return FAIL when the job is still running.
1607 */
1608 int
1609term_try_stop_job(buf_T *buf)
1610{
1611 int count;
1612 char *how = (char *)buf->b_term->tl_kill;
1613
1614#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001615 if ((how == NULL || *how == NUL)
1616 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001617 {
1618 char_u buff[DIALOG_MSG_SIZE];
1619 int ret;
1620
Bram Moolenaar00806bc2020-11-05 19:36:38 +01001621 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001622 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1623 if (ret == VIM_YES)
1624 how = "kill";
1625 else if (ret == VIM_CANCEL)
1626 return FAIL;
1627 }
1628#endif
1629 if (how == NULL || *how == NUL)
1630 return FAIL;
1631
1632 job_stop(buf->b_term->tl_job, NULL, how);
1633
Bram Moolenaar9172d232019-01-29 23:06:54 +01001634 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001635 for (count = 0; count < 100; ++count)
1636 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001637 job_T *job;
1638
1639 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001640 if (!buf_valid(buf)
1641 || buf->b_term == NULL
1642 || buf->b_term->tl_job == NULL)
1643 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001644 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001645
Bram Moolenaar9172d232019-01-29 23:06:54 +01001646 // Call job_status() to update jv_status. It may cause the job to be
1647 // cleaned up but it won't be freed.
1648 job_status(job);
1649 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001650 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001651
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001652 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001653 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001654 }
1655 return FAIL;
1656}
1657
1658/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001659 * Add the last line of the scrollback buffer to the buffer in the window.
1660 */
1661 static void
1662add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1663{
1664 buf_T *buf = term->tl_buffer;
1665 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1666 linenr_T lnum = buf->b_ml.ml_line_count;
1667
Bram Moolenaar4f974752019-02-17 17:44:42 +01001668#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001669 if (!enc_utf8 && enc_codepage > 0)
1670 {
1671 WCHAR *ret = NULL;
1672 int length = 0;
1673
1674 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1675 &ret, &length);
1676 if (ret != NULL)
1677 {
1678 WideCharToMultiByte_alloc(enc_codepage, 0,
1679 ret, length, (char **)&text, &len, 0, 0);
1680 vim_free(ret);
1681 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1682 vim_free(text);
1683 }
1684 }
1685 else
1686#endif
1687 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1688 if (empty)
1689 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001690 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001691 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001692 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001693 curbuf = curwin->w_buffer;
1694 }
1695}
1696
1697 static void
1698cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1699{
1700 attr->width = cell->width;
1701 attr->attrs = cell->attrs;
1702 attr->fg = cell->fg;
1703 attr->bg = cell->bg;
1704}
1705
1706 static int
1707equal_celattr(cellattr_T *a, cellattr_T *b)
1708{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001709 // We only compare the RGB colors, ignoring the ANSI index and type.
1710 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001711 return a->fg.red == b->fg.red
1712 && a->fg.green == b->fg.green
1713 && a->fg.blue == b->fg.blue
1714 && a->bg.red == b->bg.red
1715 && a->bg.green == b->bg.green
1716 && a->bg.blue == b->bg.blue;
1717}
1718
Bram Moolenaard96ff162018-02-18 22:13:29 +01001719/*
1720 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1721 * line at this position. Otherwise at the end.
1722 */
1723 static int
1724add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1725{
1726 if (ga_grow(&term->tl_scrollback, 1) == OK)
1727 {
1728 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1729 + term->tl_scrollback.ga_len;
1730
1731 if (lnum > 0)
1732 {
1733 int i;
1734
1735 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1736 {
1737 *line = *(line - 1);
1738 --line;
1739 }
1740 }
1741 line->sb_cols = 0;
1742 line->sb_cells = NULL;
1743 line->sb_fill_attr = *fill_attr;
1744 ++term->tl_scrollback.ga_len;
1745 return OK;
1746 }
1747 return FALSE;
1748}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001749
1750/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001751 * Remove the terminal contents from the scrollback and the buffer.
1752 * Used before adding a new scrollback line or updating the buffer for lines
1753 * displayed in the terminal.
1754 */
1755 static void
1756cleanup_scrollback(term_T *term)
1757{
1758 sb_line_T *line;
1759 garray_T *gap;
1760
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001761 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001762 gap = &term->tl_scrollback;
1763 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1764 && gap->ga_len > 0)
1765 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001766 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001767 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1768 vim_free(line->sb_cells);
1769 --gap->ga_len;
1770 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001771 curbuf = curwin->w_buffer;
1772 if (curbuf == term->tl_buffer)
1773 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001774}
1775
1776/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001777 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001778 */
1779 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001780update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001781{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001782 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001783 int len;
1784 int lines_skipped = 0;
1785 VTermPos pos;
1786 VTermScreenCell cell;
1787 cellattr_T fill_attr, new_fill_attr;
1788 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001789
1790 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1791 "Adding terminal window snapshot to buffer");
1792
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001793 // First remove the lines that were appended before, they might be
1794 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001795 cleanup_scrollback(term);
1796
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001797 screen = vterm_obtain_screen(term->tl_vterm);
1798 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001799 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1800 {
1801 len = 0;
1802 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1803 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1804 && cell.chars[0] != NUL)
1805 {
1806 len = pos.col + 1;
1807 new_fill_attr = term->tl_default_color;
1808 }
1809 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001810 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001811 cell2cellattr(&cell, &new_fill_attr);
1812
1813 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1814 ++lines_skipped;
1815 else
1816 {
1817 while (lines_skipped > 0)
1818 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001819 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001820 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001821 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001822 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001823 }
1824
1825 if (len == 0)
1826 p = NULL;
1827 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001828 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001829 if ((p != NULL || len == 0)
1830 && ga_grow(&term->tl_scrollback, 1) == OK)
1831 {
1832 garray_T ga;
1833 int width;
1834 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1835 + term->tl_scrollback.ga_len;
1836
1837 ga_init2(&ga, 1, 100);
1838 for (pos.col = 0; pos.col < len; pos.col += width)
1839 {
1840 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1841 {
1842 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001843 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001844 if (ga_grow(&ga, 1) == OK)
1845 ga.ga_len += utf_char2bytes(' ',
1846 (char_u *)ga.ga_data + ga.ga_len);
1847 }
1848 else
1849 {
1850 width = cell.width;
1851
1852 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001853 if (width == 2)
1854 // second cell of double-width character has the
1855 // same attributes.
1856 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001857
Bram Moolenaara79fd562018-12-20 20:47:32 +01001858 // Each character can be up to 6 bytes.
1859 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001860 {
1861 int i;
1862 int c;
1863
1864 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1865 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1866 (char_u *)ga.ga_data + ga.ga_len);
1867 }
1868 }
1869 }
1870 line->sb_cols = len;
1871 line->sb_cells = p;
1872 line->sb_fill_attr = new_fill_attr;
1873 fill_attr = new_fill_attr;
1874 ++term->tl_scrollback.ga_len;
1875
1876 if (ga_grow(&ga, 1) == FAIL)
1877 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1878 else
1879 {
1880 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1881 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1882 }
1883 ga_clear(&ga);
1884 }
1885 else
1886 vim_free(p);
1887 }
1888 }
1889
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001890 // Add trailing empty lines.
1891 for (pos.row = term->tl_scrollback.ga_len;
1892 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1893 ++pos.row)
1894 {
1895 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1896 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1897 }
1898
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001899 term->tl_dirty_snapshot = FALSE;
1900#ifdef FEAT_TIMERS
1901 term->tl_timer_set = FALSE;
1902#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001903}
1904
1905/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001906 * Loop over all windows in the current tab, and also curwin, which is not
1907 * encountered when using a terminal in a popup window.
1908 * Return TRUE if "*wp" was set to the next window.
1909 */
1910 static int
1911for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1912{
1913 if (*wp == NULL)
1914 *wp = firstwin;
1915 else if ((*wp)->w_next != NULL)
1916 *wp = (*wp)->w_next;
1917 else if (!*did_curwin)
1918 *wp = curwin;
1919 else
1920 return FALSE;
1921 if (*wp == curwin)
1922 *did_curwin = TRUE;
1923 return TRUE;
1924}
1925
1926/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001927 * If needed, add the current lines of the terminal to scrollback and to the
1928 * buffer. Called after the job has ended and when switching to
1929 * Terminal-Normal mode.
1930 * When "redraw" is TRUE redraw the windows that show the terminal.
1931 */
1932 static void
1933may_move_terminal_to_buffer(term_T *term, int redraw)
1934{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001935 if (term->tl_vterm == NULL)
1936 return;
1937
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001938 // Update the snapshot only if something changes or the buffer does not
1939 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001940 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1941 <= term->tl_scrollback_scrolled)
1942 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001943
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001944 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001945 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1946 &term->tl_default_color.fg, &term->tl_default_color.bg);
1947
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001948 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001949 {
1950 win_T *wp = NULL;
1951 int did_curwin = FALSE;
1952
1953 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001954 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001955 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001956 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001957 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1958 wp->w_cursor.col = 0;
1959 wp->w_valid = 0;
1960 if (wp->w_cursor.lnum >= wp->w_height)
1961 {
1962 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001963
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001964 if (wp->w_topline < min_topline)
1965 wp->w_topline = min_topline;
1966 }
1967 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001968 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001969 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001970 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001971}
1972
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001973#if defined(FEAT_TIMERS) || defined(PROTO)
1974/*
1975 * Check if any terminal timer expired. If so, copy text from the terminal to
1976 * the buffer.
1977 * Return the time until the next timer will expire.
1978 */
1979 int
1980term_check_timers(int next_due_arg, proftime_T *now)
1981{
1982 term_T *term;
1983 int next_due = next_due_arg;
1984
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001985 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001986 {
1987 if (term->tl_timer_set && !term->tl_normal_mode)
1988 {
1989 long this_due = proftime_time_left(&term->tl_timer_due, now);
1990
1991 if (this_due <= 1)
1992 {
1993 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001994 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001995 }
1996 else if (next_due == -1 || next_due > this_due)
1997 next_due = this_due;
1998 }
1999 }
2000
2001 return next_due;
2002}
2003#endif
2004
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002005/*
2006 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
2007 * otherwise end it.
2008 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002009 static void
2010set_terminal_mode(term_T *term, int normal_mode)
2011{
2012 term->tl_normal_mode = normal_mode;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002013 trigger_modechanged();
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002014 if (!normal_mode)
2015 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002016 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002017 if (term->tl_buffer == curbuf)
2018 maketitle();
2019}
2020
2021/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002022 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002023 * Move the vterm contents into the scrollback buffer and free the vterm.
2024 */
2025 static void
2026cleanup_vterm(term_T *term)
2027{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002028 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002029 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002030 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002031 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002032}
2033
2034/*
2035 * Switch from Terminal-Job mode to Terminal-Normal mode.
2036 * Suspends updating the terminal window.
2037 */
2038 static void
2039term_enter_normal_mode(void)
2040{
2041 term_T *term = curbuf->b_term;
2042
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002043 set_terminal_mode(term, TRUE);
2044
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002045 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002046 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002047
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002048 // Move the window cursor to the position of the cursor in the
2049 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002050 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2051 + term->tl_cursor_pos.row + 1;
2052 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002053 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2054 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002055 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002056
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002057 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002058 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2059}
2060
2061/*
2062 * Returns TRUE if the current window contains a terminal and we are in
2063 * Terminal-Normal mode.
2064 */
2065 int
2066term_in_normal_mode(void)
2067{
2068 term_T *term = curbuf->b_term;
2069
2070 return term != NULL && term->tl_normal_mode;
2071}
2072
2073/*
2074 * Switch from Terminal-Normal mode to Terminal-Job mode.
2075 * Restores updating the terminal window.
2076 */
2077 void
2078term_enter_job_mode()
2079{
2080 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002081
2082 set_terminal_mode(term, FALSE);
2083
2084 if (term->tl_channel_closed)
2085 cleanup_vterm(term);
2086 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002087#ifdef FEAT_PROP_POPUP
2088 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002089 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002090#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002091}
2092
2093/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002094 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002095 * Note: while waiting a terminal may be closed and freed if the channel is
2096 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002097 */
2098 static int
2099term_vgetc()
2100{
2101 int c;
2102 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002103 int modify_other_keys =
2104 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002105
2106 State = TERMINAL;
2107 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002108#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002109 ctrl_break_was_pressed = FALSE;
2110#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002111 if (modify_other_keys)
2112 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002113 c = vgetc();
2114 got_int = FALSE;
2115 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002116 if (modify_other_keys)
2117 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002118 return c;
2119}
2120
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002121static int mouse_was_outside = FALSE;
2122
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002123/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002124 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002125 * Return FAIL when the key needs to be handled in Normal mode.
2126 * Return OK when the key was dropped or sent to the terminal.
2127 */
2128 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002129send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002130{
2131 char msg[KEY_BUF_LEN];
2132 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002133 int dragging_outside = FALSE;
2134
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002135 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002136 switch (c)
2137 {
2138 case NUL:
2139 case K_ZERO:
2140 if (typed)
2141 stuffcharReadbuff(c);
2142 return FAIL;
2143
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002144 case K_TABLINE:
2145 stuffcharReadbuff(c);
2146 return FAIL;
2147
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002148 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002149 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002150 return FAIL;
2151
2152 case K_LEFTDRAG:
2153 case K_MIDDLEDRAG:
2154 case K_RIGHTDRAG:
2155 case K_X1DRAG:
2156 case K_X2DRAG:
2157 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002158 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002159 case K_LEFTMOUSE:
2160 case K_LEFTMOUSE_NM:
2161 case K_LEFTRELEASE:
2162 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002163 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002164 case K_MIDDLEMOUSE:
2165 case K_MIDDLERELEASE:
2166 case K_RIGHTMOUSE:
2167 case K_RIGHTRELEASE:
2168 case K_X1MOUSE:
2169 case K_X1RELEASE:
2170 case K_X2MOUSE:
2171 case K_X2RELEASE:
2172
2173 case K_MOUSEUP:
2174 case K_MOUSEDOWN:
2175 case K_MOUSELEFT:
2176 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002177 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002178 int row = mouse_row;
2179 int col = mouse_col;
2180
2181#ifdef FEAT_PROP_POPUP
2182 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002183 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002184 row -= popup_top_extra(curwin);
2185 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002186 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002187#endif
2188 if (row < W_WINROW(curwin)
2189 || row >= (W_WINROW(curwin) + curwin->w_height)
2190 || col < curwin->w_wincol
2191 || col >= W_ENDCOL(curwin)
2192 || dragging_outside)
2193 {
2194 // click or scroll outside the current window or on status
2195 // line or vertical separator
2196 if (typed)
2197 {
2198 stuffcharReadbuff(c);
2199 mouse_was_outside = TRUE;
2200 }
2201 return FAIL;
2202 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002203 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002204 break;
2205
2206 case K_COMMAND:
2207 return do_cmdline(NULL, getcmdkeycmd, NULL, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002208 }
2209 if (typed)
2210 mouse_was_outside = FALSE;
2211
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002212 // Convert the typed key to a sequence of bytes for the job.
2213 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002214 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002215 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002216 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2217 (char_u *)msg, (int)len, NULL);
2218
2219 return OK;
2220}
2221
2222 static void
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002223position_cursor(win_T *wp, VTermPos *pos)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002224{
2225 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2226 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002227#ifdef FEAT_PROP_POPUP
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002228 if (popup_is_popup(wp))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002229 {
Bram Moolenaarf5452692020-11-28 21:56:06 +01002230 wp->w_wrow += popup_top_extra(wp);
2231 wp->w_wcol += popup_left_extra(wp);
Bram Moolenaar6a076442020-11-15 20:32:58 +01002232 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002233 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01002234 else
2235 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002236#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002237 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2238}
2239
2240/*
2241 * Handle CTRL-W "": send register contents to the job.
2242 */
2243 static void
2244term_paste_register(int prev_c UNUSED)
2245{
2246 int c;
2247 list_T *l;
2248 listitem_T *item;
2249 long reglen = 0;
2250 int type;
2251
2252#ifdef FEAT_CMDL_INFO
2253 if (add_to_showcmd(prev_c))
2254 if (add_to_showcmd('"'))
2255 out_flush();
2256#endif
2257 c = term_vgetc();
2258#ifdef FEAT_CMDL_INFO
2259 clear_showcmd();
2260#endif
2261 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002262 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002263 return;
2264
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002265 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002266 if (c == '=' && get_expr_register() != '=')
2267 return;
2268 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002269 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002270 return;
2271
2272 l = (list_T *)get_reg_contents(c, GREG_LIST);
2273 if (l != NULL)
2274 {
2275 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002276 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002277 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002278 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002279#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002280 char_u *tmp = s;
2281
2282 if (!enc_utf8 && enc_codepage > 0)
2283 {
2284 WCHAR *ret = NULL;
2285 int length = 0;
2286
2287 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2288 (int)STRLEN(s), &ret, &length);
2289 if (ret != NULL)
2290 {
2291 WideCharToMultiByte_alloc(CP_UTF8, 0,
2292 ret, length, (char **)&s, &length, 0, 0);
2293 vim_free(ret);
2294 }
2295 }
2296#endif
2297 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2298 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002299#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002300 if (tmp != s)
2301 vim_free(s);
2302#endif
2303
2304 if (item->li_next != NULL || type == MLINE)
2305 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2306 (char_u *)"\r", 1, NULL);
2307 }
2308 list_free(l);
2309 }
2310}
2311
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002312/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002313 * Return TRUE when waiting for a character in the terminal, the cursor of the
2314 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002315 */
2316 int
2317terminal_is_active()
2318{
2319 return in_terminal_loop != NULL;
2320}
2321
Bram Moolenaar83d47902020-03-26 20:34:00 +01002322/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002323 * Return the highight group ID for the terminal and the window.
Bram Moolenaar83d47902020-03-26 20:34:00 +01002324 */
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002325 static int
2326term_get_highlight_id(term_T *term, win_T *wp)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002327{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002328 char_u *name;
2329
2330 if (wp != NULL && *wp->w_p_wcr != NUL)
2331 name = wp->w_p_wcr;
2332 else if (term->tl_highlight_name != NULL)
2333 name = term->tl_highlight_name;
2334 else
2335 name = (char_u*)"Terminal";
2336
2337 return syn_name2id(name);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002338}
2339
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002340#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002341 cursorentry_T *
2342term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2343{
2344 term_T *term = in_terminal_loop;
2345 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002346 int id;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002347 guicolor_T term_fg = INVALCOLOR;
2348 guicolor_T term_bg = INVALCOLOR;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002349
Bram Moolenaara80faa82020-04-12 19:37:17 +02002350 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002351 entry.shape = entry.mshape =
2352 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2353 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2354 SHAPE_BLOCK;
2355 entry.percentage = 20;
2356 if (term->tl_cursor_blink)
2357 {
2358 entry.blinkwait = 700;
2359 entry.blinkon = 400;
2360 entry.blinkoff = 250;
2361 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002362
Bram Moolenaar83d47902020-03-26 20:34:00 +01002363 // The highlight group overrules the defaults.
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002364 id = term_get_highlight_id(term, curwin);
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002365 if (id != 0)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002366 syn_id2colors(id, &term_fg, &term_bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002367 if (term_bg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002368 *fg = term_bg;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002369 else
2370 *fg = gui.back_pixel;
2371
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002372 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002373 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002374 if (term_fg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002375 *bg = term_fg;
2376 else
2377 *bg = gui.norm_pixel;
2378 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002379 else
2380 *bg = color_name2handle(term->tl_cursor_color);
2381 entry.name = "n";
2382 entry.used_for = SHAPE_CURSOR;
2383
2384 return &entry;
2385}
2386#endif
2387
Bram Moolenaard317b382018-02-08 22:33:31 +01002388 static void
2389may_output_cursor_props(void)
2390{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002391 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002392 || last_set_cursor_shape != desired_cursor_shape
2393 || last_set_cursor_blink != desired_cursor_blink)
2394 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002395 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002396 last_set_cursor_shape = desired_cursor_shape;
2397 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002398 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002399 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002400 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002401 ui_cursor_shape_forced(TRUE);
2402 else
2403 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2404 }
2405}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002406
Bram Moolenaard317b382018-02-08 22:33:31 +01002407/*
2408 * Set the cursor color and shape, if not last set to these.
2409 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002410 static void
2411may_set_cursor_props(term_T *term)
2412{
2413#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002414 // For the GUI the cursor properties are obtained with
2415 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002416 if (gui.in_use)
2417 return;
2418#endif
2419 if (in_terminal_loop == term)
2420 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002421 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002422 desired_cursor_shape = term->tl_cursor_shape;
2423 desired_cursor_blink = term->tl_cursor_blink;
2424 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002425 }
2426}
2427
Bram Moolenaard317b382018-02-08 22:33:31 +01002428/*
2429 * Reset the desired cursor properties and restore them when needed.
2430 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002431 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002432prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002433{
2434#ifdef FEAT_GUI
2435 if (gui.in_use)
2436 return;
2437#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002438 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002439 desired_cursor_shape = -1;
2440 desired_cursor_blink = -1;
2441 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002442}
2443
2444/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002445 * Returns TRUE if the current window contains a terminal and we are sending
2446 * keys to the job.
2447 * If "check_job_status" is TRUE update the job status.
2448 */
2449 static int
2450term_use_loop_check(int check_job_status)
2451{
2452 term_T *term = curbuf->b_term;
2453
2454 return term != NULL
2455 && !term->tl_normal_mode
2456 && term->tl_vterm != NULL
2457 && term_job_running_check(term, check_job_status);
2458}
2459
2460/*
2461 * Returns TRUE if the current window contains a terminal and we are sending
2462 * keys to the job.
2463 */
2464 int
2465term_use_loop(void)
2466{
2467 return term_use_loop_check(FALSE);
2468}
2469
2470/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002471 * Called when entering a window with the mouse. If this is a terminal window
2472 * we may want to change state.
2473 */
2474 void
2475term_win_entered()
2476{
2477 term_T *term = curbuf->b_term;
2478
2479 if (term != NULL)
2480 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002481 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002482 {
2483 reset_VIsual_and_resel();
2484 if (State & INSERT)
2485 stop_insert_mode = TRUE;
2486 }
2487 mouse_was_outside = FALSE;
2488 enter_mouse_col = mouse_col;
2489 enter_mouse_row = mouse_row;
2490 }
2491}
2492
2493/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002494 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2495 * Return the Ctrl-key value in that case.
2496 */
2497 static int
2498raw_c_to_ctrl(int c)
2499{
2500 if ((mod_mask & MOD_MASK_CTRL)
2501 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2502 return c & 0x1f;
2503 return c;
2504}
2505
2506/*
2507 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2508 * May set "mod_mask".
2509 */
2510 static int
2511ctrl_to_raw_c(int c)
2512{
2513 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2514 {
2515 mod_mask |= MOD_MASK_CTRL;
2516 return c + '@';
2517 }
2518 return c;
2519}
2520
2521/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002522 * Wait for input and send it to the job.
2523 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2524 * when there is no more typahead.
2525 * Return when the start of a CTRL-W command is typed or anything else that
2526 * should be handled as a Normal mode command.
2527 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2528 * the terminal was closed.
2529 */
2530 int
2531terminal_loop(int blocking)
2532{
2533 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002534 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002535 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002536 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002537#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002538 int tty_fd = curbuf->b_term->tl_job->jv_channel
2539 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002540#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002541 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002542
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002543 // Remember the terminal we are sending keys to. However, the terminal
2544 // might be closed while waiting for a character, e.g. typing "exit" in a
2545 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2546 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002547 in_terminal_loop = curbuf->b_term;
2548
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002549 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002550 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002551 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002552 if (termwinkey == Ctrl_W)
2553 termwinkey = 0;
2554 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002555 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002556 may_set_cursor_props(curbuf->b_term);
2557
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002558 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002559 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002560#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002561 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002562#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002563 // TODO: skip screen update when handling a sequence of keys.
2564 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002565 while (must_redraw != 0)
2566 if (update_screen(0) == FAIL)
2567 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002568 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002569 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002570 break;
2571
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002572 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002573 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002574
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002575 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002576 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002577 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002578 // Job finished while waiting for a character. Push back the
2579 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002580 if (raw_c != K_IGNORE)
2581 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002582 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002583 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002584 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002585 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002586 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002587
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002588#ifdef UNIX
2589 /*
2590 * The shell or another program may change the tty settings. Getting
2591 * them for every typed character is a bit of overhead, but it's needed
2592 * for the first character typed, e.g. when Vim starts in a shell.
2593 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002594 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002595 {
2596 ttyinfo_T info;
2597
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002598 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002599 if (get_tty_info(tty_fd, &info) == OK)
2600 term_backspace_char = info.backspace;
2601 }
2602#endif
2603
Bram Moolenaar4f974752019-02-17 17:44:42 +01002604#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002605 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2606 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002607 if (ctrl_break_was_pressed)
2608 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2609#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002610 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2611 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002612 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002613#ifdef FEAT_GUI
2614 && !curbuf->b_term->tl_system
2615#endif
2616 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002617 {
2618 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002619 int prev_raw_c = raw_c;
2620 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002621
2622#ifdef FEAT_CMDL_INFO
2623 if (add_to_showcmd(c))
2624 out_flush();
2625#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002626 raw_c = term_vgetc();
2627 c = raw_c_to_ctrl(raw_c);
2628
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002629#ifdef FEAT_CMDL_INFO
2630 clear_showcmd();
2631#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002632 if (!term_use_loop_check(TRUE)
2633 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002634 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002635 break;
2636
2637 if (prev_c == Ctrl_BSL)
2638 {
2639 if (c == Ctrl_N)
2640 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002641 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002642 term_enter_normal_mode();
2643 ret = FAIL;
2644 goto theend;
2645 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002646 // Send both keys to the terminal, first one here, second one
2647 // below.
2648 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2649 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002650 }
2651 else if (c == Ctrl_C)
2652 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002653 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002654 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2655 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002656 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002657 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002658 // "CTRL-W .": send CTRL-W to the job
2659 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002660 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002661 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002662 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002663 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002664 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002665 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002666 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002667 else if (c == 'N')
2668 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002669 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002670 term_enter_normal_mode();
2671 ret = FAIL;
2672 goto theend;
2673 }
2674 else if (c == '"')
2675 {
2676 term_paste_register(prev_c);
2677 continue;
2678 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002679 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002680 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002681 // space for CTRL-W, modifier, multi-byte char and NUL
2682 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002683
2684 // Put the command into the typeahead buffer, when using the
2685 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2686 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002687 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002688 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002689 ret = OK;
2690 goto theend;
2691 }
2692 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002693# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002694 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002695 {
2696 WCHAR wc;
2697 char_u mb[3];
2698
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002699 mb[0] = (unsigned)raw_c >> 8;
2700 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002701 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002702 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002703 }
2704# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002705 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002706 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002707 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002708 // We are sure to come back here, don't reset the cursor color
2709 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002710 restore_cursor = FALSE;
2711
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002712 ret = OK;
2713 goto theend;
2714 }
2715 }
2716 ret = FAIL;
2717
2718theend:
2719 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002720 if (restore_cursor)
2721 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002722
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002723 // Move a snapshot of the screen contents to the buffer, so that completion
2724 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002725 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2726 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002727
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002728 return ret;
2729}
2730
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002731 static void
2732may_toggle_cursor(term_T *term)
2733{
2734 if (in_terminal_loop == term)
2735 {
2736 if (term->tl_cursor_visible)
2737 cursor_on();
2738 else
2739 cursor_off();
2740 }
2741}
2742
2743/*
2744 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002745 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002746 */
2747 static int
2748color2index(VTermColor *color, int fg, int *boldp)
2749{
2750 int red = color->red;
2751 int blue = color->blue;
2752 int green = color->green;
2753
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002754 if (VTERM_COLOR_IS_INVALID(color))
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002755 return 0;
2756 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002757 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002758 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002759 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002760 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002761 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002762 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2763 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2764 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002765 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002766 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2767 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2768 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2769 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2770 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2771 case 10: return lookup_color(20, fg, boldp) + 1; // red
2772 case 11: return lookup_color(16, fg, boldp) + 1; // green
2773 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2774 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2775 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2776 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2777 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002778 }
2779 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002780
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002781 if (t_colors >= 256)
2782 {
2783 if (red == blue && red == green)
2784 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002785 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002786 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002787 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2788 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2789 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002790 int i;
2791
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002792 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002793 return 17; // 00/00/00
2794 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002795 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002796 for (i = 0; i < 23; ++i)
2797 if (red < cutoff[i])
2798 return i + 233;
2799 return 256;
2800 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002801 {
2802 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2803 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002804
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002805 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002806 for (ri = 0; ri < 5; ++ri)
2807 if (red < cutoff[ri])
2808 break;
2809 for (gi = 0; gi < 5; ++gi)
2810 if (green < cutoff[gi])
2811 break;
2812 for (bi = 0; bi < 5; ++bi)
2813 if (blue < cutoff[bi])
2814 break;
2815 return 17 + ri * 36 + gi * 6 + bi;
2816 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002817 }
2818 return 0;
2819}
2820
2821/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002822 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002823 */
2824 static int
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002825vtermAttr2hl(VTermScreenCellAttrs *cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002826{
2827 int attr = 0;
2828
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002829 if (cellattrs->bold)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002830 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002831 if (cellattrs->underline)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002832 attr |= HL_UNDERLINE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002833 if (cellattrs->italic)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002834 attr |= HL_ITALIC;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002835 if (cellattrs->strike)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002836 attr |= HL_STRIKETHROUGH;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002837 if (cellattrs->reverse)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002838 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002839 return attr;
2840}
2841
2842/*
2843 * Store Vterm attributes in "cell" from highlight flags.
2844 */
2845 static void
2846hl2vtermAttr(int attr, cellattr_T *cell)
2847{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002848 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002849 if (attr & HL_BOLD)
2850 cell->attrs.bold = 1;
2851 if (attr & HL_UNDERLINE)
2852 cell->attrs.underline = 1;
2853 if (attr & HL_ITALIC)
2854 cell->attrs.italic = 1;
2855 if (attr & HL_STRIKETHROUGH)
2856 cell->attrs.strike = 1;
2857 if (attr & HL_INVERSE)
2858 cell->attrs.reverse = 1;
2859}
2860
2861/*
2862 * Convert the attributes of a vterm cell into an attribute index.
2863 */
2864 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002865cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002866 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002867 win_T *wp,
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002868 VTermScreenCellAttrs *cellattrs,
2869 VTermColor *cellfg,
2870 VTermColor *cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002871{
2872 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002873 VTermColor *fg = cellfg;
2874 VTermColor *bg = cellbg;
2875 int is_default_fg = VTERM_COLOR_IS_DEFAULT_FG(fg);
2876 int is_default_bg = VTERM_COLOR_IS_DEFAULT_BG(bg);
2877
2878 if (is_default_fg || is_default_bg)
2879 {
2880 if (wp != NULL && *wp->w_p_wcr != NUL)
2881 {
2882 if (is_default_fg)
2883 fg = &wp->w_term_wincolor.fg;
2884 if (is_default_bg)
2885 bg = &wp->w_term_wincolor.bg;
2886 }
2887 else
2888 {
2889 if (is_default_fg)
2890 fg = &term->tl_default_color.fg;
2891 if (is_default_bg)
2892 bg = &term->tl_default_color.bg;
2893 }
2894 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002895
2896#ifdef FEAT_GUI
2897 if (gui.in_use)
2898 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002899 guicolor_T guifg = gui_mch_get_rgb_color(fg->red, fg->green, fg->blue);
2900 guicolor_T guibg = gui_mch_get_rgb_color(bg->red, bg->green, bg->blue);
2901 return get_gui_attr_idx(attr, guifg, guibg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002902 }
2903 else
2904#endif
2905#ifdef FEAT_TERMGUICOLORS
2906 if (p_tgc)
2907 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002908 guicolor_T tgcfg = VTERM_COLOR_IS_INVALID(fg)
2909 ? INVALCOLOR
2910 : gui_get_rgb_color_cmn(fg->red, fg->green, fg->blue);
2911 guicolor_T tgcbg = VTERM_COLOR_IS_INVALID(bg)
2912 ? INVALCOLOR
2913 : gui_get_rgb_color_cmn(bg->red, bg->green, bg->blue);
2914 return get_tgc_attr_idx(attr, tgcfg, tgcbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002915 }
2916 else
2917#endif
2918 {
2919 int bold = MAYBE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002920 int ctermfg = color2index(fg, TRUE, &bold);
2921 int ctermbg = color2index(bg, FALSE, &bold);
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002922
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002923 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002924 if (bold == TRUE)
2925 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002926
2927 return get_cterm_attr_idx(attr, ctermfg, ctermbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002928 }
2929 return 0;
2930}
2931
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002932 static void
2933set_dirty_snapshot(term_T *term)
2934{
2935 term->tl_dirty_snapshot = TRUE;
2936#ifdef FEAT_TIMERS
2937 if (!term->tl_normal_mode)
2938 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002939 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002940 profile_setlimit(100L, &term->tl_timer_due);
2941 term->tl_timer_set = TRUE;
2942 }
2943#endif
2944}
2945
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002946 static int
2947handle_damage(VTermRect rect, void *user)
2948{
2949 term_T *term = (term_T *)user;
2950
2951 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2952 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002953 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002954 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002955 return 1;
2956}
2957
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002958 static void
2959term_scroll_up(term_T *term, int start_row, int count)
2960{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002961 win_T *wp = NULL;
2962 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002963 VTermColor fg, bg;
2964 VTermScreenCellAttrs attr;
2965 int clear_attr;
2966
Bram Moolenaara80faa82020-04-12 19:37:17 +02002967 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002968
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002969 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002970 {
2971 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002972 {
2973 // Set the color to clear lines with.
2974 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2975 &fg, &bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002976 clear_attr = cell2attr(term, wp, &attr, &fg, &bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002977 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002978 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002979 }
2980}
2981
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002982 static int
2983handle_moverect(VTermRect dest, VTermRect src, void *user)
2984{
2985 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002986 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002987
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002988 // Scrolling up is done much more efficiently by deleting lines instead of
2989 // redrawing the text. But avoid doing this multiple times, postpone until
2990 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002991 if (dest.start_col == src.start_col
2992 && dest.end_col == src.end_col
2993 && dest.start_row < src.start_row)
2994 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002995 if (dest.start_row == 0)
2996 term->tl_postponed_scroll += count;
2997 else
2998 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002999 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003000
3001 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3002 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003003 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003004
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003005 // Note sure if the scrolling will work correctly, let's do a complete
3006 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003007 redraw_buf_later(term->tl_buffer, NOT_VALID);
3008 return 1;
3009}
3010
3011 static int
3012handle_movecursor(
3013 VTermPos pos,
3014 VTermPos oldpos UNUSED,
3015 int visible,
3016 void *user)
3017{
3018 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003019 win_T *wp = NULL;
3020 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003021
3022 term->tl_cursor_pos = pos;
3023 term->tl_cursor_visible = visible;
3024
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003025 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003026 {
3027 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003028 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003029 }
3030 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003031 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003032
3033 return 1;
3034}
3035
3036 static int
3037handle_settermprop(
3038 VTermProp prop,
3039 VTermValue *value,
3040 void *user)
3041{
3042 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003043 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003044
3045 switch (prop)
3046 {
3047 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003048 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003049 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003050 if (strval == NULL)
3051 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003052 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003053 // a blank title isn't useful, make it empty, so that "running" is
3054 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003055 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003056 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003057 // Same as blank
3058 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003059 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003060 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3061 term->tl_title = NULL;
3062 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003063 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003064 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003065#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003066 else if (!enc_utf8 && enc_codepage > 0)
3067 {
3068 WCHAR *ret = NULL;
3069 int length = 0;
3070
3071 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003072 (char*)value->string.str,
3073 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003074 if (ret != NULL)
3075 {
3076 WideCharToMultiByte_alloc(enc_codepage, 0,
3077 ret, length, (char**)&term->tl_title,
3078 &length, 0, 0);
3079 vim_free(ret);
3080 }
3081 }
3082#endif
3083 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003084 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003085 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003086 strval = NULL;
3087 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003088 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003089 if (term == curbuf->b_term)
3090 maketitle();
3091 break;
3092
3093 case VTERM_PROP_CURSORVISIBLE:
3094 term->tl_cursor_visible = value->boolean;
3095 may_toggle_cursor(term);
3096 out_flush();
3097 break;
3098
3099 case VTERM_PROP_CURSORBLINK:
3100 term->tl_cursor_blink = value->boolean;
3101 may_set_cursor_props(term);
3102 break;
3103
3104 case VTERM_PROP_CURSORSHAPE:
3105 term->tl_cursor_shape = value->number;
3106 may_set_cursor_props(term);
3107 break;
3108
3109 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003110 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003111 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003112 if (strval == NULL)
3113 break;
3114 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003115 may_set_cursor_props(term);
3116 break;
3117
3118 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003119 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003120 term->tl_using_altscreen = value->boolean;
3121 break;
3122
3123 default:
3124 break;
3125 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003126 vim_free(strval);
3127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003128 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003129 return 1;
3130}
3131
3132/*
3133 * The job running in the terminal resized the terminal.
3134 */
3135 static int
3136handle_resize(int rows, int cols, void *user)
3137{
3138 term_T *term = (term_T *)user;
3139 win_T *wp;
3140
3141 term->tl_rows = rows;
3142 term->tl_cols = cols;
3143 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003144 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003145 term->tl_vterm_size_changed = FALSE;
3146 else
3147 {
3148 FOR_ALL_WINDOWS(wp)
3149 {
3150 if (wp->w_buffer == term->tl_buffer)
3151 {
3152 win_setheight_win(rows, wp);
3153 win_setwidth_win(cols, wp);
3154 }
3155 }
3156 redraw_buf_later(term->tl_buffer, NOT_VALID);
3157 }
3158 return 1;
3159}
3160
3161/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003162 * If the number of lines that are stored goes over 'termscrollback' then
3163 * delete the first 10%.
3164 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3165 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003166 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003167 static void
3168limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003169{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003170 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003171 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003172 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003173 int i;
3174
3175 curbuf = term->tl_buffer;
3176 for (i = 0; i < todo; ++i)
3177 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003178 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3179 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003180 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003181 }
3182 curbuf = curwin->w_buffer;
3183
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003184 gap->ga_len -= todo;
3185 mch_memmove(gap->ga_data,
3186 (sb_line_T *)gap->ga_data + todo,
3187 sizeof(sb_line_T) * gap->ga_len);
3188 if (update_buffer)
3189 term->tl_scrollback_scrolled -= todo;
3190 }
3191}
3192
3193/*
3194 * Handle a line that is pushed off the top of the screen.
3195 */
3196 static int
3197handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3198{
3199 term_T *term = (term_T *)user;
3200 garray_T *gap;
3201 int update_buffer;
3202
3203 if (term->tl_normal_mode)
3204 {
3205 // In Terminal-Normal mode the user interacts with the buffer, thus we
3206 // must not change it. Postpone adding the scrollback lines.
3207 gap = &term->tl_scrollback_postponed;
3208 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003209 }
3210 else
3211 {
3212 // First remove the lines that were appended before, the pushed line
3213 // goes above it.
3214 cleanup_scrollback(term);
3215 gap = &term->tl_scrollback;
3216 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003217 }
3218
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003219 limit_scrollback(term, gap, update_buffer);
3220
3221 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003222 {
3223 cellattr_T *p = NULL;
3224 int len = 0;
3225 int i;
3226 int c;
3227 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003228 int text_len;
3229 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003230 sb_line_T *line;
3231 garray_T ga;
3232 cellattr_T fill_attr = term->tl_default_color;
3233
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003234 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003235 for (i = 0; i < cols; ++i)
3236 if (cells[i].chars[0] != 0)
3237 len = i + 1;
3238 else
3239 cell2cellattr(&cells[i], &fill_attr);
3240
3241 ga_init2(&ga, 1, 100);
3242 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003243 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003244 if (p != NULL)
3245 {
3246 for (col = 0; col < len; col += cells[col].width)
3247 {
3248 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3249 {
3250 ga.ga_len = 0;
3251 break;
3252 }
3253 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3254 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3255 (char_u *)ga.ga_data + ga.ga_len);
3256 cell2cellattr(&cells[col], &p[col]);
3257 }
3258 }
3259 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003260 {
3261 if (update_buffer)
3262 text = (char_u *)"";
3263 else
3264 text = vim_strsave((char_u *)"");
3265 text_len = 0;
3266 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003267 else
3268 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003269 text = ga.ga_data;
3270 text_len = ga.ga_len;
3271 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003272 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003273 if (update_buffer)
3274 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003275
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003276 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003277 line->sb_cols = len;
3278 line->sb_cells = p;
3279 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003280 if (update_buffer)
3281 {
3282 line->sb_text = NULL;
3283 ++term->tl_scrollback_scrolled;
3284 ga_clear(&ga); // free the text
3285 }
3286 else
3287 {
3288 line->sb_text = text;
3289 ga_init(&ga); // text is kept in tl_scrollback_postponed
3290 }
3291 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003292 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003293 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003294}
3295
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003296/*
3297 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3298 * received and stored in tl_scrollback_postponed.
3299 */
3300 static void
3301handle_postponed_scrollback(term_T *term)
3302{
3303 int i;
3304
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003305 if (term->tl_scrollback_postponed.ga_len == 0)
3306 return;
3307 ch_log(NULL, "Moving postponed scrollback to scrollback");
3308
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003309 // First remove the lines that were appended before, the pushed lines go
3310 // above it.
3311 cleanup_scrollback(term);
3312
3313 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3314 {
3315 char_u *text;
3316 sb_line_T *pp_line;
3317 sb_line_T *line;
3318
3319 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3320 break;
3321 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3322
3323 text = pp_line->sb_text;
3324 if (text == NULL)
3325 text = (char_u *)"";
3326 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3327 vim_free(pp_line->sb_text);
3328
3329 line = (sb_line_T *)term->tl_scrollback.ga_data
3330 + term->tl_scrollback.ga_len;
3331 line->sb_cols = pp_line->sb_cols;
3332 line->sb_cells = pp_line->sb_cells;
3333 line->sb_fill_attr = pp_line->sb_fill_attr;
3334 line->sb_text = NULL;
3335 ++term->tl_scrollback_scrolled;
3336 ++term->tl_scrollback.ga_len;
3337 }
3338
3339 ga_clear(&term->tl_scrollback_postponed);
3340 limit_scrollback(term, &term->tl_scrollback, TRUE);
3341}
3342
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003343static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003344 handle_damage, // damage
3345 handle_moverect, // moverect
3346 handle_movecursor, // movecursor
3347 handle_settermprop, // settermprop
3348 NULL, // bell
3349 handle_resize, // resize
3350 handle_pushline, // sb_pushline
3351 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003352};
3353
3354/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003355 * Do the work after the channel of a terminal was closed.
3356 * Must be called only when updating_screen is FALSE.
3357 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3358 */
3359 static int
3360term_after_channel_closed(term_T *term)
3361{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003362 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003363 if (!term->tl_normal_mode)
3364 {
3365 int fnum = term->tl_buffer->b_fnum;
3366
3367 cleanup_vterm(term);
3368
3369 if (term->tl_finish == TL_FINISH_CLOSE)
3370 {
3371 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003372 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003373#ifdef FEAT_PROP_POPUP
3374 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003375
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003376 // If this was a terminal in a popup window, go back to the
3377 // previous window.
3378 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3379 {
3380 pwin = curwin;
3381 if (win_valid(prevwin))
3382 win_enter(prevwin, FALSE);
3383 }
3384 else
3385#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003386 // If this is the last normal window: exit Vim.
3387 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3388 {
3389 exarg_T ea;
3390
Bram Moolenaara80faa82020-04-12 19:37:17 +02003391 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003392 ex_quit(&ea);
3393 return TRUE;
3394 }
3395
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003396 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003397 ch_log(NULL, "terminal job finished, closing window");
3398 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003399 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003400 if (curwin == aucmd_win)
3401 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003402 if (do_set_w_closing)
3403 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003404 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003405 if (do_set_w_closing)
3406 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003407 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003408#ifdef FEAT_PROP_POPUP
3409 if (pwin != NULL)
3410 popup_close_with_retval(pwin, 0);
3411#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003412 return TRUE;
3413 }
3414 if (term->tl_finish == TL_FINISH_OPEN
3415 && term->tl_buffer->b_nwindows == 0)
3416 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003417 char *cmd = term->tl_opencmd == NULL
3418 ? "botright sbuf %d"
3419 : (char *)term->tl_opencmd;
3420 size_t len = strlen(cmd) + 50;
3421 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003422
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003423 if (buf != NULL)
3424 {
3425 ch_log(NULL, "terminal job finished, opening window");
3426 vim_snprintf(buf, len, cmd, fnum);
3427 do_cmdline_cmd((char_u *)buf);
3428 vim_free(buf);
3429 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003430 }
3431 else
3432 ch_log(NULL, "terminal job finished");
3433 }
3434
3435 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3436 return FALSE;
3437}
3438
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003439#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3440/*
3441 * If the current window is a terminal in a popup window and the job has
3442 * finished, close the popup window and to back to the previous window.
3443 * Otherwise return FAIL.
3444 */
3445 int
3446may_close_term_popup(void)
3447{
3448 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3449 && !term_job_running(curbuf->b_term))
3450 {
3451 win_T *pwin = curwin;
3452
3453 if (win_valid(prevwin))
3454 win_enter(prevwin, FALSE);
3455 popup_close_with_retval(pwin, 0);
3456 return OK;
3457 }
3458 return FAIL;
3459}
3460#endif
3461
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003462/*
Bram Moolenaareea32af2021-11-21 14:51:13 +00003463 * Called when a channel is going to be closed, before invoking the close
3464 * callback.
3465 */
3466 void
3467term_channel_closing(channel_T *ch)
3468{
3469 term_T *term;
3470
3471 for (term = first_term; term != NULL; term = term->tl_next)
3472 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
3473 term->tl_channel_closing = TRUE;
3474}
3475
3476/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003477 * Called when a channel has been closed.
3478 * If this was a channel for a terminal window then finish it up.
3479 */
3480 void
3481term_channel_closed(channel_T *ch)
3482{
3483 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003484 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003485 int did_one = FALSE;
3486
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003487 for (term = first_term; term != NULL; term = next_term)
3488 {
3489 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003490 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003491 {
3492 term->tl_channel_closed = TRUE;
3493 did_one = TRUE;
3494
Bram Moolenaard23a8232018-02-10 18:45:26 +01003495 VIM_CLEAR(term->tl_title);
3496 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003497#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003498 if (term->tl_out_fd != NULL)
3499 {
3500 fclose(term->tl_out_fd);
3501 term->tl_out_fd = NULL;
3502 }
3503#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003504
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003505 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003506 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003507 // Cannot open or close windows now. Can happen when
3508 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003509 term->tl_channel_recently_closed = TRUE;
3510 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003511 }
3512
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003513 if (term_after_channel_closed(term))
3514 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003515 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003516 }
3517
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003518 if (did_one)
3519 {
3520 redraw_statuslines();
3521
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003522 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003523 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003524 typebuf_was_filled = TRUE;
3525
3526 term = curbuf->b_term;
3527 if (term != NULL)
3528 {
3529 if (term->tl_job == ch->ch_job)
3530 maketitle();
3531 update_cursor(term, term->tl_cursor_visible);
3532 }
3533 }
3534}
3535
3536/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003537 * To be called after resetting updating_screen: handle any terminal where the
3538 * channel was closed.
3539 */
3540 void
3541term_check_channel_closed_recently()
3542{
3543 term_T *term;
3544 term_T *next_term;
3545
3546 for (term = first_term; term != NULL; term = next_term)
3547 {
3548 next_term = term->tl_next;
3549 if (term->tl_channel_recently_closed)
3550 {
3551 term->tl_channel_recently_closed = FALSE;
3552 if (term_after_channel_closed(term))
3553 // start over, the list may have changed
3554 next_term = first_term;
3555 }
3556 }
3557}
3558
3559/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003560 * Fill one screen line from a line of the terminal.
3561 * Advances "pos" to past the last column.
3562 */
3563 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003564term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003565 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003566 win_T *wp,
3567 VTermScreen *screen,
3568 VTermPos *pos,
3569 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003570{
3571 int off = screen_get_current_line_off();
3572
3573 for (pos->col = 0; pos->col < max_col; )
3574 {
3575 VTermScreenCell cell;
3576 int c;
3577
3578 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003579 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003580
3581 c = cell.chars[0];
3582 if (c == NUL)
3583 {
3584 ScreenLines[off] = ' ';
3585 if (enc_utf8)
3586 ScreenLinesUC[off] = NUL;
3587 }
3588 else
3589 {
3590 if (enc_utf8)
3591 {
3592 int i;
3593
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003594 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003595 for (i = 0; i < Screen_mco
3596 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3597 {
3598 ScreenLinesC[i][off] = cell.chars[i + 1];
3599 if (cell.chars[i + 1] == 0)
3600 break;
3601 }
3602 if (c >= 0x80 || (Screen_mco > 0
3603 && ScreenLinesC[0][off] != 0))
3604 {
3605 ScreenLines[off] = ' ';
3606 ScreenLinesUC[off] = c;
3607 }
3608 else
3609 {
3610 ScreenLines[off] = c;
3611 ScreenLinesUC[off] = NUL;
3612 }
3613 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003614#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003615 else if (has_mbyte && c >= 0x80)
3616 {
3617 char_u mb[MB_MAXBYTES+1];
3618 WCHAR wc = c;
3619
3620 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3621 (char*)mb, 2, 0, 0) > 1)
3622 {
3623 ScreenLines[off] = mb[0];
3624 ScreenLines[off + 1] = mb[1];
3625 cell.width = mb_ptr2cells(mb);
3626 }
3627 else
3628 ScreenLines[off] = c;
3629 }
3630#endif
3631 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003632 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003633 ScreenLines[off] = c;
3634 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003635 ScreenAttrs[off] = cell2attr(term, wp, &cell.attrs, &cell.fg,
3636 &cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003637
3638 ++pos->col;
3639 ++off;
3640 if (cell.width == 2)
3641 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003642 // don't set the second byte to NUL for a DBCS encoding, it
3643 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003644 if (enc_utf8)
3645 {
3646 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003647 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003648 }
3649 else if (!has_mbyte)
3650 {
3651 // Can't show a double-width character with a single-byte
3652 // 'encoding', just use a space.
3653 ScreenLines[off] = ' ';
3654 ScreenAttrs[off] = ScreenAttrs[off - 1];
3655 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003656
3657 ++pos->col;
3658 ++off;
3659 }
3660 }
3661}
3662
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003663#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003664 static void
3665update_system_term(term_T *term)
3666{
3667 VTermPos pos;
3668 VTermScreen *screen;
3669
3670 if (term->tl_vterm == NULL)
3671 return;
3672 screen = vterm_obtain_screen(term->tl_vterm);
3673
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003674 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003675 while (term->tl_toprow > 0
3676 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3677 {
3678 int save_p_more = p_more;
3679
3680 p_more = FALSE;
3681 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003682 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003683 p_more = save_p_more;
3684 --term->tl_toprow;
3685 }
3686
3687 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3688 && pos.row < Rows; ++pos.row)
3689 {
3690 if (pos.row < term->tl_rows)
3691 {
3692 int max_col = MIN(Columns, term->tl_cols);
3693
Bram Moolenaar83d47902020-03-26 20:34:00 +01003694 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003695 }
3696 else
3697 pos.col = 0;
3698
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003699 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003700 }
3701
3702 term->tl_dirty_row_start = MAX_ROW;
3703 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003704}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003705#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003706
3707/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003708 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3709 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003710 * Terminal-Normal mode.
3711 */
3712 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003713term_do_update_window(win_T *wp)
3714{
3715 term_T *term = wp->w_buffer->b_term;
3716
3717 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3718}
3719
3720/*
3721 * Called to update a window that contains an active terminal.
3722 */
3723 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003724term_update_window(win_T *wp)
3725{
3726 term_T *term = wp->w_buffer->b_term;
3727 VTerm *vterm;
3728 VTermScreen *screen;
3729 VTermState *state;
3730 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003731 int rows, cols;
3732 int newrows, newcols;
3733 int minsize;
3734 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003735
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003736 vterm = term->tl_vterm;
3737 screen = vterm_obtain_screen(vterm);
3738 state = vterm_obtain_state(vterm);
3739
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003740 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3741 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003742 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003743 {
3744 term->tl_dirty_row_start = 0;
3745 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003746
3747 if (term->tl_postponed_scroll > 0
3748 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003749 // Scrolling is usually faster than redrawing, when there are only
3750 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003751 term_scroll_up(term, 0, term->tl_postponed_scroll);
3752 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003753 }
3754
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003755 /*
3756 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003757 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003758 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003759 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003760
Bram Moolenaar498c2562018-04-15 23:45:15 +02003761 newrows = 99999;
3762 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003763 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003764 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003765 // Always use curwin, it may be a popup window.
3766 win_T *wwp = twp == NULL ? curwin : twp;
3767
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003768 // When more than one window shows the same terminal, use the
3769 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003770 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003771 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003772 newrows = MIN(newrows, wwp->w_height);
3773 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003774 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003775 if (twp == NULL)
3776 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003777 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003778 if (newrows == 99999 || newcols == 99999)
3779 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003780 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3781 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3782
Bram Moolenaareba13e42021-02-23 17:47:23 +01003783 // If no cell is visible there is no point in resizing. Also, vterm can't
3784 // handle a zero height.
3785 if (newrows == 0 || newcols == 0)
3786 return;
3787
Bram Moolenaar498c2562018-04-15 23:45:15 +02003788 if (term->tl_rows != newrows || term->tl_cols != newcols)
3789 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003790 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003791 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003792 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003793 newrows);
3794 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003795
3796 // Updating the terminal size will cause the snapshot to be cleared.
3797 // When not in terminal_loop() we need to restore it.
3798 if (term != in_terminal_loop)
3799 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003800 }
3801
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003802 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003803 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003804 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003805
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003806 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3807 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003808 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003809 if (pos.row < term->tl_rows)
3810 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003811 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003812
Bram Moolenaar83d47902020-03-26 20:34:00 +01003813 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003814 }
3815 else
3816 pos.col = 0;
3817
Bram Moolenaarf118d482018-03-13 13:14:00 +01003818 screen_line(wp->w_winrow + pos.row
3819#ifdef FEAT_MENU
3820 + winbar_height(wp)
3821#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003822 , wp->w_wincol, pos.col, wp->w_width,
3823#ifdef FEAT_PROP_POPUP
3824 popup_is_popup(wp) ? SLF_POPUP :
3825#endif
3826 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003827 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003828 term->tl_dirty_row_start = MAX_ROW;
3829 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003830}
3831
3832/*
3833 * Return TRUE if "wp" is a terminal window where the job has finished.
3834 */
3835 int
3836term_is_finished(buf_T *buf)
3837{
3838 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3839}
3840
3841/*
3842 * Return TRUE if "wp" is a terminal window where the job has finished or we
3843 * are in Terminal-Normal mode, thus we show the buffer contents.
3844 */
3845 int
3846term_show_buffer(buf_T *buf)
3847{
3848 term_T *term = buf->b_term;
3849
3850 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3851}
3852
3853/*
3854 * The current buffer is going to be changed. If there is terminal
3855 * highlighting remove it now.
3856 */
3857 void
3858term_change_in_curbuf(void)
3859{
3860 term_T *term = curbuf->b_term;
3861
3862 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3863 {
3864 free_scrollback(term);
3865 redraw_buf_later(term->tl_buffer, NOT_VALID);
3866
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003867 // The buffer is now like a normal buffer, it cannot be easily
3868 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003869 set_string_option_direct((char_u *)"buftype", -1,
3870 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3871 }
3872}
3873
3874/*
3875 * Get the screen attribute for a position in the buffer.
3876 * Use a negative "col" to get the filler background color.
3877 */
3878 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003879term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003880{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003881 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003882 term_T *term = buf->b_term;
3883 sb_line_T *line;
3884 cellattr_T *cellattr;
3885
3886 if (lnum > term->tl_scrollback.ga_len)
3887 cellattr = &term->tl_default_color;
3888 else
3889 {
3890 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3891 if (col < 0 || col >= line->sb_cols)
3892 cellattr = &line->sb_fill_attr;
3893 else
3894 cellattr = line->sb_cells + col;
3895 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003896 return cell2attr(term, wp, &cellattr->attrs, &cellattr->fg, &cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003897}
3898
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003899/*
3900 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003901 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003902 */
3903 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003904cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003905{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003906 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3907 if (rgb->index == 0)
3908 rgb->type = VTERM_COLOR_RGB;
3909 else
3910 {
3911 rgb->type = VTERM_COLOR_INDEXED;
3912 --rgb->index;
3913 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003914}
3915
3916/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003917 * Initialize vterm color from the synID.
3918 * Returns TRUE if color is set to "fg" and "bg".
3919 * Otherwise returns FALSE.
3920 */
3921 static int
3922get_vterm_color_from_synid(int id, VTermColor *fg, VTermColor *bg)
3923{
3924#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3925 // Use the actual color for the GUI and when 'termguicolors' is set.
3926 if (0
3927# ifdef FEAT_GUI
3928 || gui.in_use
3929# endif
3930# ifdef FEAT_TERMGUICOLORS
3931 || p_tgc
3932# ifdef FEAT_VTP
3933 // Finally get INVALCOLOR on this execution path
3934 || (!p_tgc && t_colors >= 256)
3935# endif
3936# endif
3937 )
3938 {
3939 guicolor_T fg_rgb = INVALCOLOR;
3940 guicolor_T bg_rgb = INVALCOLOR;
3941
3942 if (id > 0)
3943 syn_id2colors(id, &fg_rgb, &bg_rgb);
3944
3945 if (fg_rgb != INVALCOLOR)
3946 {
3947 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3948 fg->red = (unsigned)(rgb >> 16);
3949 fg->green = (unsigned)(rgb >> 8) & 255;
3950 fg->blue = (unsigned)rgb & 255;
3951 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3952 }
3953 else
3954 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
3955
3956 if (bg_rgb != INVALCOLOR)
3957 {
3958 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
3959 bg->red = (unsigned)(rgb >> 16);
3960 bg->green = (unsigned)(rgb >> 8) & 255;
3961 bg->blue = (unsigned)rgb & 255;
3962 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
3963 }
3964 else
3965 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
3966
3967 return TRUE;
3968 }
3969 else
3970#endif
3971 if (t_colors >= 16)
3972 {
3973 int cterm_fg = -1;
3974 int cterm_bg = -1;
3975
3976 if (id > 0)
3977 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
3978
3979 if (cterm_fg >= 0)
3980 {
3981 cterm_color2vterm(cterm_fg, fg);
3982 fg->type |= VTERM_COLOR_DEFAULT_FG;
3983 }
3984 else
3985 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
3986
3987 if (cterm_bg >= 0)
3988 {
3989 cterm_color2vterm(cterm_bg, bg);
3990 bg->type |= VTERM_COLOR_DEFAULT_BG;
3991 }
3992 else
3993 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
3994
3995 return TRUE;
3996 }
3997
3998 return FALSE;
3999}
4000
4001 void
4002term_reset_wincolor(win_T *wp)
4003{
4004 wp->w_term_wincolor.fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4005 wp->w_term_wincolor.bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4006}
4007
4008/*
4009 * Cache the color of 'wincolor'.
4010 */
4011 void
4012term_update_wincolor(win_T *wp)
4013{
4014 int id = 0;
4015
4016 if (*wp->w_p_wcr != NUL)
4017 id = syn_name2id(wp->w_p_wcr);
4018 if (id == 0 || !get_vterm_color_from_synid(id, &wp->w_term_wincolor.fg,
4019 &wp->w_term_wincolor.bg))
4020 term_reset_wincolor(wp);
4021}
4022
4023/*
4024 * Called when option 'termguicolors' was set,
4025 * or when any highlight is changed.
4026 */
4027 void
4028term_update_wincolor_all()
4029{
4030 win_T *wp = NULL;
4031 int did_curwin = FALSE;
4032
4033 while (for_all_windows_and_curwin(&wp, &did_curwin))
4034 term_update_wincolor(wp);
4035}
4036
4037/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004038 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004039 */
4040 static void
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004041init_default_colors(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004042{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004043 VTermColor *fg, *bg;
4044 int fgval, bgval;
4045 int id;
4046
Bram Moolenaara80faa82020-04-12 19:37:17 +02004047 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004048 term->tl_default_color.width = 1;
4049 fg = &term->tl_default_color.fg;
4050 bg = &term->tl_default_color.bg;
4051
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004052 // Vterm uses a default black background. Set it to white when
4053 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004054 if (*p_bg == 'l')
4055 {
4056 fgval = 0;
4057 bgval = 255;
4058 }
4059 else
4060 {
4061 fgval = 255;
4062 bgval = 0;
4063 }
4064 fg->red = fg->green = fg->blue = fgval;
4065 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004066 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
4067 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004068
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004069 // The highlight group overrules the defaults.
4070 id = term_get_highlight_id(term, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004071
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004072 if (!get_vterm_color_from_synid(id, fg, bg))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004073 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004074#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004075 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004076#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004077
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004078 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004079 if (cterm_normal_fg_color > 0)
4080 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004081 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004082# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4083# ifdef VIMDLL
4084 if (!gui.in_use)
4085# endif
4086 {
4087 tmp = fg->red;
4088 fg->red = fg->blue;
4089 fg->blue = tmp;
4090 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004091# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004092 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004093# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004094 else
4095 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004096# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004097
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004098 if (cterm_normal_bg_color > 0)
4099 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004100 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004101# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4102# ifdef VIMDLL
4103 if (!gui.in_use)
4104# endif
4105 {
4106 tmp = fg->red;
4107 fg->red = fg->blue;
4108 fg->blue = tmp;
4109 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004110# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004111 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004112# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004113 else
4114 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004115# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004116 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004117}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004118
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004119#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4120/*
4121 * Set the 16 ANSI colors from array of RGB values
4122 */
4123 static void
4124set_vterm_palette(VTerm *vterm, long_u *rgb)
4125{
4126 int index = 0;
4127 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004128
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004129 for (; index < 16; index++)
4130 {
4131 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004132
Millyb771b6b2021-11-23 12:07:25 +00004133 color.type = VTERM_COLOR_RGB;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004134 color.red = (unsigned)(rgb[index] >> 16);
4135 color.green = (unsigned)(rgb[index] >> 8) & 255;
4136 color.blue = (unsigned)rgb[index] & 255;
4137 vterm_state_set_palette_color(state, index, &color);
4138 }
4139}
4140
4141/*
4142 * Set the ANSI color palette from a list of colors
4143 */
4144 static int
4145set_ansi_colors_list(VTerm *vterm, list_T *list)
4146{
4147 int n = 0;
4148 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004149 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004150
Bram Moolenaarb0992022020-01-30 14:55:42 +01004151 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004152 {
4153 char_u *color_name;
4154 guicolor_T guicolor;
4155
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004156 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004157 if (color_name == NULL)
4158 return FAIL;
4159
4160 guicolor = GUI_GET_COLOR(color_name);
4161 if (guicolor == INVALCOLOR)
4162 return FAIL;
4163
4164 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4165 }
4166
4167 if (n != 16 || li != NULL)
4168 return FAIL;
4169
4170 set_vterm_palette(vterm, rgb);
4171
4172 return OK;
4173}
4174
4175/*
4176 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4177 */
4178 static void
4179init_vterm_ansi_colors(VTerm *vterm)
4180{
4181 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4182
4183 if (var != NULL
4184 && (var->di_tv.v_type != VAR_LIST
4185 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004186 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004187 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004188 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004189}
4190#endif
4191
Bram Moolenaar52acb112018-03-18 19:20:22 +01004192/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004193 * Handles a "drop" command from the job in the terminal.
4194 * "item" is the file name, "item->li_next" may have options.
4195 */
4196 static void
4197handle_drop_command(listitem_T *item)
4198{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004199 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004200 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004201 int bufnr;
4202 win_T *wp;
4203 tabpage_T *tp;
4204 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004205 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004206
4207 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4208 FOR_ALL_TAB_WINDOWS(tp, wp)
4209 {
4210 if (wp->w_buffer->b_fnum == bufnr)
4211 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004212 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004213 goto_tabpage_win(tp, wp);
4214 return;
4215 }
4216 }
4217
Bram Moolenaara80faa82020-04-12 19:37:17 +02004218 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004219
4220 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4221 && opt_item->li_tv.vval.v_dict != NULL)
4222 {
4223 dict_T *dict = opt_item->li_tv.vval.v_dict;
4224 char_u *p;
4225
Bram Moolenaar8f667172018-12-14 15:38:31 +01004226 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004227 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004228 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004229 if (p != NULL)
4230 {
4231 if (check_ff_value(p) == FAIL)
4232 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4233 else
4234 ea.force_ff = *p;
4235 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004236 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004237 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004238 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004239 if (p != NULL)
4240 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004241 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004242 if (ea.cmd != NULL)
4243 {
4244 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4245 ea.force_enc = 11;
4246 tofree = ea.cmd;
4247 }
4248 }
4249
Bram Moolenaar8f667172018-12-14 15:38:31 +01004250 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004251 if (p != NULL)
4252 get_bad_opt(p, &ea);
4253
4254 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4255 ea.force_bin = FORCE_BIN;
4256 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4257 ea.force_bin = FORCE_BIN;
4258 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4259 ea.force_bin = FORCE_NOBIN;
4260 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4261 ea.force_bin = FORCE_NOBIN;
4262 }
4263
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004264 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004265 if (ea.cmd == NULL)
4266 ea.cmd = (char_u *)"split";
4267 ea.arg = fname;
4268 ea.cmdidx = CMD_split;
4269 ex_splitview(&ea);
4270
4271 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004272}
4273
4274/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004275 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4276 */
4277 static int
4278is_permitted_term_api(char_u *func, char_u *pat)
4279{
4280 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4281}
4282
4283/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004284 * Handles a function call from the job running in a terminal.
4285 * "item" is the function name, "item->li_next" has the arguments.
4286 */
4287 static void
4288handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4289{
4290 char_u *func;
4291 typval_T argvars[2];
4292 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004293 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004294
4295 if (item->li_next == NULL)
4296 {
4297 ch_log(channel, "Missing function arguments for call");
4298 return;
4299 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004300 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004301
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004302 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004303 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004304 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004305 return;
4306 }
4307
4308 argvars[0].v_type = VAR_NUMBER;
4309 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4310 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004311 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004312 funcexe.firstline = 1L;
4313 funcexe.lastline = 1L;
4314 funcexe.evaluate = TRUE;
4315 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004316 {
4317 clear_tv(&rettv);
4318 ch_log(channel, "Function %s called", func);
4319 }
4320 else
4321 ch_log(channel, "Calling function %s failed", func);
4322}
4323
4324/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004325 * URL decoding (also know as Percent-encoding).
4326 *
4327 * Note this function currently is only used for decoding shell's
4328 * OSC 7 escape sequence which we can assume all bytes are valid
4329 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4330 * encoding bytes like 0xfe, 0xff.
4331 */
4332 static size_t
4333url_decode(const char *src, const size_t len, char_u *dst)
4334{
4335 size_t i = 0, j = 0;
4336
4337 while (i < len)
4338 {
4339 if (src[i] == '%' && i + 2 < len)
4340 {
4341 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4342 j++;
4343 i += 3;
4344 }
4345 else
4346 {
4347 dst[j] = src[i];
4348 i++;
4349 j++;
4350 }
4351 }
4352 dst[j] = '\0';
4353 return j;
4354}
4355
4356/*
4357 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4358 *
4359 * The OSC 7 sequence has the format of
4360 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4361 * and what VTerm provides via VTermStringFragment is
4362 * "file://HOSTNAME/CURRENT/DIR"
4363 */
4364 static void
4365sync_shell_dir(VTermStringFragment *frag)
4366{
4367 int offset = 7; // len of "file://" is 7
4368 char *pos = (char *)frag->str + offset;
4369 char_u *new_dir;
4370
4371 // remove HOSTNAME to get PWD
Bram Moolenaar918b0892021-05-08 20:09:24 +02004372 while (*pos != '/' && offset < (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004373 {
4374 offset += 1;
4375 pos += 1;
4376 }
4377
Bram Moolenaar918b0892021-05-08 20:09:24 +02004378 if (offset >= (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004379 {
4380 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
4381 frag->str);
4382 return;
4383 }
4384
4385 new_dir = alloc(frag->len - offset + 1);
4386 url_decode(pos, frag->len-offset, new_dir);
4387 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4388 vim_free(new_dir);
4389}
4390
4391/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004392 * Called by libvterm when it cannot recognize an OSC sequence.
4393 * We recognize a terminal API command.
4394 */
4395 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004396parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004397{
4398 term_T *term = (term_T *)user;
4399 js_read_T reader;
4400 typval_T tv;
4401 channel_T *channel = term->tl_job == NULL ? NULL
4402 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004403 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004404
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004405 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
4406 if (p_asd && command == 7)
4407 {
4408 sync_shell_dir(&frag);
4409 return 1;
4410 }
4411
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004412 if (command != 51)
4413 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004414
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004415 // Concatenate what was received until the final piece is found.
4416 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4417 {
4418 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004419 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004420 }
4421 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004422 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004423 if (!frag.final)
4424 return 1;
4425
4426 ((char *)gap->ga_data)[gap->ga_len] = 0;
4427 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004428 reader.js_fill = NULL;
4429 reader.js_used = 0;
4430 if (json_decode(&reader, &tv, 0) == OK
4431 && tv.v_type == VAR_LIST
4432 && tv.vval.v_list != NULL)
4433 {
4434 listitem_T *item = tv.vval.v_list->lv_first;
4435
4436 if (item == NULL)
4437 ch_log(channel, "Missing command");
4438 else
4439 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004440 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004441
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004442 // Make sure an invoked command doesn't delete the buffer (and the
4443 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004444 ++term->tl_buffer->b_locked;
4445
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004446 item = item->li_next;
4447 if (item == NULL)
4448 ch_log(channel, "Missing argument for %s", cmd);
4449 else if (STRCMP(cmd, "drop") == 0)
4450 handle_drop_command(item);
4451 else if (STRCMP(cmd, "call") == 0)
4452 handle_call_command(term, channel, item);
4453 else
4454 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004455 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004456 }
4457 }
4458 else
4459 ch_log(channel, "Invalid JSON received");
4460
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004461 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004462 clear_tv(&tv);
4463 return 1;
4464}
4465
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004466/*
4467 * Called by libvterm when it cannot recognize a CSI sequence.
4468 * We recognize the window position report.
4469 */
4470 static int
4471parse_csi(
4472 const char *leader UNUSED,
4473 const long args[],
4474 int argcount,
4475 const char *intermed UNUSED,
4476 char command,
4477 void *user)
4478{
4479 term_T *term = (term_T *)user;
4480 char buf[100];
4481 int len;
4482 int x = 0;
4483 int y = 0;
4484 win_T *wp;
4485
4486 // We recognize only CSI 13 t
4487 if (command != 't' || argcount != 1 || args[0] != 13)
4488 return 0; // not handled
4489
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004490 // When getting the window position is not possible or it fails it results
4491 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004492#if defined(FEAT_GUI) \
4493 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4494 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004495 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004496#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004497
4498 FOR_ALL_WINDOWS(wp)
4499 if (wp->w_buffer == term->tl_buffer)
4500 break;
4501 if (wp != NULL)
4502 {
4503#ifdef FEAT_GUI
4504 if (gui.in_use)
4505 {
4506 x += wp->w_wincol * gui.char_width;
4507 y += W_WINROW(wp) * gui.char_height;
4508 }
4509 else
4510#endif
4511 {
4512 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004513 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004514 x += wp->w_wincol * 7;
4515 y += W_WINROW(wp) * 10;
4516 }
4517 }
4518
4519 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4520 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4521 (char_u *)buf, len, NULL);
4522 return 1;
4523}
4524
Bram Moolenaard8637282020-05-20 18:41:41 +02004525static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004526 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004527 parse_csi, // csi
4528 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004529 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004530};
4531
4532/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004533 * Use Vim's allocation functions for vterm so profiling works.
4534 */
4535 static void *
4536vterm_malloc(size_t size, void *data UNUSED)
4537{
Bram Moolenaar88137392021-11-12 16:01:15 +00004538 // make sure that the length is not zero
4539 return alloc_clear(size == 0 ? 1L : size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004540}
4541
4542 static void
4543vterm_memfree(void *ptr, void *data UNUSED)
4544{
4545 vim_free(ptr);
4546}
4547
4548static VTermAllocatorFunctions vterm_allocator = {
4549 &vterm_malloc,
4550 &vterm_memfree
4551};
4552
4553/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004554 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004555 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004556 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004557 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004558create_vterm(term_T *term, int rows, int cols)
4559{
4560 VTerm *vterm;
4561 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004562 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004563 VTermValue value;
4564
Bram Moolenaar756ef112018-04-10 12:04:27 +02004565 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004566 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004567 if (vterm == NULL)
4568 return FAIL;
4569
4570 // Allocate screen and state here, so we can bail out if that fails.
4571 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004572 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004573 if (state == NULL || screen == NULL)
4574 {
4575 vterm_free(vterm);
4576 return FAIL;
4577 }
4578
Bram Moolenaar52acb112018-03-18 19:20:22 +01004579 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004580 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004581 vterm_set_utf8(vterm, 1);
4582
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004583 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004584
4585 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004586 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004587 &term->tl_default_color.fg,
4588 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004589
Bram Moolenaar9e587872019-05-13 20:27:23 +02004590 if (t_colors < 16)
4591 // Less than 16 colors: assume that bold means using a bright color for
4592 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004593 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4594
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004595 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004596 vterm_screen_reset(screen, 1 /* hard */);
4597
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004598 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004599 vterm_screen_enable_altscreen(screen, 1);
4600
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004601 // For unix do not use a blinking cursor. In an xterm this causes the
4602 // cursor to blink if it's blinking in the xterm.
4603 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004604#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004605 if (GetCaretBlinkTime() == INFINITE)
4606 value.boolean = 0;
4607 else
4608 value.boolean = 1;
4609#else
4610 value.boolean = 0;
4611#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004612 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004613 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004614
4615 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004616}
4617
4618/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004619 * Called when option 'background' or 'termguicolors' was set,
4620 * or when any highlight is changed.
Bram Moolenaarad431992021-05-03 20:40:38 +02004621 */
4622 void
4623term_update_colors_all(void)
4624{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004625 term_T *term;
Bram Moolenaarad431992021-05-03 20:40:38 +02004626
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004627 FOR_ALL_TERMS(term)
4628 {
4629 if (term->tl_vterm == NULL)
4630 continue;
4631 init_default_colors(term);
4632 vterm_state_set_default_colors(
4633 vterm_obtain_state(term->tl_vterm),
4634 &term->tl_default_color.fg,
4635 &term->tl_default_color.bg);
4636 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004637}
4638
4639/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004640 * Return the text to show for the buffer name and status.
4641 */
4642 char_u *
4643term_get_status_text(term_T *term)
4644{
4645 if (term->tl_status_text == NULL)
4646 {
4647 char_u *txt;
4648 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004649 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004650
4651 if (term->tl_normal_mode)
4652 {
4653 if (term_job_running(term))
4654 txt = (char_u *)_("Terminal");
4655 else
4656 txt = (char_u *)_("Terminal-finished");
4657 }
4658 else if (term->tl_title != NULL)
4659 txt = term->tl_title;
4660 else if (term_none_open(term))
4661 txt = (char_u *)_("active");
4662 else if (term_job_running(term))
4663 txt = (char_u *)_("running");
4664 else
4665 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004666 fname = buf_get_fname(term->tl_buffer);
4667 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004668 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004669 if (term->tl_status_text != NULL)
4670 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004671 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004672 }
4673 return term->tl_status_text;
4674}
4675
4676/*
Bram Moolenaar3ad69532021-11-19 17:01:08 +00004677 * Clear the cached value of the status text.
4678 */
4679 void
4680term_clear_status_text(term_T *term)
4681{
4682 VIM_CLEAR(term->tl_status_text);
4683}
4684
4685/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004686 * Mark references in jobs of terminals.
4687 */
4688 int
4689set_ref_in_term(int copyID)
4690{
4691 int abort = FALSE;
4692 term_T *term;
4693 typval_T tv;
4694
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004695 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004696 if (term->tl_job != NULL)
4697 {
4698 tv.v_type = VAR_JOB;
4699 tv.vval.v_job = term->tl_job;
4700 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4701 }
4702 return abort;
4703}
4704
4705/*
4706 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004707 * Returns NULL when the buffer is not for a terminal window and logs a message
4708 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004709 */
4710 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004711term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004712{
4713 buf_T *buf;
4714
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004715 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004716 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004717 --emsg_off;
4718 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004719 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004720 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004721 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004722 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004723 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004724 return buf;
4725}
4726
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004727 static void
4728clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004729{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004730 CLEAR_FIELD(*cell);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004731 cell->fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4732 cell->bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004733}
4734
4735 static void
4736dump_term_color(FILE *fd, VTermColor *color)
4737{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004738 int index;
4739
4740 if (VTERM_COLOR_IS_INDEXED(color))
4741 index = color->index + 1;
4742 else if (color->type == 0)
4743 // use RGB values
4744 index = 255;
4745 else
4746 // default color
4747 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004748 fprintf(fd, "%02x%02x%02x%d",
4749 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004750 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004751}
4752
4753/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004754 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004755 *
4756 * Each screen cell in full is:
4757 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4758 * {characters} is a space for an empty cell
4759 * For a double-width character "+" is changed to "*" and the next cell is
4760 * skipped.
4761 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4762 * when "&" use the same as the previous cell.
4763 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4764 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4765 * {color-idx} is a number from 0 to 255
4766 *
4767 * Screen cell with same width, attributes and color as the previous one:
4768 * |{characters}
4769 *
4770 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4771 *
4772 * Repeating the previous screen cell:
4773 * @{count}
4774 */
4775 void
4776f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4777{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004778 buf_T *buf;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004779 term_T *term;
4780 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004781 int max_height = 0;
4782 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004783 stat_T st;
4784 FILE *fd;
4785 VTermPos pos;
4786 VTermScreen *screen;
4787 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004788 VTermState *state;
4789 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004790
4791 if (check_restricted() || check_secure())
4792 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004793
4794 if (in_vim9script()
4795 && (check_for_buffer_arg(argvars, 0) == FAIL
4796 || check_for_string_arg(argvars, 1) == FAIL
4797 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4798 return;
4799
4800 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004801 if (buf == NULL)
4802 return;
4803 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004804 if (term->tl_vterm == NULL)
4805 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004806 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004807 return;
4808 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004809
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004810 if (argvars[2].v_type != VAR_UNKNOWN)
4811 {
4812 dict_T *d;
4813
4814 if (argvars[2].v_type != VAR_DICT)
4815 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004816 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004817 return;
4818 }
4819 d = argvars[2].vval.v_dict;
4820 if (d != NULL)
4821 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004822 max_height = dict_get_number(d, (char_u *)"rows");
4823 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004824 }
4825 }
4826
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004827 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004828 if (fname == NULL)
4829 return;
4830 if (mch_stat((char *)fname, &st) >= 0)
4831 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004832 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004833 return;
4834 }
4835
Bram Moolenaard96ff162018-02-18 22:13:29 +01004836 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4837 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004838 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004839 return;
4840 }
4841
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004842 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004843
4844 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004845 state = vterm_obtain_state(term->tl_vterm);
4846 vterm_state_get_cursorpos(state, &cursor_pos);
4847
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004848 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4849 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004850 {
4851 int repeat = 0;
4852
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004853 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4854 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004855 {
4856 VTermScreenCell cell;
4857 int same_attr;
4858 int same_chars = TRUE;
4859 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004860 int is_cursor_pos = (pos.col == cursor_pos.col
4861 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004862
4863 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004864 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004865
4866 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4867 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004868 int c = cell.chars[i];
4869 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004870 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004871
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004872 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004873 if (i == 0)
4874 {
4875 c = (c == NUL) ? ' ' : c;
4876 pc = (pc == NUL) ? ' ' : pc;
4877 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004878 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004879 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004880 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004881 break;
4882 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004883 same_attr = vtermAttr2hl(&cell.attrs)
4884 == vtermAttr2hl(&prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004885 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4886 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004887 if (same_chars && cell.width == prev_cell.width && same_attr
4888 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004889 {
4890 ++repeat;
4891 }
4892 else
4893 {
4894 if (repeat > 0)
4895 {
4896 fprintf(fd, "@%d", repeat);
4897 repeat = 0;
4898 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004899 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004900
4901 if (cell.chars[0] == NUL)
4902 fputs(" ", fd);
4903 else
4904 {
4905 char_u charbuf[10];
4906 int len;
4907
4908 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4909 && cell.chars[i] != NUL; ++i)
4910 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004911 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004912 fwrite(charbuf, len, 1, fd);
4913 }
4914 }
4915
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004916 // When only the characters differ we don't write anything, the
4917 // following "|", "@" or NL will indicate using the same
4918 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004919 if (cell.width != prev_cell.width || !same_attr)
4920 {
4921 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004922 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004923 else
4924 fputs("+", fd);
4925
4926 if (same_attr)
4927 {
4928 fputs("&", fd);
4929 }
4930 else
4931 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004932 fprintf(fd, "%d", vtermAttr2hl(&cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004933 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004934 fputs("&", fd);
4935 else
4936 {
4937 fputs("#", fd);
4938 dump_term_color(fd, &cell.fg);
4939 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004940 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004941 fputs("&", fd);
4942 else
4943 {
4944 fputs("#", fd);
4945 dump_term_color(fd, &cell.bg);
4946 }
4947 }
4948 }
4949
4950 prev_cell = cell;
4951 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004952
4953 if (cell.width == 2)
4954 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004955 }
4956 if (repeat > 0)
4957 fprintf(fd, "@%d", repeat);
4958 fputs("\n", fd);
4959 }
4960
4961 fclose(fd);
4962}
4963
4964/*
4965 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4966 */
4967 static void
4968dump_is_corrupt(garray_T *gap)
4969{
4970 ga_concat(gap, (char_u *)"CORRUPT");
4971}
4972
4973 static void
4974append_cell(garray_T *gap, cellattr_T *cell)
4975{
4976 if (ga_grow(gap, 1) == OK)
4977 {
4978 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4979 ++gap->ga_len;
4980 }
4981}
4982
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004983 static void
4984clear_cellattr(cellattr_T *cell)
4985{
4986 CLEAR_FIELD(*cell);
4987 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4988 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4989}
4990
Bram Moolenaard96ff162018-02-18 22:13:29 +01004991/*
4992 * Read the dump file from "fd" and append lines to the current buffer.
4993 * Return the cell width of the longest line.
4994 */
4995 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004996read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004997{
4998 int c;
4999 garray_T ga_text;
5000 garray_T ga_cell;
5001 char_u *prev_char = NULL;
5002 int attr = 0;
5003 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005004 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005005 term_T *term = curbuf->b_term;
5006 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005007 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005008
5009 ga_init2(&ga_text, 1, 90);
5010 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005011 clear_cellattr(&cell);
5012 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005013 cursor_pos->row = -1;
5014 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005015
5016 c = fgetc(fd);
5017 for (;;)
5018 {
5019 if (c == EOF)
5020 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02005021 if (c == '\r')
5022 {
5023 // DOS line endings? Ignore.
5024 c = fgetc(fd);
5025 }
5026 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005027 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005028 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005029 if (ga_text.ga_data == NULL)
5030 dump_is_corrupt(&ga_text);
5031 if (ga_grow(&term->tl_scrollback, 1) == OK)
5032 {
5033 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
5034 + term->tl_scrollback.ga_len;
5035
5036 if (max_cells < ga_cell.ga_len)
5037 max_cells = ga_cell.ga_len;
5038 line->sb_cols = ga_cell.ga_len;
5039 line->sb_cells = ga_cell.ga_data;
5040 line->sb_fill_attr = term->tl_default_color;
5041 ++term->tl_scrollback.ga_len;
5042 ga_init(&ga_cell);
5043
5044 ga_append(&ga_text, NUL);
5045 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5046 ga_text.ga_len, FALSE);
5047 }
5048 else
5049 ga_clear(&ga_cell);
5050 ga_text.ga_len = 0;
5051
5052 c = fgetc(fd);
5053 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005054 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005055 {
5056 int prev_len = ga_text.ga_len;
5057
Bram Moolenaar9271d052018-02-25 21:39:46 +01005058 if (c == '>')
5059 {
5060 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005061 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005062 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5063 cursor_pos->col = ga_cell.ga_len;
5064 }
5065
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005066 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005067 c = fgetc(fd);
5068 if (c != EOF)
5069 ga_append(&ga_text, c);
5070 for (;;)
5071 {
5072 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005073 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005074 || c == EOF || c == '\n')
5075 break;
5076 ga_append(&ga_text, c);
5077 }
5078
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005079 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005080 vim_free(prev_char);
5081 if (ga_text.ga_data != NULL)
5082 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5083 ga_text.ga_len - prev_len);
5084
Bram Moolenaar9271d052018-02-25 21:39:46 +01005085 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005086 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005087 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005088 }
5089 else if (c == '+' || c == '*')
5090 {
5091 int is_bg;
5092
5093 cell.width = c == '+' ? 1 : 2;
5094
5095 c = fgetc(fd);
5096 if (c == '&')
5097 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005098 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005099 c = fgetc(fd);
5100 }
5101 else if (isdigit(c))
5102 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005103 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005104 attr = 0;
5105 while (isdigit(c))
5106 {
5107 attr = attr * 10 + (c - '0');
5108 c = fgetc(fd);
5109 }
5110 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005111
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005112 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005113 for (is_bg = 0; is_bg <= 1; ++is_bg)
5114 {
5115 if (c == '&')
5116 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005117 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005118 c = fgetc(fd);
5119 }
5120 else if (c == '#')
5121 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005122 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005123
5124 c = fgetc(fd);
5125 red = hex2nr(c);
5126 c = fgetc(fd);
5127 red = (red << 4) + hex2nr(c);
5128 c = fgetc(fd);
5129 green = hex2nr(c);
5130 c = fgetc(fd);
5131 green = (green << 4) + hex2nr(c);
5132 c = fgetc(fd);
5133 blue = hex2nr(c);
5134 c = fgetc(fd);
5135 blue = (blue << 4) + hex2nr(c);
5136 c = fgetc(fd);
5137 if (!isdigit(c))
5138 dump_is_corrupt(&ga_text);
5139 while (isdigit(c))
5140 {
5141 index = index * 10 + (c - '0');
5142 c = fgetc(fd);
5143 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005144 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005145 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005146 type = VTERM_COLOR_RGB;
5147 if (index == 0)
5148 {
5149 if (is_bg)
5150 type |= VTERM_COLOR_DEFAULT_BG;
5151 else
5152 type |= VTERM_COLOR_DEFAULT_FG;
5153 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005154 }
5155 else
5156 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005157 type = VTERM_COLOR_INDEXED;
5158 index -= 1;
5159 }
5160 if (is_bg)
5161 {
5162 cell.bg.type = type;
5163 cell.bg.red = red;
5164 cell.bg.green = green;
5165 cell.bg.blue = blue;
5166 cell.bg.index = index;
5167 }
5168 else
5169 {
5170 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005171 cell.fg.red = red;
5172 cell.fg.green = green;
5173 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005174 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005175 }
5176 }
5177 else
5178 dump_is_corrupt(&ga_text);
5179 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005180 }
5181 else
5182 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005183 }
5184 else
5185 dump_is_corrupt(&ga_text);
5186
5187 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005188 if (cell.width == 2)
5189 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005190 }
5191 else if (c == '@')
5192 {
5193 if (prev_char == NULL)
5194 dump_is_corrupt(&ga_text);
5195 else
5196 {
5197 int count = 0;
5198
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005199 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005200 for (;;)
5201 {
5202 c = fgetc(fd);
5203 if (!isdigit(c))
5204 break;
5205 count = count * 10 + (c - '0');
5206 }
5207
5208 while (count-- > 0)
5209 {
5210 ga_concat(&ga_text, prev_char);
5211 append_cell(&ga_cell, &cell);
5212 }
5213 }
5214 }
5215 else
5216 {
5217 dump_is_corrupt(&ga_text);
5218 c = fgetc(fd);
5219 }
5220 }
5221
5222 if (ga_text.ga_len > 0)
5223 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005224 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005225 dump_is_corrupt(&ga_text);
5226 ga_append(&ga_text, NUL);
5227 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5228 ga_text.ga_len, FALSE);
5229 }
5230
5231 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005232 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005233 vim_free(prev_char);
5234
5235 return max_cells;
5236}
5237
5238/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005239 * Return an allocated string with at least "text_width" "=" characters and
5240 * "fname" inserted in the middle.
5241 */
5242 static char_u *
5243get_separator(int text_width, char_u *fname)
5244{
5245 int width = MAX(text_width, curwin->w_width);
5246 char_u *textline;
5247 int fname_size;
5248 char_u *p = fname;
5249 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005250 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005251
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005252 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005253 if (textline == NULL)
5254 return NULL;
5255
5256 fname_size = vim_strsize(fname);
5257 if (fname_size < width - 8)
5258 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005259 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005260 width = MAX(text_width, fname_size + 8);
5261 }
5262 else if (fname_size > width - 8)
5263 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005264 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005265 p = gettail(fname);
5266 fname_size = vim_strsize(p);
5267 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005268 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005269 while (fname_size > width - 8)
5270 {
5271 p += (*mb_ptr2len)(p);
5272 fname_size = vim_strsize(p);
5273 }
5274
5275 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5276 textline[i] = '=';
5277 textline[i++] = ' ';
5278
5279 STRCPY(textline + i, p);
5280 off = STRLEN(textline);
5281 textline[off] = ' ';
5282 for (i = 1; i < (width - fname_size) / 2; ++i)
5283 textline[off + i] = '=';
5284 textline[off + i] = NUL;
5285
5286 return textline;
5287}
5288
5289/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005290 * Common for "term_dumpdiff()" and "term_dumpload()".
5291 */
5292 static void
5293term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5294{
5295 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005296 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005297 char_u buf1[NUMBUFLEN];
5298 char_u buf2[NUMBUFLEN];
5299 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005300 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005301 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005302 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005303 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005304 char_u *textline = NULL;
5305
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005306 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005307 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005308 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005309 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005310 if (fname1 == NULL || (do_diff && fname2 == NULL))
5311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005312 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005313 return;
5314 }
5315 fd1 = mch_fopen((char *)fname1, READBIN);
5316 if (fd1 == NULL)
5317 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005318 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005319 return;
5320 }
5321 if (do_diff)
5322 {
5323 fd2 = mch_fopen((char *)fname2, READBIN);
5324 if (fd2 == NULL)
5325 {
5326 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005327 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005328 return;
5329 }
5330 }
5331
5332 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005333 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5334 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5335 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5336 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5337 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005338
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005339 if (opt.jo_term_name == NULL)
5340 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005341 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005342
Bram Moolenaar51e14382019-05-25 20:21:28 +02005343 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005344 if (fname_tofree != NULL)
5345 {
5346 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5347 opt.jo_term_name = fname_tofree;
5348 }
5349 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005350
Bram Moolenaar87abab92019-06-03 21:14:59 +02005351 if (opt.jo_bufnr_buf != NULL)
5352 {
5353 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5354
5355 // With "bufnr" argument: enter the window with this buffer and make it
5356 // empty.
5357 if (wp == NULL)
5358 semsg(_(e_invarg2), "bufnr");
5359 else
5360 {
5361 buf = curbuf;
5362 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005363 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005364 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005365 redraw_later(NOT_VALID);
5366 }
5367 }
5368 else
5369 // Create a new terminal window.
5370 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5371
Bram Moolenaard96ff162018-02-18 22:13:29 +01005372 if (buf != NULL && buf->b_term != NULL)
5373 {
5374 int i;
5375 linenr_T bot_lnum;
5376 linenr_T lnum;
5377 term_T *term = buf->b_term;
5378 int width;
5379 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005380 VTermPos cursor_pos1;
5381 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005382
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005383 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005384
Bram Moolenaard96ff162018-02-18 22:13:29 +01005385 rettv->vval.v_number = buf->b_fnum;
5386
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005387 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005388 width = read_dump_file(fd1, &cursor_pos1);
5389
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005390 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005391 if (cursor_pos1.row >= 0)
5392 {
5393 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5394 coladvance(cursor_pos1.col);
5395 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005396
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005397 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005398 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005399
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005400 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005401 if (!do_diff)
5402 goto theend;
5403
5404 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5405
Bram Moolenaar4a696342018-04-05 18:45:26 +02005406 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005407 if (textline == NULL)
5408 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005409 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5410 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5411 vim_free(textline);
5412
5413 textline = get_separator(width, fname2);
5414 if (textline == NULL)
5415 goto theend;
5416 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5417 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005418 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005419
5420 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005421 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005422 if (width2 > width)
5423 {
5424 vim_free(textline);
5425 textline = alloc(width2 + 1);
5426 if (textline == NULL)
5427 goto theend;
5428 width = width2;
5429 textline[width] = NUL;
5430 }
5431 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5432
5433 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5434 {
5435 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5436 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005437 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005438 for (i = 0; i < width; ++i)
5439 textline[i] = '-';
5440 }
5441 else
5442 {
5443 char_u *line1;
5444 char_u *line2;
5445 char_u *p1;
5446 char_u *p2;
5447 int col;
5448 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5449 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5450 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5451 ->sb_cells;
5452
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005453 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005454 line1 = vim_strsave(ml_get(lnum));
5455 if (line1 == NULL)
5456 break;
5457 p1 = line1;
5458
5459 line2 = ml_get(lnum + bot_lnum);
5460 p2 = line2;
5461 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5462 {
5463 int len1 = utfc_ptr2len(p1);
5464 int len2 = utfc_ptr2len(p2);
5465
5466 textline[col] = ' ';
5467 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005468 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005469 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005470 else if (lnum == cursor_pos1.row + 1
5471 && col == cursor_pos1.col
5472 && (cursor_pos1.row != cursor_pos2.row
5473 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005474 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005475 textline[col] = '>';
5476 else if (lnum == cursor_pos2.row + 1
5477 && col == cursor_pos2.col
5478 && (cursor_pos1.row != cursor_pos2.row
5479 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005480 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005481 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005482 else if (cellattr1 != NULL && cellattr2 != NULL)
5483 {
5484 if ((cellattr1 + col)->width
5485 != (cellattr2 + col)->width)
5486 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005487 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005488 &(cellattr2 + col)->fg))
5489 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005490 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005491 &(cellattr2 + col)->bg))
5492 textline[col] = 'b';
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005493 else if (vtermAttr2hl(&(cellattr1 + col)->attrs)
5494 != vtermAttr2hl(&((cellattr2 + col)->attrs)))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005495 textline[col] = 'a';
5496 }
5497 p1 += len1;
5498 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005499 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005500 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005501
5502 while (col < width)
5503 {
5504 if (*p1 == NUL && *p2 == NUL)
5505 textline[col] = '?';
5506 else if (*p1 == NUL)
5507 {
5508 textline[col] = '+';
5509 p2 += utfc_ptr2len(p2);
5510 }
5511 else
5512 {
5513 textline[col] = '-';
5514 p1 += utfc_ptr2len(p1);
5515 }
5516 ++col;
5517 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005518
5519 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005520 }
5521 if (add_empty_scrollback(term, &term->tl_default_color,
5522 term->tl_top_diff_rows) == OK)
5523 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5524 ++bot_lnum;
5525 }
5526
5527 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5528 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005529 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005530 for (i = 0; i < width; ++i)
5531 textline[i] = '+';
5532 if (add_empty_scrollback(term, &term->tl_default_color,
5533 term->tl_top_diff_rows) == OK)
5534 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5535 ++lnum;
5536 ++bot_lnum;
5537 }
5538
5539 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005540
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005541 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005542 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005543 }
5544
5545theend:
5546 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005547 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005548 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005549 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005550 fclose(fd2);
5551}
5552
5553/*
5554 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5555 * bottom files.
5556 * Return FAIL when this is not possible.
5557 */
5558 int
5559term_swap_diff()
5560{
5561 term_T *term = curbuf->b_term;
5562 linenr_T line_count;
5563 linenr_T top_rows;
5564 linenr_T bot_rows;
5565 linenr_T bot_start;
5566 linenr_T lnum;
5567 char_u *p;
5568 sb_line_T *sb_line;
5569
5570 if (term == NULL
5571 || !term_is_finished(curbuf)
5572 || term->tl_top_diff_rows == 0
5573 || term->tl_scrollback.ga_len == 0)
5574 return FAIL;
5575
5576 line_count = curbuf->b_ml.ml_line_count;
5577 top_rows = term->tl_top_diff_rows;
5578 bot_rows = term->tl_bot_diff_rows;
5579 bot_start = line_count - bot_rows;
5580 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5581
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005582 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005583 for (lnum = 1; lnum <= top_rows; ++lnum)
5584 {
5585 p = vim_strsave(ml_get(1));
5586 if (p == NULL)
5587 return OK;
5588 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005589 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005590 vim_free(p);
5591 }
5592
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005593 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005594 for (lnum = 1; lnum <= bot_rows; ++lnum)
5595 {
5596 p = vim_strsave(ml_get(bot_start + lnum));
5597 if (p == NULL)
5598 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005599 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005600 ml_append(lnum - 1, p, 0, FALSE);
5601 vim_free(p);
5602 }
5603
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005604 // move top title to bottom
5605 p = vim_strsave(ml_get(bot_rows + 1));
5606 if (p == NULL)
5607 return OK;
5608 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005609 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005610 vim_free(p);
5611
5612 // move bottom title to top
5613 p = vim_strsave(ml_get(line_count - top_rows));
5614 if (p == NULL)
5615 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005616 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005617 ml_append(bot_rows, p, 0, FALSE);
5618 vim_free(p);
5619
Bram Moolenaard96ff162018-02-18 22:13:29 +01005620 if (top_rows == bot_rows)
5621 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005622 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005623 for (lnum = 0; lnum < top_rows; ++lnum)
5624 {
5625 sb_line_T temp;
5626
5627 temp = *(sb_line + lnum);
5628 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5629 *(sb_line + bot_start + lnum) = temp;
5630 }
5631 }
5632 else
5633 {
5634 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005635 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005636
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005637 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005638 if (temp != NULL)
5639 {
5640 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5641 mch_memmove(term->tl_scrollback.ga_data,
5642 temp + bot_start,
5643 sizeof(sb_line_T) * bot_rows);
5644 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5645 temp + top_rows,
5646 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5647 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5648 + line_count - top_rows,
5649 temp,
5650 sizeof(sb_line_T) * top_rows);
5651 vim_free(temp);
5652 }
5653 }
5654
5655 term->tl_top_diff_rows = bot_rows;
5656 term->tl_bot_diff_rows = top_rows;
5657
5658 update_screen(NOT_VALID);
5659 return OK;
5660}
5661
5662/*
5663 * "term_dumpdiff(filename, filename, options)" function
5664 */
5665 void
5666f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5667{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005668 if (in_vim9script()
5669 && (check_for_string_arg(argvars, 0) == FAIL
5670 || check_for_string_arg(argvars, 1) == FAIL
5671 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5672 return;
5673
Bram Moolenaard96ff162018-02-18 22:13:29 +01005674 term_load_dump(argvars, rettv, TRUE);
5675}
5676
5677/*
5678 * "term_dumpload(filename, options)" function
5679 */
5680 void
5681f_term_dumpload(typval_T *argvars, typval_T *rettv)
5682{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005683 if (in_vim9script()
5684 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005685 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005686 return;
5687
Bram Moolenaard96ff162018-02-18 22:13:29 +01005688 term_load_dump(argvars, rettv, FALSE);
5689}
5690
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005691/*
5692 * "term_getaltscreen(buf)" function
5693 */
5694 void
5695f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5696{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005697 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005698
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005699 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5700 return;
5701
5702 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005703 if (buf == NULL)
5704 return;
5705 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5706}
5707
5708/*
5709 * "term_getattr(attr, name)" function
5710 */
5711 void
5712f_term_getattr(typval_T *argvars, typval_T *rettv)
5713{
5714 int attr;
5715 size_t i;
5716 char_u *name;
5717
5718 static struct {
5719 char *name;
5720 int attr;
5721 } attrs[] = {
5722 {"bold", HL_BOLD},
5723 {"italic", HL_ITALIC},
5724 {"underline", HL_UNDERLINE},
5725 {"strike", HL_STRIKETHROUGH},
5726 {"reverse", HL_INVERSE},
5727 };
5728
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005729 if (in_vim9script()
5730 && (check_for_number_arg(argvars, 0) == FAIL
5731 || check_for_string_arg(argvars, 1) == FAIL))
5732 return;
5733
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005734 attr = tv_get_number(&argvars[0]);
5735 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005736 if (name == NULL)
5737 return;
5738
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005739 if (attr > HL_ALL)
5740 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02005741 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005742 if (STRCMP(name, attrs[i].name) == 0)
5743 {
5744 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5745 break;
5746 }
5747}
5748
5749/*
5750 * "term_getcursor(buf)" function
5751 */
5752 void
5753f_term_getcursor(typval_T *argvars, typval_T *rettv)
5754{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005755 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005756 term_T *term;
5757 list_T *l;
5758 dict_T *d;
5759
5760 if (rettv_list_alloc(rettv) == FAIL)
5761 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005762
5763 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5764 return;
5765
5766 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005767 if (buf == NULL)
5768 return;
5769 term = buf->b_term;
5770
5771 l = rettv->vval.v_list;
5772 list_append_number(l, term->tl_cursor_pos.row + 1);
5773 list_append_number(l, term->tl_cursor_pos.col + 1);
5774
5775 d = dict_alloc();
5776 if (d != NULL)
5777 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005778 dict_add_number(d, "visible", term->tl_cursor_visible);
5779 dict_add_number(d, "blink", blink_state_is_inverted()
5780 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5781 dict_add_number(d, "shape", term->tl_cursor_shape);
5782 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005783 list_append_dict(l, d);
5784 }
5785}
5786
5787/*
5788 * "term_getjob(buf)" function
5789 */
5790 void
5791f_term_getjob(typval_T *argvars, typval_T *rettv)
5792{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005793 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005794
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005795 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5796 return;
5797
5798 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005799 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005800 {
5801 rettv->v_type = VAR_SPECIAL;
5802 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005803 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005804 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005805
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005806 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005807 rettv->vval.v_job = buf->b_term->tl_job;
5808 if (rettv->vval.v_job != NULL)
5809 ++rettv->vval.v_job->jv_refcount;
5810}
5811
5812 static int
5813get_row_number(typval_T *tv, term_T *term)
5814{
5815 if (tv->v_type == VAR_STRING
5816 && tv->vval.v_string != NULL
5817 && STRCMP(tv->vval.v_string, ".") == 0)
5818 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005819 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005820}
5821
5822/*
5823 * "term_getline(buf, row)" function
5824 */
5825 void
5826f_term_getline(typval_T *argvars, typval_T *rettv)
5827{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005828 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005829 term_T *term;
5830 int row;
5831
5832 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005833
5834 if (in_vim9script()
5835 && (check_for_buffer_arg(argvars, 0) == FAIL
5836 || check_for_lnum_arg(argvars, 1) == FAIL))
5837 return;
5838
5839 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005840 if (buf == NULL)
5841 return;
5842 term = buf->b_term;
5843 row = get_row_number(&argvars[1], term);
5844
5845 if (term->tl_vterm == NULL)
5846 {
5847 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5848
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005849 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005850 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5851 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5852 }
5853 else
5854 {
5855 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5856 VTermRect rect;
5857 int len;
5858 char_u *p;
5859
5860 if (row < 0 || row >= term->tl_rows)
5861 return;
5862 len = term->tl_cols * MB_MAXBYTES + 1;
5863 p = alloc(len);
5864 if (p == NULL)
5865 return;
5866 rettv->vval.v_string = p;
5867
5868 rect.start_col = 0;
5869 rect.end_col = term->tl_cols;
5870 rect.start_row = row;
5871 rect.end_row = row + 1;
5872 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5873 }
5874}
5875
5876/*
5877 * "term_getscrolled(buf)" function
5878 */
5879 void
5880f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5881{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005882 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005883
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005884 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5885 return;
5886
5887 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005888 if (buf == NULL)
5889 return;
5890 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5891}
5892
5893/*
5894 * "term_getsize(buf)" function
5895 */
5896 void
5897f_term_getsize(typval_T *argvars, typval_T *rettv)
5898{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005899 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005900 list_T *l;
5901
5902 if (rettv_list_alloc(rettv) == FAIL)
5903 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005904
5905 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5906 return;
5907
5908 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005909 if (buf == NULL)
5910 return;
5911
5912 l = rettv->vval.v_list;
5913 list_append_number(l, buf->b_term->tl_rows);
5914 list_append_number(l, buf->b_term->tl_cols);
5915}
5916
5917/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005918 * "term_setsize(buf, rows, cols)" function
5919 */
5920 void
5921f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5922{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005923 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02005924 term_T *term;
5925 varnumber_T rows, cols;
5926
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005927 if (in_vim9script()
5928 && (check_for_buffer_arg(argvars, 0) == FAIL
5929 || check_for_number_arg(argvars, 1) == FAIL
5930 || check_for_number_arg(argvars, 2) == FAIL))
5931 return;
5932
5933 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005934 if (buf == NULL)
5935 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005936 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005937 return;
5938 }
5939 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005940 return;
5941 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005942 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005943 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005944 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005945 cols = cols <= 0 ? term->tl_cols : cols;
5946 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005947 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005948
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005949 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005950 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5951 term_report_winsize(term, term->tl_rows, term->tl_cols);
5952}
5953
5954/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005955 * "term_getstatus(buf)" function
5956 */
5957 void
5958f_term_getstatus(typval_T *argvars, typval_T *rettv)
5959{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005960 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005961 term_T *term;
5962 char_u val[100];
5963
5964 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005965
5966 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5967 return;
5968
5969 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005970 if (buf == NULL)
5971 return;
5972 term = buf->b_term;
5973
5974 if (term_job_running(term))
5975 STRCPY(val, "running");
5976 else
5977 STRCPY(val, "finished");
5978 if (term->tl_normal_mode)
5979 STRCAT(val, ",normal");
5980 rettv->vval.v_string = vim_strsave(val);
5981}
5982
5983/*
5984 * "term_gettitle(buf)" function
5985 */
5986 void
5987f_term_gettitle(typval_T *argvars, typval_T *rettv)
5988{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005989 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005990
5991 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005992
5993 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5994 return;
5995
5996 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005997 if (buf == NULL)
5998 return;
5999
6000 if (buf->b_term->tl_title != NULL)
6001 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
6002}
6003
6004/*
6005 * "term_gettty(buf)" function
6006 */
6007 void
6008f_term_gettty(typval_T *argvars, typval_T *rettv)
6009{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006010 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02006011 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006012 int num = 0;
6013
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006014 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006015 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006016 || check_for_opt_bool_arg(argvars, 1) == FAIL))
6017 return;
6018
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006019 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006020 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006021 if (buf == NULL)
6022 return;
6023 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02006024 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006025
6026 switch (num)
6027 {
6028 case 0:
6029 if (buf->b_term->tl_job != NULL)
6030 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006031 break;
6032 case 1:
6033 if (buf->b_term->tl_job != NULL)
6034 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006035 break;
6036 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006037 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006038 return;
6039 }
6040 if (p != NULL)
6041 rettv->vval.v_string = vim_strsave(p);
6042}
6043
6044/*
6045 * "term_list()" function
6046 */
6047 void
6048f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
6049{
6050 term_T *tp;
6051 list_T *l;
6052
6053 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
6054 return;
6055
6056 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006057 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02006058 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006059 if (list_append_number(l,
6060 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6061 return;
6062}
6063
6064/*
6065 * "term_scrape(buf, row)" function
6066 */
6067 void
6068f_term_scrape(typval_T *argvars, typval_T *rettv)
6069{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006070 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006071 VTermScreen *screen = NULL;
6072 VTermPos pos;
6073 list_T *l;
6074 term_T *term;
6075 char_u *p;
6076 sb_line_T *line;
6077
6078 if (rettv_list_alloc(rettv) == FAIL)
6079 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006080
6081 if (in_vim9script()
6082 && (check_for_buffer_arg(argvars, 0) == FAIL
6083 || check_for_lnum_arg(argvars, 1) == FAIL))
6084 return;
6085
6086 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006087 if (buf == NULL)
6088 return;
6089 term = buf->b_term;
6090
6091 l = rettv->vval.v_list;
6092 pos.row = get_row_number(&argvars[1], term);
6093
6094 if (term->tl_vterm != NULL)
6095 {
6096 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006097 if (screen == NULL) // can't really happen
6098 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006099 p = NULL;
6100 line = NULL;
6101 }
6102 else
6103 {
6104 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6105
6106 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6107 return;
6108 p = ml_get_buf(buf, lnum + 1, FALSE);
6109 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6110 }
6111
6112 for (pos.col = 0; pos.col < term->tl_cols; )
6113 {
6114 dict_T *dcell;
6115 int width;
6116 VTermScreenCellAttrs attrs;
6117 VTermColor fg, bg;
6118 char_u rgb[8];
6119 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6120 int off = 0;
6121 int i;
6122
6123 if (screen == NULL)
6124 {
6125 cellattr_T *cellattr;
6126 int len;
6127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006128 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006129 if (pos.col >= line->sb_cols)
6130 break;
6131 cellattr = line->sb_cells + pos.col;
6132 width = cellattr->width;
6133 attrs = cellattr->attrs;
6134 fg = cellattr->fg;
6135 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006136 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006137 mch_memmove(mbs, p, len);
6138 mbs[len] = NUL;
6139 p += len;
6140 }
6141 else
6142 {
6143 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006144
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006145 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6146 break;
6147 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6148 {
6149 if (cell.chars[i] == 0)
6150 break;
6151 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6152 }
6153 mbs[off] = NUL;
6154 width = cell.width;
6155 attrs = cell.attrs;
6156 fg = cell.fg;
6157 bg = cell.bg;
6158 }
6159 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006160 if (dcell == NULL)
6161 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006162 list_append_dict(l, dcell);
6163
Bram Moolenaare0be1672018-07-08 16:50:37 +02006164 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006165
6166 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6167 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006168 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006169 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6170 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006171 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006172
Bram Moolenaar87fd0922021-11-20 13:47:45 +00006173 dict_add_number(dcell, "attr",
6174 cell2attr(term, NULL, &attrs, &fg, &bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006175 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006176
6177 ++pos.col;
6178 if (width == 2)
6179 ++pos.col;
6180 }
6181}
6182
6183/*
6184 * "term_sendkeys(buf, keys)" function
6185 */
6186 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006187f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006188{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006189 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006190 char_u *msg;
6191 term_T *term;
6192
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006193 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006194 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006195 || check_for_string_arg(argvars, 1) == FAIL))
6196 return;
6197
6198 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006199 if (buf == NULL)
6200 return;
6201
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006202 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006203 if (msg == NULL)
6204 return;
6205 term = buf->b_term;
6206 if (term->tl_vterm == NULL)
6207 return;
6208
6209 while (*msg != NUL)
6210 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006211 int c;
6212
6213 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6214 {
6215 c = TO_SPECIAL(msg[1], msg[2]);
6216 msg += 3;
6217 }
6218 else
6219 {
6220 c = PTR2CHAR(msg);
6221 msg += MB_CPTR2LEN(msg);
6222 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006223 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006224 }
6225}
6226
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006227#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6228/*
6229 * "term_getansicolors(buf)" function
6230 */
6231 void
6232f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6233{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006234 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006235 term_T *term;
6236 VTermState *state;
6237 VTermColor color;
6238 char_u hexbuf[10];
6239 int index;
6240 list_T *list;
6241
6242 if (rettv_list_alloc(rettv) == FAIL)
6243 return;
6244
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006245 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6246 return;
6247
6248 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006249 if (buf == NULL)
6250 return;
6251 term = buf->b_term;
6252 if (term->tl_vterm == NULL)
6253 return;
6254
6255 list = rettv->vval.v_list;
6256 state = vterm_obtain_state(term->tl_vterm);
6257 for (index = 0; index < 16; index++)
6258 {
6259 vterm_state_get_palette_color(state, index, &color);
6260 sprintf((char *)hexbuf, "#%02x%02x%02x",
6261 color.red, color.green, color.blue);
6262 if (list_append_string(list, hexbuf, 7) == FAIL)
6263 return;
6264 }
6265}
6266
6267/*
6268 * "term_setansicolors(buf, list)" function
6269 */
6270 void
6271f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6272{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006273 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006274 term_T *term;
6275
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006276 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006277 && (check_for_buffer_arg(argvars, 0) == FAIL
6278 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006279 return;
6280
6281 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006282 if (buf == NULL)
6283 return;
6284 term = buf->b_term;
6285 if (term->tl_vterm == NULL)
6286 return;
6287
6288 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6289 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006290 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006291 return;
6292 }
6293
6294 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006295 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006296}
6297#endif
6298
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006299/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006300 * "term_setapi(buf, api)" function
6301 */
6302 void
6303f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6304{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006305 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006306 term_T *term;
6307 char_u *api;
6308
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006309 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006310 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006311 || check_for_string_arg(argvars, 1) == FAIL))
6312 return;
6313
6314 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006315 if (buf == NULL)
6316 return;
6317 term = buf->b_term;
6318 vim_free(term->tl_api);
6319 api = tv_get_string_chk(&argvars[1]);
6320 if (api != NULL)
6321 term->tl_api = vim_strsave(api);
6322 else
6323 term->tl_api = NULL;
6324}
6325
6326/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006327 * "term_setrestore(buf, command)" function
6328 */
6329 void
6330f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6331{
6332#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006333 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006334 term_T *term;
6335 char_u *cmd;
6336
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006337 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006338 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006339 || check_for_string_arg(argvars, 1) == FAIL))
6340 return;
6341
6342 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006343 if (buf == NULL)
6344 return;
6345 term = buf->b_term;
6346 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006347 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006348 if (cmd != NULL)
6349 term->tl_command = vim_strsave(cmd);
6350 else
6351 term->tl_command = NULL;
6352#endif
6353}
6354
6355/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006356 * "term_setkill(buf, how)" function
6357 */
6358 void
6359f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6360{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006361 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006362 term_T *term;
6363 char_u *how;
6364
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006365 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006366 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006367 || check_for_string_arg(argvars, 1) == FAIL))
6368 return;
6369
6370 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006371 if (buf == NULL)
6372 return;
6373 term = buf->b_term;
6374 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006375 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006376 if (how != NULL)
6377 term->tl_kill = vim_strsave(how);
6378 else
6379 term->tl_kill = NULL;
6380}
6381
6382/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006383 * "term_start(command, options)" function
6384 */
6385 void
6386f_term_start(typval_T *argvars, typval_T *rettv)
6387{
6388 jobopt_T opt;
6389 buf_T *buf;
6390
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006391 if (in_vim9script()
6392 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6393 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6394 return;
6395
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006396 init_job_options(&opt);
6397 if (argvars[1].v_type != VAR_UNKNOWN
6398 && get_job_options(&argvars[1], &opt,
6399 JO_TIMEOUT_ALL + JO_STOPONEXIT
6400 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6401 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6402 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6403 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006404 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006405 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006406 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006407 return;
6408
Bram Moolenaar13568252018-03-16 20:46:58 +01006409 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006410
6411 if (buf != NULL && buf->b_term != NULL)
6412 rettv->vval.v_number = buf->b_fnum;
6413}
6414
6415/*
6416 * "term_wait" function
6417 */
6418 void
6419f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6420{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006421 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006422
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006423 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006424 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006425 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006426 return;
6427
6428 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006429 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006430 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006431 if (buf->b_term->tl_job == NULL)
6432 {
6433 ch_log(NULL, "term_wait(): no job to wait for");
6434 return;
6435 }
6436 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006437 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006438 return;
6439
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006440 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006441 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006442 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6443 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006444 // The job is dead, keep reading channel I/O until the channel is
6445 // closed. buf->b_term may become NULL if the terminal was closed while
6446 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006447 ch_log(NULL, "term_wait(): waiting for channel to close");
6448 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6449 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006450 term_flush_messages();
6451
Bram Moolenaard45aa552018-05-21 22:50:29 +02006452 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006453 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006454 // If the terminal is closed when the channel is closed the
6455 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006456 break;
Bram Moolenaareea32af2021-11-21 14:51:13 +00006457 if (buf->b_term == NULL || buf->b_term->tl_channel_closing)
6458 // came here from a close callback, only wait one time
6459 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006460 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006461
6462 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006463 }
6464 else
6465 {
6466 long wait = 10L;
6467
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006468 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006469
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006470 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006471 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006472 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006473 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006474
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006475 // Flushing messages on channels is hopefully sufficient.
6476 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006477 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006478 }
6479}
6480
6481/*
6482 * Called when a channel has sent all the lines to a terminal.
6483 * Send a CTRL-D to mark the end of the text.
6484 */
6485 void
6486term_send_eof(channel_T *ch)
6487{
6488 term_T *term;
6489
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006490 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006491 if (term->tl_job == ch->ch_job)
6492 {
6493 if (term->tl_eof_chars != NULL)
6494 {
6495 channel_send(ch, PART_IN, term->tl_eof_chars,
6496 (int)STRLEN(term->tl_eof_chars), NULL);
6497 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6498 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006499# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006500 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006501 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006502 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6503# endif
6504 }
6505}
6506
Bram Moolenaar113e1072019-01-20 15:30:40 +01006507#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006508 job_T *
6509term_getjob(term_T *term)
6510{
6511 return term != NULL ? term->tl_job : NULL;
6512}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006513#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006514
Bram Moolenaar4f974752019-02-17 17:44:42 +01006515# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006516
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006517///////////////////////////////////////
6518// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006519#ifdef PROTO
6520typedef int COORD;
6521typedef int DWORD;
6522typedef int HANDLE;
6523typedef int *DWORD_PTR;
6524typedef int HPCON;
6525typedef int HRESULT;
6526typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006527typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006528typedef int PSIZE_T;
6529typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006530typedef int BOOL;
6531# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006532#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006533
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006534HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6535HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6536HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006537BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6538BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6539void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006540
6541 static int
6542dyn_conpty_init(int verbose)
6543{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006544 static HMODULE hKerneldll = NULL;
6545 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006546 static struct
6547 {
6548 char *name;
6549 FARPROC *ptr;
6550 } conpty_entry[] =
6551 {
6552 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6553 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6554 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6555 {"InitializeProcThreadAttributeList",
6556 (FARPROC*)&pInitializeProcThreadAttributeList},
6557 {"UpdateProcThreadAttribute",
6558 (FARPROC*)&pUpdateProcThreadAttribute},
6559 {"DeleteProcThreadAttributeList",
6560 (FARPROC*)&pDeleteProcThreadAttributeList},
6561 {NULL, NULL}
6562 };
6563
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006564 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006565 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006566 if (verbose)
6567 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006568 return FAIL;
6569 }
6570
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006571 // No need to initialize twice.
6572 if (hKerneldll)
6573 return OK;
6574
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006575 hKerneldll = vimLoadLib("kernel32.dll");
6576 for (i = 0; conpty_entry[i].name != NULL
6577 && conpty_entry[i].ptr != NULL; ++i)
6578 {
6579 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6580 conpty_entry[i].name)) == NULL)
6581 {
6582 if (verbose)
6583 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006584 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006585 return FAIL;
6586 }
6587 }
6588
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006589 return OK;
6590}
6591
6592 static int
6593conpty_term_and_job_init(
6594 term_T *term,
6595 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006596 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006597 jobopt_T *opt,
6598 jobopt_T *orig_opt)
6599{
6600 WCHAR *cmd_wchar = NULL;
6601 WCHAR *cmd_wchar_copy = NULL;
6602 WCHAR *cwd_wchar = NULL;
6603 WCHAR *env_wchar = NULL;
6604 channel_T *channel = NULL;
6605 job_T *job = NULL;
6606 HANDLE jo = NULL;
6607 garray_T ga_cmd, ga_env;
6608 char_u *cmd = NULL;
6609 HRESULT hr;
6610 COORD consize;
6611 SIZE_T breq;
6612 PROCESS_INFORMATION proc_info;
6613 HANDLE i_theirs = NULL;
6614 HANDLE o_theirs = NULL;
6615 HANDLE i_ours = NULL;
6616 HANDLE o_ours = NULL;
6617
6618 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6619 ga_init2(&ga_env, (int)sizeof(char*), 20);
6620
6621 if (argvar->v_type == VAR_STRING)
6622 {
6623 cmd = argvar->vval.v_string;
6624 }
6625 else if (argvar->v_type == VAR_LIST)
6626 {
6627 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6628 goto failed;
6629 cmd = ga_cmd.ga_data;
6630 }
6631 if (cmd == NULL || *cmd == NUL)
6632 {
6633 emsg(_(e_invarg));
6634 goto failed;
6635 }
6636
6637 term->tl_arg0_cmd = vim_strsave(cmd);
6638
6639 cmd_wchar = enc_to_utf16(cmd, NULL);
6640
6641 if (cmd_wchar != NULL)
6642 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006643 // Request by CreateProcessW
6644 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006645 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006646 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6647 }
6648
6649 ga_clear(&ga_cmd);
6650 if (cmd_wchar == NULL)
6651 goto failed;
6652 if (opt->jo_cwd != NULL)
6653 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6654
6655 win32_build_env(opt->jo_env, &ga_env, TRUE);
6656 env_wchar = ga_env.ga_data;
6657
6658 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6659 goto failed;
6660 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6661 goto failed;
6662
6663 consize.X = term->tl_cols;
6664 consize.Y = term->tl_rows;
6665 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6666 &term->tl_conpty);
6667 if (FAILED(hr))
6668 goto failed;
6669
6670 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6671
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006672 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006673 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006674 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006675 if (!term->tl_siex.lpAttributeList)
6676 goto failed;
6677 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6678 0, &breq))
6679 goto failed;
6680 if (!pUpdateProcThreadAttribute(
6681 term->tl_siex.lpAttributeList, 0,
6682 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6683 sizeof(HPCON), NULL, NULL))
6684 goto failed;
6685
6686 channel = add_channel();
6687 if (channel == NULL)
6688 goto failed;
6689
6690 job = job_alloc();
6691 if (job == NULL)
6692 goto failed;
6693 if (argvar->v_type == VAR_STRING)
6694 {
6695 int argc;
6696
6697 build_argv_from_string(cmd, &job->jv_argv, &argc);
6698 }
6699 else
6700 {
6701 int argc;
6702
6703 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6704 }
6705
6706 if (opt->jo_set & JO_IN_BUF)
6707 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6708
6709 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6710 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006711 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006712 env_wchar, cwd_wchar,
6713 &term->tl_siex.StartupInfo, &proc_info))
6714 goto failed;
6715
6716 CloseHandle(i_theirs);
6717 CloseHandle(o_theirs);
6718
6719 channel_set_pipes(channel,
6720 (sock_T)i_ours,
6721 (sock_T)o_ours,
6722 (sock_T)o_ours);
6723
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006724 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006725 channel->ch_write_text_mode = TRUE;
6726
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006727 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006728 channel->ch_anonymous_pipe = TRUE;
6729
6730 jo = CreateJobObject(NULL, NULL);
6731 if (jo == NULL)
6732 goto failed;
6733
6734 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6735 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006736 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006737 CloseHandle(jo);
6738 jo = NULL;
6739 }
6740
6741 ResumeThread(proc_info.hThread);
6742 CloseHandle(proc_info.hThread);
6743
6744 vim_free(cmd_wchar);
6745 vim_free(cmd_wchar_copy);
6746 vim_free(cwd_wchar);
6747 vim_free(env_wchar);
6748
6749 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6750 goto failed;
6751
6752#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6753 if (opt->jo_set2 & JO2_ANSI_COLORS)
6754 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6755 else
6756 init_vterm_ansi_colors(term->tl_vterm);
6757#endif
6758
6759 channel_set_job(channel, job, opt);
6760 job_set_options(job, opt);
6761
6762 job->jv_channel = channel;
6763 job->jv_proc_info = proc_info;
6764 job->jv_job_object = jo;
6765 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006766 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006767 ++job->jv_refcount;
6768 term->tl_job = job;
6769
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006770 // Redirecting stdout and stderr doesn't work at the job level. Instead
6771 // open the file here and handle it in. opt->jo_io was changed in
6772 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006773 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6774 {
6775 char_u *fname = opt->jo_io_name[PART_OUT];
6776
6777 ch_log(channel, "Opening output file %s", fname);
6778 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6779 if (term->tl_out_fd == NULL)
6780 semsg(_(e_notopen), fname);
6781 }
6782
6783 return OK;
6784
6785failed:
6786 ga_clear(&ga_cmd);
6787 ga_clear(&ga_env);
6788 vim_free(cmd_wchar);
6789 vim_free(cmd_wchar_copy);
6790 vim_free(cwd_wchar);
6791 if (channel != NULL)
6792 channel_clear(channel);
6793 if (job != NULL)
6794 {
6795 job->jv_channel = NULL;
6796 job_cleanup(job);
6797 }
6798 term->tl_job = NULL;
6799 if (jo != NULL)
6800 CloseHandle(jo);
6801
6802 if (term->tl_siex.lpAttributeList != NULL)
6803 {
6804 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6805 vim_free(term->tl_siex.lpAttributeList);
6806 }
6807 term->tl_siex.lpAttributeList = NULL;
6808 if (o_theirs != NULL)
6809 CloseHandle(o_theirs);
6810 if (o_ours != NULL)
6811 CloseHandle(o_ours);
6812 if (i_ours != NULL)
6813 CloseHandle(i_ours);
6814 if (i_theirs != NULL)
6815 CloseHandle(i_theirs);
6816 if (term->tl_conpty != NULL)
6817 pClosePseudoConsole(term->tl_conpty);
6818 term->tl_conpty = NULL;
6819 return FAIL;
6820}
6821
6822 static void
6823conpty_term_report_winsize(term_T *term, int rows, int cols)
6824{
6825 COORD consize;
6826
6827 consize.X = cols;
6828 consize.Y = rows;
6829 pResizePseudoConsole(term->tl_conpty, consize);
6830}
6831
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006832 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006833term_free_conpty(term_T *term)
6834{
6835 if (term->tl_siex.lpAttributeList != NULL)
6836 {
6837 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6838 vim_free(term->tl_siex.lpAttributeList);
6839 }
6840 term->tl_siex.lpAttributeList = NULL;
6841 if (term->tl_conpty != NULL)
6842 pClosePseudoConsole(term->tl_conpty);
6843 term->tl_conpty = NULL;
6844}
6845
6846 int
6847use_conpty(void)
6848{
6849 return has_conpty;
6850}
6851
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006852# ifndef PROTO
6853
6854#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6855#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006856#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006857
6858void* (*winpty_config_new)(UINT64, void*);
6859void* (*winpty_open)(void*, void*);
6860void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6861BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6862void (*winpty_config_set_mouse_mode)(void*, int);
6863void (*winpty_config_set_initial_size)(void*, int, int);
6864LPCWSTR (*winpty_conin_name)(void*);
6865LPCWSTR (*winpty_conout_name)(void*);
6866LPCWSTR (*winpty_conerr_name)(void*);
6867void (*winpty_free)(void*);
6868void (*winpty_config_free)(void*);
6869void (*winpty_spawn_config_free)(void*);
6870void (*winpty_error_free)(void*);
6871LPCWSTR (*winpty_error_msg)(void*);
6872BOOL (*winpty_set_size)(void*, int, int, void*);
6873HANDLE (*winpty_agent_process)(void*);
6874
6875#define WINPTY_DLL "winpty.dll"
6876
6877static HINSTANCE hWinPtyDLL = NULL;
6878# endif
6879
6880 static int
6881dyn_winpty_init(int verbose)
6882{
6883 int i;
6884 static struct
6885 {
6886 char *name;
6887 FARPROC *ptr;
6888 } winpty_entry[] =
6889 {
6890 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6891 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6892 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6893 {"winpty_config_set_mouse_mode",
6894 (FARPROC*)&winpty_config_set_mouse_mode},
6895 {"winpty_config_set_initial_size",
6896 (FARPROC*)&winpty_config_set_initial_size},
6897 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6898 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6899 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6900 {"winpty_free", (FARPROC*)&winpty_free},
6901 {"winpty_open", (FARPROC*)&winpty_open},
6902 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6903 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6904 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6905 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6906 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6907 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6908 {NULL, NULL}
6909 };
6910
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006911 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006912 if (hWinPtyDLL)
6913 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006914 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6915 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006916 if (*p_winptydll != NUL)
6917 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6918 if (!hWinPtyDLL)
6919 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6920 if (!hWinPtyDLL)
6921 {
6922 if (verbose)
Martin Tournoij1a3e5742021-07-24 13:57:29 +02006923 semsg(_(e_loadlib),
6924 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
6925 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006926 return FAIL;
6927 }
6928 for (i = 0; winpty_entry[i].name != NULL
6929 && winpty_entry[i].ptr != NULL; ++i)
6930 {
6931 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6932 winpty_entry[i].name)) == NULL)
6933 {
6934 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006935 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006936 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006937 return FAIL;
6938 }
6939 }
6940
6941 return OK;
6942}
6943
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006944 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006945winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006946 term_T *term,
6947 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006948 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006949 jobopt_T *opt,
6950 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006951{
6952 WCHAR *cmd_wchar = NULL;
6953 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006954 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006955 channel_T *channel = NULL;
6956 job_T *job = NULL;
6957 DWORD error;
6958 HANDLE jo = NULL;
6959 HANDLE child_process_handle;
6960 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006961 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006962 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006963 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006964 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006965
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006966 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6967 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006968
6969 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006970 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006971 cmd = argvar->vval.v_string;
6972 }
6973 else if (argvar->v_type == VAR_LIST)
6974 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006975 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006976 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006977 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006978 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006979 if (cmd == NULL || *cmd == NUL)
6980 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006981 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006982 goto failed;
6983 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006984
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006985 term->tl_arg0_cmd = vim_strsave(cmd);
6986
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006987 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006988 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006989 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006990 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006991 if (opt->jo_cwd != NULL)
6992 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006993
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006994 win32_build_env(opt->jo_env, &ga_env, TRUE);
6995 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006996
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006997 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6998 if (term->tl_winpty_config == NULL)
6999 goto failed;
7000
7001 winpty_config_set_mouse_mode(term->tl_winpty_config,
7002 WINPTY_MOUSE_MODE_FORCE);
7003 winpty_config_set_initial_size(term->tl_winpty_config,
7004 term->tl_cols, term->tl_rows);
7005 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
7006 if (term->tl_winpty == NULL)
7007 goto failed;
7008
7009 spawn_config = winpty_spawn_config_new(
7010 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
7011 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
7012 NULL,
7013 cmd_wchar,
7014 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007015 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007016 &winpty_err);
7017 if (spawn_config == NULL)
7018 goto failed;
7019
7020 channel = add_channel();
7021 if (channel == NULL)
7022 goto failed;
7023
7024 job = job_alloc();
7025 if (job == NULL)
7026 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02007027 if (argvar->v_type == VAR_STRING)
7028 {
7029 int argc;
7030
7031 build_argv_from_string(cmd, &job->jv_argv, &argc);
7032 }
7033 else
7034 {
7035 int argc;
7036
7037 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
7038 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007039
7040 if (opt->jo_set & JO_IN_BUF)
7041 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
7042
7043 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
7044 &child_thread_handle, &error, &winpty_err))
7045 goto failed;
7046
7047 channel_set_pipes(channel,
7048 (sock_T)CreateFileW(
7049 winpty_conin_name(term->tl_winpty),
7050 GENERIC_WRITE, 0, NULL,
7051 OPEN_EXISTING, 0, NULL),
7052 (sock_T)CreateFileW(
7053 winpty_conout_name(term->tl_winpty),
7054 GENERIC_READ, 0, NULL,
7055 OPEN_EXISTING, 0, NULL),
7056 (sock_T)CreateFileW(
7057 winpty_conerr_name(term->tl_winpty),
7058 GENERIC_READ, 0, NULL,
7059 OPEN_EXISTING, 0, NULL));
7060
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007061 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007062 channel->ch_write_text_mode = TRUE;
7063
7064 jo = CreateJobObject(NULL, NULL);
7065 if (jo == NULL)
7066 goto failed;
7067
7068 if (!AssignProcessToJobObject(jo, child_process_handle))
7069 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007070 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007071 CloseHandle(jo);
7072 jo = NULL;
7073 }
7074
7075 winpty_spawn_config_free(spawn_config);
7076 vim_free(cmd_wchar);
7077 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007078 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007079
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007080 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7081 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007082
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007083#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7084 if (opt->jo_set2 & JO2_ANSI_COLORS)
7085 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7086 else
7087 init_vterm_ansi_colors(term->tl_vterm);
7088#endif
7089
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007090 channel_set_job(channel, job, opt);
7091 job_set_options(job, opt);
7092
7093 job->jv_channel = channel;
7094 job->jv_proc_info.hProcess = child_process_handle;
7095 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7096 job->jv_job_object = jo;
7097 job->jv_status = JOB_STARTED;
7098 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007099 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007100 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007101 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007102 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007103 ++job->jv_refcount;
7104 term->tl_job = job;
7105
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007106 // Redirecting stdout and stderr doesn't work at the job level. Instead
7107 // open the file here and handle it in. opt->jo_io was changed in
7108 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007109 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7110 {
7111 char_u *fname = opt->jo_io_name[PART_OUT];
7112
7113 ch_log(channel, "Opening output file %s", fname);
7114 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7115 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007116 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007117 }
7118
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007119 return OK;
7120
7121failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007122 ga_clear(&ga_cmd);
7123 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007124 vim_free(cmd_wchar);
7125 vim_free(cwd_wchar);
7126 if (spawn_config != NULL)
7127 winpty_spawn_config_free(spawn_config);
7128 if (channel != NULL)
7129 channel_clear(channel);
7130 if (job != NULL)
7131 {
7132 job->jv_channel = NULL;
7133 job_cleanup(job);
7134 }
7135 term->tl_job = NULL;
7136 if (jo != NULL)
7137 CloseHandle(jo);
7138 if (term->tl_winpty != NULL)
7139 winpty_free(term->tl_winpty);
7140 term->tl_winpty = NULL;
7141 if (term->tl_winpty_config != NULL)
7142 winpty_config_free(term->tl_winpty_config);
7143 term->tl_winpty_config = NULL;
7144 if (winpty_err != NULL)
7145 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007146 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007147 (short_u *)winpty_error_msg(winpty_err), NULL);
7148
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007149 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007150 winpty_error_free(winpty_err);
7151 }
7152 return FAIL;
7153}
7154
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007155/*
7156 * Create a new terminal of "rows" by "cols" cells.
7157 * Store a reference in "term".
7158 * Return OK or FAIL.
7159 */
7160 static int
7161term_and_job_init(
7162 term_T *term,
7163 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007164 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007165 jobopt_T *opt,
7166 jobopt_T *orig_opt)
7167{
7168 int use_winpty = FALSE;
7169 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007170 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007171
7172 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7173 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7174
7175 if (!has_winpty && !has_conpty)
7176 // If neither is available give the errors for winpty, since when
7177 // conpty is not available it can't be installed either.
7178 return dyn_winpty_init(TRUE);
7179
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007180 if (opt->jo_tty_type != NUL)
7181 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007182
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007183 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007184 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007185 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007186 use_conpty = TRUE;
7187 else if (has_winpty)
7188 use_winpty = TRUE;
7189 // else: error
7190 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007191 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007192 {
7193 if (has_winpty)
7194 use_winpty = TRUE;
7195 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007196 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007197 {
7198 if (has_conpty)
7199 use_conpty = TRUE;
7200 else
7201 return dyn_conpty_init(TRUE);
7202 }
7203
7204 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007205 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007206
7207 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007208 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007209
7210 // error
7211 return dyn_winpty_init(TRUE);
7212}
7213
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007214 static int
7215create_pty_only(term_T *term, jobopt_T *options)
7216{
7217 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7218 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7219 char in_name[80], out_name[80];
7220 channel_T *channel = NULL;
7221
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007222 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7223 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007224
7225 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7226 GetCurrentProcessId(),
7227 curbuf->b_fnum);
7228 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7229 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7230 PIPE_UNLIMITED_INSTANCES,
7231 0, 0, NMPWAIT_NOWAIT, NULL);
7232 if (hPipeIn == INVALID_HANDLE_VALUE)
7233 goto failed;
7234
7235 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7236 GetCurrentProcessId(),
7237 curbuf->b_fnum);
7238 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7239 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7240 PIPE_UNLIMITED_INSTANCES,
7241 0, 0, 0, NULL);
7242 if (hPipeOut == INVALID_HANDLE_VALUE)
7243 goto failed;
7244
7245 ConnectNamedPipe(hPipeIn, NULL);
7246 ConnectNamedPipe(hPipeOut, NULL);
7247
7248 term->tl_job = job_alloc();
7249 if (term->tl_job == NULL)
7250 goto failed;
7251 ++term->tl_job->jv_refcount;
7252
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007253 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007254 term->tl_job->jv_status = JOB_FINISHED;
7255
7256 channel = add_channel();
7257 if (channel == NULL)
7258 goto failed;
7259 term->tl_job->jv_channel = channel;
7260 channel->ch_keep_open = TRUE;
7261 channel->ch_named_pipe = TRUE;
7262
7263 channel_set_pipes(channel,
7264 (sock_T)hPipeIn,
7265 (sock_T)hPipeOut,
7266 (sock_T)hPipeOut);
7267 channel_set_job(channel, term->tl_job, options);
7268 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7269 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7270
7271 return OK;
7272
7273failed:
7274 if (hPipeIn != NULL)
7275 CloseHandle(hPipeIn);
7276 if (hPipeOut != NULL)
7277 CloseHandle(hPipeOut);
7278 return FAIL;
7279}
7280
7281/*
7282 * Free the terminal emulator part of "term".
7283 */
7284 static void
7285term_free_vterm(term_T *term)
7286{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007287 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007288 if (term->tl_winpty != NULL)
7289 winpty_free(term->tl_winpty);
7290 term->tl_winpty = NULL;
7291 if (term->tl_winpty_config != NULL)
7292 winpty_config_free(term->tl_winpty_config);
7293 term->tl_winpty_config = NULL;
7294 if (term->tl_vterm != NULL)
7295 vterm_free(term->tl_vterm);
7296 term->tl_vterm = NULL;
7297}
7298
7299/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007300 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007301 */
7302 static void
7303term_report_winsize(term_T *term, int rows, int cols)
7304{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007305 if (term->tl_conpty)
7306 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007307 if (term->tl_winpty)
7308 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7309}
7310
7311 int
7312terminal_enabled(void)
7313{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007314 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007315}
7316
7317# else
7318
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007319///////////////////////////////////////
7320// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007321
7322/*
7323 * Create a new terminal of "rows" by "cols" cells.
7324 * Start job for "cmd".
7325 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007326 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007327 * Return OK or FAIL.
7328 */
7329 static int
7330term_and_job_init(
7331 term_T *term,
7332 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007333 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007334 jobopt_T *opt,
7335 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007336{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007337 term->tl_arg0_cmd = NULL;
7338
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007339 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7340 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007341
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007342#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7343 if (opt->jo_set2 & JO2_ANSI_COLORS)
7344 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7345 else
7346 init_vterm_ansi_colors(term->tl_vterm);
7347#endif
7348
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007349 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007350 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007351 if (term->tl_job != NULL)
7352 ++term->tl_job->jv_refcount;
7353
7354 return term->tl_job != NULL
7355 && term->tl_job->jv_channel != NULL
7356 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7357}
7358
7359 static int
7360create_pty_only(term_T *term, jobopt_T *opt)
7361{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007362 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7363 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007364
7365 term->tl_job = job_alloc();
7366 if (term->tl_job == NULL)
7367 return FAIL;
7368 ++term->tl_job->jv_refcount;
7369
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007370 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007371 term->tl_job->jv_status = JOB_FINISHED;
7372
7373 return mch_create_pty_channel(term->tl_job, opt);
7374}
7375
7376/*
7377 * Free the terminal emulator part of "term".
7378 */
7379 static void
7380term_free_vterm(term_T *term)
7381{
7382 if (term->tl_vterm != NULL)
7383 vterm_free(term->tl_vterm);
7384 term->tl_vterm = NULL;
7385}
7386
7387/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007388 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007389 */
7390 static void
7391term_report_winsize(term_T *term, int rows, int cols)
7392{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007393 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007394 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7395 {
7396 int fd = -1;
7397 int part;
7398
7399 for (part = PART_OUT; part < PART_COUNT; ++part)
7400 {
7401 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007402 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007403 break;
7404 }
7405 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7406 mch_signal_job(term->tl_job, (char_u *)"winch");
7407 }
7408}
7409
7410# endif
7411
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007412#endif // FEAT_TERMINAL