blob: f8dc219b41faca4327026e9e8a89a933bab1b8ea [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 Moolenaara48d4e42021-12-08 22:13:38 +00001131 * Read any vterm output and send it on the channel.
1132 */
1133 static void
1134term_forward_output(term_T *term)
1135{
1136 VTerm *vterm = term->tl_vterm;
1137 char buf[KEY_BUF_LEN];
1138 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1139
1140 if (curlen > 0)
1141 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1142 (char_u *)buf, (int)curlen, NULL);
1143}
1144
1145/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001146 * Write job output "msg[len]" to the vterm.
1147 */
1148 static void
Bram Moolenaar36968af2021-11-15 17:13:11 +00001149term_write_job_output(term_T *term, char_u *msg_arg, size_t len_arg)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001150{
Bram Moolenaar36968af2021-11-15 17:13:11 +00001151 char_u *msg = msg_arg;
1152 size_t len = len_arg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001153 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001154 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar36968af2021-11-15 17:13:11 +00001155 size_t limit = term->tl_buffer->b_p_twsl * term->tl_cols * 3;
1156
1157 // Limit the length to 'termwinscroll' * cols * 3 bytes. Keep the text at
1158 // the end.
1159 if (len > limit)
1160 {
1161 char_u *p = msg + len - limit;
1162
1163 p -= (*mb_head_off)(msg, p);
1164 len -= p - msg;
1165 msg = p;
1166 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001167
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001168 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001169
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001170 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001171 if (prevlen != vterm_output_get_buffer_current(vterm))
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001172 term_forward_output(term);
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001173
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001174 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001175 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1176}
1177
1178 static void
1179update_cursor(term_T *term, int redraw)
1180{
1181 if (term->tl_normal_mode)
1182 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001183#ifdef FEAT_GUI
1184 if (term->tl_system)
1185 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1186 term->tl_cursor_pos.col);
1187 else
1188#endif
1189 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001190 if (redraw)
1191 {
1192 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1193 cursor_on();
1194 out_flush();
1195#ifdef FEAT_GUI
1196 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001197 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001198 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001199 gui_mch_flush();
1200 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001201#endif
1202 }
1203}
1204
1205/*
1206 * Invoked when "msg" output from a job was received. Write it to the terminal
1207 * of "buffer".
1208 */
1209 void
1210write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1211{
1212 size_t len = STRLEN(msg);
1213 term_T *term = buffer->b_term;
1214
Bram Moolenaar4f974752019-02-17 17:44:42 +01001215#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001216 // Win32: Cannot redirect output of the job, intercept it here and write to
1217 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001218 if (term->tl_out_fd != NULL)
1219 {
1220 ch_log(channel, "Writing %d bytes to output file", (int)len);
1221 fwrite(msg, len, 1, term->tl_out_fd);
1222 return;
1223 }
1224#endif
1225
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001226 if (term->tl_vterm == NULL)
1227 {
1228 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1229 return;
1230 }
1231 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01001232 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001233 term_write_job_output(term, msg, len);
1234
Bram Moolenaar13568252018-03-16 20:46:58 +01001235#ifdef FEAT_GUI
1236 if (term->tl_system)
1237 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001238 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001239 update_system_term(term);
1240 update_cursor(term, TRUE);
1241 }
1242 else
1243#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001244 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1245 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001246 if (!term->tl_normal_mode)
1247 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001248 // Don't use update_screen() when editing the command line, it gets
1249 // cleared.
1250 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001251 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001252 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001253 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001254 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001255 // update_screen() can be slow, check the terminal wasn't closed
1256 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001257 if (buffer == curbuf && curbuf->b_term != NULL)
1258 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001259 }
1260 else
Bram Moolenaare5050712021-12-09 10:51:05 +00001261 redraw_after_callback(TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001262 }
1263}
1264
1265/*
1266 * Send a mouse position and click to the vterm
1267 */
1268 static int
1269term_send_mouse(VTerm *vterm, int button, int pressed)
1270{
1271 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001272 int row = mouse_row - W_WINROW(curwin);
1273 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001274
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001275#ifdef FEAT_PROP_POPUP
1276 if (popup_is_popup(curwin))
1277 {
1278 row -= popup_top_extra(curwin);
1279 col -= popup_left_extra(curwin);
1280 }
1281#endif
1282 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001283 if (button != 0)
1284 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001285 return TRUE;
1286}
1287
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001288static int enter_mouse_col = -1;
1289static int enter_mouse_row = -1;
1290
1291/*
1292 * Handle a mouse click, drag or release.
1293 * Return TRUE when a mouse event is sent to the terminal.
1294 */
1295 static int
1296term_mouse_click(VTerm *vterm, int key)
1297{
1298#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001299 // For modeless selection mouse drag and release events are ignored, unless
1300 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001301 static int ignore_drag_release = TRUE;
1302 VTermMouseState mouse_state;
1303
1304 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1305 if (mouse_state.flags == 0)
1306 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001307 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001308 switch (key)
1309 {
1310 case K_LEFTDRAG:
1311 case K_LEFTRELEASE:
1312 case K_RIGHTDRAG:
1313 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001314 // Ignore drag and release events when the button-down wasn't
1315 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001316 if (ignore_drag_release)
1317 {
1318 int save_mouse_col, save_mouse_row;
1319
1320 if (enter_mouse_col < 0)
1321 break;
1322
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001323 // mouse click in the window gave us focus, handle that
1324 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001325 save_mouse_col = mouse_col;
1326 save_mouse_row = mouse_row;
1327 mouse_col = enter_mouse_col;
1328 mouse_row = enter_mouse_row;
1329 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1330 mouse_col = save_mouse_col;
1331 mouse_row = save_mouse_row;
1332 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001333 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001334 case K_LEFTMOUSE:
1335 case K_RIGHTMOUSE:
1336 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1337 ignore_drag_release = TRUE;
1338 else
1339 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001340 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001341 if (clip_star.available)
1342 {
1343 int button, is_click, is_drag;
1344
1345 button = get_mouse_button(KEY2TERMCAP1(key),
1346 &is_click, &is_drag);
1347 if (mouse_model_popup() && button == MOUSE_LEFT
1348 && (mod_mask & MOD_MASK_SHIFT))
1349 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001350 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001351 button = MOUSE_RIGHT;
1352 mod_mask &= ~MOD_MASK_SHIFT;
1353 }
1354 clip_modeless(button, is_click, is_drag);
1355 }
1356 break;
1357
1358 case K_MIDDLEMOUSE:
1359 if (clip_star.available)
1360 insert_reg('*', TRUE);
1361 break;
1362 }
1363 enter_mouse_col = -1;
1364 return FALSE;
1365 }
1366#endif
1367 enter_mouse_col = -1;
1368
1369 switch (key)
1370 {
1371 case K_LEFTMOUSE:
1372 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1373 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1374 case K_LEFTRELEASE:
1375 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1376 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1377 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1378 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1379 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1380 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1381 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1382 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1383 }
1384 return TRUE;
1385}
1386
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001387/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001388 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1389 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001390 * Return the number of bytes in "buf".
1391 */
1392 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001393term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001394{
1395 VTerm *vterm = term->tl_vterm;
1396 VTermKey key = VTERM_KEY_NONE;
1397 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001398 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001399
1400 switch (c)
1401 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001402 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001403
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001404 // don't use VTERM_KEY_BACKSPACE, it always
1405 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001406 case K_BS: c = term_backspace_char; break;
1407
1408 case ESC: key = VTERM_KEY_ESCAPE; break;
1409 case K_DEL: key = VTERM_KEY_DEL; break;
1410 case K_DOWN: key = VTERM_KEY_DOWN; break;
1411 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1412 key = VTERM_KEY_DOWN; break;
1413 case K_END: key = VTERM_KEY_END; break;
1414 case K_S_END: mod = VTERM_MOD_SHIFT;
1415 key = VTERM_KEY_END; break;
1416 case K_C_END: mod = VTERM_MOD_CTRL;
1417 key = VTERM_KEY_END; break;
1418 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1419 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1420 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1421 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1422 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1423 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1424 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1425 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1426 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1427 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1428 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1429 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1430 case K_HOME: key = VTERM_KEY_HOME; break;
1431 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1432 key = VTERM_KEY_HOME; break;
1433 case K_C_HOME: mod = VTERM_MOD_CTRL;
1434 key = VTERM_KEY_HOME; break;
1435 case K_INS: key = VTERM_KEY_INS; break;
1436 case K_K0: key = VTERM_KEY_KP_0; break;
1437 case K_K1: key = VTERM_KEY_KP_1; break;
1438 case K_K2: key = VTERM_KEY_KP_2; break;
1439 case K_K3: key = VTERM_KEY_KP_3; break;
1440 case K_K4: key = VTERM_KEY_KP_4; break;
1441 case K_K5: key = VTERM_KEY_KP_5; break;
1442 case K_K6: key = VTERM_KEY_KP_6; break;
1443 case K_K7: key = VTERM_KEY_KP_7; break;
1444 case K_K8: key = VTERM_KEY_KP_8; break;
1445 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001446 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001447 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001448 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001449 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001450 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1451 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001452 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1453 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001454 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1455 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001456 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1457 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1458 case K_LEFT: key = VTERM_KEY_LEFT; break;
1459 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1460 key = VTERM_KEY_LEFT; break;
1461 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1462 key = VTERM_KEY_LEFT; break;
1463 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1464 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1465 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1466 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1467 key = VTERM_KEY_RIGHT; break;
1468 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1469 key = VTERM_KEY_RIGHT; break;
1470 case K_UP: key = VTERM_KEY_UP; break;
1471 case K_S_UP: mod = VTERM_MOD_SHIFT;
1472 key = VTERM_KEY_UP; break;
1473 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001474 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1475 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001476
Bram Moolenaara42ad572017-11-16 13:08:04 +01001477 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1478 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001479 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1480 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001481
1482 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001483 case K_LEFTMOUSE_NM:
1484 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001485 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001486 case K_LEFTRELEASE_NM:
1487 case K_MOUSEMOVE:
1488 case K_MIDDLEMOUSE:
1489 case K_MIDDLEDRAG:
1490 case K_MIDDLERELEASE:
1491 case K_RIGHTMOUSE:
1492 case K_RIGHTDRAG:
1493 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1494 return 0;
1495 other = TRUE;
1496 break;
1497
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001498 case K_X1MOUSE: /* TODO */ return 0;
1499 case K_X1DRAG: /* TODO */ return 0;
1500 case K_X1RELEASE: /* TODO */ return 0;
1501 case K_X2MOUSE: /* TODO */ return 0;
1502 case K_X2DRAG: /* TODO */ return 0;
1503 case K_X2RELEASE: /* TODO */ return 0;
1504
1505 case K_IGNORE: return 0;
1506 case K_NOP: return 0;
1507 case K_UNDO: return 0;
1508 case K_HELP: return 0;
1509 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1510 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1511 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1512 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1513 case K_SELECT: return 0;
1514#ifdef FEAT_GUI
1515 case K_VER_SCROLLBAR: return 0;
1516 case K_HOR_SCROLLBAR: return 0;
1517#endif
1518#ifdef FEAT_GUI_TABLINE
1519 case K_TABLINE: return 0;
1520 case K_TABMENU: return 0;
1521#endif
1522#ifdef FEAT_NETBEANS_INTG
1523 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1524#endif
1525#ifdef FEAT_DND
1526 case K_DROP: return 0;
1527#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001528 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001529 case K_PS: vterm_keyboard_start_paste(vterm);
1530 other = TRUE;
1531 break;
1532 case K_PE: vterm_keyboard_end_paste(vterm);
1533 other = TRUE;
1534 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001535 }
1536
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001537 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001538 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001539 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001540 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001541 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001542 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001543 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001544
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001545 /*
1546 * Convert special keys to vterm keys:
1547 * - Write keys to vterm: vterm_keyboard_key()
1548 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001549 */
1550 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001551 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001552 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001553 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001554 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001555 vterm_keyboard_unichar(vterm, c, mod);
1556
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001557 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001558 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1559}
1560
1561/*
1562 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001563 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001564 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001565 */
1566 static int
1567term_job_running_check(term_T *term, int check_job_status)
1568{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001569 // Also consider the job finished when the channel is closed, to avoid a
1570 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001571 if (term != NULL
1572 && term->tl_job != NULL
1573 && channel_is_open(term->tl_job->jv_channel))
1574 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001575 job_T *job = term->tl_job;
1576
1577 // Careful: Checking the job status may invoked callbacks, which close
1578 // the buffer and terminate "term". However, "job" will not be freed
1579 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001580 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001581 job_status(job);
1582 return (job->jv_status == JOB_STARTED
1583 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001584 }
1585 return FALSE;
1586}
1587
1588/*
1589 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001590 */
1591 int
1592term_job_running(term_T *term)
1593{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001594 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001595}
1596
1597/*
1598 * Return TRUE if "term" has an active channel and used ":term NONE".
1599 */
1600 int
1601term_none_open(term_T *term)
1602{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001603 // Also consider the job finished when the channel is closed, to avoid a
1604 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001605 return term != NULL
1606 && term->tl_job != NULL
1607 && channel_is_open(term->tl_job->jv_channel)
1608 && term->tl_job->jv_channel->ch_keep_open;
1609}
1610
1611/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001612 * Used when exiting: kill the job in "buf" if so desired.
1613 * Return OK when the job finished.
1614 * Return FAIL when the job is still running.
1615 */
1616 int
1617term_try_stop_job(buf_T *buf)
1618{
1619 int count;
1620 char *how = (char *)buf->b_term->tl_kill;
1621
1622#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001623 if ((how == NULL || *how == NUL)
1624 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001625 {
1626 char_u buff[DIALOG_MSG_SIZE];
1627 int ret;
1628
Bram Moolenaar00806bc2020-11-05 19:36:38 +01001629 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001630 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1631 if (ret == VIM_YES)
1632 how = "kill";
1633 else if (ret == VIM_CANCEL)
1634 return FAIL;
1635 }
1636#endif
1637 if (how == NULL || *how == NUL)
1638 return FAIL;
1639
1640 job_stop(buf->b_term->tl_job, NULL, how);
1641
Bram Moolenaar9172d232019-01-29 23:06:54 +01001642 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001643 for (count = 0; count < 100; ++count)
1644 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001645 job_T *job;
1646
1647 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001648 if (!buf_valid(buf)
1649 || buf->b_term == NULL
1650 || buf->b_term->tl_job == NULL)
1651 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001652 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001653
Bram Moolenaar9172d232019-01-29 23:06:54 +01001654 // Call job_status() to update jv_status. It may cause the job to be
1655 // cleaned up but it won't be freed.
1656 job_status(job);
1657 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001658 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001659
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001660 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001661 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001662 }
1663 return FAIL;
1664}
1665
1666/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001667 * Add the last line of the scrollback buffer to the buffer in the window.
1668 */
1669 static void
1670add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1671{
1672 buf_T *buf = term->tl_buffer;
1673 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1674 linenr_T lnum = buf->b_ml.ml_line_count;
1675
Bram Moolenaar4f974752019-02-17 17:44:42 +01001676#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001677 if (!enc_utf8 && enc_codepage > 0)
1678 {
1679 WCHAR *ret = NULL;
1680 int length = 0;
1681
1682 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1683 &ret, &length);
1684 if (ret != NULL)
1685 {
1686 WideCharToMultiByte_alloc(enc_codepage, 0,
1687 ret, length, (char **)&text, &len, 0, 0);
1688 vim_free(ret);
1689 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1690 vim_free(text);
1691 }
1692 }
1693 else
1694#endif
1695 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1696 if (empty)
1697 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001698 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001699 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001700 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001701 curbuf = curwin->w_buffer;
1702 }
1703}
1704
1705 static void
1706cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1707{
1708 attr->width = cell->width;
1709 attr->attrs = cell->attrs;
1710 attr->fg = cell->fg;
1711 attr->bg = cell->bg;
1712}
1713
1714 static int
1715equal_celattr(cellattr_T *a, cellattr_T *b)
1716{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001717 // We only compare the RGB colors, ignoring the ANSI index and type.
1718 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001719 return a->fg.red == b->fg.red
1720 && a->fg.green == b->fg.green
1721 && a->fg.blue == b->fg.blue
1722 && a->bg.red == b->bg.red
1723 && a->bg.green == b->bg.green
1724 && a->bg.blue == b->bg.blue;
1725}
1726
Bram Moolenaard96ff162018-02-18 22:13:29 +01001727/*
1728 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1729 * line at this position. Otherwise at the end.
1730 */
1731 static int
1732add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1733{
1734 if (ga_grow(&term->tl_scrollback, 1) == OK)
1735 {
1736 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1737 + term->tl_scrollback.ga_len;
1738
1739 if (lnum > 0)
1740 {
1741 int i;
1742
1743 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1744 {
1745 *line = *(line - 1);
1746 --line;
1747 }
1748 }
1749 line->sb_cols = 0;
1750 line->sb_cells = NULL;
1751 line->sb_fill_attr = *fill_attr;
1752 ++term->tl_scrollback.ga_len;
1753 return OK;
1754 }
1755 return FALSE;
1756}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001757
1758/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001759 * Remove the terminal contents from the scrollback and the buffer.
1760 * Used before adding a new scrollback line or updating the buffer for lines
1761 * displayed in the terminal.
1762 */
1763 static void
1764cleanup_scrollback(term_T *term)
1765{
1766 sb_line_T *line;
1767 garray_T *gap;
1768
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001769 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001770 gap = &term->tl_scrollback;
1771 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1772 && gap->ga_len > 0)
1773 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001774 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001775 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1776 vim_free(line->sb_cells);
1777 --gap->ga_len;
1778 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001779 curbuf = curwin->w_buffer;
1780 if (curbuf == term->tl_buffer)
1781 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001782}
1783
1784/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001785 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001786 */
1787 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001788update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001789{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001790 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001791 int len;
1792 int lines_skipped = 0;
1793 VTermPos pos;
1794 VTermScreenCell cell;
1795 cellattr_T fill_attr, new_fill_attr;
1796 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001797
1798 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1799 "Adding terminal window snapshot to buffer");
1800
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001801 // First remove the lines that were appended before, they might be
1802 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001803 cleanup_scrollback(term);
1804
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001805 screen = vterm_obtain_screen(term->tl_vterm);
1806 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001807 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1808 {
1809 len = 0;
1810 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1811 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1812 && cell.chars[0] != NUL)
1813 {
1814 len = pos.col + 1;
1815 new_fill_attr = term->tl_default_color;
1816 }
1817 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001818 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001819 cell2cellattr(&cell, &new_fill_attr);
1820
1821 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1822 ++lines_skipped;
1823 else
1824 {
1825 while (lines_skipped > 0)
1826 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001827 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001828 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001829 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001830 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001831 }
1832
1833 if (len == 0)
1834 p = NULL;
1835 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001836 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001837 if ((p != NULL || len == 0)
1838 && ga_grow(&term->tl_scrollback, 1) == OK)
1839 {
1840 garray_T ga;
1841 int width;
1842 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1843 + term->tl_scrollback.ga_len;
1844
1845 ga_init2(&ga, 1, 100);
1846 for (pos.col = 0; pos.col < len; pos.col += width)
1847 {
1848 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1849 {
1850 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001851 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001852 if (ga_grow(&ga, 1) == OK)
1853 ga.ga_len += utf_char2bytes(' ',
1854 (char_u *)ga.ga_data + ga.ga_len);
1855 }
1856 else
1857 {
1858 width = cell.width;
1859
1860 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001861 if (width == 2)
1862 // second cell of double-width character has the
1863 // same attributes.
1864 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001865
Bram Moolenaara79fd562018-12-20 20:47:32 +01001866 // Each character can be up to 6 bytes.
1867 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001868 {
1869 int i;
1870 int c;
1871
1872 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1873 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1874 (char_u *)ga.ga_data + ga.ga_len);
1875 }
1876 }
1877 }
1878 line->sb_cols = len;
1879 line->sb_cells = p;
1880 line->sb_fill_attr = new_fill_attr;
1881 fill_attr = new_fill_attr;
1882 ++term->tl_scrollback.ga_len;
1883
1884 if (ga_grow(&ga, 1) == FAIL)
1885 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1886 else
1887 {
1888 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1889 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1890 }
1891 ga_clear(&ga);
1892 }
1893 else
1894 vim_free(p);
1895 }
1896 }
1897
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001898 // Add trailing empty lines.
1899 for (pos.row = term->tl_scrollback.ga_len;
1900 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1901 ++pos.row)
1902 {
1903 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1904 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1905 }
1906
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001907 term->tl_dirty_snapshot = FALSE;
1908#ifdef FEAT_TIMERS
1909 term->tl_timer_set = FALSE;
1910#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001911}
1912
1913/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001914 * Loop over all windows in the current tab, and also curwin, which is not
1915 * encountered when using a terminal in a popup window.
1916 * Return TRUE if "*wp" was set to the next window.
1917 */
1918 static int
1919for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1920{
1921 if (*wp == NULL)
1922 *wp = firstwin;
1923 else if ((*wp)->w_next != NULL)
1924 *wp = (*wp)->w_next;
1925 else if (!*did_curwin)
1926 *wp = curwin;
1927 else
1928 return FALSE;
1929 if (*wp == curwin)
1930 *did_curwin = TRUE;
1931 return TRUE;
1932}
1933
1934/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001935 * If needed, add the current lines of the terminal to scrollback and to the
1936 * buffer. Called after the job has ended and when switching to
1937 * Terminal-Normal mode.
1938 * When "redraw" is TRUE redraw the windows that show the terminal.
1939 */
1940 static void
1941may_move_terminal_to_buffer(term_T *term, int redraw)
1942{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001943 if (term->tl_vterm == NULL)
1944 return;
1945
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001946 // Update the snapshot only if something changes or the buffer does not
1947 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001948 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1949 <= term->tl_scrollback_scrolled)
1950 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001951
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001952 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001953 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1954 &term->tl_default_color.fg, &term->tl_default_color.bg);
1955
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001956 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001957 {
1958 win_T *wp = NULL;
1959 int did_curwin = FALSE;
1960
1961 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001962 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001963 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001964 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001965 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1966 wp->w_cursor.col = 0;
1967 wp->w_valid = 0;
1968 if (wp->w_cursor.lnum >= wp->w_height)
1969 {
1970 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001971
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001972 if (wp->w_topline < min_topline)
1973 wp->w_topline = min_topline;
1974 }
1975 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001976 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001977 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001978 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001979}
1980
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001981#if defined(FEAT_TIMERS) || defined(PROTO)
1982/*
1983 * Check if any terminal timer expired. If so, copy text from the terminal to
1984 * the buffer.
1985 * Return the time until the next timer will expire.
1986 */
1987 int
1988term_check_timers(int next_due_arg, proftime_T *now)
1989{
1990 term_T *term;
1991 int next_due = next_due_arg;
1992
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001993 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001994 {
1995 if (term->tl_timer_set && !term->tl_normal_mode)
1996 {
1997 long this_due = proftime_time_left(&term->tl_timer_due, now);
1998
1999 if (this_due <= 1)
2000 {
2001 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002002 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002003 }
2004 else if (next_due == -1 || next_due > this_due)
2005 next_due = this_due;
2006 }
2007 }
2008
2009 return next_due;
2010}
2011#endif
2012
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002013/*
2014 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
2015 * otherwise end it.
2016 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002017 static void
2018set_terminal_mode(term_T *term, int normal_mode)
2019{
2020 term->tl_normal_mode = normal_mode;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002021 trigger_modechanged();
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002022 if (!normal_mode)
2023 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002024 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002025 if (term->tl_buffer == curbuf)
2026 maketitle();
2027}
2028
2029/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002030 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002031 * Move the vterm contents into the scrollback buffer and free the vterm.
2032 */
2033 static void
2034cleanup_vterm(term_T *term)
2035{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002036 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002037 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002038 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002039 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002040}
2041
2042/*
2043 * Switch from Terminal-Job mode to Terminal-Normal mode.
2044 * Suspends updating the terminal window.
2045 */
2046 static void
2047term_enter_normal_mode(void)
2048{
2049 term_T *term = curbuf->b_term;
2050
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002051 set_terminal_mode(term, TRUE);
2052
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002053 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002054 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002055
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002056 // Move the window cursor to the position of the cursor in the
2057 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002058 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2059 + term->tl_cursor_pos.row + 1;
2060 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002061 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2062 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002063 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002064
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002065 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002066 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2067}
2068
2069/*
2070 * Returns TRUE if the current window contains a terminal and we are in
2071 * Terminal-Normal mode.
2072 */
2073 int
2074term_in_normal_mode(void)
2075{
2076 term_T *term = curbuf->b_term;
2077
2078 return term != NULL && term->tl_normal_mode;
2079}
2080
2081/*
2082 * Switch from Terminal-Normal mode to Terminal-Job mode.
2083 * Restores updating the terminal window.
2084 */
2085 void
2086term_enter_job_mode()
2087{
2088 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002089
2090 set_terminal_mode(term, FALSE);
2091
2092 if (term->tl_channel_closed)
2093 cleanup_vterm(term);
2094 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002095#ifdef FEAT_PROP_POPUP
2096 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002097 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002098#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002099}
2100
2101/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002102 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002103 * Note: while waiting a terminal may be closed and freed if the channel is
2104 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002105 */
2106 static int
2107term_vgetc()
2108{
2109 int c;
2110 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002111 int modify_other_keys =
2112 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002113
2114 State = TERMINAL;
2115 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002116#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002117 ctrl_break_was_pressed = FALSE;
2118#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002119 if (modify_other_keys)
2120 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002121 c = vgetc();
2122 got_int = FALSE;
2123 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002124 if (modify_other_keys)
2125 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002126 return c;
2127}
2128
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002129static int mouse_was_outside = FALSE;
2130
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002131/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002132 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002133 * Return FAIL when the key needs to be handled in Normal mode.
2134 * Return OK when the key was dropped or sent to the terminal.
2135 */
2136 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002137send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002138{
2139 char msg[KEY_BUF_LEN];
2140 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002141 int dragging_outside = FALSE;
2142
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002143 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002144 switch (c)
2145 {
2146 case NUL:
2147 case K_ZERO:
2148 if (typed)
2149 stuffcharReadbuff(c);
2150 return FAIL;
2151
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002152 case K_TABLINE:
2153 stuffcharReadbuff(c);
2154 return FAIL;
2155
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002156 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002157 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002158 return FAIL;
2159
2160 case K_LEFTDRAG:
2161 case K_MIDDLEDRAG:
2162 case K_RIGHTDRAG:
2163 case K_X1DRAG:
2164 case K_X2DRAG:
2165 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002166 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002167 case K_LEFTMOUSE:
2168 case K_LEFTMOUSE_NM:
2169 case K_LEFTRELEASE:
2170 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002171 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002172 case K_MIDDLEMOUSE:
2173 case K_MIDDLERELEASE:
2174 case K_RIGHTMOUSE:
2175 case K_RIGHTRELEASE:
2176 case K_X1MOUSE:
2177 case K_X1RELEASE:
2178 case K_X2MOUSE:
2179 case K_X2RELEASE:
2180
2181 case K_MOUSEUP:
2182 case K_MOUSEDOWN:
2183 case K_MOUSELEFT:
2184 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002185 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002186 int row = mouse_row;
2187 int col = mouse_col;
2188
2189#ifdef FEAT_PROP_POPUP
2190 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002191 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002192 row -= popup_top_extra(curwin);
2193 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002194 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002195#endif
2196 if (row < W_WINROW(curwin)
2197 || row >= (W_WINROW(curwin) + curwin->w_height)
2198 || col < curwin->w_wincol
2199 || col >= W_ENDCOL(curwin)
2200 || dragging_outside)
2201 {
2202 // click or scroll outside the current window or on status
2203 // line or vertical separator
2204 if (typed)
2205 {
2206 stuffcharReadbuff(c);
2207 mouse_was_outside = TRUE;
2208 }
2209 return FAIL;
2210 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002211 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002212 break;
2213
2214 case K_COMMAND:
2215 return do_cmdline(NULL, getcmdkeycmd, NULL, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002216 }
2217 if (typed)
2218 mouse_was_outside = FALSE;
2219
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002220 // Convert the typed key to a sequence of bytes for the job.
2221 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002222 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002223 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002224 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2225 (char_u *)msg, (int)len, NULL);
2226
2227 return OK;
2228}
2229
2230 static void
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002231position_cursor(win_T *wp, VTermPos *pos)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002232{
2233 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2234 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002235#ifdef FEAT_PROP_POPUP
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002236 if (popup_is_popup(wp))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002237 {
Bram Moolenaarf5452692020-11-28 21:56:06 +01002238 wp->w_wrow += popup_top_extra(wp);
2239 wp->w_wcol += popup_left_extra(wp);
Bram Moolenaar6a076442020-11-15 20:32:58 +01002240 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002241 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01002242 else
2243 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002244#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002245 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2246}
2247
2248/*
2249 * Handle CTRL-W "": send register contents to the job.
2250 */
2251 static void
2252term_paste_register(int prev_c UNUSED)
2253{
2254 int c;
2255 list_T *l;
2256 listitem_T *item;
2257 long reglen = 0;
2258 int type;
2259
2260#ifdef FEAT_CMDL_INFO
2261 if (add_to_showcmd(prev_c))
2262 if (add_to_showcmd('"'))
2263 out_flush();
2264#endif
2265 c = term_vgetc();
2266#ifdef FEAT_CMDL_INFO
2267 clear_showcmd();
2268#endif
2269 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002270 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002271 return;
2272
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002273 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002274 if (c == '=' && get_expr_register() != '=')
2275 return;
2276 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002277 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002278 return;
2279
2280 l = (list_T *)get_reg_contents(c, GREG_LIST);
2281 if (l != NULL)
2282 {
2283 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002284 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002285 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002286 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002287#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002288 char_u *tmp = s;
2289
2290 if (!enc_utf8 && enc_codepage > 0)
2291 {
2292 WCHAR *ret = NULL;
2293 int length = 0;
2294
2295 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2296 (int)STRLEN(s), &ret, &length);
2297 if (ret != NULL)
2298 {
2299 WideCharToMultiByte_alloc(CP_UTF8, 0,
2300 ret, length, (char **)&s, &length, 0, 0);
2301 vim_free(ret);
2302 }
2303 }
2304#endif
2305 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2306 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002307#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002308 if (tmp != s)
2309 vim_free(s);
2310#endif
2311
2312 if (item->li_next != NULL || type == MLINE)
2313 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2314 (char_u *)"\r", 1, NULL);
2315 }
2316 list_free(l);
2317 }
2318}
2319
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002320/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002321 * Return TRUE when waiting for a character in the terminal, the cursor of the
2322 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002323 */
2324 int
2325terminal_is_active()
2326{
2327 return in_terminal_loop != NULL;
2328}
2329
Bram Moolenaar83d47902020-03-26 20:34:00 +01002330/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002331 * Return the highight group ID for the terminal and the window.
Bram Moolenaar83d47902020-03-26 20:34:00 +01002332 */
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002333 static int
2334term_get_highlight_id(term_T *term, win_T *wp)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002335{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002336 char_u *name;
2337
2338 if (wp != NULL && *wp->w_p_wcr != NUL)
2339 name = wp->w_p_wcr;
2340 else if (term->tl_highlight_name != NULL)
2341 name = term->tl_highlight_name;
2342 else
2343 name = (char_u*)"Terminal";
2344
2345 return syn_name2id(name);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002346}
2347
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002348#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002349 cursorentry_T *
2350term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2351{
2352 term_T *term = in_terminal_loop;
2353 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002354 int id;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002355 guicolor_T term_fg = INVALCOLOR;
2356 guicolor_T term_bg = INVALCOLOR;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002357
Bram Moolenaara80faa82020-04-12 19:37:17 +02002358 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002359 entry.shape = entry.mshape =
2360 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2361 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2362 SHAPE_BLOCK;
2363 entry.percentage = 20;
2364 if (term->tl_cursor_blink)
2365 {
2366 entry.blinkwait = 700;
2367 entry.blinkon = 400;
2368 entry.blinkoff = 250;
2369 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002370
Bram Moolenaar83d47902020-03-26 20:34:00 +01002371 // The highlight group overrules the defaults.
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002372 id = term_get_highlight_id(term, curwin);
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002373 if (id != 0)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002374 syn_id2colors(id, &term_fg, &term_bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002375 if (term_bg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002376 *fg = term_bg;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002377 else
2378 *fg = gui.back_pixel;
2379
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002380 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002381 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002382 if (term_fg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002383 *bg = term_fg;
2384 else
2385 *bg = gui.norm_pixel;
2386 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002387 else
2388 *bg = color_name2handle(term->tl_cursor_color);
2389 entry.name = "n";
2390 entry.used_for = SHAPE_CURSOR;
2391
2392 return &entry;
2393}
2394#endif
2395
Bram Moolenaard317b382018-02-08 22:33:31 +01002396 static void
2397may_output_cursor_props(void)
2398{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002399 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002400 || last_set_cursor_shape != desired_cursor_shape
2401 || last_set_cursor_blink != desired_cursor_blink)
2402 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002403 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002404 last_set_cursor_shape = desired_cursor_shape;
2405 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002406 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002407 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002408 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002409 ui_cursor_shape_forced(TRUE);
2410 else
2411 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2412 }
2413}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002414
Bram Moolenaard317b382018-02-08 22:33:31 +01002415/*
2416 * Set the cursor color and shape, if not last set to these.
2417 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002418 static void
2419may_set_cursor_props(term_T *term)
2420{
2421#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002422 // For the GUI the cursor properties are obtained with
2423 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002424 if (gui.in_use)
2425 return;
2426#endif
2427 if (in_terminal_loop == term)
2428 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002429 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002430 desired_cursor_shape = term->tl_cursor_shape;
2431 desired_cursor_blink = term->tl_cursor_blink;
2432 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002433 }
2434}
2435
Bram Moolenaard317b382018-02-08 22:33:31 +01002436/*
2437 * Reset the desired cursor properties and restore them when needed.
2438 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002439 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002440prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002441{
2442#ifdef FEAT_GUI
2443 if (gui.in_use)
2444 return;
2445#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002446 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002447 desired_cursor_shape = -1;
2448 desired_cursor_blink = -1;
2449 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002450}
2451
2452/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002453 * Returns TRUE if the current window contains a terminal and we are sending
2454 * keys to the job.
2455 * If "check_job_status" is TRUE update the job status.
2456 */
2457 static int
2458term_use_loop_check(int check_job_status)
2459{
2460 term_T *term = curbuf->b_term;
2461
2462 return term != NULL
2463 && !term->tl_normal_mode
2464 && term->tl_vterm != NULL
2465 && term_job_running_check(term, check_job_status);
2466}
2467
2468/*
2469 * Returns TRUE if the current window contains a terminal and we are sending
2470 * keys to the job.
2471 */
2472 int
2473term_use_loop(void)
2474{
2475 return term_use_loop_check(FALSE);
2476}
2477
2478/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002479 * Called when entering a window with the mouse. If this is a terminal window
2480 * we may want to change state.
2481 */
2482 void
2483term_win_entered()
2484{
2485 term_T *term = curbuf->b_term;
2486
2487 if (term != NULL)
2488 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002489 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002490 {
2491 reset_VIsual_and_resel();
2492 if (State & INSERT)
2493 stop_insert_mode = TRUE;
2494 }
2495 mouse_was_outside = FALSE;
2496 enter_mouse_col = mouse_col;
2497 enter_mouse_row = mouse_row;
2498 }
2499}
2500
Bram Moolenaara48d4e42021-12-08 22:13:38 +00002501 void
2502term_focus_change(int in_focus)
2503{
2504 term_T *term = curbuf->b_term;
2505
2506 if (term != NULL && term->tl_vterm != NULL)
2507 {
2508 VTermState *state = vterm_obtain_state(term->tl_vterm);
2509
2510 if (in_focus)
2511 vterm_state_focus_in(state);
2512 else
2513 vterm_state_focus_out(state);
2514 term_forward_output(term);
2515 }
2516}
2517
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002518/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002519 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2520 * Return the Ctrl-key value in that case.
2521 */
2522 static int
2523raw_c_to_ctrl(int c)
2524{
2525 if ((mod_mask & MOD_MASK_CTRL)
2526 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2527 return c & 0x1f;
2528 return c;
2529}
2530
2531/*
2532 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2533 * May set "mod_mask".
2534 */
2535 static int
2536ctrl_to_raw_c(int c)
2537{
2538 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2539 {
2540 mod_mask |= MOD_MASK_CTRL;
2541 return c + '@';
2542 }
2543 return c;
2544}
2545
2546/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002547 * Wait for input and send it to the job.
2548 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2549 * when there is no more typahead.
2550 * Return when the start of a CTRL-W command is typed or anything else that
2551 * should be handled as a Normal mode command.
2552 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2553 * the terminal was closed.
2554 */
2555 int
2556terminal_loop(int blocking)
2557{
2558 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002559 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002560 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002561 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002562#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002563 int tty_fd = curbuf->b_term->tl_job->jv_channel
2564 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002565#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002566 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002567
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002568 // Remember the terminal we are sending keys to. However, the terminal
2569 // might be closed while waiting for a character, e.g. typing "exit" in a
2570 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2571 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002572 in_terminal_loop = curbuf->b_term;
2573
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002574 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002575 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002576 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002577 if (termwinkey == Ctrl_W)
2578 termwinkey = 0;
2579 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002580 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002581 may_set_cursor_props(curbuf->b_term);
2582
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002583 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002584 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002585#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002586 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002587#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002588 // TODO: skip screen update when handling a sequence of keys.
2589 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002590 while (must_redraw != 0)
2591 if (update_screen(0) == FAIL)
2592 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002593 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002594 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002595 break;
2596
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002597 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002598 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002599
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002600 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002601 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002602 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002603 // Job finished while waiting for a character. Push back the
2604 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002605 if (raw_c != K_IGNORE)
2606 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002607 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002608 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002609 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002610 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002611 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002612
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002613#ifdef UNIX
2614 /*
2615 * The shell or another program may change the tty settings. Getting
2616 * them for every typed character is a bit of overhead, but it's needed
2617 * for the first character typed, e.g. when Vim starts in a shell.
2618 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002619 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002620 {
2621 ttyinfo_T info;
2622
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002623 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002624 if (get_tty_info(tty_fd, &info) == OK)
2625 term_backspace_char = info.backspace;
2626 }
2627#endif
2628
Bram Moolenaar4f974752019-02-17 17:44:42 +01002629#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002630 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2631 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002632 if (ctrl_break_was_pressed)
2633 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2634#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002635 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2636 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002637 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002638#ifdef FEAT_GUI
2639 && !curbuf->b_term->tl_system
2640#endif
2641 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002642 {
2643 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002644 int prev_raw_c = raw_c;
2645 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002646
2647#ifdef FEAT_CMDL_INFO
2648 if (add_to_showcmd(c))
2649 out_flush();
2650#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002651 raw_c = term_vgetc();
2652 c = raw_c_to_ctrl(raw_c);
2653
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002654#ifdef FEAT_CMDL_INFO
2655 clear_showcmd();
2656#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002657 if (!term_use_loop_check(TRUE)
2658 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002659 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002660 break;
2661
2662 if (prev_c == Ctrl_BSL)
2663 {
2664 if (c == Ctrl_N)
2665 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002666 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002667 term_enter_normal_mode();
2668 ret = FAIL;
2669 goto theend;
2670 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002671 // Send both keys to the terminal, first one here, second one
2672 // below.
2673 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2674 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002675 }
2676 else if (c == Ctrl_C)
2677 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002678 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002679 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2680 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002681 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002682 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002683 // "CTRL-W .": send CTRL-W to the job
2684 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002685 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002686 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002687 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002688 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002689 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002690 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002691 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002692 else if (c == 'N')
2693 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002694 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002695 term_enter_normal_mode();
2696 ret = FAIL;
2697 goto theend;
2698 }
2699 else if (c == '"')
2700 {
2701 term_paste_register(prev_c);
2702 continue;
2703 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002704 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002705 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002706 // space for CTRL-W, modifier, multi-byte char and NUL
2707 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002708
2709 // Put the command into the typeahead buffer, when using the
2710 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2711 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002712 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002713 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002714 ret = OK;
2715 goto theend;
2716 }
2717 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002718# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002719 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002720 {
2721 WCHAR wc;
2722 char_u mb[3];
2723
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002724 mb[0] = (unsigned)raw_c >> 8;
2725 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002726 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002727 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002728 }
2729# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002730 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002731 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002732 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002733 // We are sure to come back here, don't reset the cursor color
2734 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002735 restore_cursor = FALSE;
2736
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002737 ret = OK;
2738 goto theend;
2739 }
2740 }
2741 ret = FAIL;
2742
2743theend:
2744 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002745 if (restore_cursor)
2746 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002747
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002748 // Move a snapshot of the screen contents to the buffer, so that completion
2749 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002750 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2751 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002752
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002753 return ret;
2754}
2755
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002756 static void
2757may_toggle_cursor(term_T *term)
2758{
2759 if (in_terminal_loop == term)
2760 {
2761 if (term->tl_cursor_visible)
2762 cursor_on();
2763 else
2764 cursor_off();
2765 }
2766}
2767
2768/*
2769 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002770 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002771 */
2772 static int
2773color2index(VTermColor *color, int fg, int *boldp)
2774{
2775 int red = color->red;
2776 int blue = color->blue;
2777 int green = color->green;
2778
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002779 if (VTERM_COLOR_IS_INVALID(color))
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002780 return 0;
2781 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002782 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002783 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002784 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002785 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002786 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002787 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2788 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2789 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002790 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002791 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2792 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2793 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2794 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2795 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2796 case 10: return lookup_color(20, fg, boldp) + 1; // red
2797 case 11: return lookup_color(16, fg, boldp) + 1; // green
2798 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2799 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2800 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2801 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2802 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002803 }
2804 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002805
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002806 if (t_colors >= 256)
2807 {
2808 if (red == blue && red == green)
2809 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002810 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002811 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002812 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2813 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2814 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002815 int i;
2816
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002817 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002818 return 17; // 00/00/00
2819 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002820 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002821 for (i = 0; i < 23; ++i)
2822 if (red < cutoff[i])
2823 return i + 233;
2824 return 256;
2825 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002826 {
2827 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2828 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002829
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002830 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002831 for (ri = 0; ri < 5; ++ri)
2832 if (red < cutoff[ri])
2833 break;
2834 for (gi = 0; gi < 5; ++gi)
2835 if (green < cutoff[gi])
2836 break;
2837 for (bi = 0; bi < 5; ++bi)
2838 if (blue < cutoff[bi])
2839 break;
2840 return 17 + ri * 36 + gi * 6 + bi;
2841 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002842 }
2843 return 0;
2844}
2845
2846/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002847 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002848 */
2849 static int
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002850vtermAttr2hl(VTermScreenCellAttrs *cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002851{
2852 int attr = 0;
2853
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002854 if (cellattrs->bold)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002855 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002856 if (cellattrs->underline)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002857 attr |= HL_UNDERLINE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002858 if (cellattrs->italic)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002859 attr |= HL_ITALIC;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002860 if (cellattrs->strike)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002861 attr |= HL_STRIKETHROUGH;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002862 if (cellattrs->reverse)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002863 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002864 return attr;
2865}
2866
2867/*
2868 * Store Vterm attributes in "cell" from highlight flags.
2869 */
2870 static void
2871hl2vtermAttr(int attr, cellattr_T *cell)
2872{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002873 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002874 if (attr & HL_BOLD)
2875 cell->attrs.bold = 1;
2876 if (attr & HL_UNDERLINE)
2877 cell->attrs.underline = 1;
2878 if (attr & HL_ITALIC)
2879 cell->attrs.italic = 1;
2880 if (attr & HL_STRIKETHROUGH)
2881 cell->attrs.strike = 1;
2882 if (attr & HL_INVERSE)
2883 cell->attrs.reverse = 1;
2884}
2885
2886/*
2887 * Convert the attributes of a vterm cell into an attribute index.
2888 */
2889 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002890cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002891 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002892 win_T *wp,
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002893 VTermScreenCellAttrs *cellattrs,
2894 VTermColor *cellfg,
2895 VTermColor *cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002896{
2897 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002898 VTermColor *fg = cellfg;
2899 VTermColor *bg = cellbg;
2900 int is_default_fg = VTERM_COLOR_IS_DEFAULT_FG(fg);
2901 int is_default_bg = VTERM_COLOR_IS_DEFAULT_BG(bg);
2902
2903 if (is_default_fg || is_default_bg)
2904 {
2905 if (wp != NULL && *wp->w_p_wcr != NUL)
2906 {
2907 if (is_default_fg)
2908 fg = &wp->w_term_wincolor.fg;
2909 if (is_default_bg)
2910 bg = &wp->w_term_wincolor.bg;
2911 }
2912 else
2913 {
2914 if (is_default_fg)
2915 fg = &term->tl_default_color.fg;
2916 if (is_default_bg)
2917 bg = &term->tl_default_color.bg;
2918 }
2919 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002920
2921#ifdef FEAT_GUI
2922 if (gui.in_use)
2923 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002924 guicolor_T guifg = gui_mch_get_rgb_color(fg->red, fg->green, fg->blue);
2925 guicolor_T guibg = gui_mch_get_rgb_color(bg->red, bg->green, bg->blue);
2926 return get_gui_attr_idx(attr, guifg, guibg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002927 }
2928 else
2929#endif
2930#ifdef FEAT_TERMGUICOLORS
2931 if (p_tgc)
2932 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002933 guicolor_T tgcfg = VTERM_COLOR_IS_INVALID(fg)
2934 ? INVALCOLOR
2935 : gui_get_rgb_color_cmn(fg->red, fg->green, fg->blue);
2936 guicolor_T tgcbg = VTERM_COLOR_IS_INVALID(bg)
2937 ? INVALCOLOR
2938 : gui_get_rgb_color_cmn(bg->red, bg->green, bg->blue);
2939 return get_tgc_attr_idx(attr, tgcfg, tgcbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002940 }
2941 else
2942#endif
2943 {
2944 int bold = MAYBE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002945 int ctermfg = color2index(fg, TRUE, &bold);
2946 int ctermbg = color2index(bg, FALSE, &bold);
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002947
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002948 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002949 if (bold == TRUE)
2950 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002951
2952 return get_cterm_attr_idx(attr, ctermfg, ctermbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002953 }
2954 return 0;
2955}
2956
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002957 static void
2958set_dirty_snapshot(term_T *term)
2959{
2960 term->tl_dirty_snapshot = TRUE;
2961#ifdef FEAT_TIMERS
2962 if (!term->tl_normal_mode)
2963 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002964 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002965 profile_setlimit(100L, &term->tl_timer_due);
2966 term->tl_timer_set = TRUE;
2967 }
2968#endif
2969}
2970
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002971 static int
2972handle_damage(VTermRect rect, void *user)
2973{
2974 term_T *term = (term_T *)user;
2975
2976 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2977 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002978 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002979 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002980 return 1;
2981}
2982
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002983 static void
2984term_scroll_up(term_T *term, int start_row, int count)
2985{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002986 win_T *wp = NULL;
2987 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002988 VTermColor fg, bg;
2989 VTermScreenCellAttrs attr;
2990 int clear_attr;
2991
Bram Moolenaara80faa82020-04-12 19:37:17 +02002992 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002993
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002994 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002995 {
2996 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002997 {
2998 // Set the color to clear lines with.
2999 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
3000 &fg, &bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003001 clear_attr = cell2attr(term, wp, &attr, &fg, &bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003002 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003003 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003004 }
3005}
3006
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003007 static int
3008handle_moverect(VTermRect dest, VTermRect src, void *user)
3009{
3010 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003011 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003012
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003013 // Scrolling up is done much more efficiently by deleting lines instead of
3014 // redrawing the text. But avoid doing this multiple times, postpone until
3015 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003016 if (dest.start_col == src.start_col
3017 && dest.end_col == src.end_col
3018 && dest.start_row < src.start_row)
3019 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003020 if (dest.start_row == 0)
3021 term->tl_postponed_scroll += count;
3022 else
3023 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003024 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003025
3026 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3027 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003028 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003029
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003030 // Note sure if the scrolling will work correctly, let's do a complete
3031 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003032 redraw_buf_later(term->tl_buffer, NOT_VALID);
3033 return 1;
3034}
3035
3036 static int
3037handle_movecursor(
3038 VTermPos pos,
3039 VTermPos oldpos UNUSED,
3040 int visible,
3041 void *user)
3042{
3043 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003044 win_T *wp = NULL;
3045 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003046
3047 term->tl_cursor_pos = pos;
3048 term->tl_cursor_visible = visible;
3049
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003050 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003051 {
3052 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003053 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003054 }
3055 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003056 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003057
3058 return 1;
3059}
3060
3061 static int
3062handle_settermprop(
3063 VTermProp prop,
3064 VTermValue *value,
3065 void *user)
3066{
3067 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003068 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003069
3070 switch (prop)
3071 {
3072 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003073 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003074 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003075 if (strval == NULL)
3076 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003077 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003078 // a blank title isn't useful, make it empty, so that "running" is
3079 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003080 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003081 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003082 // Same as blank
3083 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003084 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003085 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3086 term->tl_title = NULL;
3087 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003088 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003089 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003090#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003091 else if (!enc_utf8 && enc_codepage > 0)
3092 {
3093 WCHAR *ret = NULL;
3094 int length = 0;
3095
3096 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003097 (char*)value->string.str,
3098 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003099 if (ret != NULL)
3100 {
3101 WideCharToMultiByte_alloc(enc_codepage, 0,
3102 ret, length, (char**)&term->tl_title,
3103 &length, 0, 0);
3104 vim_free(ret);
3105 }
3106 }
3107#endif
3108 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003109 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003110 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003111 strval = NULL;
3112 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003113 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003114 if (term == curbuf->b_term)
3115 maketitle();
3116 break;
3117
3118 case VTERM_PROP_CURSORVISIBLE:
3119 term->tl_cursor_visible = value->boolean;
3120 may_toggle_cursor(term);
3121 out_flush();
3122 break;
3123
3124 case VTERM_PROP_CURSORBLINK:
3125 term->tl_cursor_blink = value->boolean;
3126 may_set_cursor_props(term);
3127 break;
3128
3129 case VTERM_PROP_CURSORSHAPE:
3130 term->tl_cursor_shape = value->number;
3131 may_set_cursor_props(term);
3132 break;
3133
3134 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003135 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003136 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003137 if (strval == NULL)
3138 break;
3139 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003140 may_set_cursor_props(term);
3141 break;
3142
3143 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003144 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003145 term->tl_using_altscreen = value->boolean;
3146 break;
3147
3148 default:
3149 break;
3150 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003151 vim_free(strval);
3152
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003153 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003154 return 1;
3155}
3156
3157/*
3158 * The job running in the terminal resized the terminal.
3159 */
3160 static int
3161handle_resize(int rows, int cols, void *user)
3162{
3163 term_T *term = (term_T *)user;
3164 win_T *wp;
3165
3166 term->tl_rows = rows;
3167 term->tl_cols = cols;
3168 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003169 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003170 term->tl_vterm_size_changed = FALSE;
3171 else
3172 {
3173 FOR_ALL_WINDOWS(wp)
3174 {
3175 if (wp->w_buffer == term->tl_buffer)
3176 {
3177 win_setheight_win(rows, wp);
3178 win_setwidth_win(cols, wp);
3179 }
3180 }
3181 redraw_buf_later(term->tl_buffer, NOT_VALID);
3182 }
3183 return 1;
3184}
3185
3186/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003187 * If the number of lines that are stored goes over 'termscrollback' then
3188 * delete the first 10%.
3189 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3190 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003191 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003192 static void
3193limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003194{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003195 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003196 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003197 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003198 int i;
3199
3200 curbuf = term->tl_buffer;
3201 for (i = 0; i < todo; ++i)
3202 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003203 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3204 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003205 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003206 }
3207 curbuf = curwin->w_buffer;
3208
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003209 gap->ga_len -= todo;
3210 mch_memmove(gap->ga_data,
3211 (sb_line_T *)gap->ga_data + todo,
3212 sizeof(sb_line_T) * gap->ga_len);
3213 if (update_buffer)
3214 term->tl_scrollback_scrolled -= todo;
3215 }
3216}
3217
3218/*
3219 * Handle a line that is pushed off the top of the screen.
3220 */
3221 static int
3222handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3223{
3224 term_T *term = (term_T *)user;
3225 garray_T *gap;
3226 int update_buffer;
3227
3228 if (term->tl_normal_mode)
3229 {
3230 // In Terminal-Normal mode the user interacts with the buffer, thus we
3231 // must not change it. Postpone adding the scrollback lines.
3232 gap = &term->tl_scrollback_postponed;
3233 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003234 }
3235 else
3236 {
3237 // First remove the lines that were appended before, the pushed line
3238 // goes above it.
3239 cleanup_scrollback(term);
3240 gap = &term->tl_scrollback;
3241 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003242 }
3243
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003244 limit_scrollback(term, gap, update_buffer);
3245
3246 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003247 {
3248 cellattr_T *p = NULL;
3249 int len = 0;
3250 int i;
3251 int c;
3252 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003253 int text_len;
3254 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003255 sb_line_T *line;
3256 garray_T ga;
3257 cellattr_T fill_attr = term->tl_default_color;
3258
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003259 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003260 for (i = 0; i < cols; ++i)
3261 if (cells[i].chars[0] != 0)
3262 len = i + 1;
3263 else
3264 cell2cellattr(&cells[i], &fill_attr);
3265
3266 ga_init2(&ga, 1, 100);
3267 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003268 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003269 if (p != NULL)
3270 {
3271 for (col = 0; col < len; col += cells[col].width)
3272 {
3273 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3274 {
3275 ga.ga_len = 0;
3276 break;
3277 }
3278 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3279 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3280 (char_u *)ga.ga_data + ga.ga_len);
3281 cell2cellattr(&cells[col], &p[col]);
3282 }
3283 }
3284 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003285 {
3286 if (update_buffer)
3287 text = (char_u *)"";
3288 else
3289 text = vim_strsave((char_u *)"");
3290 text_len = 0;
3291 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003292 else
3293 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003294 text = ga.ga_data;
3295 text_len = ga.ga_len;
3296 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003297 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003298 if (update_buffer)
3299 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003300
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003301 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003302 line->sb_cols = len;
3303 line->sb_cells = p;
3304 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003305 if (update_buffer)
3306 {
3307 line->sb_text = NULL;
3308 ++term->tl_scrollback_scrolled;
3309 ga_clear(&ga); // free the text
3310 }
3311 else
3312 {
3313 line->sb_text = text;
3314 ga_init(&ga); // text is kept in tl_scrollback_postponed
3315 }
3316 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003317 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003318 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003319}
3320
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003321/*
3322 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3323 * received and stored in tl_scrollback_postponed.
3324 */
3325 static void
3326handle_postponed_scrollback(term_T *term)
3327{
3328 int i;
3329
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003330 if (term->tl_scrollback_postponed.ga_len == 0)
3331 return;
3332 ch_log(NULL, "Moving postponed scrollback to scrollback");
3333
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003334 // First remove the lines that were appended before, the pushed lines go
3335 // above it.
3336 cleanup_scrollback(term);
3337
3338 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3339 {
3340 char_u *text;
3341 sb_line_T *pp_line;
3342 sb_line_T *line;
3343
3344 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3345 break;
3346 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3347
3348 text = pp_line->sb_text;
3349 if (text == NULL)
3350 text = (char_u *)"";
3351 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3352 vim_free(pp_line->sb_text);
3353
3354 line = (sb_line_T *)term->tl_scrollback.ga_data
3355 + term->tl_scrollback.ga_len;
3356 line->sb_cols = pp_line->sb_cols;
3357 line->sb_cells = pp_line->sb_cells;
3358 line->sb_fill_attr = pp_line->sb_fill_attr;
3359 line->sb_text = NULL;
3360 ++term->tl_scrollback_scrolled;
3361 ++term->tl_scrollback.ga_len;
3362 }
3363
3364 ga_clear(&term->tl_scrollback_postponed);
3365 limit_scrollback(term, &term->tl_scrollback, TRUE);
3366}
3367
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003368static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003369 handle_damage, // damage
3370 handle_moverect, // moverect
3371 handle_movecursor, // movecursor
3372 handle_settermprop, // settermprop
3373 NULL, // bell
3374 handle_resize, // resize
3375 handle_pushline, // sb_pushline
3376 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003377};
3378
3379/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003380 * Do the work after the channel of a terminal was closed.
3381 * Must be called only when updating_screen is FALSE.
3382 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3383 */
3384 static int
3385term_after_channel_closed(term_T *term)
3386{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003387 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003388 if (!term->tl_normal_mode)
3389 {
3390 int fnum = term->tl_buffer->b_fnum;
3391
3392 cleanup_vterm(term);
3393
3394 if (term->tl_finish == TL_FINISH_CLOSE)
3395 {
3396 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003397 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003398#ifdef FEAT_PROP_POPUP
3399 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003400
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003401 // If this was a terminal in a popup window, go back to the
3402 // previous window.
3403 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3404 {
3405 pwin = curwin;
3406 if (win_valid(prevwin))
3407 win_enter(prevwin, FALSE);
3408 }
3409 else
3410#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003411 // If this is the last normal window: exit Vim.
3412 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3413 {
3414 exarg_T ea;
3415
Bram Moolenaara80faa82020-04-12 19:37:17 +02003416 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003417 ex_quit(&ea);
3418 return TRUE;
3419 }
3420
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003421 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003422 ch_log(NULL, "terminal job finished, closing window");
3423 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003424 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003425 if (curwin == aucmd_win)
3426 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003427 if (do_set_w_closing)
3428 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003429 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003430 if (do_set_w_closing)
3431 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003432 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003433#ifdef FEAT_PROP_POPUP
3434 if (pwin != NULL)
3435 popup_close_with_retval(pwin, 0);
3436#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003437 return TRUE;
3438 }
3439 if (term->tl_finish == TL_FINISH_OPEN
3440 && term->tl_buffer->b_nwindows == 0)
3441 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003442 char *cmd = term->tl_opencmd == NULL
3443 ? "botright sbuf %d"
3444 : (char *)term->tl_opencmd;
3445 size_t len = strlen(cmd) + 50;
3446 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003447
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003448 if (buf != NULL)
3449 {
3450 ch_log(NULL, "terminal job finished, opening window");
3451 vim_snprintf(buf, len, cmd, fnum);
3452 do_cmdline_cmd((char_u *)buf);
3453 vim_free(buf);
3454 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003455 }
3456 else
3457 ch_log(NULL, "terminal job finished");
3458 }
3459
3460 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3461 return FALSE;
3462}
3463
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003464#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3465/*
3466 * If the current window is a terminal in a popup window and the job has
3467 * finished, close the popup window and to back to the previous window.
3468 * Otherwise return FAIL.
3469 */
3470 int
3471may_close_term_popup(void)
3472{
3473 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3474 && !term_job_running(curbuf->b_term))
3475 {
3476 win_T *pwin = curwin;
3477
3478 if (win_valid(prevwin))
3479 win_enter(prevwin, FALSE);
3480 popup_close_with_retval(pwin, 0);
3481 return OK;
3482 }
3483 return FAIL;
3484}
3485#endif
3486
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003487/*
Bram Moolenaareea32af2021-11-21 14:51:13 +00003488 * Called when a channel is going to be closed, before invoking the close
3489 * callback.
3490 */
3491 void
3492term_channel_closing(channel_T *ch)
3493{
3494 term_T *term;
3495
3496 for (term = first_term; term != NULL; term = term->tl_next)
3497 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
3498 term->tl_channel_closing = TRUE;
3499}
3500
3501/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003502 * Called when a channel has been closed.
3503 * If this was a channel for a terminal window then finish it up.
3504 */
3505 void
3506term_channel_closed(channel_T *ch)
3507{
3508 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003509 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003510 int did_one = FALSE;
3511
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003512 for (term = first_term; term != NULL; term = next_term)
3513 {
3514 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003515 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003516 {
3517 term->tl_channel_closed = TRUE;
3518 did_one = TRUE;
3519
Bram Moolenaard23a8232018-02-10 18:45:26 +01003520 VIM_CLEAR(term->tl_title);
3521 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003522#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003523 if (term->tl_out_fd != NULL)
3524 {
3525 fclose(term->tl_out_fd);
3526 term->tl_out_fd = NULL;
3527 }
3528#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003529
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003530 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003531 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003532 // Cannot open or close windows now. Can happen when
3533 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003534 term->tl_channel_recently_closed = TRUE;
3535 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003536 }
3537
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003538 if (term_after_channel_closed(term))
3539 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003540 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003541 }
3542
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003543 if (did_one)
3544 {
3545 redraw_statuslines();
3546
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003547 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003548 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003549 typebuf_was_filled = TRUE;
3550
3551 term = curbuf->b_term;
3552 if (term != NULL)
3553 {
3554 if (term->tl_job == ch->ch_job)
3555 maketitle();
3556 update_cursor(term, term->tl_cursor_visible);
3557 }
3558 }
3559}
3560
3561/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003562 * To be called after resetting updating_screen: handle any terminal where the
3563 * channel was closed.
3564 */
3565 void
3566term_check_channel_closed_recently()
3567{
3568 term_T *term;
3569 term_T *next_term;
3570
3571 for (term = first_term; term != NULL; term = next_term)
3572 {
3573 next_term = term->tl_next;
3574 if (term->tl_channel_recently_closed)
3575 {
3576 term->tl_channel_recently_closed = FALSE;
3577 if (term_after_channel_closed(term))
3578 // start over, the list may have changed
3579 next_term = first_term;
3580 }
3581 }
3582}
3583
3584/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003585 * Fill one screen line from a line of the terminal.
3586 * Advances "pos" to past the last column.
3587 */
3588 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003589term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003590 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003591 win_T *wp,
3592 VTermScreen *screen,
3593 VTermPos *pos,
3594 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003595{
3596 int off = screen_get_current_line_off();
3597
3598 for (pos->col = 0; pos->col < max_col; )
3599 {
3600 VTermScreenCell cell;
3601 int c;
3602
3603 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003604 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003605
3606 c = cell.chars[0];
3607 if (c == NUL)
3608 {
3609 ScreenLines[off] = ' ';
3610 if (enc_utf8)
3611 ScreenLinesUC[off] = NUL;
3612 }
3613 else
3614 {
3615 if (enc_utf8)
3616 {
3617 int i;
3618
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003619 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003620 for (i = 0; i < Screen_mco
3621 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3622 {
3623 ScreenLinesC[i][off] = cell.chars[i + 1];
3624 if (cell.chars[i + 1] == 0)
3625 break;
3626 }
3627 if (c >= 0x80 || (Screen_mco > 0
3628 && ScreenLinesC[0][off] != 0))
3629 {
3630 ScreenLines[off] = ' ';
3631 ScreenLinesUC[off] = c;
3632 }
3633 else
3634 {
3635 ScreenLines[off] = c;
3636 ScreenLinesUC[off] = NUL;
3637 }
3638 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003639#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003640 else if (has_mbyte && c >= 0x80)
3641 {
3642 char_u mb[MB_MAXBYTES+1];
3643 WCHAR wc = c;
3644
3645 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3646 (char*)mb, 2, 0, 0) > 1)
3647 {
3648 ScreenLines[off] = mb[0];
3649 ScreenLines[off + 1] = mb[1];
3650 cell.width = mb_ptr2cells(mb);
3651 }
3652 else
3653 ScreenLines[off] = c;
3654 }
3655#endif
3656 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003657 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003658 ScreenLines[off] = c;
3659 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003660 ScreenAttrs[off] = cell2attr(term, wp, &cell.attrs, &cell.fg,
3661 &cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003662
3663 ++pos->col;
3664 ++off;
3665 if (cell.width == 2)
3666 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003667 // don't set the second byte to NUL for a DBCS encoding, it
3668 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003669 if (enc_utf8)
3670 {
3671 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003672 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003673 }
3674 else if (!has_mbyte)
3675 {
3676 // Can't show a double-width character with a single-byte
3677 // 'encoding', just use a space.
3678 ScreenLines[off] = ' ';
3679 ScreenAttrs[off] = ScreenAttrs[off - 1];
3680 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003681
3682 ++pos->col;
3683 ++off;
3684 }
3685 }
3686}
3687
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003688#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003689 static void
3690update_system_term(term_T *term)
3691{
3692 VTermPos pos;
3693 VTermScreen *screen;
3694
3695 if (term->tl_vterm == NULL)
3696 return;
3697 screen = vterm_obtain_screen(term->tl_vterm);
3698
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003699 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003700 while (term->tl_toprow > 0
3701 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3702 {
3703 int save_p_more = p_more;
3704
3705 p_more = FALSE;
3706 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003707 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003708 p_more = save_p_more;
3709 --term->tl_toprow;
3710 }
3711
3712 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3713 && pos.row < Rows; ++pos.row)
3714 {
3715 if (pos.row < term->tl_rows)
3716 {
3717 int max_col = MIN(Columns, term->tl_cols);
3718
Bram Moolenaar83d47902020-03-26 20:34:00 +01003719 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003720 }
3721 else
3722 pos.col = 0;
3723
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003724 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003725 }
3726
3727 term->tl_dirty_row_start = MAX_ROW;
3728 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003729}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003730#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003731
3732/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003733 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3734 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003735 * Terminal-Normal mode.
3736 */
3737 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003738term_do_update_window(win_T *wp)
3739{
3740 term_T *term = wp->w_buffer->b_term;
3741
3742 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3743}
3744
3745/*
3746 * Called to update a window that contains an active terminal.
3747 */
3748 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003749term_update_window(win_T *wp)
3750{
3751 term_T *term = wp->w_buffer->b_term;
3752 VTerm *vterm;
3753 VTermScreen *screen;
3754 VTermState *state;
3755 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003756 int rows, cols;
3757 int newrows, newcols;
3758 int minsize;
3759 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003760
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003761 vterm = term->tl_vterm;
3762 screen = vterm_obtain_screen(vterm);
3763 state = vterm_obtain_state(vterm);
3764
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003765 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3766 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003767 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003768 {
3769 term->tl_dirty_row_start = 0;
3770 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003771
3772 if (term->tl_postponed_scroll > 0
3773 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003774 // Scrolling is usually faster than redrawing, when there are only
3775 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003776 term_scroll_up(term, 0, term->tl_postponed_scroll);
3777 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003778 }
3779
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003780 /*
3781 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003782 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003783 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003784 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003785
Bram Moolenaar498c2562018-04-15 23:45:15 +02003786 newrows = 99999;
3787 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003788 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003789 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003790 // Always use curwin, it may be a popup window.
3791 win_T *wwp = twp == NULL ? curwin : twp;
3792
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003793 // When more than one window shows the same terminal, use the
3794 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003795 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003796 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003797 newrows = MIN(newrows, wwp->w_height);
3798 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003799 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003800 if (twp == NULL)
3801 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003802 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003803 if (newrows == 99999 || newcols == 99999)
3804 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003805 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3806 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3807
Bram Moolenaareba13e42021-02-23 17:47:23 +01003808 // If no cell is visible there is no point in resizing. Also, vterm can't
3809 // handle a zero height.
3810 if (newrows == 0 || newcols == 0)
3811 return;
3812
Bram Moolenaar498c2562018-04-15 23:45:15 +02003813 if (term->tl_rows != newrows || term->tl_cols != newcols)
3814 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003815 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003816 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003817 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003818 newrows);
3819 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003820
3821 // Updating the terminal size will cause the snapshot to be cleared.
3822 // When not in terminal_loop() we need to restore it.
3823 if (term != in_terminal_loop)
3824 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003825 }
3826
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003827 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003828 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003829 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003830
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003831 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3832 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003833 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003834 if (pos.row < term->tl_rows)
3835 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003836 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003837
Bram Moolenaar83d47902020-03-26 20:34:00 +01003838 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003839 }
3840 else
3841 pos.col = 0;
3842
Bram Moolenaarf118d482018-03-13 13:14:00 +01003843 screen_line(wp->w_winrow + pos.row
3844#ifdef FEAT_MENU
3845 + winbar_height(wp)
3846#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003847 , wp->w_wincol, pos.col, wp->w_width,
3848#ifdef FEAT_PROP_POPUP
3849 popup_is_popup(wp) ? SLF_POPUP :
3850#endif
3851 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003852 }
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00003853}
3854
3855/*
3856 * Called after updating all windows: may reset dirty rows.
3857 */
3858 void
3859term_did_update_window(win_T *wp)
3860{
3861 term_T *term = wp->w_buffer->b_term;
3862
3863 if (term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode
3864 && wp->w_redr_type == 0)
3865 {
3866 term->tl_dirty_row_start = MAX_ROW;
3867 term->tl_dirty_row_end = 0;
3868 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003869}
3870
3871/*
3872 * Return TRUE if "wp" is a terminal window where the job has finished.
3873 */
3874 int
3875term_is_finished(buf_T *buf)
3876{
3877 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3878}
3879
3880/*
3881 * Return TRUE if "wp" is a terminal window where the job has finished or we
3882 * are in Terminal-Normal mode, thus we show the buffer contents.
3883 */
3884 int
3885term_show_buffer(buf_T *buf)
3886{
3887 term_T *term = buf->b_term;
3888
3889 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3890}
3891
3892/*
3893 * The current buffer is going to be changed. If there is terminal
3894 * highlighting remove it now.
3895 */
3896 void
3897term_change_in_curbuf(void)
3898{
3899 term_T *term = curbuf->b_term;
3900
3901 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3902 {
3903 free_scrollback(term);
3904 redraw_buf_later(term->tl_buffer, NOT_VALID);
3905
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003906 // The buffer is now like a normal buffer, it cannot be easily
3907 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003908 set_string_option_direct((char_u *)"buftype", -1,
3909 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3910 }
3911}
3912
3913/*
3914 * Get the screen attribute for a position in the buffer.
3915 * Use a negative "col" to get the filler background color.
3916 */
3917 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003918term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003919{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003920 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003921 term_T *term = buf->b_term;
3922 sb_line_T *line;
3923 cellattr_T *cellattr;
3924
3925 if (lnum > term->tl_scrollback.ga_len)
3926 cellattr = &term->tl_default_color;
3927 else
3928 {
3929 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3930 if (col < 0 || col >= line->sb_cols)
3931 cellattr = &line->sb_fill_attr;
3932 else
3933 cellattr = line->sb_cells + col;
3934 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003935 return cell2attr(term, wp, &cellattr->attrs, &cellattr->fg, &cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003936}
3937
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003938/*
3939 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003940 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003941 */
3942 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003943cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003944{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003945 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3946 if (rgb->index == 0)
3947 rgb->type = VTERM_COLOR_RGB;
3948 else
3949 {
3950 rgb->type = VTERM_COLOR_INDEXED;
3951 --rgb->index;
3952 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003953}
3954
3955/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003956 * Initialize vterm color from the synID.
3957 * Returns TRUE if color is set to "fg" and "bg".
3958 * Otherwise returns FALSE.
3959 */
3960 static int
3961get_vterm_color_from_synid(int id, VTermColor *fg, VTermColor *bg)
3962{
3963#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3964 // Use the actual color for the GUI and when 'termguicolors' is set.
3965 if (0
3966# ifdef FEAT_GUI
3967 || gui.in_use
3968# endif
3969# ifdef FEAT_TERMGUICOLORS
3970 || p_tgc
3971# ifdef FEAT_VTP
3972 // Finally get INVALCOLOR on this execution path
3973 || (!p_tgc && t_colors >= 256)
3974# endif
3975# endif
3976 )
3977 {
3978 guicolor_T fg_rgb = INVALCOLOR;
3979 guicolor_T bg_rgb = INVALCOLOR;
3980
3981 if (id > 0)
3982 syn_id2colors(id, &fg_rgb, &bg_rgb);
3983
3984 if (fg_rgb != INVALCOLOR)
3985 {
3986 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3987 fg->red = (unsigned)(rgb >> 16);
3988 fg->green = (unsigned)(rgb >> 8) & 255;
3989 fg->blue = (unsigned)rgb & 255;
3990 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3991 }
3992 else
3993 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
3994
3995 if (bg_rgb != INVALCOLOR)
3996 {
3997 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
3998 bg->red = (unsigned)(rgb >> 16);
3999 bg->green = (unsigned)(rgb >> 8) & 255;
4000 bg->blue = (unsigned)rgb & 255;
4001 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
4002 }
4003 else
4004 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4005
4006 return TRUE;
4007 }
4008 else
4009#endif
4010 if (t_colors >= 16)
4011 {
4012 int cterm_fg = -1;
4013 int cterm_bg = -1;
4014
4015 if (id > 0)
4016 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
4017
4018 if (cterm_fg >= 0)
4019 {
4020 cterm_color2vterm(cterm_fg, fg);
4021 fg->type |= VTERM_COLOR_DEFAULT_FG;
4022 }
4023 else
4024 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4025
4026 if (cterm_bg >= 0)
4027 {
4028 cterm_color2vterm(cterm_bg, bg);
4029 bg->type |= VTERM_COLOR_DEFAULT_BG;
4030 }
4031 else
4032 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4033
4034 return TRUE;
4035 }
4036
4037 return FALSE;
4038}
4039
4040 void
4041term_reset_wincolor(win_T *wp)
4042{
4043 wp->w_term_wincolor.fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4044 wp->w_term_wincolor.bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4045}
4046
4047/*
4048 * Cache the color of 'wincolor'.
4049 */
4050 void
4051term_update_wincolor(win_T *wp)
4052{
4053 int id = 0;
4054
4055 if (*wp->w_p_wcr != NUL)
4056 id = syn_name2id(wp->w_p_wcr);
4057 if (id == 0 || !get_vterm_color_from_synid(id, &wp->w_term_wincolor.fg,
4058 &wp->w_term_wincolor.bg))
4059 term_reset_wincolor(wp);
4060}
4061
4062/*
4063 * Called when option 'termguicolors' was set,
4064 * or when any highlight is changed.
4065 */
4066 void
4067term_update_wincolor_all()
4068{
4069 win_T *wp = NULL;
4070 int did_curwin = FALSE;
4071
4072 while (for_all_windows_and_curwin(&wp, &did_curwin))
4073 term_update_wincolor(wp);
4074}
4075
4076/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004077 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004078 */
4079 static void
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004080init_default_colors(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004081{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004082 VTermColor *fg, *bg;
4083 int fgval, bgval;
4084 int id;
4085
Bram Moolenaara80faa82020-04-12 19:37:17 +02004086 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004087 term->tl_default_color.width = 1;
4088 fg = &term->tl_default_color.fg;
4089 bg = &term->tl_default_color.bg;
4090
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004091 // Vterm uses a default black background. Set it to white when
4092 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004093 if (*p_bg == 'l')
4094 {
4095 fgval = 0;
4096 bgval = 255;
4097 }
4098 else
4099 {
4100 fgval = 255;
4101 bgval = 0;
4102 }
4103 fg->red = fg->green = fg->blue = fgval;
4104 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004105 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
4106 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004107
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004108 // The highlight group overrules the defaults.
4109 id = term_get_highlight_id(term, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004110
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004111 if (!get_vterm_color_from_synid(id, fg, bg))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004112 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004113#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004114 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004115#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004116
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004117 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004118 if (cterm_normal_fg_color > 0)
4119 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004120 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004121# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4122# ifdef VIMDLL
4123 if (!gui.in_use)
4124# endif
4125 {
4126 tmp = fg->red;
4127 fg->red = fg->blue;
4128 fg->blue = tmp;
4129 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004130# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004131 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004132# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004133 else
4134 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004135# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004136
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004137 if (cterm_normal_bg_color > 0)
4138 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004139 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004140# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4141# ifdef VIMDLL
4142 if (!gui.in_use)
4143# endif
4144 {
4145 tmp = fg->red;
4146 fg->red = fg->blue;
4147 fg->blue = tmp;
4148 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004149# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004150 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004151# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004152 else
4153 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004154# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004155 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004156}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004157
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004158#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4159/*
4160 * Set the 16 ANSI colors from array of RGB values
4161 */
4162 static void
4163set_vterm_palette(VTerm *vterm, long_u *rgb)
4164{
4165 int index = 0;
4166 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004167
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004168 for (; index < 16; index++)
4169 {
4170 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004171
Millyb771b6b2021-11-23 12:07:25 +00004172 color.type = VTERM_COLOR_RGB;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004173 color.red = (unsigned)(rgb[index] >> 16);
4174 color.green = (unsigned)(rgb[index] >> 8) & 255;
4175 color.blue = (unsigned)rgb[index] & 255;
4176 vterm_state_set_palette_color(state, index, &color);
4177 }
4178}
4179
4180/*
4181 * Set the ANSI color palette from a list of colors
4182 */
4183 static int
4184set_ansi_colors_list(VTerm *vterm, list_T *list)
4185{
4186 int n = 0;
4187 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004188 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004189
Bram Moolenaarb0992022020-01-30 14:55:42 +01004190 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004191 {
4192 char_u *color_name;
4193 guicolor_T guicolor;
4194
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004195 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004196 if (color_name == NULL)
4197 return FAIL;
4198
4199 guicolor = GUI_GET_COLOR(color_name);
4200 if (guicolor == INVALCOLOR)
4201 return FAIL;
4202
4203 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4204 }
4205
4206 if (n != 16 || li != NULL)
4207 return FAIL;
4208
4209 set_vterm_palette(vterm, rgb);
4210
4211 return OK;
4212}
4213
4214/*
4215 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4216 */
4217 static void
4218init_vterm_ansi_colors(VTerm *vterm)
4219{
4220 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4221
4222 if (var != NULL
4223 && (var->di_tv.v_type != VAR_LIST
4224 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004225 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004226 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004227 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004228}
4229#endif
4230
Bram Moolenaar52acb112018-03-18 19:20:22 +01004231/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004232 * Handles a "drop" command from the job in the terminal.
4233 * "item" is the file name, "item->li_next" may have options.
4234 */
4235 static void
4236handle_drop_command(listitem_T *item)
4237{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004238 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004239 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004240 int bufnr;
4241 win_T *wp;
4242 tabpage_T *tp;
4243 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004244 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004245
4246 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4247 FOR_ALL_TAB_WINDOWS(tp, wp)
4248 {
4249 if (wp->w_buffer->b_fnum == bufnr)
4250 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004251 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004252 goto_tabpage_win(tp, wp);
4253 return;
4254 }
4255 }
4256
Bram Moolenaara80faa82020-04-12 19:37:17 +02004257 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004258
4259 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4260 && opt_item->li_tv.vval.v_dict != NULL)
4261 {
4262 dict_T *dict = opt_item->li_tv.vval.v_dict;
4263 char_u *p;
4264
Bram Moolenaar8f667172018-12-14 15:38:31 +01004265 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004266 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004267 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004268 if (p != NULL)
4269 {
4270 if (check_ff_value(p) == FAIL)
4271 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4272 else
4273 ea.force_ff = *p;
4274 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004275 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004276 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004277 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004278 if (p != NULL)
4279 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004280 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004281 if (ea.cmd != NULL)
4282 {
4283 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4284 ea.force_enc = 11;
4285 tofree = ea.cmd;
4286 }
4287 }
4288
Bram Moolenaar8f667172018-12-14 15:38:31 +01004289 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004290 if (p != NULL)
4291 get_bad_opt(p, &ea);
4292
4293 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4294 ea.force_bin = FORCE_BIN;
4295 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4296 ea.force_bin = FORCE_BIN;
4297 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4298 ea.force_bin = FORCE_NOBIN;
4299 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4300 ea.force_bin = FORCE_NOBIN;
4301 }
4302
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004303 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004304 if (ea.cmd == NULL)
4305 ea.cmd = (char_u *)"split";
4306 ea.arg = fname;
4307 ea.cmdidx = CMD_split;
4308 ex_splitview(&ea);
4309
4310 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004311}
4312
4313/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004314 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4315 */
4316 static int
4317is_permitted_term_api(char_u *func, char_u *pat)
4318{
4319 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4320}
4321
4322/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004323 * Handles a function call from the job running in a terminal.
4324 * "item" is the function name, "item->li_next" has the arguments.
4325 */
4326 static void
4327handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4328{
4329 char_u *func;
4330 typval_T argvars[2];
4331 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004332 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004333
4334 if (item->li_next == NULL)
4335 {
4336 ch_log(channel, "Missing function arguments for call");
4337 return;
4338 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004339 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004340
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004341 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004342 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004343 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004344 return;
4345 }
4346
4347 argvars[0].v_type = VAR_NUMBER;
4348 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4349 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004350 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004351 funcexe.fe_firstline = 1L;
4352 funcexe.fe_lastline = 1L;
4353 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004354 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004355 {
4356 clear_tv(&rettv);
4357 ch_log(channel, "Function %s called", func);
4358 }
4359 else
4360 ch_log(channel, "Calling function %s failed", func);
4361}
4362
4363/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004364 * URL decoding (also know as Percent-encoding).
4365 *
4366 * Note this function currently is only used for decoding shell's
4367 * OSC 7 escape sequence which we can assume all bytes are valid
4368 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4369 * encoding bytes like 0xfe, 0xff.
4370 */
4371 static size_t
4372url_decode(const char *src, const size_t len, char_u *dst)
4373{
4374 size_t i = 0, j = 0;
4375
4376 while (i < len)
4377 {
4378 if (src[i] == '%' && i + 2 < len)
4379 {
4380 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4381 j++;
4382 i += 3;
4383 }
4384 else
4385 {
4386 dst[j] = src[i];
4387 i++;
4388 j++;
4389 }
4390 }
4391 dst[j] = '\0';
4392 return j;
4393}
4394
4395/*
4396 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4397 *
4398 * The OSC 7 sequence has the format of
4399 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4400 * and what VTerm provides via VTermStringFragment is
4401 * "file://HOSTNAME/CURRENT/DIR"
4402 */
4403 static void
4404sync_shell_dir(VTermStringFragment *frag)
4405{
4406 int offset = 7; // len of "file://" is 7
4407 char *pos = (char *)frag->str + offset;
4408 char_u *new_dir;
4409
4410 // remove HOSTNAME to get PWD
Bram Moolenaar918b0892021-05-08 20:09:24 +02004411 while (*pos != '/' && offset < (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004412 {
4413 offset += 1;
4414 pos += 1;
4415 }
4416
Bram Moolenaar918b0892021-05-08 20:09:24 +02004417 if (offset >= (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004418 {
4419 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
4420 frag->str);
4421 return;
4422 }
4423
4424 new_dir = alloc(frag->len - offset + 1);
4425 url_decode(pos, frag->len-offset, new_dir);
4426 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4427 vim_free(new_dir);
4428}
4429
4430/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004431 * Called by libvterm when it cannot recognize an OSC sequence.
4432 * We recognize a terminal API command.
4433 */
4434 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004435parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004436{
4437 term_T *term = (term_T *)user;
4438 js_read_T reader;
4439 typval_T tv;
4440 channel_T *channel = term->tl_job == NULL ? NULL
4441 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004442 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004443
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004444 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
4445 if (p_asd && command == 7)
4446 {
4447 sync_shell_dir(&frag);
4448 return 1;
4449 }
4450
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004451 if (command != 51)
4452 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004453
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004454 // Concatenate what was received until the final piece is found.
4455 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4456 {
4457 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004458 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004459 }
4460 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004461 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004462 if (!frag.final)
4463 return 1;
4464
4465 ((char *)gap->ga_data)[gap->ga_len] = 0;
4466 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004467 reader.js_fill = NULL;
4468 reader.js_used = 0;
4469 if (json_decode(&reader, &tv, 0) == OK
4470 && tv.v_type == VAR_LIST
4471 && tv.vval.v_list != NULL)
4472 {
4473 listitem_T *item = tv.vval.v_list->lv_first;
4474
4475 if (item == NULL)
4476 ch_log(channel, "Missing command");
4477 else
4478 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004479 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004480
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004481 // Make sure an invoked command doesn't delete the buffer (and the
4482 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004483 ++term->tl_buffer->b_locked;
4484
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004485 item = item->li_next;
4486 if (item == NULL)
4487 ch_log(channel, "Missing argument for %s", cmd);
4488 else if (STRCMP(cmd, "drop") == 0)
4489 handle_drop_command(item);
4490 else if (STRCMP(cmd, "call") == 0)
4491 handle_call_command(term, channel, item);
4492 else
4493 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004494 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004495 }
4496 }
4497 else
4498 ch_log(channel, "Invalid JSON received");
4499
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004500 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004501 clear_tv(&tv);
4502 return 1;
4503}
4504
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004505/*
4506 * Called by libvterm when it cannot recognize a CSI sequence.
4507 * We recognize the window position report.
4508 */
4509 static int
4510parse_csi(
4511 const char *leader UNUSED,
4512 const long args[],
4513 int argcount,
4514 const char *intermed UNUSED,
4515 char command,
4516 void *user)
4517{
4518 term_T *term = (term_T *)user;
4519 char buf[100];
4520 int len;
4521 int x = 0;
4522 int y = 0;
4523 win_T *wp;
4524
4525 // We recognize only CSI 13 t
4526 if (command != 't' || argcount != 1 || args[0] != 13)
4527 return 0; // not handled
4528
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004529 // When getting the window position is not possible or it fails it results
4530 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004531#if defined(FEAT_GUI) \
4532 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4533 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004534 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004535#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004536
4537 FOR_ALL_WINDOWS(wp)
4538 if (wp->w_buffer == term->tl_buffer)
4539 break;
4540 if (wp != NULL)
4541 {
4542#ifdef FEAT_GUI
4543 if (gui.in_use)
4544 {
4545 x += wp->w_wincol * gui.char_width;
4546 y += W_WINROW(wp) * gui.char_height;
4547 }
4548 else
4549#endif
4550 {
4551 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004552 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004553 x += wp->w_wincol * 7;
4554 y += W_WINROW(wp) * 10;
4555 }
4556 }
4557
4558 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4559 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4560 (char_u *)buf, len, NULL);
4561 return 1;
4562}
4563
Bram Moolenaard8637282020-05-20 18:41:41 +02004564static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004565 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004566 parse_csi, // csi
4567 parse_osc, // osc
Bram Moolenaar7da34152021-11-24 19:30:55 +00004568 NULL, // dcs
4569 NULL, // apc
4570 NULL, // pm
4571 NULL // sos
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004572};
4573
4574/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004575 * Use Vim's allocation functions for vterm so profiling works.
4576 */
4577 static void *
4578vterm_malloc(size_t size, void *data UNUSED)
4579{
Bram Moolenaar88137392021-11-12 16:01:15 +00004580 // make sure that the length is not zero
4581 return alloc_clear(size == 0 ? 1L : size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004582}
4583
4584 static void
4585vterm_memfree(void *ptr, void *data UNUSED)
4586{
4587 vim_free(ptr);
4588}
4589
4590static VTermAllocatorFunctions vterm_allocator = {
4591 &vterm_malloc,
4592 &vterm_memfree
4593};
4594
4595/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004596 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004597 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004598 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004599 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004600create_vterm(term_T *term, int rows, int cols)
4601{
4602 VTerm *vterm;
4603 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004604 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004605 VTermValue value;
4606
Bram Moolenaar756ef112018-04-10 12:04:27 +02004607 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004608 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004609 if (vterm == NULL)
4610 return FAIL;
4611
4612 // Allocate screen and state here, so we can bail out if that fails.
4613 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004614 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004615 if (state == NULL || screen == NULL)
4616 {
4617 vterm_free(vterm);
4618 return FAIL;
4619 }
4620
Bram Moolenaar52acb112018-03-18 19:20:22 +01004621 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004622 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004623 vterm_set_utf8(vterm, 1);
4624
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004625 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004626
4627 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004628 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004629 &term->tl_default_color.fg,
4630 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004631
Bram Moolenaar9e587872019-05-13 20:27:23 +02004632 if (t_colors < 16)
4633 // Less than 16 colors: assume that bold means using a bright color for
4634 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004635 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4636
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004637 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004638 vterm_screen_reset(screen, 1 /* hard */);
4639
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004640 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004641 vterm_screen_enable_altscreen(screen, 1);
4642
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004643 // For unix do not use a blinking cursor. In an xterm this causes the
4644 // cursor to blink if it's blinking in the xterm.
4645 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004646#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004647 if (GetCaretBlinkTime() == INFINITE)
4648 value.boolean = 0;
4649 else
4650 value.boolean = 1;
4651#else
4652 value.boolean = 0;
4653#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004654 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004655 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004656
4657 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004658}
4659
4660/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004661 * Called when option 'background' or 'termguicolors' was set,
4662 * or when any highlight is changed.
Bram Moolenaarad431992021-05-03 20:40:38 +02004663 */
4664 void
4665term_update_colors_all(void)
4666{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004667 term_T *term;
Bram Moolenaarad431992021-05-03 20:40:38 +02004668
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004669 FOR_ALL_TERMS(term)
4670 {
4671 if (term->tl_vterm == NULL)
4672 continue;
4673 init_default_colors(term);
4674 vterm_state_set_default_colors(
4675 vterm_obtain_state(term->tl_vterm),
4676 &term->tl_default_color.fg,
4677 &term->tl_default_color.bg);
4678 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004679}
4680
4681/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004682 * Return the text to show for the buffer name and status.
4683 */
4684 char_u *
4685term_get_status_text(term_T *term)
4686{
4687 if (term->tl_status_text == NULL)
4688 {
4689 char_u *txt;
4690 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004691 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004692
4693 if (term->tl_normal_mode)
4694 {
4695 if (term_job_running(term))
4696 txt = (char_u *)_("Terminal");
4697 else
4698 txt = (char_u *)_("Terminal-finished");
4699 }
4700 else if (term->tl_title != NULL)
4701 txt = term->tl_title;
4702 else if (term_none_open(term))
4703 txt = (char_u *)_("active");
4704 else if (term_job_running(term))
4705 txt = (char_u *)_("running");
4706 else
4707 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004708 fname = buf_get_fname(term->tl_buffer);
4709 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004710 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004711 if (term->tl_status_text != NULL)
4712 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004713 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004714 }
4715 return term->tl_status_text;
4716}
4717
4718/*
Bram Moolenaar3ad69532021-11-19 17:01:08 +00004719 * Clear the cached value of the status text.
4720 */
4721 void
4722term_clear_status_text(term_T *term)
4723{
4724 VIM_CLEAR(term->tl_status_text);
4725}
4726
4727/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004728 * Mark references in jobs of terminals.
4729 */
4730 int
4731set_ref_in_term(int copyID)
4732{
4733 int abort = FALSE;
4734 term_T *term;
4735 typval_T tv;
4736
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004737 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004738 if (term->tl_job != NULL)
4739 {
4740 tv.v_type = VAR_JOB;
4741 tv.vval.v_job = term->tl_job;
4742 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4743 }
4744 return abort;
4745}
4746
4747/*
4748 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004749 * Returns NULL when the buffer is not for a terminal window and logs a message
4750 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004751 */
4752 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004753term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004754{
4755 buf_T *buf;
4756
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004757 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004758 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004759 --emsg_off;
4760 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004761 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004762 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004763 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004764 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004765 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004766 return buf;
4767}
4768
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004769 static void
4770clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004771{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004772 CLEAR_FIELD(*cell);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004773 cell->fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4774 cell->bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004775}
4776
4777 static void
4778dump_term_color(FILE *fd, VTermColor *color)
4779{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004780 int index;
4781
4782 if (VTERM_COLOR_IS_INDEXED(color))
4783 index = color->index + 1;
4784 else if (color->type == 0)
4785 // use RGB values
4786 index = 255;
4787 else
4788 // default color
4789 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004790 fprintf(fd, "%02x%02x%02x%d",
4791 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004792 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004793}
4794
4795/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004796 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004797 *
4798 * Each screen cell in full is:
4799 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4800 * {characters} is a space for an empty cell
4801 * For a double-width character "+" is changed to "*" and the next cell is
4802 * skipped.
4803 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4804 * when "&" use the same as the previous cell.
4805 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4806 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4807 * {color-idx} is a number from 0 to 255
4808 *
4809 * Screen cell with same width, attributes and color as the previous one:
4810 * |{characters}
4811 *
4812 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4813 *
4814 * Repeating the previous screen cell:
4815 * @{count}
4816 */
4817 void
4818f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4819{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004820 buf_T *buf;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004821 term_T *term;
4822 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004823 int max_height = 0;
4824 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004825 stat_T st;
4826 FILE *fd;
4827 VTermPos pos;
4828 VTermScreen *screen;
4829 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004830 VTermState *state;
4831 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004832
4833 if (check_restricted() || check_secure())
4834 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004835
4836 if (in_vim9script()
4837 && (check_for_buffer_arg(argvars, 0) == FAIL
4838 || check_for_string_arg(argvars, 1) == FAIL
4839 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4840 return;
4841
4842 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004843 if (buf == NULL)
4844 return;
4845 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004846 if (term->tl_vterm == NULL)
4847 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004848 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004849 return;
4850 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004851
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004852 if (argvars[2].v_type != VAR_UNKNOWN)
4853 {
4854 dict_T *d;
4855
4856 if (argvars[2].v_type != VAR_DICT)
4857 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004858 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004859 return;
4860 }
4861 d = argvars[2].vval.v_dict;
4862 if (d != NULL)
4863 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004864 max_height = dict_get_number(d, (char_u *)"rows");
4865 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004866 }
4867 }
4868
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004869 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004870 if (fname == NULL)
4871 return;
4872 if (mch_stat((char *)fname, &st) >= 0)
4873 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004874 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004875 return;
4876 }
4877
Bram Moolenaard96ff162018-02-18 22:13:29 +01004878 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4879 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004880 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004881 return;
4882 }
4883
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004884 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004885
4886 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004887 state = vterm_obtain_state(term->tl_vterm);
4888 vterm_state_get_cursorpos(state, &cursor_pos);
4889
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004890 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4891 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004892 {
4893 int repeat = 0;
4894
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004895 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4896 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004897 {
4898 VTermScreenCell cell;
4899 int same_attr;
4900 int same_chars = TRUE;
4901 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004902 int is_cursor_pos = (pos.col == cursor_pos.col
4903 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004904
4905 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004906 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004907
4908 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4909 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004910 int c = cell.chars[i];
4911 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004912 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004913
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004914 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004915 if (i == 0)
4916 {
4917 c = (c == NUL) ? ' ' : c;
4918 pc = (pc == NUL) ? ' ' : pc;
4919 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004920 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004921 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004922 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004923 break;
4924 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004925 same_attr = vtermAttr2hl(&cell.attrs)
4926 == vtermAttr2hl(&prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004927 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4928 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004929 if (same_chars && cell.width == prev_cell.width && same_attr
4930 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004931 {
4932 ++repeat;
4933 }
4934 else
4935 {
4936 if (repeat > 0)
4937 {
4938 fprintf(fd, "@%d", repeat);
4939 repeat = 0;
4940 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004941 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004942
4943 if (cell.chars[0] == NUL)
4944 fputs(" ", fd);
4945 else
4946 {
4947 char_u charbuf[10];
4948 int len;
4949
4950 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4951 && cell.chars[i] != NUL; ++i)
4952 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004953 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004954 fwrite(charbuf, len, 1, fd);
4955 }
4956 }
4957
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004958 // When only the characters differ we don't write anything, the
4959 // following "|", "@" or NL will indicate using the same
4960 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004961 if (cell.width != prev_cell.width || !same_attr)
4962 {
4963 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004964 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004965 else
4966 fputs("+", fd);
4967
4968 if (same_attr)
4969 {
4970 fputs("&", fd);
4971 }
4972 else
4973 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004974 fprintf(fd, "%d", vtermAttr2hl(&cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004975 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004976 fputs("&", fd);
4977 else
4978 {
4979 fputs("#", fd);
4980 dump_term_color(fd, &cell.fg);
4981 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004982 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004983 fputs("&", fd);
4984 else
4985 {
4986 fputs("#", fd);
4987 dump_term_color(fd, &cell.bg);
4988 }
4989 }
4990 }
4991
4992 prev_cell = cell;
4993 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004994
4995 if (cell.width == 2)
4996 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004997 }
4998 if (repeat > 0)
4999 fprintf(fd, "@%d", repeat);
5000 fputs("\n", fd);
5001 }
5002
5003 fclose(fd);
5004}
5005
5006/*
5007 * Called when a dump is corrupted. Put a breakpoint here when debugging.
5008 */
5009 static void
5010dump_is_corrupt(garray_T *gap)
5011{
5012 ga_concat(gap, (char_u *)"CORRUPT");
5013}
5014
5015 static void
5016append_cell(garray_T *gap, cellattr_T *cell)
5017{
5018 if (ga_grow(gap, 1) == OK)
5019 {
5020 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
5021 ++gap->ga_len;
5022 }
5023}
5024
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005025 static void
5026clear_cellattr(cellattr_T *cell)
5027{
5028 CLEAR_FIELD(*cell);
5029 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
5030 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
5031}
5032
Bram Moolenaard96ff162018-02-18 22:13:29 +01005033/*
5034 * Read the dump file from "fd" and append lines to the current buffer.
5035 * Return the cell width of the longest line.
5036 */
5037 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01005038read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005039{
5040 int c;
5041 garray_T ga_text;
5042 garray_T ga_cell;
5043 char_u *prev_char = NULL;
5044 int attr = 0;
5045 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005046 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005047 term_T *term = curbuf->b_term;
5048 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005049 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005050
5051 ga_init2(&ga_text, 1, 90);
5052 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005053 clear_cellattr(&cell);
5054 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005055 cursor_pos->row = -1;
5056 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005057
5058 c = fgetc(fd);
5059 for (;;)
5060 {
5061 if (c == EOF)
5062 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02005063 if (c == '\r')
5064 {
5065 // DOS line endings? Ignore.
5066 c = fgetc(fd);
5067 }
5068 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005069 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005070 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005071 if (ga_text.ga_data == NULL)
5072 dump_is_corrupt(&ga_text);
5073 if (ga_grow(&term->tl_scrollback, 1) == OK)
5074 {
5075 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
5076 + term->tl_scrollback.ga_len;
5077
5078 if (max_cells < ga_cell.ga_len)
5079 max_cells = ga_cell.ga_len;
5080 line->sb_cols = ga_cell.ga_len;
5081 line->sb_cells = ga_cell.ga_data;
5082 line->sb_fill_attr = term->tl_default_color;
5083 ++term->tl_scrollback.ga_len;
5084 ga_init(&ga_cell);
5085
5086 ga_append(&ga_text, NUL);
5087 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5088 ga_text.ga_len, FALSE);
5089 }
5090 else
5091 ga_clear(&ga_cell);
5092 ga_text.ga_len = 0;
5093
5094 c = fgetc(fd);
5095 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005096 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005097 {
5098 int prev_len = ga_text.ga_len;
5099
Bram Moolenaar9271d052018-02-25 21:39:46 +01005100 if (c == '>')
5101 {
5102 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005103 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005104 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5105 cursor_pos->col = ga_cell.ga_len;
5106 }
5107
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005108 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005109 c = fgetc(fd);
5110 if (c != EOF)
5111 ga_append(&ga_text, c);
5112 for (;;)
5113 {
5114 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005115 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005116 || c == EOF || c == '\n')
5117 break;
5118 ga_append(&ga_text, c);
5119 }
5120
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005121 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005122 vim_free(prev_char);
5123 if (ga_text.ga_data != NULL)
5124 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5125 ga_text.ga_len - prev_len);
5126
Bram Moolenaar9271d052018-02-25 21:39:46 +01005127 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005128 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005129 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005130 }
5131 else if (c == '+' || c == '*')
5132 {
5133 int is_bg;
5134
5135 cell.width = c == '+' ? 1 : 2;
5136
5137 c = fgetc(fd);
5138 if (c == '&')
5139 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005140 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005141 c = fgetc(fd);
5142 }
5143 else if (isdigit(c))
5144 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005145 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005146 attr = 0;
5147 while (isdigit(c))
5148 {
5149 attr = attr * 10 + (c - '0');
5150 c = fgetc(fd);
5151 }
5152 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005153
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005154 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005155 for (is_bg = 0; is_bg <= 1; ++is_bg)
5156 {
5157 if (c == '&')
5158 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005159 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005160 c = fgetc(fd);
5161 }
5162 else if (c == '#')
5163 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005164 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005165
5166 c = fgetc(fd);
5167 red = hex2nr(c);
5168 c = fgetc(fd);
5169 red = (red << 4) + hex2nr(c);
5170 c = fgetc(fd);
5171 green = hex2nr(c);
5172 c = fgetc(fd);
5173 green = (green << 4) + hex2nr(c);
5174 c = fgetc(fd);
5175 blue = hex2nr(c);
5176 c = fgetc(fd);
5177 blue = (blue << 4) + hex2nr(c);
5178 c = fgetc(fd);
5179 if (!isdigit(c))
5180 dump_is_corrupt(&ga_text);
5181 while (isdigit(c))
5182 {
5183 index = index * 10 + (c - '0');
5184 c = fgetc(fd);
5185 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005186 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005187 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005188 type = VTERM_COLOR_RGB;
5189 if (index == 0)
5190 {
5191 if (is_bg)
5192 type |= VTERM_COLOR_DEFAULT_BG;
5193 else
5194 type |= VTERM_COLOR_DEFAULT_FG;
5195 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005196 }
5197 else
5198 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005199 type = VTERM_COLOR_INDEXED;
5200 index -= 1;
5201 }
5202 if (is_bg)
5203 {
5204 cell.bg.type = type;
5205 cell.bg.red = red;
5206 cell.bg.green = green;
5207 cell.bg.blue = blue;
5208 cell.bg.index = index;
5209 }
5210 else
5211 {
5212 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005213 cell.fg.red = red;
5214 cell.fg.green = green;
5215 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005216 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005217 }
5218 }
5219 else
5220 dump_is_corrupt(&ga_text);
5221 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005222 }
5223 else
5224 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005225 }
5226 else
5227 dump_is_corrupt(&ga_text);
5228
5229 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005230 if (cell.width == 2)
5231 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005232 }
5233 else if (c == '@')
5234 {
5235 if (prev_char == NULL)
5236 dump_is_corrupt(&ga_text);
5237 else
5238 {
5239 int count = 0;
5240
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005241 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005242 for (;;)
5243 {
5244 c = fgetc(fd);
5245 if (!isdigit(c))
5246 break;
5247 count = count * 10 + (c - '0');
5248 }
5249
5250 while (count-- > 0)
5251 {
5252 ga_concat(&ga_text, prev_char);
5253 append_cell(&ga_cell, &cell);
5254 }
5255 }
5256 }
5257 else
5258 {
5259 dump_is_corrupt(&ga_text);
5260 c = fgetc(fd);
5261 }
5262 }
5263
5264 if (ga_text.ga_len > 0)
5265 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005266 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005267 dump_is_corrupt(&ga_text);
5268 ga_append(&ga_text, NUL);
5269 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5270 ga_text.ga_len, FALSE);
5271 }
5272
5273 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005274 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005275 vim_free(prev_char);
5276
5277 return max_cells;
5278}
5279
5280/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005281 * Return an allocated string with at least "text_width" "=" characters and
5282 * "fname" inserted in the middle.
5283 */
5284 static char_u *
5285get_separator(int text_width, char_u *fname)
5286{
5287 int width = MAX(text_width, curwin->w_width);
5288 char_u *textline;
5289 int fname_size;
5290 char_u *p = fname;
5291 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005292 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005293
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005294 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005295 if (textline == NULL)
5296 return NULL;
5297
5298 fname_size = vim_strsize(fname);
5299 if (fname_size < width - 8)
5300 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005301 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005302 width = MAX(text_width, fname_size + 8);
5303 }
5304 else if (fname_size > width - 8)
5305 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005306 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005307 p = gettail(fname);
5308 fname_size = vim_strsize(p);
5309 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005310 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005311 while (fname_size > width - 8)
5312 {
5313 p += (*mb_ptr2len)(p);
5314 fname_size = vim_strsize(p);
5315 }
5316
5317 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5318 textline[i] = '=';
5319 textline[i++] = ' ';
5320
5321 STRCPY(textline + i, p);
5322 off = STRLEN(textline);
5323 textline[off] = ' ';
5324 for (i = 1; i < (width - fname_size) / 2; ++i)
5325 textline[off + i] = '=';
5326 textline[off + i] = NUL;
5327
5328 return textline;
5329}
5330
5331/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005332 * Common for "term_dumpdiff()" and "term_dumpload()".
5333 */
5334 static void
5335term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5336{
5337 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005338 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005339 char_u buf1[NUMBUFLEN];
5340 char_u buf2[NUMBUFLEN];
5341 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005342 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005343 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005344 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005345 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005346 char_u *textline = NULL;
5347
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005348 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005349 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005350 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005351 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005352 if (fname1 == NULL || (do_diff && fname2 == NULL))
5353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005354 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005355 return;
5356 }
5357 fd1 = mch_fopen((char *)fname1, READBIN);
5358 if (fd1 == NULL)
5359 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005360 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005361 return;
5362 }
5363 if (do_diff)
5364 {
5365 fd2 = mch_fopen((char *)fname2, READBIN);
5366 if (fd2 == NULL)
5367 {
5368 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005369 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005370 return;
5371 }
5372 }
5373
5374 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005375 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5376 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5377 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5378 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5379 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005380
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005381 if (opt.jo_term_name == NULL)
5382 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005383 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005384
Bram Moolenaar51e14382019-05-25 20:21:28 +02005385 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005386 if (fname_tofree != NULL)
5387 {
5388 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5389 opt.jo_term_name = fname_tofree;
5390 }
5391 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005392
Bram Moolenaar87abab92019-06-03 21:14:59 +02005393 if (opt.jo_bufnr_buf != NULL)
5394 {
5395 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5396
5397 // With "bufnr" argument: enter the window with this buffer and make it
5398 // empty.
5399 if (wp == NULL)
5400 semsg(_(e_invarg2), "bufnr");
5401 else
5402 {
5403 buf = curbuf;
5404 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005405 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005406 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005407 redraw_later(NOT_VALID);
5408 }
5409 }
5410 else
5411 // Create a new terminal window.
5412 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5413
Bram Moolenaard96ff162018-02-18 22:13:29 +01005414 if (buf != NULL && buf->b_term != NULL)
5415 {
5416 int i;
5417 linenr_T bot_lnum;
5418 linenr_T lnum;
5419 term_T *term = buf->b_term;
5420 int width;
5421 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005422 VTermPos cursor_pos1;
5423 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005424
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005425 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005426
Bram Moolenaard96ff162018-02-18 22:13:29 +01005427 rettv->vval.v_number = buf->b_fnum;
5428
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005429 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005430 width = read_dump_file(fd1, &cursor_pos1);
5431
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005432 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005433 if (cursor_pos1.row >= 0)
5434 {
5435 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5436 coladvance(cursor_pos1.col);
5437 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005438
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005439 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005440 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005441
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005442 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005443 if (!do_diff)
5444 goto theend;
5445
5446 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5447
Bram Moolenaar4a696342018-04-05 18:45:26 +02005448 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005449 if (textline == NULL)
5450 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005451 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5452 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5453 vim_free(textline);
5454
5455 textline = get_separator(width, fname2);
5456 if (textline == NULL)
5457 goto theend;
5458 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5459 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005460 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005461
5462 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005463 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005464 if (width2 > width)
5465 {
5466 vim_free(textline);
5467 textline = alloc(width2 + 1);
5468 if (textline == NULL)
5469 goto theend;
5470 width = width2;
5471 textline[width] = NUL;
5472 }
5473 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5474
5475 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5476 {
5477 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5478 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005479 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005480 for (i = 0; i < width; ++i)
5481 textline[i] = '-';
5482 }
5483 else
5484 {
5485 char_u *line1;
5486 char_u *line2;
5487 char_u *p1;
5488 char_u *p2;
5489 int col;
5490 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5491 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5492 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5493 ->sb_cells;
5494
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005495 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005496 line1 = vim_strsave(ml_get(lnum));
5497 if (line1 == NULL)
5498 break;
5499 p1 = line1;
5500
5501 line2 = ml_get(lnum + bot_lnum);
5502 p2 = line2;
5503 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5504 {
5505 int len1 = utfc_ptr2len(p1);
5506 int len2 = utfc_ptr2len(p2);
5507
5508 textline[col] = ' ';
5509 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005510 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005511 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005512 else if (lnum == cursor_pos1.row + 1
5513 && col == cursor_pos1.col
5514 && (cursor_pos1.row != cursor_pos2.row
5515 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005516 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005517 textline[col] = '>';
5518 else if (lnum == cursor_pos2.row + 1
5519 && col == cursor_pos2.col
5520 && (cursor_pos1.row != cursor_pos2.row
5521 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005522 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005523 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005524 else if (cellattr1 != NULL && cellattr2 != NULL)
5525 {
5526 if ((cellattr1 + col)->width
5527 != (cellattr2 + col)->width)
5528 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005529 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005530 &(cellattr2 + col)->fg))
5531 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005532 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005533 &(cellattr2 + col)->bg))
5534 textline[col] = 'b';
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005535 else if (vtermAttr2hl(&(cellattr1 + col)->attrs)
5536 != vtermAttr2hl(&((cellattr2 + col)->attrs)))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005537 textline[col] = 'a';
5538 }
5539 p1 += len1;
5540 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005541 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005542 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005543
5544 while (col < width)
5545 {
5546 if (*p1 == NUL && *p2 == NUL)
5547 textline[col] = '?';
5548 else if (*p1 == NUL)
5549 {
5550 textline[col] = '+';
5551 p2 += utfc_ptr2len(p2);
5552 }
5553 else
5554 {
5555 textline[col] = '-';
5556 p1 += utfc_ptr2len(p1);
5557 }
5558 ++col;
5559 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005560
5561 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005562 }
5563 if (add_empty_scrollback(term, &term->tl_default_color,
5564 term->tl_top_diff_rows) == OK)
5565 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5566 ++bot_lnum;
5567 }
5568
5569 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5570 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005571 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005572 for (i = 0; i < width; ++i)
5573 textline[i] = '+';
5574 if (add_empty_scrollback(term, &term->tl_default_color,
5575 term->tl_top_diff_rows) == OK)
5576 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5577 ++lnum;
5578 ++bot_lnum;
5579 }
5580
5581 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005582
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005583 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005584 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005585 }
5586
5587theend:
5588 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005589 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005590 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005591 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005592 fclose(fd2);
5593}
5594
5595/*
5596 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5597 * bottom files.
5598 * Return FAIL when this is not possible.
5599 */
5600 int
5601term_swap_diff()
5602{
5603 term_T *term = curbuf->b_term;
5604 linenr_T line_count;
5605 linenr_T top_rows;
5606 linenr_T bot_rows;
5607 linenr_T bot_start;
5608 linenr_T lnum;
5609 char_u *p;
5610 sb_line_T *sb_line;
5611
5612 if (term == NULL
5613 || !term_is_finished(curbuf)
5614 || term->tl_top_diff_rows == 0
5615 || term->tl_scrollback.ga_len == 0)
5616 return FAIL;
5617
5618 line_count = curbuf->b_ml.ml_line_count;
5619 top_rows = term->tl_top_diff_rows;
5620 bot_rows = term->tl_bot_diff_rows;
5621 bot_start = line_count - bot_rows;
5622 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5623
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005624 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005625 for (lnum = 1; lnum <= top_rows; ++lnum)
5626 {
5627 p = vim_strsave(ml_get(1));
5628 if (p == NULL)
5629 return OK;
5630 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005631 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005632 vim_free(p);
5633 }
5634
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005635 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005636 for (lnum = 1; lnum <= bot_rows; ++lnum)
5637 {
5638 p = vim_strsave(ml_get(bot_start + lnum));
5639 if (p == NULL)
5640 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005641 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005642 ml_append(lnum - 1, p, 0, FALSE);
5643 vim_free(p);
5644 }
5645
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005646 // move top title to bottom
5647 p = vim_strsave(ml_get(bot_rows + 1));
5648 if (p == NULL)
5649 return OK;
5650 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005651 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005652 vim_free(p);
5653
5654 // move bottom title to top
5655 p = vim_strsave(ml_get(line_count - top_rows));
5656 if (p == NULL)
5657 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005658 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005659 ml_append(bot_rows, p, 0, FALSE);
5660 vim_free(p);
5661
Bram Moolenaard96ff162018-02-18 22:13:29 +01005662 if (top_rows == bot_rows)
5663 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005664 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005665 for (lnum = 0; lnum < top_rows; ++lnum)
5666 {
5667 sb_line_T temp;
5668
5669 temp = *(sb_line + lnum);
5670 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5671 *(sb_line + bot_start + lnum) = temp;
5672 }
5673 }
5674 else
5675 {
5676 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005677 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005678
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005679 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005680 if (temp != NULL)
5681 {
5682 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5683 mch_memmove(term->tl_scrollback.ga_data,
5684 temp + bot_start,
5685 sizeof(sb_line_T) * bot_rows);
5686 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5687 temp + top_rows,
5688 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5689 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5690 + line_count - top_rows,
5691 temp,
5692 sizeof(sb_line_T) * top_rows);
5693 vim_free(temp);
5694 }
5695 }
5696
5697 term->tl_top_diff_rows = bot_rows;
5698 term->tl_bot_diff_rows = top_rows;
5699
5700 update_screen(NOT_VALID);
5701 return OK;
5702}
5703
5704/*
5705 * "term_dumpdiff(filename, filename, options)" function
5706 */
5707 void
5708f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5709{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005710 if (in_vim9script()
5711 && (check_for_string_arg(argvars, 0) == FAIL
5712 || check_for_string_arg(argvars, 1) == FAIL
5713 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5714 return;
5715
Bram Moolenaard96ff162018-02-18 22:13:29 +01005716 term_load_dump(argvars, rettv, TRUE);
5717}
5718
5719/*
5720 * "term_dumpload(filename, options)" function
5721 */
5722 void
5723f_term_dumpload(typval_T *argvars, typval_T *rettv)
5724{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005725 if (in_vim9script()
5726 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005727 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005728 return;
5729
Bram Moolenaard96ff162018-02-18 22:13:29 +01005730 term_load_dump(argvars, rettv, FALSE);
5731}
5732
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005733/*
5734 * "term_getaltscreen(buf)" function
5735 */
5736 void
5737f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5738{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005739 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005740
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005741 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5742 return;
5743
5744 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005745 if (buf == NULL)
5746 return;
5747 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5748}
5749
5750/*
5751 * "term_getattr(attr, name)" function
5752 */
5753 void
5754f_term_getattr(typval_T *argvars, typval_T *rettv)
5755{
5756 int attr;
5757 size_t i;
5758 char_u *name;
5759
5760 static struct {
5761 char *name;
5762 int attr;
5763 } attrs[] = {
5764 {"bold", HL_BOLD},
5765 {"italic", HL_ITALIC},
5766 {"underline", HL_UNDERLINE},
5767 {"strike", HL_STRIKETHROUGH},
5768 {"reverse", HL_INVERSE},
5769 };
5770
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005771 if (in_vim9script()
5772 && (check_for_number_arg(argvars, 0) == FAIL
5773 || check_for_string_arg(argvars, 1) == FAIL))
5774 return;
5775
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005776 attr = tv_get_number(&argvars[0]);
5777 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005778 if (name == NULL)
5779 return;
5780
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005781 if (attr > HL_ALL)
5782 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02005783 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005784 if (STRCMP(name, attrs[i].name) == 0)
5785 {
5786 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5787 break;
5788 }
5789}
5790
5791/*
5792 * "term_getcursor(buf)" function
5793 */
5794 void
5795f_term_getcursor(typval_T *argvars, typval_T *rettv)
5796{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005797 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005798 term_T *term;
5799 list_T *l;
5800 dict_T *d;
5801
5802 if (rettv_list_alloc(rettv) == FAIL)
5803 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005804
5805 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5806 return;
5807
5808 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005809 if (buf == NULL)
5810 return;
5811 term = buf->b_term;
5812
5813 l = rettv->vval.v_list;
5814 list_append_number(l, term->tl_cursor_pos.row + 1);
5815 list_append_number(l, term->tl_cursor_pos.col + 1);
5816
5817 d = dict_alloc();
5818 if (d != NULL)
5819 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005820 dict_add_number(d, "visible", term->tl_cursor_visible);
5821 dict_add_number(d, "blink", blink_state_is_inverted()
5822 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5823 dict_add_number(d, "shape", term->tl_cursor_shape);
5824 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005825 list_append_dict(l, d);
5826 }
5827}
5828
5829/*
5830 * "term_getjob(buf)" function
5831 */
5832 void
5833f_term_getjob(typval_T *argvars, typval_T *rettv)
5834{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005835 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005836
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005837 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5838 return;
5839
5840 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005841 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005842 {
5843 rettv->v_type = VAR_SPECIAL;
5844 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005845 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005846 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005847
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005848 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005849 rettv->vval.v_job = buf->b_term->tl_job;
5850 if (rettv->vval.v_job != NULL)
5851 ++rettv->vval.v_job->jv_refcount;
5852}
5853
5854 static int
5855get_row_number(typval_T *tv, term_T *term)
5856{
5857 if (tv->v_type == VAR_STRING
5858 && tv->vval.v_string != NULL
5859 && STRCMP(tv->vval.v_string, ".") == 0)
5860 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005861 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005862}
5863
5864/*
5865 * "term_getline(buf, row)" function
5866 */
5867 void
5868f_term_getline(typval_T *argvars, typval_T *rettv)
5869{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005870 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005871 term_T *term;
5872 int row;
5873
5874 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005875
5876 if (in_vim9script()
5877 && (check_for_buffer_arg(argvars, 0) == FAIL
5878 || check_for_lnum_arg(argvars, 1) == FAIL))
5879 return;
5880
5881 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005882 if (buf == NULL)
5883 return;
5884 term = buf->b_term;
5885 row = get_row_number(&argvars[1], term);
5886
5887 if (term->tl_vterm == NULL)
5888 {
5889 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5890
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005891 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005892 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5893 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5894 }
5895 else
5896 {
5897 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5898 VTermRect rect;
5899 int len;
5900 char_u *p;
5901
5902 if (row < 0 || row >= term->tl_rows)
5903 return;
5904 len = term->tl_cols * MB_MAXBYTES + 1;
5905 p = alloc(len);
5906 if (p == NULL)
5907 return;
5908 rettv->vval.v_string = p;
5909
5910 rect.start_col = 0;
5911 rect.end_col = term->tl_cols;
5912 rect.start_row = row;
5913 rect.end_row = row + 1;
5914 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5915 }
5916}
5917
5918/*
5919 * "term_getscrolled(buf)" function
5920 */
5921 void
5922f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5923{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005924 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005925
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005926 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5927 return;
5928
5929 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005930 if (buf == NULL)
5931 return;
5932 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5933}
5934
5935/*
5936 * "term_getsize(buf)" function
5937 */
5938 void
5939f_term_getsize(typval_T *argvars, typval_T *rettv)
5940{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005941 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005942 list_T *l;
5943
5944 if (rettv_list_alloc(rettv) == FAIL)
5945 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005946
5947 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5948 return;
5949
5950 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005951 if (buf == NULL)
5952 return;
5953
5954 l = rettv->vval.v_list;
5955 list_append_number(l, buf->b_term->tl_rows);
5956 list_append_number(l, buf->b_term->tl_cols);
5957}
5958
5959/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005960 * "term_setsize(buf, rows, cols)" function
5961 */
5962 void
5963f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5964{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005965 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02005966 term_T *term;
5967 varnumber_T rows, cols;
5968
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005969 if (in_vim9script()
5970 && (check_for_buffer_arg(argvars, 0) == FAIL
5971 || check_for_number_arg(argvars, 1) == FAIL
5972 || check_for_number_arg(argvars, 2) == FAIL))
5973 return;
5974
5975 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005976 if (buf == NULL)
5977 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005978 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005979 return;
5980 }
5981 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005982 return;
5983 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005984 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005985 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005986 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005987 cols = cols <= 0 ? term->tl_cols : cols;
5988 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005989 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005990
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005991 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005992 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5993 term_report_winsize(term, term->tl_rows, term->tl_cols);
5994}
5995
5996/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005997 * "term_getstatus(buf)" function
5998 */
5999 void
6000f_term_getstatus(typval_T *argvars, typval_T *rettv)
6001{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006002 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006003 term_T *term;
6004 char_u val[100];
6005
6006 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006007
6008 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6009 return;
6010
6011 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006012 if (buf == NULL)
6013 return;
6014 term = buf->b_term;
6015
6016 if (term_job_running(term))
6017 STRCPY(val, "running");
6018 else
6019 STRCPY(val, "finished");
6020 if (term->tl_normal_mode)
6021 STRCAT(val, ",normal");
6022 rettv->vval.v_string = vim_strsave(val);
6023}
6024
6025/*
6026 * "term_gettitle(buf)" function
6027 */
6028 void
6029f_term_gettitle(typval_T *argvars, typval_T *rettv)
6030{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006031 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006032
6033 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006034
6035 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6036 return;
6037
6038 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006039 if (buf == NULL)
6040 return;
6041
6042 if (buf->b_term->tl_title != NULL)
6043 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
6044}
6045
6046/*
6047 * "term_gettty(buf)" function
6048 */
6049 void
6050f_term_gettty(typval_T *argvars, typval_T *rettv)
6051{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006052 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02006053 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006054 int num = 0;
6055
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006056 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006057 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006058 || check_for_opt_bool_arg(argvars, 1) == FAIL))
6059 return;
6060
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006061 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006062 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006063 if (buf == NULL)
6064 return;
6065 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02006066 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006067
6068 switch (num)
6069 {
6070 case 0:
6071 if (buf->b_term->tl_job != NULL)
6072 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006073 break;
6074 case 1:
6075 if (buf->b_term->tl_job != NULL)
6076 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006077 break;
6078 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006079 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006080 return;
6081 }
6082 if (p != NULL)
6083 rettv->vval.v_string = vim_strsave(p);
6084}
6085
6086/*
6087 * "term_list()" function
6088 */
6089 void
6090f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
6091{
6092 term_T *tp;
6093 list_T *l;
6094
6095 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
6096 return;
6097
6098 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006099 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02006100 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006101 if (list_append_number(l,
6102 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6103 return;
6104}
6105
6106/*
6107 * "term_scrape(buf, row)" function
6108 */
6109 void
6110f_term_scrape(typval_T *argvars, typval_T *rettv)
6111{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006112 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006113 VTermScreen *screen = NULL;
6114 VTermPos pos;
6115 list_T *l;
6116 term_T *term;
6117 char_u *p;
6118 sb_line_T *line;
6119
6120 if (rettv_list_alloc(rettv) == FAIL)
6121 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006122
6123 if (in_vim9script()
6124 && (check_for_buffer_arg(argvars, 0) == FAIL
6125 || check_for_lnum_arg(argvars, 1) == FAIL))
6126 return;
6127
6128 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006129 if (buf == NULL)
6130 return;
6131 term = buf->b_term;
6132
6133 l = rettv->vval.v_list;
6134 pos.row = get_row_number(&argvars[1], term);
6135
6136 if (term->tl_vterm != NULL)
6137 {
6138 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006139 if (screen == NULL) // can't really happen
6140 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006141 p = NULL;
6142 line = NULL;
6143 }
6144 else
6145 {
6146 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6147
6148 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6149 return;
6150 p = ml_get_buf(buf, lnum + 1, FALSE);
6151 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6152 }
6153
6154 for (pos.col = 0; pos.col < term->tl_cols; )
6155 {
6156 dict_T *dcell;
6157 int width;
6158 VTermScreenCellAttrs attrs;
6159 VTermColor fg, bg;
6160 char_u rgb[8];
6161 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6162 int off = 0;
6163 int i;
6164
6165 if (screen == NULL)
6166 {
6167 cellattr_T *cellattr;
6168 int len;
6169
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006170 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006171 if (pos.col >= line->sb_cols)
6172 break;
6173 cellattr = line->sb_cells + pos.col;
6174 width = cellattr->width;
6175 attrs = cellattr->attrs;
6176 fg = cellattr->fg;
6177 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006178 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006179 mch_memmove(mbs, p, len);
6180 mbs[len] = NUL;
6181 p += len;
6182 }
6183 else
6184 {
6185 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006186
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006187 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6188 break;
6189 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6190 {
6191 if (cell.chars[i] == 0)
6192 break;
6193 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6194 }
6195 mbs[off] = NUL;
6196 width = cell.width;
6197 attrs = cell.attrs;
6198 fg = cell.fg;
6199 bg = cell.bg;
6200 }
6201 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006202 if (dcell == NULL)
6203 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006204 list_append_dict(l, dcell);
6205
Bram Moolenaare0be1672018-07-08 16:50:37 +02006206 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006207
6208 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6209 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006210 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006211 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6212 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006213 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006214
Bram Moolenaar87fd0922021-11-20 13:47:45 +00006215 dict_add_number(dcell, "attr",
6216 cell2attr(term, NULL, &attrs, &fg, &bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006217 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006218
6219 ++pos.col;
6220 if (width == 2)
6221 ++pos.col;
6222 }
6223}
6224
6225/*
6226 * "term_sendkeys(buf, keys)" function
6227 */
6228 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006229f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006230{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006231 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006232 char_u *msg;
6233 term_T *term;
6234
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006235 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006236 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006237 || check_for_string_arg(argvars, 1) == FAIL))
6238 return;
6239
6240 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006241 if (buf == NULL)
6242 return;
6243
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006244 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006245 if (msg == NULL)
6246 return;
6247 term = buf->b_term;
6248 if (term->tl_vterm == NULL)
6249 return;
6250
6251 while (*msg != NUL)
6252 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006253 int c;
6254
6255 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6256 {
6257 c = TO_SPECIAL(msg[1], msg[2]);
6258 msg += 3;
6259 }
6260 else
6261 {
6262 c = PTR2CHAR(msg);
6263 msg += MB_CPTR2LEN(msg);
6264 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006265 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006266 }
6267}
6268
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006269#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6270/*
6271 * "term_getansicolors(buf)" function
6272 */
6273 void
6274f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6275{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006276 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006277 term_T *term;
6278 VTermState *state;
6279 VTermColor color;
6280 char_u hexbuf[10];
6281 int index;
6282 list_T *list;
6283
6284 if (rettv_list_alloc(rettv) == FAIL)
6285 return;
6286
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006287 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6288 return;
6289
6290 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006291 if (buf == NULL)
6292 return;
6293 term = buf->b_term;
6294 if (term->tl_vterm == NULL)
6295 return;
6296
6297 list = rettv->vval.v_list;
6298 state = vterm_obtain_state(term->tl_vterm);
6299 for (index = 0; index < 16; index++)
6300 {
6301 vterm_state_get_palette_color(state, index, &color);
6302 sprintf((char *)hexbuf, "#%02x%02x%02x",
6303 color.red, color.green, color.blue);
6304 if (list_append_string(list, hexbuf, 7) == FAIL)
6305 return;
6306 }
6307}
6308
6309/*
6310 * "term_setansicolors(buf, list)" function
6311 */
6312 void
6313f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6314{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006315 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006316 term_T *term;
6317
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006318 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006319 && (check_for_buffer_arg(argvars, 0) == FAIL
6320 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006321 return;
6322
6323 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006324 if (buf == NULL)
6325 return;
6326 term = buf->b_term;
6327 if (term->tl_vterm == NULL)
6328 return;
6329
6330 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6331 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006332 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006333 return;
6334 }
6335
6336 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006337 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006338}
6339#endif
6340
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006341/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006342 * "term_setapi(buf, api)" function
6343 */
6344 void
6345f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6346{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006347 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006348 term_T *term;
6349 char_u *api;
6350
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006351 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006352 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006353 || check_for_string_arg(argvars, 1) == FAIL))
6354 return;
6355
6356 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006357 if (buf == NULL)
6358 return;
6359 term = buf->b_term;
6360 vim_free(term->tl_api);
6361 api = tv_get_string_chk(&argvars[1]);
6362 if (api != NULL)
6363 term->tl_api = vim_strsave(api);
6364 else
6365 term->tl_api = NULL;
6366}
6367
6368/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006369 * "term_setrestore(buf, command)" function
6370 */
6371 void
6372f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6373{
6374#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006375 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006376 term_T *term;
6377 char_u *cmd;
6378
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006379 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006380 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006381 || check_for_string_arg(argvars, 1) == FAIL))
6382 return;
6383
6384 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006385 if (buf == NULL)
6386 return;
6387 term = buf->b_term;
6388 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006389 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006390 if (cmd != NULL)
6391 term->tl_command = vim_strsave(cmd);
6392 else
6393 term->tl_command = NULL;
6394#endif
6395}
6396
6397/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006398 * "term_setkill(buf, how)" function
6399 */
6400 void
6401f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6402{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006403 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006404 term_T *term;
6405 char_u *how;
6406
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006407 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006408 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006409 || check_for_string_arg(argvars, 1) == FAIL))
6410 return;
6411
6412 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006413 if (buf == NULL)
6414 return;
6415 term = buf->b_term;
6416 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006417 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006418 if (how != NULL)
6419 term->tl_kill = vim_strsave(how);
6420 else
6421 term->tl_kill = NULL;
6422}
6423
6424/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006425 * "term_start(command, options)" function
6426 */
6427 void
6428f_term_start(typval_T *argvars, typval_T *rettv)
6429{
6430 jobopt_T opt;
6431 buf_T *buf;
6432
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006433 if (in_vim9script()
6434 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6435 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6436 return;
6437
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006438 init_job_options(&opt);
6439 if (argvars[1].v_type != VAR_UNKNOWN
6440 && get_job_options(&argvars[1], &opt,
6441 JO_TIMEOUT_ALL + JO_STOPONEXIT
6442 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6443 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6444 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6445 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006446 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006447 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006448 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006449 return;
6450
Bram Moolenaar13568252018-03-16 20:46:58 +01006451 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006452
6453 if (buf != NULL && buf->b_term != NULL)
6454 rettv->vval.v_number = buf->b_fnum;
6455}
6456
6457/*
6458 * "term_wait" function
6459 */
6460 void
6461f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6462{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006463 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006464
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006465 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006466 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006467 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006468 return;
6469
6470 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006471 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006472 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006473 if (buf->b_term->tl_job == NULL)
6474 {
6475 ch_log(NULL, "term_wait(): no job to wait for");
6476 return;
6477 }
6478 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006479 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006480 return;
6481
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006482 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006483 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006484 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6485 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006486 // The job is dead, keep reading channel I/O until the channel is
6487 // closed. buf->b_term may become NULL if the terminal was closed while
6488 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006489 ch_log(NULL, "term_wait(): waiting for channel to close");
6490 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6491 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006492 term_flush_messages();
6493
Bram Moolenaard45aa552018-05-21 22:50:29 +02006494 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006495 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006496 // If the terminal is closed when the channel is closed the
6497 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006498 break;
Bram Moolenaareea32af2021-11-21 14:51:13 +00006499 if (buf->b_term == NULL || buf->b_term->tl_channel_closing)
6500 // came here from a close callback, only wait one time
6501 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006502 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006503
6504 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006505 }
6506 else
6507 {
6508 long wait = 10L;
6509
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006510 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006511
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006512 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006513 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006514 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006515 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006516
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006517 // Flushing messages on channels is hopefully sufficient.
6518 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006519 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006520 }
6521}
6522
6523/*
6524 * Called when a channel has sent all the lines to a terminal.
6525 * Send a CTRL-D to mark the end of the text.
6526 */
6527 void
6528term_send_eof(channel_T *ch)
6529{
6530 term_T *term;
6531
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006532 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006533 if (term->tl_job == ch->ch_job)
6534 {
6535 if (term->tl_eof_chars != NULL)
6536 {
6537 channel_send(ch, PART_IN, term->tl_eof_chars,
6538 (int)STRLEN(term->tl_eof_chars), NULL);
6539 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6540 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006541# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006542 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006543 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006544 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6545# endif
6546 }
6547}
6548
Bram Moolenaar113e1072019-01-20 15:30:40 +01006549#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006550 job_T *
6551term_getjob(term_T *term)
6552{
6553 return term != NULL ? term->tl_job : NULL;
6554}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006555#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006556
Bram Moolenaar4f974752019-02-17 17:44:42 +01006557# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006558
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006559///////////////////////////////////////
6560// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006561#ifdef PROTO
6562typedef int COORD;
6563typedef int DWORD;
6564typedef int HANDLE;
6565typedef int *DWORD_PTR;
6566typedef int HPCON;
6567typedef int HRESULT;
6568typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006569typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006570typedef int PSIZE_T;
6571typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006572typedef int BOOL;
6573# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006574#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006575
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006576HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6577HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6578HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006579BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6580BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6581void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006582
6583 static int
6584dyn_conpty_init(int verbose)
6585{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006586 static HMODULE hKerneldll = NULL;
6587 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006588 static struct
6589 {
6590 char *name;
6591 FARPROC *ptr;
6592 } conpty_entry[] =
6593 {
6594 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6595 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6596 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6597 {"InitializeProcThreadAttributeList",
6598 (FARPROC*)&pInitializeProcThreadAttributeList},
6599 {"UpdateProcThreadAttribute",
6600 (FARPROC*)&pUpdateProcThreadAttribute},
6601 {"DeleteProcThreadAttributeList",
6602 (FARPROC*)&pDeleteProcThreadAttributeList},
6603 {NULL, NULL}
6604 };
6605
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006606 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006607 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006608 if (verbose)
6609 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006610 return FAIL;
6611 }
6612
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006613 // No need to initialize twice.
6614 if (hKerneldll)
6615 return OK;
6616
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006617 hKerneldll = vimLoadLib("kernel32.dll");
6618 for (i = 0; conpty_entry[i].name != NULL
6619 && conpty_entry[i].ptr != NULL; ++i)
6620 {
6621 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6622 conpty_entry[i].name)) == NULL)
6623 {
6624 if (verbose)
6625 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006626 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006627 return FAIL;
6628 }
6629 }
6630
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006631 return OK;
6632}
6633
6634 static int
6635conpty_term_and_job_init(
6636 term_T *term,
6637 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006638 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006639 jobopt_T *opt,
6640 jobopt_T *orig_opt)
6641{
6642 WCHAR *cmd_wchar = NULL;
6643 WCHAR *cmd_wchar_copy = NULL;
6644 WCHAR *cwd_wchar = NULL;
6645 WCHAR *env_wchar = NULL;
6646 channel_T *channel = NULL;
6647 job_T *job = NULL;
6648 HANDLE jo = NULL;
6649 garray_T ga_cmd, ga_env;
6650 char_u *cmd = NULL;
6651 HRESULT hr;
6652 COORD consize;
6653 SIZE_T breq;
6654 PROCESS_INFORMATION proc_info;
6655 HANDLE i_theirs = NULL;
6656 HANDLE o_theirs = NULL;
6657 HANDLE i_ours = NULL;
6658 HANDLE o_ours = NULL;
6659
6660 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6661 ga_init2(&ga_env, (int)sizeof(char*), 20);
6662
6663 if (argvar->v_type == VAR_STRING)
6664 {
6665 cmd = argvar->vval.v_string;
6666 }
6667 else if (argvar->v_type == VAR_LIST)
6668 {
6669 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6670 goto failed;
6671 cmd = ga_cmd.ga_data;
6672 }
6673 if (cmd == NULL || *cmd == NUL)
6674 {
6675 emsg(_(e_invarg));
6676 goto failed;
6677 }
6678
6679 term->tl_arg0_cmd = vim_strsave(cmd);
6680
6681 cmd_wchar = enc_to_utf16(cmd, NULL);
6682
6683 if (cmd_wchar != NULL)
6684 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006685 // Request by CreateProcessW
6686 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006687 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006688 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6689 }
6690
6691 ga_clear(&ga_cmd);
6692 if (cmd_wchar == NULL)
6693 goto failed;
6694 if (opt->jo_cwd != NULL)
6695 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6696
6697 win32_build_env(opt->jo_env, &ga_env, TRUE);
6698 env_wchar = ga_env.ga_data;
6699
6700 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6701 goto failed;
6702 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6703 goto failed;
6704
6705 consize.X = term->tl_cols;
6706 consize.Y = term->tl_rows;
6707 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6708 &term->tl_conpty);
6709 if (FAILED(hr))
6710 goto failed;
6711
6712 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6713
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006714 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006715 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006716 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006717 if (!term->tl_siex.lpAttributeList)
6718 goto failed;
6719 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6720 0, &breq))
6721 goto failed;
6722 if (!pUpdateProcThreadAttribute(
6723 term->tl_siex.lpAttributeList, 0,
6724 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6725 sizeof(HPCON), NULL, NULL))
6726 goto failed;
6727
6728 channel = add_channel();
6729 if (channel == NULL)
6730 goto failed;
6731
6732 job = job_alloc();
6733 if (job == NULL)
6734 goto failed;
6735 if (argvar->v_type == VAR_STRING)
6736 {
6737 int argc;
6738
6739 build_argv_from_string(cmd, &job->jv_argv, &argc);
6740 }
6741 else
6742 {
6743 int argc;
6744
6745 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6746 }
6747
6748 if (opt->jo_set & JO_IN_BUF)
6749 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6750
6751 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6752 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006753 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006754 env_wchar, cwd_wchar,
6755 &term->tl_siex.StartupInfo, &proc_info))
6756 goto failed;
6757
6758 CloseHandle(i_theirs);
6759 CloseHandle(o_theirs);
6760
6761 channel_set_pipes(channel,
6762 (sock_T)i_ours,
6763 (sock_T)o_ours,
6764 (sock_T)o_ours);
6765
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006766 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006767 channel->ch_write_text_mode = TRUE;
6768
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006769 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006770 channel->ch_anonymous_pipe = TRUE;
6771
6772 jo = CreateJobObject(NULL, NULL);
6773 if (jo == NULL)
6774 goto failed;
6775
6776 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6777 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006778 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006779 CloseHandle(jo);
6780 jo = NULL;
6781 }
6782
6783 ResumeThread(proc_info.hThread);
6784 CloseHandle(proc_info.hThread);
6785
6786 vim_free(cmd_wchar);
6787 vim_free(cmd_wchar_copy);
6788 vim_free(cwd_wchar);
6789 vim_free(env_wchar);
6790
6791 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6792 goto failed;
6793
6794#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6795 if (opt->jo_set2 & JO2_ANSI_COLORS)
6796 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6797 else
6798 init_vterm_ansi_colors(term->tl_vterm);
6799#endif
6800
6801 channel_set_job(channel, job, opt);
6802 job_set_options(job, opt);
6803
6804 job->jv_channel = channel;
6805 job->jv_proc_info = proc_info;
6806 job->jv_job_object = jo;
6807 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006808 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006809 ++job->jv_refcount;
6810 term->tl_job = job;
6811
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006812 // Redirecting stdout and stderr doesn't work at the job level. Instead
6813 // open the file here and handle it in. opt->jo_io was changed in
6814 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006815 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6816 {
6817 char_u *fname = opt->jo_io_name[PART_OUT];
6818
6819 ch_log(channel, "Opening output file %s", fname);
6820 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6821 if (term->tl_out_fd == NULL)
6822 semsg(_(e_notopen), fname);
6823 }
6824
6825 return OK;
6826
6827failed:
6828 ga_clear(&ga_cmd);
6829 ga_clear(&ga_env);
6830 vim_free(cmd_wchar);
6831 vim_free(cmd_wchar_copy);
6832 vim_free(cwd_wchar);
6833 if (channel != NULL)
6834 channel_clear(channel);
6835 if (job != NULL)
6836 {
6837 job->jv_channel = NULL;
6838 job_cleanup(job);
6839 }
6840 term->tl_job = NULL;
6841 if (jo != NULL)
6842 CloseHandle(jo);
6843
6844 if (term->tl_siex.lpAttributeList != NULL)
6845 {
6846 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6847 vim_free(term->tl_siex.lpAttributeList);
6848 }
6849 term->tl_siex.lpAttributeList = NULL;
6850 if (o_theirs != NULL)
6851 CloseHandle(o_theirs);
6852 if (o_ours != NULL)
6853 CloseHandle(o_ours);
6854 if (i_ours != NULL)
6855 CloseHandle(i_ours);
6856 if (i_theirs != NULL)
6857 CloseHandle(i_theirs);
6858 if (term->tl_conpty != NULL)
6859 pClosePseudoConsole(term->tl_conpty);
6860 term->tl_conpty = NULL;
6861 return FAIL;
6862}
6863
6864 static void
6865conpty_term_report_winsize(term_T *term, int rows, int cols)
6866{
6867 COORD consize;
6868
6869 consize.X = cols;
6870 consize.Y = rows;
6871 pResizePseudoConsole(term->tl_conpty, consize);
6872}
6873
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006874 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006875term_free_conpty(term_T *term)
6876{
6877 if (term->tl_siex.lpAttributeList != NULL)
6878 {
6879 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6880 vim_free(term->tl_siex.lpAttributeList);
6881 }
6882 term->tl_siex.lpAttributeList = NULL;
6883 if (term->tl_conpty != NULL)
6884 pClosePseudoConsole(term->tl_conpty);
6885 term->tl_conpty = NULL;
6886}
6887
6888 int
6889use_conpty(void)
6890{
6891 return has_conpty;
6892}
6893
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006894# ifndef PROTO
6895
6896#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6897#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006898#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006899
6900void* (*winpty_config_new)(UINT64, void*);
6901void* (*winpty_open)(void*, void*);
6902void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6903BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6904void (*winpty_config_set_mouse_mode)(void*, int);
6905void (*winpty_config_set_initial_size)(void*, int, int);
6906LPCWSTR (*winpty_conin_name)(void*);
6907LPCWSTR (*winpty_conout_name)(void*);
6908LPCWSTR (*winpty_conerr_name)(void*);
6909void (*winpty_free)(void*);
6910void (*winpty_config_free)(void*);
6911void (*winpty_spawn_config_free)(void*);
6912void (*winpty_error_free)(void*);
6913LPCWSTR (*winpty_error_msg)(void*);
6914BOOL (*winpty_set_size)(void*, int, int, void*);
6915HANDLE (*winpty_agent_process)(void*);
6916
6917#define WINPTY_DLL "winpty.dll"
6918
6919static HINSTANCE hWinPtyDLL = NULL;
6920# endif
6921
6922 static int
6923dyn_winpty_init(int verbose)
6924{
6925 int i;
6926 static struct
6927 {
6928 char *name;
6929 FARPROC *ptr;
6930 } winpty_entry[] =
6931 {
6932 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6933 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6934 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6935 {"winpty_config_set_mouse_mode",
6936 (FARPROC*)&winpty_config_set_mouse_mode},
6937 {"winpty_config_set_initial_size",
6938 (FARPROC*)&winpty_config_set_initial_size},
6939 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6940 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6941 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6942 {"winpty_free", (FARPROC*)&winpty_free},
6943 {"winpty_open", (FARPROC*)&winpty_open},
6944 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6945 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6946 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6947 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6948 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6949 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6950 {NULL, NULL}
6951 };
6952
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006953 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006954 if (hWinPtyDLL)
6955 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006956 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6957 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006958 if (*p_winptydll != NUL)
6959 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6960 if (!hWinPtyDLL)
6961 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6962 if (!hWinPtyDLL)
6963 {
6964 if (verbose)
Martin Tournoij1a3e5742021-07-24 13:57:29 +02006965 semsg(_(e_loadlib),
6966 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
6967 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006968 return FAIL;
6969 }
6970 for (i = 0; winpty_entry[i].name != NULL
6971 && winpty_entry[i].ptr != NULL; ++i)
6972 {
6973 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6974 winpty_entry[i].name)) == NULL)
6975 {
6976 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006977 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006978 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006979 return FAIL;
6980 }
6981 }
6982
6983 return OK;
6984}
6985
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006986 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006987winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006988 term_T *term,
6989 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006990 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006991 jobopt_T *opt,
6992 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006993{
6994 WCHAR *cmd_wchar = NULL;
6995 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006996 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006997 channel_T *channel = NULL;
6998 job_T *job = NULL;
6999 DWORD error;
7000 HANDLE jo = NULL;
7001 HANDLE child_process_handle;
7002 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01007003 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007004 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007005 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007006 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007007
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007008 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
7009 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007010
7011 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007012 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007013 cmd = argvar->vval.v_string;
7014 }
7015 else if (argvar->v_type == VAR_LIST)
7016 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007017 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007018 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007019 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007020 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007021 if (cmd == NULL || *cmd == NUL)
7022 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007023 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007024 goto failed;
7025 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007026
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007027 term->tl_arg0_cmd = vim_strsave(cmd);
7028
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007029 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007030 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007031 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007032 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007033 if (opt->jo_cwd != NULL)
7034 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007035
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007036 win32_build_env(opt->jo_env, &ga_env, TRUE);
7037 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007038
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007039 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
7040 if (term->tl_winpty_config == NULL)
7041 goto failed;
7042
7043 winpty_config_set_mouse_mode(term->tl_winpty_config,
7044 WINPTY_MOUSE_MODE_FORCE);
7045 winpty_config_set_initial_size(term->tl_winpty_config,
7046 term->tl_cols, term->tl_rows);
7047 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
7048 if (term->tl_winpty == NULL)
7049 goto failed;
7050
7051 spawn_config = winpty_spawn_config_new(
7052 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
7053 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
7054 NULL,
7055 cmd_wchar,
7056 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007057 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007058 &winpty_err);
7059 if (spawn_config == NULL)
7060 goto failed;
7061
7062 channel = add_channel();
7063 if (channel == NULL)
7064 goto failed;
7065
7066 job = job_alloc();
7067 if (job == NULL)
7068 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02007069 if (argvar->v_type == VAR_STRING)
7070 {
7071 int argc;
7072
7073 build_argv_from_string(cmd, &job->jv_argv, &argc);
7074 }
7075 else
7076 {
7077 int argc;
7078
7079 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
7080 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007081
7082 if (opt->jo_set & JO_IN_BUF)
7083 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
7084
7085 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
7086 &child_thread_handle, &error, &winpty_err))
7087 goto failed;
7088
7089 channel_set_pipes(channel,
7090 (sock_T)CreateFileW(
7091 winpty_conin_name(term->tl_winpty),
7092 GENERIC_WRITE, 0, NULL,
7093 OPEN_EXISTING, 0, NULL),
7094 (sock_T)CreateFileW(
7095 winpty_conout_name(term->tl_winpty),
7096 GENERIC_READ, 0, NULL,
7097 OPEN_EXISTING, 0, NULL),
7098 (sock_T)CreateFileW(
7099 winpty_conerr_name(term->tl_winpty),
7100 GENERIC_READ, 0, NULL,
7101 OPEN_EXISTING, 0, NULL));
7102
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007103 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007104 channel->ch_write_text_mode = TRUE;
7105
7106 jo = CreateJobObject(NULL, NULL);
7107 if (jo == NULL)
7108 goto failed;
7109
7110 if (!AssignProcessToJobObject(jo, child_process_handle))
7111 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007112 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007113 CloseHandle(jo);
7114 jo = NULL;
7115 }
7116
7117 winpty_spawn_config_free(spawn_config);
7118 vim_free(cmd_wchar);
7119 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007120 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007121
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007122 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7123 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007124
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007125#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7126 if (opt->jo_set2 & JO2_ANSI_COLORS)
7127 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7128 else
7129 init_vterm_ansi_colors(term->tl_vterm);
7130#endif
7131
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007132 channel_set_job(channel, job, opt);
7133 job_set_options(job, opt);
7134
7135 job->jv_channel = channel;
7136 job->jv_proc_info.hProcess = child_process_handle;
7137 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7138 job->jv_job_object = jo;
7139 job->jv_status = JOB_STARTED;
7140 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007141 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007142 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007143 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007144 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007145 ++job->jv_refcount;
7146 term->tl_job = job;
7147
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007148 // Redirecting stdout and stderr doesn't work at the job level. Instead
7149 // open the file here and handle it in. opt->jo_io was changed in
7150 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007151 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7152 {
7153 char_u *fname = opt->jo_io_name[PART_OUT];
7154
7155 ch_log(channel, "Opening output file %s", fname);
7156 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7157 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007158 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007159 }
7160
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007161 return OK;
7162
7163failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007164 ga_clear(&ga_cmd);
7165 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007166 vim_free(cmd_wchar);
7167 vim_free(cwd_wchar);
7168 if (spawn_config != NULL)
7169 winpty_spawn_config_free(spawn_config);
7170 if (channel != NULL)
7171 channel_clear(channel);
7172 if (job != NULL)
7173 {
7174 job->jv_channel = NULL;
7175 job_cleanup(job);
7176 }
7177 term->tl_job = NULL;
7178 if (jo != NULL)
7179 CloseHandle(jo);
7180 if (term->tl_winpty != NULL)
7181 winpty_free(term->tl_winpty);
7182 term->tl_winpty = NULL;
7183 if (term->tl_winpty_config != NULL)
7184 winpty_config_free(term->tl_winpty_config);
7185 term->tl_winpty_config = NULL;
7186 if (winpty_err != NULL)
7187 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007188 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007189 (short_u *)winpty_error_msg(winpty_err), NULL);
7190
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007191 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007192 winpty_error_free(winpty_err);
7193 }
7194 return FAIL;
7195}
7196
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007197/*
7198 * Create a new terminal of "rows" by "cols" cells.
7199 * Store a reference in "term".
7200 * Return OK or FAIL.
7201 */
7202 static int
7203term_and_job_init(
7204 term_T *term,
7205 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007206 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007207 jobopt_T *opt,
7208 jobopt_T *orig_opt)
7209{
7210 int use_winpty = FALSE;
7211 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007212 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007213
7214 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7215 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7216
7217 if (!has_winpty && !has_conpty)
7218 // If neither is available give the errors for winpty, since when
7219 // conpty is not available it can't be installed either.
7220 return dyn_winpty_init(TRUE);
7221
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007222 if (opt->jo_tty_type != NUL)
7223 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007224
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007225 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007226 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007227 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007228 use_conpty = TRUE;
7229 else if (has_winpty)
7230 use_winpty = TRUE;
7231 // else: error
7232 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007233 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007234 {
7235 if (has_winpty)
7236 use_winpty = TRUE;
7237 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007238 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007239 {
7240 if (has_conpty)
7241 use_conpty = TRUE;
7242 else
7243 return dyn_conpty_init(TRUE);
7244 }
7245
7246 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007247 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007248
7249 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007250 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007251
7252 // error
7253 return dyn_winpty_init(TRUE);
7254}
7255
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007256 static int
7257create_pty_only(term_T *term, jobopt_T *options)
7258{
7259 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7260 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7261 char in_name[80], out_name[80];
7262 channel_T *channel = NULL;
7263
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007264 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7265 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007266
7267 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7268 GetCurrentProcessId(),
7269 curbuf->b_fnum);
7270 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7271 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7272 PIPE_UNLIMITED_INSTANCES,
7273 0, 0, NMPWAIT_NOWAIT, NULL);
7274 if (hPipeIn == INVALID_HANDLE_VALUE)
7275 goto failed;
7276
7277 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7278 GetCurrentProcessId(),
7279 curbuf->b_fnum);
7280 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7281 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7282 PIPE_UNLIMITED_INSTANCES,
7283 0, 0, 0, NULL);
7284 if (hPipeOut == INVALID_HANDLE_VALUE)
7285 goto failed;
7286
7287 ConnectNamedPipe(hPipeIn, NULL);
7288 ConnectNamedPipe(hPipeOut, NULL);
7289
7290 term->tl_job = job_alloc();
7291 if (term->tl_job == NULL)
7292 goto failed;
7293 ++term->tl_job->jv_refcount;
7294
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007295 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007296 term->tl_job->jv_status = JOB_FINISHED;
7297
7298 channel = add_channel();
7299 if (channel == NULL)
7300 goto failed;
7301 term->tl_job->jv_channel = channel;
7302 channel->ch_keep_open = TRUE;
7303 channel->ch_named_pipe = TRUE;
7304
7305 channel_set_pipes(channel,
7306 (sock_T)hPipeIn,
7307 (sock_T)hPipeOut,
7308 (sock_T)hPipeOut);
7309 channel_set_job(channel, term->tl_job, options);
7310 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7311 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7312
7313 return OK;
7314
7315failed:
7316 if (hPipeIn != NULL)
7317 CloseHandle(hPipeIn);
7318 if (hPipeOut != NULL)
7319 CloseHandle(hPipeOut);
7320 return FAIL;
7321}
7322
7323/*
7324 * Free the terminal emulator part of "term".
7325 */
7326 static void
7327term_free_vterm(term_T *term)
7328{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007329 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007330 if (term->tl_winpty != NULL)
7331 winpty_free(term->tl_winpty);
7332 term->tl_winpty = NULL;
7333 if (term->tl_winpty_config != NULL)
7334 winpty_config_free(term->tl_winpty_config);
7335 term->tl_winpty_config = NULL;
7336 if (term->tl_vterm != NULL)
7337 vterm_free(term->tl_vterm);
7338 term->tl_vterm = NULL;
7339}
7340
7341/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007342 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007343 */
7344 static void
7345term_report_winsize(term_T *term, int rows, int cols)
7346{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007347 if (term->tl_conpty)
7348 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007349 if (term->tl_winpty)
7350 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7351}
7352
7353 int
7354terminal_enabled(void)
7355{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007356 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007357}
7358
7359# else
7360
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007361///////////////////////////////////////
7362// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007363
7364/*
7365 * Create a new terminal of "rows" by "cols" cells.
7366 * Start job for "cmd".
7367 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007368 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007369 * Return OK or FAIL.
7370 */
7371 static int
7372term_and_job_init(
7373 term_T *term,
7374 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007375 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007376 jobopt_T *opt,
7377 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007378{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007379 term->tl_arg0_cmd = NULL;
7380
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007381 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7382 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007383
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007384#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7385 if (opt->jo_set2 & JO2_ANSI_COLORS)
7386 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7387 else
7388 init_vterm_ansi_colors(term->tl_vterm);
7389#endif
7390
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007391 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007392 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007393 if (term->tl_job != NULL)
7394 ++term->tl_job->jv_refcount;
7395
7396 return term->tl_job != NULL
7397 && term->tl_job->jv_channel != NULL
7398 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7399}
7400
7401 static int
7402create_pty_only(term_T *term, jobopt_T *opt)
7403{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007404 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7405 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007406
7407 term->tl_job = job_alloc();
7408 if (term->tl_job == NULL)
7409 return FAIL;
7410 ++term->tl_job->jv_refcount;
7411
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007412 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007413 term->tl_job->jv_status = JOB_FINISHED;
7414
7415 return mch_create_pty_channel(term->tl_job, opt);
7416}
7417
7418/*
7419 * Free the terminal emulator part of "term".
7420 */
7421 static void
7422term_free_vterm(term_T *term)
7423{
7424 if (term->tl_vterm != NULL)
7425 vterm_free(term->tl_vterm);
7426 term->tl_vterm = NULL;
7427}
7428
7429/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007430 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007431 */
7432 static void
7433term_report_winsize(term_T *term, int rows, int cols)
7434{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007435 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007436 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7437 {
7438 int fd = -1;
7439 int part;
7440
7441 for (part = PART_OUT; part < PART_COUNT; ++part)
7442 {
7443 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007444 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007445 break;
7446 }
7447 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7448 mch_signal_job(term->tl_job, (char_u *)"winch");
7449 }
7450}
7451
7452# endif
7453
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007454#endif // FEAT_TERMINAL