blob: 6f07055ebc11efbad20fd7ad374556b9f76102d8 [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 Moolenaar2e6ab182017-09-20 10:03:07 +0200102 int tl_channel_closed;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200103 int tl_channel_recently_closed; // still need to handle tl_finish
104
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100105 int tl_finish;
106#define TL_FINISH_UNSET NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100107#define TL_FINISH_CLOSE 'c' // ++close or :terminal without argument
108#define TL_FINISH_NOCLOSE 'n' // ++noclose
109#define TL_FINISH_OPEN 'o' // ++open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200110 char_u *tl_opencmd;
111 char_u *tl_eof_chars;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200112 char_u *tl_api; // prefix for terminal API function
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200113
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100114 char_u *tl_arg0_cmd; // To format the status bar
115
Bram Moolenaar4f974752019-02-17 17:44:42 +0100116#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200117 void *tl_winpty_config;
118 void *tl_winpty;
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200119
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100120 HPCON tl_conpty;
121 DYN_STARTUPINFOEXW tl_siex; // Structure that always needs to be hold
122
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200123 FILE *tl_out_fd;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200124#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100125#if defined(FEAT_SESSION)
126 char_u *tl_command;
127#endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100128 char_u *tl_kill;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200129
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100130 // last known vterm size
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200131 int tl_rows;
132 int tl_cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200133
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100134 char_u *tl_title; // NULL or allocated
135 char_u *tl_status_text; // NULL or allocated
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200136
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100137 // Range of screen rows to update. Zero based.
138 int tl_dirty_row_start; // MAX_ROW if nothing dirty
139 int tl_dirty_row_end; // row below last one to update
140 int tl_dirty_snapshot; // text updated after making snapshot
Bram Moolenaar56bc8e22018-05-10 18:05:56 +0200141#ifdef FEAT_TIMERS
142 int tl_timer_set;
143 proftime_T tl_timer_due;
144#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100145 int tl_postponed_scroll; // to be scrolled up
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200146
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200147 garray_T tl_scrollback;
148 int tl_scrollback_scrolled;
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100149 garray_T tl_scrollback_postponed;
150
Bram Moolenaar83d47902020-03-26 20:34:00 +0100151 char_u *tl_highlight_name; // replaces "Terminal"; allocated
152
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200153 cellattr_T tl_default_color;
154
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100155 linenr_T tl_top_diff_rows; // rows of top diff file or zero
156 linenr_T tl_bot_diff_rows; // rows of bottom diff file
Bram Moolenaard96ff162018-02-18 22:13:29 +0100157
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200158 VTermPos tl_cursor_pos;
159 int tl_cursor_visible;
160 int tl_cursor_blink;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100161 int tl_cursor_shape; // 1: block, 2: underline, 3: bar
162 char_u *tl_cursor_color; // NULL or allocated
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200163
164 int tl_using_altscreen;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200165 garray_T tl_osc_buf; // incomplete OSC string
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200166};
167
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100168#define TMODE_ONCE 1 // CTRL-\ CTRL-N used
169#define TMODE_LOOP 2 // CTRL-W N used
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200170
171/*
172 * List of all active terminals.
173 */
174static term_T *first_term = NULL;
175
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100176// Terminal active in terminal_loop().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200177static term_T *in_terminal_loop = NULL;
178
Bram Moolenaar4f974752019-02-17 17:44:42 +0100179#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100180static BOOL has_winpty = FALSE;
181static BOOL has_conpty = FALSE;
182#endif
183
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100184#define MAX_ROW 999999 // used for tl_dirty_row_end to update all rows
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200185#define KEY_BUF_LEN 200
186
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200187#define FOR_ALL_TERMS(term) \
188 for ((term) = first_term; (term) != NULL; (term) = (term)->tl_next)
189
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200190/*
191 * Functions with separate implementation for MS-Windows and Unix-like systems.
192 */
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200193static 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 +0200194static int create_pty_only(term_T *term, jobopt_T *opt);
195static void term_report_winsize(term_T *term, int rows, int cols);
196static void term_free_vterm(term_T *term);
Bram Moolenaar13568252018-03-16 20:46:58 +0100197#ifdef FEAT_GUI
198static void update_system_term(term_T *term);
199#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200200
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100201static void handle_postponed_scrollback(term_T *term);
202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100203// The character that we know (or assume) that the terminal expects for the
204// backspace key.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200205static int term_backspace_char = BS;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200206
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100207// "Terminal" highlight group colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +0100208static int term_default_cterm_fg = -1;
209static int term_default_cterm_bg = -1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200210
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100211// Store the last set and the desired cursor properties, so that we only update
212// them when needed. Doing it unnecessary may result in flicker.
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200213static char_u *last_set_cursor_color = NULL;
214static char_u *desired_cursor_color = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +0100215static int last_set_cursor_shape = -1;
216static int desired_cursor_shape = -1;
217static int last_set_cursor_blink = -1;
218static int desired_cursor_blink = -1;
219
220
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100221///////////////////////////////////////
222// 1. Generic code for all systems.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200223
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200224 static int
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200225cursor_color_equal(char_u *lhs_color, char_u *rhs_color)
226{
227 if (lhs_color != NULL && rhs_color != NULL)
228 return STRCMP(lhs_color, rhs_color) == 0;
229 return lhs_color == NULL && rhs_color == NULL;
230}
231
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200232 static void
233cursor_color_copy(char_u **to_color, char_u *from_color)
234{
235 // Avoid a free & alloc if the value is already right.
236 if (cursor_color_equal(*to_color, from_color))
237 return;
238 vim_free(*to_color);
239 *to_color = (from_color == NULL) ? NULL : vim_strsave(from_color);
240}
241
242 static char_u *
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200243cursor_color_get(char_u *color)
244{
245 return (color == NULL) ? (char_u *)"" : color;
246}
247
248
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200249/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200250 * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
Bram Moolenaar498c2562018-04-15 23:45:15 +0200251 * current window.
252 * Sets "rows" and/or "cols" to zero when it should follow the window size.
253 * Return TRUE if the size is the minimum size: "24*80".
254 */
255 static int
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200256parse_termwinsize(win_T *wp, int *rows, int *cols)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200257{
258 int minsize = FALSE;
259
260 *rows = 0;
261 *cols = 0;
262
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200263 if (*wp->w_p_tws != NUL)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200264 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200265 char_u *p = vim_strchr(wp->w_p_tws, 'x');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200266
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100267 // Syntax of value was already checked when it's set.
Bram Moolenaar498c2562018-04-15 23:45:15 +0200268 if (p == NULL)
269 {
270 minsize = TRUE;
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200271 p = vim_strchr(wp->w_p_tws, '*');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200272 }
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200273 *rows = atoi((char *)wp->w_p_tws);
Bram Moolenaar498c2562018-04-15 23:45:15 +0200274 *cols = atoi((char *)p + 1);
275 }
276 return minsize;
277}
278
279/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200280 * Determine the terminal size from 'termwinsize' and the current window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200281 */
282 static void
Bram Moolenaarb936b792020-09-04 18:34:09 +0200283set_term_and_win_size(term_T *term, jobopt_T *opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200284{
Bram Moolenaarb936b792020-09-04 18:34:09 +0200285 int rows, cols;
286 int minsize;
287
Bram Moolenaar13568252018-03-16 20:46:58 +0100288#ifdef FEAT_GUI
289 if (term->tl_system)
290 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100291 // Use the whole screen for the system command. However, it will start
292 // at the command line and scroll up as needed, using tl_toprow.
Bram Moolenaar13568252018-03-16 20:46:58 +0100293 term->tl_rows = Rows;
294 term->tl_cols = Columns;
Bram Moolenaar07b46af2018-04-10 14:56:18 +0200295 return;
Bram Moolenaar13568252018-03-16 20:46:58 +0100296 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100297#endif
Bram Moolenaarb936b792020-09-04 18:34:09 +0200298 term->tl_rows = curwin->w_height;
299 term->tl_cols = curwin->w_width;
300
301 minsize = parse_termwinsize(curwin, &rows, &cols);
302 if (minsize)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200303 {
Bram Moolenaarb936b792020-09-04 18:34:09 +0200304 if (term->tl_rows < rows)
305 term->tl_rows = rows;
306 if (term->tl_cols < cols)
307 term->tl_cols = cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200308 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200309 if ((opt->jo_set2 & JO2_TERM_ROWS))
310 term->tl_rows = opt->jo_term_rows;
311 else if (rows != 0)
312 term->tl_rows = rows;
313 if ((opt->jo_set2 & JO2_TERM_COLS))
314 term->tl_cols = opt->jo_term_cols;
315 else if (cols != 0)
316 term->tl_cols = cols;
317
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200318 if (!opt->jo_hidden)
Bram Moolenaarb936b792020-09-04 18:34:09 +0200319 {
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200320 if (term->tl_rows != curwin->w_height)
321 win_setheight_win(term->tl_rows, curwin);
322 if (term->tl_cols != curwin->w_width)
323 win_setwidth_win(term->tl_cols, curwin);
Bram Moolenaarb936b792020-09-04 18:34:09 +0200324
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200325 // Set 'winsize' now to avoid a resize at the next redraw.
326 if (!minsize && *curwin->w_p_tws != NUL)
327 {
328 char_u buf[100];
329
330 vim_snprintf((char *)buf, 100, "%dx%d",
331 term->tl_rows, term->tl_cols);
332 set_option_value((char_u *)"termwinsize", 0L, buf, OPT_LOCAL);
333 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200334 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200335}
336
337/*
338 * Initialize job options for a terminal job.
339 * Caller may overrule some of them.
340 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100341 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200342init_job_options(jobopt_T *opt)
343{
344 clear_job_options(opt);
345
346 opt->jo_mode = MODE_RAW;
347 opt->jo_out_mode = MODE_RAW;
348 opt->jo_err_mode = MODE_RAW;
349 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
350}
351
352/*
353 * Set job options mandatory for a terminal job.
354 */
355 static void
356setup_job_options(jobopt_T *opt, int rows, int cols)
357{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100358#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100359 // Win32: Redirecting the job output won't work, thus always connect stdout
360 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200361 if (!(opt->jo_set & JO_OUT_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200362#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200363 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100364 // Connect stdout to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200365 opt->jo_io[PART_OUT] = JIO_BUFFER;
366 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
367 opt->jo_modifiable[PART_OUT] = 0;
368 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
369 }
370
Bram Moolenaar4f974752019-02-17 17:44:42 +0100371#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100372 // Win32: Redirecting the job output won't work, thus always connect stderr
373 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200374 if (!(opt->jo_set & JO_ERR_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200375#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200376 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100377 // Connect stderr to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200378 opt->jo_io[PART_ERR] = JIO_BUFFER;
379 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
380 opt->jo_modifiable[PART_ERR] = 0;
381 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
382 }
383
384 opt->jo_pty = TRUE;
385 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
386 opt->jo_term_rows = rows;
387 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
388 opt->jo_term_cols = cols;
389}
390
391/*
Bram Moolenaar5c381eb2019-06-25 06:50:31 +0200392 * Flush messages on channels.
393 */
394 static void
395term_flush_messages()
396{
397 mch_check_messages();
398 parse_queued_messages();
399}
400
401/*
Bram Moolenaard96ff162018-02-18 22:13:29 +0100402 * Close a terminal buffer (and its window). Used when creating the terminal
403 * fails.
404 */
405 static void
406term_close_buffer(buf_T *buf, buf_T *old_curbuf)
407{
408 free_terminal(buf);
409 if (old_curbuf != NULL)
410 {
411 --curbuf->b_nwindows;
412 curbuf = old_curbuf;
413 curwin->w_buffer = curbuf;
414 ++curbuf->b_nwindows;
415 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100416 CHECK_CURBUF;
Bram Moolenaard96ff162018-02-18 22:13:29 +0100417
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100418 // Wiping out the buffer will also close the window and call
419 // free_terminal().
Bram Moolenaard96ff162018-02-18 22:13:29 +0100420 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
421}
422
423/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200424 * Start a terminal window and return its buffer.
Bram Moolenaar13568252018-03-16 20:46:58 +0100425 * Use either "argvar" or "argv", the other must be NULL.
426 * When "flags" has TERM_START_NOJOB only create the buffer, b_term and open
427 * the window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200428 * Returns NULL when failed.
429 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100430 buf_T *
431term_start(
432 typval_T *argvar,
433 char **argv,
434 jobopt_T *opt,
435 int flags)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200436{
437 exarg_T split_ea;
438 win_T *old_curwin = curwin;
439 term_T *term;
440 buf_T *old_curbuf = NULL;
441 int res;
442 buf_T *newbuf;
Bram Moolenaare1004402020-10-24 20:49:43 +0200443 int vertical = opt->jo_vertical || (cmdmod.cmod_split & WSP_VERT);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200444 jobopt_T orig_opt; // only partly filled
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200445
446 if (check_restricted() || check_secure())
447 return NULL;
448
449 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
450 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
451 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
Bram Moolenaarb0992022020-01-30 14:55:42 +0100452 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))
453 || (argvar != NULL
454 && argvar->v_type == VAR_LIST
455 && argvar->vval.v_list != NULL
456 && argvar->vval.v_list->lv_first == &range_list_item))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200457 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100458 emsg(_(e_invarg));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200459 return NULL;
460 }
461
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200462 term = ALLOC_CLEAR_ONE(term_T);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200463 if (term == NULL)
464 return NULL;
465 term->tl_dirty_row_end = MAX_ROW;
466 term->tl_cursor_visible = TRUE;
467 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
468 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100469#ifdef FEAT_GUI
470 term->tl_system = (flags & TERM_START_SYSTEM);
471#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200472 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100473 ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200474 ga_init2(&term->tl_osc_buf, sizeof(char), 300);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200475
Bram Moolenaara80faa82020-04-12 19:37:17 +0200476 CLEAR_FIELD(split_ea);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200477 if (opt->jo_curwin)
478 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100479 // Create a new buffer in the current window.
Bram Moolenaar13568252018-03-16 20:46:58 +0100480 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200481 {
482 no_write_message();
483 vim_free(term);
484 return NULL;
485 }
486 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaarb1009092020-05-31 16:04:42 +0200487 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
488 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
489 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200490 {
491 vim_free(term);
492 return NULL;
493 }
494 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100495 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200496 {
497 buf_T *buf;
498
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100499 // Create a new buffer without a window. Make it the current buffer for
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100500 // a moment to be able to do the initializations.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200501 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
502 BLN_NEW | BLN_LISTED);
503 if (buf == NULL || ml_open(buf) == FAIL)
504 {
505 vim_free(term);
506 return NULL;
507 }
508 old_curbuf = curbuf;
509 --curbuf->b_nwindows;
510 curbuf = buf;
511 curwin->w_buffer = buf;
512 ++curbuf->b_nwindows;
513 }
514 else
515 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100516 // Open a new window or tab.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200517 split_ea.cmdidx = CMD_new;
518 split_ea.cmd = (char_u *)"new";
519 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100520 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200521 {
522 split_ea.line2 = opt->jo_term_rows;
523 split_ea.addr_count = 1;
524 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100525 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200526 {
527 split_ea.line2 = opt->jo_term_cols;
528 split_ea.addr_count = 1;
529 }
530
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100531 if (vertical)
Bram Moolenaare1004402020-10-24 20:49:43 +0200532 cmdmod.cmod_split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200533 ex_splitview(&split_ea);
534 if (curwin == old_curwin)
535 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100536 // split failed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200537 vim_free(term);
538 return NULL;
539 }
540 }
541 term->tl_buffer = curbuf;
542 curbuf->b_term = term;
543
544 if (!opt->jo_hidden)
545 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100546 // Only one size was taken care of with :new, do the other one. With
547 // "curwin" both need to be done.
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100548 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200549 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100550 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200551 win_setwidth(opt->jo_term_cols);
552 }
553
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100554 // Link the new terminal in the list of active terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200555 term->tl_next = first_term;
556 first_term = term;
557
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100558 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
559
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200560 if (opt->jo_term_name != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100561 {
562 vim_free(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200563 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100564 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100565 else if (argv != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100566 {
567 vim_free(curbuf->b_ffname);
Bram Moolenaar13568252018-03-16 20:46:58 +0100568 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100569 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200570 else
571 {
572 int i;
573 size_t len;
574 char_u *cmd, *p;
575
576 if (argvar->v_type == VAR_STRING)
577 {
578 cmd = argvar->vval.v_string;
579 if (cmd == NULL)
580 cmd = (char_u *)"";
581 else if (STRCMP(cmd, "NONE") == 0)
582 cmd = (char_u *)"pty";
583 }
584 else if (argvar->v_type != VAR_LIST
585 || argvar->vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +0100586 || argvar->vval.v_list->lv_len == 0
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100587 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200588 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
589 cmd = (char_u*)"";
590
591 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200592 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200593
594 for (i = 0; p != NULL; ++i)
595 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100596 // Prepend a ! to the command name to avoid the buffer name equals
597 // the executable, otherwise ":w!" would overwrite it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200598 if (i == 0)
599 vim_snprintf((char *)p, len, "!%s", cmd);
600 else
601 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
602 if (buflist_findname(p) == NULL)
603 {
604 vim_free(curbuf->b_ffname);
605 curbuf->b_ffname = p;
606 break;
607 }
608 }
609 }
Bram Moolenaare010c722020-02-24 21:37:54 +0100610 vim_free(curbuf->b_sfname);
611 curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200612 curbuf->b_fname = curbuf->b_ffname;
613
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100614 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
615
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200616 if (opt->jo_term_opencmd != NULL)
617 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
618
619 if (opt->jo_eof_chars != NULL)
620 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
621
622 set_string_option_direct((char_u *)"buftype", -1,
623 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200624 // Avoid that 'buftype' is reset when this buffer is entered.
625 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200626
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100627 // Mark the buffer as not modifiable. It can only be made modifiable after
628 // the job finished.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200629 curbuf->b_p_ma = FALSE;
630
Bram Moolenaarb936b792020-09-04 18:34:09 +0200631 set_term_and_win_size(term, opt);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100632#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200633 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
634#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200635 setup_job_options(opt, term->tl_rows, term->tl_cols);
636
Bram Moolenaar13568252018-03-16 20:46:58 +0100637 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100638 return curbuf;
639
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100640#if defined(FEAT_SESSION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100641 // Remember the command for the session file.
Bram Moolenaar13568252018-03-16 20:46:58 +0100642 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100643 term->tl_command = vim_strsave((char_u *)"NONE");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100644 else if (argvar->v_type == VAR_STRING)
645 {
646 char_u *cmd = argvar->vval.v_string;
647
648 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
649 term->tl_command = vim_strsave(cmd);
650 }
651 else if (argvar->v_type == VAR_LIST
652 && argvar->vval.v_list != NULL
653 && argvar->vval.v_list->lv_len > 0)
654 {
655 garray_T ga;
656 listitem_T *item;
657
658 ga_init2(&ga, 1, 100);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200659 FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100660 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100661 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100662 char_u *p;
663
664 if (s == NULL)
665 break;
666 p = vim_strsave_fnameescape(s, FALSE);
667 if (p == NULL)
668 break;
669 ga_concat(&ga, p);
670 vim_free(p);
671 ga_append(&ga, ' ');
672 }
673 if (item == NULL)
674 {
675 ga_append(&ga, NUL);
676 term->tl_command = ga.ga_data;
677 }
678 else
679 ga_clear(&ga);
680 }
681#endif
682
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100683 if (opt->jo_term_kill != NULL)
684 {
685 char_u *p = skiptowhite(opt->jo_term_kill);
686
687 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
688 }
689
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200690 if (opt->jo_term_api != NULL)
Bram Moolenaar21109272020-01-30 16:27:20 +0100691 {
692 char_u *p = skiptowhite(opt->jo_term_api);
693
694 term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
695 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200696 else
697 term->tl_api = vim_strsave((char_u *)"Tapi_");
698
Bram Moolenaar83d47902020-03-26 20:34:00 +0100699 if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
700 term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
701
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100702 // System dependent: setup the vterm and maybe start the job in it.
Bram Moolenaar13568252018-03-16 20:46:58 +0100703 if (argv == NULL
704 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200705 && argvar->vval.v_string != NULL
706 && STRCMP(argvar->vval.v_string, "NONE") == 0)
707 res = create_pty_only(term, opt);
708 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200709 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200710
711 newbuf = curbuf;
712 if (res == OK)
713 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100714 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200715 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
716 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100717#ifdef FEAT_GUI
718 if (term->tl_system)
719 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100720 // display first line below typed command
Bram Moolenaar13568252018-03-16 20:46:58 +0100721 term->tl_toprow = msg_row + 1;
722 term->tl_dirty_row_end = 0;
723 }
724#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200725
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100726 // Make sure we don't get stuck on sending keys to the job, it leads to
727 // a deadlock if the job is waiting for Vim to read.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200728 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
729
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200730 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200731 {
732 --curbuf->b_nwindows;
733 curbuf = old_curbuf;
734 curwin->w_buffer = curbuf;
735 ++curbuf->b_nwindows;
736 }
737 }
738 else
739 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100740 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200741 return NULL;
742 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100743
Bram Moolenaar13568252018-03-16 20:46:58 +0100744 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar28ed4df2019-10-26 16:21:40 +0200745 if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
746 apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200747 return newbuf;
748}
749
750/*
751 * ":terminal": open a terminal window and execute a job in it.
752 */
753 void
754ex_terminal(exarg_T *eap)
755{
756 typval_T argvar[2];
757 jobopt_T opt;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100758 int opt_shell = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200759 char_u *cmd;
760 char_u *tofree = NULL;
761
762 init_job_options(&opt);
763
764 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100765 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200766 {
767 char_u *p, *ep;
768
769 cmd += 2;
770 p = skiptowhite(cmd);
771 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200772 if (ep != NULL)
773 {
774 if (ep < p)
775 p = ep;
776 else
777 ep = NULL;
778 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200779
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200780# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
781 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
782 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200783 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200784 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100785 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200786 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200787 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200788 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200789 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200790 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200791 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200792 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100793 opt.jo_term_norestore = 1;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100794 else if (OPTARG_HAS("shell"))
795 opt_shell = TRUE;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200796 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100797 {
798 opt.jo_set2 |= JO2_TERM_KILL;
799 opt.jo_term_kill = ep + 1;
800 p = skiptowhite(cmd);
801 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200802 else if (OPTARG_HAS("api"))
803 {
804 opt.jo_set2 |= JO2_TERM_API;
805 if (ep != NULL)
806 {
807 opt.jo_term_api = ep + 1;
808 p = skiptowhite(cmd);
809 }
810 else
811 opt.jo_term_api = NULL;
812 }
813 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200814 {
815 opt.jo_set2 |= JO2_TERM_ROWS;
816 opt.jo_term_rows = atoi((char *)ep + 1);
817 p = skiptowhite(cmd);
818 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200819 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200820 {
821 opt.jo_set2 |= JO2_TERM_COLS;
822 opt.jo_term_cols = atoi((char *)ep + 1);
823 p = skiptowhite(cmd);
824 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200825 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200826 {
827 char_u *buf = NULL;
828 char_u *keys;
829
Bram Moolenaar21109272020-01-30 16:27:20 +0100830 vim_free(opt.jo_eof_chars);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200831 p = skiptowhite(cmd);
832 *p = NUL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200833 keys = replace_termcodes(ep + 1, &buf,
834 REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200835 opt.jo_set2 |= JO2_EOF_CHARS;
836 opt.jo_eof_chars = vim_strsave(keys);
837 vim_free(buf);
838 *p = ' ';
839 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100840#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100841 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
842 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100843 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100844 int tty_type = NUL;
845
846 p = skiptowhite(cmd);
847 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
848 tty_type = 'w';
849 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
850 tty_type = 'c';
851 else
852 {
853 semsg(e_invargval, "type");
854 goto theend;
855 }
856 opt.jo_set2 |= JO2_TTY_TYPE;
857 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100858 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100859#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200860 else
861 {
862 if (*p)
863 *p = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100864 semsg(_("E181: Invalid attribute: %s"), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100865 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200866 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200867# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200868 cmd = skipwhite(p);
869 }
870 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100871 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100872 // Make a copy of 'shell', an autocommand may change the option.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200873 tofree = cmd = vim_strsave(p_sh);
874
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100875 // default to close when the shell exits
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100876 if (opt.jo_term_finish == NUL)
Bram Moolenaare2978022020-04-26 14:47:44 +0200877 opt.jo_term_finish = TL_FINISH_CLOSE;
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100878 }
879
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200880 if (eap->addr_count > 0)
881 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100882 // Write lines from current buffer to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200883 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
884 opt.jo_io[PART_IN] = JIO_BUFFER;
885 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
886 opt.jo_in_top = eap->line1;
887 opt.jo_in_bot = eap->line2;
888 }
889
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100890 if (opt_shell && tofree == NULL)
891 {
892#ifdef UNIX
893 char **argv = NULL;
894 char_u *tofree1 = NULL;
895 char_u *tofree2 = NULL;
896
897 // :term ++shell command
898 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
899 term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaaradf4aa22019-11-10 22:36:44 +0100900 vim_free(argv);
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100901 vim_free(tofree1);
902 vim_free(tofree2);
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100903 goto theend;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100904#else
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100905# ifdef MSWIN
906 long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
907 char_u *newcmd;
908
909 newcmd = alloc(cmdlen);
910 if (newcmd == NULL)
911 goto theend;
912 tofree = newcmd;
913 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
914 cmd = newcmd;
915# else
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100916 emsg(_("E279: Sorry, ++shell is not supported on this system"));
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100917 goto theend;
918# endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100919#endif
920 }
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100921 argvar[0].v_type = VAR_STRING;
922 argvar[0].vval.v_string = cmd;
923 argvar[1].v_type = VAR_UNKNOWN;
924 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100925
926theend:
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100927 vim_free(tofree);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200928 vim_free(opt.jo_eof_chars);
929}
930
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100931#if defined(FEAT_SESSION) || defined(PROTO)
932/*
933 * Write a :terminal command to the session file to restore the terminal in
934 * window "wp".
935 * Return FAIL if writing fails.
936 */
937 int
Bram Moolenaar0e655112020-09-11 20:36:36 +0200938term_write_session(FILE *fd, win_T *wp, hashtab_T *terminal_bufs)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100939{
Bram Moolenaar0e655112020-09-11 20:36:36 +0200940 const int bufnr = wp->w_buffer->b_fnum;
941 term_T *term = wp->w_buffer->b_term;
942
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200943 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200944 {
945 // There are multiple views into this terminal buffer. We don't want to
946 // create the terminal multiple times. If it's the first time, create,
947 // otherwise link to the first buffer.
948 char id_as_str[NUMBUFLEN];
949 hashitem_T *entry;
950
951 vim_snprintf(id_as_str, sizeof(id_as_str), "%d", bufnr);
952
953 entry = hash_find(terminal_bufs, (char_u *)id_as_str);
954 if (!HASHITEM_EMPTY(entry))
955 {
956 // we've already opened this terminal buffer
957 if (fprintf(fd, "execute 'buffer ' . s:term_buf_%d", bufnr) < 0)
958 return FAIL;
959 return put_eol(fd);
960 }
961 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100962
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100963 // Create the terminal and run the command. This is not without
964 // risk, but let's assume the user only creates a session when this
965 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100966 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
967 term->tl_cols, term->tl_rows) < 0)
968 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100969#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100970 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
971 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100972#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100973 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
974 return FAIL;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200975 if (put_eol(fd) != OK)
976 return FAIL;
977
978 if (fprintf(fd, "let s:term_buf_%d = bufnr()", bufnr) < 0)
979 return FAIL;
980
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200981 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200982 {
983 char *hash_key = alloc(NUMBUFLEN);
984
985 vim_snprintf(hash_key, NUMBUFLEN, "%d", bufnr);
986 hash_add(terminal_bufs, (char_u *)hash_key);
987 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100988
989 return put_eol(fd);
990}
991
992/*
993 * Return TRUE if "buf" has a terminal that should be restored.
994 */
995 int
996term_should_restore(buf_T *buf)
997{
998 term_T *term = buf->b_term;
999
1000 return term != NULL && (term->tl_command == NULL
1001 || STRCMP(term->tl_command, "NONE") != 0);
1002}
1003#endif
1004
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001005/*
1006 * Free the scrollback buffer for "term".
1007 */
1008 static void
1009free_scrollback(term_T *term)
1010{
1011 int i;
1012
1013 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
1014 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
1015 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001016 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
1017 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
1018 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001019}
1020
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001021
1022// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +02001023static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001024
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001025/*
1026 * Free a terminal and everything it refers to.
1027 * Kills the job if there is one.
1028 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001029 * The actual terminal structure is freed later in free_unused_terminals(),
1030 * because callbacks may wipe out a buffer while the terminal is still
1031 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001032 */
1033 void
1034free_terminal(buf_T *buf)
1035{
1036 term_T *term = buf->b_term;
1037 term_T *tp;
1038
1039 if (term == NULL)
1040 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001041
1042 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001043 if (first_term == term)
1044 first_term = term->tl_next;
1045 else
1046 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
1047 if (tp->tl_next == term)
1048 {
1049 tp->tl_next = term->tl_next;
1050 break;
1051 }
1052
1053 if (term->tl_job != NULL)
1054 {
1055 if (term->tl_job->jv_status != JOB_ENDED
1056 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +01001057 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001058 job_stop(term->tl_job, NULL, "kill");
1059 job_unref(term->tl_job);
1060 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001061 term->tl_next = terminals_to_free;
1062 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001063
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001064 buf->b_term = NULL;
1065 if (in_terminal_loop == term)
1066 in_terminal_loop = NULL;
1067}
1068
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001069 void
1070free_unused_terminals()
1071{
1072 while (terminals_to_free != NULL)
1073 {
1074 term_T *term = terminals_to_free;
1075
1076 terminals_to_free = term->tl_next;
1077
1078 free_scrollback(term);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02001079 ga_clear(&term->tl_osc_buf);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001080
1081 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001082 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001083 vim_free(term->tl_title);
1084#ifdef FEAT_SESSION
1085 vim_free(term->tl_command);
1086#endif
1087 vim_free(term->tl_kill);
1088 vim_free(term->tl_status_text);
1089 vim_free(term->tl_opencmd);
1090 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001091 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001092#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001093 if (term->tl_out_fd != NULL)
1094 fclose(term->tl_out_fd);
1095#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001096 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001097 vim_free(term->tl_cursor_color);
1098 vim_free(term);
1099 }
1100}
1101
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001102/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001103 * Get the part that is connected to the tty. Normally this is PART_IN, but
1104 * when writing buffer lines to the job it can be another. This makes it
1105 * possible to do "1,5term vim -".
1106 */
1107 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001108get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001109{
1110#ifdef UNIX
1111 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1112 int i;
1113
1114 for (i = 0; i < 3; ++i)
1115 {
1116 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1117
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001118 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001119 return parts[i];
1120 }
1121#endif
1122 return PART_IN;
1123}
1124
1125/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001126 * Write job output "msg[len]" to the vterm.
1127 */
1128 static void
1129term_write_job_output(term_T *term, char_u *msg, size_t len)
1130{
1131 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001132 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001133
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001134 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001135
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001136 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001137 if (prevlen != vterm_output_get_buffer_current(vterm))
1138 {
1139 char buf[KEY_BUF_LEN];
1140 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1141
1142 if (curlen > 0)
1143 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1144 (char_u *)buf, (int)curlen, NULL);
1145 }
1146
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001147 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001148 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1149}
1150
1151 static void
1152update_cursor(term_T *term, int redraw)
1153{
1154 if (term->tl_normal_mode)
1155 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001156#ifdef FEAT_GUI
1157 if (term->tl_system)
1158 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1159 term->tl_cursor_pos.col);
1160 else
1161#endif
1162 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001163 if (redraw)
1164 {
1165 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1166 cursor_on();
1167 out_flush();
1168#ifdef FEAT_GUI
1169 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001170 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001171 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001172 gui_mch_flush();
1173 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001174#endif
1175 }
1176}
1177
1178/*
1179 * Invoked when "msg" output from a job was received. Write it to the terminal
1180 * of "buffer".
1181 */
1182 void
1183write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1184{
1185 size_t len = STRLEN(msg);
1186 term_T *term = buffer->b_term;
1187
Bram Moolenaar4f974752019-02-17 17:44:42 +01001188#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001189 // Win32: Cannot redirect output of the job, intercept it here and write to
1190 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001191 if (term->tl_out_fd != NULL)
1192 {
1193 ch_log(channel, "Writing %d bytes to output file", (int)len);
1194 fwrite(msg, len, 1, term->tl_out_fd);
1195 return;
1196 }
1197#endif
1198
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001199 if (term->tl_vterm == NULL)
1200 {
1201 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1202 return;
1203 }
1204 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01001205 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001206 term_write_job_output(term, msg, len);
1207
Bram Moolenaar13568252018-03-16 20:46:58 +01001208#ifdef FEAT_GUI
1209 if (term->tl_system)
1210 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001211 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001212 update_system_term(term);
1213 update_cursor(term, TRUE);
1214 }
1215 else
1216#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001217 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1218 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001219 if (!term->tl_normal_mode)
1220 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001221 // Don't use update_screen() when editing the command line, it gets
1222 // cleared.
1223 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001224 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001225 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001226 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001227 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001228 // update_screen() can be slow, check the terminal wasn't closed
1229 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001230 if (buffer == curbuf && curbuf->b_term != NULL)
1231 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001232 }
1233 else
1234 redraw_after_callback(TRUE);
1235 }
1236}
1237
1238/*
1239 * Send a mouse position and click to the vterm
1240 */
1241 static int
1242term_send_mouse(VTerm *vterm, int button, int pressed)
1243{
1244 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001245 int row = mouse_row - W_WINROW(curwin);
1246 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001247
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001248#ifdef FEAT_PROP_POPUP
1249 if (popup_is_popup(curwin))
1250 {
1251 row -= popup_top_extra(curwin);
1252 col -= popup_left_extra(curwin);
1253 }
1254#endif
1255 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001256 if (button != 0)
1257 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001258 return TRUE;
1259}
1260
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001261static int enter_mouse_col = -1;
1262static int enter_mouse_row = -1;
1263
1264/*
1265 * Handle a mouse click, drag or release.
1266 * Return TRUE when a mouse event is sent to the terminal.
1267 */
1268 static int
1269term_mouse_click(VTerm *vterm, int key)
1270{
1271#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001272 // For modeless selection mouse drag and release events are ignored, unless
1273 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001274 static int ignore_drag_release = TRUE;
1275 VTermMouseState mouse_state;
1276
1277 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1278 if (mouse_state.flags == 0)
1279 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001280 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001281 switch (key)
1282 {
1283 case K_LEFTDRAG:
1284 case K_LEFTRELEASE:
1285 case K_RIGHTDRAG:
1286 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001287 // Ignore drag and release events when the button-down wasn't
1288 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001289 if (ignore_drag_release)
1290 {
1291 int save_mouse_col, save_mouse_row;
1292
1293 if (enter_mouse_col < 0)
1294 break;
1295
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001296 // mouse click in the window gave us focus, handle that
1297 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001298 save_mouse_col = mouse_col;
1299 save_mouse_row = mouse_row;
1300 mouse_col = enter_mouse_col;
1301 mouse_row = enter_mouse_row;
1302 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1303 mouse_col = save_mouse_col;
1304 mouse_row = save_mouse_row;
1305 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001306 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001307 case K_LEFTMOUSE:
1308 case K_RIGHTMOUSE:
1309 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1310 ignore_drag_release = TRUE;
1311 else
1312 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001313 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001314 if (clip_star.available)
1315 {
1316 int button, is_click, is_drag;
1317
1318 button = get_mouse_button(KEY2TERMCAP1(key),
1319 &is_click, &is_drag);
1320 if (mouse_model_popup() && button == MOUSE_LEFT
1321 && (mod_mask & MOD_MASK_SHIFT))
1322 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001323 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001324 button = MOUSE_RIGHT;
1325 mod_mask &= ~MOD_MASK_SHIFT;
1326 }
1327 clip_modeless(button, is_click, is_drag);
1328 }
1329 break;
1330
1331 case K_MIDDLEMOUSE:
1332 if (clip_star.available)
1333 insert_reg('*', TRUE);
1334 break;
1335 }
1336 enter_mouse_col = -1;
1337 return FALSE;
1338 }
1339#endif
1340 enter_mouse_col = -1;
1341
1342 switch (key)
1343 {
1344 case K_LEFTMOUSE:
1345 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1346 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1347 case K_LEFTRELEASE:
1348 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1349 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1350 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1351 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1352 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1353 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1354 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1355 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1356 }
1357 return TRUE;
1358}
1359
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001360/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001361 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1362 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001363 * Return the number of bytes in "buf".
1364 */
1365 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001366term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001367{
1368 VTerm *vterm = term->tl_vterm;
1369 VTermKey key = VTERM_KEY_NONE;
1370 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001371 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001372
1373 switch (c)
1374 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001375 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001376
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001377 // don't use VTERM_KEY_BACKSPACE, it always
1378 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001379 case K_BS: c = term_backspace_char; break;
1380
1381 case ESC: key = VTERM_KEY_ESCAPE; break;
1382 case K_DEL: key = VTERM_KEY_DEL; break;
1383 case K_DOWN: key = VTERM_KEY_DOWN; break;
1384 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1385 key = VTERM_KEY_DOWN; break;
1386 case K_END: key = VTERM_KEY_END; break;
1387 case K_S_END: mod = VTERM_MOD_SHIFT;
1388 key = VTERM_KEY_END; break;
1389 case K_C_END: mod = VTERM_MOD_CTRL;
1390 key = VTERM_KEY_END; break;
1391 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1392 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1393 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1394 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1395 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1396 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1397 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1398 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1399 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1400 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1401 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1402 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1403 case K_HOME: key = VTERM_KEY_HOME; break;
1404 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1405 key = VTERM_KEY_HOME; break;
1406 case K_C_HOME: mod = VTERM_MOD_CTRL;
1407 key = VTERM_KEY_HOME; break;
1408 case K_INS: key = VTERM_KEY_INS; break;
1409 case K_K0: key = VTERM_KEY_KP_0; break;
1410 case K_K1: key = VTERM_KEY_KP_1; break;
1411 case K_K2: key = VTERM_KEY_KP_2; break;
1412 case K_K3: key = VTERM_KEY_KP_3; break;
1413 case K_K4: key = VTERM_KEY_KP_4; break;
1414 case K_K5: key = VTERM_KEY_KP_5; break;
1415 case K_K6: key = VTERM_KEY_KP_6; break;
1416 case K_K7: key = VTERM_KEY_KP_7; break;
1417 case K_K8: key = VTERM_KEY_KP_8; break;
1418 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001419 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001420 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001421 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001422 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001423 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1424 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001425 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1426 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001427 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1428 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001429 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1430 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1431 case K_LEFT: key = VTERM_KEY_LEFT; break;
1432 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1433 key = VTERM_KEY_LEFT; break;
1434 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1435 key = VTERM_KEY_LEFT; break;
1436 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1437 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1438 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1439 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1440 key = VTERM_KEY_RIGHT; break;
1441 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1442 key = VTERM_KEY_RIGHT; break;
1443 case K_UP: key = VTERM_KEY_UP; break;
1444 case K_S_UP: mod = VTERM_MOD_SHIFT;
1445 key = VTERM_KEY_UP; break;
1446 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001447 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1448 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001449
Bram Moolenaara42ad572017-11-16 13:08:04 +01001450 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1451 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001452 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1453 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001454
1455 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001456 case K_LEFTMOUSE_NM:
1457 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001458 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001459 case K_LEFTRELEASE_NM:
1460 case K_MOUSEMOVE:
1461 case K_MIDDLEMOUSE:
1462 case K_MIDDLEDRAG:
1463 case K_MIDDLERELEASE:
1464 case K_RIGHTMOUSE:
1465 case K_RIGHTDRAG:
1466 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1467 return 0;
1468 other = TRUE;
1469 break;
1470
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001471 case K_X1MOUSE: /* TODO */ return 0;
1472 case K_X1DRAG: /* TODO */ return 0;
1473 case K_X1RELEASE: /* TODO */ return 0;
1474 case K_X2MOUSE: /* TODO */ return 0;
1475 case K_X2DRAG: /* TODO */ return 0;
1476 case K_X2RELEASE: /* TODO */ return 0;
1477
1478 case K_IGNORE: return 0;
1479 case K_NOP: return 0;
1480 case K_UNDO: return 0;
1481 case K_HELP: return 0;
1482 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1483 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1484 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1485 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1486 case K_SELECT: return 0;
1487#ifdef FEAT_GUI
1488 case K_VER_SCROLLBAR: return 0;
1489 case K_HOR_SCROLLBAR: return 0;
1490#endif
1491#ifdef FEAT_GUI_TABLINE
1492 case K_TABLINE: return 0;
1493 case K_TABMENU: return 0;
1494#endif
1495#ifdef FEAT_NETBEANS_INTG
1496 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1497#endif
1498#ifdef FEAT_DND
1499 case K_DROP: return 0;
1500#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001501 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001502 case K_PS: vterm_keyboard_start_paste(vterm);
1503 other = TRUE;
1504 break;
1505 case K_PE: vterm_keyboard_end_paste(vterm);
1506 other = TRUE;
1507 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001508 }
1509
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001510 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001511 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001512 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001513 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001514 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001515 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001516 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001517
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001518 /*
1519 * Convert special keys to vterm keys:
1520 * - Write keys to vterm: vterm_keyboard_key()
1521 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001522 */
1523 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001524 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001525 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001526 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001527 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001528 vterm_keyboard_unichar(vterm, c, mod);
1529
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001530 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001531 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1532}
1533
1534/*
1535 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001536 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001537 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001538 */
1539 static int
1540term_job_running_check(term_T *term, int check_job_status)
1541{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001542 // Also consider the job finished when the channel is closed, to avoid a
1543 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001544 if (term != NULL
1545 && term->tl_job != NULL
1546 && channel_is_open(term->tl_job->jv_channel))
1547 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001548 job_T *job = term->tl_job;
1549
1550 // Careful: Checking the job status may invoked callbacks, which close
1551 // the buffer and terminate "term". However, "job" will not be freed
1552 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001553 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001554 job_status(job);
1555 return (job->jv_status == JOB_STARTED
1556 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001557 }
1558 return FALSE;
1559}
1560
1561/*
1562 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001563 */
1564 int
1565term_job_running(term_T *term)
1566{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001567 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001568}
1569
1570/*
1571 * Return TRUE if "term" has an active channel and used ":term NONE".
1572 */
1573 int
1574term_none_open(term_T *term)
1575{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001576 // Also consider the job finished when the channel is closed, to avoid a
1577 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001578 return term != NULL
1579 && term->tl_job != NULL
1580 && channel_is_open(term->tl_job->jv_channel)
1581 && term->tl_job->jv_channel->ch_keep_open;
1582}
1583
1584/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001585 * Used when exiting: kill the job in "buf" if so desired.
1586 * Return OK when the job finished.
1587 * Return FAIL when the job is still running.
1588 */
1589 int
1590term_try_stop_job(buf_T *buf)
1591{
1592 int count;
1593 char *how = (char *)buf->b_term->tl_kill;
1594
1595#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001596 if ((how == NULL || *how == NUL)
1597 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001598 {
1599 char_u buff[DIALOG_MSG_SIZE];
1600 int ret;
1601
Bram Moolenaar00806bc2020-11-05 19:36:38 +01001602 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001603 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1604 if (ret == VIM_YES)
1605 how = "kill";
1606 else if (ret == VIM_CANCEL)
1607 return FAIL;
1608 }
1609#endif
1610 if (how == NULL || *how == NUL)
1611 return FAIL;
1612
1613 job_stop(buf->b_term->tl_job, NULL, how);
1614
Bram Moolenaar9172d232019-01-29 23:06:54 +01001615 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001616 for (count = 0; count < 100; ++count)
1617 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001618 job_T *job;
1619
1620 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001621 if (!buf_valid(buf)
1622 || buf->b_term == NULL
1623 || buf->b_term->tl_job == NULL)
1624 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001625 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001626
Bram Moolenaar9172d232019-01-29 23:06:54 +01001627 // Call job_status() to update jv_status. It may cause the job to be
1628 // cleaned up but it won't be freed.
1629 job_status(job);
1630 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001631 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001632
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001633 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001634 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001635 }
1636 return FAIL;
1637}
1638
1639/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001640 * Add the last line of the scrollback buffer to the buffer in the window.
1641 */
1642 static void
1643add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1644{
1645 buf_T *buf = term->tl_buffer;
1646 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1647 linenr_T lnum = buf->b_ml.ml_line_count;
1648
Bram Moolenaar4f974752019-02-17 17:44:42 +01001649#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001650 if (!enc_utf8 && enc_codepage > 0)
1651 {
1652 WCHAR *ret = NULL;
1653 int length = 0;
1654
1655 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1656 &ret, &length);
1657 if (ret != NULL)
1658 {
1659 WideCharToMultiByte_alloc(enc_codepage, 0,
1660 ret, length, (char **)&text, &len, 0, 0);
1661 vim_free(ret);
1662 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1663 vim_free(text);
1664 }
1665 }
1666 else
1667#endif
1668 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1669 if (empty)
1670 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001671 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001672 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001673 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001674 curbuf = curwin->w_buffer;
1675 }
1676}
1677
1678 static void
1679cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1680{
1681 attr->width = cell->width;
1682 attr->attrs = cell->attrs;
1683 attr->fg = cell->fg;
1684 attr->bg = cell->bg;
1685}
1686
1687 static int
1688equal_celattr(cellattr_T *a, cellattr_T *b)
1689{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001690 // We only compare the RGB colors, ignoring the ANSI index and type.
1691 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001692 return a->fg.red == b->fg.red
1693 && a->fg.green == b->fg.green
1694 && a->fg.blue == b->fg.blue
1695 && a->bg.red == b->bg.red
1696 && a->bg.green == b->bg.green
1697 && a->bg.blue == b->bg.blue;
1698}
1699
Bram Moolenaard96ff162018-02-18 22:13:29 +01001700/*
1701 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1702 * line at this position. Otherwise at the end.
1703 */
1704 static int
1705add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1706{
1707 if (ga_grow(&term->tl_scrollback, 1) == OK)
1708 {
1709 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1710 + term->tl_scrollback.ga_len;
1711
1712 if (lnum > 0)
1713 {
1714 int i;
1715
1716 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1717 {
1718 *line = *(line - 1);
1719 --line;
1720 }
1721 }
1722 line->sb_cols = 0;
1723 line->sb_cells = NULL;
1724 line->sb_fill_attr = *fill_attr;
1725 ++term->tl_scrollback.ga_len;
1726 return OK;
1727 }
1728 return FALSE;
1729}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001730
1731/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001732 * Remove the terminal contents from the scrollback and the buffer.
1733 * Used before adding a new scrollback line or updating the buffer for lines
1734 * displayed in the terminal.
1735 */
1736 static void
1737cleanup_scrollback(term_T *term)
1738{
1739 sb_line_T *line;
1740 garray_T *gap;
1741
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001742 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001743 gap = &term->tl_scrollback;
1744 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1745 && gap->ga_len > 0)
1746 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001747 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001748 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1749 vim_free(line->sb_cells);
1750 --gap->ga_len;
1751 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001752 curbuf = curwin->w_buffer;
1753 if (curbuf == term->tl_buffer)
1754 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001755}
1756
1757/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001758 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001759 */
1760 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001761update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001762{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001763 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001764 int len;
1765 int lines_skipped = 0;
1766 VTermPos pos;
1767 VTermScreenCell cell;
1768 cellattr_T fill_attr, new_fill_attr;
1769 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001770
1771 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1772 "Adding terminal window snapshot to buffer");
1773
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001774 // First remove the lines that were appended before, they might be
1775 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001776 cleanup_scrollback(term);
1777
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001778 screen = vterm_obtain_screen(term->tl_vterm);
1779 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001780 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1781 {
1782 len = 0;
1783 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1784 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1785 && cell.chars[0] != NUL)
1786 {
1787 len = pos.col + 1;
1788 new_fill_attr = term->tl_default_color;
1789 }
1790 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001791 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001792 cell2cellattr(&cell, &new_fill_attr);
1793
1794 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1795 ++lines_skipped;
1796 else
1797 {
1798 while (lines_skipped > 0)
1799 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001800 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001801 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001802 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001803 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001804 }
1805
1806 if (len == 0)
1807 p = NULL;
1808 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001809 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001810 if ((p != NULL || len == 0)
1811 && ga_grow(&term->tl_scrollback, 1) == OK)
1812 {
1813 garray_T ga;
1814 int width;
1815 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1816 + term->tl_scrollback.ga_len;
1817
1818 ga_init2(&ga, 1, 100);
1819 for (pos.col = 0; pos.col < len; pos.col += width)
1820 {
1821 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1822 {
1823 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001824 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001825 if (ga_grow(&ga, 1) == OK)
1826 ga.ga_len += utf_char2bytes(' ',
1827 (char_u *)ga.ga_data + ga.ga_len);
1828 }
1829 else
1830 {
1831 width = cell.width;
1832
1833 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001834 if (width == 2)
1835 // second cell of double-width character has the
1836 // same attributes.
1837 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001838
Bram Moolenaara79fd562018-12-20 20:47:32 +01001839 // Each character can be up to 6 bytes.
1840 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001841 {
1842 int i;
1843 int c;
1844
1845 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1846 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1847 (char_u *)ga.ga_data + ga.ga_len);
1848 }
1849 }
1850 }
1851 line->sb_cols = len;
1852 line->sb_cells = p;
1853 line->sb_fill_attr = new_fill_attr;
1854 fill_attr = new_fill_attr;
1855 ++term->tl_scrollback.ga_len;
1856
1857 if (ga_grow(&ga, 1) == FAIL)
1858 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1859 else
1860 {
1861 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1862 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1863 }
1864 ga_clear(&ga);
1865 }
1866 else
1867 vim_free(p);
1868 }
1869 }
1870
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001871 // Add trailing empty lines.
1872 for (pos.row = term->tl_scrollback.ga_len;
1873 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1874 ++pos.row)
1875 {
1876 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1877 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1878 }
1879
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001880 term->tl_dirty_snapshot = FALSE;
1881#ifdef FEAT_TIMERS
1882 term->tl_timer_set = FALSE;
1883#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001884}
1885
1886/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001887 * Loop over all windows in the current tab, and also curwin, which is not
1888 * encountered when using a terminal in a popup window.
1889 * Return TRUE if "*wp" was set to the next window.
1890 */
1891 static int
1892for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1893{
1894 if (*wp == NULL)
1895 *wp = firstwin;
1896 else if ((*wp)->w_next != NULL)
1897 *wp = (*wp)->w_next;
1898 else if (!*did_curwin)
1899 *wp = curwin;
1900 else
1901 return FALSE;
1902 if (*wp == curwin)
1903 *did_curwin = TRUE;
1904 return TRUE;
1905}
1906
1907/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001908 * If needed, add the current lines of the terminal to scrollback and to the
1909 * buffer. Called after the job has ended and when switching to
1910 * Terminal-Normal mode.
1911 * When "redraw" is TRUE redraw the windows that show the terminal.
1912 */
1913 static void
1914may_move_terminal_to_buffer(term_T *term, int redraw)
1915{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001916 if (term->tl_vterm == NULL)
1917 return;
1918
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001919 // Update the snapshot only if something changes or the buffer does not
1920 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001921 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1922 <= term->tl_scrollback_scrolled)
1923 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001924
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001925 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001926 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1927 &term->tl_default_color.fg, &term->tl_default_color.bg);
1928
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001929 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001930 {
1931 win_T *wp = NULL;
1932 int did_curwin = FALSE;
1933
1934 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001935 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001936 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001937 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001938 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1939 wp->w_cursor.col = 0;
1940 wp->w_valid = 0;
1941 if (wp->w_cursor.lnum >= wp->w_height)
1942 {
1943 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001944
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001945 if (wp->w_topline < min_topline)
1946 wp->w_topline = min_topline;
1947 }
1948 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001949 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001950 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001951 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001952}
1953
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001954#if defined(FEAT_TIMERS) || defined(PROTO)
1955/*
1956 * Check if any terminal timer expired. If so, copy text from the terminal to
1957 * the buffer.
1958 * Return the time until the next timer will expire.
1959 */
1960 int
1961term_check_timers(int next_due_arg, proftime_T *now)
1962{
1963 term_T *term;
1964 int next_due = next_due_arg;
1965
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001966 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001967 {
1968 if (term->tl_timer_set && !term->tl_normal_mode)
1969 {
1970 long this_due = proftime_time_left(&term->tl_timer_due, now);
1971
1972 if (this_due <= 1)
1973 {
1974 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001975 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001976 }
1977 else if (next_due == -1 || next_due > this_due)
1978 next_due = this_due;
1979 }
1980 }
1981
1982 return next_due;
1983}
1984#endif
1985
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001986/*
1987 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
1988 * otherwise end it.
1989 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001990 static void
1991set_terminal_mode(term_T *term, int normal_mode)
1992{
1993 term->tl_normal_mode = normal_mode;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001994 if (!normal_mode)
1995 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001996 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001997 if (term->tl_buffer == curbuf)
1998 maketitle();
1999}
2000
2001/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002002 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002003 * Move the vterm contents into the scrollback buffer and free the vterm.
2004 */
2005 static void
2006cleanup_vterm(term_T *term)
2007{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002008 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002009 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002010 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002011 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002012}
2013
2014/*
2015 * Switch from Terminal-Job mode to Terminal-Normal mode.
2016 * Suspends updating the terminal window.
2017 */
2018 static void
2019term_enter_normal_mode(void)
2020{
2021 term_T *term = curbuf->b_term;
2022
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002023 set_terminal_mode(term, TRUE);
2024
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002025 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002026 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002027
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002028 // Move the window cursor to the position of the cursor in the
2029 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002030 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2031 + term->tl_cursor_pos.row + 1;
2032 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002033 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2034 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002035 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002036
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002037 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002038 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2039}
2040
2041/*
2042 * Returns TRUE if the current window contains a terminal and we are in
2043 * Terminal-Normal mode.
2044 */
2045 int
2046term_in_normal_mode(void)
2047{
2048 term_T *term = curbuf->b_term;
2049
2050 return term != NULL && term->tl_normal_mode;
2051}
2052
2053/*
2054 * Switch from Terminal-Normal mode to Terminal-Job mode.
2055 * Restores updating the terminal window.
2056 */
2057 void
2058term_enter_job_mode()
2059{
2060 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002061
2062 set_terminal_mode(term, FALSE);
2063
2064 if (term->tl_channel_closed)
2065 cleanup_vterm(term);
2066 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002067#ifdef FEAT_PROP_POPUP
2068 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002069 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002070#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002071}
2072
2073/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002074 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002075 * Note: while waiting a terminal may be closed and freed if the channel is
2076 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002077 */
2078 static int
2079term_vgetc()
2080{
2081 int c;
2082 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002083 int modify_other_keys =
2084 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002085
2086 State = TERMINAL;
2087 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002088#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002089 ctrl_break_was_pressed = FALSE;
2090#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002091 if (modify_other_keys)
2092 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002093 c = vgetc();
2094 got_int = FALSE;
2095 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002096 if (modify_other_keys)
2097 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002098 return c;
2099}
2100
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002101static int mouse_was_outside = FALSE;
2102
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002103/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002104 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002105 * Return FAIL when the key needs to be handled in Normal mode.
2106 * Return OK when the key was dropped or sent to the terminal.
2107 */
2108 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002109send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002110{
2111 char msg[KEY_BUF_LEN];
2112 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002113 int dragging_outside = FALSE;
2114
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002115 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002116 switch (c)
2117 {
2118 case NUL:
2119 case K_ZERO:
2120 if (typed)
2121 stuffcharReadbuff(c);
2122 return FAIL;
2123
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002124 case K_TABLINE:
2125 stuffcharReadbuff(c);
2126 return FAIL;
2127
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002128 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002129 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002130 return FAIL;
2131
2132 case K_LEFTDRAG:
2133 case K_MIDDLEDRAG:
2134 case K_RIGHTDRAG:
2135 case K_X1DRAG:
2136 case K_X2DRAG:
2137 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002138 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002139 case K_LEFTMOUSE:
2140 case K_LEFTMOUSE_NM:
2141 case K_LEFTRELEASE:
2142 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002143 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002144 case K_MIDDLEMOUSE:
2145 case K_MIDDLERELEASE:
2146 case K_RIGHTMOUSE:
2147 case K_RIGHTRELEASE:
2148 case K_X1MOUSE:
2149 case K_X1RELEASE:
2150 case K_X2MOUSE:
2151 case K_X2RELEASE:
2152
2153 case K_MOUSEUP:
2154 case K_MOUSEDOWN:
2155 case K_MOUSELEFT:
2156 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002157 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002158 int row = mouse_row;
2159 int col = mouse_col;
2160
2161#ifdef FEAT_PROP_POPUP
2162 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002163 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002164 row -= popup_top_extra(curwin);
2165 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002166 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002167#endif
2168 if (row < W_WINROW(curwin)
2169 || row >= (W_WINROW(curwin) + curwin->w_height)
2170 || col < curwin->w_wincol
2171 || col >= W_ENDCOL(curwin)
2172 || dragging_outside)
2173 {
2174 // click or scroll outside the current window or on status
2175 // line or vertical separator
2176 if (typed)
2177 {
2178 stuffcharReadbuff(c);
2179 mouse_was_outside = TRUE;
2180 }
2181 return FAIL;
2182 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002183 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002184 break;
2185
2186 case K_COMMAND:
2187 return do_cmdline(NULL, getcmdkeycmd, NULL, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002188 }
2189 if (typed)
2190 mouse_was_outside = FALSE;
2191
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002192 // Convert the typed key to a sequence of bytes for the job.
2193 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002194 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002195 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002196 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2197 (char_u *)msg, (int)len, NULL);
2198
2199 return OK;
2200}
2201
2202 static void
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002203position_cursor(win_T *wp, VTermPos *pos)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002204{
2205 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2206 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002207#ifdef FEAT_PROP_POPUP
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002208 if (popup_is_popup(wp))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002209 {
Bram Moolenaarf5452692020-11-28 21:56:06 +01002210 wp->w_wrow += popup_top_extra(wp);
2211 wp->w_wcol += popup_left_extra(wp);
Bram Moolenaar6a076442020-11-15 20:32:58 +01002212 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002213 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01002214 else
2215 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002216#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002217 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2218}
2219
2220/*
2221 * Handle CTRL-W "": send register contents to the job.
2222 */
2223 static void
2224term_paste_register(int prev_c UNUSED)
2225{
2226 int c;
2227 list_T *l;
2228 listitem_T *item;
2229 long reglen = 0;
2230 int type;
2231
2232#ifdef FEAT_CMDL_INFO
2233 if (add_to_showcmd(prev_c))
2234 if (add_to_showcmd('"'))
2235 out_flush();
2236#endif
2237 c = term_vgetc();
2238#ifdef FEAT_CMDL_INFO
2239 clear_showcmd();
2240#endif
2241 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002242 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002243 return;
2244
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002245 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002246 if (c == '=' && get_expr_register() != '=')
2247 return;
2248 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002249 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002250 return;
2251
2252 l = (list_T *)get_reg_contents(c, GREG_LIST);
2253 if (l != NULL)
2254 {
2255 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002256 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002257 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002258 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002259#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002260 char_u *tmp = s;
2261
2262 if (!enc_utf8 && enc_codepage > 0)
2263 {
2264 WCHAR *ret = NULL;
2265 int length = 0;
2266
2267 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2268 (int)STRLEN(s), &ret, &length);
2269 if (ret != NULL)
2270 {
2271 WideCharToMultiByte_alloc(CP_UTF8, 0,
2272 ret, length, (char **)&s, &length, 0, 0);
2273 vim_free(ret);
2274 }
2275 }
2276#endif
2277 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2278 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002279#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002280 if (tmp != s)
2281 vim_free(s);
2282#endif
2283
2284 if (item->li_next != NULL || type == MLINE)
2285 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2286 (char_u *)"\r", 1, NULL);
2287 }
2288 list_free(l);
2289 }
2290}
2291
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002292/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002293 * Return TRUE when waiting for a character in the terminal, the cursor of the
2294 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002295 */
2296 int
2297terminal_is_active()
2298{
2299 return in_terminal_loop != NULL;
2300}
2301
Bram Moolenaar83d47902020-03-26 20:34:00 +01002302/*
2303 * Return the highight group name for the terminal; "Terminal" if not set.
2304 */
2305 static char_u *
2306term_get_highlight_name(term_T *term)
2307{
2308 if (term->tl_highlight_name == NULL)
2309 return (char_u *)"Terminal";
2310 return term->tl_highlight_name;
2311}
2312
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002313#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002314 cursorentry_T *
2315term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2316{
2317 term_T *term = in_terminal_loop;
2318 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002319 int id;
2320 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002321
Bram Moolenaara80faa82020-04-12 19:37:17 +02002322 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002323 entry.shape = entry.mshape =
2324 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2325 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2326 SHAPE_BLOCK;
2327 entry.percentage = 20;
2328 if (term->tl_cursor_blink)
2329 {
2330 entry.blinkwait = 700;
2331 entry.blinkon = 400;
2332 entry.blinkoff = 250;
2333 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002334
Bram Moolenaar83d47902020-03-26 20:34:00 +01002335 // The highlight group overrules the defaults.
2336 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002337 if (id != 0)
2338 {
2339 syn_id2colors(id, &term_fg, &term_bg);
2340 *fg = term_bg;
2341 }
2342 else
2343 *fg = gui.back_pixel;
2344
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002345 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002346 {
2347 if (id != 0)
2348 *bg = term_fg;
2349 else
2350 *bg = gui.norm_pixel;
2351 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002352 else
2353 *bg = color_name2handle(term->tl_cursor_color);
2354 entry.name = "n";
2355 entry.used_for = SHAPE_CURSOR;
2356
2357 return &entry;
2358}
2359#endif
2360
Bram Moolenaard317b382018-02-08 22:33:31 +01002361 static void
2362may_output_cursor_props(void)
2363{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002364 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002365 || last_set_cursor_shape != desired_cursor_shape
2366 || last_set_cursor_blink != desired_cursor_blink)
2367 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002368 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002369 last_set_cursor_shape = desired_cursor_shape;
2370 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002371 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002372 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002373 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002374 ui_cursor_shape_forced(TRUE);
2375 else
2376 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2377 }
2378}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002379
Bram Moolenaard317b382018-02-08 22:33:31 +01002380/*
2381 * Set the cursor color and shape, if not last set to these.
2382 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002383 static void
2384may_set_cursor_props(term_T *term)
2385{
2386#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002387 // For the GUI the cursor properties are obtained with
2388 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002389 if (gui.in_use)
2390 return;
2391#endif
2392 if (in_terminal_loop == term)
2393 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002394 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002395 desired_cursor_shape = term->tl_cursor_shape;
2396 desired_cursor_blink = term->tl_cursor_blink;
2397 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002398 }
2399}
2400
Bram Moolenaard317b382018-02-08 22:33:31 +01002401/*
2402 * Reset the desired cursor properties and restore them when needed.
2403 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002404 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002405prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002406{
2407#ifdef FEAT_GUI
2408 if (gui.in_use)
2409 return;
2410#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002411 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002412 desired_cursor_shape = -1;
2413 desired_cursor_blink = -1;
2414 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002415}
2416
2417/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002418 * Returns TRUE if the current window contains a terminal and we are sending
2419 * keys to the job.
2420 * If "check_job_status" is TRUE update the job status.
2421 */
2422 static int
2423term_use_loop_check(int check_job_status)
2424{
2425 term_T *term = curbuf->b_term;
2426
2427 return term != NULL
2428 && !term->tl_normal_mode
2429 && term->tl_vterm != NULL
2430 && term_job_running_check(term, check_job_status);
2431}
2432
2433/*
2434 * Returns TRUE if the current window contains a terminal and we are sending
2435 * keys to the job.
2436 */
2437 int
2438term_use_loop(void)
2439{
2440 return term_use_loop_check(FALSE);
2441}
2442
2443/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002444 * Called when entering a window with the mouse. If this is a terminal window
2445 * we may want to change state.
2446 */
2447 void
2448term_win_entered()
2449{
2450 term_T *term = curbuf->b_term;
2451
2452 if (term != NULL)
2453 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002454 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002455 {
2456 reset_VIsual_and_resel();
2457 if (State & INSERT)
2458 stop_insert_mode = TRUE;
2459 }
2460 mouse_was_outside = FALSE;
2461 enter_mouse_col = mouse_col;
2462 enter_mouse_row = mouse_row;
2463 }
2464}
2465
2466/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002467 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2468 * Return the Ctrl-key value in that case.
2469 */
2470 static int
2471raw_c_to_ctrl(int c)
2472{
2473 if ((mod_mask & MOD_MASK_CTRL)
2474 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2475 return c & 0x1f;
2476 return c;
2477}
2478
2479/*
2480 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2481 * May set "mod_mask".
2482 */
2483 static int
2484ctrl_to_raw_c(int c)
2485{
2486 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2487 {
2488 mod_mask |= MOD_MASK_CTRL;
2489 return c + '@';
2490 }
2491 return c;
2492}
2493
2494/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002495 * Wait for input and send it to the job.
2496 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2497 * when there is no more typahead.
2498 * Return when the start of a CTRL-W command is typed or anything else that
2499 * should be handled as a Normal mode command.
2500 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2501 * the terminal was closed.
2502 */
2503 int
2504terminal_loop(int blocking)
2505{
2506 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002507 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002508 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002509 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002510#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002511 int tty_fd = curbuf->b_term->tl_job->jv_channel
2512 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002513#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002514 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002515
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002516 // Remember the terminal we are sending keys to. However, the terminal
2517 // might be closed while waiting for a character, e.g. typing "exit" in a
2518 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2519 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002520 in_terminal_loop = curbuf->b_term;
2521
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002522 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002523 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002524 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002525 if (termwinkey == Ctrl_W)
2526 termwinkey = 0;
2527 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002528 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002529 may_set_cursor_props(curbuf->b_term);
2530
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002531 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002532 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002533#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002534 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002535#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002536 // TODO: skip screen update when handling a sequence of keys.
2537 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002538 while (must_redraw != 0)
2539 if (update_screen(0) == FAIL)
2540 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002541 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002542 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002543 break;
2544
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002545 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002546 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002547
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002548 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002549 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002550 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002551 // Job finished while waiting for a character. Push back the
2552 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002553 if (raw_c != K_IGNORE)
2554 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002555 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002556 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002557 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002558 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002559 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002560
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002561#ifdef UNIX
2562 /*
2563 * The shell or another program may change the tty settings. Getting
2564 * them for every typed character is a bit of overhead, but it's needed
2565 * for the first character typed, e.g. when Vim starts in a shell.
2566 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002567 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002568 {
2569 ttyinfo_T info;
2570
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002571 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002572 if (get_tty_info(tty_fd, &info) == OK)
2573 term_backspace_char = info.backspace;
2574 }
2575#endif
2576
Bram Moolenaar4f974752019-02-17 17:44:42 +01002577#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002578 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2579 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002580 if (ctrl_break_was_pressed)
2581 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2582#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002583 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2584 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002585 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002586#ifdef FEAT_GUI
2587 && !curbuf->b_term->tl_system
2588#endif
2589 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002590 {
2591 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002592 int prev_raw_c = raw_c;
2593 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002594
2595#ifdef FEAT_CMDL_INFO
2596 if (add_to_showcmd(c))
2597 out_flush();
2598#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002599 raw_c = term_vgetc();
2600 c = raw_c_to_ctrl(raw_c);
2601
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002602#ifdef FEAT_CMDL_INFO
2603 clear_showcmd();
2604#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002605 if (!term_use_loop_check(TRUE)
2606 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002607 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002608 break;
2609
2610 if (prev_c == Ctrl_BSL)
2611 {
2612 if (c == Ctrl_N)
2613 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002614 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002615 term_enter_normal_mode();
2616 ret = FAIL;
2617 goto theend;
2618 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002619 // Send both keys to the terminal, first one here, second one
2620 // below.
2621 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2622 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002623 }
2624 else if (c == Ctrl_C)
2625 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002626 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002627 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2628 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002629 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002630 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002631 // "CTRL-W .": send CTRL-W to the job
2632 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002633 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002634 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002635 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002636 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002637 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002638 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002639 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002640 else if (c == 'N')
2641 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002642 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002643 term_enter_normal_mode();
2644 ret = FAIL;
2645 goto theend;
2646 }
2647 else if (c == '"')
2648 {
2649 term_paste_register(prev_c);
2650 continue;
2651 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002652 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002653 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002654 // space for CTRL-W, modifier, multi-byte char and NUL
2655 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002656
2657 // Put the command into the typeahead buffer, when using the
2658 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2659 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002660 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002661 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002662 ret = OK;
2663 goto theend;
2664 }
2665 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002666# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002667 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002668 {
2669 WCHAR wc;
2670 char_u mb[3];
2671
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002672 mb[0] = (unsigned)raw_c >> 8;
2673 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002674 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002675 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002676 }
2677# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002678 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002679 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002680 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002681 // We are sure to come back here, don't reset the cursor color
2682 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002683 restore_cursor = FALSE;
2684
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002685 ret = OK;
2686 goto theend;
2687 }
2688 }
2689 ret = FAIL;
2690
2691theend:
2692 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002693 if (restore_cursor)
2694 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002695
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002696 // Move a snapshot of the screen contents to the buffer, so that completion
2697 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002698 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2699 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002700
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002701 return ret;
2702}
2703
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002704 static void
2705may_toggle_cursor(term_T *term)
2706{
2707 if (in_terminal_loop == term)
2708 {
2709 if (term->tl_cursor_visible)
2710 cursor_on();
2711 else
2712 cursor_off();
2713 }
2714}
2715
2716/*
Bram Moolenaar83d47902020-03-26 20:34:00 +01002717 * Cache "Terminal" highlight group colors.
2718 */
2719 void
2720set_terminal_default_colors(int cterm_fg, int cterm_bg)
2721{
2722 term_default_cterm_fg = cterm_fg - 1;
2723 term_default_cterm_bg = cterm_bg - 1;
2724}
2725
2726 static int
2727get_default_cterm_fg(term_T *term)
2728{
2729 if (term->tl_highlight_name != NULL)
2730 {
2731 int id = syn_name2id(term->tl_highlight_name);
2732 int fg = -1;
2733 int bg = -1;
2734
2735 if (id > 0)
2736 syn_id2cterm_bg(id, &fg, &bg);
2737 return fg;
2738 }
2739 return term_default_cterm_fg;
2740}
2741
2742 static int
2743get_default_cterm_bg(term_T *term)
2744{
2745 if (term->tl_highlight_name != NULL)
2746 {
2747 int id = syn_name2id(term->tl_highlight_name);
2748 int fg = -1;
2749 int bg = -1;
2750
2751 if (id > 0)
2752 syn_id2cterm_bg(id, &fg, &bg);
2753 return bg;
2754 }
2755 return term_default_cterm_bg;
2756}
2757
2758/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002759 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002760 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002761 */
2762 static int
2763color2index(VTermColor *color, int fg, int *boldp)
2764{
2765 int red = color->red;
2766 int blue = color->blue;
2767 int green = color->green;
2768
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002769 if (VTERM_COLOR_IS_DEFAULT_FG(color)
2770 || VTERM_COLOR_IS_DEFAULT_BG(color))
2771 return 0;
2772 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002773 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002774 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002775 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002776 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002777 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002778 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2779 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2780 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002781 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002782 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2783 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2784 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2785 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2786 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2787 case 10: return lookup_color(20, fg, boldp) + 1; // red
2788 case 11: return lookup_color(16, fg, boldp) + 1; // green
2789 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2790 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2791 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2792 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2793 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002794 }
2795 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002796
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002797 if (t_colors >= 256)
2798 {
2799 if (red == blue && red == green)
2800 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002801 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002802 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002803 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2804 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2805 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002806 int i;
2807
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002808 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002809 return 17; // 00/00/00
2810 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002811 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002812 for (i = 0; i < 23; ++i)
2813 if (red < cutoff[i])
2814 return i + 233;
2815 return 256;
2816 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002817 {
2818 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2819 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002820
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002821 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002822 for (ri = 0; ri < 5; ++ri)
2823 if (red < cutoff[ri])
2824 break;
2825 for (gi = 0; gi < 5; ++gi)
2826 if (green < cutoff[gi])
2827 break;
2828 for (bi = 0; bi < 5; ++bi)
2829 if (blue < cutoff[bi])
2830 break;
2831 return 17 + ri * 36 + gi * 6 + bi;
2832 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002833 }
2834 return 0;
2835}
2836
2837/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002838 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002839 */
2840 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002841vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002842{
2843 int attr = 0;
2844
2845 if (cellattrs.bold)
2846 attr |= HL_BOLD;
2847 if (cellattrs.underline)
2848 attr |= HL_UNDERLINE;
2849 if (cellattrs.italic)
2850 attr |= HL_ITALIC;
2851 if (cellattrs.strike)
2852 attr |= HL_STRIKETHROUGH;
2853 if (cellattrs.reverse)
2854 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002855 return attr;
2856}
2857
2858/*
2859 * Store Vterm attributes in "cell" from highlight flags.
2860 */
2861 static void
2862hl2vtermAttr(int attr, cellattr_T *cell)
2863{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002864 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002865 if (attr & HL_BOLD)
2866 cell->attrs.bold = 1;
2867 if (attr & HL_UNDERLINE)
2868 cell->attrs.underline = 1;
2869 if (attr & HL_ITALIC)
2870 cell->attrs.italic = 1;
2871 if (attr & HL_STRIKETHROUGH)
2872 cell->attrs.strike = 1;
2873 if (attr & HL_INVERSE)
2874 cell->attrs.reverse = 1;
2875}
2876
2877/*
2878 * Convert the attributes of a vterm cell into an attribute index.
2879 */
2880 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002881cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002882 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002883 win_T *wp,
2884 VTermScreenCellAttrs cellattrs,
2885 VTermColor cellfg,
2886 VTermColor cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002887{
2888 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002889
2890#ifdef FEAT_GUI
2891 if (gui.in_use)
2892 {
2893 guicolor_T fg, bg;
2894
2895 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2896 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2897 return get_gui_attr_idx(attr, fg, bg);
2898 }
2899 else
2900#endif
2901#ifdef FEAT_TERMGUICOLORS
2902 if (p_tgc)
2903 {
2904 guicolor_T fg, bg;
2905
2906 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2907 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2908
2909 return get_tgc_attr_idx(attr, fg, bg);
2910 }
2911 else
2912#endif
2913 {
2914 int bold = MAYBE;
2915 int fg = color2index(&cellfg, TRUE, &bold);
2916 int bg = color2index(&cellbg, FALSE, &bold);
2917
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002918 // Use the 'wincolor' or "Terminal" highlighting for the default
2919 // colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002920 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002921 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002922 int wincolor_fg = -1;
2923 int wincolor_bg = -1;
2924
2925 if (wp != NULL && *wp->w_p_wcr != NUL)
2926 {
2927 int id = syn_name2id(curwin->w_p_wcr);
2928
2929 // Get the 'wincolor' group colors.
2930 if (id > 0)
2931 syn_id2cterm_bg(id, &wincolor_fg, &wincolor_bg);
2932 }
2933 if (fg == 0)
2934 {
2935 if (wincolor_fg >= 0)
2936 fg = wincolor_fg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002937 else
2938 {
2939 int cterm_fg = get_default_cterm_fg(term);
2940
2941 if (cterm_fg >= 0)
2942 fg = cterm_fg + 1;
2943 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002944 }
2945 if (bg == 0)
2946 {
2947 if (wincolor_bg >= 0)
2948 bg = wincolor_bg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002949 else
2950 {
2951 int cterm_bg = get_default_cterm_bg(term);
2952
2953 if (cterm_bg >= 0)
2954 bg = cterm_bg + 1;
2955 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002956 }
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002957 }
2958
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002959 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002960 if (bold == TRUE)
2961 attr |= HL_BOLD;
2962 return get_cterm_attr_idx(attr, fg, bg);
2963 }
2964 return 0;
2965}
2966
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002967 static void
2968set_dirty_snapshot(term_T *term)
2969{
2970 term->tl_dirty_snapshot = TRUE;
2971#ifdef FEAT_TIMERS
2972 if (!term->tl_normal_mode)
2973 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002974 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002975 profile_setlimit(100L, &term->tl_timer_due);
2976 term->tl_timer_set = TRUE;
2977 }
2978#endif
2979}
2980
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002981 static int
2982handle_damage(VTermRect rect, void *user)
2983{
2984 term_T *term = (term_T *)user;
2985
2986 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2987 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002988 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002989 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002990 return 1;
2991}
2992
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002993 static void
2994term_scroll_up(term_T *term, int start_row, int count)
2995{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002996 win_T *wp = NULL;
2997 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002998 VTermColor fg, bg;
2999 VTermScreenCellAttrs attr;
3000 int clear_attr;
3001
Bram Moolenaara80faa82020-04-12 19:37:17 +02003002 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003003
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003004 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003005 {
3006 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003007 {
3008 // Set the color to clear lines with.
3009 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
3010 &fg, &bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01003011 clear_attr = cell2attr(term, wp, attr, fg, bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003012 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003013 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003014 }
3015}
3016
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003017 static int
3018handle_moverect(VTermRect dest, VTermRect src, void *user)
3019{
3020 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003021 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003022
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003023 // Scrolling up is done much more efficiently by deleting lines instead of
3024 // redrawing the text. But avoid doing this multiple times, postpone until
3025 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003026 if (dest.start_col == src.start_col
3027 && dest.end_col == src.end_col
3028 && dest.start_row < src.start_row)
3029 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003030 if (dest.start_row == 0)
3031 term->tl_postponed_scroll += count;
3032 else
3033 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003034 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003035
3036 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3037 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003038 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003039
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003040 // Note sure if the scrolling will work correctly, let's do a complete
3041 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003042 redraw_buf_later(term->tl_buffer, NOT_VALID);
3043 return 1;
3044}
3045
3046 static int
3047handle_movecursor(
3048 VTermPos pos,
3049 VTermPos oldpos UNUSED,
3050 int visible,
3051 void *user)
3052{
3053 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003054 win_T *wp = NULL;
3055 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003056
3057 term->tl_cursor_pos = pos;
3058 term->tl_cursor_visible = visible;
3059
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003060 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003061 {
3062 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003063 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003064 }
3065 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003066 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003067
3068 return 1;
3069}
3070
3071 static int
3072handle_settermprop(
3073 VTermProp prop,
3074 VTermValue *value,
3075 void *user)
3076{
3077 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003078 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003079
3080 switch (prop)
3081 {
3082 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003083 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003084 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003085 if (strval == NULL)
3086 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003087 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003088 // a blank title isn't useful, make it empty, so that "running" is
3089 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003090 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003091 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003092 // Same as blank
3093 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003094 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003095 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3096 term->tl_title = NULL;
3097 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003098 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003099 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003100#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003101 else if (!enc_utf8 && enc_codepage > 0)
3102 {
3103 WCHAR *ret = NULL;
3104 int length = 0;
3105
3106 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003107 (char*)value->string.str,
3108 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003109 if (ret != NULL)
3110 {
3111 WideCharToMultiByte_alloc(enc_codepage, 0,
3112 ret, length, (char**)&term->tl_title,
3113 &length, 0, 0);
3114 vim_free(ret);
3115 }
3116 }
3117#endif
3118 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003119 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003120 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003121 strval = NULL;
3122 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003123 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003124 if (term == curbuf->b_term)
3125 maketitle();
3126 break;
3127
3128 case VTERM_PROP_CURSORVISIBLE:
3129 term->tl_cursor_visible = value->boolean;
3130 may_toggle_cursor(term);
3131 out_flush();
3132 break;
3133
3134 case VTERM_PROP_CURSORBLINK:
3135 term->tl_cursor_blink = value->boolean;
3136 may_set_cursor_props(term);
3137 break;
3138
3139 case VTERM_PROP_CURSORSHAPE:
3140 term->tl_cursor_shape = value->number;
3141 may_set_cursor_props(term);
3142 break;
3143
3144 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003145 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003146 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003147 if (strval == NULL)
3148 break;
3149 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003150 may_set_cursor_props(term);
3151 break;
3152
3153 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003154 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003155 term->tl_using_altscreen = value->boolean;
3156 break;
3157
3158 default:
3159 break;
3160 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003161 vim_free(strval);
3162
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003163 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003164 return 1;
3165}
3166
3167/*
3168 * The job running in the terminal resized the terminal.
3169 */
3170 static int
3171handle_resize(int rows, int cols, void *user)
3172{
3173 term_T *term = (term_T *)user;
3174 win_T *wp;
3175
3176 term->tl_rows = rows;
3177 term->tl_cols = cols;
3178 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003179 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003180 term->tl_vterm_size_changed = FALSE;
3181 else
3182 {
3183 FOR_ALL_WINDOWS(wp)
3184 {
3185 if (wp->w_buffer == term->tl_buffer)
3186 {
3187 win_setheight_win(rows, wp);
3188 win_setwidth_win(cols, wp);
3189 }
3190 }
3191 redraw_buf_later(term->tl_buffer, NOT_VALID);
3192 }
3193 return 1;
3194}
3195
3196/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003197 * If the number of lines that are stored goes over 'termscrollback' then
3198 * delete the first 10%.
3199 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3200 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003201 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003202 static void
3203limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003204{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003205 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003206 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003207 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003208 int i;
3209
3210 curbuf = term->tl_buffer;
3211 for (i = 0; i < todo; ++i)
3212 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003213 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3214 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003215 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003216 }
3217 curbuf = curwin->w_buffer;
3218
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003219 gap->ga_len -= todo;
3220 mch_memmove(gap->ga_data,
3221 (sb_line_T *)gap->ga_data + todo,
3222 sizeof(sb_line_T) * gap->ga_len);
3223 if (update_buffer)
3224 term->tl_scrollback_scrolled -= todo;
3225 }
3226}
3227
3228/*
3229 * Handle a line that is pushed off the top of the screen.
3230 */
3231 static int
3232handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3233{
3234 term_T *term = (term_T *)user;
3235 garray_T *gap;
3236 int update_buffer;
3237
3238 if (term->tl_normal_mode)
3239 {
3240 // In Terminal-Normal mode the user interacts with the buffer, thus we
3241 // must not change it. Postpone adding the scrollback lines.
3242 gap = &term->tl_scrollback_postponed;
3243 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003244 }
3245 else
3246 {
3247 // First remove the lines that were appended before, the pushed line
3248 // goes above it.
3249 cleanup_scrollback(term);
3250 gap = &term->tl_scrollback;
3251 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003252 }
3253
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003254 limit_scrollback(term, gap, update_buffer);
3255
3256 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003257 {
3258 cellattr_T *p = NULL;
3259 int len = 0;
3260 int i;
3261 int c;
3262 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003263 int text_len;
3264 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003265 sb_line_T *line;
3266 garray_T ga;
3267 cellattr_T fill_attr = term->tl_default_color;
3268
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003269 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003270 for (i = 0; i < cols; ++i)
3271 if (cells[i].chars[0] != 0)
3272 len = i + 1;
3273 else
3274 cell2cellattr(&cells[i], &fill_attr);
3275
3276 ga_init2(&ga, 1, 100);
3277 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003278 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003279 if (p != NULL)
3280 {
3281 for (col = 0; col < len; col += cells[col].width)
3282 {
3283 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3284 {
3285 ga.ga_len = 0;
3286 break;
3287 }
3288 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3289 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3290 (char_u *)ga.ga_data + ga.ga_len);
3291 cell2cellattr(&cells[col], &p[col]);
3292 }
3293 }
3294 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003295 {
3296 if (update_buffer)
3297 text = (char_u *)"";
3298 else
3299 text = vim_strsave((char_u *)"");
3300 text_len = 0;
3301 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003302 else
3303 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003304 text = ga.ga_data;
3305 text_len = ga.ga_len;
3306 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003307 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003308 if (update_buffer)
3309 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003310
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003311 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003312 line->sb_cols = len;
3313 line->sb_cells = p;
3314 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003315 if (update_buffer)
3316 {
3317 line->sb_text = NULL;
3318 ++term->tl_scrollback_scrolled;
3319 ga_clear(&ga); // free the text
3320 }
3321 else
3322 {
3323 line->sb_text = text;
3324 ga_init(&ga); // text is kept in tl_scrollback_postponed
3325 }
3326 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003327 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003328 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003329}
3330
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003331/*
3332 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3333 * received and stored in tl_scrollback_postponed.
3334 */
3335 static void
3336handle_postponed_scrollback(term_T *term)
3337{
3338 int i;
3339
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003340 if (term->tl_scrollback_postponed.ga_len == 0)
3341 return;
3342 ch_log(NULL, "Moving postponed scrollback to scrollback");
3343
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003344 // First remove the lines that were appended before, the pushed lines go
3345 // above it.
3346 cleanup_scrollback(term);
3347
3348 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3349 {
3350 char_u *text;
3351 sb_line_T *pp_line;
3352 sb_line_T *line;
3353
3354 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3355 break;
3356 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3357
3358 text = pp_line->sb_text;
3359 if (text == NULL)
3360 text = (char_u *)"";
3361 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3362 vim_free(pp_line->sb_text);
3363
3364 line = (sb_line_T *)term->tl_scrollback.ga_data
3365 + term->tl_scrollback.ga_len;
3366 line->sb_cols = pp_line->sb_cols;
3367 line->sb_cells = pp_line->sb_cells;
3368 line->sb_fill_attr = pp_line->sb_fill_attr;
3369 line->sb_text = NULL;
3370 ++term->tl_scrollback_scrolled;
3371 ++term->tl_scrollback.ga_len;
3372 }
3373
3374 ga_clear(&term->tl_scrollback_postponed);
3375 limit_scrollback(term, &term->tl_scrollback, TRUE);
3376}
3377
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003378static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003379 handle_damage, // damage
3380 handle_moverect, // moverect
3381 handle_movecursor, // movecursor
3382 handle_settermprop, // settermprop
3383 NULL, // bell
3384 handle_resize, // resize
3385 handle_pushline, // sb_pushline
3386 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003387};
3388
3389/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003390 * Do the work after the channel of a terminal was closed.
3391 * Must be called only when updating_screen is FALSE.
3392 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3393 */
3394 static int
3395term_after_channel_closed(term_T *term)
3396{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003397 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003398 if (!term->tl_normal_mode)
3399 {
3400 int fnum = term->tl_buffer->b_fnum;
3401
3402 cleanup_vterm(term);
3403
3404 if (term->tl_finish == TL_FINISH_CLOSE)
3405 {
3406 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003407 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003408#ifdef FEAT_PROP_POPUP
3409 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003410
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003411 // If this was a terminal in a popup window, go back to the
3412 // previous window.
3413 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3414 {
3415 pwin = curwin;
3416 if (win_valid(prevwin))
3417 win_enter(prevwin, FALSE);
3418 }
3419 else
3420#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003421 // If this is the last normal window: exit Vim.
3422 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3423 {
3424 exarg_T ea;
3425
Bram Moolenaara80faa82020-04-12 19:37:17 +02003426 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003427 ex_quit(&ea);
3428 return TRUE;
3429 }
3430
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003431 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003432 ch_log(NULL, "terminal job finished, closing window");
3433 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003434 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003435 if (curwin == aucmd_win)
3436 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003437 if (do_set_w_closing)
3438 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003439 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003440 if (do_set_w_closing)
3441 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003442 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003443#ifdef FEAT_PROP_POPUP
3444 if (pwin != NULL)
3445 popup_close_with_retval(pwin, 0);
3446#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003447 return TRUE;
3448 }
3449 if (term->tl_finish == TL_FINISH_OPEN
3450 && term->tl_buffer->b_nwindows == 0)
3451 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003452 char *cmd = term->tl_opencmd == NULL
3453 ? "botright sbuf %d"
3454 : (char *)term->tl_opencmd;
3455 size_t len = strlen(cmd) + 50;
3456 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003457
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003458 if (buf != NULL)
3459 {
3460 ch_log(NULL, "terminal job finished, opening window");
3461 vim_snprintf(buf, len, cmd, fnum);
3462 do_cmdline_cmd((char_u *)buf);
3463 vim_free(buf);
3464 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003465 }
3466 else
3467 ch_log(NULL, "terminal job finished");
3468 }
3469
3470 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3471 return FALSE;
3472}
3473
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003474#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3475/*
3476 * If the current window is a terminal in a popup window and the job has
3477 * finished, close the popup window and to back to the previous window.
3478 * Otherwise return FAIL.
3479 */
3480 int
3481may_close_term_popup(void)
3482{
3483 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3484 && !term_job_running(curbuf->b_term))
3485 {
3486 win_T *pwin = curwin;
3487
3488 if (win_valid(prevwin))
3489 win_enter(prevwin, FALSE);
3490 popup_close_with_retval(pwin, 0);
3491 return OK;
3492 }
3493 return FAIL;
3494}
3495#endif
3496
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003497/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003498 * Called when a channel has been closed.
3499 * If this was a channel for a terminal window then finish it up.
3500 */
3501 void
3502term_channel_closed(channel_T *ch)
3503{
3504 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003505 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003506 int did_one = FALSE;
3507
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003508 for (term = first_term; term != NULL; term = next_term)
3509 {
3510 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003511 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003512 {
3513 term->tl_channel_closed = TRUE;
3514 did_one = TRUE;
3515
Bram Moolenaard23a8232018-02-10 18:45:26 +01003516 VIM_CLEAR(term->tl_title);
3517 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003518#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003519 if (term->tl_out_fd != NULL)
3520 {
3521 fclose(term->tl_out_fd);
3522 term->tl_out_fd = NULL;
3523 }
3524#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003525
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003526 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003527 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003528 // Cannot open or close windows now. Can happen when
3529 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003530 term->tl_channel_recently_closed = TRUE;
3531 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003532 }
3533
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003534 if (term_after_channel_closed(term))
3535 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003536 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003537 }
3538
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003539 if (did_one)
3540 {
3541 redraw_statuslines();
3542
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003543 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003544 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003545 typebuf_was_filled = TRUE;
3546
3547 term = curbuf->b_term;
3548 if (term != NULL)
3549 {
3550 if (term->tl_job == ch->ch_job)
3551 maketitle();
3552 update_cursor(term, term->tl_cursor_visible);
3553 }
3554 }
3555}
3556
3557/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003558 * To be called after resetting updating_screen: handle any terminal where the
3559 * channel was closed.
3560 */
3561 void
3562term_check_channel_closed_recently()
3563{
3564 term_T *term;
3565 term_T *next_term;
3566
3567 for (term = first_term; term != NULL; term = next_term)
3568 {
3569 next_term = term->tl_next;
3570 if (term->tl_channel_recently_closed)
3571 {
3572 term->tl_channel_recently_closed = FALSE;
3573 if (term_after_channel_closed(term))
3574 // start over, the list may have changed
3575 next_term = first_term;
3576 }
3577 }
3578}
3579
3580/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003581 * Fill one screen line from a line of the terminal.
3582 * Advances "pos" to past the last column.
3583 */
3584 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003585term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003586 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003587 win_T *wp,
3588 VTermScreen *screen,
3589 VTermPos *pos,
3590 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003591{
3592 int off = screen_get_current_line_off();
3593
3594 for (pos->col = 0; pos->col < max_col; )
3595 {
3596 VTermScreenCell cell;
3597 int c;
3598
3599 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003600 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003601
3602 c = cell.chars[0];
3603 if (c == NUL)
3604 {
3605 ScreenLines[off] = ' ';
3606 if (enc_utf8)
3607 ScreenLinesUC[off] = NUL;
3608 }
3609 else
3610 {
3611 if (enc_utf8)
3612 {
3613 int i;
3614
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003615 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003616 for (i = 0; i < Screen_mco
3617 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3618 {
3619 ScreenLinesC[i][off] = cell.chars[i + 1];
3620 if (cell.chars[i + 1] == 0)
3621 break;
3622 }
3623 if (c >= 0x80 || (Screen_mco > 0
3624 && ScreenLinesC[0][off] != 0))
3625 {
3626 ScreenLines[off] = ' ';
3627 ScreenLinesUC[off] = c;
3628 }
3629 else
3630 {
3631 ScreenLines[off] = c;
3632 ScreenLinesUC[off] = NUL;
3633 }
3634 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003635#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003636 else if (has_mbyte && c >= 0x80)
3637 {
3638 char_u mb[MB_MAXBYTES+1];
3639 WCHAR wc = c;
3640
3641 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3642 (char*)mb, 2, 0, 0) > 1)
3643 {
3644 ScreenLines[off] = mb[0];
3645 ScreenLines[off + 1] = mb[1];
3646 cell.width = mb_ptr2cells(mb);
3647 }
3648 else
3649 ScreenLines[off] = c;
3650 }
3651#endif
3652 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003653 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003654 ScreenLines[off] = c;
3655 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003656 ScreenAttrs[off] = cell2attr(term, wp, cell.attrs, cell.fg, cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003657
3658 ++pos->col;
3659 ++off;
3660 if (cell.width == 2)
3661 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003662 // don't set the second byte to NUL for a DBCS encoding, it
3663 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003664 if (enc_utf8)
3665 {
3666 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003667 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003668 }
3669 else if (!has_mbyte)
3670 {
3671 // Can't show a double-width character with a single-byte
3672 // 'encoding', just use a space.
3673 ScreenLines[off] = ' ';
3674 ScreenAttrs[off] = ScreenAttrs[off - 1];
3675 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003676
3677 ++pos->col;
3678 ++off;
3679 }
3680 }
3681}
3682
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003683#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003684 static void
3685update_system_term(term_T *term)
3686{
3687 VTermPos pos;
3688 VTermScreen *screen;
3689
3690 if (term->tl_vterm == NULL)
3691 return;
3692 screen = vterm_obtain_screen(term->tl_vterm);
3693
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003694 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003695 while (term->tl_toprow > 0
3696 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3697 {
3698 int save_p_more = p_more;
3699
3700 p_more = FALSE;
3701 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003702 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003703 p_more = save_p_more;
3704 --term->tl_toprow;
3705 }
3706
3707 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3708 && pos.row < Rows; ++pos.row)
3709 {
3710 if (pos.row < term->tl_rows)
3711 {
3712 int max_col = MIN(Columns, term->tl_cols);
3713
Bram Moolenaar83d47902020-03-26 20:34:00 +01003714 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003715 }
3716 else
3717 pos.col = 0;
3718
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003719 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003720 }
3721
3722 term->tl_dirty_row_start = MAX_ROW;
3723 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003724}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003725#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003726
3727/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003728 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3729 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003730 * Terminal-Normal mode.
3731 */
3732 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003733term_do_update_window(win_T *wp)
3734{
3735 term_T *term = wp->w_buffer->b_term;
3736
3737 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3738}
3739
3740/*
3741 * Called to update a window that contains an active terminal.
3742 */
3743 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003744term_update_window(win_T *wp)
3745{
3746 term_T *term = wp->w_buffer->b_term;
3747 VTerm *vterm;
3748 VTermScreen *screen;
3749 VTermState *state;
3750 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003751 int rows, cols;
3752 int newrows, newcols;
3753 int minsize;
3754 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003755
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003756 vterm = term->tl_vterm;
3757 screen = vterm_obtain_screen(vterm);
3758 state = vterm_obtain_state(vterm);
3759
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003760 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3761 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003762 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003763 {
3764 term->tl_dirty_row_start = 0;
3765 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003766
3767 if (term->tl_postponed_scroll > 0
3768 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003769 // Scrolling is usually faster than redrawing, when there are only
3770 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003771 term_scroll_up(term, 0, term->tl_postponed_scroll);
3772 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003773 }
3774
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003775 /*
3776 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003777 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003778 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003779 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003780
Bram Moolenaar498c2562018-04-15 23:45:15 +02003781 newrows = 99999;
3782 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003783 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003784 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003785 // Always use curwin, it may be a popup window.
3786 win_T *wwp = twp == NULL ? curwin : twp;
3787
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003788 // When more than one window shows the same terminal, use the
3789 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003790 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003791 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003792 newrows = MIN(newrows, wwp->w_height);
3793 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003794 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003795 if (twp == NULL)
3796 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003797 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003798 if (newrows == 99999 || newcols == 99999)
3799 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003800 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3801 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3802
Bram Moolenaareba13e42021-02-23 17:47:23 +01003803 // If no cell is visible there is no point in resizing. Also, vterm can't
3804 // handle a zero height.
3805 if (newrows == 0 || newcols == 0)
3806 return;
3807
Bram Moolenaar498c2562018-04-15 23:45:15 +02003808 if (term->tl_rows != newrows || term->tl_cols != newcols)
3809 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003810 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003811 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003812 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003813 newrows);
3814 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003815
3816 // Updating the terminal size will cause the snapshot to be cleared.
3817 // When not in terminal_loop() we need to restore it.
3818 if (term != in_terminal_loop)
3819 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003820 }
3821
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003822 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003823 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003824 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003825
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003826 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3827 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003828 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003829 if (pos.row < term->tl_rows)
3830 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003831 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003832
Bram Moolenaar83d47902020-03-26 20:34:00 +01003833 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003834 }
3835 else
3836 pos.col = 0;
3837
Bram Moolenaarf118d482018-03-13 13:14:00 +01003838 screen_line(wp->w_winrow + pos.row
3839#ifdef FEAT_MENU
3840 + winbar_height(wp)
3841#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003842 , wp->w_wincol, pos.col, wp->w_width,
3843#ifdef FEAT_PROP_POPUP
3844 popup_is_popup(wp) ? SLF_POPUP :
3845#endif
3846 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003847 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003848 term->tl_dirty_row_start = MAX_ROW;
3849 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003850}
3851
3852/*
3853 * Return TRUE if "wp" is a terminal window where the job has finished.
3854 */
3855 int
3856term_is_finished(buf_T *buf)
3857{
3858 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3859}
3860
3861/*
3862 * Return TRUE if "wp" is a terminal window where the job has finished or we
3863 * are in Terminal-Normal mode, thus we show the buffer contents.
3864 */
3865 int
3866term_show_buffer(buf_T *buf)
3867{
3868 term_T *term = buf->b_term;
3869
3870 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3871}
3872
3873/*
3874 * The current buffer is going to be changed. If there is terminal
3875 * highlighting remove it now.
3876 */
3877 void
3878term_change_in_curbuf(void)
3879{
3880 term_T *term = curbuf->b_term;
3881
3882 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3883 {
3884 free_scrollback(term);
3885 redraw_buf_later(term->tl_buffer, NOT_VALID);
3886
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003887 // The buffer is now like a normal buffer, it cannot be easily
3888 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003889 set_string_option_direct((char_u *)"buftype", -1,
3890 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3891 }
3892}
3893
3894/*
3895 * Get the screen attribute for a position in the buffer.
3896 * Use a negative "col" to get the filler background color.
3897 */
3898 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003899term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003900{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003901 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003902 term_T *term = buf->b_term;
3903 sb_line_T *line;
3904 cellattr_T *cellattr;
3905
3906 if (lnum > term->tl_scrollback.ga_len)
3907 cellattr = &term->tl_default_color;
3908 else
3909 {
3910 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3911 if (col < 0 || col >= line->sb_cols)
3912 cellattr = &line->sb_fill_attr;
3913 else
3914 cellattr = line->sb_cells + col;
3915 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003916 return cell2attr(term, wp, cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003917}
3918
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003919/*
3920 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003921 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003922 */
3923 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003924cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003925{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003926 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3927 if (rgb->index == 0)
3928 rgb->type = VTERM_COLOR_RGB;
3929 else
3930 {
3931 rgb->type = VTERM_COLOR_INDEXED;
3932 --rgb->index;
3933 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003934}
3935
3936/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003937 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003938 */
3939 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003940init_default_colors(term_T *term, win_T *wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003941{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003942 VTermColor *fg, *bg;
3943 int fgval, bgval;
3944 int id;
3945
Bram Moolenaara80faa82020-04-12 19:37:17 +02003946 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003947 term->tl_default_color.width = 1;
3948 fg = &term->tl_default_color.fg;
3949 bg = &term->tl_default_color.bg;
3950
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003951 // Vterm uses a default black background. Set it to white when
3952 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003953 if (*p_bg == 'l')
3954 {
3955 fgval = 0;
3956 bgval = 255;
3957 }
3958 else
3959 {
3960 fgval = 255;
3961 bgval = 0;
3962 }
3963 fg->red = fg->green = fg->blue = fgval;
3964 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003965 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3966 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003967
Bram Moolenaar83d47902020-03-26 20:34:00 +01003968 // The 'wincolor' or the highlight group overrules the defaults.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003969 if (wp != NULL && *wp->w_p_wcr != NUL)
3970 id = syn_name2id(wp->w_p_wcr);
3971 else
Bram Moolenaar83d47902020-03-26 20:34:00 +01003972 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003973
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003974 // Use the actual color for the GUI and when 'termguicolors' is set.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003975#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3976 if (0
3977# ifdef FEAT_GUI
3978 || gui.in_use
3979# endif
3980# ifdef FEAT_TERMGUICOLORS
3981 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003982# ifdef FEAT_VTP
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003983 // Finally get INVALCOLOR on this execution path
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003984 || (!p_tgc && t_colors >= 256)
3985# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003986# endif
3987 )
3988 {
3989 guicolor_T fg_rgb = INVALCOLOR;
3990 guicolor_T bg_rgb = INVALCOLOR;
3991
3992 if (id != 0)
3993 syn_id2colors(id, &fg_rgb, &bg_rgb);
3994
3995# ifdef FEAT_GUI
3996 if (gui.in_use)
3997 {
3998 if (fg_rgb == INVALCOLOR)
3999 fg_rgb = gui.norm_pixel;
4000 if (bg_rgb == INVALCOLOR)
4001 bg_rgb = gui.back_pixel;
4002 }
4003# ifdef FEAT_TERMGUICOLORS
4004 else
4005# endif
4006# endif
4007# ifdef FEAT_TERMGUICOLORS
4008 {
4009 if (fg_rgb == INVALCOLOR)
4010 fg_rgb = cterm_normal_fg_gui_color;
4011 if (bg_rgb == INVALCOLOR)
4012 bg_rgb = cterm_normal_bg_gui_color;
4013 }
4014# endif
4015 if (fg_rgb != INVALCOLOR)
4016 {
4017 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
4018
4019 fg->red = (unsigned)(rgb >> 16);
4020 fg->green = (unsigned)(rgb >> 8) & 255;
4021 fg->blue = (unsigned)rgb & 255;
4022 }
4023 if (bg_rgb != INVALCOLOR)
4024 {
4025 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
4026
4027 bg->red = (unsigned)(rgb >> 16);
4028 bg->green = (unsigned)(rgb >> 8) & 255;
4029 bg->blue = (unsigned)rgb & 255;
4030 }
4031 }
4032 else
4033#endif
4034 if (id != 0 && t_colors >= 16)
4035 {
Bram Moolenaar83d47902020-03-26 20:34:00 +01004036 int cterm_fg = get_default_cterm_fg(term);
4037 int cterm_bg = get_default_cterm_bg(term);
4038
4039 if (cterm_fg >= 0)
4040 cterm_color2vterm(cterm_fg, fg);
4041 if (cterm_bg >= 0)
4042 cterm_color2vterm(cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004043 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004044 else
4045 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004046#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004047 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004048#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004049
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004050 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004051 if (cterm_normal_fg_color > 0)
4052 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004053 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004054# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4055# ifdef VIMDLL
4056 if (!gui.in_use)
4057# endif
4058 {
4059 tmp = fg->red;
4060 fg->red = fg->blue;
4061 fg->blue = tmp;
4062 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004063# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004064 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004065# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004066 else
4067 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004068# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004069
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004070 if (cterm_normal_bg_color > 0)
4071 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004072 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004073# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4074# ifdef VIMDLL
4075 if (!gui.in_use)
4076# endif
4077 {
4078 tmp = fg->red;
4079 fg->red = fg->blue;
4080 fg->blue = tmp;
4081 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004082# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004083 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004084# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004085 else
4086 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004087# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004088 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004089}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004090
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004091#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4092/*
4093 * Set the 16 ANSI colors from array of RGB values
4094 */
4095 static void
4096set_vterm_palette(VTerm *vterm, long_u *rgb)
4097{
4098 int index = 0;
4099 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004100
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004101 for (; index < 16; index++)
4102 {
4103 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004104
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004105 color.red = (unsigned)(rgb[index] >> 16);
4106 color.green = (unsigned)(rgb[index] >> 8) & 255;
4107 color.blue = (unsigned)rgb[index] & 255;
4108 vterm_state_set_palette_color(state, index, &color);
4109 }
4110}
4111
4112/*
4113 * Set the ANSI color palette from a list of colors
4114 */
4115 static int
4116set_ansi_colors_list(VTerm *vterm, list_T *list)
4117{
4118 int n = 0;
4119 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004120 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004121
Bram Moolenaarb0992022020-01-30 14:55:42 +01004122 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004123 {
4124 char_u *color_name;
4125 guicolor_T guicolor;
4126
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004127 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004128 if (color_name == NULL)
4129 return FAIL;
4130
4131 guicolor = GUI_GET_COLOR(color_name);
4132 if (guicolor == INVALCOLOR)
4133 return FAIL;
4134
4135 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4136 }
4137
4138 if (n != 16 || li != NULL)
4139 return FAIL;
4140
4141 set_vterm_palette(vterm, rgb);
4142
4143 return OK;
4144}
4145
4146/*
4147 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4148 */
4149 static void
4150init_vterm_ansi_colors(VTerm *vterm)
4151{
4152 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4153
4154 if (var != NULL
4155 && (var->di_tv.v_type != VAR_LIST
4156 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004157 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004158 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004159 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004160}
4161#endif
4162
Bram Moolenaar52acb112018-03-18 19:20:22 +01004163/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004164 * Handles a "drop" command from the job in the terminal.
4165 * "item" is the file name, "item->li_next" may have options.
4166 */
4167 static void
4168handle_drop_command(listitem_T *item)
4169{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004170 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004171 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004172 int bufnr;
4173 win_T *wp;
4174 tabpage_T *tp;
4175 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004176 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004177
4178 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4179 FOR_ALL_TAB_WINDOWS(tp, wp)
4180 {
4181 if (wp->w_buffer->b_fnum == bufnr)
4182 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004183 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004184 goto_tabpage_win(tp, wp);
4185 return;
4186 }
4187 }
4188
Bram Moolenaara80faa82020-04-12 19:37:17 +02004189 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004190
4191 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4192 && opt_item->li_tv.vval.v_dict != NULL)
4193 {
4194 dict_T *dict = opt_item->li_tv.vval.v_dict;
4195 char_u *p;
4196
Bram Moolenaar8f667172018-12-14 15:38:31 +01004197 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004198 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004199 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004200 if (p != NULL)
4201 {
4202 if (check_ff_value(p) == FAIL)
4203 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4204 else
4205 ea.force_ff = *p;
4206 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004207 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004208 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004209 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004210 if (p != NULL)
4211 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004212 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004213 if (ea.cmd != NULL)
4214 {
4215 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4216 ea.force_enc = 11;
4217 tofree = ea.cmd;
4218 }
4219 }
4220
Bram Moolenaar8f667172018-12-14 15:38:31 +01004221 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004222 if (p != NULL)
4223 get_bad_opt(p, &ea);
4224
4225 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4226 ea.force_bin = FORCE_BIN;
4227 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4228 ea.force_bin = FORCE_BIN;
4229 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4230 ea.force_bin = FORCE_NOBIN;
4231 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4232 ea.force_bin = FORCE_NOBIN;
4233 }
4234
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004235 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004236 if (ea.cmd == NULL)
4237 ea.cmd = (char_u *)"split";
4238 ea.arg = fname;
4239 ea.cmdidx = CMD_split;
4240 ex_splitview(&ea);
4241
4242 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004243}
4244
4245/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004246 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4247 */
4248 static int
4249is_permitted_term_api(char_u *func, char_u *pat)
4250{
4251 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4252}
4253
4254/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004255 * Handles a function call from the job running in a terminal.
4256 * "item" is the function name, "item->li_next" has the arguments.
4257 */
4258 static void
4259handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4260{
4261 char_u *func;
4262 typval_T argvars[2];
4263 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004264 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004265
4266 if (item->li_next == NULL)
4267 {
4268 ch_log(channel, "Missing function arguments for call");
4269 return;
4270 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004271 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004272
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004273 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004274 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004275 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004276 return;
4277 }
4278
4279 argvars[0].v_type = VAR_NUMBER;
4280 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4281 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004282 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004283 funcexe.firstline = 1L;
4284 funcexe.lastline = 1L;
4285 funcexe.evaluate = TRUE;
4286 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004287 {
4288 clear_tv(&rettv);
4289 ch_log(channel, "Function %s called", func);
4290 }
4291 else
4292 ch_log(channel, "Calling function %s failed", func);
4293}
4294
4295/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004296 * URL decoding (also know as Percent-encoding).
4297 *
4298 * Note this function currently is only used for decoding shell's
4299 * OSC 7 escape sequence which we can assume all bytes are valid
4300 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4301 * encoding bytes like 0xfe, 0xff.
4302 */
4303 static size_t
4304url_decode(const char *src, const size_t len, char_u *dst)
4305{
4306 size_t i = 0, j = 0;
4307
4308 while (i < len)
4309 {
4310 if (src[i] == '%' && i + 2 < len)
4311 {
4312 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4313 j++;
4314 i += 3;
4315 }
4316 else
4317 {
4318 dst[j] = src[i];
4319 i++;
4320 j++;
4321 }
4322 }
4323 dst[j] = '\0';
4324 return j;
4325}
4326
4327/*
4328 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4329 *
4330 * The OSC 7 sequence has the format of
4331 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4332 * and what VTerm provides via VTermStringFragment is
4333 * "file://HOSTNAME/CURRENT/DIR"
4334 */
4335 static void
4336sync_shell_dir(VTermStringFragment *frag)
4337{
4338 int offset = 7; // len of "file://" is 7
4339 char *pos = (char *)frag->str + offset;
4340 char_u *new_dir;
4341
4342 // remove HOSTNAME to get PWD
4343 while (*pos != '/' && offset < frag->len)
4344 {
4345 offset += 1;
4346 pos += 1;
4347 }
4348
4349 if (offset >= frag->len)
4350 {
4351 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
4352 frag->str);
4353 return;
4354 }
4355
4356 new_dir = alloc(frag->len - offset + 1);
4357 url_decode(pos, frag->len-offset, new_dir);
4358 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4359 vim_free(new_dir);
4360}
4361
4362/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004363 * Called by libvterm when it cannot recognize an OSC sequence.
4364 * We recognize a terminal API command.
4365 */
4366 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004367parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004368{
4369 term_T *term = (term_T *)user;
4370 js_read_T reader;
4371 typval_T tv;
4372 channel_T *channel = term->tl_job == NULL ? NULL
4373 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004374 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004375
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004376 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
4377 if (p_asd && command == 7)
4378 {
4379 sync_shell_dir(&frag);
4380 return 1;
4381 }
4382
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004383 if (command != 51)
4384 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004385
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004386 // Concatenate what was received until the final piece is found.
4387 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4388 {
4389 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004390 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004391 }
4392 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004393 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004394 if (!frag.final)
4395 return 1;
4396
4397 ((char *)gap->ga_data)[gap->ga_len] = 0;
4398 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004399 reader.js_fill = NULL;
4400 reader.js_used = 0;
4401 if (json_decode(&reader, &tv, 0) == OK
4402 && tv.v_type == VAR_LIST
4403 && tv.vval.v_list != NULL)
4404 {
4405 listitem_T *item = tv.vval.v_list->lv_first;
4406
4407 if (item == NULL)
4408 ch_log(channel, "Missing command");
4409 else
4410 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004411 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004412
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004413 // Make sure an invoked command doesn't delete the buffer (and the
4414 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004415 ++term->tl_buffer->b_locked;
4416
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004417 item = item->li_next;
4418 if (item == NULL)
4419 ch_log(channel, "Missing argument for %s", cmd);
4420 else if (STRCMP(cmd, "drop") == 0)
4421 handle_drop_command(item);
4422 else if (STRCMP(cmd, "call") == 0)
4423 handle_call_command(term, channel, item);
4424 else
4425 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004426 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004427 }
4428 }
4429 else
4430 ch_log(channel, "Invalid JSON received");
4431
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004432 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004433 clear_tv(&tv);
4434 return 1;
4435}
4436
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004437/*
4438 * Called by libvterm when it cannot recognize a CSI sequence.
4439 * We recognize the window position report.
4440 */
4441 static int
4442parse_csi(
4443 const char *leader UNUSED,
4444 const long args[],
4445 int argcount,
4446 const char *intermed UNUSED,
4447 char command,
4448 void *user)
4449{
4450 term_T *term = (term_T *)user;
4451 char buf[100];
4452 int len;
4453 int x = 0;
4454 int y = 0;
4455 win_T *wp;
4456
4457 // We recognize only CSI 13 t
4458 if (command != 't' || argcount != 1 || args[0] != 13)
4459 return 0; // not handled
4460
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004461 // When getting the window position is not possible or it fails it results
4462 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004463#if defined(FEAT_GUI) \
4464 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4465 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004466 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004467#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004468
4469 FOR_ALL_WINDOWS(wp)
4470 if (wp->w_buffer == term->tl_buffer)
4471 break;
4472 if (wp != NULL)
4473 {
4474#ifdef FEAT_GUI
4475 if (gui.in_use)
4476 {
4477 x += wp->w_wincol * gui.char_width;
4478 y += W_WINROW(wp) * gui.char_height;
4479 }
4480 else
4481#endif
4482 {
4483 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004484 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004485 x += wp->w_wincol * 7;
4486 y += W_WINROW(wp) * 10;
4487 }
4488 }
4489
4490 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4491 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4492 (char_u *)buf, len, NULL);
4493 return 1;
4494}
4495
Bram Moolenaard8637282020-05-20 18:41:41 +02004496static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004497 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004498 parse_csi, // csi
4499 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004500 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004501};
4502
4503/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004504 * Use Vim's allocation functions for vterm so profiling works.
4505 */
4506 static void *
4507vterm_malloc(size_t size, void *data UNUSED)
4508{
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004509 return alloc_clear(size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004510}
4511
4512 static void
4513vterm_memfree(void *ptr, void *data UNUSED)
4514{
4515 vim_free(ptr);
4516}
4517
4518static VTermAllocatorFunctions vterm_allocator = {
4519 &vterm_malloc,
4520 &vterm_memfree
4521};
4522
4523/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004524 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004525 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004526 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004527 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004528create_vterm(term_T *term, int rows, int cols)
4529{
4530 VTerm *vterm;
4531 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004532 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004533 VTermValue value;
4534
Bram Moolenaar756ef112018-04-10 12:04:27 +02004535 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004536 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004537 if (vterm == NULL)
4538 return FAIL;
4539
4540 // Allocate screen and state here, so we can bail out if that fails.
4541 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004542 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004543 if (state == NULL || screen == NULL)
4544 {
4545 vterm_free(vterm);
4546 return FAIL;
4547 }
4548
Bram Moolenaar52acb112018-03-18 19:20:22 +01004549 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004550 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004551 vterm_set_utf8(vterm, 1);
4552
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004553 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004554
4555 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004556 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004557 &term->tl_default_color.fg,
4558 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004559
Bram Moolenaar9e587872019-05-13 20:27:23 +02004560 if (t_colors < 16)
4561 // Less than 16 colors: assume that bold means using a bright color for
4562 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004563 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4564
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004565 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004566 vterm_screen_reset(screen, 1 /* hard */);
4567
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004568 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004569 vterm_screen_enable_altscreen(screen, 1);
4570
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004571 // For unix do not use a blinking cursor. In an xterm this causes the
4572 // cursor to blink if it's blinking in the xterm.
4573 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004574#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004575 if (GetCaretBlinkTime() == INFINITE)
4576 value.boolean = 0;
4577 else
4578 value.boolean = 1;
4579#else
4580 value.boolean = 0;
4581#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004582 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004583 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004584
4585 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004586}
4587
4588/*
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004589 * Called when 'wincolor' was set.
4590 */
4591 void
4592term_update_colors(void)
4593{
4594 term_T *term = curwin->w_buffer->b_term;
4595
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01004596 if (term->tl_vterm == NULL)
4597 return;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004598 init_default_colors(term, curwin);
4599 vterm_state_set_default_colors(
4600 vterm_obtain_state(term->tl_vterm),
4601 &term->tl_default_color.fg,
4602 &term->tl_default_color.bg);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01004603
4604 redraw_later(NOT_VALID);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004605}
4606
4607/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004608 * Return the text to show for the buffer name and status.
4609 */
4610 char_u *
4611term_get_status_text(term_T *term)
4612{
4613 if (term->tl_status_text == NULL)
4614 {
4615 char_u *txt;
4616 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004617 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004618
4619 if (term->tl_normal_mode)
4620 {
4621 if (term_job_running(term))
4622 txt = (char_u *)_("Terminal");
4623 else
4624 txt = (char_u *)_("Terminal-finished");
4625 }
4626 else if (term->tl_title != NULL)
4627 txt = term->tl_title;
4628 else if (term_none_open(term))
4629 txt = (char_u *)_("active");
4630 else if (term_job_running(term))
4631 txt = (char_u *)_("running");
4632 else
4633 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004634 fname = buf_get_fname(term->tl_buffer);
4635 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004636 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004637 if (term->tl_status_text != NULL)
4638 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004639 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004640 }
4641 return term->tl_status_text;
4642}
4643
4644/*
4645 * Mark references in jobs of terminals.
4646 */
4647 int
4648set_ref_in_term(int copyID)
4649{
4650 int abort = FALSE;
4651 term_T *term;
4652 typval_T tv;
4653
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004654 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004655 if (term->tl_job != NULL)
4656 {
4657 tv.v_type = VAR_JOB;
4658 tv.vval.v_job = term->tl_job;
4659 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4660 }
4661 return abort;
4662}
4663
4664/*
4665 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004666 * Returns NULL when the buffer is not for a terminal window and logs a message
4667 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004668 */
4669 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004670term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004671{
4672 buf_T *buf;
4673
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004674 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004675 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004676 --emsg_off;
4677 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004678 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004679 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004680 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004681 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004682 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004683 return buf;
4684}
4685
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004686 static void
4687clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004688{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004689 CLEAR_FIELD(*cell);
4690 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4691 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004692}
4693
4694 static void
4695dump_term_color(FILE *fd, VTermColor *color)
4696{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004697 int index;
4698
4699 if (VTERM_COLOR_IS_INDEXED(color))
4700 index = color->index + 1;
4701 else if (color->type == 0)
4702 // use RGB values
4703 index = 255;
4704 else
4705 // default color
4706 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004707 fprintf(fd, "%02x%02x%02x%d",
4708 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004709 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004710}
4711
4712/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004713 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004714 *
4715 * Each screen cell in full is:
4716 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4717 * {characters} is a space for an empty cell
4718 * For a double-width character "+" is changed to "*" and the next cell is
4719 * skipped.
4720 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4721 * when "&" use the same as the previous cell.
4722 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4723 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4724 * {color-idx} is a number from 0 to 255
4725 *
4726 * Screen cell with same width, attributes and color as the previous one:
4727 * |{characters}
4728 *
4729 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4730 *
4731 * Repeating the previous screen cell:
4732 * @{count}
4733 */
4734 void
4735f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4736{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004737 buf_T *buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004738 term_T *term;
4739 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004740 int max_height = 0;
4741 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004742 stat_T st;
4743 FILE *fd;
4744 VTermPos pos;
4745 VTermScreen *screen;
4746 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004747 VTermState *state;
4748 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004749
4750 if (check_restricted() || check_secure())
4751 return;
4752 if (buf == NULL)
4753 return;
4754 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004755 if (term->tl_vterm == NULL)
4756 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004757 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004758 return;
4759 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004760
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004761 if (argvars[2].v_type != VAR_UNKNOWN)
4762 {
4763 dict_T *d;
4764
4765 if (argvars[2].v_type != VAR_DICT)
4766 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004767 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004768 return;
4769 }
4770 d = argvars[2].vval.v_dict;
4771 if (d != NULL)
4772 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004773 max_height = dict_get_number(d, (char_u *)"rows");
4774 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004775 }
4776 }
4777
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004778 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004779 if (fname == NULL)
4780 return;
4781 if (mch_stat((char *)fname, &st) >= 0)
4782 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004783 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004784 return;
4785 }
4786
Bram Moolenaard96ff162018-02-18 22:13:29 +01004787 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4788 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004789 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004790 return;
4791 }
4792
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004793 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004794
4795 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004796 state = vterm_obtain_state(term->tl_vterm);
4797 vterm_state_get_cursorpos(state, &cursor_pos);
4798
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004799 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4800 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004801 {
4802 int repeat = 0;
4803
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004804 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4805 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004806 {
4807 VTermScreenCell cell;
4808 int same_attr;
4809 int same_chars = TRUE;
4810 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004811 int is_cursor_pos = (pos.col == cursor_pos.col
4812 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004813
4814 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004815 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004816
4817 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4818 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004819 int c = cell.chars[i];
4820 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004821 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004822
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004823 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004824 if (i == 0)
4825 {
4826 c = (c == NUL) ? ' ' : c;
4827 pc = (pc == NUL) ? ' ' : pc;
4828 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004829 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004830 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004831 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004832 break;
4833 }
4834 same_attr = vtermAttr2hl(cell.attrs)
4835 == vtermAttr2hl(prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004836 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4837 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004838 if (same_chars && cell.width == prev_cell.width && same_attr
4839 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004840 {
4841 ++repeat;
4842 }
4843 else
4844 {
4845 if (repeat > 0)
4846 {
4847 fprintf(fd, "@%d", repeat);
4848 repeat = 0;
4849 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004850 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004851
4852 if (cell.chars[0] == NUL)
4853 fputs(" ", fd);
4854 else
4855 {
4856 char_u charbuf[10];
4857 int len;
4858
4859 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4860 && cell.chars[i] != NUL; ++i)
4861 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004862 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004863 fwrite(charbuf, len, 1, fd);
4864 }
4865 }
4866
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004867 // When only the characters differ we don't write anything, the
4868 // following "|", "@" or NL will indicate using the same
4869 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004870 if (cell.width != prev_cell.width || !same_attr)
4871 {
4872 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004873 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004874 else
4875 fputs("+", fd);
4876
4877 if (same_attr)
4878 {
4879 fputs("&", fd);
4880 }
4881 else
4882 {
4883 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004884 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004885 fputs("&", fd);
4886 else
4887 {
4888 fputs("#", fd);
4889 dump_term_color(fd, &cell.fg);
4890 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004891 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004892 fputs("&", fd);
4893 else
4894 {
4895 fputs("#", fd);
4896 dump_term_color(fd, &cell.bg);
4897 }
4898 }
4899 }
4900
4901 prev_cell = cell;
4902 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004903
4904 if (cell.width == 2)
4905 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004906 }
4907 if (repeat > 0)
4908 fprintf(fd, "@%d", repeat);
4909 fputs("\n", fd);
4910 }
4911
4912 fclose(fd);
4913}
4914
4915/*
4916 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4917 */
4918 static void
4919dump_is_corrupt(garray_T *gap)
4920{
4921 ga_concat(gap, (char_u *)"CORRUPT");
4922}
4923
4924 static void
4925append_cell(garray_T *gap, cellattr_T *cell)
4926{
4927 if (ga_grow(gap, 1) == OK)
4928 {
4929 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4930 ++gap->ga_len;
4931 }
4932}
4933
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004934 static void
4935clear_cellattr(cellattr_T *cell)
4936{
4937 CLEAR_FIELD(*cell);
4938 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4939 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4940}
4941
Bram Moolenaard96ff162018-02-18 22:13:29 +01004942/*
4943 * Read the dump file from "fd" and append lines to the current buffer.
4944 * Return the cell width of the longest line.
4945 */
4946 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004947read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004948{
4949 int c;
4950 garray_T ga_text;
4951 garray_T ga_cell;
4952 char_u *prev_char = NULL;
4953 int attr = 0;
4954 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004955 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004956 term_T *term = curbuf->b_term;
4957 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004958 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004959
4960 ga_init2(&ga_text, 1, 90);
4961 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004962 clear_cellattr(&cell);
4963 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004964 cursor_pos->row = -1;
4965 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004966
4967 c = fgetc(fd);
4968 for (;;)
4969 {
4970 if (c == EOF)
4971 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004972 if (c == '\r')
4973 {
4974 // DOS line endings? Ignore.
4975 c = fgetc(fd);
4976 }
4977 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004978 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004979 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004980 if (ga_text.ga_data == NULL)
4981 dump_is_corrupt(&ga_text);
4982 if (ga_grow(&term->tl_scrollback, 1) == OK)
4983 {
4984 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
4985 + term->tl_scrollback.ga_len;
4986
4987 if (max_cells < ga_cell.ga_len)
4988 max_cells = ga_cell.ga_len;
4989 line->sb_cols = ga_cell.ga_len;
4990 line->sb_cells = ga_cell.ga_data;
4991 line->sb_fill_attr = term->tl_default_color;
4992 ++term->tl_scrollback.ga_len;
4993 ga_init(&ga_cell);
4994
4995 ga_append(&ga_text, NUL);
4996 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4997 ga_text.ga_len, FALSE);
4998 }
4999 else
5000 ga_clear(&ga_cell);
5001 ga_text.ga_len = 0;
5002
5003 c = fgetc(fd);
5004 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005005 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005006 {
5007 int prev_len = ga_text.ga_len;
5008
Bram Moolenaar9271d052018-02-25 21:39:46 +01005009 if (c == '>')
5010 {
5011 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005012 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005013 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5014 cursor_pos->col = ga_cell.ga_len;
5015 }
5016
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005017 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005018 c = fgetc(fd);
5019 if (c != EOF)
5020 ga_append(&ga_text, c);
5021 for (;;)
5022 {
5023 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005024 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005025 || c == EOF || c == '\n')
5026 break;
5027 ga_append(&ga_text, c);
5028 }
5029
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005030 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005031 vim_free(prev_char);
5032 if (ga_text.ga_data != NULL)
5033 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5034 ga_text.ga_len - prev_len);
5035
Bram Moolenaar9271d052018-02-25 21:39:46 +01005036 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005037 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005038 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005039 }
5040 else if (c == '+' || c == '*')
5041 {
5042 int is_bg;
5043
5044 cell.width = c == '+' ? 1 : 2;
5045
5046 c = fgetc(fd);
5047 if (c == '&')
5048 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005049 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005050 c = fgetc(fd);
5051 }
5052 else if (isdigit(c))
5053 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005054 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005055 attr = 0;
5056 while (isdigit(c))
5057 {
5058 attr = attr * 10 + (c - '0');
5059 c = fgetc(fd);
5060 }
5061 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005062
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005063 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005064 for (is_bg = 0; is_bg <= 1; ++is_bg)
5065 {
5066 if (c == '&')
5067 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005068 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005069 c = fgetc(fd);
5070 }
5071 else if (c == '#')
5072 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005073 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005074
5075 c = fgetc(fd);
5076 red = hex2nr(c);
5077 c = fgetc(fd);
5078 red = (red << 4) + hex2nr(c);
5079 c = fgetc(fd);
5080 green = hex2nr(c);
5081 c = fgetc(fd);
5082 green = (green << 4) + hex2nr(c);
5083 c = fgetc(fd);
5084 blue = hex2nr(c);
5085 c = fgetc(fd);
5086 blue = (blue << 4) + hex2nr(c);
5087 c = fgetc(fd);
5088 if (!isdigit(c))
5089 dump_is_corrupt(&ga_text);
5090 while (isdigit(c))
5091 {
5092 index = index * 10 + (c - '0');
5093 c = fgetc(fd);
5094 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005095 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005096 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005097 type = VTERM_COLOR_RGB;
5098 if (index == 0)
5099 {
5100 if (is_bg)
5101 type |= VTERM_COLOR_DEFAULT_BG;
5102 else
5103 type |= VTERM_COLOR_DEFAULT_FG;
5104 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005105 }
5106 else
5107 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005108 type = VTERM_COLOR_INDEXED;
5109 index -= 1;
5110 }
5111 if (is_bg)
5112 {
5113 cell.bg.type = type;
5114 cell.bg.red = red;
5115 cell.bg.green = green;
5116 cell.bg.blue = blue;
5117 cell.bg.index = index;
5118 }
5119 else
5120 {
5121 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005122 cell.fg.red = red;
5123 cell.fg.green = green;
5124 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005125 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005126 }
5127 }
5128 else
5129 dump_is_corrupt(&ga_text);
5130 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005131 }
5132 else
5133 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005134 }
5135 else
5136 dump_is_corrupt(&ga_text);
5137
5138 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005139 if (cell.width == 2)
5140 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005141 }
5142 else if (c == '@')
5143 {
5144 if (prev_char == NULL)
5145 dump_is_corrupt(&ga_text);
5146 else
5147 {
5148 int count = 0;
5149
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005150 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005151 for (;;)
5152 {
5153 c = fgetc(fd);
5154 if (!isdigit(c))
5155 break;
5156 count = count * 10 + (c - '0');
5157 }
5158
5159 while (count-- > 0)
5160 {
5161 ga_concat(&ga_text, prev_char);
5162 append_cell(&ga_cell, &cell);
5163 }
5164 }
5165 }
5166 else
5167 {
5168 dump_is_corrupt(&ga_text);
5169 c = fgetc(fd);
5170 }
5171 }
5172
5173 if (ga_text.ga_len > 0)
5174 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005175 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005176 dump_is_corrupt(&ga_text);
5177 ga_append(&ga_text, NUL);
5178 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5179 ga_text.ga_len, FALSE);
5180 }
5181
5182 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005183 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005184 vim_free(prev_char);
5185
5186 return max_cells;
5187}
5188
5189/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005190 * Return an allocated string with at least "text_width" "=" characters and
5191 * "fname" inserted in the middle.
5192 */
5193 static char_u *
5194get_separator(int text_width, char_u *fname)
5195{
5196 int width = MAX(text_width, curwin->w_width);
5197 char_u *textline;
5198 int fname_size;
5199 char_u *p = fname;
5200 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005201 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005202
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005203 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005204 if (textline == NULL)
5205 return NULL;
5206
5207 fname_size = vim_strsize(fname);
5208 if (fname_size < width - 8)
5209 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005210 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005211 width = MAX(text_width, fname_size + 8);
5212 }
5213 else if (fname_size > width - 8)
5214 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005215 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005216 p = gettail(fname);
5217 fname_size = vim_strsize(p);
5218 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005219 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005220 while (fname_size > width - 8)
5221 {
5222 p += (*mb_ptr2len)(p);
5223 fname_size = vim_strsize(p);
5224 }
5225
5226 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5227 textline[i] = '=';
5228 textline[i++] = ' ';
5229
5230 STRCPY(textline + i, p);
5231 off = STRLEN(textline);
5232 textline[off] = ' ';
5233 for (i = 1; i < (width - fname_size) / 2; ++i)
5234 textline[off + i] = '=';
5235 textline[off + i] = NUL;
5236
5237 return textline;
5238}
5239
5240/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005241 * Common for "term_dumpdiff()" and "term_dumpload()".
5242 */
5243 static void
5244term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5245{
5246 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005247 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005248 char_u buf1[NUMBUFLEN];
5249 char_u buf2[NUMBUFLEN];
5250 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005251 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005252 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005253 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005254 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005255 char_u *textline = NULL;
5256
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005257 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005258 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005259 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005260 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005261 if (fname1 == NULL || (do_diff && fname2 == NULL))
5262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005263 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005264 return;
5265 }
5266 fd1 = mch_fopen((char *)fname1, READBIN);
5267 if (fd1 == NULL)
5268 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005269 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005270 return;
5271 }
5272 if (do_diff)
5273 {
5274 fd2 = mch_fopen((char *)fname2, READBIN);
5275 if (fd2 == NULL)
5276 {
5277 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005278 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005279 return;
5280 }
5281 }
5282
5283 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005284 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5285 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5286 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5287 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5288 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005289
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005290 if (opt.jo_term_name == NULL)
5291 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005292 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005293
Bram Moolenaar51e14382019-05-25 20:21:28 +02005294 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005295 if (fname_tofree != NULL)
5296 {
5297 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5298 opt.jo_term_name = fname_tofree;
5299 }
5300 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005301
Bram Moolenaar87abab92019-06-03 21:14:59 +02005302 if (opt.jo_bufnr_buf != NULL)
5303 {
5304 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5305
5306 // With "bufnr" argument: enter the window with this buffer and make it
5307 // empty.
5308 if (wp == NULL)
5309 semsg(_(e_invarg2), "bufnr");
5310 else
5311 {
5312 buf = curbuf;
5313 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005314 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005315 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005316 redraw_later(NOT_VALID);
5317 }
5318 }
5319 else
5320 // Create a new terminal window.
5321 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5322
Bram Moolenaard96ff162018-02-18 22:13:29 +01005323 if (buf != NULL && buf->b_term != NULL)
5324 {
5325 int i;
5326 linenr_T bot_lnum;
5327 linenr_T lnum;
5328 term_T *term = buf->b_term;
5329 int width;
5330 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005331 VTermPos cursor_pos1;
5332 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005333
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005334 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005335
Bram Moolenaard96ff162018-02-18 22:13:29 +01005336 rettv->vval.v_number = buf->b_fnum;
5337
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005338 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005339 width = read_dump_file(fd1, &cursor_pos1);
5340
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005341 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005342 if (cursor_pos1.row >= 0)
5343 {
5344 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5345 coladvance(cursor_pos1.col);
5346 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005347
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005348 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005349 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005350
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005351 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005352 if (!do_diff)
5353 goto theend;
5354
5355 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5356
Bram Moolenaar4a696342018-04-05 18:45:26 +02005357 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005358 if (textline == NULL)
5359 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005360 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5361 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5362 vim_free(textline);
5363
5364 textline = get_separator(width, fname2);
5365 if (textline == NULL)
5366 goto theend;
5367 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5368 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005369 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005370
5371 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005372 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005373 if (width2 > width)
5374 {
5375 vim_free(textline);
5376 textline = alloc(width2 + 1);
5377 if (textline == NULL)
5378 goto theend;
5379 width = width2;
5380 textline[width] = NUL;
5381 }
5382 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5383
5384 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5385 {
5386 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5387 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005388 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005389 for (i = 0; i < width; ++i)
5390 textline[i] = '-';
5391 }
5392 else
5393 {
5394 char_u *line1;
5395 char_u *line2;
5396 char_u *p1;
5397 char_u *p2;
5398 int col;
5399 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5400 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5401 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5402 ->sb_cells;
5403
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005404 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005405 line1 = vim_strsave(ml_get(lnum));
5406 if (line1 == NULL)
5407 break;
5408 p1 = line1;
5409
5410 line2 = ml_get(lnum + bot_lnum);
5411 p2 = line2;
5412 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5413 {
5414 int len1 = utfc_ptr2len(p1);
5415 int len2 = utfc_ptr2len(p2);
5416
5417 textline[col] = ' ';
5418 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005419 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005420 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005421 else if (lnum == cursor_pos1.row + 1
5422 && col == cursor_pos1.col
5423 && (cursor_pos1.row != cursor_pos2.row
5424 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005425 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005426 textline[col] = '>';
5427 else if (lnum == cursor_pos2.row + 1
5428 && col == cursor_pos2.col
5429 && (cursor_pos1.row != cursor_pos2.row
5430 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005431 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005432 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005433 else if (cellattr1 != NULL && cellattr2 != NULL)
5434 {
5435 if ((cellattr1 + col)->width
5436 != (cellattr2 + col)->width)
5437 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005438 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005439 &(cellattr2 + col)->fg))
5440 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005441 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005442 &(cellattr2 + col)->bg))
5443 textline[col] = 'b';
5444 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5445 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5446 textline[col] = 'a';
5447 }
5448 p1 += len1;
5449 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005450 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005451 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005452
5453 while (col < width)
5454 {
5455 if (*p1 == NUL && *p2 == NUL)
5456 textline[col] = '?';
5457 else if (*p1 == NUL)
5458 {
5459 textline[col] = '+';
5460 p2 += utfc_ptr2len(p2);
5461 }
5462 else
5463 {
5464 textline[col] = '-';
5465 p1 += utfc_ptr2len(p1);
5466 }
5467 ++col;
5468 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005469
5470 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005471 }
5472 if (add_empty_scrollback(term, &term->tl_default_color,
5473 term->tl_top_diff_rows) == OK)
5474 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5475 ++bot_lnum;
5476 }
5477
5478 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5479 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005480 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005481 for (i = 0; i < width; ++i)
5482 textline[i] = '+';
5483 if (add_empty_scrollback(term, &term->tl_default_color,
5484 term->tl_top_diff_rows) == OK)
5485 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5486 ++lnum;
5487 ++bot_lnum;
5488 }
5489
5490 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005491
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005492 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005493 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005494 }
5495
5496theend:
5497 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005498 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005499 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005500 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005501 fclose(fd2);
5502}
5503
5504/*
5505 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5506 * bottom files.
5507 * Return FAIL when this is not possible.
5508 */
5509 int
5510term_swap_diff()
5511{
5512 term_T *term = curbuf->b_term;
5513 linenr_T line_count;
5514 linenr_T top_rows;
5515 linenr_T bot_rows;
5516 linenr_T bot_start;
5517 linenr_T lnum;
5518 char_u *p;
5519 sb_line_T *sb_line;
5520
5521 if (term == NULL
5522 || !term_is_finished(curbuf)
5523 || term->tl_top_diff_rows == 0
5524 || term->tl_scrollback.ga_len == 0)
5525 return FAIL;
5526
5527 line_count = curbuf->b_ml.ml_line_count;
5528 top_rows = term->tl_top_diff_rows;
5529 bot_rows = term->tl_bot_diff_rows;
5530 bot_start = line_count - bot_rows;
5531 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5532
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005533 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005534 for (lnum = 1; lnum <= top_rows; ++lnum)
5535 {
5536 p = vim_strsave(ml_get(1));
5537 if (p == NULL)
5538 return OK;
5539 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005540 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005541 vim_free(p);
5542 }
5543
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005544 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005545 for (lnum = 1; lnum <= bot_rows; ++lnum)
5546 {
5547 p = vim_strsave(ml_get(bot_start + lnum));
5548 if (p == NULL)
5549 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005550 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005551 ml_append(lnum - 1, p, 0, FALSE);
5552 vim_free(p);
5553 }
5554
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005555 // move top title to bottom
5556 p = vim_strsave(ml_get(bot_rows + 1));
5557 if (p == NULL)
5558 return OK;
5559 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005560 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005561 vim_free(p);
5562
5563 // move bottom title to top
5564 p = vim_strsave(ml_get(line_count - top_rows));
5565 if (p == NULL)
5566 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005567 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005568 ml_append(bot_rows, p, 0, FALSE);
5569 vim_free(p);
5570
Bram Moolenaard96ff162018-02-18 22:13:29 +01005571 if (top_rows == bot_rows)
5572 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005573 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005574 for (lnum = 0; lnum < top_rows; ++lnum)
5575 {
5576 sb_line_T temp;
5577
5578 temp = *(sb_line + lnum);
5579 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5580 *(sb_line + bot_start + lnum) = temp;
5581 }
5582 }
5583 else
5584 {
5585 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005586 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005587
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005588 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005589 if (temp != NULL)
5590 {
5591 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5592 mch_memmove(term->tl_scrollback.ga_data,
5593 temp + bot_start,
5594 sizeof(sb_line_T) * bot_rows);
5595 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5596 temp + top_rows,
5597 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5598 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5599 + line_count - top_rows,
5600 temp,
5601 sizeof(sb_line_T) * top_rows);
5602 vim_free(temp);
5603 }
5604 }
5605
5606 term->tl_top_diff_rows = bot_rows;
5607 term->tl_bot_diff_rows = top_rows;
5608
5609 update_screen(NOT_VALID);
5610 return OK;
5611}
5612
5613/*
5614 * "term_dumpdiff(filename, filename, options)" function
5615 */
5616 void
5617f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5618{
5619 term_load_dump(argvars, rettv, TRUE);
5620}
5621
5622/*
5623 * "term_dumpload(filename, options)" function
5624 */
5625 void
5626f_term_dumpload(typval_T *argvars, typval_T *rettv)
5627{
5628 term_load_dump(argvars, rettv, FALSE);
5629}
5630
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005631/*
5632 * "term_getaltscreen(buf)" function
5633 */
5634 void
5635f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5636{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005637 buf_T *buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005638
5639 if (buf == NULL)
5640 return;
5641 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5642}
5643
5644/*
5645 * "term_getattr(attr, name)" function
5646 */
5647 void
5648f_term_getattr(typval_T *argvars, typval_T *rettv)
5649{
5650 int attr;
5651 size_t i;
5652 char_u *name;
5653
5654 static struct {
5655 char *name;
5656 int attr;
5657 } attrs[] = {
5658 {"bold", HL_BOLD},
5659 {"italic", HL_ITALIC},
5660 {"underline", HL_UNDERLINE},
5661 {"strike", HL_STRIKETHROUGH},
5662 {"reverse", HL_INVERSE},
5663 };
5664
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005665 attr = tv_get_number(&argvars[0]);
5666 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005667 if (name == NULL)
5668 return;
5669
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005670 if (attr > HL_ALL)
5671 attr = syn_attr2attr(attr);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005672 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
5673 if (STRCMP(name, attrs[i].name) == 0)
5674 {
5675 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5676 break;
5677 }
5678}
5679
5680/*
5681 * "term_getcursor(buf)" function
5682 */
5683 void
5684f_term_getcursor(typval_T *argvars, typval_T *rettv)
5685{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005686 buf_T *buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005687 term_T *term;
5688 list_T *l;
5689 dict_T *d;
5690
5691 if (rettv_list_alloc(rettv) == FAIL)
5692 return;
5693 if (buf == NULL)
5694 return;
5695 term = buf->b_term;
5696
5697 l = rettv->vval.v_list;
5698 list_append_number(l, term->tl_cursor_pos.row + 1);
5699 list_append_number(l, term->tl_cursor_pos.col + 1);
5700
5701 d = dict_alloc();
5702 if (d != NULL)
5703 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005704 dict_add_number(d, "visible", term->tl_cursor_visible);
5705 dict_add_number(d, "blink", blink_state_is_inverted()
5706 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5707 dict_add_number(d, "shape", term->tl_cursor_shape);
5708 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005709 list_append_dict(l, d);
5710 }
5711}
5712
5713/*
5714 * "term_getjob(buf)" function
5715 */
5716 void
5717f_term_getjob(typval_T *argvars, typval_T *rettv)
5718{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005719 buf_T *buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005720
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005721 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005722 {
5723 rettv->v_type = VAR_SPECIAL;
5724 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005725 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005726 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005727
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005728 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005729 rettv->vval.v_job = buf->b_term->tl_job;
5730 if (rettv->vval.v_job != NULL)
5731 ++rettv->vval.v_job->jv_refcount;
5732}
5733
5734 static int
5735get_row_number(typval_T *tv, term_T *term)
5736{
5737 if (tv->v_type == VAR_STRING
5738 && tv->vval.v_string != NULL
5739 && STRCMP(tv->vval.v_string, ".") == 0)
5740 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005741 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005742}
5743
5744/*
5745 * "term_getline(buf, row)" function
5746 */
5747 void
5748f_term_getline(typval_T *argvars, typval_T *rettv)
5749{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005750 buf_T *buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005751 term_T *term;
5752 int row;
5753
5754 rettv->v_type = VAR_STRING;
5755 if (buf == NULL)
5756 return;
5757 term = buf->b_term;
5758 row = get_row_number(&argvars[1], term);
5759
5760 if (term->tl_vterm == NULL)
5761 {
5762 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5763
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005764 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005765 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5766 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5767 }
5768 else
5769 {
5770 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5771 VTermRect rect;
5772 int len;
5773 char_u *p;
5774
5775 if (row < 0 || row >= term->tl_rows)
5776 return;
5777 len = term->tl_cols * MB_MAXBYTES + 1;
5778 p = alloc(len);
5779 if (p == NULL)
5780 return;
5781 rettv->vval.v_string = p;
5782
5783 rect.start_col = 0;
5784 rect.end_col = term->tl_cols;
5785 rect.start_row = row;
5786 rect.end_row = row + 1;
5787 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5788 }
5789}
5790
5791/*
5792 * "term_getscrolled(buf)" function
5793 */
5794 void
5795f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5796{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005797 buf_T *buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005798
5799 if (buf == NULL)
5800 return;
5801 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5802}
5803
5804/*
5805 * "term_getsize(buf)" function
5806 */
5807 void
5808f_term_getsize(typval_T *argvars, typval_T *rettv)
5809{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005810 buf_T *buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005811 list_T *l;
5812
5813 if (rettv_list_alloc(rettv) == FAIL)
5814 return;
5815 if (buf == NULL)
5816 return;
5817
5818 l = rettv->vval.v_list;
5819 list_append_number(l, buf->b_term->tl_rows);
5820 list_append_number(l, buf->b_term->tl_cols);
5821}
5822
5823/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005824 * "term_setsize(buf, rows, cols)" function
5825 */
5826 void
5827f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5828{
5829 buf_T *buf = term_get_buf(argvars, "term_setsize()");
5830 term_T *term;
5831 varnumber_T rows, cols;
5832
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005833 if (buf == NULL)
5834 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005835 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005836 return;
5837 }
5838 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005839 return;
5840 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005841 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005842 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005843 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005844 cols = cols <= 0 ? term->tl_cols : cols;
5845 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005846 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005847
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005848 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005849 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5850 term_report_winsize(term, term->tl_rows, term->tl_cols);
5851}
5852
5853/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005854 * "term_getstatus(buf)" function
5855 */
5856 void
5857f_term_getstatus(typval_T *argvars, typval_T *rettv)
5858{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005859 buf_T *buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005860 term_T *term;
5861 char_u val[100];
5862
5863 rettv->v_type = VAR_STRING;
5864 if (buf == NULL)
5865 return;
5866 term = buf->b_term;
5867
5868 if (term_job_running(term))
5869 STRCPY(val, "running");
5870 else
5871 STRCPY(val, "finished");
5872 if (term->tl_normal_mode)
5873 STRCAT(val, ",normal");
5874 rettv->vval.v_string = vim_strsave(val);
5875}
5876
5877/*
5878 * "term_gettitle(buf)" function
5879 */
5880 void
5881f_term_gettitle(typval_T *argvars, typval_T *rettv)
5882{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005883 buf_T *buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005884
5885 rettv->v_type = VAR_STRING;
5886 if (buf == NULL)
5887 return;
5888
5889 if (buf->b_term->tl_title != NULL)
5890 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5891}
5892
5893/*
5894 * "term_gettty(buf)" function
5895 */
5896 void
5897f_term_gettty(typval_T *argvars, typval_T *rettv)
5898{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005899 buf_T *buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005900 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005901 int num = 0;
5902
5903 rettv->v_type = VAR_STRING;
5904 if (buf == NULL)
5905 return;
5906 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02005907 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005908
5909 switch (num)
5910 {
5911 case 0:
5912 if (buf->b_term->tl_job != NULL)
5913 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005914 break;
5915 case 1:
5916 if (buf->b_term->tl_job != NULL)
5917 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005918 break;
5919 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005920 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005921 return;
5922 }
5923 if (p != NULL)
5924 rettv->vval.v_string = vim_strsave(p);
5925}
5926
5927/*
5928 * "term_list()" function
5929 */
5930 void
5931f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
5932{
5933 term_T *tp;
5934 list_T *l;
5935
5936 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
5937 return;
5938
5939 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005940 FOR_ALL_TERMS(tp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005941 if (tp != NULL && tp->tl_buffer != NULL)
5942 if (list_append_number(l,
5943 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
5944 return;
5945}
5946
5947/*
5948 * "term_scrape(buf, row)" function
5949 */
5950 void
5951f_term_scrape(typval_T *argvars, typval_T *rettv)
5952{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005953 buf_T *buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005954 VTermScreen *screen = NULL;
5955 VTermPos pos;
5956 list_T *l;
5957 term_T *term;
5958 char_u *p;
5959 sb_line_T *line;
5960
5961 if (rettv_list_alloc(rettv) == FAIL)
5962 return;
5963 if (buf == NULL)
5964 return;
5965 term = buf->b_term;
5966
5967 l = rettv->vval.v_list;
5968 pos.row = get_row_number(&argvars[1], term);
5969
5970 if (term->tl_vterm != NULL)
5971 {
5972 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01005973 if (screen == NULL) // can't really happen
5974 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005975 p = NULL;
5976 line = NULL;
5977 }
5978 else
5979 {
5980 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
5981
5982 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
5983 return;
5984 p = ml_get_buf(buf, lnum + 1, FALSE);
5985 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
5986 }
5987
5988 for (pos.col = 0; pos.col < term->tl_cols; )
5989 {
5990 dict_T *dcell;
5991 int width;
5992 VTermScreenCellAttrs attrs;
5993 VTermColor fg, bg;
5994 char_u rgb[8];
5995 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
5996 int off = 0;
5997 int i;
5998
5999 if (screen == NULL)
6000 {
6001 cellattr_T *cellattr;
6002 int len;
6003
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006004 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006005 if (pos.col >= line->sb_cols)
6006 break;
6007 cellattr = line->sb_cells + pos.col;
6008 width = cellattr->width;
6009 attrs = cellattr->attrs;
6010 fg = cellattr->fg;
6011 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006012 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006013 mch_memmove(mbs, p, len);
6014 mbs[len] = NUL;
6015 p += len;
6016 }
6017 else
6018 {
6019 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006020
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006021 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6022 break;
6023 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6024 {
6025 if (cell.chars[i] == 0)
6026 break;
6027 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6028 }
6029 mbs[off] = NUL;
6030 width = cell.width;
6031 attrs = cell.attrs;
6032 fg = cell.fg;
6033 bg = cell.bg;
6034 }
6035 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006036 if (dcell == NULL)
6037 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006038 list_append_dict(l, dcell);
6039
Bram Moolenaare0be1672018-07-08 16:50:37 +02006040 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006041
6042 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6043 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006044 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006045 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6046 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006047 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006048
Bram Moolenaar83d47902020-03-26 20:34:00 +01006049 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006050 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006051
6052 ++pos.col;
6053 if (width == 2)
6054 ++pos.col;
6055 }
6056}
6057
6058/*
6059 * "term_sendkeys(buf, keys)" function
6060 */
6061 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006062f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006063{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006064 buf_T *buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006065 char_u *msg;
6066 term_T *term;
6067
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006068 if (buf == NULL)
6069 return;
6070
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006071 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006072 if (msg == NULL)
6073 return;
6074 term = buf->b_term;
6075 if (term->tl_vterm == NULL)
6076 return;
6077
6078 while (*msg != NUL)
6079 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006080 int c;
6081
6082 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6083 {
6084 c = TO_SPECIAL(msg[1], msg[2]);
6085 msg += 3;
6086 }
6087 else
6088 {
6089 c = PTR2CHAR(msg);
6090 msg += MB_CPTR2LEN(msg);
6091 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006092 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006093 }
6094}
6095
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006096#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6097/*
6098 * "term_getansicolors(buf)" function
6099 */
6100 void
6101f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6102{
6103 buf_T *buf = term_get_buf(argvars, "term_getansicolors()");
6104 term_T *term;
6105 VTermState *state;
6106 VTermColor color;
6107 char_u hexbuf[10];
6108 int index;
6109 list_T *list;
6110
6111 if (rettv_list_alloc(rettv) == FAIL)
6112 return;
6113
6114 if (buf == NULL)
6115 return;
6116 term = buf->b_term;
6117 if (term->tl_vterm == NULL)
6118 return;
6119
6120 list = rettv->vval.v_list;
6121 state = vterm_obtain_state(term->tl_vterm);
6122 for (index = 0; index < 16; index++)
6123 {
6124 vterm_state_get_palette_color(state, index, &color);
6125 sprintf((char *)hexbuf, "#%02x%02x%02x",
6126 color.red, color.green, color.blue);
6127 if (list_append_string(list, hexbuf, 7) == FAIL)
6128 return;
6129 }
6130}
6131
6132/*
6133 * "term_setansicolors(buf, list)" function
6134 */
6135 void
6136f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6137{
6138 buf_T *buf = term_get_buf(argvars, "term_setansicolors()");
6139 term_T *term;
6140
6141 if (buf == NULL)
6142 return;
6143 term = buf->b_term;
6144 if (term->tl_vterm == NULL)
6145 return;
6146
6147 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006149 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006150 return;
6151 }
6152
6153 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006154 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006155}
6156#endif
6157
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006158/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006159 * "term_setapi(buf, api)" function
6160 */
6161 void
6162f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6163{
6164 buf_T *buf = term_get_buf(argvars, "term_setapi()");
6165 term_T *term;
6166 char_u *api;
6167
6168 if (buf == NULL)
6169 return;
6170 term = buf->b_term;
6171 vim_free(term->tl_api);
6172 api = tv_get_string_chk(&argvars[1]);
6173 if (api != NULL)
6174 term->tl_api = vim_strsave(api);
6175 else
6176 term->tl_api = NULL;
6177}
6178
6179/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006180 * "term_setrestore(buf, command)" function
6181 */
6182 void
6183f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6184{
6185#if defined(FEAT_SESSION)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006186 buf_T *buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006187 term_T *term;
6188 char_u *cmd;
6189
6190 if (buf == NULL)
6191 return;
6192 term = buf->b_term;
6193 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006194 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006195 if (cmd != NULL)
6196 term->tl_command = vim_strsave(cmd);
6197 else
6198 term->tl_command = NULL;
6199#endif
6200}
6201
6202/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006203 * "term_setkill(buf, how)" function
6204 */
6205 void
6206f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6207{
6208 buf_T *buf = term_get_buf(argvars, "term_setkill()");
6209 term_T *term;
6210 char_u *how;
6211
6212 if (buf == NULL)
6213 return;
6214 term = buf->b_term;
6215 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006216 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006217 if (how != NULL)
6218 term->tl_kill = vim_strsave(how);
6219 else
6220 term->tl_kill = NULL;
6221}
6222
6223/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006224 * "term_start(command, options)" function
6225 */
6226 void
6227f_term_start(typval_T *argvars, typval_T *rettv)
6228{
6229 jobopt_T opt;
6230 buf_T *buf;
6231
6232 init_job_options(&opt);
6233 if (argvars[1].v_type != VAR_UNKNOWN
6234 && get_job_options(&argvars[1], &opt,
6235 JO_TIMEOUT_ALL + JO_STOPONEXIT
6236 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6237 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6238 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6239 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006240 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006241 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006242 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006243 return;
6244
Bram Moolenaar13568252018-03-16 20:46:58 +01006245 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006246
6247 if (buf != NULL && buf->b_term != NULL)
6248 rettv->vval.v_number = buf->b_fnum;
6249}
6250
6251/*
6252 * "term_wait" function
6253 */
6254 void
6255f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6256{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006257 buf_T *buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006258
6259 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006260 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006261 if (buf->b_term->tl_job == NULL)
6262 {
6263 ch_log(NULL, "term_wait(): no job to wait for");
6264 return;
6265 }
6266 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006267 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006268 return;
6269
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006270 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006271 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006272 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6273 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006274 // The job is dead, keep reading channel I/O until the channel is
6275 // closed. buf->b_term may become NULL if the terminal was closed while
6276 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006277 ch_log(NULL, "term_wait(): waiting for channel to close");
6278 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6279 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006280 term_flush_messages();
6281
Bram Moolenaard45aa552018-05-21 22:50:29 +02006282 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006283 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006284 // If the terminal is closed when the channel is closed the
6285 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006286 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006287 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006288
6289 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006290 }
6291 else
6292 {
6293 long wait = 10L;
6294
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006295 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006296
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006297 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006298 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006299 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006300 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006301
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006302 // Flushing messages on channels is hopefully sufficient.
6303 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006304 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006305 }
6306}
6307
6308/*
6309 * Called when a channel has sent all the lines to a terminal.
6310 * Send a CTRL-D to mark the end of the text.
6311 */
6312 void
6313term_send_eof(channel_T *ch)
6314{
6315 term_T *term;
6316
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006317 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006318 if (term->tl_job == ch->ch_job)
6319 {
6320 if (term->tl_eof_chars != NULL)
6321 {
6322 channel_send(ch, PART_IN, term->tl_eof_chars,
6323 (int)STRLEN(term->tl_eof_chars), NULL);
6324 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6325 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006326# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006327 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006328 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006329 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6330# endif
6331 }
6332}
6333
Bram Moolenaar113e1072019-01-20 15:30:40 +01006334#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006335 job_T *
6336term_getjob(term_T *term)
6337{
6338 return term != NULL ? term->tl_job : NULL;
6339}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006340#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006341
Bram Moolenaar4f974752019-02-17 17:44:42 +01006342# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006343
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006344///////////////////////////////////////
6345// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006346#ifdef PROTO
6347typedef int COORD;
6348typedef int DWORD;
6349typedef int HANDLE;
6350typedef int *DWORD_PTR;
6351typedef int HPCON;
6352typedef int HRESULT;
6353typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006354typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006355typedef int PSIZE_T;
6356typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006357typedef int BOOL;
6358# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006359#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006360
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006361HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6362HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6363HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006364BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6365BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6366void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006367
6368 static int
6369dyn_conpty_init(int verbose)
6370{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006371 static HMODULE hKerneldll = NULL;
6372 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006373 static struct
6374 {
6375 char *name;
6376 FARPROC *ptr;
6377 } conpty_entry[] =
6378 {
6379 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6380 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6381 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6382 {"InitializeProcThreadAttributeList",
6383 (FARPROC*)&pInitializeProcThreadAttributeList},
6384 {"UpdateProcThreadAttribute",
6385 (FARPROC*)&pUpdateProcThreadAttribute},
6386 {"DeleteProcThreadAttributeList",
6387 (FARPROC*)&pDeleteProcThreadAttributeList},
6388 {NULL, NULL}
6389 };
6390
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006391 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006392 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006393 if (verbose)
6394 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006395 return FAIL;
6396 }
6397
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006398 // No need to initialize twice.
6399 if (hKerneldll)
6400 return OK;
6401
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006402 hKerneldll = vimLoadLib("kernel32.dll");
6403 for (i = 0; conpty_entry[i].name != NULL
6404 && conpty_entry[i].ptr != NULL; ++i)
6405 {
6406 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6407 conpty_entry[i].name)) == NULL)
6408 {
6409 if (verbose)
6410 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006411 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006412 return FAIL;
6413 }
6414 }
6415
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006416 return OK;
6417}
6418
6419 static int
6420conpty_term_and_job_init(
6421 term_T *term,
6422 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006423 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006424 jobopt_T *opt,
6425 jobopt_T *orig_opt)
6426{
6427 WCHAR *cmd_wchar = NULL;
6428 WCHAR *cmd_wchar_copy = NULL;
6429 WCHAR *cwd_wchar = NULL;
6430 WCHAR *env_wchar = NULL;
6431 channel_T *channel = NULL;
6432 job_T *job = NULL;
6433 HANDLE jo = NULL;
6434 garray_T ga_cmd, ga_env;
6435 char_u *cmd = NULL;
6436 HRESULT hr;
6437 COORD consize;
6438 SIZE_T breq;
6439 PROCESS_INFORMATION proc_info;
6440 HANDLE i_theirs = NULL;
6441 HANDLE o_theirs = NULL;
6442 HANDLE i_ours = NULL;
6443 HANDLE o_ours = NULL;
6444
6445 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6446 ga_init2(&ga_env, (int)sizeof(char*), 20);
6447
6448 if (argvar->v_type == VAR_STRING)
6449 {
6450 cmd = argvar->vval.v_string;
6451 }
6452 else if (argvar->v_type == VAR_LIST)
6453 {
6454 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6455 goto failed;
6456 cmd = ga_cmd.ga_data;
6457 }
6458 if (cmd == NULL || *cmd == NUL)
6459 {
6460 emsg(_(e_invarg));
6461 goto failed;
6462 }
6463
6464 term->tl_arg0_cmd = vim_strsave(cmd);
6465
6466 cmd_wchar = enc_to_utf16(cmd, NULL);
6467
6468 if (cmd_wchar != NULL)
6469 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006470 // Request by CreateProcessW
6471 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006472 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006473 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6474 }
6475
6476 ga_clear(&ga_cmd);
6477 if (cmd_wchar == NULL)
6478 goto failed;
6479 if (opt->jo_cwd != NULL)
6480 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6481
6482 win32_build_env(opt->jo_env, &ga_env, TRUE);
6483 env_wchar = ga_env.ga_data;
6484
6485 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6486 goto failed;
6487 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6488 goto failed;
6489
6490 consize.X = term->tl_cols;
6491 consize.Y = term->tl_rows;
6492 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6493 &term->tl_conpty);
6494 if (FAILED(hr))
6495 goto failed;
6496
6497 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6498
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006499 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006500 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006501 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006502 if (!term->tl_siex.lpAttributeList)
6503 goto failed;
6504 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6505 0, &breq))
6506 goto failed;
6507 if (!pUpdateProcThreadAttribute(
6508 term->tl_siex.lpAttributeList, 0,
6509 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6510 sizeof(HPCON), NULL, NULL))
6511 goto failed;
6512
6513 channel = add_channel();
6514 if (channel == NULL)
6515 goto failed;
6516
6517 job = job_alloc();
6518 if (job == NULL)
6519 goto failed;
6520 if (argvar->v_type == VAR_STRING)
6521 {
6522 int argc;
6523
6524 build_argv_from_string(cmd, &job->jv_argv, &argc);
6525 }
6526 else
6527 {
6528 int argc;
6529
6530 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6531 }
6532
6533 if (opt->jo_set & JO_IN_BUF)
6534 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6535
6536 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6537 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006538 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006539 env_wchar, cwd_wchar,
6540 &term->tl_siex.StartupInfo, &proc_info))
6541 goto failed;
6542
6543 CloseHandle(i_theirs);
6544 CloseHandle(o_theirs);
6545
6546 channel_set_pipes(channel,
6547 (sock_T)i_ours,
6548 (sock_T)o_ours,
6549 (sock_T)o_ours);
6550
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006551 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006552 channel->ch_write_text_mode = TRUE;
6553
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006554 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006555 channel->ch_anonymous_pipe = TRUE;
6556
6557 jo = CreateJobObject(NULL, NULL);
6558 if (jo == NULL)
6559 goto failed;
6560
6561 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6562 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006563 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006564 CloseHandle(jo);
6565 jo = NULL;
6566 }
6567
6568 ResumeThread(proc_info.hThread);
6569 CloseHandle(proc_info.hThread);
6570
6571 vim_free(cmd_wchar);
6572 vim_free(cmd_wchar_copy);
6573 vim_free(cwd_wchar);
6574 vim_free(env_wchar);
6575
6576 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6577 goto failed;
6578
6579#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6580 if (opt->jo_set2 & JO2_ANSI_COLORS)
6581 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6582 else
6583 init_vterm_ansi_colors(term->tl_vterm);
6584#endif
6585
6586 channel_set_job(channel, job, opt);
6587 job_set_options(job, opt);
6588
6589 job->jv_channel = channel;
6590 job->jv_proc_info = proc_info;
6591 job->jv_job_object = jo;
6592 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006593 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006594 ++job->jv_refcount;
6595 term->tl_job = job;
6596
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006597 // Redirecting stdout and stderr doesn't work at the job level. Instead
6598 // open the file here and handle it in. opt->jo_io was changed in
6599 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006600 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6601 {
6602 char_u *fname = opt->jo_io_name[PART_OUT];
6603
6604 ch_log(channel, "Opening output file %s", fname);
6605 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6606 if (term->tl_out_fd == NULL)
6607 semsg(_(e_notopen), fname);
6608 }
6609
6610 return OK;
6611
6612failed:
6613 ga_clear(&ga_cmd);
6614 ga_clear(&ga_env);
6615 vim_free(cmd_wchar);
6616 vim_free(cmd_wchar_copy);
6617 vim_free(cwd_wchar);
6618 if (channel != NULL)
6619 channel_clear(channel);
6620 if (job != NULL)
6621 {
6622 job->jv_channel = NULL;
6623 job_cleanup(job);
6624 }
6625 term->tl_job = NULL;
6626 if (jo != NULL)
6627 CloseHandle(jo);
6628
6629 if (term->tl_siex.lpAttributeList != NULL)
6630 {
6631 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6632 vim_free(term->tl_siex.lpAttributeList);
6633 }
6634 term->tl_siex.lpAttributeList = NULL;
6635 if (o_theirs != NULL)
6636 CloseHandle(o_theirs);
6637 if (o_ours != NULL)
6638 CloseHandle(o_ours);
6639 if (i_ours != NULL)
6640 CloseHandle(i_ours);
6641 if (i_theirs != NULL)
6642 CloseHandle(i_theirs);
6643 if (term->tl_conpty != NULL)
6644 pClosePseudoConsole(term->tl_conpty);
6645 term->tl_conpty = NULL;
6646 return FAIL;
6647}
6648
6649 static void
6650conpty_term_report_winsize(term_T *term, int rows, int cols)
6651{
6652 COORD consize;
6653
6654 consize.X = cols;
6655 consize.Y = rows;
6656 pResizePseudoConsole(term->tl_conpty, consize);
6657}
6658
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006659 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006660term_free_conpty(term_T *term)
6661{
6662 if (term->tl_siex.lpAttributeList != NULL)
6663 {
6664 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6665 vim_free(term->tl_siex.lpAttributeList);
6666 }
6667 term->tl_siex.lpAttributeList = NULL;
6668 if (term->tl_conpty != NULL)
6669 pClosePseudoConsole(term->tl_conpty);
6670 term->tl_conpty = NULL;
6671}
6672
6673 int
6674use_conpty(void)
6675{
6676 return has_conpty;
6677}
6678
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006679# ifndef PROTO
6680
6681#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6682#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006683#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006684
6685void* (*winpty_config_new)(UINT64, void*);
6686void* (*winpty_open)(void*, void*);
6687void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6688BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6689void (*winpty_config_set_mouse_mode)(void*, int);
6690void (*winpty_config_set_initial_size)(void*, int, int);
6691LPCWSTR (*winpty_conin_name)(void*);
6692LPCWSTR (*winpty_conout_name)(void*);
6693LPCWSTR (*winpty_conerr_name)(void*);
6694void (*winpty_free)(void*);
6695void (*winpty_config_free)(void*);
6696void (*winpty_spawn_config_free)(void*);
6697void (*winpty_error_free)(void*);
6698LPCWSTR (*winpty_error_msg)(void*);
6699BOOL (*winpty_set_size)(void*, int, int, void*);
6700HANDLE (*winpty_agent_process)(void*);
6701
6702#define WINPTY_DLL "winpty.dll"
6703
6704static HINSTANCE hWinPtyDLL = NULL;
6705# endif
6706
6707 static int
6708dyn_winpty_init(int verbose)
6709{
6710 int i;
6711 static struct
6712 {
6713 char *name;
6714 FARPROC *ptr;
6715 } winpty_entry[] =
6716 {
6717 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6718 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6719 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6720 {"winpty_config_set_mouse_mode",
6721 (FARPROC*)&winpty_config_set_mouse_mode},
6722 {"winpty_config_set_initial_size",
6723 (FARPROC*)&winpty_config_set_initial_size},
6724 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6725 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6726 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6727 {"winpty_free", (FARPROC*)&winpty_free},
6728 {"winpty_open", (FARPROC*)&winpty_open},
6729 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6730 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6731 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6732 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6733 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6734 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6735 {NULL, NULL}
6736 };
6737
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006738 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006739 if (hWinPtyDLL)
6740 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006741 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6742 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006743 if (*p_winptydll != NUL)
6744 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6745 if (!hWinPtyDLL)
6746 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6747 if (!hWinPtyDLL)
6748 {
6749 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006750 semsg(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006751 : (char_u *)WINPTY_DLL);
6752 return FAIL;
6753 }
6754 for (i = 0; winpty_entry[i].name != NULL
6755 && winpty_entry[i].ptr != NULL; ++i)
6756 {
6757 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6758 winpty_entry[i].name)) == NULL)
6759 {
6760 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006761 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006762 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006763 return FAIL;
6764 }
6765 }
6766
6767 return OK;
6768}
6769
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006770 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006771winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006772 term_T *term,
6773 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006774 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006775 jobopt_T *opt,
6776 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006777{
6778 WCHAR *cmd_wchar = NULL;
6779 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006780 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006781 channel_T *channel = NULL;
6782 job_T *job = NULL;
6783 DWORD error;
6784 HANDLE jo = NULL;
6785 HANDLE child_process_handle;
6786 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006787 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006788 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006789 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006790 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006791
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006792 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6793 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006794
6795 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006796 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006797 cmd = argvar->vval.v_string;
6798 }
6799 else if (argvar->v_type == VAR_LIST)
6800 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006801 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006802 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006803 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006804 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006805 if (cmd == NULL || *cmd == NUL)
6806 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006807 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006808 goto failed;
6809 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006810
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006811 term->tl_arg0_cmd = vim_strsave(cmd);
6812
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006813 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006814 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006815 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006816 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006817 if (opt->jo_cwd != NULL)
6818 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006819
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006820 win32_build_env(opt->jo_env, &ga_env, TRUE);
6821 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006822
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006823 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6824 if (term->tl_winpty_config == NULL)
6825 goto failed;
6826
6827 winpty_config_set_mouse_mode(term->tl_winpty_config,
6828 WINPTY_MOUSE_MODE_FORCE);
6829 winpty_config_set_initial_size(term->tl_winpty_config,
6830 term->tl_cols, term->tl_rows);
6831 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6832 if (term->tl_winpty == NULL)
6833 goto failed;
6834
6835 spawn_config = winpty_spawn_config_new(
6836 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6837 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6838 NULL,
6839 cmd_wchar,
6840 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006841 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006842 &winpty_err);
6843 if (spawn_config == NULL)
6844 goto failed;
6845
6846 channel = add_channel();
6847 if (channel == NULL)
6848 goto failed;
6849
6850 job = job_alloc();
6851 if (job == NULL)
6852 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006853 if (argvar->v_type == VAR_STRING)
6854 {
6855 int argc;
6856
6857 build_argv_from_string(cmd, &job->jv_argv, &argc);
6858 }
6859 else
6860 {
6861 int argc;
6862
6863 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6864 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006865
6866 if (opt->jo_set & JO_IN_BUF)
6867 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6868
6869 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
6870 &child_thread_handle, &error, &winpty_err))
6871 goto failed;
6872
6873 channel_set_pipes(channel,
6874 (sock_T)CreateFileW(
6875 winpty_conin_name(term->tl_winpty),
6876 GENERIC_WRITE, 0, NULL,
6877 OPEN_EXISTING, 0, NULL),
6878 (sock_T)CreateFileW(
6879 winpty_conout_name(term->tl_winpty),
6880 GENERIC_READ, 0, NULL,
6881 OPEN_EXISTING, 0, NULL),
6882 (sock_T)CreateFileW(
6883 winpty_conerr_name(term->tl_winpty),
6884 GENERIC_READ, 0, NULL,
6885 OPEN_EXISTING, 0, NULL));
6886
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006887 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006888 channel->ch_write_text_mode = TRUE;
6889
6890 jo = CreateJobObject(NULL, NULL);
6891 if (jo == NULL)
6892 goto failed;
6893
6894 if (!AssignProcessToJobObject(jo, child_process_handle))
6895 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006896 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006897 CloseHandle(jo);
6898 jo = NULL;
6899 }
6900
6901 winpty_spawn_config_free(spawn_config);
6902 vim_free(cmd_wchar);
6903 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006904 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006905
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006906 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6907 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006908
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006909#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6910 if (opt->jo_set2 & JO2_ANSI_COLORS)
6911 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6912 else
6913 init_vterm_ansi_colors(term->tl_vterm);
6914#endif
6915
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006916 channel_set_job(channel, job, opt);
6917 job_set_options(job, opt);
6918
6919 job->jv_channel = channel;
6920 job->jv_proc_info.hProcess = child_process_handle;
6921 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
6922 job->jv_job_object = jo;
6923 job->jv_status = JOB_STARTED;
6924 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006925 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006926 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006927 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006928 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006929 ++job->jv_refcount;
6930 term->tl_job = job;
6931
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006932 // Redirecting stdout and stderr doesn't work at the job level. Instead
6933 // open the file here and handle it in. opt->jo_io was changed in
6934 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006935 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6936 {
6937 char_u *fname = opt->jo_io_name[PART_OUT];
6938
6939 ch_log(channel, "Opening output file %s", fname);
6940 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6941 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006942 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006943 }
6944
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006945 return OK;
6946
6947failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006948 ga_clear(&ga_cmd);
6949 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006950 vim_free(cmd_wchar);
6951 vim_free(cwd_wchar);
6952 if (spawn_config != NULL)
6953 winpty_spawn_config_free(spawn_config);
6954 if (channel != NULL)
6955 channel_clear(channel);
6956 if (job != NULL)
6957 {
6958 job->jv_channel = NULL;
6959 job_cleanup(job);
6960 }
6961 term->tl_job = NULL;
6962 if (jo != NULL)
6963 CloseHandle(jo);
6964 if (term->tl_winpty != NULL)
6965 winpty_free(term->tl_winpty);
6966 term->tl_winpty = NULL;
6967 if (term->tl_winpty_config != NULL)
6968 winpty_config_free(term->tl_winpty_config);
6969 term->tl_winpty_config = NULL;
6970 if (winpty_err != NULL)
6971 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006972 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006973 (short_u *)winpty_error_msg(winpty_err), NULL);
6974
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006975 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006976 winpty_error_free(winpty_err);
6977 }
6978 return FAIL;
6979}
6980
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006981/*
6982 * Create a new terminal of "rows" by "cols" cells.
6983 * Store a reference in "term".
6984 * Return OK or FAIL.
6985 */
6986 static int
6987term_and_job_init(
6988 term_T *term,
6989 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01006990 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006991 jobopt_T *opt,
6992 jobopt_T *orig_opt)
6993{
6994 int use_winpty = FALSE;
6995 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006996 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006997
6998 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
6999 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7000
7001 if (!has_winpty && !has_conpty)
7002 // If neither is available give the errors for winpty, since when
7003 // conpty is not available it can't be installed either.
7004 return dyn_winpty_init(TRUE);
7005
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007006 if (opt->jo_tty_type != NUL)
7007 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007008
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007009 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007010 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007011 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007012 use_conpty = TRUE;
7013 else if (has_winpty)
7014 use_winpty = TRUE;
7015 // else: error
7016 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007017 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007018 {
7019 if (has_winpty)
7020 use_winpty = TRUE;
7021 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007022 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007023 {
7024 if (has_conpty)
7025 use_conpty = TRUE;
7026 else
7027 return dyn_conpty_init(TRUE);
7028 }
7029
7030 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007031 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007032
7033 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007034 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007035
7036 // error
7037 return dyn_winpty_init(TRUE);
7038}
7039
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007040 static int
7041create_pty_only(term_T *term, jobopt_T *options)
7042{
7043 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7044 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7045 char in_name[80], out_name[80];
7046 channel_T *channel = NULL;
7047
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007048 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7049 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007050
7051 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7052 GetCurrentProcessId(),
7053 curbuf->b_fnum);
7054 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7055 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7056 PIPE_UNLIMITED_INSTANCES,
7057 0, 0, NMPWAIT_NOWAIT, NULL);
7058 if (hPipeIn == INVALID_HANDLE_VALUE)
7059 goto failed;
7060
7061 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7062 GetCurrentProcessId(),
7063 curbuf->b_fnum);
7064 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7065 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7066 PIPE_UNLIMITED_INSTANCES,
7067 0, 0, 0, NULL);
7068 if (hPipeOut == INVALID_HANDLE_VALUE)
7069 goto failed;
7070
7071 ConnectNamedPipe(hPipeIn, NULL);
7072 ConnectNamedPipe(hPipeOut, NULL);
7073
7074 term->tl_job = job_alloc();
7075 if (term->tl_job == NULL)
7076 goto failed;
7077 ++term->tl_job->jv_refcount;
7078
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007079 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007080 term->tl_job->jv_status = JOB_FINISHED;
7081
7082 channel = add_channel();
7083 if (channel == NULL)
7084 goto failed;
7085 term->tl_job->jv_channel = channel;
7086 channel->ch_keep_open = TRUE;
7087 channel->ch_named_pipe = TRUE;
7088
7089 channel_set_pipes(channel,
7090 (sock_T)hPipeIn,
7091 (sock_T)hPipeOut,
7092 (sock_T)hPipeOut);
7093 channel_set_job(channel, term->tl_job, options);
7094 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7095 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7096
7097 return OK;
7098
7099failed:
7100 if (hPipeIn != NULL)
7101 CloseHandle(hPipeIn);
7102 if (hPipeOut != NULL)
7103 CloseHandle(hPipeOut);
7104 return FAIL;
7105}
7106
7107/*
7108 * Free the terminal emulator part of "term".
7109 */
7110 static void
7111term_free_vterm(term_T *term)
7112{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007113 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007114 if (term->tl_winpty != NULL)
7115 winpty_free(term->tl_winpty);
7116 term->tl_winpty = NULL;
7117 if (term->tl_winpty_config != NULL)
7118 winpty_config_free(term->tl_winpty_config);
7119 term->tl_winpty_config = NULL;
7120 if (term->tl_vterm != NULL)
7121 vterm_free(term->tl_vterm);
7122 term->tl_vterm = NULL;
7123}
7124
7125/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007126 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007127 */
7128 static void
7129term_report_winsize(term_T *term, int rows, int cols)
7130{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007131 if (term->tl_conpty)
7132 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007133 if (term->tl_winpty)
7134 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7135}
7136
7137 int
7138terminal_enabled(void)
7139{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007140 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007141}
7142
7143# else
7144
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007145///////////////////////////////////////
7146// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007147
7148/*
7149 * Create a new terminal of "rows" by "cols" cells.
7150 * Start job for "cmd".
7151 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007152 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007153 * Return OK or FAIL.
7154 */
7155 static int
7156term_and_job_init(
7157 term_T *term,
7158 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007159 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007160 jobopt_T *opt,
7161 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007162{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007163 term->tl_arg0_cmd = NULL;
7164
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007165 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7166 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007167
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007168#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7169 if (opt->jo_set2 & JO2_ANSI_COLORS)
7170 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7171 else
7172 init_vterm_ansi_colors(term->tl_vterm);
7173#endif
7174
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007175 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007176 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007177 if (term->tl_job != NULL)
7178 ++term->tl_job->jv_refcount;
7179
7180 return term->tl_job != NULL
7181 && term->tl_job->jv_channel != NULL
7182 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7183}
7184
7185 static int
7186create_pty_only(term_T *term, jobopt_T *opt)
7187{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007188 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7189 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007190
7191 term->tl_job = job_alloc();
7192 if (term->tl_job == NULL)
7193 return FAIL;
7194 ++term->tl_job->jv_refcount;
7195
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007196 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007197 term->tl_job->jv_status = JOB_FINISHED;
7198
7199 return mch_create_pty_channel(term->tl_job, opt);
7200}
7201
7202/*
7203 * Free the terminal emulator part of "term".
7204 */
7205 static void
7206term_free_vterm(term_T *term)
7207{
7208 if (term->tl_vterm != NULL)
7209 vterm_free(term->tl_vterm);
7210 term->tl_vterm = NULL;
7211}
7212
7213/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007214 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007215 */
7216 static void
7217term_report_winsize(term_T *term, int rows, int cols)
7218{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007219 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007220 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7221 {
7222 int fd = -1;
7223 int part;
7224
7225 for (part = PART_OUT; part < PART_COUNT; ++part)
7226 {
7227 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007228 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007229 break;
7230 }
7231 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7232 mch_signal_job(term->tl_job, (char_u *)"winch");
7233 }
7234}
7235
7236# endif
7237
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007238#endif // FEAT_TERMINAL