blob: eb9e780cf1390781b2891cf682b8732b93d663ac [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{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004759 buf_T *buf;
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;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004774
4775 if (in_vim9script()
4776 && (check_for_buffer_arg(argvars, 0) == FAIL
4777 || check_for_string_arg(argvars, 1) == FAIL
4778 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4779 return;
4780
4781 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004782 if (buf == NULL)
4783 return;
4784 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004785 if (term->tl_vterm == NULL)
4786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004787 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004788 return;
4789 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004790
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004791 if (argvars[2].v_type != VAR_UNKNOWN)
4792 {
4793 dict_T *d;
4794
4795 if (argvars[2].v_type != VAR_DICT)
4796 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004797 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004798 return;
4799 }
4800 d = argvars[2].vval.v_dict;
4801 if (d != NULL)
4802 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004803 max_height = dict_get_number(d, (char_u *)"rows");
4804 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004805 }
4806 }
4807
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004808 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004809 if (fname == NULL)
4810 return;
4811 if (mch_stat((char *)fname, &st) >= 0)
4812 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004813 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004814 return;
4815 }
4816
Bram Moolenaard96ff162018-02-18 22:13:29 +01004817 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4818 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004819 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004820 return;
4821 }
4822
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004823 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004824
4825 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004826 state = vterm_obtain_state(term->tl_vterm);
4827 vterm_state_get_cursorpos(state, &cursor_pos);
4828
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004829 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4830 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004831 {
4832 int repeat = 0;
4833
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004834 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4835 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004836 {
4837 VTermScreenCell cell;
4838 int same_attr;
4839 int same_chars = TRUE;
4840 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004841 int is_cursor_pos = (pos.col == cursor_pos.col
4842 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004843
4844 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004845 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004846
4847 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4848 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004849 int c = cell.chars[i];
4850 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004851 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004852
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004853 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004854 if (i == 0)
4855 {
4856 c = (c == NUL) ? ' ' : c;
4857 pc = (pc == NUL) ? ' ' : pc;
4858 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004859 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004860 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004861 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004862 break;
4863 }
4864 same_attr = vtermAttr2hl(cell.attrs)
4865 == vtermAttr2hl(prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004866 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4867 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004868 if (same_chars && cell.width == prev_cell.width && same_attr
4869 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004870 {
4871 ++repeat;
4872 }
4873 else
4874 {
4875 if (repeat > 0)
4876 {
4877 fprintf(fd, "@%d", repeat);
4878 repeat = 0;
4879 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004880 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004881
4882 if (cell.chars[0] == NUL)
4883 fputs(" ", fd);
4884 else
4885 {
4886 char_u charbuf[10];
4887 int len;
4888
4889 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4890 && cell.chars[i] != NUL; ++i)
4891 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004892 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004893 fwrite(charbuf, len, 1, fd);
4894 }
4895 }
4896
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004897 // When only the characters differ we don't write anything, the
4898 // following "|", "@" or NL will indicate using the same
4899 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004900 if (cell.width != prev_cell.width || !same_attr)
4901 {
4902 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004903 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004904 else
4905 fputs("+", fd);
4906
4907 if (same_attr)
4908 {
4909 fputs("&", fd);
4910 }
4911 else
4912 {
4913 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004914 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004915 fputs("&", fd);
4916 else
4917 {
4918 fputs("#", fd);
4919 dump_term_color(fd, &cell.fg);
4920 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004921 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004922 fputs("&", fd);
4923 else
4924 {
4925 fputs("#", fd);
4926 dump_term_color(fd, &cell.bg);
4927 }
4928 }
4929 }
4930
4931 prev_cell = cell;
4932 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004933
4934 if (cell.width == 2)
4935 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004936 }
4937 if (repeat > 0)
4938 fprintf(fd, "@%d", repeat);
4939 fputs("\n", fd);
4940 }
4941
4942 fclose(fd);
4943}
4944
4945/*
4946 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4947 */
4948 static void
4949dump_is_corrupt(garray_T *gap)
4950{
4951 ga_concat(gap, (char_u *)"CORRUPT");
4952}
4953
4954 static void
4955append_cell(garray_T *gap, cellattr_T *cell)
4956{
4957 if (ga_grow(gap, 1) == OK)
4958 {
4959 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4960 ++gap->ga_len;
4961 }
4962}
4963
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004964 static void
4965clear_cellattr(cellattr_T *cell)
4966{
4967 CLEAR_FIELD(*cell);
4968 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4969 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4970}
4971
Bram Moolenaard96ff162018-02-18 22:13:29 +01004972/*
4973 * Read the dump file from "fd" and append lines to the current buffer.
4974 * Return the cell width of the longest line.
4975 */
4976 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004977read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004978{
4979 int c;
4980 garray_T ga_text;
4981 garray_T ga_cell;
4982 char_u *prev_char = NULL;
4983 int attr = 0;
4984 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004985 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004986 term_T *term = curbuf->b_term;
4987 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004988 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004989
4990 ga_init2(&ga_text, 1, 90);
4991 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004992 clear_cellattr(&cell);
4993 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004994 cursor_pos->row = -1;
4995 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004996
4997 c = fgetc(fd);
4998 for (;;)
4999 {
5000 if (c == EOF)
5001 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02005002 if (c == '\r')
5003 {
5004 // DOS line endings? Ignore.
5005 c = fgetc(fd);
5006 }
5007 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005008 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005009 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005010 if (ga_text.ga_data == NULL)
5011 dump_is_corrupt(&ga_text);
5012 if (ga_grow(&term->tl_scrollback, 1) == OK)
5013 {
5014 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
5015 + term->tl_scrollback.ga_len;
5016
5017 if (max_cells < ga_cell.ga_len)
5018 max_cells = ga_cell.ga_len;
5019 line->sb_cols = ga_cell.ga_len;
5020 line->sb_cells = ga_cell.ga_data;
5021 line->sb_fill_attr = term->tl_default_color;
5022 ++term->tl_scrollback.ga_len;
5023 ga_init(&ga_cell);
5024
5025 ga_append(&ga_text, NUL);
5026 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5027 ga_text.ga_len, FALSE);
5028 }
5029 else
5030 ga_clear(&ga_cell);
5031 ga_text.ga_len = 0;
5032
5033 c = fgetc(fd);
5034 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005035 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005036 {
5037 int prev_len = ga_text.ga_len;
5038
Bram Moolenaar9271d052018-02-25 21:39:46 +01005039 if (c == '>')
5040 {
5041 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005042 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005043 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5044 cursor_pos->col = ga_cell.ga_len;
5045 }
5046
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005047 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005048 c = fgetc(fd);
5049 if (c != EOF)
5050 ga_append(&ga_text, c);
5051 for (;;)
5052 {
5053 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005054 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005055 || c == EOF || c == '\n')
5056 break;
5057 ga_append(&ga_text, c);
5058 }
5059
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005060 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005061 vim_free(prev_char);
5062 if (ga_text.ga_data != NULL)
5063 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5064 ga_text.ga_len - prev_len);
5065
Bram Moolenaar9271d052018-02-25 21:39:46 +01005066 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005067 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005068 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005069 }
5070 else if (c == '+' || c == '*')
5071 {
5072 int is_bg;
5073
5074 cell.width = c == '+' ? 1 : 2;
5075
5076 c = fgetc(fd);
5077 if (c == '&')
5078 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005079 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005080 c = fgetc(fd);
5081 }
5082 else if (isdigit(c))
5083 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005084 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005085 attr = 0;
5086 while (isdigit(c))
5087 {
5088 attr = attr * 10 + (c - '0');
5089 c = fgetc(fd);
5090 }
5091 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005092
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005093 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005094 for (is_bg = 0; is_bg <= 1; ++is_bg)
5095 {
5096 if (c == '&')
5097 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005098 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005099 c = fgetc(fd);
5100 }
5101 else if (c == '#')
5102 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005103 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005104
5105 c = fgetc(fd);
5106 red = hex2nr(c);
5107 c = fgetc(fd);
5108 red = (red << 4) + hex2nr(c);
5109 c = fgetc(fd);
5110 green = hex2nr(c);
5111 c = fgetc(fd);
5112 green = (green << 4) + hex2nr(c);
5113 c = fgetc(fd);
5114 blue = hex2nr(c);
5115 c = fgetc(fd);
5116 blue = (blue << 4) + hex2nr(c);
5117 c = fgetc(fd);
5118 if (!isdigit(c))
5119 dump_is_corrupt(&ga_text);
5120 while (isdigit(c))
5121 {
5122 index = index * 10 + (c - '0');
5123 c = fgetc(fd);
5124 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005125 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005126 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005127 type = VTERM_COLOR_RGB;
5128 if (index == 0)
5129 {
5130 if (is_bg)
5131 type |= VTERM_COLOR_DEFAULT_BG;
5132 else
5133 type |= VTERM_COLOR_DEFAULT_FG;
5134 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005135 }
5136 else
5137 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005138 type = VTERM_COLOR_INDEXED;
5139 index -= 1;
5140 }
5141 if (is_bg)
5142 {
5143 cell.bg.type = type;
5144 cell.bg.red = red;
5145 cell.bg.green = green;
5146 cell.bg.blue = blue;
5147 cell.bg.index = index;
5148 }
5149 else
5150 {
5151 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005152 cell.fg.red = red;
5153 cell.fg.green = green;
5154 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005155 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005156 }
5157 }
5158 else
5159 dump_is_corrupt(&ga_text);
5160 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005161 }
5162 else
5163 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005164 }
5165 else
5166 dump_is_corrupt(&ga_text);
5167
5168 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005169 if (cell.width == 2)
5170 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005171 }
5172 else if (c == '@')
5173 {
5174 if (prev_char == NULL)
5175 dump_is_corrupt(&ga_text);
5176 else
5177 {
5178 int count = 0;
5179
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005180 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005181 for (;;)
5182 {
5183 c = fgetc(fd);
5184 if (!isdigit(c))
5185 break;
5186 count = count * 10 + (c - '0');
5187 }
5188
5189 while (count-- > 0)
5190 {
5191 ga_concat(&ga_text, prev_char);
5192 append_cell(&ga_cell, &cell);
5193 }
5194 }
5195 }
5196 else
5197 {
5198 dump_is_corrupt(&ga_text);
5199 c = fgetc(fd);
5200 }
5201 }
5202
5203 if (ga_text.ga_len > 0)
5204 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005205 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005206 dump_is_corrupt(&ga_text);
5207 ga_append(&ga_text, NUL);
5208 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5209 ga_text.ga_len, FALSE);
5210 }
5211
5212 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005213 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005214 vim_free(prev_char);
5215
5216 return max_cells;
5217}
5218
5219/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005220 * Return an allocated string with at least "text_width" "=" characters and
5221 * "fname" inserted in the middle.
5222 */
5223 static char_u *
5224get_separator(int text_width, char_u *fname)
5225{
5226 int width = MAX(text_width, curwin->w_width);
5227 char_u *textline;
5228 int fname_size;
5229 char_u *p = fname;
5230 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005231 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005232
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005233 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005234 if (textline == NULL)
5235 return NULL;
5236
5237 fname_size = vim_strsize(fname);
5238 if (fname_size < width - 8)
5239 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005240 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005241 width = MAX(text_width, fname_size + 8);
5242 }
5243 else if (fname_size > width - 8)
5244 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005245 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005246 p = gettail(fname);
5247 fname_size = vim_strsize(p);
5248 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005249 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005250 while (fname_size > width - 8)
5251 {
5252 p += (*mb_ptr2len)(p);
5253 fname_size = vim_strsize(p);
5254 }
5255
5256 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5257 textline[i] = '=';
5258 textline[i++] = ' ';
5259
5260 STRCPY(textline + i, p);
5261 off = STRLEN(textline);
5262 textline[off] = ' ';
5263 for (i = 1; i < (width - fname_size) / 2; ++i)
5264 textline[off + i] = '=';
5265 textline[off + i] = NUL;
5266
5267 return textline;
5268}
5269
5270/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005271 * Common for "term_dumpdiff()" and "term_dumpload()".
5272 */
5273 static void
5274term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5275{
5276 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005277 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005278 char_u buf1[NUMBUFLEN];
5279 char_u buf2[NUMBUFLEN];
5280 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005281 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005282 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005283 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005284 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005285 char_u *textline = NULL;
5286
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005287 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005288 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005289 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005290 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005291 if (fname1 == NULL || (do_diff && fname2 == NULL))
5292 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005293 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005294 return;
5295 }
5296 fd1 = mch_fopen((char *)fname1, READBIN);
5297 if (fd1 == NULL)
5298 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005299 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005300 return;
5301 }
5302 if (do_diff)
5303 {
5304 fd2 = mch_fopen((char *)fname2, READBIN);
5305 if (fd2 == NULL)
5306 {
5307 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005308 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005309 return;
5310 }
5311 }
5312
5313 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005314 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5315 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5316 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5317 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5318 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005319
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005320 if (opt.jo_term_name == NULL)
5321 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005322 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005323
Bram Moolenaar51e14382019-05-25 20:21:28 +02005324 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005325 if (fname_tofree != NULL)
5326 {
5327 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5328 opt.jo_term_name = fname_tofree;
5329 }
5330 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005331
Bram Moolenaar87abab92019-06-03 21:14:59 +02005332 if (opt.jo_bufnr_buf != NULL)
5333 {
5334 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5335
5336 // With "bufnr" argument: enter the window with this buffer and make it
5337 // empty.
5338 if (wp == NULL)
5339 semsg(_(e_invarg2), "bufnr");
5340 else
5341 {
5342 buf = curbuf;
5343 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005344 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005345 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005346 redraw_later(NOT_VALID);
5347 }
5348 }
5349 else
5350 // Create a new terminal window.
5351 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5352
Bram Moolenaard96ff162018-02-18 22:13:29 +01005353 if (buf != NULL && buf->b_term != NULL)
5354 {
5355 int i;
5356 linenr_T bot_lnum;
5357 linenr_T lnum;
5358 term_T *term = buf->b_term;
5359 int width;
5360 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005361 VTermPos cursor_pos1;
5362 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005363
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005364 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005365
Bram Moolenaard96ff162018-02-18 22:13:29 +01005366 rettv->vval.v_number = buf->b_fnum;
5367
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005368 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005369 width = read_dump_file(fd1, &cursor_pos1);
5370
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005371 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005372 if (cursor_pos1.row >= 0)
5373 {
5374 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5375 coladvance(cursor_pos1.col);
5376 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005377
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005378 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005379 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005380
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005381 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005382 if (!do_diff)
5383 goto theend;
5384
5385 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5386
Bram Moolenaar4a696342018-04-05 18:45:26 +02005387 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005388 if (textline == NULL)
5389 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005390 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5391 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5392 vim_free(textline);
5393
5394 textline = get_separator(width, fname2);
5395 if (textline == NULL)
5396 goto theend;
5397 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5398 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005399 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005400
5401 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005402 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005403 if (width2 > width)
5404 {
5405 vim_free(textline);
5406 textline = alloc(width2 + 1);
5407 if (textline == NULL)
5408 goto theend;
5409 width = width2;
5410 textline[width] = NUL;
5411 }
5412 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5413
5414 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5415 {
5416 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5417 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005418 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005419 for (i = 0; i < width; ++i)
5420 textline[i] = '-';
5421 }
5422 else
5423 {
5424 char_u *line1;
5425 char_u *line2;
5426 char_u *p1;
5427 char_u *p2;
5428 int col;
5429 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5430 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5431 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5432 ->sb_cells;
5433
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005434 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005435 line1 = vim_strsave(ml_get(lnum));
5436 if (line1 == NULL)
5437 break;
5438 p1 = line1;
5439
5440 line2 = ml_get(lnum + bot_lnum);
5441 p2 = line2;
5442 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5443 {
5444 int len1 = utfc_ptr2len(p1);
5445 int len2 = utfc_ptr2len(p2);
5446
5447 textline[col] = ' ';
5448 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005449 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005450 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005451 else if (lnum == cursor_pos1.row + 1
5452 && col == cursor_pos1.col
5453 && (cursor_pos1.row != cursor_pos2.row
5454 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005455 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005456 textline[col] = '>';
5457 else if (lnum == cursor_pos2.row + 1
5458 && col == cursor_pos2.col
5459 && (cursor_pos1.row != cursor_pos2.row
5460 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005461 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005462 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005463 else if (cellattr1 != NULL && cellattr2 != NULL)
5464 {
5465 if ((cellattr1 + col)->width
5466 != (cellattr2 + col)->width)
5467 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005468 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005469 &(cellattr2 + col)->fg))
5470 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005471 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005472 &(cellattr2 + col)->bg))
5473 textline[col] = 'b';
5474 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5475 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5476 textline[col] = 'a';
5477 }
5478 p1 += len1;
5479 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005480 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005481 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005482
5483 while (col < width)
5484 {
5485 if (*p1 == NUL && *p2 == NUL)
5486 textline[col] = '?';
5487 else if (*p1 == NUL)
5488 {
5489 textline[col] = '+';
5490 p2 += utfc_ptr2len(p2);
5491 }
5492 else
5493 {
5494 textline[col] = '-';
5495 p1 += utfc_ptr2len(p1);
5496 }
5497 ++col;
5498 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005499
5500 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005501 }
5502 if (add_empty_scrollback(term, &term->tl_default_color,
5503 term->tl_top_diff_rows) == OK)
5504 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5505 ++bot_lnum;
5506 }
5507
5508 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5509 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005510 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005511 for (i = 0; i < width; ++i)
5512 textline[i] = '+';
5513 if (add_empty_scrollback(term, &term->tl_default_color,
5514 term->tl_top_diff_rows) == OK)
5515 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5516 ++lnum;
5517 ++bot_lnum;
5518 }
5519
5520 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005521
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005522 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005523 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005524 }
5525
5526theend:
5527 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005528 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005529 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005530 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005531 fclose(fd2);
5532}
5533
5534/*
5535 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5536 * bottom files.
5537 * Return FAIL when this is not possible.
5538 */
5539 int
5540term_swap_diff()
5541{
5542 term_T *term = curbuf->b_term;
5543 linenr_T line_count;
5544 linenr_T top_rows;
5545 linenr_T bot_rows;
5546 linenr_T bot_start;
5547 linenr_T lnum;
5548 char_u *p;
5549 sb_line_T *sb_line;
5550
5551 if (term == NULL
5552 || !term_is_finished(curbuf)
5553 || term->tl_top_diff_rows == 0
5554 || term->tl_scrollback.ga_len == 0)
5555 return FAIL;
5556
5557 line_count = curbuf->b_ml.ml_line_count;
5558 top_rows = term->tl_top_diff_rows;
5559 bot_rows = term->tl_bot_diff_rows;
5560 bot_start = line_count - bot_rows;
5561 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5562
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005563 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005564 for (lnum = 1; lnum <= top_rows; ++lnum)
5565 {
5566 p = vim_strsave(ml_get(1));
5567 if (p == NULL)
5568 return OK;
5569 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005570 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005571 vim_free(p);
5572 }
5573
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005574 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005575 for (lnum = 1; lnum <= bot_rows; ++lnum)
5576 {
5577 p = vim_strsave(ml_get(bot_start + lnum));
5578 if (p == NULL)
5579 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005580 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005581 ml_append(lnum - 1, p, 0, FALSE);
5582 vim_free(p);
5583 }
5584
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005585 // move top title to bottom
5586 p = vim_strsave(ml_get(bot_rows + 1));
5587 if (p == NULL)
5588 return OK;
5589 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005590 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005591 vim_free(p);
5592
5593 // move bottom title to top
5594 p = vim_strsave(ml_get(line_count - top_rows));
5595 if (p == NULL)
5596 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005597 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005598 ml_append(bot_rows, p, 0, FALSE);
5599 vim_free(p);
5600
Bram Moolenaard96ff162018-02-18 22:13:29 +01005601 if (top_rows == bot_rows)
5602 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005603 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005604 for (lnum = 0; lnum < top_rows; ++lnum)
5605 {
5606 sb_line_T temp;
5607
5608 temp = *(sb_line + lnum);
5609 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5610 *(sb_line + bot_start + lnum) = temp;
5611 }
5612 }
5613 else
5614 {
5615 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005616 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005617
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005618 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005619 if (temp != NULL)
5620 {
5621 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5622 mch_memmove(term->tl_scrollback.ga_data,
5623 temp + bot_start,
5624 sizeof(sb_line_T) * bot_rows);
5625 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5626 temp + top_rows,
5627 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5628 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5629 + line_count - top_rows,
5630 temp,
5631 sizeof(sb_line_T) * top_rows);
5632 vim_free(temp);
5633 }
5634 }
5635
5636 term->tl_top_diff_rows = bot_rows;
5637 term->tl_bot_diff_rows = top_rows;
5638
5639 update_screen(NOT_VALID);
5640 return OK;
5641}
5642
5643/*
5644 * "term_dumpdiff(filename, filename, options)" function
5645 */
5646 void
5647f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5648{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005649 if (in_vim9script()
5650 && (check_for_string_arg(argvars, 0) == FAIL
5651 || check_for_string_arg(argvars, 1) == FAIL
5652 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5653 return;
5654
Bram Moolenaard96ff162018-02-18 22:13:29 +01005655 term_load_dump(argvars, rettv, TRUE);
5656}
5657
5658/*
5659 * "term_dumpload(filename, options)" function
5660 */
5661 void
5662f_term_dumpload(typval_T *argvars, typval_T *rettv)
5663{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005664 if (in_vim9script()
5665 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005666 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005667 return;
5668
Bram Moolenaard96ff162018-02-18 22:13:29 +01005669 term_load_dump(argvars, rettv, FALSE);
5670}
5671
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005672/*
5673 * "term_getaltscreen(buf)" function
5674 */
5675 void
5676f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5677{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005678 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005679
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005680 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5681 return;
5682
5683 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005684 if (buf == NULL)
5685 return;
5686 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5687}
5688
5689/*
5690 * "term_getattr(attr, name)" function
5691 */
5692 void
5693f_term_getattr(typval_T *argvars, typval_T *rettv)
5694{
5695 int attr;
5696 size_t i;
5697 char_u *name;
5698
5699 static struct {
5700 char *name;
5701 int attr;
5702 } attrs[] = {
5703 {"bold", HL_BOLD},
5704 {"italic", HL_ITALIC},
5705 {"underline", HL_UNDERLINE},
5706 {"strike", HL_STRIKETHROUGH},
5707 {"reverse", HL_INVERSE},
5708 };
5709
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005710 if (in_vim9script()
5711 && (check_for_number_arg(argvars, 0) == FAIL
5712 || check_for_string_arg(argvars, 1) == FAIL))
5713 return;
5714
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005715 attr = tv_get_number(&argvars[0]);
5716 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005717 if (name == NULL)
5718 return;
5719
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005720 if (attr > HL_ALL)
5721 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02005722 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005723 if (STRCMP(name, attrs[i].name) == 0)
5724 {
5725 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5726 break;
5727 }
5728}
5729
5730/*
5731 * "term_getcursor(buf)" function
5732 */
5733 void
5734f_term_getcursor(typval_T *argvars, typval_T *rettv)
5735{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005736 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005737 term_T *term;
5738 list_T *l;
5739 dict_T *d;
5740
5741 if (rettv_list_alloc(rettv) == FAIL)
5742 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005743
5744 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5745 return;
5746
5747 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005748 if (buf == NULL)
5749 return;
5750 term = buf->b_term;
5751
5752 l = rettv->vval.v_list;
5753 list_append_number(l, term->tl_cursor_pos.row + 1);
5754 list_append_number(l, term->tl_cursor_pos.col + 1);
5755
5756 d = dict_alloc();
5757 if (d != NULL)
5758 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005759 dict_add_number(d, "visible", term->tl_cursor_visible);
5760 dict_add_number(d, "blink", blink_state_is_inverted()
5761 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5762 dict_add_number(d, "shape", term->tl_cursor_shape);
5763 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005764 list_append_dict(l, d);
5765 }
5766}
5767
5768/*
5769 * "term_getjob(buf)" function
5770 */
5771 void
5772f_term_getjob(typval_T *argvars, typval_T *rettv)
5773{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005774 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005775
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005776 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5777 return;
5778
5779 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005780 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005781 {
5782 rettv->v_type = VAR_SPECIAL;
5783 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005784 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005785 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005786
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005787 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005788 rettv->vval.v_job = buf->b_term->tl_job;
5789 if (rettv->vval.v_job != NULL)
5790 ++rettv->vval.v_job->jv_refcount;
5791}
5792
5793 static int
5794get_row_number(typval_T *tv, term_T *term)
5795{
5796 if (tv->v_type == VAR_STRING
5797 && tv->vval.v_string != NULL
5798 && STRCMP(tv->vval.v_string, ".") == 0)
5799 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005800 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005801}
5802
5803/*
5804 * "term_getline(buf, row)" function
5805 */
5806 void
5807f_term_getline(typval_T *argvars, typval_T *rettv)
5808{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005809 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005810 term_T *term;
5811 int row;
5812
5813 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005814
5815 if (in_vim9script()
5816 && (check_for_buffer_arg(argvars, 0) == FAIL
5817 || check_for_lnum_arg(argvars, 1) == FAIL))
5818 return;
5819
5820 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005821 if (buf == NULL)
5822 return;
5823 term = buf->b_term;
5824 row = get_row_number(&argvars[1], term);
5825
5826 if (term->tl_vterm == NULL)
5827 {
5828 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5829
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005830 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005831 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5832 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5833 }
5834 else
5835 {
5836 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5837 VTermRect rect;
5838 int len;
5839 char_u *p;
5840
5841 if (row < 0 || row >= term->tl_rows)
5842 return;
5843 len = term->tl_cols * MB_MAXBYTES + 1;
5844 p = alloc(len);
5845 if (p == NULL)
5846 return;
5847 rettv->vval.v_string = p;
5848
5849 rect.start_col = 0;
5850 rect.end_col = term->tl_cols;
5851 rect.start_row = row;
5852 rect.end_row = row + 1;
5853 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5854 }
5855}
5856
5857/*
5858 * "term_getscrolled(buf)" function
5859 */
5860 void
5861f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5862{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005863 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005864
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005865 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5866 return;
5867
5868 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005869 if (buf == NULL)
5870 return;
5871 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5872}
5873
5874/*
5875 * "term_getsize(buf)" function
5876 */
5877 void
5878f_term_getsize(typval_T *argvars, typval_T *rettv)
5879{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005880 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005881 list_T *l;
5882
5883 if (rettv_list_alloc(rettv) == FAIL)
5884 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005885
5886 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5887 return;
5888
5889 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005890 if (buf == NULL)
5891 return;
5892
5893 l = rettv->vval.v_list;
5894 list_append_number(l, buf->b_term->tl_rows);
5895 list_append_number(l, buf->b_term->tl_cols);
5896}
5897
5898/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005899 * "term_setsize(buf, rows, cols)" function
5900 */
5901 void
5902f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5903{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005904 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02005905 term_T *term;
5906 varnumber_T rows, cols;
5907
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005908 if (in_vim9script()
5909 && (check_for_buffer_arg(argvars, 0) == FAIL
5910 || check_for_number_arg(argvars, 1) == FAIL
5911 || check_for_number_arg(argvars, 2) == FAIL))
5912 return;
5913
5914 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005915 if (buf == NULL)
5916 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005917 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005918 return;
5919 }
5920 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005921 return;
5922 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005923 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005924 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005925 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005926 cols = cols <= 0 ? term->tl_cols : cols;
5927 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005928 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005929
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005930 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005931 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5932 term_report_winsize(term, term->tl_rows, term->tl_cols);
5933}
5934
5935/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005936 * "term_getstatus(buf)" function
5937 */
5938 void
5939f_term_getstatus(typval_T *argvars, typval_T *rettv)
5940{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005941 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005942 term_T *term;
5943 char_u val[100];
5944
5945 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005946
5947 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5948 return;
5949
5950 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005951 if (buf == NULL)
5952 return;
5953 term = buf->b_term;
5954
5955 if (term_job_running(term))
5956 STRCPY(val, "running");
5957 else
5958 STRCPY(val, "finished");
5959 if (term->tl_normal_mode)
5960 STRCAT(val, ",normal");
5961 rettv->vval.v_string = vim_strsave(val);
5962}
5963
5964/*
5965 * "term_gettitle(buf)" function
5966 */
5967 void
5968f_term_gettitle(typval_T *argvars, typval_T *rettv)
5969{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005970 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005971
5972 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005973
5974 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5975 return;
5976
5977 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005978 if (buf == NULL)
5979 return;
5980
5981 if (buf->b_term->tl_title != NULL)
5982 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5983}
5984
5985/*
5986 * "term_gettty(buf)" function
5987 */
5988 void
5989f_term_gettty(typval_T *argvars, typval_T *rettv)
5990{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005991 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005992 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005993 int num = 0;
5994
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005995 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005996 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005997 || check_for_opt_bool_arg(argvars, 1) == FAIL))
5998 return;
5999
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006000 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006001 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006002 if (buf == NULL)
6003 return;
6004 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02006005 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006006
6007 switch (num)
6008 {
6009 case 0:
6010 if (buf->b_term->tl_job != NULL)
6011 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006012 break;
6013 case 1:
6014 if (buf->b_term->tl_job != NULL)
6015 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006016 break;
6017 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006018 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006019 return;
6020 }
6021 if (p != NULL)
6022 rettv->vval.v_string = vim_strsave(p);
6023}
6024
6025/*
6026 * "term_list()" function
6027 */
6028 void
6029f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
6030{
6031 term_T *tp;
6032 list_T *l;
6033
6034 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
6035 return;
6036
6037 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006038 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02006039 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006040 if (list_append_number(l,
6041 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6042 return;
6043}
6044
6045/*
6046 * "term_scrape(buf, row)" function
6047 */
6048 void
6049f_term_scrape(typval_T *argvars, typval_T *rettv)
6050{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006051 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006052 VTermScreen *screen = NULL;
6053 VTermPos pos;
6054 list_T *l;
6055 term_T *term;
6056 char_u *p;
6057 sb_line_T *line;
6058
6059 if (rettv_list_alloc(rettv) == FAIL)
6060 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006061
6062 if (in_vim9script()
6063 && (check_for_buffer_arg(argvars, 0) == FAIL
6064 || check_for_lnum_arg(argvars, 1) == FAIL))
6065 return;
6066
6067 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006068 if (buf == NULL)
6069 return;
6070 term = buf->b_term;
6071
6072 l = rettv->vval.v_list;
6073 pos.row = get_row_number(&argvars[1], term);
6074
6075 if (term->tl_vterm != NULL)
6076 {
6077 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006078 if (screen == NULL) // can't really happen
6079 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006080 p = NULL;
6081 line = NULL;
6082 }
6083 else
6084 {
6085 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6086
6087 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6088 return;
6089 p = ml_get_buf(buf, lnum + 1, FALSE);
6090 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6091 }
6092
6093 for (pos.col = 0; pos.col < term->tl_cols; )
6094 {
6095 dict_T *dcell;
6096 int width;
6097 VTermScreenCellAttrs attrs;
6098 VTermColor fg, bg;
6099 char_u rgb[8];
6100 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6101 int off = 0;
6102 int i;
6103
6104 if (screen == NULL)
6105 {
6106 cellattr_T *cellattr;
6107 int len;
6108
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006109 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006110 if (pos.col >= line->sb_cols)
6111 break;
6112 cellattr = line->sb_cells + pos.col;
6113 width = cellattr->width;
6114 attrs = cellattr->attrs;
6115 fg = cellattr->fg;
6116 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006117 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006118 mch_memmove(mbs, p, len);
6119 mbs[len] = NUL;
6120 p += len;
6121 }
6122 else
6123 {
6124 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006125
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006126 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6127 break;
6128 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6129 {
6130 if (cell.chars[i] == 0)
6131 break;
6132 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6133 }
6134 mbs[off] = NUL;
6135 width = cell.width;
6136 attrs = cell.attrs;
6137 fg = cell.fg;
6138 bg = cell.bg;
6139 }
6140 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006141 if (dcell == NULL)
6142 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006143 list_append_dict(l, dcell);
6144
Bram Moolenaare0be1672018-07-08 16:50:37 +02006145 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006146
6147 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6148 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006149 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006150 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6151 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006152 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006153
Bram Moolenaar83d47902020-03-26 20:34:00 +01006154 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006155 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006156
6157 ++pos.col;
6158 if (width == 2)
6159 ++pos.col;
6160 }
6161}
6162
6163/*
6164 * "term_sendkeys(buf, keys)" function
6165 */
6166 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006167f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006168{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006169 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006170 char_u *msg;
6171 term_T *term;
6172
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006173 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006174 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006175 || check_for_string_arg(argvars, 1) == FAIL))
6176 return;
6177
6178 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006179 if (buf == NULL)
6180 return;
6181
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006182 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006183 if (msg == NULL)
6184 return;
6185 term = buf->b_term;
6186 if (term->tl_vterm == NULL)
6187 return;
6188
6189 while (*msg != NUL)
6190 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006191 int c;
6192
6193 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6194 {
6195 c = TO_SPECIAL(msg[1], msg[2]);
6196 msg += 3;
6197 }
6198 else
6199 {
6200 c = PTR2CHAR(msg);
6201 msg += MB_CPTR2LEN(msg);
6202 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006203 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006204 }
6205}
6206
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006207#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6208/*
6209 * "term_getansicolors(buf)" function
6210 */
6211 void
6212f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6213{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006214 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006215 term_T *term;
6216 VTermState *state;
6217 VTermColor color;
6218 char_u hexbuf[10];
6219 int index;
6220 list_T *list;
6221
6222 if (rettv_list_alloc(rettv) == FAIL)
6223 return;
6224
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006225 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6226 return;
6227
6228 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006229 if (buf == NULL)
6230 return;
6231 term = buf->b_term;
6232 if (term->tl_vterm == NULL)
6233 return;
6234
6235 list = rettv->vval.v_list;
6236 state = vterm_obtain_state(term->tl_vterm);
6237 for (index = 0; index < 16; index++)
6238 {
6239 vterm_state_get_palette_color(state, index, &color);
6240 sprintf((char *)hexbuf, "#%02x%02x%02x",
6241 color.red, color.green, color.blue);
6242 if (list_append_string(list, hexbuf, 7) == FAIL)
6243 return;
6244 }
6245}
6246
6247/*
6248 * "term_setansicolors(buf, list)" function
6249 */
6250 void
6251f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6252{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006253 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006254 term_T *term;
6255
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006256 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006257 && (check_for_buffer_arg(argvars, 0) == FAIL
6258 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006259 return;
6260
6261 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006262 if (buf == NULL)
6263 return;
6264 term = buf->b_term;
6265 if (term->tl_vterm == NULL)
6266 return;
6267
6268 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6269 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006270 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006271 return;
6272 }
6273
6274 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006275 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006276}
6277#endif
6278
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006279/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006280 * "term_setapi(buf, api)" function
6281 */
6282 void
6283f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6284{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006285 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006286 term_T *term;
6287 char_u *api;
6288
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006289 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006290 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006291 || check_for_string_arg(argvars, 1) == FAIL))
6292 return;
6293
6294 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006295 if (buf == NULL)
6296 return;
6297 term = buf->b_term;
6298 vim_free(term->tl_api);
6299 api = tv_get_string_chk(&argvars[1]);
6300 if (api != NULL)
6301 term->tl_api = vim_strsave(api);
6302 else
6303 term->tl_api = NULL;
6304}
6305
6306/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006307 * "term_setrestore(buf, command)" function
6308 */
6309 void
6310f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6311{
6312#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006313 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006314 term_T *term;
6315 char_u *cmd;
6316
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006317 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006318 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006319 || check_for_string_arg(argvars, 1) == FAIL))
6320 return;
6321
6322 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006323 if (buf == NULL)
6324 return;
6325 term = buf->b_term;
6326 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006327 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006328 if (cmd != NULL)
6329 term->tl_command = vim_strsave(cmd);
6330 else
6331 term->tl_command = NULL;
6332#endif
6333}
6334
6335/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006336 * "term_setkill(buf, how)" function
6337 */
6338 void
6339f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6340{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006341 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006342 term_T *term;
6343 char_u *how;
6344
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006345 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006346 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006347 || check_for_string_arg(argvars, 1) == FAIL))
6348 return;
6349
6350 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006351 if (buf == NULL)
6352 return;
6353 term = buf->b_term;
6354 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006355 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006356 if (how != NULL)
6357 term->tl_kill = vim_strsave(how);
6358 else
6359 term->tl_kill = NULL;
6360}
6361
6362/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006363 * "term_start(command, options)" function
6364 */
6365 void
6366f_term_start(typval_T *argvars, typval_T *rettv)
6367{
6368 jobopt_T opt;
6369 buf_T *buf;
6370
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006371 if (in_vim9script()
6372 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6373 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6374 return;
6375
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006376 init_job_options(&opt);
6377 if (argvars[1].v_type != VAR_UNKNOWN
6378 && get_job_options(&argvars[1], &opt,
6379 JO_TIMEOUT_ALL + JO_STOPONEXIT
6380 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6381 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6382 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6383 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006384 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006385 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006386 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006387 return;
6388
Bram Moolenaar13568252018-03-16 20:46:58 +01006389 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006390
6391 if (buf != NULL && buf->b_term != NULL)
6392 rettv->vval.v_number = buf->b_fnum;
6393}
6394
6395/*
6396 * "term_wait" function
6397 */
6398 void
6399f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6400{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006401 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006402
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006403 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006404 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006405 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006406 return;
6407
6408 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006409 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006410 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006411 if (buf->b_term->tl_job == NULL)
6412 {
6413 ch_log(NULL, "term_wait(): no job to wait for");
6414 return;
6415 }
6416 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006417 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006418 return;
6419
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006420 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006421 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006422 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6423 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006424 // The job is dead, keep reading channel I/O until the channel is
6425 // closed. buf->b_term may become NULL if the terminal was closed while
6426 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006427 ch_log(NULL, "term_wait(): waiting for channel to close");
6428 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6429 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006430 term_flush_messages();
6431
Bram Moolenaard45aa552018-05-21 22:50:29 +02006432 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006433 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006434 // If the terminal is closed when the channel is closed the
6435 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006436 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006437 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006438
6439 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006440 }
6441 else
6442 {
6443 long wait = 10L;
6444
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006445 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006446
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006447 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006448 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006449 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006450 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006451
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006452 // Flushing messages on channels is hopefully sufficient.
6453 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006454 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006455 }
6456}
6457
6458/*
6459 * Called when a channel has sent all the lines to a terminal.
6460 * Send a CTRL-D to mark the end of the text.
6461 */
6462 void
6463term_send_eof(channel_T *ch)
6464{
6465 term_T *term;
6466
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006467 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006468 if (term->tl_job == ch->ch_job)
6469 {
6470 if (term->tl_eof_chars != NULL)
6471 {
6472 channel_send(ch, PART_IN, term->tl_eof_chars,
6473 (int)STRLEN(term->tl_eof_chars), NULL);
6474 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6475 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006476# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006477 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006478 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006479 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6480# endif
6481 }
6482}
6483
Bram Moolenaar113e1072019-01-20 15:30:40 +01006484#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006485 job_T *
6486term_getjob(term_T *term)
6487{
6488 return term != NULL ? term->tl_job : NULL;
6489}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006490#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006491
Bram Moolenaar4f974752019-02-17 17:44:42 +01006492# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006493
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006494///////////////////////////////////////
6495// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006496#ifdef PROTO
6497typedef int COORD;
6498typedef int DWORD;
6499typedef int HANDLE;
6500typedef int *DWORD_PTR;
6501typedef int HPCON;
6502typedef int HRESULT;
6503typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006504typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006505typedef int PSIZE_T;
6506typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006507typedef int BOOL;
6508# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006509#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006510
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006511HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6512HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6513HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006514BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6515BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6516void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006517
6518 static int
6519dyn_conpty_init(int verbose)
6520{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006521 static HMODULE hKerneldll = NULL;
6522 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006523 static struct
6524 {
6525 char *name;
6526 FARPROC *ptr;
6527 } conpty_entry[] =
6528 {
6529 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6530 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6531 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6532 {"InitializeProcThreadAttributeList",
6533 (FARPROC*)&pInitializeProcThreadAttributeList},
6534 {"UpdateProcThreadAttribute",
6535 (FARPROC*)&pUpdateProcThreadAttribute},
6536 {"DeleteProcThreadAttributeList",
6537 (FARPROC*)&pDeleteProcThreadAttributeList},
6538 {NULL, NULL}
6539 };
6540
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006541 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006542 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006543 if (verbose)
6544 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006545 return FAIL;
6546 }
6547
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006548 // No need to initialize twice.
6549 if (hKerneldll)
6550 return OK;
6551
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006552 hKerneldll = vimLoadLib("kernel32.dll");
6553 for (i = 0; conpty_entry[i].name != NULL
6554 && conpty_entry[i].ptr != NULL; ++i)
6555 {
6556 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6557 conpty_entry[i].name)) == NULL)
6558 {
6559 if (verbose)
6560 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006561 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006562 return FAIL;
6563 }
6564 }
6565
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006566 return OK;
6567}
6568
6569 static int
6570conpty_term_and_job_init(
6571 term_T *term,
6572 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006573 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006574 jobopt_T *opt,
6575 jobopt_T *orig_opt)
6576{
6577 WCHAR *cmd_wchar = NULL;
6578 WCHAR *cmd_wchar_copy = NULL;
6579 WCHAR *cwd_wchar = NULL;
6580 WCHAR *env_wchar = NULL;
6581 channel_T *channel = NULL;
6582 job_T *job = NULL;
6583 HANDLE jo = NULL;
6584 garray_T ga_cmd, ga_env;
6585 char_u *cmd = NULL;
6586 HRESULT hr;
6587 COORD consize;
6588 SIZE_T breq;
6589 PROCESS_INFORMATION proc_info;
6590 HANDLE i_theirs = NULL;
6591 HANDLE o_theirs = NULL;
6592 HANDLE i_ours = NULL;
6593 HANDLE o_ours = NULL;
6594
6595 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6596 ga_init2(&ga_env, (int)sizeof(char*), 20);
6597
6598 if (argvar->v_type == VAR_STRING)
6599 {
6600 cmd = argvar->vval.v_string;
6601 }
6602 else if (argvar->v_type == VAR_LIST)
6603 {
6604 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6605 goto failed;
6606 cmd = ga_cmd.ga_data;
6607 }
6608 if (cmd == NULL || *cmd == NUL)
6609 {
6610 emsg(_(e_invarg));
6611 goto failed;
6612 }
6613
6614 term->tl_arg0_cmd = vim_strsave(cmd);
6615
6616 cmd_wchar = enc_to_utf16(cmd, NULL);
6617
6618 if (cmd_wchar != NULL)
6619 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006620 // Request by CreateProcessW
6621 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006622 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006623 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6624 }
6625
6626 ga_clear(&ga_cmd);
6627 if (cmd_wchar == NULL)
6628 goto failed;
6629 if (opt->jo_cwd != NULL)
6630 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6631
6632 win32_build_env(opt->jo_env, &ga_env, TRUE);
6633 env_wchar = ga_env.ga_data;
6634
6635 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6636 goto failed;
6637 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6638 goto failed;
6639
6640 consize.X = term->tl_cols;
6641 consize.Y = term->tl_rows;
6642 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6643 &term->tl_conpty);
6644 if (FAILED(hr))
6645 goto failed;
6646
6647 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6648
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006649 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006650 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006651 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006652 if (!term->tl_siex.lpAttributeList)
6653 goto failed;
6654 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6655 0, &breq))
6656 goto failed;
6657 if (!pUpdateProcThreadAttribute(
6658 term->tl_siex.lpAttributeList, 0,
6659 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6660 sizeof(HPCON), NULL, NULL))
6661 goto failed;
6662
6663 channel = add_channel();
6664 if (channel == NULL)
6665 goto failed;
6666
6667 job = job_alloc();
6668 if (job == NULL)
6669 goto failed;
6670 if (argvar->v_type == VAR_STRING)
6671 {
6672 int argc;
6673
6674 build_argv_from_string(cmd, &job->jv_argv, &argc);
6675 }
6676 else
6677 {
6678 int argc;
6679
6680 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6681 }
6682
6683 if (opt->jo_set & JO_IN_BUF)
6684 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6685
6686 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6687 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006688 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006689 env_wchar, cwd_wchar,
6690 &term->tl_siex.StartupInfo, &proc_info))
6691 goto failed;
6692
6693 CloseHandle(i_theirs);
6694 CloseHandle(o_theirs);
6695
6696 channel_set_pipes(channel,
6697 (sock_T)i_ours,
6698 (sock_T)o_ours,
6699 (sock_T)o_ours);
6700
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006701 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006702 channel->ch_write_text_mode = TRUE;
6703
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006704 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006705 channel->ch_anonymous_pipe = TRUE;
6706
6707 jo = CreateJobObject(NULL, NULL);
6708 if (jo == NULL)
6709 goto failed;
6710
6711 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6712 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006713 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006714 CloseHandle(jo);
6715 jo = NULL;
6716 }
6717
6718 ResumeThread(proc_info.hThread);
6719 CloseHandle(proc_info.hThread);
6720
6721 vim_free(cmd_wchar);
6722 vim_free(cmd_wchar_copy);
6723 vim_free(cwd_wchar);
6724 vim_free(env_wchar);
6725
6726 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6727 goto failed;
6728
6729#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6730 if (opt->jo_set2 & JO2_ANSI_COLORS)
6731 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6732 else
6733 init_vterm_ansi_colors(term->tl_vterm);
6734#endif
6735
6736 channel_set_job(channel, job, opt);
6737 job_set_options(job, opt);
6738
6739 job->jv_channel = channel;
6740 job->jv_proc_info = proc_info;
6741 job->jv_job_object = jo;
6742 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006743 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006744 ++job->jv_refcount;
6745 term->tl_job = job;
6746
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006747 // Redirecting stdout and stderr doesn't work at the job level. Instead
6748 // open the file here and handle it in. opt->jo_io was changed in
6749 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006750 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6751 {
6752 char_u *fname = opt->jo_io_name[PART_OUT];
6753
6754 ch_log(channel, "Opening output file %s", fname);
6755 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6756 if (term->tl_out_fd == NULL)
6757 semsg(_(e_notopen), fname);
6758 }
6759
6760 return OK;
6761
6762failed:
6763 ga_clear(&ga_cmd);
6764 ga_clear(&ga_env);
6765 vim_free(cmd_wchar);
6766 vim_free(cmd_wchar_copy);
6767 vim_free(cwd_wchar);
6768 if (channel != NULL)
6769 channel_clear(channel);
6770 if (job != NULL)
6771 {
6772 job->jv_channel = NULL;
6773 job_cleanup(job);
6774 }
6775 term->tl_job = NULL;
6776 if (jo != NULL)
6777 CloseHandle(jo);
6778
6779 if (term->tl_siex.lpAttributeList != NULL)
6780 {
6781 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6782 vim_free(term->tl_siex.lpAttributeList);
6783 }
6784 term->tl_siex.lpAttributeList = NULL;
6785 if (o_theirs != NULL)
6786 CloseHandle(o_theirs);
6787 if (o_ours != NULL)
6788 CloseHandle(o_ours);
6789 if (i_ours != NULL)
6790 CloseHandle(i_ours);
6791 if (i_theirs != NULL)
6792 CloseHandle(i_theirs);
6793 if (term->tl_conpty != NULL)
6794 pClosePseudoConsole(term->tl_conpty);
6795 term->tl_conpty = NULL;
6796 return FAIL;
6797}
6798
6799 static void
6800conpty_term_report_winsize(term_T *term, int rows, int cols)
6801{
6802 COORD consize;
6803
6804 consize.X = cols;
6805 consize.Y = rows;
6806 pResizePseudoConsole(term->tl_conpty, consize);
6807}
6808
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006809 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006810term_free_conpty(term_T *term)
6811{
6812 if (term->tl_siex.lpAttributeList != NULL)
6813 {
6814 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6815 vim_free(term->tl_siex.lpAttributeList);
6816 }
6817 term->tl_siex.lpAttributeList = NULL;
6818 if (term->tl_conpty != NULL)
6819 pClosePseudoConsole(term->tl_conpty);
6820 term->tl_conpty = NULL;
6821}
6822
6823 int
6824use_conpty(void)
6825{
6826 return has_conpty;
6827}
6828
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006829# ifndef PROTO
6830
6831#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6832#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006833#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006834
6835void* (*winpty_config_new)(UINT64, void*);
6836void* (*winpty_open)(void*, void*);
6837void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6838BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6839void (*winpty_config_set_mouse_mode)(void*, int);
6840void (*winpty_config_set_initial_size)(void*, int, int);
6841LPCWSTR (*winpty_conin_name)(void*);
6842LPCWSTR (*winpty_conout_name)(void*);
6843LPCWSTR (*winpty_conerr_name)(void*);
6844void (*winpty_free)(void*);
6845void (*winpty_config_free)(void*);
6846void (*winpty_spawn_config_free)(void*);
6847void (*winpty_error_free)(void*);
6848LPCWSTR (*winpty_error_msg)(void*);
6849BOOL (*winpty_set_size)(void*, int, int, void*);
6850HANDLE (*winpty_agent_process)(void*);
6851
6852#define WINPTY_DLL "winpty.dll"
6853
6854static HINSTANCE hWinPtyDLL = NULL;
6855# endif
6856
6857 static int
6858dyn_winpty_init(int verbose)
6859{
6860 int i;
6861 static struct
6862 {
6863 char *name;
6864 FARPROC *ptr;
6865 } winpty_entry[] =
6866 {
6867 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6868 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6869 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6870 {"winpty_config_set_mouse_mode",
6871 (FARPROC*)&winpty_config_set_mouse_mode},
6872 {"winpty_config_set_initial_size",
6873 (FARPROC*)&winpty_config_set_initial_size},
6874 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6875 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6876 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6877 {"winpty_free", (FARPROC*)&winpty_free},
6878 {"winpty_open", (FARPROC*)&winpty_open},
6879 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6880 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6881 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6882 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6883 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6884 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6885 {NULL, NULL}
6886 };
6887
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006888 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006889 if (hWinPtyDLL)
6890 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006891 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6892 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006893 if (*p_winptydll != NUL)
6894 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6895 if (!hWinPtyDLL)
6896 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6897 if (!hWinPtyDLL)
6898 {
6899 if (verbose)
Martin Tournoij1a3e5742021-07-24 13:57:29 +02006900 semsg(_(e_loadlib),
6901 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
6902 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006903 return FAIL;
6904 }
6905 for (i = 0; winpty_entry[i].name != NULL
6906 && winpty_entry[i].ptr != NULL; ++i)
6907 {
6908 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6909 winpty_entry[i].name)) == NULL)
6910 {
6911 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006912 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006913 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006914 return FAIL;
6915 }
6916 }
6917
6918 return OK;
6919}
6920
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006921 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006922winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006923 term_T *term,
6924 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006925 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006926 jobopt_T *opt,
6927 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006928{
6929 WCHAR *cmd_wchar = NULL;
6930 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006931 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006932 channel_T *channel = NULL;
6933 job_T *job = NULL;
6934 DWORD error;
6935 HANDLE jo = NULL;
6936 HANDLE child_process_handle;
6937 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006938 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006939 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006940 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006941 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006942
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006943 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6944 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006945
6946 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006947 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006948 cmd = argvar->vval.v_string;
6949 }
6950 else if (argvar->v_type == VAR_LIST)
6951 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006952 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006953 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006954 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006955 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006956 if (cmd == NULL || *cmd == NUL)
6957 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006958 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006959 goto failed;
6960 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006961
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006962 term->tl_arg0_cmd = vim_strsave(cmd);
6963
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006964 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006965 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006966 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006967 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006968 if (opt->jo_cwd != NULL)
6969 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006970
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006971 win32_build_env(opt->jo_env, &ga_env, TRUE);
6972 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006973
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006974 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6975 if (term->tl_winpty_config == NULL)
6976 goto failed;
6977
6978 winpty_config_set_mouse_mode(term->tl_winpty_config,
6979 WINPTY_MOUSE_MODE_FORCE);
6980 winpty_config_set_initial_size(term->tl_winpty_config,
6981 term->tl_cols, term->tl_rows);
6982 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6983 if (term->tl_winpty == NULL)
6984 goto failed;
6985
6986 spawn_config = winpty_spawn_config_new(
6987 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6988 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6989 NULL,
6990 cmd_wchar,
6991 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006992 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006993 &winpty_err);
6994 if (spawn_config == NULL)
6995 goto failed;
6996
6997 channel = add_channel();
6998 if (channel == NULL)
6999 goto failed;
7000
7001 job = job_alloc();
7002 if (job == NULL)
7003 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02007004 if (argvar->v_type == VAR_STRING)
7005 {
7006 int argc;
7007
7008 build_argv_from_string(cmd, &job->jv_argv, &argc);
7009 }
7010 else
7011 {
7012 int argc;
7013
7014 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
7015 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007016
7017 if (opt->jo_set & JO_IN_BUF)
7018 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
7019
7020 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
7021 &child_thread_handle, &error, &winpty_err))
7022 goto failed;
7023
7024 channel_set_pipes(channel,
7025 (sock_T)CreateFileW(
7026 winpty_conin_name(term->tl_winpty),
7027 GENERIC_WRITE, 0, NULL,
7028 OPEN_EXISTING, 0, NULL),
7029 (sock_T)CreateFileW(
7030 winpty_conout_name(term->tl_winpty),
7031 GENERIC_READ, 0, NULL,
7032 OPEN_EXISTING, 0, NULL),
7033 (sock_T)CreateFileW(
7034 winpty_conerr_name(term->tl_winpty),
7035 GENERIC_READ, 0, NULL,
7036 OPEN_EXISTING, 0, NULL));
7037
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007038 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007039 channel->ch_write_text_mode = TRUE;
7040
7041 jo = CreateJobObject(NULL, NULL);
7042 if (jo == NULL)
7043 goto failed;
7044
7045 if (!AssignProcessToJobObject(jo, child_process_handle))
7046 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007047 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007048 CloseHandle(jo);
7049 jo = NULL;
7050 }
7051
7052 winpty_spawn_config_free(spawn_config);
7053 vim_free(cmd_wchar);
7054 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007055 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007056
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007057 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7058 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007059
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007060#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7061 if (opt->jo_set2 & JO2_ANSI_COLORS)
7062 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7063 else
7064 init_vterm_ansi_colors(term->tl_vterm);
7065#endif
7066
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007067 channel_set_job(channel, job, opt);
7068 job_set_options(job, opt);
7069
7070 job->jv_channel = channel;
7071 job->jv_proc_info.hProcess = child_process_handle;
7072 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7073 job->jv_job_object = jo;
7074 job->jv_status = JOB_STARTED;
7075 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007076 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007077 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007078 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007079 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007080 ++job->jv_refcount;
7081 term->tl_job = job;
7082
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007083 // Redirecting stdout and stderr doesn't work at the job level. Instead
7084 // open the file here and handle it in. opt->jo_io was changed in
7085 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007086 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7087 {
7088 char_u *fname = opt->jo_io_name[PART_OUT];
7089
7090 ch_log(channel, "Opening output file %s", fname);
7091 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7092 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007093 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007094 }
7095
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007096 return OK;
7097
7098failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007099 ga_clear(&ga_cmd);
7100 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007101 vim_free(cmd_wchar);
7102 vim_free(cwd_wchar);
7103 if (spawn_config != NULL)
7104 winpty_spawn_config_free(spawn_config);
7105 if (channel != NULL)
7106 channel_clear(channel);
7107 if (job != NULL)
7108 {
7109 job->jv_channel = NULL;
7110 job_cleanup(job);
7111 }
7112 term->tl_job = NULL;
7113 if (jo != NULL)
7114 CloseHandle(jo);
7115 if (term->tl_winpty != NULL)
7116 winpty_free(term->tl_winpty);
7117 term->tl_winpty = NULL;
7118 if (term->tl_winpty_config != NULL)
7119 winpty_config_free(term->tl_winpty_config);
7120 term->tl_winpty_config = NULL;
7121 if (winpty_err != NULL)
7122 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007123 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007124 (short_u *)winpty_error_msg(winpty_err), NULL);
7125
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007126 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007127 winpty_error_free(winpty_err);
7128 }
7129 return FAIL;
7130}
7131
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007132/*
7133 * Create a new terminal of "rows" by "cols" cells.
7134 * Store a reference in "term".
7135 * Return OK or FAIL.
7136 */
7137 static int
7138term_and_job_init(
7139 term_T *term,
7140 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007141 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007142 jobopt_T *opt,
7143 jobopt_T *orig_opt)
7144{
7145 int use_winpty = FALSE;
7146 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007147 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007148
7149 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7150 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7151
7152 if (!has_winpty && !has_conpty)
7153 // If neither is available give the errors for winpty, since when
7154 // conpty is not available it can't be installed either.
7155 return dyn_winpty_init(TRUE);
7156
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007157 if (opt->jo_tty_type != NUL)
7158 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007159
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007160 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007161 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007162 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007163 use_conpty = TRUE;
7164 else if (has_winpty)
7165 use_winpty = TRUE;
7166 // else: error
7167 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007168 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007169 {
7170 if (has_winpty)
7171 use_winpty = TRUE;
7172 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007173 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007174 {
7175 if (has_conpty)
7176 use_conpty = TRUE;
7177 else
7178 return dyn_conpty_init(TRUE);
7179 }
7180
7181 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007182 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007183
7184 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007185 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007186
7187 // error
7188 return dyn_winpty_init(TRUE);
7189}
7190
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007191 static int
7192create_pty_only(term_T *term, jobopt_T *options)
7193{
7194 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7195 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7196 char in_name[80], out_name[80];
7197 channel_T *channel = NULL;
7198
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007199 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7200 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007201
7202 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7203 GetCurrentProcessId(),
7204 curbuf->b_fnum);
7205 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7206 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7207 PIPE_UNLIMITED_INSTANCES,
7208 0, 0, NMPWAIT_NOWAIT, NULL);
7209 if (hPipeIn == INVALID_HANDLE_VALUE)
7210 goto failed;
7211
7212 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7213 GetCurrentProcessId(),
7214 curbuf->b_fnum);
7215 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7216 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7217 PIPE_UNLIMITED_INSTANCES,
7218 0, 0, 0, NULL);
7219 if (hPipeOut == INVALID_HANDLE_VALUE)
7220 goto failed;
7221
7222 ConnectNamedPipe(hPipeIn, NULL);
7223 ConnectNamedPipe(hPipeOut, NULL);
7224
7225 term->tl_job = job_alloc();
7226 if (term->tl_job == NULL)
7227 goto failed;
7228 ++term->tl_job->jv_refcount;
7229
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007230 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007231 term->tl_job->jv_status = JOB_FINISHED;
7232
7233 channel = add_channel();
7234 if (channel == NULL)
7235 goto failed;
7236 term->tl_job->jv_channel = channel;
7237 channel->ch_keep_open = TRUE;
7238 channel->ch_named_pipe = TRUE;
7239
7240 channel_set_pipes(channel,
7241 (sock_T)hPipeIn,
7242 (sock_T)hPipeOut,
7243 (sock_T)hPipeOut);
7244 channel_set_job(channel, term->tl_job, options);
7245 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7246 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7247
7248 return OK;
7249
7250failed:
7251 if (hPipeIn != NULL)
7252 CloseHandle(hPipeIn);
7253 if (hPipeOut != NULL)
7254 CloseHandle(hPipeOut);
7255 return FAIL;
7256}
7257
7258/*
7259 * Free the terminal emulator part of "term".
7260 */
7261 static void
7262term_free_vterm(term_T *term)
7263{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007264 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007265 if (term->tl_winpty != NULL)
7266 winpty_free(term->tl_winpty);
7267 term->tl_winpty = NULL;
7268 if (term->tl_winpty_config != NULL)
7269 winpty_config_free(term->tl_winpty_config);
7270 term->tl_winpty_config = NULL;
7271 if (term->tl_vterm != NULL)
7272 vterm_free(term->tl_vterm);
7273 term->tl_vterm = NULL;
7274}
7275
7276/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007277 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007278 */
7279 static void
7280term_report_winsize(term_T *term, int rows, int cols)
7281{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007282 if (term->tl_conpty)
7283 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007284 if (term->tl_winpty)
7285 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7286}
7287
7288 int
7289terminal_enabled(void)
7290{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007291 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007292}
7293
7294# else
7295
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007296///////////////////////////////////////
7297// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007298
7299/*
7300 * Create a new terminal of "rows" by "cols" cells.
7301 * Start job for "cmd".
7302 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007303 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007304 * Return OK or FAIL.
7305 */
7306 static int
7307term_and_job_init(
7308 term_T *term,
7309 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007310 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007311 jobopt_T *opt,
7312 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007313{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007314 term->tl_arg0_cmd = NULL;
7315
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007316 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7317 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007318
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007319#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7320 if (opt->jo_set2 & JO2_ANSI_COLORS)
7321 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7322 else
7323 init_vterm_ansi_colors(term->tl_vterm);
7324#endif
7325
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007326 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007327 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007328 if (term->tl_job != NULL)
7329 ++term->tl_job->jv_refcount;
7330
7331 return term->tl_job != NULL
7332 && term->tl_job->jv_channel != NULL
7333 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7334}
7335
7336 static int
7337create_pty_only(term_T *term, jobopt_T *opt)
7338{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007339 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7340 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007341
7342 term->tl_job = job_alloc();
7343 if (term->tl_job == NULL)
7344 return FAIL;
7345 ++term->tl_job->jv_refcount;
7346
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007347 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007348 term->tl_job->jv_status = JOB_FINISHED;
7349
7350 return mch_create_pty_channel(term->tl_job, opt);
7351}
7352
7353/*
7354 * Free the terminal emulator part of "term".
7355 */
7356 static void
7357term_free_vterm(term_T *term)
7358{
7359 if (term->tl_vterm != NULL)
7360 vterm_free(term->tl_vterm);
7361 term->tl_vterm = NULL;
7362}
7363
7364/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007365 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007366 */
7367 static void
7368term_report_winsize(term_T *term, int rows, int cols)
7369{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007370 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007371 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7372 {
7373 int fd = -1;
7374 int part;
7375
7376 for (part = PART_OUT; part < PART_COUNT; ++part)
7377 {
7378 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007379 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007380 break;
7381 }
7382 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7383 mch_signal_job(term->tl_job, (char_u *)"winch");
7384 }
7385}
7386
7387# endif
7388
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007389#endif // FEAT_TERMINAL