blob: 9db04756d67d6a6509250d334bcb7a422e85e18b [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;
Bram Moolenaare5b44862021-05-30 13:54:03 +0200448#ifdef FEAT_CMDWIN
449 if (cmdwin_type != 0)
450 {
451 emsg(_(e_cannot_open_terminal_from_command_line_window));
452 return NULL;
453 }
454#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200455
456 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
457 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
458 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
Bram Moolenaarb0992022020-01-30 14:55:42 +0100459 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))
460 || (argvar != NULL
461 && argvar->v_type == VAR_LIST
462 && argvar->vval.v_list != NULL
463 && argvar->vval.v_list->lv_first == &range_list_item))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200464 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100465 emsg(_(e_invarg));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200466 return NULL;
467 }
468
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200469 term = ALLOC_CLEAR_ONE(term_T);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200470 if (term == NULL)
471 return NULL;
472 term->tl_dirty_row_end = MAX_ROW;
473 term->tl_cursor_visible = TRUE;
474 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
475 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100476#ifdef FEAT_GUI
477 term->tl_system = (flags & TERM_START_SYSTEM);
478#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200479 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100480 ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200481 ga_init2(&term->tl_osc_buf, sizeof(char), 300);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200482
Bram Moolenaaraeed2a62021-04-29 20:18:45 +0200483 setpcmark();
Bram Moolenaara80faa82020-04-12 19:37:17 +0200484 CLEAR_FIELD(split_ea);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200485 if (opt->jo_curwin)
486 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100487 // Create a new buffer in the current window.
Bram Moolenaar13568252018-03-16 20:46:58 +0100488 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200489 {
490 no_write_message();
491 vim_free(term);
492 return NULL;
493 }
494 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaarb1009092020-05-31 16:04:42 +0200495 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
496 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
497 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200498 {
499 vim_free(term);
500 return NULL;
501 }
502 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100503 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200504 {
505 buf_T *buf;
506
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100507 // Create a new buffer without a window. Make it the current buffer for
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100508 // a moment to be able to do the initializations.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200509 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
510 BLN_NEW | BLN_LISTED);
511 if (buf == NULL || ml_open(buf) == FAIL)
512 {
513 vim_free(term);
514 return NULL;
515 }
516 old_curbuf = curbuf;
517 --curbuf->b_nwindows;
518 curbuf = buf;
519 curwin->w_buffer = buf;
520 ++curbuf->b_nwindows;
521 }
522 else
523 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100524 // Open a new window or tab.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200525 split_ea.cmdidx = CMD_new;
526 split_ea.cmd = (char_u *)"new";
527 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100528 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200529 {
530 split_ea.line2 = opt->jo_term_rows;
531 split_ea.addr_count = 1;
532 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100533 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200534 {
535 split_ea.line2 = opt->jo_term_cols;
536 split_ea.addr_count = 1;
537 }
538
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100539 if (vertical)
Bram Moolenaare1004402020-10-24 20:49:43 +0200540 cmdmod.cmod_split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200541 ex_splitview(&split_ea);
542 if (curwin == old_curwin)
543 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100544 // split failed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200545 vim_free(term);
546 return NULL;
547 }
548 }
549 term->tl_buffer = curbuf;
550 curbuf->b_term = term;
551
552 if (!opt->jo_hidden)
553 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100554 // Only one size was taken care of with :new, do the other one. With
555 // "curwin" both need to be done.
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100556 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200557 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100558 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200559 win_setwidth(opt->jo_term_cols);
560 }
561
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100562 // Link the new terminal in the list of active terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200563 term->tl_next = first_term;
564 first_term = term;
565
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100566 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
567
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200568 if (opt->jo_term_name != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100569 {
570 vim_free(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200571 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100572 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100573 else if (argv != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100574 {
575 vim_free(curbuf->b_ffname);
Bram Moolenaar13568252018-03-16 20:46:58 +0100576 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100577 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200578 else
579 {
580 int i;
581 size_t len;
582 char_u *cmd, *p;
583
584 if (argvar->v_type == VAR_STRING)
585 {
586 cmd = argvar->vval.v_string;
587 if (cmd == NULL)
588 cmd = (char_u *)"";
589 else if (STRCMP(cmd, "NONE") == 0)
590 cmd = (char_u *)"pty";
591 }
592 else if (argvar->v_type != VAR_LIST
593 || argvar->vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +0100594 || argvar->vval.v_list->lv_len == 0
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100595 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200596 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
597 cmd = (char_u*)"";
598
599 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200600 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200601
602 for (i = 0; p != NULL; ++i)
603 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100604 // Prepend a ! to the command name to avoid the buffer name equals
605 // the executable, otherwise ":w!" would overwrite it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200606 if (i == 0)
607 vim_snprintf((char *)p, len, "!%s", cmd);
608 else
609 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
610 if (buflist_findname(p) == NULL)
611 {
612 vim_free(curbuf->b_ffname);
613 curbuf->b_ffname = p;
614 break;
615 }
616 }
617 }
Bram Moolenaare010c722020-02-24 21:37:54 +0100618 vim_free(curbuf->b_sfname);
619 curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200620 curbuf->b_fname = curbuf->b_ffname;
621
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100622 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
623
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200624 if (opt->jo_term_opencmd != NULL)
625 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
626
627 if (opt->jo_eof_chars != NULL)
628 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
629
630 set_string_option_direct((char_u *)"buftype", -1,
631 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200632 // Avoid that 'buftype' is reset when this buffer is entered.
633 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200634
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100635 // Mark the buffer as not modifiable. It can only be made modifiable after
636 // the job finished.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200637 curbuf->b_p_ma = FALSE;
638
Bram Moolenaarb936b792020-09-04 18:34:09 +0200639 set_term_and_win_size(term, opt);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100640#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200641 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
642#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200643 setup_job_options(opt, term->tl_rows, term->tl_cols);
644
Bram Moolenaar13568252018-03-16 20:46:58 +0100645 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100646 return curbuf;
647
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100648#if defined(FEAT_SESSION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100649 // Remember the command for the session file.
Bram Moolenaar13568252018-03-16 20:46:58 +0100650 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100651 term->tl_command = vim_strsave((char_u *)"NONE");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100652 else if (argvar->v_type == VAR_STRING)
653 {
654 char_u *cmd = argvar->vval.v_string;
655
656 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
657 term->tl_command = vim_strsave(cmd);
658 }
659 else if (argvar->v_type == VAR_LIST
660 && argvar->vval.v_list != NULL
661 && argvar->vval.v_list->lv_len > 0)
662 {
663 garray_T ga;
664 listitem_T *item;
665
666 ga_init2(&ga, 1, 100);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200667 FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100668 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100669 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100670 char_u *p;
671
672 if (s == NULL)
673 break;
674 p = vim_strsave_fnameescape(s, FALSE);
675 if (p == NULL)
676 break;
677 ga_concat(&ga, p);
678 vim_free(p);
679 ga_append(&ga, ' ');
680 }
681 if (item == NULL)
682 {
683 ga_append(&ga, NUL);
684 term->tl_command = ga.ga_data;
685 }
686 else
687 ga_clear(&ga);
688 }
689#endif
690
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100691 if (opt->jo_term_kill != NULL)
692 {
693 char_u *p = skiptowhite(opt->jo_term_kill);
694
695 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
696 }
697
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200698 if (opt->jo_term_api != NULL)
Bram Moolenaar21109272020-01-30 16:27:20 +0100699 {
700 char_u *p = skiptowhite(opt->jo_term_api);
701
702 term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
703 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200704 else
705 term->tl_api = vim_strsave((char_u *)"Tapi_");
706
Bram Moolenaar83d47902020-03-26 20:34:00 +0100707 if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
708 term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
709
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100710 // System dependent: setup the vterm and maybe start the job in it.
Bram Moolenaar13568252018-03-16 20:46:58 +0100711 if (argv == NULL
712 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200713 && argvar->vval.v_string != NULL
714 && STRCMP(argvar->vval.v_string, "NONE") == 0)
715 res = create_pty_only(term, opt);
716 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200717 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200718
719 newbuf = curbuf;
720 if (res == OK)
721 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100722 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200723 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
724 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100725#ifdef FEAT_GUI
726 if (term->tl_system)
727 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100728 // display first line below typed command
Bram Moolenaar13568252018-03-16 20:46:58 +0100729 term->tl_toprow = msg_row + 1;
730 term->tl_dirty_row_end = 0;
731 }
732#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200733
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100734 // Make sure we don't get stuck on sending keys to the job, it leads to
735 // a deadlock if the job is waiting for Vim to read.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200736 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
737
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200738 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200739 {
740 --curbuf->b_nwindows;
741 curbuf = old_curbuf;
742 curwin->w_buffer = curbuf;
743 ++curbuf->b_nwindows;
744 }
745 }
746 else
747 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100748 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200749 return NULL;
750 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100751
Bram Moolenaar13568252018-03-16 20:46:58 +0100752 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar28ed4df2019-10-26 16:21:40 +0200753 if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
754 apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200755 return newbuf;
756}
757
758/*
759 * ":terminal": open a terminal window and execute a job in it.
760 */
761 void
762ex_terminal(exarg_T *eap)
763{
764 typval_T argvar[2];
765 jobopt_T opt;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100766 int opt_shell = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200767 char_u *cmd;
768 char_u *tofree = NULL;
769
770 init_job_options(&opt);
771
772 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100773 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200774 {
775 char_u *p, *ep;
776
777 cmd += 2;
778 p = skiptowhite(cmd);
779 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200780 if (ep != NULL)
781 {
782 if (ep < p)
783 p = ep;
784 else
785 ep = NULL;
786 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200787
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200788# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
789 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
790 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200791 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200792 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100793 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200794 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200795 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200796 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200797 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200798 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200799 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200800 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100801 opt.jo_term_norestore = 1;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100802 else if (OPTARG_HAS("shell"))
803 opt_shell = TRUE;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200804 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100805 {
806 opt.jo_set2 |= JO2_TERM_KILL;
807 opt.jo_term_kill = ep + 1;
808 p = skiptowhite(cmd);
809 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200810 else if (OPTARG_HAS("api"))
811 {
812 opt.jo_set2 |= JO2_TERM_API;
813 if (ep != NULL)
814 {
815 opt.jo_term_api = ep + 1;
816 p = skiptowhite(cmd);
817 }
818 else
819 opt.jo_term_api = NULL;
820 }
821 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200822 {
823 opt.jo_set2 |= JO2_TERM_ROWS;
824 opt.jo_term_rows = atoi((char *)ep + 1);
825 p = skiptowhite(cmd);
826 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200827 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200828 {
829 opt.jo_set2 |= JO2_TERM_COLS;
830 opt.jo_term_cols = atoi((char *)ep + 1);
831 p = skiptowhite(cmd);
832 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200833 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200834 {
835 char_u *buf = NULL;
836 char_u *keys;
837
Bram Moolenaar21109272020-01-30 16:27:20 +0100838 vim_free(opt.jo_eof_chars);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200839 p = skiptowhite(cmd);
840 *p = NUL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200841 keys = replace_termcodes(ep + 1, &buf,
842 REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200843 opt.jo_set2 |= JO2_EOF_CHARS;
844 opt.jo_eof_chars = vim_strsave(keys);
845 vim_free(buf);
846 *p = ' ';
847 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100848#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100849 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
850 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100851 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100852 int tty_type = NUL;
853
854 p = skiptowhite(cmd);
855 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
856 tty_type = 'w';
857 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
858 tty_type = 'c';
859 else
860 {
861 semsg(e_invargval, "type");
862 goto theend;
863 }
864 opt.jo_set2 |= JO2_TTY_TYPE;
865 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100866 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100867#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200868 else
869 {
870 if (*p)
871 *p = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100872 semsg(_("E181: Invalid attribute: %s"), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100873 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200874 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200875# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200876 cmd = skipwhite(p);
877 }
878 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100879 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100880 // Make a copy of 'shell', an autocommand may change the option.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200881 tofree = cmd = vim_strsave(p_sh);
882
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100883 // default to close when the shell exits
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100884 if (opt.jo_term_finish == NUL)
Bram Moolenaare2978022020-04-26 14:47:44 +0200885 opt.jo_term_finish = TL_FINISH_CLOSE;
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100886 }
887
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200888 if (eap->addr_count > 0)
889 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100890 // Write lines from current buffer to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200891 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
892 opt.jo_io[PART_IN] = JIO_BUFFER;
893 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
894 opt.jo_in_top = eap->line1;
895 opt.jo_in_bot = eap->line2;
896 }
897
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100898 if (opt_shell && tofree == NULL)
899 {
900#ifdef UNIX
901 char **argv = NULL;
902 char_u *tofree1 = NULL;
903 char_u *tofree2 = NULL;
904
905 // :term ++shell command
906 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
907 term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaaradf4aa22019-11-10 22:36:44 +0100908 vim_free(argv);
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100909 vim_free(tofree1);
910 vim_free(tofree2);
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100911 goto theend;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100912#else
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100913# ifdef MSWIN
914 long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
915 char_u *newcmd;
916
917 newcmd = alloc(cmdlen);
918 if (newcmd == NULL)
919 goto theend;
920 tofree = newcmd;
921 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
922 cmd = newcmd;
923# else
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100924 emsg(_("E279: Sorry, ++shell is not supported on this system"));
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100925 goto theend;
926# endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100927#endif
928 }
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100929 argvar[0].v_type = VAR_STRING;
930 argvar[0].vval.v_string = cmd;
931 argvar[1].v_type = VAR_UNKNOWN;
932 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100933
934theend:
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100935 vim_free(tofree);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200936 vim_free(opt.jo_eof_chars);
937}
938
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100939#if defined(FEAT_SESSION) || defined(PROTO)
940/*
941 * Write a :terminal command to the session file to restore the terminal in
942 * window "wp".
943 * Return FAIL if writing fails.
944 */
945 int
Bram Moolenaar0e655112020-09-11 20:36:36 +0200946term_write_session(FILE *fd, win_T *wp, hashtab_T *terminal_bufs)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100947{
Bram Moolenaar0e655112020-09-11 20:36:36 +0200948 const int bufnr = wp->w_buffer->b_fnum;
949 term_T *term = wp->w_buffer->b_term;
950
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200951 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200952 {
953 // There are multiple views into this terminal buffer. We don't want to
954 // create the terminal multiple times. If it's the first time, create,
955 // otherwise link to the first buffer.
956 char id_as_str[NUMBUFLEN];
957 hashitem_T *entry;
958
959 vim_snprintf(id_as_str, sizeof(id_as_str), "%d", bufnr);
960
961 entry = hash_find(terminal_bufs, (char_u *)id_as_str);
962 if (!HASHITEM_EMPTY(entry))
963 {
964 // we've already opened this terminal buffer
965 if (fprintf(fd, "execute 'buffer ' . s:term_buf_%d", bufnr) < 0)
966 return FAIL;
967 return put_eol(fd);
968 }
969 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100970
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100971 // Create the terminal and run the command. This is not without
972 // risk, but let's assume the user only creates a session when this
973 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100974 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
975 term->tl_cols, term->tl_rows) < 0)
976 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100977#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100978 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
979 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100980#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100981 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
982 return FAIL;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200983 if (put_eol(fd) != OK)
984 return FAIL;
985
986 if (fprintf(fd, "let s:term_buf_%d = bufnr()", bufnr) < 0)
987 return FAIL;
988
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200989 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200990 {
991 char *hash_key = alloc(NUMBUFLEN);
992
993 vim_snprintf(hash_key, NUMBUFLEN, "%d", bufnr);
994 hash_add(terminal_bufs, (char_u *)hash_key);
995 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100996
997 return put_eol(fd);
998}
999
1000/*
1001 * Return TRUE if "buf" has a terminal that should be restored.
1002 */
1003 int
1004term_should_restore(buf_T *buf)
1005{
1006 term_T *term = buf->b_term;
1007
1008 return term != NULL && (term->tl_command == NULL
1009 || STRCMP(term->tl_command, "NONE") != 0);
1010}
1011#endif
1012
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001013/*
1014 * Free the scrollback buffer for "term".
1015 */
1016 static void
1017free_scrollback(term_T *term)
1018{
1019 int i;
1020
1021 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
1022 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
1023 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001024 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
1025 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
1026 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001027}
1028
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001029
1030// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +02001031static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001032
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001033/*
1034 * Free a terminal and everything it refers to.
1035 * Kills the job if there is one.
1036 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001037 * The actual terminal structure is freed later in free_unused_terminals(),
1038 * because callbacks may wipe out a buffer while the terminal is still
1039 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001040 */
1041 void
1042free_terminal(buf_T *buf)
1043{
1044 term_T *term = buf->b_term;
1045 term_T *tp;
1046
1047 if (term == NULL)
1048 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001049
1050 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001051 if (first_term == term)
1052 first_term = term->tl_next;
1053 else
1054 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
1055 if (tp->tl_next == term)
1056 {
1057 tp->tl_next = term->tl_next;
1058 break;
1059 }
1060
1061 if (term->tl_job != NULL)
1062 {
1063 if (term->tl_job->jv_status != JOB_ENDED
1064 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +01001065 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001066 job_stop(term->tl_job, NULL, "kill");
1067 job_unref(term->tl_job);
1068 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001069 term->tl_next = terminals_to_free;
1070 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001071
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001072 buf->b_term = NULL;
1073 if (in_terminal_loop == term)
1074 in_terminal_loop = NULL;
1075}
1076
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001077 void
1078free_unused_terminals()
1079{
1080 while (terminals_to_free != NULL)
1081 {
1082 term_T *term = terminals_to_free;
1083
1084 terminals_to_free = term->tl_next;
1085
1086 free_scrollback(term);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02001087 ga_clear(&term->tl_osc_buf);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001088
1089 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001090 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001091 vim_free(term->tl_title);
1092#ifdef FEAT_SESSION
1093 vim_free(term->tl_command);
1094#endif
1095 vim_free(term->tl_kill);
1096 vim_free(term->tl_status_text);
1097 vim_free(term->tl_opencmd);
1098 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001099 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001100#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001101 if (term->tl_out_fd != NULL)
1102 fclose(term->tl_out_fd);
1103#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001104 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001105 vim_free(term->tl_cursor_color);
1106 vim_free(term);
1107 }
1108}
1109
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001110/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001111 * Get the part that is connected to the tty. Normally this is PART_IN, but
1112 * when writing buffer lines to the job it can be another. This makes it
1113 * possible to do "1,5term vim -".
1114 */
1115 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001116get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001117{
1118#ifdef UNIX
1119 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1120 int i;
1121
1122 for (i = 0; i < 3; ++i)
1123 {
1124 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1125
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001126 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001127 return parts[i];
1128 }
1129#endif
1130 return PART_IN;
1131}
1132
1133/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001134 * Write job output "msg[len]" to the vterm.
1135 */
1136 static void
1137term_write_job_output(term_T *term, char_u *msg, size_t len)
1138{
1139 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001140 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001141
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001142 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001143
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001144 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001145 if (prevlen != vterm_output_get_buffer_current(vterm))
1146 {
1147 char buf[KEY_BUF_LEN];
1148 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1149
1150 if (curlen > 0)
1151 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1152 (char_u *)buf, (int)curlen, NULL);
1153 }
1154
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001155 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001156 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1157}
1158
1159 static void
1160update_cursor(term_T *term, int redraw)
1161{
1162 if (term->tl_normal_mode)
1163 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001164#ifdef FEAT_GUI
1165 if (term->tl_system)
1166 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1167 term->tl_cursor_pos.col);
1168 else
1169#endif
1170 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001171 if (redraw)
1172 {
1173 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1174 cursor_on();
1175 out_flush();
1176#ifdef FEAT_GUI
1177 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001178 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001179 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001180 gui_mch_flush();
1181 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001182#endif
1183 }
1184}
1185
1186/*
1187 * Invoked when "msg" output from a job was received. Write it to the terminal
1188 * of "buffer".
1189 */
1190 void
1191write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1192{
1193 size_t len = STRLEN(msg);
1194 term_T *term = buffer->b_term;
1195
Bram Moolenaar4f974752019-02-17 17:44:42 +01001196#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001197 // Win32: Cannot redirect output of the job, intercept it here and write to
1198 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001199 if (term->tl_out_fd != NULL)
1200 {
1201 ch_log(channel, "Writing %d bytes to output file", (int)len);
1202 fwrite(msg, len, 1, term->tl_out_fd);
1203 return;
1204 }
1205#endif
1206
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001207 if (term->tl_vterm == NULL)
1208 {
1209 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1210 return;
1211 }
1212 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01001213 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001214 term_write_job_output(term, msg, len);
1215
Bram Moolenaar13568252018-03-16 20:46:58 +01001216#ifdef FEAT_GUI
1217 if (term->tl_system)
1218 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001219 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001220 update_system_term(term);
1221 update_cursor(term, TRUE);
1222 }
1223 else
1224#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001225 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1226 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001227 if (!term->tl_normal_mode)
1228 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001229 // Don't use update_screen() when editing the command line, it gets
1230 // cleared.
1231 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001232 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001233 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001234 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001235 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001236 // update_screen() can be slow, check the terminal wasn't closed
1237 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001238 if (buffer == curbuf && curbuf->b_term != NULL)
1239 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001240 }
1241 else
1242 redraw_after_callback(TRUE);
1243 }
1244}
1245
1246/*
1247 * Send a mouse position and click to the vterm
1248 */
1249 static int
1250term_send_mouse(VTerm *vterm, int button, int pressed)
1251{
1252 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001253 int row = mouse_row - W_WINROW(curwin);
1254 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001255
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001256#ifdef FEAT_PROP_POPUP
1257 if (popup_is_popup(curwin))
1258 {
1259 row -= popup_top_extra(curwin);
1260 col -= popup_left_extra(curwin);
1261 }
1262#endif
1263 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001264 if (button != 0)
1265 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001266 return TRUE;
1267}
1268
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001269static int enter_mouse_col = -1;
1270static int enter_mouse_row = -1;
1271
1272/*
1273 * Handle a mouse click, drag or release.
1274 * Return TRUE when a mouse event is sent to the terminal.
1275 */
1276 static int
1277term_mouse_click(VTerm *vterm, int key)
1278{
1279#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001280 // For modeless selection mouse drag and release events are ignored, unless
1281 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001282 static int ignore_drag_release = TRUE;
1283 VTermMouseState mouse_state;
1284
1285 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1286 if (mouse_state.flags == 0)
1287 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001288 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001289 switch (key)
1290 {
1291 case K_LEFTDRAG:
1292 case K_LEFTRELEASE:
1293 case K_RIGHTDRAG:
1294 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001295 // Ignore drag and release events when the button-down wasn't
1296 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001297 if (ignore_drag_release)
1298 {
1299 int save_mouse_col, save_mouse_row;
1300
1301 if (enter_mouse_col < 0)
1302 break;
1303
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001304 // mouse click in the window gave us focus, handle that
1305 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001306 save_mouse_col = mouse_col;
1307 save_mouse_row = mouse_row;
1308 mouse_col = enter_mouse_col;
1309 mouse_row = enter_mouse_row;
1310 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1311 mouse_col = save_mouse_col;
1312 mouse_row = save_mouse_row;
1313 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001314 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001315 case K_LEFTMOUSE:
1316 case K_RIGHTMOUSE:
1317 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1318 ignore_drag_release = TRUE;
1319 else
1320 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001321 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001322 if (clip_star.available)
1323 {
1324 int button, is_click, is_drag;
1325
1326 button = get_mouse_button(KEY2TERMCAP1(key),
1327 &is_click, &is_drag);
1328 if (mouse_model_popup() && button == MOUSE_LEFT
1329 && (mod_mask & MOD_MASK_SHIFT))
1330 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001331 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001332 button = MOUSE_RIGHT;
1333 mod_mask &= ~MOD_MASK_SHIFT;
1334 }
1335 clip_modeless(button, is_click, is_drag);
1336 }
1337 break;
1338
1339 case K_MIDDLEMOUSE:
1340 if (clip_star.available)
1341 insert_reg('*', TRUE);
1342 break;
1343 }
1344 enter_mouse_col = -1;
1345 return FALSE;
1346 }
1347#endif
1348 enter_mouse_col = -1;
1349
1350 switch (key)
1351 {
1352 case K_LEFTMOUSE:
1353 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1354 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1355 case K_LEFTRELEASE:
1356 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1357 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1358 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1359 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1360 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1361 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1362 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1363 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1364 }
1365 return TRUE;
1366}
1367
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001368/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001369 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1370 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001371 * Return the number of bytes in "buf".
1372 */
1373 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001374term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001375{
1376 VTerm *vterm = term->tl_vterm;
1377 VTermKey key = VTERM_KEY_NONE;
1378 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001379 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001380
1381 switch (c)
1382 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001383 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001384
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001385 // don't use VTERM_KEY_BACKSPACE, it always
1386 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001387 case K_BS: c = term_backspace_char; break;
1388
1389 case ESC: key = VTERM_KEY_ESCAPE; break;
1390 case K_DEL: key = VTERM_KEY_DEL; break;
1391 case K_DOWN: key = VTERM_KEY_DOWN; break;
1392 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1393 key = VTERM_KEY_DOWN; break;
1394 case K_END: key = VTERM_KEY_END; break;
1395 case K_S_END: mod = VTERM_MOD_SHIFT;
1396 key = VTERM_KEY_END; break;
1397 case K_C_END: mod = VTERM_MOD_CTRL;
1398 key = VTERM_KEY_END; break;
1399 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1400 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1401 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1402 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1403 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1404 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1405 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1406 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1407 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1408 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1409 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1410 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1411 case K_HOME: key = VTERM_KEY_HOME; break;
1412 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1413 key = VTERM_KEY_HOME; break;
1414 case K_C_HOME: mod = VTERM_MOD_CTRL;
1415 key = VTERM_KEY_HOME; break;
1416 case K_INS: key = VTERM_KEY_INS; break;
1417 case K_K0: key = VTERM_KEY_KP_0; break;
1418 case K_K1: key = VTERM_KEY_KP_1; break;
1419 case K_K2: key = VTERM_KEY_KP_2; break;
1420 case K_K3: key = VTERM_KEY_KP_3; break;
1421 case K_K4: key = VTERM_KEY_KP_4; break;
1422 case K_K5: key = VTERM_KEY_KP_5; break;
1423 case K_K6: key = VTERM_KEY_KP_6; break;
1424 case K_K7: key = VTERM_KEY_KP_7; break;
1425 case K_K8: key = VTERM_KEY_KP_8; break;
1426 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001427 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001428 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001429 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001430 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001431 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1432 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001433 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1434 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001435 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1436 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001437 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1438 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1439 case K_LEFT: key = VTERM_KEY_LEFT; break;
1440 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1441 key = VTERM_KEY_LEFT; break;
1442 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1443 key = VTERM_KEY_LEFT; break;
1444 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1445 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1446 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1447 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1448 key = VTERM_KEY_RIGHT; break;
1449 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1450 key = VTERM_KEY_RIGHT; break;
1451 case K_UP: key = VTERM_KEY_UP; break;
1452 case K_S_UP: mod = VTERM_MOD_SHIFT;
1453 key = VTERM_KEY_UP; break;
1454 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001455 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1456 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001457
Bram Moolenaara42ad572017-11-16 13:08:04 +01001458 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1459 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001460 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1461 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001462
1463 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001464 case K_LEFTMOUSE_NM:
1465 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001466 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001467 case K_LEFTRELEASE_NM:
1468 case K_MOUSEMOVE:
1469 case K_MIDDLEMOUSE:
1470 case K_MIDDLEDRAG:
1471 case K_MIDDLERELEASE:
1472 case K_RIGHTMOUSE:
1473 case K_RIGHTDRAG:
1474 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1475 return 0;
1476 other = TRUE;
1477 break;
1478
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001479 case K_X1MOUSE: /* TODO */ return 0;
1480 case K_X1DRAG: /* TODO */ return 0;
1481 case K_X1RELEASE: /* TODO */ return 0;
1482 case K_X2MOUSE: /* TODO */ return 0;
1483 case K_X2DRAG: /* TODO */ return 0;
1484 case K_X2RELEASE: /* TODO */ return 0;
1485
1486 case K_IGNORE: return 0;
1487 case K_NOP: return 0;
1488 case K_UNDO: return 0;
1489 case K_HELP: return 0;
1490 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1491 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1492 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1493 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1494 case K_SELECT: return 0;
1495#ifdef FEAT_GUI
1496 case K_VER_SCROLLBAR: return 0;
1497 case K_HOR_SCROLLBAR: return 0;
1498#endif
1499#ifdef FEAT_GUI_TABLINE
1500 case K_TABLINE: return 0;
1501 case K_TABMENU: return 0;
1502#endif
1503#ifdef FEAT_NETBEANS_INTG
1504 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1505#endif
1506#ifdef FEAT_DND
1507 case K_DROP: return 0;
1508#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001509 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001510 case K_PS: vterm_keyboard_start_paste(vterm);
1511 other = TRUE;
1512 break;
1513 case K_PE: vterm_keyboard_end_paste(vterm);
1514 other = TRUE;
1515 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001516 }
1517
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001518 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001519 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001520 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001521 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001522 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001523 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001524 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001525
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001526 /*
1527 * Convert special keys to vterm keys:
1528 * - Write keys to vterm: vterm_keyboard_key()
1529 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001530 */
1531 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001532 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001533 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001534 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001535 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001536 vterm_keyboard_unichar(vterm, c, mod);
1537
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001538 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001539 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1540}
1541
1542/*
1543 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001544 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001545 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001546 */
1547 static int
1548term_job_running_check(term_T *term, int check_job_status)
1549{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001550 // Also consider the job finished when the channel is closed, to avoid a
1551 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001552 if (term != NULL
1553 && term->tl_job != NULL
1554 && channel_is_open(term->tl_job->jv_channel))
1555 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001556 job_T *job = term->tl_job;
1557
1558 // Careful: Checking the job status may invoked callbacks, which close
1559 // the buffer and terminate "term". However, "job" will not be freed
1560 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001561 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001562 job_status(job);
1563 return (job->jv_status == JOB_STARTED
1564 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001565 }
1566 return FALSE;
1567}
1568
1569/*
1570 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001571 */
1572 int
1573term_job_running(term_T *term)
1574{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001575 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001576}
1577
1578/*
1579 * Return TRUE if "term" has an active channel and used ":term NONE".
1580 */
1581 int
1582term_none_open(term_T *term)
1583{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001584 // Also consider the job finished when the channel is closed, to avoid a
1585 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001586 return term != NULL
1587 && term->tl_job != NULL
1588 && channel_is_open(term->tl_job->jv_channel)
1589 && term->tl_job->jv_channel->ch_keep_open;
1590}
1591
1592/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001593 * Used when exiting: kill the job in "buf" if so desired.
1594 * Return OK when the job finished.
1595 * Return FAIL when the job is still running.
1596 */
1597 int
1598term_try_stop_job(buf_T *buf)
1599{
1600 int count;
1601 char *how = (char *)buf->b_term->tl_kill;
1602
1603#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001604 if ((how == NULL || *how == NUL)
1605 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001606 {
1607 char_u buff[DIALOG_MSG_SIZE];
1608 int ret;
1609
Bram Moolenaar00806bc2020-11-05 19:36:38 +01001610 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001611 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1612 if (ret == VIM_YES)
1613 how = "kill";
1614 else if (ret == VIM_CANCEL)
1615 return FAIL;
1616 }
1617#endif
1618 if (how == NULL || *how == NUL)
1619 return FAIL;
1620
1621 job_stop(buf->b_term->tl_job, NULL, how);
1622
Bram Moolenaar9172d232019-01-29 23:06:54 +01001623 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001624 for (count = 0; count < 100; ++count)
1625 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001626 job_T *job;
1627
1628 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001629 if (!buf_valid(buf)
1630 || buf->b_term == NULL
1631 || buf->b_term->tl_job == NULL)
1632 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001633 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001634
Bram Moolenaar9172d232019-01-29 23:06:54 +01001635 // Call job_status() to update jv_status. It may cause the job to be
1636 // cleaned up but it won't be freed.
1637 job_status(job);
1638 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001639 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001640
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001641 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001642 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001643 }
1644 return FAIL;
1645}
1646
1647/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001648 * Add the last line of the scrollback buffer to the buffer in the window.
1649 */
1650 static void
1651add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1652{
1653 buf_T *buf = term->tl_buffer;
1654 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1655 linenr_T lnum = buf->b_ml.ml_line_count;
1656
Bram Moolenaar4f974752019-02-17 17:44:42 +01001657#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001658 if (!enc_utf8 && enc_codepage > 0)
1659 {
1660 WCHAR *ret = NULL;
1661 int length = 0;
1662
1663 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1664 &ret, &length);
1665 if (ret != NULL)
1666 {
1667 WideCharToMultiByte_alloc(enc_codepage, 0,
1668 ret, length, (char **)&text, &len, 0, 0);
1669 vim_free(ret);
1670 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1671 vim_free(text);
1672 }
1673 }
1674 else
1675#endif
1676 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1677 if (empty)
1678 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001679 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001680 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001681 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001682 curbuf = curwin->w_buffer;
1683 }
1684}
1685
1686 static void
1687cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1688{
1689 attr->width = cell->width;
1690 attr->attrs = cell->attrs;
1691 attr->fg = cell->fg;
1692 attr->bg = cell->bg;
1693}
1694
1695 static int
1696equal_celattr(cellattr_T *a, cellattr_T *b)
1697{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001698 // We only compare the RGB colors, ignoring the ANSI index and type.
1699 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001700 return a->fg.red == b->fg.red
1701 && a->fg.green == b->fg.green
1702 && a->fg.blue == b->fg.blue
1703 && a->bg.red == b->bg.red
1704 && a->bg.green == b->bg.green
1705 && a->bg.blue == b->bg.blue;
1706}
1707
Bram Moolenaard96ff162018-02-18 22:13:29 +01001708/*
1709 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1710 * line at this position. Otherwise at the end.
1711 */
1712 static int
1713add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1714{
1715 if (ga_grow(&term->tl_scrollback, 1) == OK)
1716 {
1717 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1718 + term->tl_scrollback.ga_len;
1719
1720 if (lnum > 0)
1721 {
1722 int i;
1723
1724 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1725 {
1726 *line = *(line - 1);
1727 --line;
1728 }
1729 }
1730 line->sb_cols = 0;
1731 line->sb_cells = NULL;
1732 line->sb_fill_attr = *fill_attr;
1733 ++term->tl_scrollback.ga_len;
1734 return OK;
1735 }
1736 return FALSE;
1737}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001738
1739/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001740 * Remove the terminal contents from the scrollback and the buffer.
1741 * Used before adding a new scrollback line or updating the buffer for lines
1742 * displayed in the terminal.
1743 */
1744 static void
1745cleanup_scrollback(term_T *term)
1746{
1747 sb_line_T *line;
1748 garray_T *gap;
1749
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001750 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001751 gap = &term->tl_scrollback;
1752 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1753 && gap->ga_len > 0)
1754 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001755 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001756 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1757 vim_free(line->sb_cells);
1758 --gap->ga_len;
1759 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001760 curbuf = curwin->w_buffer;
1761 if (curbuf == term->tl_buffer)
1762 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001763}
1764
1765/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001766 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001767 */
1768 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001769update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001770{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001771 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001772 int len;
1773 int lines_skipped = 0;
1774 VTermPos pos;
1775 VTermScreenCell cell;
1776 cellattr_T fill_attr, new_fill_attr;
1777 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001778
1779 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1780 "Adding terminal window snapshot to buffer");
1781
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001782 // First remove the lines that were appended before, they might be
1783 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001784 cleanup_scrollback(term);
1785
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001786 screen = vterm_obtain_screen(term->tl_vterm);
1787 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001788 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1789 {
1790 len = 0;
1791 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1792 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1793 && cell.chars[0] != NUL)
1794 {
1795 len = pos.col + 1;
1796 new_fill_attr = term->tl_default_color;
1797 }
1798 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001799 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001800 cell2cellattr(&cell, &new_fill_attr);
1801
1802 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1803 ++lines_skipped;
1804 else
1805 {
1806 while (lines_skipped > 0)
1807 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001808 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001809 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001810 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001811 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001812 }
1813
1814 if (len == 0)
1815 p = NULL;
1816 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001817 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001818 if ((p != NULL || len == 0)
1819 && ga_grow(&term->tl_scrollback, 1) == OK)
1820 {
1821 garray_T ga;
1822 int width;
1823 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1824 + term->tl_scrollback.ga_len;
1825
1826 ga_init2(&ga, 1, 100);
1827 for (pos.col = 0; pos.col < len; pos.col += width)
1828 {
1829 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1830 {
1831 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001832 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001833 if (ga_grow(&ga, 1) == OK)
1834 ga.ga_len += utf_char2bytes(' ',
1835 (char_u *)ga.ga_data + ga.ga_len);
1836 }
1837 else
1838 {
1839 width = cell.width;
1840
1841 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001842 if (width == 2)
1843 // second cell of double-width character has the
1844 // same attributes.
1845 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001846
Bram Moolenaara79fd562018-12-20 20:47:32 +01001847 // Each character can be up to 6 bytes.
1848 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001849 {
1850 int i;
1851 int c;
1852
1853 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1854 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1855 (char_u *)ga.ga_data + ga.ga_len);
1856 }
1857 }
1858 }
1859 line->sb_cols = len;
1860 line->sb_cells = p;
1861 line->sb_fill_attr = new_fill_attr;
1862 fill_attr = new_fill_attr;
1863 ++term->tl_scrollback.ga_len;
1864
1865 if (ga_grow(&ga, 1) == FAIL)
1866 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1867 else
1868 {
1869 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1870 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1871 }
1872 ga_clear(&ga);
1873 }
1874 else
1875 vim_free(p);
1876 }
1877 }
1878
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001879 // Add trailing empty lines.
1880 for (pos.row = term->tl_scrollback.ga_len;
1881 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1882 ++pos.row)
1883 {
1884 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1885 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1886 }
1887
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001888 term->tl_dirty_snapshot = FALSE;
1889#ifdef FEAT_TIMERS
1890 term->tl_timer_set = FALSE;
1891#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001892}
1893
1894/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001895 * Loop over all windows in the current tab, and also curwin, which is not
1896 * encountered when using a terminal in a popup window.
1897 * Return TRUE if "*wp" was set to the next window.
1898 */
1899 static int
1900for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1901{
1902 if (*wp == NULL)
1903 *wp = firstwin;
1904 else if ((*wp)->w_next != NULL)
1905 *wp = (*wp)->w_next;
1906 else if (!*did_curwin)
1907 *wp = curwin;
1908 else
1909 return FALSE;
1910 if (*wp == curwin)
1911 *did_curwin = TRUE;
1912 return TRUE;
1913}
1914
1915/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001916 * If needed, add the current lines of the terminal to scrollback and to the
1917 * buffer. Called after the job has ended and when switching to
1918 * Terminal-Normal mode.
1919 * When "redraw" is TRUE redraw the windows that show the terminal.
1920 */
1921 static void
1922may_move_terminal_to_buffer(term_T *term, int redraw)
1923{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001924 if (term->tl_vterm == NULL)
1925 return;
1926
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001927 // Update the snapshot only if something changes or the buffer does not
1928 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001929 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1930 <= term->tl_scrollback_scrolled)
1931 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001932
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001933 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001934 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1935 &term->tl_default_color.fg, &term->tl_default_color.bg);
1936
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001937 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001938 {
1939 win_T *wp = NULL;
1940 int did_curwin = FALSE;
1941
1942 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001943 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001944 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001945 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001946 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1947 wp->w_cursor.col = 0;
1948 wp->w_valid = 0;
1949 if (wp->w_cursor.lnum >= wp->w_height)
1950 {
1951 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001952
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001953 if (wp->w_topline < min_topline)
1954 wp->w_topline = min_topline;
1955 }
1956 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001957 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001958 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001959 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001960}
1961
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001962#if defined(FEAT_TIMERS) || defined(PROTO)
1963/*
1964 * Check if any terminal timer expired. If so, copy text from the terminal to
1965 * the buffer.
1966 * Return the time until the next timer will expire.
1967 */
1968 int
1969term_check_timers(int next_due_arg, proftime_T *now)
1970{
1971 term_T *term;
1972 int next_due = next_due_arg;
1973
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001974 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001975 {
1976 if (term->tl_timer_set && !term->tl_normal_mode)
1977 {
1978 long this_due = proftime_time_left(&term->tl_timer_due, now);
1979
1980 if (this_due <= 1)
1981 {
1982 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001983 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001984 }
1985 else if (next_due == -1 || next_due > this_due)
1986 next_due = this_due;
1987 }
1988 }
1989
1990 return next_due;
1991}
1992#endif
1993
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001994/*
1995 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
1996 * otherwise end it.
1997 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001998 static void
1999set_terminal_mode(term_T *term, int normal_mode)
2000{
2001 term->tl_normal_mode = normal_mode;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002002 if (!normal_mode)
2003 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002004 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002005 if (term->tl_buffer == curbuf)
2006 maketitle();
2007}
2008
2009/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002010 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002011 * Move the vterm contents into the scrollback buffer and free the vterm.
2012 */
2013 static void
2014cleanup_vterm(term_T *term)
2015{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002016 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002017 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002018 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002019 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002020}
2021
2022/*
2023 * Switch from Terminal-Job mode to Terminal-Normal mode.
2024 * Suspends updating the terminal window.
2025 */
2026 static void
2027term_enter_normal_mode(void)
2028{
2029 term_T *term = curbuf->b_term;
2030
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002031 set_terminal_mode(term, TRUE);
2032
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002033 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002034 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002035
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002036 // Move the window cursor to the position of the cursor in the
2037 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002038 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2039 + term->tl_cursor_pos.row + 1;
2040 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002041 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2042 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002043 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002044
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002045 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002046 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2047}
2048
2049/*
2050 * Returns TRUE if the current window contains a terminal and we are in
2051 * Terminal-Normal mode.
2052 */
2053 int
2054term_in_normal_mode(void)
2055{
2056 term_T *term = curbuf->b_term;
2057
2058 return term != NULL && term->tl_normal_mode;
2059}
2060
2061/*
2062 * Switch from Terminal-Normal mode to Terminal-Job mode.
2063 * Restores updating the terminal window.
2064 */
2065 void
2066term_enter_job_mode()
2067{
2068 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002069
2070 set_terminal_mode(term, FALSE);
2071
2072 if (term->tl_channel_closed)
2073 cleanup_vterm(term);
2074 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002075#ifdef FEAT_PROP_POPUP
2076 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002077 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002078#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002079}
2080
2081/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002082 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002083 * Note: while waiting a terminal may be closed and freed if the channel is
2084 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002085 */
2086 static int
2087term_vgetc()
2088{
2089 int c;
2090 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002091 int modify_other_keys =
2092 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002093
2094 State = TERMINAL;
2095 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002096#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002097 ctrl_break_was_pressed = FALSE;
2098#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002099 if (modify_other_keys)
2100 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002101 c = vgetc();
2102 got_int = FALSE;
2103 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002104 if (modify_other_keys)
2105 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002106 return c;
2107}
2108
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002109static int mouse_was_outside = FALSE;
2110
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002111/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002112 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002113 * Return FAIL when the key needs to be handled in Normal mode.
2114 * Return OK when the key was dropped or sent to the terminal.
2115 */
2116 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002117send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002118{
2119 char msg[KEY_BUF_LEN];
2120 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002121 int dragging_outside = FALSE;
2122
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002123 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002124 switch (c)
2125 {
2126 case NUL:
2127 case K_ZERO:
2128 if (typed)
2129 stuffcharReadbuff(c);
2130 return FAIL;
2131
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002132 case K_TABLINE:
2133 stuffcharReadbuff(c);
2134 return FAIL;
2135
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002136 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002137 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002138 return FAIL;
2139
2140 case K_LEFTDRAG:
2141 case K_MIDDLEDRAG:
2142 case K_RIGHTDRAG:
2143 case K_X1DRAG:
2144 case K_X2DRAG:
2145 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002146 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002147 case K_LEFTMOUSE:
2148 case K_LEFTMOUSE_NM:
2149 case K_LEFTRELEASE:
2150 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002151 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002152 case K_MIDDLEMOUSE:
2153 case K_MIDDLERELEASE:
2154 case K_RIGHTMOUSE:
2155 case K_RIGHTRELEASE:
2156 case K_X1MOUSE:
2157 case K_X1RELEASE:
2158 case K_X2MOUSE:
2159 case K_X2RELEASE:
2160
2161 case K_MOUSEUP:
2162 case K_MOUSEDOWN:
2163 case K_MOUSELEFT:
2164 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002165 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002166 int row = mouse_row;
2167 int col = mouse_col;
2168
2169#ifdef FEAT_PROP_POPUP
2170 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002171 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002172 row -= popup_top_extra(curwin);
2173 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002174 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002175#endif
2176 if (row < W_WINROW(curwin)
2177 || row >= (W_WINROW(curwin) + curwin->w_height)
2178 || col < curwin->w_wincol
2179 || col >= W_ENDCOL(curwin)
2180 || dragging_outside)
2181 {
2182 // click or scroll outside the current window or on status
2183 // line or vertical separator
2184 if (typed)
2185 {
2186 stuffcharReadbuff(c);
2187 mouse_was_outside = TRUE;
2188 }
2189 return FAIL;
2190 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002191 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002192 break;
2193
2194 case K_COMMAND:
2195 return do_cmdline(NULL, getcmdkeycmd, NULL, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002196 }
2197 if (typed)
2198 mouse_was_outside = FALSE;
2199
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002200 // Convert the typed key to a sequence of bytes for the job.
2201 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002202 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002203 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002204 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2205 (char_u *)msg, (int)len, NULL);
2206
2207 return OK;
2208}
2209
2210 static void
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002211position_cursor(win_T *wp, VTermPos *pos)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002212{
2213 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2214 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002215#ifdef FEAT_PROP_POPUP
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002216 if (popup_is_popup(wp))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002217 {
Bram Moolenaarf5452692020-11-28 21:56:06 +01002218 wp->w_wrow += popup_top_extra(wp);
2219 wp->w_wcol += popup_left_extra(wp);
Bram Moolenaar6a076442020-11-15 20:32:58 +01002220 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002221 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01002222 else
2223 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002224#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002225 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2226}
2227
2228/*
2229 * Handle CTRL-W "": send register contents to the job.
2230 */
2231 static void
2232term_paste_register(int prev_c UNUSED)
2233{
2234 int c;
2235 list_T *l;
2236 listitem_T *item;
2237 long reglen = 0;
2238 int type;
2239
2240#ifdef FEAT_CMDL_INFO
2241 if (add_to_showcmd(prev_c))
2242 if (add_to_showcmd('"'))
2243 out_flush();
2244#endif
2245 c = term_vgetc();
2246#ifdef FEAT_CMDL_INFO
2247 clear_showcmd();
2248#endif
2249 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002250 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002251 return;
2252
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002253 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002254 if (c == '=' && get_expr_register() != '=')
2255 return;
2256 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002257 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002258 return;
2259
2260 l = (list_T *)get_reg_contents(c, GREG_LIST);
2261 if (l != NULL)
2262 {
2263 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002264 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002265 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002266 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002267#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002268 char_u *tmp = s;
2269
2270 if (!enc_utf8 && enc_codepage > 0)
2271 {
2272 WCHAR *ret = NULL;
2273 int length = 0;
2274
2275 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2276 (int)STRLEN(s), &ret, &length);
2277 if (ret != NULL)
2278 {
2279 WideCharToMultiByte_alloc(CP_UTF8, 0,
2280 ret, length, (char **)&s, &length, 0, 0);
2281 vim_free(ret);
2282 }
2283 }
2284#endif
2285 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2286 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002287#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002288 if (tmp != s)
2289 vim_free(s);
2290#endif
2291
2292 if (item->li_next != NULL || type == MLINE)
2293 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2294 (char_u *)"\r", 1, NULL);
2295 }
2296 list_free(l);
2297 }
2298}
2299
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002300/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002301 * Return TRUE when waiting for a character in the terminal, the cursor of the
2302 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002303 */
2304 int
2305terminal_is_active()
2306{
2307 return in_terminal_loop != NULL;
2308}
2309
Bram Moolenaar83d47902020-03-26 20:34:00 +01002310/*
2311 * Return the highight group name for the terminal; "Terminal" if not set.
2312 */
2313 static char_u *
2314term_get_highlight_name(term_T *term)
2315{
2316 if (term->tl_highlight_name == NULL)
2317 return (char_u *)"Terminal";
2318 return term->tl_highlight_name;
2319}
2320
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002321#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002322 cursorentry_T *
2323term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2324{
2325 term_T *term = in_terminal_loop;
2326 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002327 int id;
2328 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002329
Bram Moolenaara80faa82020-04-12 19:37:17 +02002330 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002331 entry.shape = entry.mshape =
2332 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2333 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2334 SHAPE_BLOCK;
2335 entry.percentage = 20;
2336 if (term->tl_cursor_blink)
2337 {
2338 entry.blinkwait = 700;
2339 entry.blinkon = 400;
2340 entry.blinkoff = 250;
2341 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002342
Bram Moolenaar83d47902020-03-26 20:34:00 +01002343 // The highlight group overrules the defaults.
2344 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002345 if (id != 0)
2346 {
2347 syn_id2colors(id, &term_fg, &term_bg);
2348 *fg = term_bg;
2349 }
2350 else
2351 *fg = gui.back_pixel;
2352
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002353 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002354 {
2355 if (id != 0)
2356 *bg = term_fg;
2357 else
2358 *bg = gui.norm_pixel;
2359 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002360 else
2361 *bg = color_name2handle(term->tl_cursor_color);
2362 entry.name = "n";
2363 entry.used_for = SHAPE_CURSOR;
2364
2365 return &entry;
2366}
2367#endif
2368
Bram Moolenaard317b382018-02-08 22:33:31 +01002369 static void
2370may_output_cursor_props(void)
2371{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002372 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002373 || last_set_cursor_shape != desired_cursor_shape
2374 || last_set_cursor_blink != desired_cursor_blink)
2375 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002376 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002377 last_set_cursor_shape = desired_cursor_shape;
2378 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002379 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002380 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002381 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002382 ui_cursor_shape_forced(TRUE);
2383 else
2384 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2385 }
2386}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002387
Bram Moolenaard317b382018-02-08 22:33:31 +01002388/*
2389 * Set the cursor color and shape, if not last set to these.
2390 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002391 static void
2392may_set_cursor_props(term_T *term)
2393{
2394#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002395 // For the GUI the cursor properties are obtained with
2396 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002397 if (gui.in_use)
2398 return;
2399#endif
2400 if (in_terminal_loop == term)
2401 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002402 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002403 desired_cursor_shape = term->tl_cursor_shape;
2404 desired_cursor_blink = term->tl_cursor_blink;
2405 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002406 }
2407}
2408
Bram Moolenaard317b382018-02-08 22:33:31 +01002409/*
2410 * Reset the desired cursor properties and restore them when needed.
2411 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002412 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002413prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002414{
2415#ifdef FEAT_GUI
2416 if (gui.in_use)
2417 return;
2418#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002419 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002420 desired_cursor_shape = -1;
2421 desired_cursor_blink = -1;
2422 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002423}
2424
2425/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002426 * Returns TRUE if the current window contains a terminal and we are sending
2427 * keys to the job.
2428 * If "check_job_status" is TRUE update the job status.
2429 */
2430 static int
2431term_use_loop_check(int check_job_status)
2432{
2433 term_T *term = curbuf->b_term;
2434
2435 return term != NULL
2436 && !term->tl_normal_mode
2437 && term->tl_vterm != NULL
2438 && term_job_running_check(term, check_job_status);
2439}
2440
2441/*
2442 * Returns TRUE if the current window contains a terminal and we are sending
2443 * keys to the job.
2444 */
2445 int
2446term_use_loop(void)
2447{
2448 return term_use_loop_check(FALSE);
2449}
2450
2451/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002452 * Called when entering a window with the mouse. If this is a terminal window
2453 * we may want to change state.
2454 */
2455 void
2456term_win_entered()
2457{
2458 term_T *term = curbuf->b_term;
2459
2460 if (term != NULL)
2461 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002462 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002463 {
2464 reset_VIsual_and_resel();
2465 if (State & INSERT)
2466 stop_insert_mode = TRUE;
2467 }
2468 mouse_was_outside = FALSE;
2469 enter_mouse_col = mouse_col;
2470 enter_mouse_row = mouse_row;
2471 }
2472}
2473
2474/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002475 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2476 * Return the Ctrl-key value in that case.
2477 */
2478 static int
2479raw_c_to_ctrl(int c)
2480{
2481 if ((mod_mask & MOD_MASK_CTRL)
2482 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2483 return c & 0x1f;
2484 return c;
2485}
2486
2487/*
2488 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2489 * May set "mod_mask".
2490 */
2491 static int
2492ctrl_to_raw_c(int c)
2493{
2494 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2495 {
2496 mod_mask |= MOD_MASK_CTRL;
2497 return c + '@';
2498 }
2499 return c;
2500}
2501
2502/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002503 * Wait for input and send it to the job.
2504 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2505 * when there is no more typahead.
2506 * Return when the start of a CTRL-W command is typed or anything else that
2507 * should be handled as a Normal mode command.
2508 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2509 * the terminal was closed.
2510 */
2511 int
2512terminal_loop(int blocking)
2513{
2514 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002515 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002516 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002517 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002518#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002519 int tty_fd = curbuf->b_term->tl_job->jv_channel
2520 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002521#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002522 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002523
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002524 // Remember the terminal we are sending keys to. However, the terminal
2525 // might be closed while waiting for a character, e.g. typing "exit" in a
2526 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2527 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002528 in_terminal_loop = curbuf->b_term;
2529
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002530 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002531 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002532 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002533 if (termwinkey == Ctrl_W)
2534 termwinkey = 0;
2535 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002536 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002537 may_set_cursor_props(curbuf->b_term);
2538
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002539 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002540 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002541#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002542 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002543#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002544 // TODO: skip screen update when handling a sequence of keys.
2545 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002546 while (must_redraw != 0)
2547 if (update_screen(0) == FAIL)
2548 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002549 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002550 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002551 break;
2552
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002553 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002554 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002555
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002556 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002557 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002558 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002559 // Job finished while waiting for a character. Push back the
2560 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002561 if (raw_c != K_IGNORE)
2562 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002563 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002564 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002565 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002566 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002567 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002568
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002569#ifdef UNIX
2570 /*
2571 * The shell or another program may change the tty settings. Getting
2572 * them for every typed character is a bit of overhead, but it's needed
2573 * for the first character typed, e.g. when Vim starts in a shell.
2574 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002575 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002576 {
2577 ttyinfo_T info;
2578
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002579 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002580 if (get_tty_info(tty_fd, &info) == OK)
2581 term_backspace_char = info.backspace;
2582 }
2583#endif
2584
Bram Moolenaar4f974752019-02-17 17:44:42 +01002585#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002586 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2587 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002588 if (ctrl_break_was_pressed)
2589 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2590#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002591 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2592 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002593 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002594#ifdef FEAT_GUI
2595 && !curbuf->b_term->tl_system
2596#endif
2597 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002598 {
2599 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002600 int prev_raw_c = raw_c;
2601 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002602
2603#ifdef FEAT_CMDL_INFO
2604 if (add_to_showcmd(c))
2605 out_flush();
2606#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002607 raw_c = term_vgetc();
2608 c = raw_c_to_ctrl(raw_c);
2609
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002610#ifdef FEAT_CMDL_INFO
2611 clear_showcmd();
2612#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002613 if (!term_use_loop_check(TRUE)
2614 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002615 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002616 break;
2617
2618 if (prev_c == Ctrl_BSL)
2619 {
2620 if (c == Ctrl_N)
2621 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002622 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002623 term_enter_normal_mode();
2624 ret = FAIL;
2625 goto theend;
2626 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002627 // Send both keys to the terminal, first one here, second one
2628 // below.
2629 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2630 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002631 }
2632 else if (c == Ctrl_C)
2633 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002634 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002635 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2636 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002637 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002638 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002639 // "CTRL-W .": send CTRL-W to the job
2640 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002641 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002642 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002643 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002644 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002645 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002646 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002647 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002648 else if (c == 'N')
2649 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002650 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002651 term_enter_normal_mode();
2652 ret = FAIL;
2653 goto theend;
2654 }
2655 else if (c == '"')
2656 {
2657 term_paste_register(prev_c);
2658 continue;
2659 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002660 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002661 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002662 // space for CTRL-W, modifier, multi-byte char and NUL
2663 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002664
2665 // Put the command into the typeahead buffer, when using the
2666 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2667 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002668 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002669 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002670 ret = OK;
2671 goto theend;
2672 }
2673 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002674# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002675 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002676 {
2677 WCHAR wc;
2678 char_u mb[3];
2679
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002680 mb[0] = (unsigned)raw_c >> 8;
2681 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002682 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002683 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002684 }
2685# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002686 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002687 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002688 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002689 // We are sure to come back here, don't reset the cursor color
2690 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002691 restore_cursor = FALSE;
2692
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002693 ret = OK;
2694 goto theend;
2695 }
2696 }
2697 ret = FAIL;
2698
2699theend:
2700 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002701 if (restore_cursor)
2702 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002703
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002704 // Move a snapshot of the screen contents to the buffer, so that completion
2705 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002706 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2707 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002708
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002709 return ret;
2710}
2711
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002712 static void
2713may_toggle_cursor(term_T *term)
2714{
2715 if (in_terminal_loop == term)
2716 {
2717 if (term->tl_cursor_visible)
2718 cursor_on();
2719 else
2720 cursor_off();
2721 }
2722}
2723
2724/*
Bram Moolenaar83d47902020-03-26 20:34:00 +01002725 * Cache "Terminal" highlight group colors.
2726 */
2727 void
2728set_terminal_default_colors(int cterm_fg, int cterm_bg)
2729{
2730 term_default_cterm_fg = cterm_fg - 1;
2731 term_default_cterm_bg = cterm_bg - 1;
2732}
2733
2734 static int
2735get_default_cterm_fg(term_T *term)
2736{
2737 if (term->tl_highlight_name != NULL)
2738 {
2739 int id = syn_name2id(term->tl_highlight_name);
2740 int fg = -1;
2741 int bg = -1;
2742
2743 if (id > 0)
2744 syn_id2cterm_bg(id, &fg, &bg);
2745 return fg;
2746 }
2747 return term_default_cterm_fg;
2748}
2749
2750 static int
2751get_default_cterm_bg(term_T *term)
2752{
2753 if (term->tl_highlight_name != NULL)
2754 {
2755 int id = syn_name2id(term->tl_highlight_name);
2756 int fg = -1;
2757 int bg = -1;
2758
2759 if (id > 0)
2760 syn_id2cterm_bg(id, &fg, &bg);
2761 return bg;
2762 }
2763 return term_default_cterm_bg;
2764}
2765
2766/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002767 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002768 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002769 */
2770 static int
2771color2index(VTermColor *color, int fg, int *boldp)
2772{
2773 int red = color->red;
2774 int blue = color->blue;
2775 int green = color->green;
2776
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002777 if (VTERM_COLOR_IS_DEFAULT_FG(color)
2778 || VTERM_COLOR_IS_DEFAULT_BG(color))
2779 return 0;
2780 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002781 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002782 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002783 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002784 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002785 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002786 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2787 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2788 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002789 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002790 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2791 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2792 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2793 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2794 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2795 case 10: return lookup_color(20, fg, boldp) + 1; // red
2796 case 11: return lookup_color(16, fg, boldp) + 1; // green
2797 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2798 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2799 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2800 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2801 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002802 }
2803 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002804
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002805 if (t_colors >= 256)
2806 {
2807 if (red == blue && red == green)
2808 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002809 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002810 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002811 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2812 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2813 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002814 int i;
2815
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002816 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002817 return 17; // 00/00/00
2818 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002819 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002820 for (i = 0; i < 23; ++i)
2821 if (red < cutoff[i])
2822 return i + 233;
2823 return 256;
2824 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002825 {
2826 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2827 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002828
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002829 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002830 for (ri = 0; ri < 5; ++ri)
2831 if (red < cutoff[ri])
2832 break;
2833 for (gi = 0; gi < 5; ++gi)
2834 if (green < cutoff[gi])
2835 break;
2836 for (bi = 0; bi < 5; ++bi)
2837 if (blue < cutoff[bi])
2838 break;
2839 return 17 + ri * 36 + gi * 6 + bi;
2840 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002841 }
2842 return 0;
2843}
2844
2845/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002846 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002847 */
2848 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002849vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002850{
2851 int attr = 0;
2852
2853 if (cellattrs.bold)
2854 attr |= HL_BOLD;
2855 if (cellattrs.underline)
2856 attr |= HL_UNDERLINE;
2857 if (cellattrs.italic)
2858 attr |= HL_ITALIC;
2859 if (cellattrs.strike)
2860 attr |= HL_STRIKETHROUGH;
2861 if (cellattrs.reverse)
2862 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002863 return attr;
2864}
2865
2866/*
2867 * Store Vterm attributes in "cell" from highlight flags.
2868 */
2869 static void
2870hl2vtermAttr(int attr, cellattr_T *cell)
2871{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002872 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002873 if (attr & HL_BOLD)
2874 cell->attrs.bold = 1;
2875 if (attr & HL_UNDERLINE)
2876 cell->attrs.underline = 1;
2877 if (attr & HL_ITALIC)
2878 cell->attrs.italic = 1;
2879 if (attr & HL_STRIKETHROUGH)
2880 cell->attrs.strike = 1;
2881 if (attr & HL_INVERSE)
2882 cell->attrs.reverse = 1;
2883}
2884
2885/*
2886 * Convert the attributes of a vterm cell into an attribute index.
2887 */
2888 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002889cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002890 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002891 win_T *wp,
2892 VTermScreenCellAttrs cellattrs,
2893 VTermColor cellfg,
2894 VTermColor cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002895{
2896 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002897
2898#ifdef FEAT_GUI
2899 if (gui.in_use)
2900 {
2901 guicolor_T fg, bg;
2902
2903 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2904 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2905 return get_gui_attr_idx(attr, fg, bg);
2906 }
2907 else
2908#endif
2909#ifdef FEAT_TERMGUICOLORS
2910 if (p_tgc)
2911 {
2912 guicolor_T fg, bg;
2913
2914 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2915 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2916
2917 return get_tgc_attr_idx(attr, fg, bg);
2918 }
2919 else
2920#endif
2921 {
2922 int bold = MAYBE;
2923 int fg = color2index(&cellfg, TRUE, &bold);
2924 int bg = color2index(&cellbg, FALSE, &bold);
2925
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002926 // Use the 'wincolor' or "Terminal" highlighting for the default
2927 // colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002928 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002929 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002930 int wincolor_fg = -1;
2931 int wincolor_bg = -1;
2932
2933 if (wp != NULL && *wp->w_p_wcr != NUL)
2934 {
2935 int id = syn_name2id(curwin->w_p_wcr);
2936
2937 // Get the 'wincolor' group colors.
2938 if (id > 0)
2939 syn_id2cterm_bg(id, &wincolor_fg, &wincolor_bg);
2940 }
2941 if (fg == 0)
2942 {
2943 if (wincolor_fg >= 0)
2944 fg = wincolor_fg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002945 else
2946 {
2947 int cterm_fg = get_default_cterm_fg(term);
2948
2949 if (cterm_fg >= 0)
2950 fg = cterm_fg + 1;
2951 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002952 }
2953 if (bg == 0)
2954 {
2955 if (wincolor_bg >= 0)
2956 bg = wincolor_bg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002957 else
2958 {
2959 int cterm_bg = get_default_cterm_bg(term);
2960
2961 if (cterm_bg >= 0)
2962 bg = cterm_bg + 1;
2963 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002964 }
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002965 }
2966
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002967 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002968 if (bold == TRUE)
2969 attr |= HL_BOLD;
2970 return get_cterm_attr_idx(attr, fg, bg);
2971 }
2972 return 0;
2973}
2974
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002975 static void
2976set_dirty_snapshot(term_T *term)
2977{
2978 term->tl_dirty_snapshot = TRUE;
2979#ifdef FEAT_TIMERS
2980 if (!term->tl_normal_mode)
2981 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002982 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002983 profile_setlimit(100L, &term->tl_timer_due);
2984 term->tl_timer_set = TRUE;
2985 }
2986#endif
2987}
2988
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002989 static int
2990handle_damage(VTermRect rect, void *user)
2991{
2992 term_T *term = (term_T *)user;
2993
2994 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2995 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002996 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002997 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002998 return 1;
2999}
3000
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003001 static void
3002term_scroll_up(term_T *term, int start_row, int count)
3003{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003004 win_T *wp = NULL;
3005 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003006 VTermColor fg, bg;
3007 VTermScreenCellAttrs attr;
3008 int clear_attr;
3009
Bram Moolenaara80faa82020-04-12 19:37:17 +02003010 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003011
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003012 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003013 {
3014 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003015 {
3016 // Set the color to clear lines with.
3017 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
3018 &fg, &bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01003019 clear_attr = cell2attr(term, wp, attr, fg, bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003020 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003021 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003022 }
3023}
3024
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003025 static int
3026handle_moverect(VTermRect dest, VTermRect src, void *user)
3027{
3028 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003029 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003030
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003031 // Scrolling up is done much more efficiently by deleting lines instead of
3032 // redrawing the text. But avoid doing this multiple times, postpone until
3033 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003034 if (dest.start_col == src.start_col
3035 && dest.end_col == src.end_col
3036 && dest.start_row < src.start_row)
3037 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003038 if (dest.start_row == 0)
3039 term->tl_postponed_scroll += count;
3040 else
3041 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003042 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003043
3044 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3045 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003046 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003047
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003048 // Note sure if the scrolling will work correctly, let's do a complete
3049 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003050 redraw_buf_later(term->tl_buffer, NOT_VALID);
3051 return 1;
3052}
3053
3054 static int
3055handle_movecursor(
3056 VTermPos pos,
3057 VTermPos oldpos UNUSED,
3058 int visible,
3059 void *user)
3060{
3061 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003062 win_T *wp = NULL;
3063 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003064
3065 term->tl_cursor_pos = pos;
3066 term->tl_cursor_visible = visible;
3067
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003068 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003069 {
3070 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003071 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003072 }
3073 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003074 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003075
3076 return 1;
3077}
3078
3079 static int
3080handle_settermprop(
3081 VTermProp prop,
3082 VTermValue *value,
3083 void *user)
3084{
3085 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003086 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003087
3088 switch (prop)
3089 {
3090 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003091 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003092 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003093 if (strval == NULL)
3094 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003095 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003096 // a blank title isn't useful, make it empty, so that "running" is
3097 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003098 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003099 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003100 // Same as blank
3101 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003102 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003103 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3104 term->tl_title = NULL;
3105 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003106 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003107 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003108#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003109 else if (!enc_utf8 && enc_codepage > 0)
3110 {
3111 WCHAR *ret = NULL;
3112 int length = 0;
3113
3114 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003115 (char*)value->string.str,
3116 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003117 if (ret != NULL)
3118 {
3119 WideCharToMultiByte_alloc(enc_codepage, 0,
3120 ret, length, (char**)&term->tl_title,
3121 &length, 0, 0);
3122 vim_free(ret);
3123 }
3124 }
3125#endif
3126 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003127 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003128 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003129 strval = NULL;
3130 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003131 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003132 if (term == curbuf->b_term)
3133 maketitle();
3134 break;
3135
3136 case VTERM_PROP_CURSORVISIBLE:
3137 term->tl_cursor_visible = value->boolean;
3138 may_toggle_cursor(term);
3139 out_flush();
3140 break;
3141
3142 case VTERM_PROP_CURSORBLINK:
3143 term->tl_cursor_blink = value->boolean;
3144 may_set_cursor_props(term);
3145 break;
3146
3147 case VTERM_PROP_CURSORSHAPE:
3148 term->tl_cursor_shape = value->number;
3149 may_set_cursor_props(term);
3150 break;
3151
3152 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003153 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003154 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003155 if (strval == NULL)
3156 break;
3157 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003158 may_set_cursor_props(term);
3159 break;
3160
3161 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003162 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003163 term->tl_using_altscreen = value->boolean;
3164 break;
3165
3166 default:
3167 break;
3168 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003169 vim_free(strval);
3170
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003171 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003172 return 1;
3173}
3174
3175/*
3176 * The job running in the terminal resized the terminal.
3177 */
3178 static int
3179handle_resize(int rows, int cols, void *user)
3180{
3181 term_T *term = (term_T *)user;
3182 win_T *wp;
3183
3184 term->tl_rows = rows;
3185 term->tl_cols = cols;
3186 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003187 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003188 term->tl_vterm_size_changed = FALSE;
3189 else
3190 {
3191 FOR_ALL_WINDOWS(wp)
3192 {
3193 if (wp->w_buffer == term->tl_buffer)
3194 {
3195 win_setheight_win(rows, wp);
3196 win_setwidth_win(cols, wp);
3197 }
3198 }
3199 redraw_buf_later(term->tl_buffer, NOT_VALID);
3200 }
3201 return 1;
3202}
3203
3204/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003205 * If the number of lines that are stored goes over 'termscrollback' then
3206 * delete the first 10%.
3207 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3208 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003209 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003210 static void
3211limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003212{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003213 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003214 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003215 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003216 int i;
3217
3218 curbuf = term->tl_buffer;
3219 for (i = 0; i < todo; ++i)
3220 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003221 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3222 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003223 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003224 }
3225 curbuf = curwin->w_buffer;
3226
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003227 gap->ga_len -= todo;
3228 mch_memmove(gap->ga_data,
3229 (sb_line_T *)gap->ga_data + todo,
3230 sizeof(sb_line_T) * gap->ga_len);
3231 if (update_buffer)
3232 term->tl_scrollback_scrolled -= todo;
3233 }
3234}
3235
3236/*
3237 * Handle a line that is pushed off the top of the screen.
3238 */
3239 static int
3240handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3241{
3242 term_T *term = (term_T *)user;
3243 garray_T *gap;
3244 int update_buffer;
3245
3246 if (term->tl_normal_mode)
3247 {
3248 // In Terminal-Normal mode the user interacts with the buffer, thus we
3249 // must not change it. Postpone adding the scrollback lines.
3250 gap = &term->tl_scrollback_postponed;
3251 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003252 }
3253 else
3254 {
3255 // First remove the lines that were appended before, the pushed line
3256 // goes above it.
3257 cleanup_scrollback(term);
3258 gap = &term->tl_scrollback;
3259 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003260 }
3261
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003262 limit_scrollback(term, gap, update_buffer);
3263
3264 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003265 {
3266 cellattr_T *p = NULL;
3267 int len = 0;
3268 int i;
3269 int c;
3270 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003271 int text_len;
3272 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003273 sb_line_T *line;
3274 garray_T ga;
3275 cellattr_T fill_attr = term->tl_default_color;
3276
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003277 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003278 for (i = 0; i < cols; ++i)
3279 if (cells[i].chars[0] != 0)
3280 len = i + 1;
3281 else
3282 cell2cellattr(&cells[i], &fill_attr);
3283
3284 ga_init2(&ga, 1, 100);
3285 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003286 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003287 if (p != NULL)
3288 {
3289 for (col = 0; col < len; col += cells[col].width)
3290 {
3291 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3292 {
3293 ga.ga_len = 0;
3294 break;
3295 }
3296 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3297 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3298 (char_u *)ga.ga_data + ga.ga_len);
3299 cell2cellattr(&cells[col], &p[col]);
3300 }
3301 }
3302 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003303 {
3304 if (update_buffer)
3305 text = (char_u *)"";
3306 else
3307 text = vim_strsave((char_u *)"");
3308 text_len = 0;
3309 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003310 else
3311 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003312 text = ga.ga_data;
3313 text_len = ga.ga_len;
3314 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003315 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003316 if (update_buffer)
3317 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003318
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003319 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003320 line->sb_cols = len;
3321 line->sb_cells = p;
3322 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003323 if (update_buffer)
3324 {
3325 line->sb_text = NULL;
3326 ++term->tl_scrollback_scrolled;
3327 ga_clear(&ga); // free the text
3328 }
3329 else
3330 {
3331 line->sb_text = text;
3332 ga_init(&ga); // text is kept in tl_scrollback_postponed
3333 }
3334 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003335 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003336 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003337}
3338
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003339/*
3340 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3341 * received and stored in tl_scrollback_postponed.
3342 */
3343 static void
3344handle_postponed_scrollback(term_T *term)
3345{
3346 int i;
3347
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003348 if (term->tl_scrollback_postponed.ga_len == 0)
3349 return;
3350 ch_log(NULL, "Moving postponed scrollback to scrollback");
3351
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003352 // First remove the lines that were appended before, the pushed lines go
3353 // above it.
3354 cleanup_scrollback(term);
3355
3356 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3357 {
3358 char_u *text;
3359 sb_line_T *pp_line;
3360 sb_line_T *line;
3361
3362 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3363 break;
3364 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3365
3366 text = pp_line->sb_text;
3367 if (text == NULL)
3368 text = (char_u *)"";
3369 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3370 vim_free(pp_line->sb_text);
3371
3372 line = (sb_line_T *)term->tl_scrollback.ga_data
3373 + term->tl_scrollback.ga_len;
3374 line->sb_cols = pp_line->sb_cols;
3375 line->sb_cells = pp_line->sb_cells;
3376 line->sb_fill_attr = pp_line->sb_fill_attr;
3377 line->sb_text = NULL;
3378 ++term->tl_scrollback_scrolled;
3379 ++term->tl_scrollback.ga_len;
3380 }
3381
3382 ga_clear(&term->tl_scrollback_postponed);
3383 limit_scrollback(term, &term->tl_scrollback, TRUE);
3384}
3385
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003386static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003387 handle_damage, // damage
3388 handle_moverect, // moverect
3389 handle_movecursor, // movecursor
3390 handle_settermprop, // settermprop
3391 NULL, // bell
3392 handle_resize, // resize
3393 handle_pushline, // sb_pushline
3394 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003395};
3396
3397/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003398 * Do the work after the channel of a terminal was closed.
3399 * Must be called only when updating_screen is FALSE.
3400 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3401 */
3402 static int
3403term_after_channel_closed(term_T *term)
3404{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003405 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003406 if (!term->tl_normal_mode)
3407 {
3408 int fnum = term->tl_buffer->b_fnum;
3409
3410 cleanup_vterm(term);
3411
3412 if (term->tl_finish == TL_FINISH_CLOSE)
3413 {
3414 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003415 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003416#ifdef FEAT_PROP_POPUP
3417 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003418
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003419 // If this was a terminal in a popup window, go back to the
3420 // previous window.
3421 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3422 {
3423 pwin = curwin;
3424 if (win_valid(prevwin))
3425 win_enter(prevwin, FALSE);
3426 }
3427 else
3428#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003429 // If this is the last normal window: exit Vim.
3430 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3431 {
3432 exarg_T ea;
3433
Bram Moolenaara80faa82020-04-12 19:37:17 +02003434 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003435 ex_quit(&ea);
3436 return TRUE;
3437 }
3438
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003439 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003440 ch_log(NULL, "terminal job finished, closing window");
3441 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003442 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003443 if (curwin == aucmd_win)
3444 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003445 if (do_set_w_closing)
3446 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003447 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003448 if (do_set_w_closing)
3449 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003450 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003451#ifdef FEAT_PROP_POPUP
3452 if (pwin != NULL)
3453 popup_close_with_retval(pwin, 0);
3454#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003455 return TRUE;
3456 }
3457 if (term->tl_finish == TL_FINISH_OPEN
3458 && term->tl_buffer->b_nwindows == 0)
3459 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003460 char *cmd = term->tl_opencmd == NULL
3461 ? "botright sbuf %d"
3462 : (char *)term->tl_opencmd;
3463 size_t len = strlen(cmd) + 50;
3464 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003465
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003466 if (buf != NULL)
3467 {
3468 ch_log(NULL, "terminal job finished, opening window");
3469 vim_snprintf(buf, len, cmd, fnum);
3470 do_cmdline_cmd((char_u *)buf);
3471 vim_free(buf);
3472 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003473 }
3474 else
3475 ch_log(NULL, "terminal job finished");
3476 }
3477
3478 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3479 return FALSE;
3480}
3481
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003482#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3483/*
3484 * If the current window is a terminal in a popup window and the job has
3485 * finished, close the popup window and to back to the previous window.
3486 * Otherwise return FAIL.
3487 */
3488 int
3489may_close_term_popup(void)
3490{
3491 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3492 && !term_job_running(curbuf->b_term))
3493 {
3494 win_T *pwin = curwin;
3495
3496 if (win_valid(prevwin))
3497 win_enter(prevwin, FALSE);
3498 popup_close_with_retval(pwin, 0);
3499 return OK;
3500 }
3501 return FAIL;
3502}
3503#endif
3504
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003505/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003506 * Called when a channel has been closed.
3507 * If this was a channel for a terminal window then finish it up.
3508 */
3509 void
3510term_channel_closed(channel_T *ch)
3511{
3512 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003513 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003514 int did_one = FALSE;
3515
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003516 for (term = first_term; term != NULL; term = next_term)
3517 {
3518 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003519 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003520 {
3521 term->tl_channel_closed = TRUE;
3522 did_one = TRUE;
3523
Bram Moolenaard23a8232018-02-10 18:45:26 +01003524 VIM_CLEAR(term->tl_title);
3525 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003526#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003527 if (term->tl_out_fd != NULL)
3528 {
3529 fclose(term->tl_out_fd);
3530 term->tl_out_fd = NULL;
3531 }
3532#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003533
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003534 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003535 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003536 // Cannot open or close windows now. Can happen when
3537 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003538 term->tl_channel_recently_closed = TRUE;
3539 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003540 }
3541
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003542 if (term_after_channel_closed(term))
3543 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003544 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003545 }
3546
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003547 if (did_one)
3548 {
3549 redraw_statuslines();
3550
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003551 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003552 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003553 typebuf_was_filled = TRUE;
3554
3555 term = curbuf->b_term;
3556 if (term != NULL)
3557 {
3558 if (term->tl_job == ch->ch_job)
3559 maketitle();
3560 update_cursor(term, term->tl_cursor_visible);
3561 }
3562 }
3563}
3564
3565/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003566 * To be called after resetting updating_screen: handle any terminal where the
3567 * channel was closed.
3568 */
3569 void
3570term_check_channel_closed_recently()
3571{
3572 term_T *term;
3573 term_T *next_term;
3574
3575 for (term = first_term; term != NULL; term = next_term)
3576 {
3577 next_term = term->tl_next;
3578 if (term->tl_channel_recently_closed)
3579 {
3580 term->tl_channel_recently_closed = FALSE;
3581 if (term_after_channel_closed(term))
3582 // start over, the list may have changed
3583 next_term = first_term;
3584 }
3585 }
3586}
3587
3588/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003589 * Fill one screen line from a line of the terminal.
3590 * Advances "pos" to past the last column.
3591 */
3592 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003593term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003594 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003595 win_T *wp,
3596 VTermScreen *screen,
3597 VTermPos *pos,
3598 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003599{
3600 int off = screen_get_current_line_off();
3601
3602 for (pos->col = 0; pos->col < max_col; )
3603 {
3604 VTermScreenCell cell;
3605 int c;
3606
3607 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003608 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003609
3610 c = cell.chars[0];
3611 if (c == NUL)
3612 {
3613 ScreenLines[off] = ' ';
3614 if (enc_utf8)
3615 ScreenLinesUC[off] = NUL;
3616 }
3617 else
3618 {
3619 if (enc_utf8)
3620 {
3621 int i;
3622
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003623 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003624 for (i = 0; i < Screen_mco
3625 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3626 {
3627 ScreenLinesC[i][off] = cell.chars[i + 1];
3628 if (cell.chars[i + 1] == 0)
3629 break;
3630 }
3631 if (c >= 0x80 || (Screen_mco > 0
3632 && ScreenLinesC[0][off] != 0))
3633 {
3634 ScreenLines[off] = ' ';
3635 ScreenLinesUC[off] = c;
3636 }
3637 else
3638 {
3639 ScreenLines[off] = c;
3640 ScreenLinesUC[off] = NUL;
3641 }
3642 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003643#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003644 else if (has_mbyte && c >= 0x80)
3645 {
3646 char_u mb[MB_MAXBYTES+1];
3647 WCHAR wc = c;
3648
3649 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3650 (char*)mb, 2, 0, 0) > 1)
3651 {
3652 ScreenLines[off] = mb[0];
3653 ScreenLines[off + 1] = mb[1];
3654 cell.width = mb_ptr2cells(mb);
3655 }
3656 else
3657 ScreenLines[off] = c;
3658 }
3659#endif
3660 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003661 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003662 ScreenLines[off] = c;
3663 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003664 ScreenAttrs[off] = cell2attr(term, wp, cell.attrs, cell.fg, cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003665
3666 ++pos->col;
3667 ++off;
3668 if (cell.width == 2)
3669 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003670 // don't set the second byte to NUL for a DBCS encoding, it
3671 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003672 if (enc_utf8)
3673 {
3674 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003675 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003676 }
3677 else if (!has_mbyte)
3678 {
3679 // Can't show a double-width character with a single-byte
3680 // 'encoding', just use a space.
3681 ScreenLines[off] = ' ';
3682 ScreenAttrs[off] = ScreenAttrs[off - 1];
3683 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003684
3685 ++pos->col;
3686 ++off;
3687 }
3688 }
3689}
3690
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003691#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003692 static void
3693update_system_term(term_T *term)
3694{
3695 VTermPos pos;
3696 VTermScreen *screen;
3697
3698 if (term->tl_vterm == NULL)
3699 return;
3700 screen = vterm_obtain_screen(term->tl_vterm);
3701
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003702 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003703 while (term->tl_toprow > 0
3704 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3705 {
3706 int save_p_more = p_more;
3707
3708 p_more = FALSE;
3709 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003710 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003711 p_more = save_p_more;
3712 --term->tl_toprow;
3713 }
3714
3715 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3716 && pos.row < Rows; ++pos.row)
3717 {
3718 if (pos.row < term->tl_rows)
3719 {
3720 int max_col = MIN(Columns, term->tl_cols);
3721
Bram Moolenaar83d47902020-03-26 20:34:00 +01003722 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003723 }
3724 else
3725 pos.col = 0;
3726
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003727 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003728 }
3729
3730 term->tl_dirty_row_start = MAX_ROW;
3731 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003732}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003733#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003734
3735/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003736 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3737 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003738 * Terminal-Normal mode.
3739 */
3740 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003741term_do_update_window(win_T *wp)
3742{
3743 term_T *term = wp->w_buffer->b_term;
3744
3745 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3746}
3747
3748/*
3749 * Called to update a window that contains an active terminal.
3750 */
3751 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003752term_update_window(win_T *wp)
3753{
3754 term_T *term = wp->w_buffer->b_term;
3755 VTerm *vterm;
3756 VTermScreen *screen;
3757 VTermState *state;
3758 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003759 int rows, cols;
3760 int newrows, newcols;
3761 int minsize;
3762 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003763
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003764 vterm = term->tl_vterm;
3765 screen = vterm_obtain_screen(vterm);
3766 state = vterm_obtain_state(vterm);
3767
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003768 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3769 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003770 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003771 {
3772 term->tl_dirty_row_start = 0;
3773 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003774
3775 if (term->tl_postponed_scroll > 0
3776 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003777 // Scrolling is usually faster than redrawing, when there are only
3778 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003779 term_scroll_up(term, 0, term->tl_postponed_scroll);
3780 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003781 }
3782
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003783 /*
3784 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003785 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003786 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003787 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003788
Bram Moolenaar498c2562018-04-15 23:45:15 +02003789 newrows = 99999;
3790 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003791 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003792 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003793 // Always use curwin, it may be a popup window.
3794 win_T *wwp = twp == NULL ? curwin : twp;
3795
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003796 // When more than one window shows the same terminal, use the
3797 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003798 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003799 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003800 newrows = MIN(newrows, wwp->w_height);
3801 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003802 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003803 if (twp == NULL)
3804 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003805 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003806 if (newrows == 99999 || newcols == 99999)
3807 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003808 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3809 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3810
Bram Moolenaareba13e42021-02-23 17:47:23 +01003811 // If no cell is visible there is no point in resizing. Also, vterm can't
3812 // handle a zero height.
3813 if (newrows == 0 || newcols == 0)
3814 return;
3815
Bram Moolenaar498c2562018-04-15 23:45:15 +02003816 if (term->tl_rows != newrows || term->tl_cols != newcols)
3817 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003818 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003819 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003820 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003821 newrows);
3822 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003823
3824 // Updating the terminal size will cause the snapshot to be cleared.
3825 // When not in terminal_loop() we need to restore it.
3826 if (term != in_terminal_loop)
3827 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003828 }
3829
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003830 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003831 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003832 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003833
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003834 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3835 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003836 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003837 if (pos.row < term->tl_rows)
3838 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003839 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003840
Bram Moolenaar83d47902020-03-26 20:34:00 +01003841 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003842 }
3843 else
3844 pos.col = 0;
3845
Bram Moolenaarf118d482018-03-13 13:14:00 +01003846 screen_line(wp->w_winrow + pos.row
3847#ifdef FEAT_MENU
3848 + winbar_height(wp)
3849#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003850 , wp->w_wincol, pos.col, wp->w_width,
3851#ifdef FEAT_PROP_POPUP
3852 popup_is_popup(wp) ? SLF_POPUP :
3853#endif
3854 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003855 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003856 term->tl_dirty_row_start = MAX_ROW;
3857 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003858}
3859
3860/*
3861 * Return TRUE if "wp" is a terminal window where the job has finished.
3862 */
3863 int
3864term_is_finished(buf_T *buf)
3865{
3866 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3867}
3868
3869/*
3870 * Return TRUE if "wp" is a terminal window where the job has finished or we
3871 * are in Terminal-Normal mode, thus we show the buffer contents.
3872 */
3873 int
3874term_show_buffer(buf_T *buf)
3875{
3876 term_T *term = buf->b_term;
3877
3878 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3879}
3880
3881/*
3882 * The current buffer is going to be changed. If there is terminal
3883 * highlighting remove it now.
3884 */
3885 void
3886term_change_in_curbuf(void)
3887{
3888 term_T *term = curbuf->b_term;
3889
3890 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3891 {
3892 free_scrollback(term);
3893 redraw_buf_later(term->tl_buffer, NOT_VALID);
3894
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003895 // The buffer is now like a normal buffer, it cannot be easily
3896 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003897 set_string_option_direct((char_u *)"buftype", -1,
3898 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3899 }
3900}
3901
3902/*
3903 * Get the screen attribute for a position in the buffer.
3904 * Use a negative "col" to get the filler background color.
3905 */
3906 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003907term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003908{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003909 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003910 term_T *term = buf->b_term;
3911 sb_line_T *line;
3912 cellattr_T *cellattr;
3913
3914 if (lnum > term->tl_scrollback.ga_len)
3915 cellattr = &term->tl_default_color;
3916 else
3917 {
3918 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3919 if (col < 0 || col >= line->sb_cols)
3920 cellattr = &line->sb_fill_attr;
3921 else
3922 cellattr = line->sb_cells + col;
3923 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003924 return cell2attr(term, wp, cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003925}
3926
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003927/*
3928 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003929 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003930 */
3931 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003932cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003933{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003934 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3935 if (rgb->index == 0)
3936 rgb->type = VTERM_COLOR_RGB;
3937 else
3938 {
3939 rgb->type = VTERM_COLOR_INDEXED;
3940 --rgb->index;
3941 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003942}
3943
3944/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003945 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003946 */
3947 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003948init_default_colors(term_T *term, win_T *wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003949{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003950 VTermColor *fg, *bg;
3951 int fgval, bgval;
3952 int id;
3953
Bram Moolenaara80faa82020-04-12 19:37:17 +02003954 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003955 term->tl_default_color.width = 1;
3956 fg = &term->tl_default_color.fg;
3957 bg = &term->tl_default_color.bg;
3958
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003959 // Vterm uses a default black background. Set it to white when
3960 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003961 if (*p_bg == 'l')
3962 {
3963 fgval = 0;
3964 bgval = 255;
3965 }
3966 else
3967 {
3968 fgval = 255;
3969 bgval = 0;
3970 }
3971 fg->red = fg->green = fg->blue = fgval;
3972 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003973 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3974 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003975
Bram Moolenaar83d47902020-03-26 20:34:00 +01003976 // The 'wincolor' or the highlight group overrules the defaults.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003977 if (wp != NULL && *wp->w_p_wcr != NUL)
3978 id = syn_name2id(wp->w_p_wcr);
3979 else
Bram Moolenaar83d47902020-03-26 20:34:00 +01003980 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003981
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003982 // Use the actual color for the GUI and when 'termguicolors' is set.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003983#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3984 if (0
3985# ifdef FEAT_GUI
3986 || gui.in_use
3987# endif
3988# ifdef FEAT_TERMGUICOLORS
3989 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003990# ifdef FEAT_VTP
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003991 // Finally get INVALCOLOR on this execution path
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003992 || (!p_tgc && t_colors >= 256)
3993# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003994# endif
3995 )
3996 {
3997 guicolor_T fg_rgb = INVALCOLOR;
3998 guicolor_T bg_rgb = INVALCOLOR;
3999
4000 if (id != 0)
4001 syn_id2colors(id, &fg_rgb, &bg_rgb);
4002
4003# ifdef FEAT_GUI
4004 if (gui.in_use)
4005 {
4006 if (fg_rgb == INVALCOLOR)
4007 fg_rgb = gui.norm_pixel;
4008 if (bg_rgb == INVALCOLOR)
4009 bg_rgb = gui.back_pixel;
4010 }
4011# ifdef FEAT_TERMGUICOLORS
4012 else
4013# endif
4014# endif
4015# ifdef FEAT_TERMGUICOLORS
4016 {
4017 if (fg_rgb == INVALCOLOR)
4018 fg_rgb = cterm_normal_fg_gui_color;
4019 if (bg_rgb == INVALCOLOR)
4020 bg_rgb = cterm_normal_bg_gui_color;
4021 }
4022# endif
4023 if (fg_rgb != INVALCOLOR)
4024 {
4025 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
4026
4027 fg->red = (unsigned)(rgb >> 16);
4028 fg->green = (unsigned)(rgb >> 8) & 255;
4029 fg->blue = (unsigned)rgb & 255;
4030 }
4031 if (bg_rgb != INVALCOLOR)
4032 {
4033 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
4034
4035 bg->red = (unsigned)(rgb >> 16);
4036 bg->green = (unsigned)(rgb >> 8) & 255;
4037 bg->blue = (unsigned)rgb & 255;
4038 }
4039 }
4040 else
4041#endif
4042 if (id != 0 && t_colors >= 16)
4043 {
Bram Moolenaar83d47902020-03-26 20:34:00 +01004044 int cterm_fg = get_default_cterm_fg(term);
4045 int cterm_bg = get_default_cterm_bg(term);
4046
4047 if (cterm_fg >= 0)
4048 cterm_color2vterm(cterm_fg, fg);
4049 if (cterm_bg >= 0)
4050 cterm_color2vterm(cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004051 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004052 else
4053 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004054#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004055 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004056#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004057
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004058 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004059 if (cterm_normal_fg_color > 0)
4060 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004061 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004062# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4063# ifdef VIMDLL
4064 if (!gui.in_use)
4065# endif
4066 {
4067 tmp = fg->red;
4068 fg->red = fg->blue;
4069 fg->blue = tmp;
4070 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004071# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004072 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004073# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004074 else
4075 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004076# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004077
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004078 if (cterm_normal_bg_color > 0)
4079 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004080 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004081# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4082# ifdef VIMDLL
4083 if (!gui.in_use)
4084# endif
4085 {
4086 tmp = fg->red;
4087 fg->red = fg->blue;
4088 fg->blue = tmp;
4089 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004090# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004091 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004092# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004093 else
4094 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004095# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004096 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004097}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004098
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004099#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4100/*
4101 * Set the 16 ANSI colors from array of RGB values
4102 */
4103 static void
4104set_vterm_palette(VTerm *vterm, long_u *rgb)
4105{
4106 int index = 0;
4107 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004108
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004109 for (; index < 16; index++)
4110 {
4111 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004112
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004113 color.red = (unsigned)(rgb[index] >> 16);
4114 color.green = (unsigned)(rgb[index] >> 8) & 255;
4115 color.blue = (unsigned)rgb[index] & 255;
4116 vterm_state_set_palette_color(state, index, &color);
4117 }
4118}
4119
4120/*
4121 * Set the ANSI color palette from a list of colors
4122 */
4123 static int
4124set_ansi_colors_list(VTerm *vterm, list_T *list)
4125{
4126 int n = 0;
4127 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004128 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004129
Bram Moolenaarb0992022020-01-30 14:55:42 +01004130 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004131 {
4132 char_u *color_name;
4133 guicolor_T guicolor;
4134
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004135 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004136 if (color_name == NULL)
4137 return FAIL;
4138
4139 guicolor = GUI_GET_COLOR(color_name);
4140 if (guicolor == INVALCOLOR)
4141 return FAIL;
4142
4143 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4144 }
4145
4146 if (n != 16 || li != NULL)
4147 return FAIL;
4148
4149 set_vterm_palette(vterm, rgb);
4150
4151 return OK;
4152}
4153
4154/*
4155 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4156 */
4157 static void
4158init_vterm_ansi_colors(VTerm *vterm)
4159{
4160 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4161
4162 if (var != NULL
4163 && (var->di_tv.v_type != VAR_LIST
4164 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004165 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004166 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004167 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004168}
4169#endif
4170
Bram Moolenaar52acb112018-03-18 19:20:22 +01004171/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004172 * Handles a "drop" command from the job in the terminal.
4173 * "item" is the file name, "item->li_next" may have options.
4174 */
4175 static void
4176handle_drop_command(listitem_T *item)
4177{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004178 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004179 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004180 int bufnr;
4181 win_T *wp;
4182 tabpage_T *tp;
4183 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004184 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004185
4186 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4187 FOR_ALL_TAB_WINDOWS(tp, wp)
4188 {
4189 if (wp->w_buffer->b_fnum == bufnr)
4190 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004191 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004192 goto_tabpage_win(tp, wp);
4193 return;
4194 }
4195 }
4196
Bram Moolenaara80faa82020-04-12 19:37:17 +02004197 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004198
4199 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4200 && opt_item->li_tv.vval.v_dict != NULL)
4201 {
4202 dict_T *dict = opt_item->li_tv.vval.v_dict;
4203 char_u *p;
4204
Bram Moolenaar8f667172018-12-14 15:38:31 +01004205 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004206 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004207 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004208 if (p != NULL)
4209 {
4210 if (check_ff_value(p) == FAIL)
4211 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4212 else
4213 ea.force_ff = *p;
4214 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004215 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004216 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004217 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004218 if (p != NULL)
4219 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004220 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004221 if (ea.cmd != NULL)
4222 {
4223 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4224 ea.force_enc = 11;
4225 tofree = ea.cmd;
4226 }
4227 }
4228
Bram Moolenaar8f667172018-12-14 15:38:31 +01004229 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004230 if (p != NULL)
4231 get_bad_opt(p, &ea);
4232
4233 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4234 ea.force_bin = FORCE_BIN;
4235 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4236 ea.force_bin = FORCE_BIN;
4237 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4238 ea.force_bin = FORCE_NOBIN;
4239 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4240 ea.force_bin = FORCE_NOBIN;
4241 }
4242
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004243 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004244 if (ea.cmd == NULL)
4245 ea.cmd = (char_u *)"split";
4246 ea.arg = fname;
4247 ea.cmdidx = CMD_split;
4248 ex_splitview(&ea);
4249
4250 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004251}
4252
4253/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004254 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4255 */
4256 static int
4257is_permitted_term_api(char_u *func, char_u *pat)
4258{
4259 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4260}
4261
4262/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004263 * Handles a function call from the job running in a terminal.
4264 * "item" is the function name, "item->li_next" has the arguments.
4265 */
4266 static void
4267handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4268{
4269 char_u *func;
4270 typval_T argvars[2];
4271 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004272 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004273
4274 if (item->li_next == NULL)
4275 {
4276 ch_log(channel, "Missing function arguments for call");
4277 return;
4278 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004279 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004280
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004281 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004282 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004283 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004284 return;
4285 }
4286
4287 argvars[0].v_type = VAR_NUMBER;
4288 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4289 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004290 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004291 funcexe.firstline = 1L;
4292 funcexe.lastline = 1L;
4293 funcexe.evaluate = TRUE;
4294 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004295 {
4296 clear_tv(&rettv);
4297 ch_log(channel, "Function %s called", func);
4298 }
4299 else
4300 ch_log(channel, "Calling function %s failed", func);
4301}
4302
4303/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004304 * URL decoding (also know as Percent-encoding).
4305 *
4306 * Note this function currently is only used for decoding shell's
4307 * OSC 7 escape sequence which we can assume all bytes are valid
4308 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4309 * encoding bytes like 0xfe, 0xff.
4310 */
4311 static size_t
4312url_decode(const char *src, const size_t len, char_u *dst)
4313{
4314 size_t i = 0, j = 0;
4315
4316 while (i < len)
4317 {
4318 if (src[i] == '%' && i + 2 < len)
4319 {
4320 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4321 j++;
4322 i += 3;
4323 }
4324 else
4325 {
4326 dst[j] = src[i];
4327 i++;
4328 j++;
4329 }
4330 }
4331 dst[j] = '\0';
4332 return j;
4333}
4334
4335/*
4336 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4337 *
4338 * The OSC 7 sequence has the format of
4339 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4340 * and what VTerm provides via VTermStringFragment is
4341 * "file://HOSTNAME/CURRENT/DIR"
4342 */
4343 static void
4344sync_shell_dir(VTermStringFragment *frag)
4345{
4346 int offset = 7; // len of "file://" is 7
4347 char *pos = (char *)frag->str + offset;
4348 char_u *new_dir;
4349
4350 // remove HOSTNAME to get PWD
Bram Moolenaar918b0892021-05-08 20:09:24 +02004351 while (*pos != '/' && offset < (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004352 {
4353 offset += 1;
4354 pos += 1;
4355 }
4356
Bram Moolenaar918b0892021-05-08 20:09:24 +02004357 if (offset >= (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004358 {
4359 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
4360 frag->str);
4361 return;
4362 }
4363
4364 new_dir = alloc(frag->len - offset + 1);
4365 url_decode(pos, frag->len-offset, new_dir);
4366 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4367 vim_free(new_dir);
4368}
4369
4370/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004371 * Called by libvterm when it cannot recognize an OSC sequence.
4372 * We recognize a terminal API command.
4373 */
4374 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004375parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004376{
4377 term_T *term = (term_T *)user;
4378 js_read_T reader;
4379 typval_T tv;
4380 channel_T *channel = term->tl_job == NULL ? NULL
4381 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004382 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004383
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004384 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
4385 if (p_asd && command == 7)
4386 {
4387 sync_shell_dir(&frag);
4388 return 1;
4389 }
4390
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004391 if (command != 51)
4392 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004393
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004394 // Concatenate what was received until the final piece is found.
4395 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4396 {
4397 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004398 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004399 }
4400 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004401 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004402 if (!frag.final)
4403 return 1;
4404
4405 ((char *)gap->ga_data)[gap->ga_len] = 0;
4406 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004407 reader.js_fill = NULL;
4408 reader.js_used = 0;
4409 if (json_decode(&reader, &tv, 0) == OK
4410 && tv.v_type == VAR_LIST
4411 && tv.vval.v_list != NULL)
4412 {
4413 listitem_T *item = tv.vval.v_list->lv_first;
4414
4415 if (item == NULL)
4416 ch_log(channel, "Missing command");
4417 else
4418 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004419 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004420
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004421 // Make sure an invoked command doesn't delete the buffer (and the
4422 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004423 ++term->tl_buffer->b_locked;
4424
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004425 item = item->li_next;
4426 if (item == NULL)
4427 ch_log(channel, "Missing argument for %s", cmd);
4428 else if (STRCMP(cmd, "drop") == 0)
4429 handle_drop_command(item);
4430 else if (STRCMP(cmd, "call") == 0)
4431 handle_call_command(term, channel, item);
4432 else
4433 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004434 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004435 }
4436 }
4437 else
4438 ch_log(channel, "Invalid JSON received");
4439
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004440 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004441 clear_tv(&tv);
4442 return 1;
4443}
4444
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004445/*
4446 * Called by libvterm when it cannot recognize a CSI sequence.
4447 * We recognize the window position report.
4448 */
4449 static int
4450parse_csi(
4451 const char *leader UNUSED,
4452 const long args[],
4453 int argcount,
4454 const char *intermed UNUSED,
4455 char command,
4456 void *user)
4457{
4458 term_T *term = (term_T *)user;
4459 char buf[100];
4460 int len;
4461 int x = 0;
4462 int y = 0;
4463 win_T *wp;
4464
4465 // We recognize only CSI 13 t
4466 if (command != 't' || argcount != 1 || args[0] != 13)
4467 return 0; // not handled
4468
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004469 // When getting the window position is not possible or it fails it results
4470 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004471#if defined(FEAT_GUI) \
4472 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4473 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004474 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004475#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004476
4477 FOR_ALL_WINDOWS(wp)
4478 if (wp->w_buffer == term->tl_buffer)
4479 break;
4480 if (wp != NULL)
4481 {
4482#ifdef FEAT_GUI
4483 if (gui.in_use)
4484 {
4485 x += wp->w_wincol * gui.char_width;
4486 y += W_WINROW(wp) * gui.char_height;
4487 }
4488 else
4489#endif
4490 {
4491 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004492 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004493 x += wp->w_wincol * 7;
4494 y += W_WINROW(wp) * 10;
4495 }
4496 }
4497
4498 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4499 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4500 (char_u *)buf, len, NULL);
4501 return 1;
4502}
4503
Bram Moolenaard8637282020-05-20 18:41:41 +02004504static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004505 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004506 parse_csi, // csi
4507 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004508 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004509};
4510
4511/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004512 * Use Vim's allocation functions for vterm so profiling works.
4513 */
4514 static void *
4515vterm_malloc(size_t size, void *data UNUSED)
4516{
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004517 return alloc_clear(size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004518}
4519
4520 static void
4521vterm_memfree(void *ptr, void *data UNUSED)
4522{
4523 vim_free(ptr);
4524}
4525
4526static VTermAllocatorFunctions vterm_allocator = {
4527 &vterm_malloc,
4528 &vterm_memfree
4529};
4530
4531/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004532 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004533 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004534 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004535 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004536create_vterm(term_T *term, int rows, int cols)
4537{
4538 VTerm *vterm;
4539 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004540 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004541 VTermValue value;
4542
Bram Moolenaar756ef112018-04-10 12:04:27 +02004543 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004544 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004545 if (vterm == NULL)
4546 return FAIL;
4547
4548 // Allocate screen and state here, so we can bail out if that fails.
4549 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004550 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004551 if (state == NULL || screen == NULL)
4552 {
4553 vterm_free(vterm);
4554 return FAIL;
4555 }
4556
Bram Moolenaar52acb112018-03-18 19:20:22 +01004557 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004558 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004559 vterm_set_utf8(vterm, 1);
4560
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004561 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004562
4563 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004564 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004565 &term->tl_default_color.fg,
4566 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004567
Bram Moolenaar9e587872019-05-13 20:27:23 +02004568 if (t_colors < 16)
4569 // Less than 16 colors: assume that bold means using a bright color for
4570 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004571 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4572
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004573 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004574 vterm_screen_reset(screen, 1 /* hard */);
4575
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004576 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004577 vterm_screen_enable_altscreen(screen, 1);
4578
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004579 // For unix do not use a blinking cursor. In an xterm this causes the
4580 // cursor to blink if it's blinking in the xterm.
4581 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004582#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004583 if (GetCaretBlinkTime() == INFINITE)
4584 value.boolean = 0;
4585 else
4586 value.boolean = 1;
4587#else
4588 value.boolean = 0;
4589#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004590 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004591 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004592
4593 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004594}
4595
4596/*
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004597 * Called when 'wincolor' was set.
4598 */
4599 void
Bram Moolenaarad431992021-05-03 20:40:38 +02004600term_update_colors(term_T *term)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004601{
Bram Moolenaarad431992021-05-03 20:40:38 +02004602 win_T *wp;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004603
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01004604 if (term->tl_vterm == NULL)
4605 return;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004606 init_default_colors(term, curwin);
4607 vterm_state_set_default_colors(
4608 vterm_obtain_state(term->tl_vterm),
4609 &term->tl_default_color.fg,
4610 &term->tl_default_color.bg);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01004611
Bram Moolenaarad431992021-05-03 20:40:38 +02004612 FOR_ALL_WINDOWS(wp)
4613 if (wp->w_buffer == term->tl_buffer)
4614 redraw_win_later(wp, NOT_VALID);
4615}
4616
4617/*
4618 * Called when 'background' was set.
4619 */
4620 void
4621term_update_colors_all(void)
4622{
4623 term_T *tp;
4624
4625 FOR_ALL_TERMS(tp)
4626 term_update_colors(tp);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004627}
4628
4629/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004630 * Return the text to show for the buffer name and status.
4631 */
4632 char_u *
4633term_get_status_text(term_T *term)
4634{
4635 if (term->tl_status_text == NULL)
4636 {
4637 char_u *txt;
4638 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004639 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004640
4641 if (term->tl_normal_mode)
4642 {
4643 if (term_job_running(term))
4644 txt = (char_u *)_("Terminal");
4645 else
4646 txt = (char_u *)_("Terminal-finished");
4647 }
4648 else if (term->tl_title != NULL)
4649 txt = term->tl_title;
4650 else if (term_none_open(term))
4651 txt = (char_u *)_("active");
4652 else if (term_job_running(term))
4653 txt = (char_u *)_("running");
4654 else
4655 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004656 fname = buf_get_fname(term->tl_buffer);
4657 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004658 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004659 if (term->tl_status_text != NULL)
4660 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004661 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004662 }
4663 return term->tl_status_text;
4664}
4665
4666/*
4667 * Mark references in jobs of terminals.
4668 */
4669 int
4670set_ref_in_term(int copyID)
4671{
4672 int abort = FALSE;
4673 term_T *term;
4674 typval_T tv;
4675
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004676 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004677 if (term->tl_job != NULL)
4678 {
4679 tv.v_type = VAR_JOB;
4680 tv.vval.v_job = term->tl_job;
4681 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4682 }
4683 return abort;
4684}
4685
4686/*
4687 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004688 * Returns NULL when the buffer is not for a terminal window and logs a message
4689 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004690 */
4691 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004692term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004693{
4694 buf_T *buf;
4695
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004696 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004697 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004698 --emsg_off;
4699 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004700 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004701 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004702 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004703 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004704 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004705 return buf;
4706}
4707
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004708 static void
4709clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004710{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004711 CLEAR_FIELD(*cell);
4712 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4713 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004714}
4715
4716 static void
4717dump_term_color(FILE *fd, VTermColor *color)
4718{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004719 int index;
4720
4721 if (VTERM_COLOR_IS_INDEXED(color))
4722 index = color->index + 1;
4723 else if (color->type == 0)
4724 // use RGB values
4725 index = 255;
4726 else
4727 // default color
4728 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004729 fprintf(fd, "%02x%02x%02x%d",
4730 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004731 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004732}
4733
4734/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004735 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004736 *
4737 * Each screen cell in full is:
4738 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4739 * {characters} is a space for an empty cell
4740 * For a double-width character "+" is changed to "*" and the next cell is
4741 * skipped.
4742 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4743 * when "&" use the same as the previous cell.
4744 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4745 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4746 * {color-idx} is a number from 0 to 255
4747 *
4748 * Screen cell with same width, attributes and color as the previous one:
4749 * |{characters}
4750 *
4751 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4752 *
4753 * Repeating the previous screen cell:
4754 * @{count}
4755 */
4756 void
4757f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4758{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004759 buf_T *buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004760 term_T *term;
4761 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004762 int max_height = 0;
4763 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004764 stat_T st;
4765 FILE *fd;
4766 VTermPos pos;
4767 VTermScreen *screen;
4768 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004769 VTermState *state;
4770 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004771
4772 if (check_restricted() || check_secure())
4773 return;
4774 if (buf == NULL)
4775 return;
4776 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004777 if (term->tl_vterm == NULL)
4778 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004779 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004780 return;
4781 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004782
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004783 if (argvars[2].v_type != VAR_UNKNOWN)
4784 {
4785 dict_T *d;
4786
4787 if (argvars[2].v_type != VAR_DICT)
4788 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004789 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004790 return;
4791 }
4792 d = argvars[2].vval.v_dict;
4793 if (d != NULL)
4794 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004795 max_height = dict_get_number(d, (char_u *)"rows");
4796 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004797 }
4798 }
4799
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004800 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004801 if (fname == NULL)
4802 return;
4803 if (mch_stat((char *)fname, &st) >= 0)
4804 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004805 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004806 return;
4807 }
4808
Bram Moolenaard96ff162018-02-18 22:13:29 +01004809 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4810 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004811 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004812 return;
4813 }
4814
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004815 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004816
4817 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004818 state = vterm_obtain_state(term->tl_vterm);
4819 vterm_state_get_cursorpos(state, &cursor_pos);
4820
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004821 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4822 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004823 {
4824 int repeat = 0;
4825
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004826 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4827 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004828 {
4829 VTermScreenCell cell;
4830 int same_attr;
4831 int same_chars = TRUE;
4832 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004833 int is_cursor_pos = (pos.col == cursor_pos.col
4834 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004835
4836 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004837 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004838
4839 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4840 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004841 int c = cell.chars[i];
4842 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004843 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004844
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004845 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004846 if (i == 0)
4847 {
4848 c = (c == NUL) ? ' ' : c;
4849 pc = (pc == NUL) ? ' ' : pc;
4850 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004851 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004852 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004853 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004854 break;
4855 }
4856 same_attr = vtermAttr2hl(cell.attrs)
4857 == vtermAttr2hl(prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004858 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4859 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004860 if (same_chars && cell.width == prev_cell.width && same_attr
4861 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004862 {
4863 ++repeat;
4864 }
4865 else
4866 {
4867 if (repeat > 0)
4868 {
4869 fprintf(fd, "@%d", repeat);
4870 repeat = 0;
4871 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004872 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004873
4874 if (cell.chars[0] == NUL)
4875 fputs(" ", fd);
4876 else
4877 {
4878 char_u charbuf[10];
4879 int len;
4880
4881 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4882 && cell.chars[i] != NUL; ++i)
4883 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004884 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004885 fwrite(charbuf, len, 1, fd);
4886 }
4887 }
4888
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004889 // When only the characters differ we don't write anything, the
4890 // following "|", "@" or NL will indicate using the same
4891 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004892 if (cell.width != prev_cell.width || !same_attr)
4893 {
4894 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004895 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004896 else
4897 fputs("+", fd);
4898
4899 if (same_attr)
4900 {
4901 fputs("&", fd);
4902 }
4903 else
4904 {
4905 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004906 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004907 fputs("&", fd);
4908 else
4909 {
4910 fputs("#", fd);
4911 dump_term_color(fd, &cell.fg);
4912 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004913 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004914 fputs("&", fd);
4915 else
4916 {
4917 fputs("#", fd);
4918 dump_term_color(fd, &cell.bg);
4919 }
4920 }
4921 }
4922
4923 prev_cell = cell;
4924 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004925
4926 if (cell.width == 2)
4927 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004928 }
4929 if (repeat > 0)
4930 fprintf(fd, "@%d", repeat);
4931 fputs("\n", fd);
4932 }
4933
4934 fclose(fd);
4935}
4936
4937/*
4938 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4939 */
4940 static void
4941dump_is_corrupt(garray_T *gap)
4942{
4943 ga_concat(gap, (char_u *)"CORRUPT");
4944}
4945
4946 static void
4947append_cell(garray_T *gap, cellattr_T *cell)
4948{
4949 if (ga_grow(gap, 1) == OK)
4950 {
4951 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4952 ++gap->ga_len;
4953 }
4954}
4955
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004956 static void
4957clear_cellattr(cellattr_T *cell)
4958{
4959 CLEAR_FIELD(*cell);
4960 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4961 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4962}
4963
Bram Moolenaard96ff162018-02-18 22:13:29 +01004964/*
4965 * Read the dump file from "fd" and append lines to the current buffer.
4966 * Return the cell width of the longest line.
4967 */
4968 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004969read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004970{
4971 int c;
4972 garray_T ga_text;
4973 garray_T ga_cell;
4974 char_u *prev_char = NULL;
4975 int attr = 0;
4976 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004977 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004978 term_T *term = curbuf->b_term;
4979 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004980 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004981
4982 ga_init2(&ga_text, 1, 90);
4983 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004984 clear_cellattr(&cell);
4985 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004986 cursor_pos->row = -1;
4987 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004988
4989 c = fgetc(fd);
4990 for (;;)
4991 {
4992 if (c == EOF)
4993 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004994 if (c == '\r')
4995 {
4996 // DOS line endings? Ignore.
4997 c = fgetc(fd);
4998 }
4999 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005000 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005001 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005002 if (ga_text.ga_data == NULL)
5003 dump_is_corrupt(&ga_text);
5004 if (ga_grow(&term->tl_scrollback, 1) == OK)
5005 {
5006 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
5007 + term->tl_scrollback.ga_len;
5008
5009 if (max_cells < ga_cell.ga_len)
5010 max_cells = ga_cell.ga_len;
5011 line->sb_cols = ga_cell.ga_len;
5012 line->sb_cells = ga_cell.ga_data;
5013 line->sb_fill_attr = term->tl_default_color;
5014 ++term->tl_scrollback.ga_len;
5015 ga_init(&ga_cell);
5016
5017 ga_append(&ga_text, NUL);
5018 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5019 ga_text.ga_len, FALSE);
5020 }
5021 else
5022 ga_clear(&ga_cell);
5023 ga_text.ga_len = 0;
5024
5025 c = fgetc(fd);
5026 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005027 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005028 {
5029 int prev_len = ga_text.ga_len;
5030
Bram Moolenaar9271d052018-02-25 21:39:46 +01005031 if (c == '>')
5032 {
5033 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005034 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005035 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5036 cursor_pos->col = ga_cell.ga_len;
5037 }
5038
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005039 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005040 c = fgetc(fd);
5041 if (c != EOF)
5042 ga_append(&ga_text, c);
5043 for (;;)
5044 {
5045 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005046 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005047 || c == EOF || c == '\n')
5048 break;
5049 ga_append(&ga_text, c);
5050 }
5051
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005052 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005053 vim_free(prev_char);
5054 if (ga_text.ga_data != NULL)
5055 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5056 ga_text.ga_len - prev_len);
5057
Bram Moolenaar9271d052018-02-25 21:39:46 +01005058 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005059 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005060 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005061 }
5062 else if (c == '+' || c == '*')
5063 {
5064 int is_bg;
5065
5066 cell.width = c == '+' ? 1 : 2;
5067
5068 c = fgetc(fd);
5069 if (c == '&')
5070 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005071 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005072 c = fgetc(fd);
5073 }
5074 else if (isdigit(c))
5075 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005076 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005077 attr = 0;
5078 while (isdigit(c))
5079 {
5080 attr = attr * 10 + (c - '0');
5081 c = fgetc(fd);
5082 }
5083 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005084
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005085 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005086 for (is_bg = 0; is_bg <= 1; ++is_bg)
5087 {
5088 if (c == '&')
5089 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005090 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005091 c = fgetc(fd);
5092 }
5093 else if (c == '#')
5094 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005095 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005096
5097 c = fgetc(fd);
5098 red = hex2nr(c);
5099 c = fgetc(fd);
5100 red = (red << 4) + hex2nr(c);
5101 c = fgetc(fd);
5102 green = hex2nr(c);
5103 c = fgetc(fd);
5104 green = (green << 4) + hex2nr(c);
5105 c = fgetc(fd);
5106 blue = hex2nr(c);
5107 c = fgetc(fd);
5108 blue = (blue << 4) + hex2nr(c);
5109 c = fgetc(fd);
5110 if (!isdigit(c))
5111 dump_is_corrupt(&ga_text);
5112 while (isdigit(c))
5113 {
5114 index = index * 10 + (c - '0');
5115 c = fgetc(fd);
5116 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005117 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005118 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005119 type = VTERM_COLOR_RGB;
5120 if (index == 0)
5121 {
5122 if (is_bg)
5123 type |= VTERM_COLOR_DEFAULT_BG;
5124 else
5125 type |= VTERM_COLOR_DEFAULT_FG;
5126 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005127 }
5128 else
5129 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005130 type = VTERM_COLOR_INDEXED;
5131 index -= 1;
5132 }
5133 if (is_bg)
5134 {
5135 cell.bg.type = type;
5136 cell.bg.red = red;
5137 cell.bg.green = green;
5138 cell.bg.blue = blue;
5139 cell.bg.index = index;
5140 }
5141 else
5142 {
5143 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005144 cell.fg.red = red;
5145 cell.fg.green = green;
5146 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005147 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005148 }
5149 }
5150 else
5151 dump_is_corrupt(&ga_text);
5152 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005153 }
5154 else
5155 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005156 }
5157 else
5158 dump_is_corrupt(&ga_text);
5159
5160 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005161 if (cell.width == 2)
5162 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005163 }
5164 else if (c == '@')
5165 {
5166 if (prev_char == NULL)
5167 dump_is_corrupt(&ga_text);
5168 else
5169 {
5170 int count = 0;
5171
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005172 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005173 for (;;)
5174 {
5175 c = fgetc(fd);
5176 if (!isdigit(c))
5177 break;
5178 count = count * 10 + (c - '0');
5179 }
5180
5181 while (count-- > 0)
5182 {
5183 ga_concat(&ga_text, prev_char);
5184 append_cell(&ga_cell, &cell);
5185 }
5186 }
5187 }
5188 else
5189 {
5190 dump_is_corrupt(&ga_text);
5191 c = fgetc(fd);
5192 }
5193 }
5194
5195 if (ga_text.ga_len > 0)
5196 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005197 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005198 dump_is_corrupt(&ga_text);
5199 ga_append(&ga_text, NUL);
5200 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5201 ga_text.ga_len, FALSE);
5202 }
5203
5204 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005205 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005206 vim_free(prev_char);
5207
5208 return max_cells;
5209}
5210
5211/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005212 * Return an allocated string with at least "text_width" "=" characters and
5213 * "fname" inserted in the middle.
5214 */
5215 static char_u *
5216get_separator(int text_width, char_u *fname)
5217{
5218 int width = MAX(text_width, curwin->w_width);
5219 char_u *textline;
5220 int fname_size;
5221 char_u *p = fname;
5222 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005223 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005224
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005225 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005226 if (textline == NULL)
5227 return NULL;
5228
5229 fname_size = vim_strsize(fname);
5230 if (fname_size < width - 8)
5231 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005232 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005233 width = MAX(text_width, fname_size + 8);
5234 }
5235 else if (fname_size > width - 8)
5236 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005237 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005238 p = gettail(fname);
5239 fname_size = vim_strsize(p);
5240 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005241 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005242 while (fname_size > width - 8)
5243 {
5244 p += (*mb_ptr2len)(p);
5245 fname_size = vim_strsize(p);
5246 }
5247
5248 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5249 textline[i] = '=';
5250 textline[i++] = ' ';
5251
5252 STRCPY(textline + i, p);
5253 off = STRLEN(textline);
5254 textline[off] = ' ';
5255 for (i = 1; i < (width - fname_size) / 2; ++i)
5256 textline[off + i] = '=';
5257 textline[off + i] = NUL;
5258
5259 return textline;
5260}
5261
5262/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005263 * Common for "term_dumpdiff()" and "term_dumpload()".
5264 */
5265 static void
5266term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5267{
5268 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005269 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005270 char_u buf1[NUMBUFLEN];
5271 char_u buf2[NUMBUFLEN];
5272 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005273 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005274 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005275 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005276 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005277 char_u *textline = NULL;
5278
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +02005279 if (in_vim9script()
5280 && (check_for_string_arg(argvars, 0) == FAIL
5281 || check_for_dict_arg(argvars, 1) == FAIL))
5282 return;
5283
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005284 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005285 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005286 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005287 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005288 if (fname1 == NULL || (do_diff && fname2 == NULL))
5289 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005290 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005291 return;
5292 }
5293 fd1 = mch_fopen((char *)fname1, READBIN);
5294 if (fd1 == NULL)
5295 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005296 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005297 return;
5298 }
5299 if (do_diff)
5300 {
5301 fd2 = mch_fopen((char *)fname2, READBIN);
5302 if (fd2 == NULL)
5303 {
5304 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005305 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005306 return;
5307 }
5308 }
5309
5310 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005311 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5312 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5313 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5314 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5315 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005316
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005317 if (opt.jo_term_name == NULL)
5318 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005319 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005320
Bram Moolenaar51e14382019-05-25 20:21:28 +02005321 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005322 if (fname_tofree != NULL)
5323 {
5324 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5325 opt.jo_term_name = fname_tofree;
5326 }
5327 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005328
Bram Moolenaar87abab92019-06-03 21:14:59 +02005329 if (opt.jo_bufnr_buf != NULL)
5330 {
5331 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5332
5333 // With "bufnr" argument: enter the window with this buffer and make it
5334 // empty.
5335 if (wp == NULL)
5336 semsg(_(e_invarg2), "bufnr");
5337 else
5338 {
5339 buf = curbuf;
5340 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005341 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005342 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005343 redraw_later(NOT_VALID);
5344 }
5345 }
5346 else
5347 // Create a new terminal window.
5348 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5349
Bram Moolenaard96ff162018-02-18 22:13:29 +01005350 if (buf != NULL && buf->b_term != NULL)
5351 {
5352 int i;
5353 linenr_T bot_lnum;
5354 linenr_T lnum;
5355 term_T *term = buf->b_term;
5356 int width;
5357 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005358 VTermPos cursor_pos1;
5359 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005360
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005361 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005362
Bram Moolenaard96ff162018-02-18 22:13:29 +01005363 rettv->vval.v_number = buf->b_fnum;
5364
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005365 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005366 width = read_dump_file(fd1, &cursor_pos1);
5367
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005368 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005369 if (cursor_pos1.row >= 0)
5370 {
5371 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5372 coladvance(cursor_pos1.col);
5373 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005374
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005375 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005376 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005377
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005378 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005379 if (!do_diff)
5380 goto theend;
5381
5382 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5383
Bram Moolenaar4a696342018-04-05 18:45:26 +02005384 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005385 if (textline == NULL)
5386 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005387 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5388 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5389 vim_free(textline);
5390
5391 textline = get_separator(width, fname2);
5392 if (textline == NULL)
5393 goto theend;
5394 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5395 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005396 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005397
5398 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005399 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005400 if (width2 > width)
5401 {
5402 vim_free(textline);
5403 textline = alloc(width2 + 1);
5404 if (textline == NULL)
5405 goto theend;
5406 width = width2;
5407 textline[width] = NUL;
5408 }
5409 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5410
5411 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5412 {
5413 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5414 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005415 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005416 for (i = 0; i < width; ++i)
5417 textline[i] = '-';
5418 }
5419 else
5420 {
5421 char_u *line1;
5422 char_u *line2;
5423 char_u *p1;
5424 char_u *p2;
5425 int col;
5426 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5427 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5428 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5429 ->sb_cells;
5430
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005431 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005432 line1 = vim_strsave(ml_get(lnum));
5433 if (line1 == NULL)
5434 break;
5435 p1 = line1;
5436
5437 line2 = ml_get(lnum + bot_lnum);
5438 p2 = line2;
5439 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5440 {
5441 int len1 = utfc_ptr2len(p1);
5442 int len2 = utfc_ptr2len(p2);
5443
5444 textline[col] = ' ';
5445 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005446 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005447 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005448 else if (lnum == cursor_pos1.row + 1
5449 && col == cursor_pos1.col
5450 && (cursor_pos1.row != cursor_pos2.row
5451 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005452 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005453 textline[col] = '>';
5454 else if (lnum == cursor_pos2.row + 1
5455 && col == cursor_pos2.col
5456 && (cursor_pos1.row != cursor_pos2.row
5457 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005458 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005459 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005460 else if (cellattr1 != NULL && cellattr2 != NULL)
5461 {
5462 if ((cellattr1 + col)->width
5463 != (cellattr2 + col)->width)
5464 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005465 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005466 &(cellattr2 + col)->fg))
5467 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005468 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005469 &(cellattr2 + col)->bg))
5470 textline[col] = 'b';
5471 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5472 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5473 textline[col] = 'a';
5474 }
5475 p1 += len1;
5476 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005477 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005478 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005479
5480 while (col < width)
5481 {
5482 if (*p1 == NUL && *p2 == NUL)
5483 textline[col] = '?';
5484 else if (*p1 == NUL)
5485 {
5486 textline[col] = '+';
5487 p2 += utfc_ptr2len(p2);
5488 }
5489 else
5490 {
5491 textline[col] = '-';
5492 p1 += utfc_ptr2len(p1);
5493 }
5494 ++col;
5495 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005496
5497 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005498 }
5499 if (add_empty_scrollback(term, &term->tl_default_color,
5500 term->tl_top_diff_rows) == OK)
5501 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5502 ++bot_lnum;
5503 }
5504
5505 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5506 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005507 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005508 for (i = 0; i < width; ++i)
5509 textline[i] = '+';
5510 if (add_empty_scrollback(term, &term->tl_default_color,
5511 term->tl_top_diff_rows) == OK)
5512 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5513 ++lnum;
5514 ++bot_lnum;
5515 }
5516
5517 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005518
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005519 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005520 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005521 }
5522
5523theend:
5524 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005525 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005526 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005527 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005528 fclose(fd2);
5529}
5530
5531/*
5532 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5533 * bottom files.
5534 * Return FAIL when this is not possible.
5535 */
5536 int
5537term_swap_diff()
5538{
5539 term_T *term = curbuf->b_term;
5540 linenr_T line_count;
5541 linenr_T top_rows;
5542 linenr_T bot_rows;
5543 linenr_T bot_start;
5544 linenr_T lnum;
5545 char_u *p;
5546 sb_line_T *sb_line;
5547
5548 if (term == NULL
5549 || !term_is_finished(curbuf)
5550 || term->tl_top_diff_rows == 0
5551 || term->tl_scrollback.ga_len == 0)
5552 return FAIL;
5553
5554 line_count = curbuf->b_ml.ml_line_count;
5555 top_rows = term->tl_top_diff_rows;
5556 bot_rows = term->tl_bot_diff_rows;
5557 bot_start = line_count - bot_rows;
5558 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5559
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005560 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005561 for (lnum = 1; lnum <= top_rows; ++lnum)
5562 {
5563 p = vim_strsave(ml_get(1));
5564 if (p == NULL)
5565 return OK;
5566 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005567 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005568 vim_free(p);
5569 }
5570
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005571 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005572 for (lnum = 1; lnum <= bot_rows; ++lnum)
5573 {
5574 p = vim_strsave(ml_get(bot_start + lnum));
5575 if (p == NULL)
5576 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005577 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005578 ml_append(lnum - 1, p, 0, FALSE);
5579 vim_free(p);
5580 }
5581
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005582 // move top title to bottom
5583 p = vim_strsave(ml_get(bot_rows + 1));
5584 if (p == NULL)
5585 return OK;
5586 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005587 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005588 vim_free(p);
5589
5590 // move bottom title to top
5591 p = vim_strsave(ml_get(line_count - top_rows));
5592 if (p == NULL)
5593 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005594 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005595 ml_append(bot_rows, p, 0, FALSE);
5596 vim_free(p);
5597
Bram Moolenaard96ff162018-02-18 22:13:29 +01005598 if (top_rows == bot_rows)
5599 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005600 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005601 for (lnum = 0; lnum < top_rows; ++lnum)
5602 {
5603 sb_line_T temp;
5604
5605 temp = *(sb_line + lnum);
5606 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5607 *(sb_line + bot_start + lnum) = temp;
5608 }
5609 }
5610 else
5611 {
5612 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005613 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005614
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005615 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005616 if (temp != NULL)
5617 {
5618 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5619 mch_memmove(term->tl_scrollback.ga_data,
5620 temp + bot_start,
5621 sizeof(sb_line_T) * bot_rows);
5622 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5623 temp + top_rows,
5624 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5625 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5626 + line_count - top_rows,
5627 temp,
5628 sizeof(sb_line_T) * top_rows);
5629 vim_free(temp);
5630 }
5631 }
5632
5633 term->tl_top_diff_rows = bot_rows;
5634 term->tl_bot_diff_rows = top_rows;
5635
5636 update_screen(NOT_VALID);
5637 return OK;
5638}
5639
5640/*
5641 * "term_dumpdiff(filename, filename, options)" function
5642 */
5643 void
5644f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5645{
5646 term_load_dump(argvars, rettv, TRUE);
5647}
5648
5649/*
5650 * "term_dumpload(filename, options)" function
5651 */
5652 void
5653f_term_dumpload(typval_T *argvars, typval_T *rettv)
5654{
5655 term_load_dump(argvars, rettv, FALSE);
5656}
5657
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005658/*
5659 * "term_getaltscreen(buf)" function
5660 */
5661 void
5662f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5663{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005664 buf_T *buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005665
5666 if (buf == NULL)
5667 return;
5668 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5669}
5670
5671/*
5672 * "term_getattr(attr, name)" function
5673 */
5674 void
5675f_term_getattr(typval_T *argvars, typval_T *rettv)
5676{
5677 int attr;
5678 size_t i;
5679 char_u *name;
5680
5681 static struct {
5682 char *name;
5683 int attr;
5684 } attrs[] = {
5685 {"bold", HL_BOLD},
5686 {"italic", HL_ITALIC},
5687 {"underline", HL_UNDERLINE},
5688 {"strike", HL_STRIKETHROUGH},
5689 {"reverse", HL_INVERSE},
5690 };
5691
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005692 if (in_vim9script()
5693 && (check_for_number_arg(argvars, 0) == FAIL
5694 || check_for_string_arg(argvars, 1) == FAIL))
5695 return;
5696
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005697 attr = tv_get_number(&argvars[0]);
5698 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005699 if (name == NULL)
5700 return;
5701
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005702 if (attr > HL_ALL)
5703 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02005704 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005705 if (STRCMP(name, attrs[i].name) == 0)
5706 {
5707 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5708 break;
5709 }
5710}
5711
5712/*
5713 * "term_getcursor(buf)" function
5714 */
5715 void
5716f_term_getcursor(typval_T *argvars, typval_T *rettv)
5717{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005718 buf_T *buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005719 term_T *term;
5720 list_T *l;
5721 dict_T *d;
5722
5723 if (rettv_list_alloc(rettv) == FAIL)
5724 return;
5725 if (buf == NULL)
5726 return;
5727 term = buf->b_term;
5728
5729 l = rettv->vval.v_list;
5730 list_append_number(l, term->tl_cursor_pos.row + 1);
5731 list_append_number(l, term->tl_cursor_pos.col + 1);
5732
5733 d = dict_alloc();
5734 if (d != NULL)
5735 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005736 dict_add_number(d, "visible", term->tl_cursor_visible);
5737 dict_add_number(d, "blink", blink_state_is_inverted()
5738 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5739 dict_add_number(d, "shape", term->tl_cursor_shape);
5740 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005741 list_append_dict(l, d);
5742 }
5743}
5744
5745/*
5746 * "term_getjob(buf)" function
5747 */
5748 void
5749f_term_getjob(typval_T *argvars, typval_T *rettv)
5750{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005751 buf_T *buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005752
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005753 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005754 {
5755 rettv->v_type = VAR_SPECIAL;
5756 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005757 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005758 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005759
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005760 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005761 rettv->vval.v_job = buf->b_term->tl_job;
5762 if (rettv->vval.v_job != NULL)
5763 ++rettv->vval.v_job->jv_refcount;
5764}
5765
5766 static int
5767get_row_number(typval_T *tv, term_T *term)
5768{
5769 if (tv->v_type == VAR_STRING
5770 && tv->vval.v_string != NULL
5771 && STRCMP(tv->vval.v_string, ".") == 0)
5772 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005773 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005774}
5775
5776/*
5777 * "term_getline(buf, row)" function
5778 */
5779 void
5780f_term_getline(typval_T *argvars, typval_T *rettv)
5781{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005782 buf_T *buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005783 term_T *term;
5784 int row;
5785
5786 rettv->v_type = VAR_STRING;
5787 if (buf == NULL)
5788 return;
5789 term = buf->b_term;
5790 row = get_row_number(&argvars[1], term);
5791
5792 if (term->tl_vterm == NULL)
5793 {
5794 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5795
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005796 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005797 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5798 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5799 }
5800 else
5801 {
5802 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5803 VTermRect rect;
5804 int len;
5805 char_u *p;
5806
5807 if (row < 0 || row >= term->tl_rows)
5808 return;
5809 len = term->tl_cols * MB_MAXBYTES + 1;
5810 p = alloc(len);
5811 if (p == NULL)
5812 return;
5813 rettv->vval.v_string = p;
5814
5815 rect.start_col = 0;
5816 rect.end_col = term->tl_cols;
5817 rect.start_row = row;
5818 rect.end_row = row + 1;
5819 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5820 }
5821}
5822
5823/*
5824 * "term_getscrolled(buf)" function
5825 */
5826 void
5827f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5828{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005829 buf_T *buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005830
5831 if (buf == NULL)
5832 return;
5833 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5834}
5835
5836/*
5837 * "term_getsize(buf)" function
5838 */
5839 void
5840f_term_getsize(typval_T *argvars, typval_T *rettv)
5841{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005842 buf_T *buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005843 list_T *l;
5844
5845 if (rettv_list_alloc(rettv) == FAIL)
5846 return;
5847 if (buf == NULL)
5848 return;
5849
5850 l = rettv->vval.v_list;
5851 list_append_number(l, buf->b_term->tl_rows);
5852 list_append_number(l, buf->b_term->tl_cols);
5853}
5854
5855/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005856 * "term_setsize(buf, rows, cols)" function
5857 */
5858 void
5859f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5860{
5861 buf_T *buf = term_get_buf(argvars, "term_setsize()");
5862 term_T *term;
5863 varnumber_T rows, cols;
5864
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005865 if (buf == NULL)
5866 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005867 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005868 return;
5869 }
5870 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005871 return;
5872 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005873 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005874 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005875 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005876 cols = cols <= 0 ? term->tl_cols : cols;
5877 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005878 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005879
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005880 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005881 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5882 term_report_winsize(term, term->tl_rows, term->tl_cols);
5883}
5884
5885/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005886 * "term_getstatus(buf)" function
5887 */
5888 void
5889f_term_getstatus(typval_T *argvars, typval_T *rettv)
5890{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005891 buf_T *buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005892 term_T *term;
5893 char_u val[100];
5894
5895 rettv->v_type = VAR_STRING;
5896 if (buf == NULL)
5897 return;
5898 term = buf->b_term;
5899
5900 if (term_job_running(term))
5901 STRCPY(val, "running");
5902 else
5903 STRCPY(val, "finished");
5904 if (term->tl_normal_mode)
5905 STRCAT(val, ",normal");
5906 rettv->vval.v_string = vim_strsave(val);
5907}
5908
5909/*
5910 * "term_gettitle(buf)" function
5911 */
5912 void
5913f_term_gettitle(typval_T *argvars, typval_T *rettv)
5914{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005915 buf_T *buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005916
5917 rettv->v_type = VAR_STRING;
5918 if (buf == NULL)
5919 return;
5920
5921 if (buf->b_term->tl_title != NULL)
5922 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5923}
5924
5925/*
5926 * "term_gettty(buf)" function
5927 */
5928 void
5929f_term_gettty(typval_T *argvars, typval_T *rettv)
5930{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005931 buf_T *buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005932 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005933 int num = 0;
5934
5935 rettv->v_type = VAR_STRING;
5936 if (buf == NULL)
5937 return;
5938 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02005939 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005940
5941 switch (num)
5942 {
5943 case 0:
5944 if (buf->b_term->tl_job != NULL)
5945 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005946 break;
5947 case 1:
5948 if (buf->b_term->tl_job != NULL)
5949 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005950 break;
5951 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005952 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005953 return;
5954 }
5955 if (p != NULL)
5956 rettv->vval.v_string = vim_strsave(p);
5957}
5958
5959/*
5960 * "term_list()" function
5961 */
5962 void
5963f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
5964{
5965 term_T *tp;
5966 list_T *l;
5967
5968 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
5969 return;
5970
5971 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005972 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02005973 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005974 if (list_append_number(l,
5975 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
5976 return;
5977}
5978
5979/*
5980 * "term_scrape(buf, row)" function
5981 */
5982 void
5983f_term_scrape(typval_T *argvars, typval_T *rettv)
5984{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005985 buf_T *buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005986 VTermScreen *screen = NULL;
5987 VTermPos pos;
5988 list_T *l;
5989 term_T *term;
5990 char_u *p;
5991 sb_line_T *line;
5992
5993 if (rettv_list_alloc(rettv) == FAIL)
5994 return;
5995 if (buf == NULL)
5996 return;
5997 term = buf->b_term;
5998
5999 l = rettv->vval.v_list;
6000 pos.row = get_row_number(&argvars[1], term);
6001
6002 if (term->tl_vterm != NULL)
6003 {
6004 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006005 if (screen == NULL) // can't really happen
6006 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006007 p = NULL;
6008 line = NULL;
6009 }
6010 else
6011 {
6012 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6013
6014 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6015 return;
6016 p = ml_get_buf(buf, lnum + 1, FALSE);
6017 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6018 }
6019
6020 for (pos.col = 0; pos.col < term->tl_cols; )
6021 {
6022 dict_T *dcell;
6023 int width;
6024 VTermScreenCellAttrs attrs;
6025 VTermColor fg, bg;
6026 char_u rgb[8];
6027 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6028 int off = 0;
6029 int i;
6030
6031 if (screen == NULL)
6032 {
6033 cellattr_T *cellattr;
6034 int len;
6035
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006036 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006037 if (pos.col >= line->sb_cols)
6038 break;
6039 cellattr = line->sb_cells + pos.col;
6040 width = cellattr->width;
6041 attrs = cellattr->attrs;
6042 fg = cellattr->fg;
6043 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006044 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006045 mch_memmove(mbs, p, len);
6046 mbs[len] = NUL;
6047 p += len;
6048 }
6049 else
6050 {
6051 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006052
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006053 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6054 break;
6055 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6056 {
6057 if (cell.chars[i] == 0)
6058 break;
6059 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6060 }
6061 mbs[off] = NUL;
6062 width = cell.width;
6063 attrs = cell.attrs;
6064 fg = cell.fg;
6065 bg = cell.bg;
6066 }
6067 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006068 if (dcell == NULL)
6069 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006070 list_append_dict(l, dcell);
6071
Bram Moolenaare0be1672018-07-08 16:50:37 +02006072 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006073
6074 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6075 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006076 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006077 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6078 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006079 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006080
Bram Moolenaar83d47902020-03-26 20:34:00 +01006081 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006082 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006083
6084 ++pos.col;
6085 if (width == 2)
6086 ++pos.col;
6087 }
6088}
6089
6090/*
6091 * "term_sendkeys(buf, keys)" function
6092 */
6093 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006094f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006095{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006096 buf_T *buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006097 char_u *msg;
6098 term_T *term;
6099
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006100 if (buf == NULL)
6101 return;
6102
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006103 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006104 if (msg == NULL)
6105 return;
6106 term = buf->b_term;
6107 if (term->tl_vterm == NULL)
6108 return;
6109
6110 while (*msg != NUL)
6111 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006112 int c;
6113
6114 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6115 {
6116 c = TO_SPECIAL(msg[1], msg[2]);
6117 msg += 3;
6118 }
6119 else
6120 {
6121 c = PTR2CHAR(msg);
6122 msg += MB_CPTR2LEN(msg);
6123 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006124 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006125 }
6126}
6127
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006128#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6129/*
6130 * "term_getansicolors(buf)" function
6131 */
6132 void
6133f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6134{
6135 buf_T *buf = term_get_buf(argvars, "term_getansicolors()");
6136 term_T *term;
6137 VTermState *state;
6138 VTermColor color;
6139 char_u hexbuf[10];
6140 int index;
6141 list_T *list;
6142
6143 if (rettv_list_alloc(rettv) == FAIL)
6144 return;
6145
6146 if (buf == NULL)
6147 return;
6148 term = buf->b_term;
6149 if (term->tl_vterm == NULL)
6150 return;
6151
6152 list = rettv->vval.v_list;
6153 state = vterm_obtain_state(term->tl_vterm);
6154 for (index = 0; index < 16; index++)
6155 {
6156 vterm_state_get_palette_color(state, index, &color);
6157 sprintf((char *)hexbuf, "#%02x%02x%02x",
6158 color.red, color.green, color.blue);
6159 if (list_append_string(list, hexbuf, 7) == FAIL)
6160 return;
6161 }
6162}
6163
6164/*
6165 * "term_setansicolors(buf, list)" function
6166 */
6167 void
6168f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6169{
6170 buf_T *buf = term_get_buf(argvars, "term_setansicolors()");
6171 term_T *term;
6172
6173 if (buf == NULL)
6174 return;
6175 term = buf->b_term;
6176 if (term->tl_vterm == NULL)
6177 return;
6178
6179 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6180 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006181 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006182 return;
6183 }
6184
6185 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006186 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006187}
6188#endif
6189
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006190/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006191 * "term_setapi(buf, api)" function
6192 */
6193 void
6194f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6195{
6196 buf_T *buf = term_get_buf(argvars, "term_setapi()");
6197 term_T *term;
6198 char_u *api;
6199
6200 if (buf == NULL)
6201 return;
6202 term = buf->b_term;
6203 vim_free(term->tl_api);
6204 api = tv_get_string_chk(&argvars[1]);
6205 if (api != NULL)
6206 term->tl_api = vim_strsave(api);
6207 else
6208 term->tl_api = NULL;
6209}
6210
6211/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006212 * "term_setrestore(buf, command)" function
6213 */
6214 void
6215f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6216{
6217#if defined(FEAT_SESSION)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006218 buf_T *buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006219 term_T *term;
6220 char_u *cmd;
6221
6222 if (buf == NULL)
6223 return;
6224 term = buf->b_term;
6225 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006226 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006227 if (cmd != NULL)
6228 term->tl_command = vim_strsave(cmd);
6229 else
6230 term->tl_command = NULL;
6231#endif
6232}
6233
6234/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006235 * "term_setkill(buf, how)" function
6236 */
6237 void
6238f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6239{
6240 buf_T *buf = term_get_buf(argvars, "term_setkill()");
6241 term_T *term;
6242 char_u *how;
6243
6244 if (buf == NULL)
6245 return;
6246 term = buf->b_term;
6247 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006248 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006249 if (how != NULL)
6250 term->tl_kill = vim_strsave(how);
6251 else
6252 term->tl_kill = NULL;
6253}
6254
6255/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006256 * "term_start(command, options)" function
6257 */
6258 void
6259f_term_start(typval_T *argvars, typval_T *rettv)
6260{
6261 jobopt_T opt;
6262 buf_T *buf;
6263
6264 init_job_options(&opt);
6265 if (argvars[1].v_type != VAR_UNKNOWN
6266 && get_job_options(&argvars[1], &opt,
6267 JO_TIMEOUT_ALL + JO_STOPONEXIT
6268 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6269 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6270 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6271 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006272 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006273 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006274 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006275 return;
6276
Bram Moolenaar13568252018-03-16 20:46:58 +01006277 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006278
6279 if (buf != NULL && buf->b_term != NULL)
6280 rettv->vval.v_number = buf->b_fnum;
6281}
6282
6283/*
6284 * "term_wait" function
6285 */
6286 void
6287f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6288{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006289 buf_T *buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006290
6291 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006292 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006293 if (buf->b_term->tl_job == NULL)
6294 {
6295 ch_log(NULL, "term_wait(): no job to wait for");
6296 return;
6297 }
6298 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006299 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006300 return;
6301
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006302 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006303 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006304 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6305 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006306 // The job is dead, keep reading channel I/O until the channel is
6307 // closed. buf->b_term may become NULL if the terminal was closed while
6308 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006309 ch_log(NULL, "term_wait(): waiting for channel to close");
6310 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6311 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006312 term_flush_messages();
6313
Bram Moolenaard45aa552018-05-21 22:50:29 +02006314 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006315 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006316 // If the terminal is closed when the channel is closed the
6317 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006318 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006319 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006320
6321 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006322 }
6323 else
6324 {
6325 long wait = 10L;
6326
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006327 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006328
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006329 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006330 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006331 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006332 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006333
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006334 // Flushing messages on channels is hopefully sufficient.
6335 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006336 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006337 }
6338}
6339
6340/*
6341 * Called when a channel has sent all the lines to a terminal.
6342 * Send a CTRL-D to mark the end of the text.
6343 */
6344 void
6345term_send_eof(channel_T *ch)
6346{
6347 term_T *term;
6348
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006349 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006350 if (term->tl_job == ch->ch_job)
6351 {
6352 if (term->tl_eof_chars != NULL)
6353 {
6354 channel_send(ch, PART_IN, term->tl_eof_chars,
6355 (int)STRLEN(term->tl_eof_chars), NULL);
6356 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6357 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006358# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006359 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006360 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006361 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6362# endif
6363 }
6364}
6365
Bram Moolenaar113e1072019-01-20 15:30:40 +01006366#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006367 job_T *
6368term_getjob(term_T *term)
6369{
6370 return term != NULL ? term->tl_job : NULL;
6371}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006372#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006373
Bram Moolenaar4f974752019-02-17 17:44:42 +01006374# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006375
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006376///////////////////////////////////////
6377// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006378#ifdef PROTO
6379typedef int COORD;
6380typedef int DWORD;
6381typedef int HANDLE;
6382typedef int *DWORD_PTR;
6383typedef int HPCON;
6384typedef int HRESULT;
6385typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006386typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006387typedef int PSIZE_T;
6388typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006389typedef int BOOL;
6390# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006391#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006392
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006393HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6394HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6395HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006396BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6397BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6398void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006399
6400 static int
6401dyn_conpty_init(int verbose)
6402{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006403 static HMODULE hKerneldll = NULL;
6404 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006405 static struct
6406 {
6407 char *name;
6408 FARPROC *ptr;
6409 } conpty_entry[] =
6410 {
6411 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6412 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6413 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6414 {"InitializeProcThreadAttributeList",
6415 (FARPROC*)&pInitializeProcThreadAttributeList},
6416 {"UpdateProcThreadAttribute",
6417 (FARPROC*)&pUpdateProcThreadAttribute},
6418 {"DeleteProcThreadAttributeList",
6419 (FARPROC*)&pDeleteProcThreadAttributeList},
6420 {NULL, NULL}
6421 };
6422
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006423 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006424 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006425 if (verbose)
6426 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006427 return FAIL;
6428 }
6429
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006430 // No need to initialize twice.
6431 if (hKerneldll)
6432 return OK;
6433
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006434 hKerneldll = vimLoadLib("kernel32.dll");
6435 for (i = 0; conpty_entry[i].name != NULL
6436 && conpty_entry[i].ptr != NULL; ++i)
6437 {
6438 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6439 conpty_entry[i].name)) == NULL)
6440 {
6441 if (verbose)
6442 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006443 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006444 return FAIL;
6445 }
6446 }
6447
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006448 return OK;
6449}
6450
6451 static int
6452conpty_term_and_job_init(
6453 term_T *term,
6454 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006455 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006456 jobopt_T *opt,
6457 jobopt_T *orig_opt)
6458{
6459 WCHAR *cmd_wchar = NULL;
6460 WCHAR *cmd_wchar_copy = NULL;
6461 WCHAR *cwd_wchar = NULL;
6462 WCHAR *env_wchar = NULL;
6463 channel_T *channel = NULL;
6464 job_T *job = NULL;
6465 HANDLE jo = NULL;
6466 garray_T ga_cmd, ga_env;
6467 char_u *cmd = NULL;
6468 HRESULT hr;
6469 COORD consize;
6470 SIZE_T breq;
6471 PROCESS_INFORMATION proc_info;
6472 HANDLE i_theirs = NULL;
6473 HANDLE o_theirs = NULL;
6474 HANDLE i_ours = NULL;
6475 HANDLE o_ours = NULL;
6476
6477 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6478 ga_init2(&ga_env, (int)sizeof(char*), 20);
6479
6480 if (argvar->v_type == VAR_STRING)
6481 {
6482 cmd = argvar->vval.v_string;
6483 }
6484 else if (argvar->v_type == VAR_LIST)
6485 {
6486 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6487 goto failed;
6488 cmd = ga_cmd.ga_data;
6489 }
6490 if (cmd == NULL || *cmd == NUL)
6491 {
6492 emsg(_(e_invarg));
6493 goto failed;
6494 }
6495
6496 term->tl_arg0_cmd = vim_strsave(cmd);
6497
6498 cmd_wchar = enc_to_utf16(cmd, NULL);
6499
6500 if (cmd_wchar != NULL)
6501 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006502 // Request by CreateProcessW
6503 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006504 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006505 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6506 }
6507
6508 ga_clear(&ga_cmd);
6509 if (cmd_wchar == NULL)
6510 goto failed;
6511 if (opt->jo_cwd != NULL)
6512 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6513
6514 win32_build_env(opt->jo_env, &ga_env, TRUE);
6515 env_wchar = ga_env.ga_data;
6516
6517 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6518 goto failed;
6519 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6520 goto failed;
6521
6522 consize.X = term->tl_cols;
6523 consize.Y = term->tl_rows;
6524 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6525 &term->tl_conpty);
6526 if (FAILED(hr))
6527 goto failed;
6528
6529 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6530
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006531 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006532 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006533 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006534 if (!term->tl_siex.lpAttributeList)
6535 goto failed;
6536 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6537 0, &breq))
6538 goto failed;
6539 if (!pUpdateProcThreadAttribute(
6540 term->tl_siex.lpAttributeList, 0,
6541 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6542 sizeof(HPCON), NULL, NULL))
6543 goto failed;
6544
6545 channel = add_channel();
6546 if (channel == NULL)
6547 goto failed;
6548
6549 job = job_alloc();
6550 if (job == NULL)
6551 goto failed;
6552 if (argvar->v_type == VAR_STRING)
6553 {
6554 int argc;
6555
6556 build_argv_from_string(cmd, &job->jv_argv, &argc);
6557 }
6558 else
6559 {
6560 int argc;
6561
6562 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6563 }
6564
6565 if (opt->jo_set & JO_IN_BUF)
6566 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6567
6568 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6569 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006570 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006571 env_wchar, cwd_wchar,
6572 &term->tl_siex.StartupInfo, &proc_info))
6573 goto failed;
6574
6575 CloseHandle(i_theirs);
6576 CloseHandle(o_theirs);
6577
6578 channel_set_pipes(channel,
6579 (sock_T)i_ours,
6580 (sock_T)o_ours,
6581 (sock_T)o_ours);
6582
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006583 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006584 channel->ch_write_text_mode = TRUE;
6585
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006586 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006587 channel->ch_anonymous_pipe = TRUE;
6588
6589 jo = CreateJobObject(NULL, NULL);
6590 if (jo == NULL)
6591 goto failed;
6592
6593 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6594 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006595 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006596 CloseHandle(jo);
6597 jo = NULL;
6598 }
6599
6600 ResumeThread(proc_info.hThread);
6601 CloseHandle(proc_info.hThread);
6602
6603 vim_free(cmd_wchar);
6604 vim_free(cmd_wchar_copy);
6605 vim_free(cwd_wchar);
6606 vim_free(env_wchar);
6607
6608 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6609 goto failed;
6610
6611#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6612 if (opt->jo_set2 & JO2_ANSI_COLORS)
6613 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6614 else
6615 init_vterm_ansi_colors(term->tl_vterm);
6616#endif
6617
6618 channel_set_job(channel, job, opt);
6619 job_set_options(job, opt);
6620
6621 job->jv_channel = channel;
6622 job->jv_proc_info = proc_info;
6623 job->jv_job_object = jo;
6624 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006625 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006626 ++job->jv_refcount;
6627 term->tl_job = job;
6628
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006629 // Redirecting stdout and stderr doesn't work at the job level. Instead
6630 // open the file here and handle it in. opt->jo_io was changed in
6631 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006632 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6633 {
6634 char_u *fname = opt->jo_io_name[PART_OUT];
6635
6636 ch_log(channel, "Opening output file %s", fname);
6637 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6638 if (term->tl_out_fd == NULL)
6639 semsg(_(e_notopen), fname);
6640 }
6641
6642 return OK;
6643
6644failed:
6645 ga_clear(&ga_cmd);
6646 ga_clear(&ga_env);
6647 vim_free(cmd_wchar);
6648 vim_free(cmd_wchar_copy);
6649 vim_free(cwd_wchar);
6650 if (channel != NULL)
6651 channel_clear(channel);
6652 if (job != NULL)
6653 {
6654 job->jv_channel = NULL;
6655 job_cleanup(job);
6656 }
6657 term->tl_job = NULL;
6658 if (jo != NULL)
6659 CloseHandle(jo);
6660
6661 if (term->tl_siex.lpAttributeList != NULL)
6662 {
6663 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6664 vim_free(term->tl_siex.lpAttributeList);
6665 }
6666 term->tl_siex.lpAttributeList = NULL;
6667 if (o_theirs != NULL)
6668 CloseHandle(o_theirs);
6669 if (o_ours != NULL)
6670 CloseHandle(o_ours);
6671 if (i_ours != NULL)
6672 CloseHandle(i_ours);
6673 if (i_theirs != NULL)
6674 CloseHandle(i_theirs);
6675 if (term->tl_conpty != NULL)
6676 pClosePseudoConsole(term->tl_conpty);
6677 term->tl_conpty = NULL;
6678 return FAIL;
6679}
6680
6681 static void
6682conpty_term_report_winsize(term_T *term, int rows, int cols)
6683{
6684 COORD consize;
6685
6686 consize.X = cols;
6687 consize.Y = rows;
6688 pResizePseudoConsole(term->tl_conpty, consize);
6689}
6690
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006691 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006692term_free_conpty(term_T *term)
6693{
6694 if (term->tl_siex.lpAttributeList != NULL)
6695 {
6696 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6697 vim_free(term->tl_siex.lpAttributeList);
6698 }
6699 term->tl_siex.lpAttributeList = NULL;
6700 if (term->tl_conpty != NULL)
6701 pClosePseudoConsole(term->tl_conpty);
6702 term->tl_conpty = NULL;
6703}
6704
6705 int
6706use_conpty(void)
6707{
6708 return has_conpty;
6709}
6710
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006711# ifndef PROTO
6712
6713#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6714#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006715#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006716
6717void* (*winpty_config_new)(UINT64, void*);
6718void* (*winpty_open)(void*, void*);
6719void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6720BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6721void (*winpty_config_set_mouse_mode)(void*, int);
6722void (*winpty_config_set_initial_size)(void*, int, int);
6723LPCWSTR (*winpty_conin_name)(void*);
6724LPCWSTR (*winpty_conout_name)(void*);
6725LPCWSTR (*winpty_conerr_name)(void*);
6726void (*winpty_free)(void*);
6727void (*winpty_config_free)(void*);
6728void (*winpty_spawn_config_free)(void*);
6729void (*winpty_error_free)(void*);
6730LPCWSTR (*winpty_error_msg)(void*);
6731BOOL (*winpty_set_size)(void*, int, int, void*);
6732HANDLE (*winpty_agent_process)(void*);
6733
6734#define WINPTY_DLL "winpty.dll"
6735
6736static HINSTANCE hWinPtyDLL = NULL;
6737# endif
6738
6739 static int
6740dyn_winpty_init(int verbose)
6741{
6742 int i;
6743 static struct
6744 {
6745 char *name;
6746 FARPROC *ptr;
6747 } winpty_entry[] =
6748 {
6749 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6750 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6751 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6752 {"winpty_config_set_mouse_mode",
6753 (FARPROC*)&winpty_config_set_mouse_mode},
6754 {"winpty_config_set_initial_size",
6755 (FARPROC*)&winpty_config_set_initial_size},
6756 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6757 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6758 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6759 {"winpty_free", (FARPROC*)&winpty_free},
6760 {"winpty_open", (FARPROC*)&winpty_open},
6761 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6762 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6763 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6764 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6765 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6766 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6767 {NULL, NULL}
6768 };
6769
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006770 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006771 if (hWinPtyDLL)
6772 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006773 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6774 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006775 if (*p_winptydll != NUL)
6776 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6777 if (!hWinPtyDLL)
6778 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6779 if (!hWinPtyDLL)
6780 {
6781 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006782 semsg(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006783 : (char_u *)WINPTY_DLL);
6784 return FAIL;
6785 }
6786 for (i = 0; winpty_entry[i].name != NULL
6787 && winpty_entry[i].ptr != NULL; ++i)
6788 {
6789 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6790 winpty_entry[i].name)) == NULL)
6791 {
6792 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006793 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006794 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006795 return FAIL;
6796 }
6797 }
6798
6799 return OK;
6800}
6801
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006802 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006803winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006804 term_T *term,
6805 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006806 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006807 jobopt_T *opt,
6808 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006809{
6810 WCHAR *cmd_wchar = NULL;
6811 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006812 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006813 channel_T *channel = NULL;
6814 job_T *job = NULL;
6815 DWORD error;
6816 HANDLE jo = NULL;
6817 HANDLE child_process_handle;
6818 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006819 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006820 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006821 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006822 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006823
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006824 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6825 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006826
6827 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006828 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006829 cmd = argvar->vval.v_string;
6830 }
6831 else if (argvar->v_type == VAR_LIST)
6832 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006833 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006834 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006835 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006836 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006837 if (cmd == NULL || *cmd == NUL)
6838 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006839 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006840 goto failed;
6841 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006842
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006843 term->tl_arg0_cmd = vim_strsave(cmd);
6844
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006845 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006846 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006847 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006848 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006849 if (opt->jo_cwd != NULL)
6850 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006851
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006852 win32_build_env(opt->jo_env, &ga_env, TRUE);
6853 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006854
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006855 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6856 if (term->tl_winpty_config == NULL)
6857 goto failed;
6858
6859 winpty_config_set_mouse_mode(term->tl_winpty_config,
6860 WINPTY_MOUSE_MODE_FORCE);
6861 winpty_config_set_initial_size(term->tl_winpty_config,
6862 term->tl_cols, term->tl_rows);
6863 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6864 if (term->tl_winpty == NULL)
6865 goto failed;
6866
6867 spawn_config = winpty_spawn_config_new(
6868 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6869 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6870 NULL,
6871 cmd_wchar,
6872 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006873 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006874 &winpty_err);
6875 if (spawn_config == NULL)
6876 goto failed;
6877
6878 channel = add_channel();
6879 if (channel == NULL)
6880 goto failed;
6881
6882 job = job_alloc();
6883 if (job == NULL)
6884 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006885 if (argvar->v_type == VAR_STRING)
6886 {
6887 int argc;
6888
6889 build_argv_from_string(cmd, &job->jv_argv, &argc);
6890 }
6891 else
6892 {
6893 int argc;
6894
6895 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6896 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006897
6898 if (opt->jo_set & JO_IN_BUF)
6899 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6900
6901 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
6902 &child_thread_handle, &error, &winpty_err))
6903 goto failed;
6904
6905 channel_set_pipes(channel,
6906 (sock_T)CreateFileW(
6907 winpty_conin_name(term->tl_winpty),
6908 GENERIC_WRITE, 0, NULL,
6909 OPEN_EXISTING, 0, NULL),
6910 (sock_T)CreateFileW(
6911 winpty_conout_name(term->tl_winpty),
6912 GENERIC_READ, 0, NULL,
6913 OPEN_EXISTING, 0, NULL),
6914 (sock_T)CreateFileW(
6915 winpty_conerr_name(term->tl_winpty),
6916 GENERIC_READ, 0, NULL,
6917 OPEN_EXISTING, 0, NULL));
6918
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006919 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006920 channel->ch_write_text_mode = TRUE;
6921
6922 jo = CreateJobObject(NULL, NULL);
6923 if (jo == NULL)
6924 goto failed;
6925
6926 if (!AssignProcessToJobObject(jo, child_process_handle))
6927 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006928 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006929 CloseHandle(jo);
6930 jo = NULL;
6931 }
6932
6933 winpty_spawn_config_free(spawn_config);
6934 vim_free(cmd_wchar);
6935 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006936 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006937
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006938 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6939 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006940
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006941#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6942 if (opt->jo_set2 & JO2_ANSI_COLORS)
6943 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6944 else
6945 init_vterm_ansi_colors(term->tl_vterm);
6946#endif
6947
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006948 channel_set_job(channel, job, opt);
6949 job_set_options(job, opt);
6950
6951 job->jv_channel = channel;
6952 job->jv_proc_info.hProcess = child_process_handle;
6953 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
6954 job->jv_job_object = jo;
6955 job->jv_status = JOB_STARTED;
6956 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006957 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006958 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006959 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006960 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006961 ++job->jv_refcount;
6962 term->tl_job = job;
6963
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006964 // Redirecting stdout and stderr doesn't work at the job level. Instead
6965 // open the file here and handle it in. opt->jo_io was changed in
6966 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006967 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6968 {
6969 char_u *fname = opt->jo_io_name[PART_OUT];
6970
6971 ch_log(channel, "Opening output file %s", fname);
6972 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6973 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006974 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006975 }
6976
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006977 return OK;
6978
6979failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006980 ga_clear(&ga_cmd);
6981 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006982 vim_free(cmd_wchar);
6983 vim_free(cwd_wchar);
6984 if (spawn_config != NULL)
6985 winpty_spawn_config_free(spawn_config);
6986 if (channel != NULL)
6987 channel_clear(channel);
6988 if (job != NULL)
6989 {
6990 job->jv_channel = NULL;
6991 job_cleanup(job);
6992 }
6993 term->tl_job = NULL;
6994 if (jo != NULL)
6995 CloseHandle(jo);
6996 if (term->tl_winpty != NULL)
6997 winpty_free(term->tl_winpty);
6998 term->tl_winpty = NULL;
6999 if (term->tl_winpty_config != NULL)
7000 winpty_config_free(term->tl_winpty_config);
7001 term->tl_winpty_config = NULL;
7002 if (winpty_err != NULL)
7003 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007004 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007005 (short_u *)winpty_error_msg(winpty_err), NULL);
7006
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007007 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007008 winpty_error_free(winpty_err);
7009 }
7010 return FAIL;
7011}
7012
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007013/*
7014 * Create a new terminal of "rows" by "cols" cells.
7015 * Store a reference in "term".
7016 * Return OK or FAIL.
7017 */
7018 static int
7019term_and_job_init(
7020 term_T *term,
7021 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007022 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007023 jobopt_T *opt,
7024 jobopt_T *orig_opt)
7025{
7026 int use_winpty = FALSE;
7027 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007028 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007029
7030 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7031 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7032
7033 if (!has_winpty && !has_conpty)
7034 // If neither is available give the errors for winpty, since when
7035 // conpty is not available it can't be installed either.
7036 return dyn_winpty_init(TRUE);
7037
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007038 if (opt->jo_tty_type != NUL)
7039 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007040
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007041 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007042 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007043 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007044 use_conpty = TRUE;
7045 else if (has_winpty)
7046 use_winpty = TRUE;
7047 // else: error
7048 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007049 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007050 {
7051 if (has_winpty)
7052 use_winpty = TRUE;
7053 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007054 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007055 {
7056 if (has_conpty)
7057 use_conpty = TRUE;
7058 else
7059 return dyn_conpty_init(TRUE);
7060 }
7061
7062 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007063 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007064
7065 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007066 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007067
7068 // error
7069 return dyn_winpty_init(TRUE);
7070}
7071
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007072 static int
7073create_pty_only(term_T *term, jobopt_T *options)
7074{
7075 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7076 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7077 char in_name[80], out_name[80];
7078 channel_T *channel = NULL;
7079
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007080 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7081 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007082
7083 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7084 GetCurrentProcessId(),
7085 curbuf->b_fnum);
7086 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7087 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7088 PIPE_UNLIMITED_INSTANCES,
7089 0, 0, NMPWAIT_NOWAIT, NULL);
7090 if (hPipeIn == INVALID_HANDLE_VALUE)
7091 goto failed;
7092
7093 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7094 GetCurrentProcessId(),
7095 curbuf->b_fnum);
7096 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7097 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7098 PIPE_UNLIMITED_INSTANCES,
7099 0, 0, 0, NULL);
7100 if (hPipeOut == INVALID_HANDLE_VALUE)
7101 goto failed;
7102
7103 ConnectNamedPipe(hPipeIn, NULL);
7104 ConnectNamedPipe(hPipeOut, NULL);
7105
7106 term->tl_job = job_alloc();
7107 if (term->tl_job == NULL)
7108 goto failed;
7109 ++term->tl_job->jv_refcount;
7110
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007111 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007112 term->tl_job->jv_status = JOB_FINISHED;
7113
7114 channel = add_channel();
7115 if (channel == NULL)
7116 goto failed;
7117 term->tl_job->jv_channel = channel;
7118 channel->ch_keep_open = TRUE;
7119 channel->ch_named_pipe = TRUE;
7120
7121 channel_set_pipes(channel,
7122 (sock_T)hPipeIn,
7123 (sock_T)hPipeOut,
7124 (sock_T)hPipeOut);
7125 channel_set_job(channel, term->tl_job, options);
7126 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7127 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7128
7129 return OK;
7130
7131failed:
7132 if (hPipeIn != NULL)
7133 CloseHandle(hPipeIn);
7134 if (hPipeOut != NULL)
7135 CloseHandle(hPipeOut);
7136 return FAIL;
7137}
7138
7139/*
7140 * Free the terminal emulator part of "term".
7141 */
7142 static void
7143term_free_vterm(term_T *term)
7144{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007145 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007146 if (term->tl_winpty != NULL)
7147 winpty_free(term->tl_winpty);
7148 term->tl_winpty = NULL;
7149 if (term->tl_winpty_config != NULL)
7150 winpty_config_free(term->tl_winpty_config);
7151 term->tl_winpty_config = NULL;
7152 if (term->tl_vterm != NULL)
7153 vterm_free(term->tl_vterm);
7154 term->tl_vterm = NULL;
7155}
7156
7157/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007158 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007159 */
7160 static void
7161term_report_winsize(term_T *term, int rows, int cols)
7162{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007163 if (term->tl_conpty)
7164 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007165 if (term->tl_winpty)
7166 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7167}
7168
7169 int
7170terminal_enabled(void)
7171{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007172 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007173}
7174
7175# else
7176
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007177///////////////////////////////////////
7178// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007179
7180/*
7181 * Create a new terminal of "rows" by "cols" cells.
7182 * Start job for "cmd".
7183 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007184 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007185 * Return OK or FAIL.
7186 */
7187 static int
7188term_and_job_init(
7189 term_T *term,
7190 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007191 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007192 jobopt_T *opt,
7193 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007194{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007195 term->tl_arg0_cmd = NULL;
7196
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007197 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7198 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007199
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007200#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7201 if (opt->jo_set2 & JO2_ANSI_COLORS)
7202 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7203 else
7204 init_vterm_ansi_colors(term->tl_vterm);
7205#endif
7206
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007207 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007208 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007209 if (term->tl_job != NULL)
7210 ++term->tl_job->jv_refcount;
7211
7212 return term->tl_job != NULL
7213 && term->tl_job->jv_channel != NULL
7214 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7215}
7216
7217 static int
7218create_pty_only(term_T *term, jobopt_T *opt)
7219{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007220 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7221 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007222
7223 term->tl_job = job_alloc();
7224 if (term->tl_job == NULL)
7225 return FAIL;
7226 ++term->tl_job->jv_refcount;
7227
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007228 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007229 term->tl_job->jv_status = JOB_FINISHED;
7230
7231 return mch_create_pty_channel(term->tl_job, opt);
7232}
7233
7234/*
7235 * Free the terminal emulator part of "term".
7236 */
7237 static void
7238term_free_vterm(term_T *term)
7239{
7240 if (term->tl_vterm != NULL)
7241 vterm_free(term->tl_vterm);
7242 term->tl_vterm = NULL;
7243}
7244
7245/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007246 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007247 */
7248 static void
7249term_report_winsize(term_T *term, int rows, int cols)
7250{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007251 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007252 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7253 {
7254 int fd = -1;
7255 int part;
7256
7257 for (part = PART_OUT; part < PART_COUNT; ++part)
7258 {
7259 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007260 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007261 break;
7262 }
7263 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7264 mch_signal_job(term->tl_job, (char_u *)"winch");
7265 }
7266}
7267
7268# endif
7269
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007270#endif // FEAT_TERMINAL