blob: 1adf690adefc1465e9eef6d2ac0ca93f2f1ea41f [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// Store the last set and the desired cursor properties, so that we only update
208// them when needed. Doing it unnecessary may result in flicker.
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200209static char_u *last_set_cursor_color = NULL;
210static char_u *desired_cursor_color = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +0100211static int last_set_cursor_shape = -1;
212static int desired_cursor_shape = -1;
213static int last_set_cursor_blink = -1;
214static int desired_cursor_blink = -1;
215
216
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100217///////////////////////////////////////
218// 1. Generic code for all systems.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200219
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200220 static int
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200221cursor_color_equal(char_u *lhs_color, char_u *rhs_color)
222{
223 if (lhs_color != NULL && rhs_color != NULL)
224 return STRCMP(lhs_color, rhs_color) == 0;
225 return lhs_color == NULL && rhs_color == NULL;
226}
227
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200228 static void
229cursor_color_copy(char_u **to_color, char_u *from_color)
230{
231 // Avoid a free & alloc if the value is already right.
232 if (cursor_color_equal(*to_color, from_color))
233 return;
234 vim_free(*to_color);
235 *to_color = (from_color == NULL) ? NULL : vim_strsave(from_color);
236}
237
238 static char_u *
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200239cursor_color_get(char_u *color)
240{
241 return (color == NULL) ? (char_u *)"" : color;
242}
243
244
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200245/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200246 * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
Bram Moolenaar498c2562018-04-15 23:45:15 +0200247 * current window.
248 * Sets "rows" and/or "cols" to zero when it should follow the window size.
249 * Return TRUE if the size is the minimum size: "24*80".
250 */
251 static int
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200252parse_termwinsize(win_T *wp, int *rows, int *cols)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200253{
254 int minsize = FALSE;
255
256 *rows = 0;
257 *cols = 0;
258
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200259 if (*wp->w_p_tws != NUL)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200260 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200261 char_u *p = vim_strchr(wp->w_p_tws, 'x');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200262
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100263 // Syntax of value was already checked when it's set.
Bram Moolenaar498c2562018-04-15 23:45:15 +0200264 if (p == NULL)
265 {
266 minsize = TRUE;
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200267 p = vim_strchr(wp->w_p_tws, '*');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200268 }
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200269 *rows = atoi((char *)wp->w_p_tws);
Bram Moolenaar498c2562018-04-15 23:45:15 +0200270 *cols = atoi((char *)p + 1);
271 }
272 return minsize;
273}
274
275/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200276 * Determine the terminal size from 'termwinsize' and the current window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200277 */
278 static void
Bram Moolenaarb936b792020-09-04 18:34:09 +0200279set_term_and_win_size(term_T *term, jobopt_T *opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200280{
Bram Moolenaarb936b792020-09-04 18:34:09 +0200281 int rows, cols;
282 int minsize;
283
Bram Moolenaar13568252018-03-16 20:46:58 +0100284#ifdef FEAT_GUI
285 if (term->tl_system)
286 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100287 // Use the whole screen for the system command. However, it will start
288 // at the command line and scroll up as needed, using tl_toprow.
Bram Moolenaar13568252018-03-16 20:46:58 +0100289 term->tl_rows = Rows;
290 term->tl_cols = Columns;
Bram Moolenaar07b46af2018-04-10 14:56:18 +0200291 return;
Bram Moolenaar13568252018-03-16 20:46:58 +0100292 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100293#endif
Bram Moolenaarb936b792020-09-04 18:34:09 +0200294 term->tl_rows = curwin->w_height;
295 term->tl_cols = curwin->w_width;
296
297 minsize = parse_termwinsize(curwin, &rows, &cols);
298 if (minsize)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200299 {
Bram Moolenaarb936b792020-09-04 18:34:09 +0200300 if (term->tl_rows < rows)
301 term->tl_rows = rows;
302 if (term->tl_cols < cols)
303 term->tl_cols = cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200304 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200305 if ((opt->jo_set2 & JO2_TERM_ROWS))
306 term->tl_rows = opt->jo_term_rows;
307 else if (rows != 0)
308 term->tl_rows = rows;
309 if ((opt->jo_set2 & JO2_TERM_COLS))
310 term->tl_cols = opt->jo_term_cols;
311 else if (cols != 0)
312 term->tl_cols = cols;
313
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200314 if (!opt->jo_hidden)
Bram Moolenaarb936b792020-09-04 18:34:09 +0200315 {
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200316 if (term->tl_rows != curwin->w_height)
317 win_setheight_win(term->tl_rows, curwin);
318 if (term->tl_cols != curwin->w_width)
319 win_setwidth_win(term->tl_cols, curwin);
Bram Moolenaarb936b792020-09-04 18:34:09 +0200320
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200321 // Set 'winsize' now to avoid a resize at the next redraw.
322 if (!minsize && *curwin->w_p_tws != NUL)
323 {
324 char_u buf[100];
325
326 vim_snprintf((char *)buf, 100, "%dx%d",
327 term->tl_rows, term->tl_cols);
328 set_option_value((char_u *)"termwinsize", 0L, buf, OPT_LOCAL);
329 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200330 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200331}
332
333/*
334 * Initialize job options for a terminal job.
335 * Caller may overrule some of them.
336 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100337 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200338init_job_options(jobopt_T *opt)
339{
340 clear_job_options(opt);
341
342 opt->jo_mode = MODE_RAW;
343 opt->jo_out_mode = MODE_RAW;
344 opt->jo_err_mode = MODE_RAW;
345 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
346}
347
348/*
349 * Set job options mandatory for a terminal job.
350 */
351 static void
352setup_job_options(jobopt_T *opt, int rows, int cols)
353{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100354#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100355 // Win32: Redirecting the job output won't work, thus always connect stdout
356 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200357 if (!(opt->jo_set & JO_OUT_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200358#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200359 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100360 // Connect stdout to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200361 opt->jo_io[PART_OUT] = JIO_BUFFER;
362 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
363 opt->jo_modifiable[PART_OUT] = 0;
364 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
365 }
366
Bram Moolenaar4f974752019-02-17 17:44:42 +0100367#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100368 // Win32: Redirecting the job output won't work, thus always connect stderr
369 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200370 if (!(opt->jo_set & JO_ERR_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200371#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200372 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100373 // Connect stderr to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200374 opt->jo_io[PART_ERR] = JIO_BUFFER;
375 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
376 opt->jo_modifiable[PART_ERR] = 0;
377 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
378 }
379
380 opt->jo_pty = TRUE;
381 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
382 opt->jo_term_rows = rows;
383 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
384 opt->jo_term_cols = cols;
385}
386
387/*
Bram Moolenaar5c381eb2019-06-25 06:50:31 +0200388 * Flush messages on channels.
389 */
390 static void
391term_flush_messages()
392{
393 mch_check_messages();
394 parse_queued_messages();
395}
396
397/*
Bram Moolenaard96ff162018-02-18 22:13:29 +0100398 * Close a terminal buffer (and its window). Used when creating the terminal
399 * fails.
400 */
401 static void
402term_close_buffer(buf_T *buf, buf_T *old_curbuf)
403{
404 free_terminal(buf);
405 if (old_curbuf != NULL)
406 {
407 --curbuf->b_nwindows;
408 curbuf = old_curbuf;
409 curwin->w_buffer = curbuf;
410 ++curbuf->b_nwindows;
411 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100412 CHECK_CURBUF;
Bram Moolenaard96ff162018-02-18 22:13:29 +0100413
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100414 // Wiping out the buffer will also close the window and call
415 // free_terminal().
Bram Moolenaard96ff162018-02-18 22:13:29 +0100416 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
417}
418
419/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200420 * Start a terminal window and return its buffer.
Bram Moolenaar13568252018-03-16 20:46:58 +0100421 * Use either "argvar" or "argv", the other must be NULL.
422 * When "flags" has TERM_START_NOJOB only create the buffer, b_term and open
423 * the window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200424 * Returns NULL when failed.
425 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100426 buf_T *
427term_start(
428 typval_T *argvar,
429 char **argv,
430 jobopt_T *opt,
431 int flags)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200432{
433 exarg_T split_ea;
434 win_T *old_curwin = curwin;
435 term_T *term;
436 buf_T *old_curbuf = NULL;
437 int res;
438 buf_T *newbuf;
Bram Moolenaare1004402020-10-24 20:49:43 +0200439 int vertical = opt->jo_vertical || (cmdmod.cmod_split & WSP_VERT);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200440 jobopt_T orig_opt; // only partly filled
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200441
442 if (check_restricted() || check_secure())
443 return NULL;
Bram Moolenaare5b44862021-05-30 13:54:03 +0200444#ifdef FEAT_CMDWIN
445 if (cmdwin_type != 0)
446 {
447 emsg(_(e_cannot_open_terminal_from_command_line_window));
448 return NULL;
449 }
450#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200451
452 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
453 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
454 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
Bram Moolenaarb0992022020-01-30 14:55:42 +0100455 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))
456 || (argvar != NULL
457 && argvar->v_type == VAR_LIST
458 && argvar->vval.v_list != NULL
459 && argvar->vval.v_list->lv_first == &range_list_item))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200460 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100461 emsg(_(e_invarg));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200462 return NULL;
463 }
464
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200465 term = ALLOC_CLEAR_ONE(term_T);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200466 if (term == NULL)
467 return NULL;
468 term->tl_dirty_row_end = MAX_ROW;
469 term->tl_cursor_visible = TRUE;
470 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
471 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100472#ifdef FEAT_GUI
473 term->tl_system = (flags & TERM_START_SYSTEM);
474#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200475 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100476 ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200477 ga_init2(&term->tl_osc_buf, sizeof(char), 300);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200478
Bram Moolenaaraeed2a62021-04-29 20:18:45 +0200479 setpcmark();
Bram Moolenaara80faa82020-04-12 19:37:17 +0200480 CLEAR_FIELD(split_ea);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200481 if (opt->jo_curwin)
482 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100483 // Create a new buffer in the current window.
Bram Moolenaar13568252018-03-16 20:46:58 +0100484 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200485 {
486 no_write_message();
487 vim_free(term);
488 return NULL;
489 }
490 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaarb1009092020-05-31 16:04:42 +0200491 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
492 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
493 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200494 {
495 vim_free(term);
496 return NULL;
497 }
498 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100499 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200500 {
501 buf_T *buf;
502
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100503 // Create a new buffer without a window. Make it the current buffer for
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100504 // a moment to be able to do the initializations.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200505 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
506 BLN_NEW | BLN_LISTED);
507 if (buf == NULL || ml_open(buf) == FAIL)
508 {
509 vim_free(term);
510 return NULL;
511 }
512 old_curbuf = curbuf;
513 --curbuf->b_nwindows;
514 curbuf = buf;
515 curwin->w_buffer = buf;
516 ++curbuf->b_nwindows;
517 }
518 else
519 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100520 // Open a new window or tab.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200521 split_ea.cmdidx = CMD_new;
522 split_ea.cmd = (char_u *)"new";
523 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100524 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200525 {
526 split_ea.line2 = opt->jo_term_rows;
527 split_ea.addr_count = 1;
528 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100529 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200530 {
531 split_ea.line2 = opt->jo_term_cols;
532 split_ea.addr_count = 1;
533 }
534
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100535 if (vertical)
Bram Moolenaare1004402020-10-24 20:49:43 +0200536 cmdmod.cmod_split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200537 ex_splitview(&split_ea);
538 if (curwin == old_curwin)
539 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100540 // split failed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200541 vim_free(term);
542 return NULL;
543 }
544 }
545 term->tl_buffer = curbuf;
546 curbuf->b_term = term;
547
548 if (!opt->jo_hidden)
549 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100550 // Only one size was taken care of with :new, do the other one. With
551 // "curwin" both need to be done.
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100552 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200553 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100554 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200555 win_setwidth(opt->jo_term_cols);
556 }
557
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100558 // Link the new terminal in the list of active terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200559 term->tl_next = first_term;
560 first_term = term;
561
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100562 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
563
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200564 if (opt->jo_term_name != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100565 {
566 vim_free(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200567 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100568 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100569 else if (argv != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100570 {
571 vim_free(curbuf->b_ffname);
Bram Moolenaar13568252018-03-16 20:46:58 +0100572 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100573 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200574 else
575 {
576 int i;
577 size_t len;
578 char_u *cmd, *p;
579
580 if (argvar->v_type == VAR_STRING)
581 {
582 cmd = argvar->vval.v_string;
583 if (cmd == NULL)
584 cmd = (char_u *)"";
585 else if (STRCMP(cmd, "NONE") == 0)
586 cmd = (char_u *)"pty";
587 }
588 else if (argvar->v_type != VAR_LIST
589 || argvar->vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +0100590 || argvar->vval.v_list->lv_len == 0
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100591 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200592 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
593 cmd = (char_u*)"";
594
595 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200596 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200597
598 for (i = 0; p != NULL; ++i)
599 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100600 // Prepend a ! to the command name to avoid the buffer name equals
601 // the executable, otherwise ":w!" would overwrite it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200602 if (i == 0)
603 vim_snprintf((char *)p, len, "!%s", cmd);
604 else
605 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
606 if (buflist_findname(p) == NULL)
607 {
608 vim_free(curbuf->b_ffname);
609 curbuf->b_ffname = p;
610 break;
611 }
612 }
613 }
Bram Moolenaare010c722020-02-24 21:37:54 +0100614 vim_free(curbuf->b_sfname);
615 curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200616 curbuf->b_fname = curbuf->b_ffname;
617
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100618 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
619
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200620 if (opt->jo_term_opencmd != NULL)
621 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
622
623 if (opt->jo_eof_chars != NULL)
624 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
625
626 set_string_option_direct((char_u *)"buftype", -1,
627 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200628 // Avoid that 'buftype' is reset when this buffer is entered.
629 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200630
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100631 // Mark the buffer as not modifiable. It can only be made modifiable after
632 // the job finished.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200633 curbuf->b_p_ma = FALSE;
634
Bram Moolenaarb936b792020-09-04 18:34:09 +0200635 set_term_and_win_size(term, opt);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100636#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200637 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
638#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200639 setup_job_options(opt, term->tl_rows, term->tl_cols);
640
Bram Moolenaar13568252018-03-16 20:46:58 +0100641 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100642 return curbuf;
643
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100644#if defined(FEAT_SESSION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100645 // Remember the command for the session file.
Bram Moolenaar13568252018-03-16 20:46:58 +0100646 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100647 term->tl_command = vim_strsave((char_u *)"NONE");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100648 else if (argvar->v_type == VAR_STRING)
649 {
650 char_u *cmd = argvar->vval.v_string;
651
652 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
653 term->tl_command = vim_strsave(cmd);
654 }
655 else if (argvar->v_type == VAR_LIST
656 && argvar->vval.v_list != NULL
657 && argvar->vval.v_list->lv_len > 0)
658 {
659 garray_T ga;
660 listitem_T *item;
661
662 ga_init2(&ga, 1, 100);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200663 FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100664 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100665 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100666 char_u *p;
667
668 if (s == NULL)
669 break;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100670 p = vim_strsave_fnameescape(s, VSE_NONE);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100671 if (p == NULL)
672 break;
673 ga_concat(&ga, p);
674 vim_free(p);
675 ga_append(&ga, ' ');
676 }
677 if (item == NULL)
678 {
679 ga_append(&ga, NUL);
680 term->tl_command = ga.ga_data;
681 }
682 else
683 ga_clear(&ga);
684 }
685#endif
686
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100687 if (opt->jo_term_kill != NULL)
688 {
689 char_u *p = skiptowhite(opt->jo_term_kill);
690
691 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
692 }
693
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200694 if (opt->jo_term_api != NULL)
Bram Moolenaar21109272020-01-30 16:27:20 +0100695 {
696 char_u *p = skiptowhite(opt->jo_term_api);
697
698 term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
699 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200700 else
701 term->tl_api = vim_strsave((char_u *)"Tapi_");
702
Bram Moolenaar83d47902020-03-26 20:34:00 +0100703 if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
704 term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
705
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100706 // System dependent: setup the vterm and maybe start the job in it.
Bram Moolenaar13568252018-03-16 20:46:58 +0100707 if (argv == NULL
708 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200709 && argvar->vval.v_string != NULL
710 && STRCMP(argvar->vval.v_string, "NONE") == 0)
711 res = create_pty_only(term, opt);
712 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200713 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200714
715 newbuf = curbuf;
716 if (res == OK)
717 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100718 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200719 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
720 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100721#ifdef FEAT_GUI
722 if (term->tl_system)
723 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100724 // display first line below typed command
Bram Moolenaar13568252018-03-16 20:46:58 +0100725 term->tl_toprow = msg_row + 1;
726 term->tl_dirty_row_end = 0;
727 }
728#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200729
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100730 // Make sure we don't get stuck on sending keys to the job, it leads to
731 // a deadlock if the job is waiting for Vim to read.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200732 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
733
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200734 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200735 {
736 --curbuf->b_nwindows;
737 curbuf = old_curbuf;
738 curwin->w_buffer = curbuf;
739 ++curbuf->b_nwindows;
740 }
741 }
742 else
743 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100744 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200745 return NULL;
746 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100747
Bram Moolenaar13568252018-03-16 20:46:58 +0100748 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar28ed4df2019-10-26 16:21:40 +0200749 if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
750 apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200751 return newbuf;
752}
753
754/*
755 * ":terminal": open a terminal window and execute a job in it.
756 */
757 void
758ex_terminal(exarg_T *eap)
759{
760 typval_T argvar[2];
761 jobopt_T opt;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100762 int opt_shell = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200763 char_u *cmd;
764 char_u *tofree = NULL;
765
766 init_job_options(&opt);
767
768 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100769 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200770 {
771 char_u *p, *ep;
772
773 cmd += 2;
774 p = skiptowhite(cmd);
775 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200776 if (ep != NULL)
777 {
778 if (ep < p)
779 p = ep;
780 else
781 ep = NULL;
782 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200783
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200784# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
785 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
786 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200787 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200788 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100789 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200790 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200791 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200792 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200793 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200794 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200795 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200796 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100797 opt.jo_term_norestore = 1;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100798 else if (OPTARG_HAS("shell"))
799 opt_shell = TRUE;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200800 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100801 {
802 opt.jo_set2 |= JO2_TERM_KILL;
803 opt.jo_term_kill = ep + 1;
804 p = skiptowhite(cmd);
805 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200806 else if (OPTARG_HAS("api"))
807 {
808 opt.jo_set2 |= JO2_TERM_API;
809 if (ep != NULL)
810 {
811 opt.jo_term_api = ep + 1;
812 p = skiptowhite(cmd);
813 }
814 else
815 opt.jo_term_api = NULL;
816 }
817 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200818 {
819 opt.jo_set2 |= JO2_TERM_ROWS;
820 opt.jo_term_rows = atoi((char *)ep + 1);
821 p = skiptowhite(cmd);
822 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200823 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200824 {
825 opt.jo_set2 |= JO2_TERM_COLS;
826 opt.jo_term_cols = atoi((char *)ep + 1);
827 p = skiptowhite(cmd);
828 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200829 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200830 {
831 char_u *buf = NULL;
832 char_u *keys;
833
Bram Moolenaar21109272020-01-30 16:27:20 +0100834 vim_free(opt.jo_eof_chars);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200835 p = skiptowhite(cmd);
836 *p = NUL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200837 keys = replace_termcodes(ep + 1, &buf,
838 REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200839 opt.jo_set2 |= JO2_EOF_CHARS;
840 opt.jo_eof_chars = vim_strsave(keys);
841 vim_free(buf);
842 *p = ' ';
843 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100844#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100845 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
846 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100847 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100848 int tty_type = NUL;
849
850 p = skiptowhite(cmd);
851 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
852 tty_type = 'w';
853 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
854 tty_type = 'c';
855 else
856 {
857 semsg(e_invargval, "type");
858 goto theend;
859 }
860 opt.jo_set2 |= JO2_TTY_TYPE;
861 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100862 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100863#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200864 else
865 {
866 if (*p)
867 *p = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100868 semsg(_("E181: Invalid attribute: %s"), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100869 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200870 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200871# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200872 cmd = skipwhite(p);
873 }
874 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100875 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100876 // Make a copy of 'shell', an autocommand may change the option.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200877 tofree = cmd = vim_strsave(p_sh);
878
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100879 // default to close when the shell exits
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100880 if (opt.jo_term_finish == NUL)
Bram Moolenaare2978022020-04-26 14:47:44 +0200881 opt.jo_term_finish = TL_FINISH_CLOSE;
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100882 }
883
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200884 if (eap->addr_count > 0)
885 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100886 // Write lines from current buffer to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200887 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
888 opt.jo_io[PART_IN] = JIO_BUFFER;
889 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
890 opt.jo_in_top = eap->line1;
891 opt.jo_in_bot = eap->line2;
892 }
893
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100894 if (opt_shell && tofree == NULL)
895 {
896#ifdef UNIX
897 char **argv = NULL;
898 char_u *tofree1 = NULL;
899 char_u *tofree2 = NULL;
900
901 // :term ++shell command
902 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
903 term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaaradf4aa22019-11-10 22:36:44 +0100904 vim_free(argv);
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100905 vim_free(tofree1);
906 vim_free(tofree2);
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100907 goto theend;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100908#else
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100909# ifdef MSWIN
910 long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
911 char_u *newcmd;
912
913 newcmd = alloc(cmdlen);
914 if (newcmd == NULL)
915 goto theend;
916 tofree = newcmd;
917 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
918 cmd = newcmd;
919# else
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100920 emsg(_("E279: Sorry, ++shell is not supported on this system"));
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100921 goto theend;
922# endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100923#endif
924 }
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100925 argvar[0].v_type = VAR_STRING;
926 argvar[0].vval.v_string = cmd;
927 argvar[1].v_type = VAR_UNKNOWN;
928 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100929
930theend:
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100931 vim_free(tofree);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200932 vim_free(opt.jo_eof_chars);
933}
934
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100935#if defined(FEAT_SESSION) || defined(PROTO)
936/*
937 * Write a :terminal command to the session file to restore the terminal in
938 * window "wp".
939 * Return FAIL if writing fails.
940 */
941 int
Bram Moolenaar0e655112020-09-11 20:36:36 +0200942term_write_session(FILE *fd, win_T *wp, hashtab_T *terminal_bufs)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100943{
Bram Moolenaar0e655112020-09-11 20:36:36 +0200944 const int bufnr = wp->w_buffer->b_fnum;
945 term_T *term = wp->w_buffer->b_term;
946
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200947 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200948 {
949 // There are multiple views into this terminal buffer. We don't want to
950 // create the terminal multiple times. If it's the first time, create,
951 // otherwise link to the first buffer.
952 char id_as_str[NUMBUFLEN];
953 hashitem_T *entry;
954
955 vim_snprintf(id_as_str, sizeof(id_as_str), "%d", bufnr);
956
957 entry = hash_find(terminal_bufs, (char_u *)id_as_str);
958 if (!HASHITEM_EMPTY(entry))
959 {
960 // we've already opened this terminal buffer
961 if (fprintf(fd, "execute 'buffer ' . s:term_buf_%d", bufnr) < 0)
962 return FAIL;
963 return put_eol(fd);
964 }
965 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100966
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100967 // Create the terminal and run the command. This is not without
968 // risk, but let's assume the user only creates a session when this
969 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100970 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
971 term->tl_cols, term->tl_rows) < 0)
972 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100973#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100974 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
975 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100976#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100977 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
978 return FAIL;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200979 if (put_eol(fd) != OK)
980 return FAIL;
981
982 if (fprintf(fd, "let s:term_buf_%d = bufnr()", bufnr) < 0)
983 return FAIL;
984
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200985 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200986 {
987 char *hash_key = alloc(NUMBUFLEN);
988
989 vim_snprintf(hash_key, NUMBUFLEN, "%d", bufnr);
990 hash_add(terminal_bufs, (char_u *)hash_key);
991 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100992
993 return put_eol(fd);
994}
995
996/*
997 * Return TRUE if "buf" has a terminal that should be restored.
998 */
999 int
1000term_should_restore(buf_T *buf)
1001{
1002 term_T *term = buf->b_term;
1003
1004 return term != NULL && (term->tl_command == NULL
1005 || STRCMP(term->tl_command, "NONE") != 0);
1006}
1007#endif
1008
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001009/*
1010 * Free the scrollback buffer for "term".
1011 */
1012 static void
1013free_scrollback(term_T *term)
1014{
1015 int i;
1016
1017 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
1018 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
1019 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001020 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
1021 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
1022 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001023}
1024
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001025
1026// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +02001027static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001028
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001029/*
1030 * Free a terminal and everything it refers to.
1031 * Kills the job if there is one.
1032 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001033 * The actual terminal structure is freed later in free_unused_terminals(),
1034 * because callbacks may wipe out a buffer while the terminal is still
1035 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001036 */
1037 void
1038free_terminal(buf_T *buf)
1039{
1040 term_T *term = buf->b_term;
1041 term_T *tp;
1042
1043 if (term == NULL)
1044 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001045
1046 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001047 if (first_term == term)
1048 first_term = term->tl_next;
1049 else
1050 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
1051 if (tp->tl_next == term)
1052 {
1053 tp->tl_next = term->tl_next;
1054 break;
1055 }
1056
1057 if (term->tl_job != NULL)
1058 {
1059 if (term->tl_job->jv_status != JOB_ENDED
1060 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +01001061 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001062 job_stop(term->tl_job, NULL, "kill");
1063 job_unref(term->tl_job);
1064 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001065 term->tl_next = terminals_to_free;
1066 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001067
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001068 buf->b_term = NULL;
1069 if (in_terminal_loop == term)
1070 in_terminal_loop = NULL;
1071}
1072
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001073 void
1074free_unused_terminals()
1075{
1076 while (terminals_to_free != NULL)
1077 {
1078 term_T *term = terminals_to_free;
1079
1080 terminals_to_free = term->tl_next;
1081
1082 free_scrollback(term);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02001083 ga_clear(&term->tl_osc_buf);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001084
1085 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001086 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001087 vim_free(term->tl_title);
1088#ifdef FEAT_SESSION
1089 vim_free(term->tl_command);
1090#endif
1091 vim_free(term->tl_kill);
1092 vim_free(term->tl_status_text);
1093 vim_free(term->tl_opencmd);
1094 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001095 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001096#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001097 if (term->tl_out_fd != NULL)
1098 fclose(term->tl_out_fd);
1099#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001100 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001101 vim_free(term->tl_cursor_color);
1102 vim_free(term);
1103 }
1104}
1105
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001106/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001107 * Get the part that is connected to the tty. Normally this is PART_IN, but
1108 * when writing buffer lines to the job it can be another. This makes it
1109 * possible to do "1,5term vim -".
1110 */
1111 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001112get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001113{
1114#ifdef UNIX
1115 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1116 int i;
1117
1118 for (i = 0; i < 3; ++i)
1119 {
1120 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1121
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001122 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001123 return parts[i];
1124 }
1125#endif
1126 return PART_IN;
1127}
1128
1129/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001130 * Write job output "msg[len]" to the vterm.
1131 */
1132 static void
1133term_write_job_output(term_T *term, char_u *msg, size_t len)
1134{
1135 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001136 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001137
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001138 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001139
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001140 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001141 if (prevlen != vterm_output_get_buffer_current(vterm))
1142 {
1143 char buf[KEY_BUF_LEN];
1144 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1145
1146 if (curlen > 0)
1147 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1148 (char_u *)buf, (int)curlen, NULL);
1149 }
1150
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001151 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001152 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1153}
1154
1155 static void
1156update_cursor(term_T *term, int redraw)
1157{
1158 if (term->tl_normal_mode)
1159 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001160#ifdef FEAT_GUI
1161 if (term->tl_system)
1162 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1163 term->tl_cursor_pos.col);
1164 else
1165#endif
1166 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001167 if (redraw)
1168 {
1169 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1170 cursor_on();
1171 out_flush();
1172#ifdef FEAT_GUI
1173 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001174 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001175 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001176 gui_mch_flush();
1177 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001178#endif
1179 }
1180}
1181
1182/*
1183 * Invoked when "msg" output from a job was received. Write it to the terminal
1184 * of "buffer".
1185 */
1186 void
1187write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1188{
1189 size_t len = STRLEN(msg);
1190 term_T *term = buffer->b_term;
1191
Bram Moolenaar4f974752019-02-17 17:44:42 +01001192#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001193 // Win32: Cannot redirect output of the job, intercept it here and write to
1194 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001195 if (term->tl_out_fd != NULL)
1196 {
1197 ch_log(channel, "Writing %d bytes to output file", (int)len);
1198 fwrite(msg, len, 1, term->tl_out_fd);
1199 return;
1200 }
1201#endif
1202
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001203 if (term->tl_vterm == NULL)
1204 {
1205 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1206 return;
1207 }
1208 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01001209 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001210 term_write_job_output(term, msg, len);
1211
Bram Moolenaar13568252018-03-16 20:46:58 +01001212#ifdef FEAT_GUI
1213 if (term->tl_system)
1214 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001215 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001216 update_system_term(term);
1217 update_cursor(term, TRUE);
1218 }
1219 else
1220#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001221 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1222 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001223 if (!term->tl_normal_mode)
1224 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001225 // Don't use update_screen() when editing the command line, it gets
1226 // cleared.
1227 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001228 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001229 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001230 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001231 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001232 // update_screen() can be slow, check the terminal wasn't closed
1233 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001234 if (buffer == curbuf && curbuf->b_term != NULL)
1235 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001236 }
1237 else
1238 redraw_after_callback(TRUE);
1239 }
1240}
1241
1242/*
1243 * Send a mouse position and click to the vterm
1244 */
1245 static int
1246term_send_mouse(VTerm *vterm, int button, int pressed)
1247{
1248 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001249 int row = mouse_row - W_WINROW(curwin);
1250 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001251
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001252#ifdef FEAT_PROP_POPUP
1253 if (popup_is_popup(curwin))
1254 {
1255 row -= popup_top_extra(curwin);
1256 col -= popup_left_extra(curwin);
1257 }
1258#endif
1259 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001260 if (button != 0)
1261 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001262 return TRUE;
1263}
1264
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001265static int enter_mouse_col = -1;
1266static int enter_mouse_row = -1;
1267
1268/*
1269 * Handle a mouse click, drag or release.
1270 * Return TRUE when a mouse event is sent to the terminal.
1271 */
1272 static int
1273term_mouse_click(VTerm *vterm, int key)
1274{
1275#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001276 // For modeless selection mouse drag and release events are ignored, unless
1277 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001278 static int ignore_drag_release = TRUE;
1279 VTermMouseState mouse_state;
1280
1281 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1282 if (mouse_state.flags == 0)
1283 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001284 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001285 switch (key)
1286 {
1287 case K_LEFTDRAG:
1288 case K_LEFTRELEASE:
1289 case K_RIGHTDRAG:
1290 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001291 // Ignore drag and release events when the button-down wasn't
1292 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001293 if (ignore_drag_release)
1294 {
1295 int save_mouse_col, save_mouse_row;
1296
1297 if (enter_mouse_col < 0)
1298 break;
1299
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001300 // mouse click in the window gave us focus, handle that
1301 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001302 save_mouse_col = mouse_col;
1303 save_mouse_row = mouse_row;
1304 mouse_col = enter_mouse_col;
1305 mouse_row = enter_mouse_row;
1306 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1307 mouse_col = save_mouse_col;
1308 mouse_row = save_mouse_row;
1309 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001310 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001311 case K_LEFTMOUSE:
1312 case K_RIGHTMOUSE:
1313 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1314 ignore_drag_release = TRUE;
1315 else
1316 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001317 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001318 if (clip_star.available)
1319 {
1320 int button, is_click, is_drag;
1321
1322 button = get_mouse_button(KEY2TERMCAP1(key),
1323 &is_click, &is_drag);
1324 if (mouse_model_popup() && button == MOUSE_LEFT
1325 && (mod_mask & MOD_MASK_SHIFT))
1326 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001327 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001328 button = MOUSE_RIGHT;
1329 mod_mask &= ~MOD_MASK_SHIFT;
1330 }
1331 clip_modeless(button, is_click, is_drag);
1332 }
1333 break;
1334
1335 case K_MIDDLEMOUSE:
1336 if (clip_star.available)
1337 insert_reg('*', TRUE);
1338 break;
1339 }
1340 enter_mouse_col = -1;
1341 return FALSE;
1342 }
1343#endif
1344 enter_mouse_col = -1;
1345
1346 switch (key)
1347 {
1348 case K_LEFTMOUSE:
1349 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1350 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1351 case K_LEFTRELEASE:
1352 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1353 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1354 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1355 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1356 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1357 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1358 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1359 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1360 }
1361 return TRUE;
1362}
1363
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001364/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001365 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1366 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001367 * Return the number of bytes in "buf".
1368 */
1369 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001370term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001371{
1372 VTerm *vterm = term->tl_vterm;
1373 VTermKey key = VTERM_KEY_NONE;
1374 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001375 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001376
1377 switch (c)
1378 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001379 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001380
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001381 // don't use VTERM_KEY_BACKSPACE, it always
1382 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001383 case K_BS: c = term_backspace_char; break;
1384
1385 case ESC: key = VTERM_KEY_ESCAPE; break;
1386 case K_DEL: key = VTERM_KEY_DEL; break;
1387 case K_DOWN: key = VTERM_KEY_DOWN; break;
1388 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1389 key = VTERM_KEY_DOWN; break;
1390 case K_END: key = VTERM_KEY_END; break;
1391 case K_S_END: mod = VTERM_MOD_SHIFT;
1392 key = VTERM_KEY_END; break;
1393 case K_C_END: mod = VTERM_MOD_CTRL;
1394 key = VTERM_KEY_END; break;
1395 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1396 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1397 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1398 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1399 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1400 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1401 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1402 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1403 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1404 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1405 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1406 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1407 case K_HOME: key = VTERM_KEY_HOME; break;
1408 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1409 key = VTERM_KEY_HOME; break;
1410 case K_C_HOME: mod = VTERM_MOD_CTRL;
1411 key = VTERM_KEY_HOME; break;
1412 case K_INS: key = VTERM_KEY_INS; break;
1413 case K_K0: key = VTERM_KEY_KP_0; break;
1414 case K_K1: key = VTERM_KEY_KP_1; break;
1415 case K_K2: key = VTERM_KEY_KP_2; break;
1416 case K_K3: key = VTERM_KEY_KP_3; break;
1417 case K_K4: key = VTERM_KEY_KP_4; break;
1418 case K_K5: key = VTERM_KEY_KP_5; break;
1419 case K_K6: key = VTERM_KEY_KP_6; break;
1420 case K_K7: key = VTERM_KEY_KP_7; break;
1421 case K_K8: key = VTERM_KEY_KP_8; break;
1422 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001423 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001424 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001425 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001426 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001427 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1428 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001429 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1430 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001431 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1432 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001433 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1434 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1435 case K_LEFT: key = VTERM_KEY_LEFT; break;
1436 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1437 key = VTERM_KEY_LEFT; break;
1438 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1439 key = VTERM_KEY_LEFT; break;
1440 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1441 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1442 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1443 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1444 key = VTERM_KEY_RIGHT; break;
1445 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1446 key = VTERM_KEY_RIGHT; break;
1447 case K_UP: key = VTERM_KEY_UP; break;
1448 case K_S_UP: mod = VTERM_MOD_SHIFT;
1449 key = VTERM_KEY_UP; break;
1450 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001451 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1452 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001453
Bram Moolenaara42ad572017-11-16 13:08:04 +01001454 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1455 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001456 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1457 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001458
1459 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001460 case K_LEFTMOUSE_NM:
1461 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001462 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001463 case K_LEFTRELEASE_NM:
1464 case K_MOUSEMOVE:
1465 case K_MIDDLEMOUSE:
1466 case K_MIDDLEDRAG:
1467 case K_MIDDLERELEASE:
1468 case K_RIGHTMOUSE:
1469 case K_RIGHTDRAG:
1470 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1471 return 0;
1472 other = TRUE;
1473 break;
1474
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001475 case K_X1MOUSE: /* TODO */ return 0;
1476 case K_X1DRAG: /* TODO */ return 0;
1477 case K_X1RELEASE: /* TODO */ return 0;
1478 case K_X2MOUSE: /* TODO */ return 0;
1479 case K_X2DRAG: /* TODO */ return 0;
1480 case K_X2RELEASE: /* TODO */ return 0;
1481
1482 case K_IGNORE: return 0;
1483 case K_NOP: return 0;
1484 case K_UNDO: return 0;
1485 case K_HELP: return 0;
1486 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1487 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1488 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1489 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1490 case K_SELECT: return 0;
1491#ifdef FEAT_GUI
1492 case K_VER_SCROLLBAR: return 0;
1493 case K_HOR_SCROLLBAR: return 0;
1494#endif
1495#ifdef FEAT_GUI_TABLINE
1496 case K_TABLINE: return 0;
1497 case K_TABMENU: return 0;
1498#endif
1499#ifdef FEAT_NETBEANS_INTG
1500 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1501#endif
1502#ifdef FEAT_DND
1503 case K_DROP: return 0;
1504#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001505 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001506 case K_PS: vterm_keyboard_start_paste(vterm);
1507 other = TRUE;
1508 break;
1509 case K_PE: vterm_keyboard_end_paste(vterm);
1510 other = TRUE;
1511 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001512 }
1513
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001514 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001515 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001516 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001517 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001518 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001519 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001520 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001521
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001522 /*
1523 * Convert special keys to vterm keys:
1524 * - Write keys to vterm: vterm_keyboard_key()
1525 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001526 */
1527 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001528 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001529 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001530 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001531 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001532 vterm_keyboard_unichar(vterm, c, mod);
1533
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001534 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001535 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1536}
1537
1538/*
1539 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001540 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001541 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001542 */
1543 static int
1544term_job_running_check(term_T *term, int check_job_status)
1545{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001546 // Also consider the job finished when the channel is closed, to avoid a
1547 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001548 if (term != NULL
1549 && term->tl_job != NULL
1550 && channel_is_open(term->tl_job->jv_channel))
1551 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001552 job_T *job = term->tl_job;
1553
1554 // Careful: Checking the job status may invoked callbacks, which close
1555 // the buffer and terminate "term". However, "job" will not be freed
1556 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001557 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001558 job_status(job);
1559 return (job->jv_status == JOB_STARTED
1560 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001561 }
1562 return FALSE;
1563}
1564
1565/*
1566 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001567 */
1568 int
1569term_job_running(term_T *term)
1570{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001571 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001572}
1573
1574/*
1575 * Return TRUE if "term" has an active channel and used ":term NONE".
1576 */
1577 int
1578term_none_open(term_T *term)
1579{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001580 // Also consider the job finished when the channel is closed, to avoid a
1581 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001582 return term != NULL
1583 && term->tl_job != NULL
1584 && channel_is_open(term->tl_job->jv_channel)
1585 && term->tl_job->jv_channel->ch_keep_open;
1586}
1587
1588/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001589 * Used when exiting: kill the job in "buf" if so desired.
1590 * Return OK when the job finished.
1591 * Return FAIL when the job is still running.
1592 */
1593 int
1594term_try_stop_job(buf_T *buf)
1595{
1596 int count;
1597 char *how = (char *)buf->b_term->tl_kill;
1598
1599#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001600 if ((how == NULL || *how == NUL)
1601 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001602 {
1603 char_u buff[DIALOG_MSG_SIZE];
1604 int ret;
1605
Bram Moolenaar00806bc2020-11-05 19:36:38 +01001606 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001607 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1608 if (ret == VIM_YES)
1609 how = "kill";
1610 else if (ret == VIM_CANCEL)
1611 return FAIL;
1612 }
1613#endif
1614 if (how == NULL || *how == NUL)
1615 return FAIL;
1616
1617 job_stop(buf->b_term->tl_job, NULL, how);
1618
Bram Moolenaar9172d232019-01-29 23:06:54 +01001619 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001620 for (count = 0; count < 100; ++count)
1621 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001622 job_T *job;
1623
1624 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001625 if (!buf_valid(buf)
1626 || buf->b_term == NULL
1627 || buf->b_term->tl_job == NULL)
1628 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001629 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001630
Bram Moolenaar9172d232019-01-29 23:06:54 +01001631 // Call job_status() to update jv_status. It may cause the job to be
1632 // cleaned up but it won't be freed.
1633 job_status(job);
1634 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001635 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001636
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001637 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001638 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001639 }
1640 return FAIL;
1641}
1642
1643/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001644 * Add the last line of the scrollback buffer to the buffer in the window.
1645 */
1646 static void
1647add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1648{
1649 buf_T *buf = term->tl_buffer;
1650 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1651 linenr_T lnum = buf->b_ml.ml_line_count;
1652
Bram Moolenaar4f974752019-02-17 17:44:42 +01001653#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001654 if (!enc_utf8 && enc_codepage > 0)
1655 {
1656 WCHAR *ret = NULL;
1657 int length = 0;
1658
1659 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1660 &ret, &length);
1661 if (ret != NULL)
1662 {
1663 WideCharToMultiByte_alloc(enc_codepage, 0,
1664 ret, length, (char **)&text, &len, 0, 0);
1665 vim_free(ret);
1666 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1667 vim_free(text);
1668 }
1669 }
1670 else
1671#endif
1672 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1673 if (empty)
1674 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001675 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001676 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001677 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001678 curbuf = curwin->w_buffer;
1679 }
1680}
1681
1682 static void
1683cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1684{
1685 attr->width = cell->width;
1686 attr->attrs = cell->attrs;
1687 attr->fg = cell->fg;
1688 attr->bg = cell->bg;
1689}
1690
1691 static int
1692equal_celattr(cellattr_T *a, cellattr_T *b)
1693{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001694 // We only compare the RGB colors, ignoring the ANSI index and type.
1695 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001696 return a->fg.red == b->fg.red
1697 && a->fg.green == b->fg.green
1698 && a->fg.blue == b->fg.blue
1699 && a->bg.red == b->bg.red
1700 && a->bg.green == b->bg.green
1701 && a->bg.blue == b->bg.blue;
1702}
1703
Bram Moolenaard96ff162018-02-18 22:13:29 +01001704/*
1705 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1706 * line at this position. Otherwise at the end.
1707 */
1708 static int
1709add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1710{
1711 if (ga_grow(&term->tl_scrollback, 1) == OK)
1712 {
1713 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1714 + term->tl_scrollback.ga_len;
1715
1716 if (lnum > 0)
1717 {
1718 int i;
1719
1720 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1721 {
1722 *line = *(line - 1);
1723 --line;
1724 }
1725 }
1726 line->sb_cols = 0;
1727 line->sb_cells = NULL;
1728 line->sb_fill_attr = *fill_attr;
1729 ++term->tl_scrollback.ga_len;
1730 return OK;
1731 }
1732 return FALSE;
1733}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001734
1735/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001736 * Remove the terminal contents from the scrollback and the buffer.
1737 * Used before adding a new scrollback line or updating the buffer for lines
1738 * displayed in the terminal.
1739 */
1740 static void
1741cleanup_scrollback(term_T *term)
1742{
1743 sb_line_T *line;
1744 garray_T *gap;
1745
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001746 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001747 gap = &term->tl_scrollback;
1748 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1749 && gap->ga_len > 0)
1750 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001751 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001752 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1753 vim_free(line->sb_cells);
1754 --gap->ga_len;
1755 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001756 curbuf = curwin->w_buffer;
1757 if (curbuf == term->tl_buffer)
1758 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001759}
1760
1761/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001762 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001763 */
1764 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001765update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001766{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001767 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001768 int len;
1769 int lines_skipped = 0;
1770 VTermPos pos;
1771 VTermScreenCell cell;
1772 cellattr_T fill_attr, new_fill_attr;
1773 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001774
1775 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1776 "Adding terminal window snapshot to buffer");
1777
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001778 // First remove the lines that were appended before, they might be
1779 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001780 cleanup_scrollback(term);
1781
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001782 screen = vterm_obtain_screen(term->tl_vterm);
1783 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001784 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1785 {
1786 len = 0;
1787 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1788 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1789 && cell.chars[0] != NUL)
1790 {
1791 len = pos.col + 1;
1792 new_fill_attr = term->tl_default_color;
1793 }
1794 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001795 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001796 cell2cellattr(&cell, &new_fill_attr);
1797
1798 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1799 ++lines_skipped;
1800 else
1801 {
1802 while (lines_skipped > 0)
1803 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001804 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001805 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001806 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001807 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001808 }
1809
1810 if (len == 0)
1811 p = NULL;
1812 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001813 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001814 if ((p != NULL || len == 0)
1815 && ga_grow(&term->tl_scrollback, 1) == OK)
1816 {
1817 garray_T ga;
1818 int width;
1819 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1820 + term->tl_scrollback.ga_len;
1821
1822 ga_init2(&ga, 1, 100);
1823 for (pos.col = 0; pos.col < len; pos.col += width)
1824 {
1825 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1826 {
1827 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001828 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001829 if (ga_grow(&ga, 1) == OK)
1830 ga.ga_len += utf_char2bytes(' ',
1831 (char_u *)ga.ga_data + ga.ga_len);
1832 }
1833 else
1834 {
1835 width = cell.width;
1836
1837 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001838 if (width == 2)
1839 // second cell of double-width character has the
1840 // same attributes.
1841 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001842
Bram Moolenaara79fd562018-12-20 20:47:32 +01001843 // Each character can be up to 6 bytes.
1844 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001845 {
1846 int i;
1847 int c;
1848
1849 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1850 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1851 (char_u *)ga.ga_data + ga.ga_len);
1852 }
1853 }
1854 }
1855 line->sb_cols = len;
1856 line->sb_cells = p;
1857 line->sb_fill_attr = new_fill_attr;
1858 fill_attr = new_fill_attr;
1859 ++term->tl_scrollback.ga_len;
1860
1861 if (ga_grow(&ga, 1) == FAIL)
1862 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1863 else
1864 {
1865 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1866 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1867 }
1868 ga_clear(&ga);
1869 }
1870 else
1871 vim_free(p);
1872 }
1873 }
1874
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001875 // Add trailing empty lines.
1876 for (pos.row = term->tl_scrollback.ga_len;
1877 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1878 ++pos.row)
1879 {
1880 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1881 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1882 }
1883
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001884 term->tl_dirty_snapshot = FALSE;
1885#ifdef FEAT_TIMERS
1886 term->tl_timer_set = FALSE;
1887#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001888}
1889
1890/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001891 * Loop over all windows in the current tab, and also curwin, which is not
1892 * encountered when using a terminal in a popup window.
1893 * Return TRUE if "*wp" was set to the next window.
1894 */
1895 static int
1896for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1897{
1898 if (*wp == NULL)
1899 *wp = firstwin;
1900 else if ((*wp)->w_next != NULL)
1901 *wp = (*wp)->w_next;
1902 else if (!*did_curwin)
1903 *wp = curwin;
1904 else
1905 return FALSE;
1906 if (*wp == curwin)
1907 *did_curwin = TRUE;
1908 return TRUE;
1909}
1910
1911/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001912 * If needed, add the current lines of the terminal to scrollback and to the
1913 * buffer. Called after the job has ended and when switching to
1914 * Terminal-Normal mode.
1915 * When "redraw" is TRUE redraw the windows that show the terminal.
1916 */
1917 static void
1918may_move_terminal_to_buffer(term_T *term, int redraw)
1919{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001920 if (term->tl_vterm == NULL)
1921 return;
1922
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001923 // Update the snapshot only if something changes or the buffer does not
1924 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001925 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1926 <= term->tl_scrollback_scrolled)
1927 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001928
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001929 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001930 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1931 &term->tl_default_color.fg, &term->tl_default_color.bg);
1932
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001933 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001934 {
1935 win_T *wp = NULL;
1936 int did_curwin = FALSE;
1937
1938 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001939 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001940 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001941 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001942 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1943 wp->w_cursor.col = 0;
1944 wp->w_valid = 0;
1945 if (wp->w_cursor.lnum >= wp->w_height)
1946 {
1947 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001948
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001949 if (wp->w_topline < min_topline)
1950 wp->w_topline = min_topline;
1951 }
1952 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001953 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001954 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001955 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001956}
1957
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001958#if defined(FEAT_TIMERS) || defined(PROTO)
1959/*
1960 * Check if any terminal timer expired. If so, copy text from the terminal to
1961 * the buffer.
1962 * Return the time until the next timer will expire.
1963 */
1964 int
1965term_check_timers(int next_due_arg, proftime_T *now)
1966{
1967 term_T *term;
1968 int next_due = next_due_arg;
1969
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001970 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001971 {
1972 if (term->tl_timer_set && !term->tl_normal_mode)
1973 {
1974 long this_due = proftime_time_left(&term->tl_timer_due, now);
1975
1976 if (this_due <= 1)
1977 {
1978 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001979 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001980 }
1981 else if (next_due == -1 || next_due > this_due)
1982 next_due = this_due;
1983 }
1984 }
1985
1986 return next_due;
1987}
1988#endif
1989
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001990/*
1991 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
1992 * otherwise end it.
1993 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001994 static void
1995set_terminal_mode(term_T *term, int normal_mode)
1996{
1997 term->tl_normal_mode = normal_mode;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001998 trigger_modechanged();
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001999 if (!normal_mode)
2000 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002001 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002002 if (term->tl_buffer == curbuf)
2003 maketitle();
2004}
2005
2006/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002007 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002008 * Move the vterm contents into the scrollback buffer and free the vterm.
2009 */
2010 static void
2011cleanup_vterm(term_T *term)
2012{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002013 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002014 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002015 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002016 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002017}
2018
2019/*
2020 * Switch from Terminal-Job mode to Terminal-Normal mode.
2021 * Suspends updating the terminal window.
2022 */
2023 static void
2024term_enter_normal_mode(void)
2025{
2026 term_T *term = curbuf->b_term;
2027
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002028 set_terminal_mode(term, TRUE);
2029
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002030 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002031 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002032
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002033 // Move the window cursor to the position of the cursor in the
2034 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002035 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2036 + term->tl_cursor_pos.row + 1;
2037 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002038 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2039 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002040 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002041
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002042 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002043 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2044}
2045
2046/*
2047 * Returns TRUE if the current window contains a terminal and we are in
2048 * Terminal-Normal mode.
2049 */
2050 int
2051term_in_normal_mode(void)
2052{
2053 term_T *term = curbuf->b_term;
2054
2055 return term != NULL && term->tl_normal_mode;
2056}
2057
2058/*
2059 * Switch from Terminal-Normal mode to Terminal-Job mode.
2060 * Restores updating the terminal window.
2061 */
2062 void
2063term_enter_job_mode()
2064{
2065 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002066
2067 set_terminal_mode(term, FALSE);
2068
2069 if (term->tl_channel_closed)
2070 cleanup_vterm(term);
2071 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002072#ifdef FEAT_PROP_POPUP
2073 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002074 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002075#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002076}
2077
2078/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002079 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002080 * Note: while waiting a terminal may be closed and freed if the channel is
2081 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002082 */
2083 static int
2084term_vgetc()
2085{
2086 int c;
2087 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002088 int modify_other_keys =
2089 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002090
2091 State = TERMINAL;
2092 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002093#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002094 ctrl_break_was_pressed = FALSE;
2095#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002096 if (modify_other_keys)
2097 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002098 c = vgetc();
2099 got_int = FALSE;
2100 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002101 if (modify_other_keys)
2102 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002103 return c;
2104}
2105
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002106static int mouse_was_outside = FALSE;
2107
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002108/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002109 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002110 * Return FAIL when the key needs to be handled in Normal mode.
2111 * Return OK when the key was dropped or sent to the terminal.
2112 */
2113 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002114send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002115{
2116 char msg[KEY_BUF_LEN];
2117 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002118 int dragging_outside = FALSE;
2119
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002120 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002121 switch (c)
2122 {
2123 case NUL:
2124 case K_ZERO:
2125 if (typed)
2126 stuffcharReadbuff(c);
2127 return FAIL;
2128
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002129 case K_TABLINE:
2130 stuffcharReadbuff(c);
2131 return FAIL;
2132
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002133 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002134 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002135 return FAIL;
2136
2137 case K_LEFTDRAG:
2138 case K_MIDDLEDRAG:
2139 case K_RIGHTDRAG:
2140 case K_X1DRAG:
2141 case K_X2DRAG:
2142 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002143 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002144 case K_LEFTMOUSE:
2145 case K_LEFTMOUSE_NM:
2146 case K_LEFTRELEASE:
2147 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002148 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002149 case K_MIDDLEMOUSE:
2150 case K_MIDDLERELEASE:
2151 case K_RIGHTMOUSE:
2152 case K_RIGHTRELEASE:
2153 case K_X1MOUSE:
2154 case K_X1RELEASE:
2155 case K_X2MOUSE:
2156 case K_X2RELEASE:
2157
2158 case K_MOUSEUP:
2159 case K_MOUSEDOWN:
2160 case K_MOUSELEFT:
2161 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002162 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002163 int row = mouse_row;
2164 int col = mouse_col;
2165
2166#ifdef FEAT_PROP_POPUP
2167 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002168 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002169 row -= popup_top_extra(curwin);
2170 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002171 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002172#endif
2173 if (row < W_WINROW(curwin)
2174 || row >= (W_WINROW(curwin) + curwin->w_height)
2175 || col < curwin->w_wincol
2176 || col >= W_ENDCOL(curwin)
2177 || dragging_outside)
2178 {
2179 // click or scroll outside the current window or on status
2180 // line or vertical separator
2181 if (typed)
2182 {
2183 stuffcharReadbuff(c);
2184 mouse_was_outside = TRUE;
2185 }
2186 return FAIL;
2187 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002188 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002189 break;
2190
2191 case K_COMMAND:
2192 return do_cmdline(NULL, getcmdkeycmd, NULL, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002193 }
2194 if (typed)
2195 mouse_was_outside = FALSE;
2196
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002197 // Convert the typed key to a sequence of bytes for the job.
2198 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002199 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002200 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002201 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2202 (char_u *)msg, (int)len, NULL);
2203
2204 return OK;
2205}
2206
2207 static void
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002208position_cursor(win_T *wp, VTermPos *pos)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002209{
2210 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2211 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002212#ifdef FEAT_PROP_POPUP
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002213 if (popup_is_popup(wp))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002214 {
Bram Moolenaarf5452692020-11-28 21:56:06 +01002215 wp->w_wrow += popup_top_extra(wp);
2216 wp->w_wcol += popup_left_extra(wp);
Bram Moolenaar6a076442020-11-15 20:32:58 +01002217 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002218 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01002219 else
2220 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002221#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002222 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2223}
2224
2225/*
2226 * Handle CTRL-W "": send register contents to the job.
2227 */
2228 static void
2229term_paste_register(int prev_c UNUSED)
2230{
2231 int c;
2232 list_T *l;
2233 listitem_T *item;
2234 long reglen = 0;
2235 int type;
2236
2237#ifdef FEAT_CMDL_INFO
2238 if (add_to_showcmd(prev_c))
2239 if (add_to_showcmd('"'))
2240 out_flush();
2241#endif
2242 c = term_vgetc();
2243#ifdef FEAT_CMDL_INFO
2244 clear_showcmd();
2245#endif
2246 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002247 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002248 return;
2249
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002250 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002251 if (c == '=' && get_expr_register() != '=')
2252 return;
2253 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002254 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002255 return;
2256
2257 l = (list_T *)get_reg_contents(c, GREG_LIST);
2258 if (l != NULL)
2259 {
2260 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002261 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002262 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002263 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002264#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002265 char_u *tmp = s;
2266
2267 if (!enc_utf8 && enc_codepage > 0)
2268 {
2269 WCHAR *ret = NULL;
2270 int length = 0;
2271
2272 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2273 (int)STRLEN(s), &ret, &length);
2274 if (ret != NULL)
2275 {
2276 WideCharToMultiByte_alloc(CP_UTF8, 0,
2277 ret, length, (char **)&s, &length, 0, 0);
2278 vim_free(ret);
2279 }
2280 }
2281#endif
2282 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2283 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002284#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002285 if (tmp != s)
2286 vim_free(s);
2287#endif
2288
2289 if (item->li_next != NULL || type == MLINE)
2290 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2291 (char_u *)"\r", 1, NULL);
2292 }
2293 list_free(l);
2294 }
2295}
2296
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002297/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002298 * Return TRUE when waiting for a character in the terminal, the cursor of the
2299 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002300 */
2301 int
2302terminal_is_active()
2303{
2304 return in_terminal_loop != NULL;
2305}
2306
Bram Moolenaar83d47902020-03-26 20:34:00 +01002307/*
2308 * Return the highight group name for the terminal; "Terminal" if not set.
2309 */
2310 static char_u *
2311term_get_highlight_name(term_T *term)
2312{
2313 if (term->tl_highlight_name == NULL)
2314 return (char_u *)"Terminal";
2315 return term->tl_highlight_name;
2316}
2317
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002318#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002319 cursorentry_T *
2320term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2321{
2322 term_T *term = in_terminal_loop;
2323 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002324 int id;
2325 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002326
Bram Moolenaara80faa82020-04-12 19:37:17 +02002327 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002328 entry.shape = entry.mshape =
2329 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2330 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2331 SHAPE_BLOCK;
2332 entry.percentage = 20;
2333 if (term->tl_cursor_blink)
2334 {
2335 entry.blinkwait = 700;
2336 entry.blinkon = 400;
2337 entry.blinkoff = 250;
2338 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002339
Bram Moolenaar83d47902020-03-26 20:34:00 +01002340 // The highlight group overrules the defaults.
2341 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002342 if (id != 0)
2343 {
2344 syn_id2colors(id, &term_fg, &term_bg);
2345 *fg = term_bg;
2346 }
2347 else
2348 *fg = gui.back_pixel;
2349
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002350 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002351 {
2352 if (id != 0)
2353 *bg = term_fg;
2354 else
2355 *bg = gui.norm_pixel;
2356 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002357 else
2358 *bg = color_name2handle(term->tl_cursor_color);
2359 entry.name = "n";
2360 entry.used_for = SHAPE_CURSOR;
2361
2362 return &entry;
2363}
2364#endif
2365
Bram Moolenaard317b382018-02-08 22:33:31 +01002366 static void
2367may_output_cursor_props(void)
2368{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002369 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002370 || last_set_cursor_shape != desired_cursor_shape
2371 || last_set_cursor_blink != desired_cursor_blink)
2372 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002373 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002374 last_set_cursor_shape = desired_cursor_shape;
2375 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002376 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002377 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002378 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002379 ui_cursor_shape_forced(TRUE);
2380 else
2381 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2382 }
2383}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002384
Bram Moolenaard317b382018-02-08 22:33:31 +01002385/*
2386 * Set the cursor color and shape, if not last set to these.
2387 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002388 static void
2389may_set_cursor_props(term_T *term)
2390{
2391#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002392 // For the GUI the cursor properties are obtained with
2393 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002394 if (gui.in_use)
2395 return;
2396#endif
2397 if (in_terminal_loop == term)
2398 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002399 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002400 desired_cursor_shape = term->tl_cursor_shape;
2401 desired_cursor_blink = term->tl_cursor_blink;
2402 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002403 }
2404}
2405
Bram Moolenaard317b382018-02-08 22:33:31 +01002406/*
2407 * Reset the desired cursor properties and restore them when needed.
2408 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002409 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002410prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002411{
2412#ifdef FEAT_GUI
2413 if (gui.in_use)
2414 return;
2415#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002416 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002417 desired_cursor_shape = -1;
2418 desired_cursor_blink = -1;
2419 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002420}
2421
2422/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002423 * Returns TRUE if the current window contains a terminal and we are sending
2424 * keys to the job.
2425 * If "check_job_status" is TRUE update the job status.
2426 */
2427 static int
2428term_use_loop_check(int check_job_status)
2429{
2430 term_T *term = curbuf->b_term;
2431
2432 return term != NULL
2433 && !term->tl_normal_mode
2434 && term->tl_vterm != NULL
2435 && term_job_running_check(term, check_job_status);
2436}
2437
2438/*
2439 * Returns TRUE if the current window contains a terminal and we are sending
2440 * keys to the job.
2441 */
2442 int
2443term_use_loop(void)
2444{
2445 return term_use_loop_check(FALSE);
2446}
2447
2448/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002449 * Called when entering a window with the mouse. If this is a terminal window
2450 * we may want to change state.
2451 */
2452 void
2453term_win_entered()
2454{
2455 term_T *term = curbuf->b_term;
2456
2457 if (term != NULL)
2458 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002459 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002460 {
2461 reset_VIsual_and_resel();
2462 if (State & INSERT)
2463 stop_insert_mode = TRUE;
2464 }
2465 mouse_was_outside = FALSE;
2466 enter_mouse_col = mouse_col;
2467 enter_mouse_row = mouse_row;
2468 }
2469}
2470
2471/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002472 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2473 * Return the Ctrl-key value in that case.
2474 */
2475 static int
2476raw_c_to_ctrl(int c)
2477{
2478 if ((mod_mask & MOD_MASK_CTRL)
2479 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2480 return c & 0x1f;
2481 return c;
2482}
2483
2484/*
2485 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2486 * May set "mod_mask".
2487 */
2488 static int
2489ctrl_to_raw_c(int c)
2490{
2491 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2492 {
2493 mod_mask |= MOD_MASK_CTRL;
2494 return c + '@';
2495 }
2496 return c;
2497}
2498
2499/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002500 * Wait for input and send it to the job.
2501 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2502 * when there is no more typahead.
2503 * Return when the start of a CTRL-W command is typed or anything else that
2504 * should be handled as a Normal mode command.
2505 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2506 * the terminal was closed.
2507 */
2508 int
2509terminal_loop(int blocking)
2510{
2511 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002512 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002513 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002514 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002515#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002516 int tty_fd = curbuf->b_term->tl_job->jv_channel
2517 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002518#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002519 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002520
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002521 // Remember the terminal we are sending keys to. However, the terminal
2522 // might be closed while waiting for a character, e.g. typing "exit" in a
2523 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2524 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002525 in_terminal_loop = curbuf->b_term;
2526
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002527 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002528 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002529 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002530 if (termwinkey == Ctrl_W)
2531 termwinkey = 0;
2532 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002533 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002534 may_set_cursor_props(curbuf->b_term);
2535
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002536 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002537 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002538#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002539 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002540#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002541 // TODO: skip screen update when handling a sequence of keys.
2542 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002543 while (must_redraw != 0)
2544 if (update_screen(0) == FAIL)
2545 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002546 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002547 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002548 break;
2549
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002550 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002551 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002552
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002553 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002554 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002555 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002556 // Job finished while waiting for a character. Push back the
2557 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002558 if (raw_c != K_IGNORE)
2559 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002560 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002561 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002562 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002563 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002564 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002565
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002566#ifdef UNIX
2567 /*
2568 * The shell or another program may change the tty settings. Getting
2569 * them for every typed character is a bit of overhead, but it's needed
2570 * for the first character typed, e.g. when Vim starts in a shell.
2571 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002572 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002573 {
2574 ttyinfo_T info;
2575
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002576 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002577 if (get_tty_info(tty_fd, &info) == OK)
2578 term_backspace_char = info.backspace;
2579 }
2580#endif
2581
Bram Moolenaar4f974752019-02-17 17:44:42 +01002582#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002583 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2584 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002585 if (ctrl_break_was_pressed)
2586 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2587#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002588 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2589 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002590 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002591#ifdef FEAT_GUI
2592 && !curbuf->b_term->tl_system
2593#endif
2594 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002595 {
2596 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002597 int prev_raw_c = raw_c;
2598 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002599
2600#ifdef FEAT_CMDL_INFO
2601 if (add_to_showcmd(c))
2602 out_flush();
2603#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002604 raw_c = term_vgetc();
2605 c = raw_c_to_ctrl(raw_c);
2606
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002607#ifdef FEAT_CMDL_INFO
2608 clear_showcmd();
2609#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002610 if (!term_use_loop_check(TRUE)
2611 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002612 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002613 break;
2614
2615 if (prev_c == Ctrl_BSL)
2616 {
2617 if (c == Ctrl_N)
2618 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002619 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002620 term_enter_normal_mode();
2621 ret = FAIL;
2622 goto theend;
2623 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002624 // Send both keys to the terminal, first one here, second one
2625 // below.
2626 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2627 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002628 }
2629 else if (c == Ctrl_C)
2630 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002631 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002632 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2633 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002634 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002635 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002636 // "CTRL-W .": send CTRL-W to the job
2637 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002638 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002639 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002640 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002641 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002642 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002643 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002644 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002645 else if (c == 'N')
2646 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002647 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002648 term_enter_normal_mode();
2649 ret = FAIL;
2650 goto theend;
2651 }
2652 else if (c == '"')
2653 {
2654 term_paste_register(prev_c);
2655 continue;
2656 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002657 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002658 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002659 // space for CTRL-W, modifier, multi-byte char and NUL
2660 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002661
2662 // Put the command into the typeahead buffer, when using the
2663 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2664 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002665 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002666 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002667 ret = OK;
2668 goto theend;
2669 }
2670 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002671# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002672 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002673 {
2674 WCHAR wc;
2675 char_u mb[3];
2676
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002677 mb[0] = (unsigned)raw_c >> 8;
2678 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002679 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002680 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002681 }
2682# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002683 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002684 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002685 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002686 // We are sure to come back here, don't reset the cursor color
2687 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002688 restore_cursor = FALSE;
2689
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002690 ret = OK;
2691 goto theend;
2692 }
2693 }
2694 ret = FAIL;
2695
2696theend:
2697 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002698 if (restore_cursor)
2699 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002700
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002701 // Move a snapshot of the screen contents to the buffer, so that completion
2702 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002703 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2704 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002705
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002706 return ret;
2707}
2708
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002709 static void
2710may_toggle_cursor(term_T *term)
2711{
2712 if (in_terminal_loop == term)
2713 {
2714 if (term->tl_cursor_visible)
2715 cursor_on();
2716 else
2717 cursor_off();
2718 }
2719}
2720
2721/*
2722 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002723 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002724 */
2725 static int
2726color2index(VTermColor *color, int fg, int *boldp)
2727{
2728 int red = color->red;
2729 int blue = color->blue;
2730 int green = color->green;
2731
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002732 if (VTERM_COLOR_IS_DEFAULT_FG(color)
2733 || VTERM_COLOR_IS_DEFAULT_BG(color))
2734 return 0;
2735 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002736 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002737 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002738 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002739 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002740 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002741 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2742 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2743 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002744 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002745 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2746 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2747 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2748 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2749 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2750 case 10: return lookup_color(20, fg, boldp) + 1; // red
2751 case 11: return lookup_color(16, fg, boldp) + 1; // green
2752 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2753 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2754 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2755 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2756 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002757 }
2758 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002759
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002760 if (t_colors >= 256)
2761 {
2762 if (red == blue && red == green)
2763 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002764 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002765 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002766 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2767 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2768 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002769 int i;
2770
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002771 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002772 return 17; // 00/00/00
2773 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002774 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002775 for (i = 0; i < 23; ++i)
2776 if (red < cutoff[i])
2777 return i + 233;
2778 return 256;
2779 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002780 {
2781 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2782 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002783
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002784 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002785 for (ri = 0; ri < 5; ++ri)
2786 if (red < cutoff[ri])
2787 break;
2788 for (gi = 0; gi < 5; ++gi)
2789 if (green < cutoff[gi])
2790 break;
2791 for (bi = 0; bi < 5; ++bi)
2792 if (blue < cutoff[bi])
2793 break;
2794 return 17 + ri * 36 + gi * 6 + bi;
2795 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002796 }
2797 return 0;
2798}
2799
2800/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002801 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002802 */
2803 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002804vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002805{
2806 int attr = 0;
2807
2808 if (cellattrs.bold)
2809 attr |= HL_BOLD;
2810 if (cellattrs.underline)
2811 attr |= HL_UNDERLINE;
2812 if (cellattrs.italic)
2813 attr |= HL_ITALIC;
2814 if (cellattrs.strike)
2815 attr |= HL_STRIKETHROUGH;
2816 if (cellattrs.reverse)
2817 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002818 return attr;
2819}
2820
2821/*
2822 * Store Vterm attributes in "cell" from highlight flags.
2823 */
2824 static void
2825hl2vtermAttr(int attr, cellattr_T *cell)
2826{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002827 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002828 if (attr & HL_BOLD)
2829 cell->attrs.bold = 1;
2830 if (attr & HL_UNDERLINE)
2831 cell->attrs.underline = 1;
2832 if (attr & HL_ITALIC)
2833 cell->attrs.italic = 1;
2834 if (attr & HL_STRIKETHROUGH)
2835 cell->attrs.strike = 1;
2836 if (attr & HL_INVERSE)
2837 cell->attrs.reverse = 1;
2838}
2839
2840/*
2841 * Convert the attributes of a vterm cell into an attribute index.
2842 */
2843 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002844cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002845 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002846 win_T *wp,
2847 VTermScreenCellAttrs cellattrs,
2848 VTermColor cellfg,
2849 VTermColor cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002850{
2851 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002852
2853#ifdef FEAT_GUI
2854 if (gui.in_use)
2855 {
2856 guicolor_T fg, bg;
2857
2858 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2859 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2860 return get_gui_attr_idx(attr, fg, bg);
2861 }
2862 else
2863#endif
2864#ifdef FEAT_TERMGUICOLORS
2865 if (p_tgc)
2866 {
Milly7b5f45b2021-10-15 22:25:43 +01002867 guicolor_T fg = INVALCOLOR;
2868 guicolor_T bg = INVALCOLOR;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002869
Milly7b5f45b2021-10-15 22:25:43 +01002870 // Use the 'wincolor' or "Terminal" highlighting for the default
2871 // colors.
2872 if (VTERM_COLOR_IS_DEFAULT_FG(&cellfg)
2873 || VTERM_COLOR_IS_DEFAULT_BG(&cellbg))
2874 {
2875 int id = 0;
2876
2877 if (wp != NULL && *wp->w_p_wcr != NUL)
2878 id = syn_name2id(wp->w_p_wcr);
2879 if (id == 0)
2880 id = syn_name2id(term_get_highlight_name(term));
2881 if (id > 0)
2882 syn_id2colors(id, &fg, &bg);
2883 if (!VTERM_COLOR_IS_DEFAULT_FG(&cellfg))
2884 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green,
2885 cellfg.blue);
2886 if (!VTERM_COLOR_IS_DEFAULT_BG(&cellbg))
2887 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green,
2888 cellbg.blue);
2889 }
2890 else
2891 {
2892 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2893 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2894 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002895
2896 return get_tgc_attr_idx(attr, fg, bg);
2897 }
2898 else
2899#endif
2900 {
2901 int bold = MAYBE;
2902 int fg = color2index(&cellfg, TRUE, &bold);
2903 int bg = color2index(&cellbg, FALSE, &bold);
2904
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002905 // Use the 'wincolor' or "Terminal" highlighting for the default
2906 // colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002907 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002908 {
Milly7b5f45b2021-10-15 22:25:43 +01002909 int cterm_fg = -1;
2910 int cterm_bg = -1;
2911 int id = 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002912
2913 if (wp != NULL && *wp->w_p_wcr != NUL)
Milly7b5f45b2021-10-15 22:25:43 +01002914 id = syn_name2id(wp->w_p_wcr);
2915 if (id == 0)
2916 id = syn_name2id(term_get_highlight_name(term));
2917 if (id > 0)
2918 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
2919 if (fg == 0 && cterm_fg >= 0)
2920 fg = cterm_fg + 1;
2921 if (bg == 0 && cterm_bg >= 0)
2922 bg = cterm_bg + 1;
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002923 }
2924
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002925 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002926 if (bold == TRUE)
2927 attr |= HL_BOLD;
2928 return get_cterm_attr_idx(attr, fg, bg);
2929 }
2930 return 0;
2931}
2932
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002933 static void
2934set_dirty_snapshot(term_T *term)
2935{
2936 term->tl_dirty_snapshot = TRUE;
2937#ifdef FEAT_TIMERS
2938 if (!term->tl_normal_mode)
2939 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002940 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002941 profile_setlimit(100L, &term->tl_timer_due);
2942 term->tl_timer_set = TRUE;
2943 }
2944#endif
2945}
2946
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002947 static int
2948handle_damage(VTermRect rect, void *user)
2949{
2950 term_T *term = (term_T *)user;
2951
2952 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2953 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002954 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002955 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002956 return 1;
2957}
2958
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002959 static void
2960term_scroll_up(term_T *term, int start_row, int count)
2961{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002962 win_T *wp = NULL;
2963 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002964 VTermColor fg, bg;
2965 VTermScreenCellAttrs attr;
2966 int clear_attr;
2967
Bram Moolenaara80faa82020-04-12 19:37:17 +02002968 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002969
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002970 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002971 {
2972 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002973 {
2974 // Set the color to clear lines with.
2975 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2976 &fg, &bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002977 clear_attr = cell2attr(term, wp, attr, fg, bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002978 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002979 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002980 }
2981}
2982
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002983 static int
2984handle_moverect(VTermRect dest, VTermRect src, void *user)
2985{
2986 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002987 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002988
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002989 // Scrolling up is done much more efficiently by deleting lines instead of
2990 // redrawing the text. But avoid doing this multiple times, postpone until
2991 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002992 if (dest.start_col == src.start_col
2993 && dest.end_col == src.end_col
2994 && dest.start_row < src.start_row)
2995 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002996 if (dest.start_row == 0)
2997 term->tl_postponed_scroll += count;
2998 else
2999 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003000 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003001
3002 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3003 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003004 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003005
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003006 // Note sure if the scrolling will work correctly, let's do a complete
3007 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003008 redraw_buf_later(term->tl_buffer, NOT_VALID);
3009 return 1;
3010}
3011
3012 static int
3013handle_movecursor(
3014 VTermPos pos,
3015 VTermPos oldpos UNUSED,
3016 int visible,
3017 void *user)
3018{
3019 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003020 win_T *wp = NULL;
3021 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003022
3023 term->tl_cursor_pos = pos;
3024 term->tl_cursor_visible = visible;
3025
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003026 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003027 {
3028 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003029 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003030 }
3031 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003032 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003033
3034 return 1;
3035}
3036
3037 static int
3038handle_settermprop(
3039 VTermProp prop,
3040 VTermValue *value,
3041 void *user)
3042{
3043 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003044 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003045
3046 switch (prop)
3047 {
3048 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003049 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003050 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003051 if (strval == NULL)
3052 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003053 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003054 // a blank title isn't useful, make it empty, so that "running" is
3055 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003056 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003057 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003058 // Same as blank
3059 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003060 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003061 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3062 term->tl_title = NULL;
3063 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003064 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003065 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003066#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003067 else if (!enc_utf8 && enc_codepage > 0)
3068 {
3069 WCHAR *ret = NULL;
3070 int length = 0;
3071
3072 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003073 (char*)value->string.str,
3074 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003075 if (ret != NULL)
3076 {
3077 WideCharToMultiByte_alloc(enc_codepage, 0,
3078 ret, length, (char**)&term->tl_title,
3079 &length, 0, 0);
3080 vim_free(ret);
3081 }
3082 }
3083#endif
3084 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003085 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003086 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003087 strval = NULL;
3088 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003089 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003090 if (term == curbuf->b_term)
3091 maketitle();
3092 break;
3093
3094 case VTERM_PROP_CURSORVISIBLE:
3095 term->tl_cursor_visible = value->boolean;
3096 may_toggle_cursor(term);
3097 out_flush();
3098 break;
3099
3100 case VTERM_PROP_CURSORBLINK:
3101 term->tl_cursor_blink = value->boolean;
3102 may_set_cursor_props(term);
3103 break;
3104
3105 case VTERM_PROP_CURSORSHAPE:
3106 term->tl_cursor_shape = value->number;
3107 may_set_cursor_props(term);
3108 break;
3109
3110 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003111 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003112 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003113 if (strval == NULL)
3114 break;
3115 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003116 may_set_cursor_props(term);
3117 break;
3118
3119 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003120 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003121 term->tl_using_altscreen = value->boolean;
3122 break;
3123
3124 default:
3125 break;
3126 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003127 vim_free(strval);
3128
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003129 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003130 return 1;
3131}
3132
3133/*
3134 * The job running in the terminal resized the terminal.
3135 */
3136 static int
3137handle_resize(int rows, int cols, void *user)
3138{
3139 term_T *term = (term_T *)user;
3140 win_T *wp;
3141
3142 term->tl_rows = rows;
3143 term->tl_cols = cols;
3144 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003145 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003146 term->tl_vterm_size_changed = FALSE;
3147 else
3148 {
3149 FOR_ALL_WINDOWS(wp)
3150 {
3151 if (wp->w_buffer == term->tl_buffer)
3152 {
3153 win_setheight_win(rows, wp);
3154 win_setwidth_win(cols, wp);
3155 }
3156 }
3157 redraw_buf_later(term->tl_buffer, NOT_VALID);
3158 }
3159 return 1;
3160}
3161
3162/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003163 * If the number of lines that are stored goes over 'termscrollback' then
3164 * delete the first 10%.
3165 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3166 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003167 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003168 static void
3169limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003170{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003171 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003172 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003173 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003174 int i;
3175
3176 curbuf = term->tl_buffer;
3177 for (i = 0; i < todo; ++i)
3178 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003179 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3180 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003181 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003182 }
3183 curbuf = curwin->w_buffer;
3184
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003185 gap->ga_len -= todo;
3186 mch_memmove(gap->ga_data,
3187 (sb_line_T *)gap->ga_data + todo,
3188 sizeof(sb_line_T) * gap->ga_len);
3189 if (update_buffer)
3190 term->tl_scrollback_scrolled -= todo;
3191 }
3192}
3193
3194/*
3195 * Handle a line that is pushed off the top of the screen.
3196 */
3197 static int
3198handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3199{
3200 term_T *term = (term_T *)user;
3201 garray_T *gap;
3202 int update_buffer;
3203
3204 if (term->tl_normal_mode)
3205 {
3206 // In Terminal-Normal mode the user interacts with the buffer, thus we
3207 // must not change it. Postpone adding the scrollback lines.
3208 gap = &term->tl_scrollback_postponed;
3209 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003210 }
3211 else
3212 {
3213 // First remove the lines that were appended before, the pushed line
3214 // goes above it.
3215 cleanup_scrollback(term);
3216 gap = &term->tl_scrollback;
3217 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003218 }
3219
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003220 limit_scrollback(term, gap, update_buffer);
3221
3222 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003223 {
3224 cellattr_T *p = NULL;
3225 int len = 0;
3226 int i;
3227 int c;
3228 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003229 int text_len;
3230 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003231 sb_line_T *line;
3232 garray_T ga;
3233 cellattr_T fill_attr = term->tl_default_color;
3234
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003235 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003236 for (i = 0; i < cols; ++i)
3237 if (cells[i].chars[0] != 0)
3238 len = i + 1;
3239 else
3240 cell2cellattr(&cells[i], &fill_attr);
3241
3242 ga_init2(&ga, 1, 100);
3243 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003244 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003245 if (p != NULL)
3246 {
3247 for (col = 0; col < len; col += cells[col].width)
3248 {
3249 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3250 {
3251 ga.ga_len = 0;
3252 break;
3253 }
3254 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3255 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3256 (char_u *)ga.ga_data + ga.ga_len);
3257 cell2cellattr(&cells[col], &p[col]);
3258 }
3259 }
3260 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003261 {
3262 if (update_buffer)
3263 text = (char_u *)"";
3264 else
3265 text = vim_strsave((char_u *)"");
3266 text_len = 0;
3267 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003268 else
3269 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003270 text = ga.ga_data;
3271 text_len = ga.ga_len;
3272 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003273 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003274 if (update_buffer)
3275 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003276
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003277 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003278 line->sb_cols = len;
3279 line->sb_cells = p;
3280 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003281 if (update_buffer)
3282 {
3283 line->sb_text = NULL;
3284 ++term->tl_scrollback_scrolled;
3285 ga_clear(&ga); // free the text
3286 }
3287 else
3288 {
3289 line->sb_text = text;
3290 ga_init(&ga); // text is kept in tl_scrollback_postponed
3291 }
3292 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003293 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003294 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003295}
3296
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003297/*
3298 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3299 * received and stored in tl_scrollback_postponed.
3300 */
3301 static void
3302handle_postponed_scrollback(term_T *term)
3303{
3304 int i;
3305
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003306 if (term->tl_scrollback_postponed.ga_len == 0)
3307 return;
3308 ch_log(NULL, "Moving postponed scrollback to scrollback");
3309
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003310 // First remove the lines that were appended before, the pushed lines go
3311 // above it.
3312 cleanup_scrollback(term);
3313
3314 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3315 {
3316 char_u *text;
3317 sb_line_T *pp_line;
3318 sb_line_T *line;
3319
3320 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3321 break;
3322 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3323
3324 text = pp_line->sb_text;
3325 if (text == NULL)
3326 text = (char_u *)"";
3327 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3328 vim_free(pp_line->sb_text);
3329
3330 line = (sb_line_T *)term->tl_scrollback.ga_data
3331 + term->tl_scrollback.ga_len;
3332 line->sb_cols = pp_line->sb_cols;
3333 line->sb_cells = pp_line->sb_cells;
3334 line->sb_fill_attr = pp_line->sb_fill_attr;
3335 line->sb_text = NULL;
3336 ++term->tl_scrollback_scrolled;
3337 ++term->tl_scrollback.ga_len;
3338 }
3339
3340 ga_clear(&term->tl_scrollback_postponed);
3341 limit_scrollback(term, &term->tl_scrollback, TRUE);
3342}
3343
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003344static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003345 handle_damage, // damage
3346 handle_moverect, // moverect
3347 handle_movecursor, // movecursor
3348 handle_settermprop, // settermprop
3349 NULL, // bell
3350 handle_resize, // resize
3351 handle_pushline, // sb_pushline
3352 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003353};
3354
3355/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003356 * Do the work after the channel of a terminal was closed.
3357 * Must be called only when updating_screen is FALSE.
3358 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3359 */
3360 static int
3361term_after_channel_closed(term_T *term)
3362{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003363 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003364 if (!term->tl_normal_mode)
3365 {
3366 int fnum = term->tl_buffer->b_fnum;
3367
3368 cleanup_vterm(term);
3369
3370 if (term->tl_finish == TL_FINISH_CLOSE)
3371 {
3372 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003373 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003374#ifdef FEAT_PROP_POPUP
3375 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003376
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003377 // If this was a terminal in a popup window, go back to the
3378 // previous window.
3379 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3380 {
3381 pwin = curwin;
3382 if (win_valid(prevwin))
3383 win_enter(prevwin, FALSE);
3384 }
3385 else
3386#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003387 // If this is the last normal window: exit Vim.
3388 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3389 {
3390 exarg_T ea;
3391
Bram Moolenaara80faa82020-04-12 19:37:17 +02003392 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003393 ex_quit(&ea);
3394 return TRUE;
3395 }
3396
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003397 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003398 ch_log(NULL, "terminal job finished, closing window");
3399 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003400 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003401 if (curwin == aucmd_win)
3402 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003403 if (do_set_w_closing)
3404 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003405 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003406 if (do_set_w_closing)
3407 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003408 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003409#ifdef FEAT_PROP_POPUP
3410 if (pwin != NULL)
3411 popup_close_with_retval(pwin, 0);
3412#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003413 return TRUE;
3414 }
3415 if (term->tl_finish == TL_FINISH_OPEN
3416 && term->tl_buffer->b_nwindows == 0)
3417 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003418 char *cmd = term->tl_opencmd == NULL
3419 ? "botright sbuf %d"
3420 : (char *)term->tl_opencmd;
3421 size_t len = strlen(cmd) + 50;
3422 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003423
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003424 if (buf != NULL)
3425 {
3426 ch_log(NULL, "terminal job finished, opening window");
3427 vim_snprintf(buf, len, cmd, fnum);
3428 do_cmdline_cmd((char_u *)buf);
3429 vim_free(buf);
3430 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003431 }
3432 else
3433 ch_log(NULL, "terminal job finished");
3434 }
3435
3436 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3437 return FALSE;
3438}
3439
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003440#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3441/*
3442 * If the current window is a terminal in a popup window and the job has
3443 * finished, close the popup window and to back to the previous window.
3444 * Otherwise return FAIL.
3445 */
3446 int
3447may_close_term_popup(void)
3448{
3449 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3450 && !term_job_running(curbuf->b_term))
3451 {
3452 win_T *pwin = curwin;
3453
3454 if (win_valid(prevwin))
3455 win_enter(prevwin, FALSE);
3456 popup_close_with_retval(pwin, 0);
3457 return OK;
3458 }
3459 return FAIL;
3460}
3461#endif
3462
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003463/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003464 * Called when a channel has been closed.
3465 * If this was a channel for a terminal window then finish it up.
3466 */
3467 void
3468term_channel_closed(channel_T *ch)
3469{
3470 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003471 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003472 int did_one = FALSE;
3473
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003474 for (term = first_term; term != NULL; term = next_term)
3475 {
3476 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003477 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003478 {
3479 term->tl_channel_closed = TRUE;
3480 did_one = TRUE;
3481
Bram Moolenaard23a8232018-02-10 18:45:26 +01003482 VIM_CLEAR(term->tl_title);
3483 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003484#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003485 if (term->tl_out_fd != NULL)
3486 {
3487 fclose(term->tl_out_fd);
3488 term->tl_out_fd = NULL;
3489 }
3490#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003491
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003492 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003493 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003494 // Cannot open or close windows now. Can happen when
3495 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003496 term->tl_channel_recently_closed = TRUE;
3497 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003498 }
3499
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003500 if (term_after_channel_closed(term))
3501 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003502 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003503 }
3504
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003505 if (did_one)
3506 {
3507 redraw_statuslines();
3508
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003509 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003510 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003511 typebuf_was_filled = TRUE;
3512
3513 term = curbuf->b_term;
3514 if (term != NULL)
3515 {
3516 if (term->tl_job == ch->ch_job)
3517 maketitle();
3518 update_cursor(term, term->tl_cursor_visible);
3519 }
3520 }
3521}
3522
3523/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003524 * To be called after resetting updating_screen: handle any terminal where the
3525 * channel was closed.
3526 */
3527 void
3528term_check_channel_closed_recently()
3529{
3530 term_T *term;
3531 term_T *next_term;
3532
3533 for (term = first_term; term != NULL; term = next_term)
3534 {
3535 next_term = term->tl_next;
3536 if (term->tl_channel_recently_closed)
3537 {
3538 term->tl_channel_recently_closed = FALSE;
3539 if (term_after_channel_closed(term))
3540 // start over, the list may have changed
3541 next_term = first_term;
3542 }
3543 }
3544}
3545
3546/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003547 * Fill one screen line from a line of the terminal.
3548 * Advances "pos" to past the last column.
3549 */
3550 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003551term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003552 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003553 win_T *wp,
3554 VTermScreen *screen,
3555 VTermPos *pos,
3556 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003557{
3558 int off = screen_get_current_line_off();
3559
3560 for (pos->col = 0; pos->col < max_col; )
3561 {
3562 VTermScreenCell cell;
3563 int c;
3564
3565 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003566 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003567
3568 c = cell.chars[0];
3569 if (c == NUL)
3570 {
3571 ScreenLines[off] = ' ';
3572 if (enc_utf8)
3573 ScreenLinesUC[off] = NUL;
3574 }
3575 else
3576 {
3577 if (enc_utf8)
3578 {
3579 int i;
3580
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003581 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003582 for (i = 0; i < Screen_mco
3583 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3584 {
3585 ScreenLinesC[i][off] = cell.chars[i + 1];
3586 if (cell.chars[i + 1] == 0)
3587 break;
3588 }
3589 if (c >= 0x80 || (Screen_mco > 0
3590 && ScreenLinesC[0][off] != 0))
3591 {
3592 ScreenLines[off] = ' ';
3593 ScreenLinesUC[off] = c;
3594 }
3595 else
3596 {
3597 ScreenLines[off] = c;
3598 ScreenLinesUC[off] = NUL;
3599 }
3600 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003601#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003602 else if (has_mbyte && c >= 0x80)
3603 {
3604 char_u mb[MB_MAXBYTES+1];
3605 WCHAR wc = c;
3606
3607 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3608 (char*)mb, 2, 0, 0) > 1)
3609 {
3610 ScreenLines[off] = mb[0];
3611 ScreenLines[off + 1] = mb[1];
3612 cell.width = mb_ptr2cells(mb);
3613 }
3614 else
3615 ScreenLines[off] = c;
3616 }
3617#endif
3618 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003619 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003620 ScreenLines[off] = c;
3621 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003622 ScreenAttrs[off] = cell2attr(term, wp, cell.attrs, cell.fg, cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003623
3624 ++pos->col;
3625 ++off;
3626 if (cell.width == 2)
3627 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003628 // don't set the second byte to NUL for a DBCS encoding, it
3629 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003630 if (enc_utf8)
3631 {
3632 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003633 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003634 }
3635 else if (!has_mbyte)
3636 {
3637 // Can't show a double-width character with a single-byte
3638 // 'encoding', just use a space.
3639 ScreenLines[off] = ' ';
3640 ScreenAttrs[off] = ScreenAttrs[off - 1];
3641 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003642
3643 ++pos->col;
3644 ++off;
3645 }
3646 }
3647}
3648
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003649#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003650 static void
3651update_system_term(term_T *term)
3652{
3653 VTermPos pos;
3654 VTermScreen *screen;
3655
3656 if (term->tl_vterm == NULL)
3657 return;
3658 screen = vterm_obtain_screen(term->tl_vterm);
3659
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003660 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003661 while (term->tl_toprow > 0
3662 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3663 {
3664 int save_p_more = p_more;
3665
3666 p_more = FALSE;
3667 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003668 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003669 p_more = save_p_more;
3670 --term->tl_toprow;
3671 }
3672
3673 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3674 && pos.row < Rows; ++pos.row)
3675 {
3676 if (pos.row < term->tl_rows)
3677 {
3678 int max_col = MIN(Columns, term->tl_cols);
3679
Bram Moolenaar83d47902020-03-26 20:34:00 +01003680 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003681 }
3682 else
3683 pos.col = 0;
3684
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003685 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003686 }
3687
3688 term->tl_dirty_row_start = MAX_ROW;
3689 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003690}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003691#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003692
3693/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003694 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3695 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003696 * Terminal-Normal mode.
3697 */
3698 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003699term_do_update_window(win_T *wp)
3700{
3701 term_T *term = wp->w_buffer->b_term;
3702
3703 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3704}
3705
3706/*
3707 * Called to update a window that contains an active terminal.
3708 */
3709 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003710term_update_window(win_T *wp)
3711{
3712 term_T *term = wp->w_buffer->b_term;
3713 VTerm *vterm;
3714 VTermScreen *screen;
3715 VTermState *state;
3716 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003717 int rows, cols;
3718 int newrows, newcols;
3719 int minsize;
3720 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003721
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003722 vterm = term->tl_vterm;
3723 screen = vterm_obtain_screen(vterm);
3724 state = vterm_obtain_state(vterm);
3725
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003726 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3727 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003728 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003729 {
3730 term->tl_dirty_row_start = 0;
3731 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003732
3733 if (term->tl_postponed_scroll > 0
3734 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003735 // Scrolling is usually faster than redrawing, when there are only
3736 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003737 term_scroll_up(term, 0, term->tl_postponed_scroll);
3738 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003739 }
3740
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003741 /*
3742 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003743 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003744 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003745 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003746
Bram Moolenaar498c2562018-04-15 23:45:15 +02003747 newrows = 99999;
3748 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003749 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003750 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003751 // Always use curwin, it may be a popup window.
3752 win_T *wwp = twp == NULL ? curwin : twp;
3753
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003754 // When more than one window shows the same terminal, use the
3755 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003756 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003757 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003758 newrows = MIN(newrows, wwp->w_height);
3759 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003760 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003761 if (twp == NULL)
3762 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003763 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003764 if (newrows == 99999 || newcols == 99999)
3765 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003766 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3767 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3768
Bram Moolenaareba13e42021-02-23 17:47:23 +01003769 // If no cell is visible there is no point in resizing. Also, vterm can't
3770 // handle a zero height.
3771 if (newrows == 0 || newcols == 0)
3772 return;
3773
Bram Moolenaar498c2562018-04-15 23:45:15 +02003774 if (term->tl_rows != newrows || term->tl_cols != newcols)
3775 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003776 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003777 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003778 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003779 newrows);
3780 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003781
3782 // Updating the terminal size will cause the snapshot to be cleared.
3783 // When not in terminal_loop() we need to restore it.
3784 if (term != in_terminal_loop)
3785 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003786 }
3787
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003788 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003789 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003790 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003791
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003792 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3793 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003794 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003795 if (pos.row < term->tl_rows)
3796 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003797 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003798
Bram Moolenaar83d47902020-03-26 20:34:00 +01003799 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003800 }
3801 else
3802 pos.col = 0;
3803
Bram Moolenaarf118d482018-03-13 13:14:00 +01003804 screen_line(wp->w_winrow + pos.row
3805#ifdef FEAT_MENU
3806 + winbar_height(wp)
3807#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003808 , wp->w_wincol, pos.col, wp->w_width,
3809#ifdef FEAT_PROP_POPUP
3810 popup_is_popup(wp) ? SLF_POPUP :
3811#endif
3812 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003813 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003814 term->tl_dirty_row_start = MAX_ROW;
3815 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003816}
3817
3818/*
3819 * Return TRUE if "wp" is a terminal window where the job has finished.
3820 */
3821 int
3822term_is_finished(buf_T *buf)
3823{
3824 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3825}
3826
3827/*
3828 * Return TRUE if "wp" is a terminal window where the job has finished or we
3829 * are in Terminal-Normal mode, thus we show the buffer contents.
3830 */
3831 int
3832term_show_buffer(buf_T *buf)
3833{
3834 term_T *term = buf->b_term;
3835
3836 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3837}
3838
3839/*
3840 * The current buffer is going to be changed. If there is terminal
3841 * highlighting remove it now.
3842 */
3843 void
3844term_change_in_curbuf(void)
3845{
3846 term_T *term = curbuf->b_term;
3847
3848 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3849 {
3850 free_scrollback(term);
3851 redraw_buf_later(term->tl_buffer, NOT_VALID);
3852
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003853 // The buffer is now like a normal buffer, it cannot be easily
3854 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003855 set_string_option_direct((char_u *)"buftype", -1,
3856 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3857 }
3858}
3859
3860/*
3861 * Get the screen attribute for a position in the buffer.
3862 * Use a negative "col" to get the filler background color.
3863 */
3864 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003865term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003866{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003867 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003868 term_T *term = buf->b_term;
3869 sb_line_T *line;
3870 cellattr_T *cellattr;
3871
3872 if (lnum > term->tl_scrollback.ga_len)
3873 cellattr = &term->tl_default_color;
3874 else
3875 {
3876 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3877 if (col < 0 || col >= line->sb_cols)
3878 cellattr = &line->sb_fill_attr;
3879 else
3880 cellattr = line->sb_cells + col;
3881 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003882 return cell2attr(term, wp, cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003883}
3884
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003885/*
3886 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003887 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003888 */
3889 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003890cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003891{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003892 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3893 if (rgb->index == 0)
3894 rgb->type = VTERM_COLOR_RGB;
3895 else
3896 {
3897 rgb->type = VTERM_COLOR_INDEXED;
3898 --rgb->index;
3899 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003900}
3901
3902/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003903 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003904 */
3905 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003906init_default_colors(term_T *term, win_T *wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003907{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003908 VTermColor *fg, *bg;
3909 int fgval, bgval;
3910 int id;
3911
Bram Moolenaara80faa82020-04-12 19:37:17 +02003912 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003913 term->tl_default_color.width = 1;
3914 fg = &term->tl_default_color.fg;
3915 bg = &term->tl_default_color.bg;
3916
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003917 // Vterm uses a default black background. Set it to white when
3918 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003919 if (*p_bg == 'l')
3920 {
3921 fgval = 0;
3922 bgval = 255;
3923 }
3924 else
3925 {
3926 fgval = 255;
3927 bgval = 0;
3928 }
3929 fg->red = fg->green = fg->blue = fgval;
3930 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003931 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3932 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003933
Bram Moolenaar83d47902020-03-26 20:34:00 +01003934 // The 'wincolor' or the highlight group overrules the defaults.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003935 if (wp != NULL && *wp->w_p_wcr != NUL)
3936 id = syn_name2id(wp->w_p_wcr);
3937 else
Bram Moolenaar83d47902020-03-26 20:34:00 +01003938 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003939
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003940 // Use the actual color for the GUI and when 'termguicolors' is set.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003941#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3942 if (0
3943# ifdef FEAT_GUI
3944 || gui.in_use
3945# endif
3946# ifdef FEAT_TERMGUICOLORS
3947 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003948# ifdef FEAT_VTP
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003949 // Finally get INVALCOLOR on this execution path
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003950 || (!p_tgc && t_colors >= 256)
3951# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003952# endif
3953 )
3954 {
3955 guicolor_T fg_rgb = INVALCOLOR;
3956 guicolor_T bg_rgb = INVALCOLOR;
3957
3958 if (id != 0)
3959 syn_id2colors(id, &fg_rgb, &bg_rgb);
3960
3961# ifdef FEAT_GUI
3962 if (gui.in_use)
3963 {
3964 if (fg_rgb == INVALCOLOR)
3965 fg_rgb = gui.norm_pixel;
3966 if (bg_rgb == INVALCOLOR)
3967 bg_rgb = gui.back_pixel;
3968 }
3969# ifdef FEAT_TERMGUICOLORS
3970 else
3971# endif
3972# endif
3973# ifdef FEAT_TERMGUICOLORS
3974 {
3975 if (fg_rgb == INVALCOLOR)
3976 fg_rgb = cterm_normal_fg_gui_color;
3977 if (bg_rgb == INVALCOLOR)
3978 bg_rgb = cterm_normal_bg_gui_color;
3979 }
3980# endif
3981 if (fg_rgb != INVALCOLOR)
3982 {
3983 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3984
3985 fg->red = (unsigned)(rgb >> 16);
3986 fg->green = (unsigned)(rgb >> 8) & 255;
3987 fg->blue = (unsigned)rgb & 255;
3988 }
3989 if (bg_rgb != INVALCOLOR)
3990 {
3991 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
3992
3993 bg->red = (unsigned)(rgb >> 16);
3994 bg->green = (unsigned)(rgb >> 8) & 255;
3995 bg->blue = (unsigned)rgb & 255;
3996 }
3997 }
3998 else
3999#endif
4000 if (id != 0 && t_colors >= 16)
4001 {
Milly7b5f45b2021-10-15 22:25:43 +01004002 int cterm_fg = -1;
4003 int cterm_bg = -1;
4004 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01004005
4006 if (cterm_fg >= 0)
4007 cterm_color2vterm(cterm_fg, fg);
4008 if (cterm_bg >= 0)
4009 cterm_color2vterm(cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004010 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004011 else
4012 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004013#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004014 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004015#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004016
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004017 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004018 if (cterm_normal_fg_color > 0)
4019 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004020 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004021# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4022# ifdef VIMDLL
4023 if (!gui.in_use)
4024# endif
4025 {
4026 tmp = fg->red;
4027 fg->red = fg->blue;
4028 fg->blue = tmp;
4029 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004030# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004031 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004032# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004033 else
4034 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004035# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004036
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004037 if (cterm_normal_bg_color > 0)
4038 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004039 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004040# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4041# ifdef VIMDLL
4042 if (!gui.in_use)
4043# endif
4044 {
4045 tmp = fg->red;
4046 fg->red = fg->blue;
4047 fg->blue = tmp;
4048 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004049# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004050 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004051# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004052 else
4053 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004054# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004055 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004056}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004057
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004058#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4059/*
4060 * Set the 16 ANSI colors from array of RGB values
4061 */
4062 static void
4063set_vterm_palette(VTerm *vterm, long_u *rgb)
4064{
4065 int index = 0;
4066 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004067
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004068 for (; index < 16; index++)
4069 {
4070 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004071
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004072 color.red = (unsigned)(rgb[index] >> 16);
4073 color.green = (unsigned)(rgb[index] >> 8) & 255;
4074 color.blue = (unsigned)rgb[index] & 255;
4075 vterm_state_set_palette_color(state, index, &color);
4076 }
4077}
4078
4079/*
4080 * Set the ANSI color palette from a list of colors
4081 */
4082 static int
4083set_ansi_colors_list(VTerm *vterm, list_T *list)
4084{
4085 int n = 0;
4086 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004087 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004088
Bram Moolenaarb0992022020-01-30 14:55:42 +01004089 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004090 {
4091 char_u *color_name;
4092 guicolor_T guicolor;
4093
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004094 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004095 if (color_name == NULL)
4096 return FAIL;
4097
4098 guicolor = GUI_GET_COLOR(color_name);
4099 if (guicolor == INVALCOLOR)
4100 return FAIL;
4101
4102 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4103 }
4104
4105 if (n != 16 || li != NULL)
4106 return FAIL;
4107
4108 set_vterm_palette(vterm, rgb);
4109
4110 return OK;
4111}
4112
4113/*
4114 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4115 */
4116 static void
4117init_vterm_ansi_colors(VTerm *vterm)
4118{
4119 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4120
4121 if (var != NULL
4122 && (var->di_tv.v_type != VAR_LIST
4123 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004124 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004125 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004126 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004127}
4128#endif
4129
Bram Moolenaar52acb112018-03-18 19:20:22 +01004130/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004131 * Handles a "drop" command from the job in the terminal.
4132 * "item" is the file name, "item->li_next" may have options.
4133 */
4134 static void
4135handle_drop_command(listitem_T *item)
4136{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004137 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004138 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004139 int bufnr;
4140 win_T *wp;
4141 tabpage_T *tp;
4142 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004143 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004144
4145 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4146 FOR_ALL_TAB_WINDOWS(tp, wp)
4147 {
4148 if (wp->w_buffer->b_fnum == bufnr)
4149 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004150 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004151 goto_tabpage_win(tp, wp);
4152 return;
4153 }
4154 }
4155
Bram Moolenaara80faa82020-04-12 19:37:17 +02004156 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004157
4158 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4159 && opt_item->li_tv.vval.v_dict != NULL)
4160 {
4161 dict_T *dict = opt_item->li_tv.vval.v_dict;
4162 char_u *p;
4163
Bram Moolenaar8f667172018-12-14 15:38:31 +01004164 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004165 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004166 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004167 if (p != NULL)
4168 {
4169 if (check_ff_value(p) == FAIL)
4170 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4171 else
4172 ea.force_ff = *p;
4173 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004174 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004175 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004176 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004177 if (p != NULL)
4178 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004179 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004180 if (ea.cmd != NULL)
4181 {
4182 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4183 ea.force_enc = 11;
4184 tofree = ea.cmd;
4185 }
4186 }
4187
Bram Moolenaar8f667172018-12-14 15:38:31 +01004188 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004189 if (p != NULL)
4190 get_bad_opt(p, &ea);
4191
4192 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4193 ea.force_bin = FORCE_BIN;
4194 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4195 ea.force_bin = FORCE_BIN;
4196 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4197 ea.force_bin = FORCE_NOBIN;
4198 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4199 ea.force_bin = FORCE_NOBIN;
4200 }
4201
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004202 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004203 if (ea.cmd == NULL)
4204 ea.cmd = (char_u *)"split";
4205 ea.arg = fname;
4206 ea.cmdidx = CMD_split;
4207 ex_splitview(&ea);
4208
4209 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004210}
4211
4212/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004213 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4214 */
4215 static int
4216is_permitted_term_api(char_u *func, char_u *pat)
4217{
4218 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4219}
4220
4221/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004222 * Handles a function call from the job running in a terminal.
4223 * "item" is the function name, "item->li_next" has the arguments.
4224 */
4225 static void
4226handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4227{
4228 char_u *func;
4229 typval_T argvars[2];
4230 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004231 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004232
4233 if (item->li_next == NULL)
4234 {
4235 ch_log(channel, "Missing function arguments for call");
4236 return;
4237 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004238 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004239
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004240 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004241 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004242 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004243 return;
4244 }
4245
4246 argvars[0].v_type = VAR_NUMBER;
4247 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4248 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004249 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004250 funcexe.firstline = 1L;
4251 funcexe.lastline = 1L;
4252 funcexe.evaluate = TRUE;
4253 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004254 {
4255 clear_tv(&rettv);
4256 ch_log(channel, "Function %s called", func);
4257 }
4258 else
4259 ch_log(channel, "Calling function %s failed", func);
4260}
4261
4262/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004263 * URL decoding (also know as Percent-encoding).
4264 *
4265 * Note this function currently is only used for decoding shell's
4266 * OSC 7 escape sequence which we can assume all bytes are valid
4267 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4268 * encoding bytes like 0xfe, 0xff.
4269 */
4270 static size_t
4271url_decode(const char *src, const size_t len, char_u *dst)
4272{
4273 size_t i = 0, j = 0;
4274
4275 while (i < len)
4276 {
4277 if (src[i] == '%' && i + 2 < len)
4278 {
4279 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4280 j++;
4281 i += 3;
4282 }
4283 else
4284 {
4285 dst[j] = src[i];
4286 i++;
4287 j++;
4288 }
4289 }
4290 dst[j] = '\0';
4291 return j;
4292}
4293
4294/*
4295 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4296 *
4297 * The OSC 7 sequence has the format of
4298 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4299 * and what VTerm provides via VTermStringFragment is
4300 * "file://HOSTNAME/CURRENT/DIR"
4301 */
4302 static void
4303sync_shell_dir(VTermStringFragment *frag)
4304{
4305 int offset = 7; // len of "file://" is 7
4306 char *pos = (char *)frag->str + offset;
4307 char_u *new_dir;
4308
4309 // remove HOSTNAME to get PWD
Bram Moolenaar918b0892021-05-08 20:09:24 +02004310 while (*pos != '/' && offset < (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004311 {
4312 offset += 1;
4313 pos += 1;
4314 }
4315
Bram Moolenaar918b0892021-05-08 20:09:24 +02004316 if (offset >= (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004317 {
4318 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
4319 frag->str);
4320 return;
4321 }
4322
4323 new_dir = alloc(frag->len - offset + 1);
4324 url_decode(pos, frag->len-offset, new_dir);
4325 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4326 vim_free(new_dir);
4327}
4328
4329/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004330 * Called by libvterm when it cannot recognize an OSC sequence.
4331 * We recognize a terminal API command.
4332 */
4333 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004334parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004335{
4336 term_T *term = (term_T *)user;
4337 js_read_T reader;
4338 typval_T tv;
4339 channel_T *channel = term->tl_job == NULL ? NULL
4340 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004341 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004342
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004343 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
4344 if (p_asd && command == 7)
4345 {
4346 sync_shell_dir(&frag);
4347 return 1;
4348 }
4349
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004350 if (command != 51)
4351 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004352
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004353 // Concatenate what was received until the final piece is found.
4354 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4355 {
4356 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004357 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004358 }
4359 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004360 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004361 if (!frag.final)
4362 return 1;
4363
4364 ((char *)gap->ga_data)[gap->ga_len] = 0;
4365 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004366 reader.js_fill = NULL;
4367 reader.js_used = 0;
4368 if (json_decode(&reader, &tv, 0) == OK
4369 && tv.v_type == VAR_LIST
4370 && tv.vval.v_list != NULL)
4371 {
4372 listitem_T *item = tv.vval.v_list->lv_first;
4373
4374 if (item == NULL)
4375 ch_log(channel, "Missing command");
4376 else
4377 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004378 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004379
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004380 // Make sure an invoked command doesn't delete the buffer (and the
4381 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004382 ++term->tl_buffer->b_locked;
4383
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004384 item = item->li_next;
4385 if (item == NULL)
4386 ch_log(channel, "Missing argument for %s", cmd);
4387 else if (STRCMP(cmd, "drop") == 0)
4388 handle_drop_command(item);
4389 else if (STRCMP(cmd, "call") == 0)
4390 handle_call_command(term, channel, item);
4391 else
4392 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004393 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004394 }
4395 }
4396 else
4397 ch_log(channel, "Invalid JSON received");
4398
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004399 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004400 clear_tv(&tv);
4401 return 1;
4402}
4403
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004404/*
4405 * Called by libvterm when it cannot recognize a CSI sequence.
4406 * We recognize the window position report.
4407 */
4408 static int
4409parse_csi(
4410 const char *leader UNUSED,
4411 const long args[],
4412 int argcount,
4413 const char *intermed UNUSED,
4414 char command,
4415 void *user)
4416{
4417 term_T *term = (term_T *)user;
4418 char buf[100];
4419 int len;
4420 int x = 0;
4421 int y = 0;
4422 win_T *wp;
4423
4424 // We recognize only CSI 13 t
4425 if (command != 't' || argcount != 1 || args[0] != 13)
4426 return 0; // not handled
4427
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004428 // When getting the window position is not possible or it fails it results
4429 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004430#if defined(FEAT_GUI) \
4431 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4432 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004433 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004434#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004435
4436 FOR_ALL_WINDOWS(wp)
4437 if (wp->w_buffer == term->tl_buffer)
4438 break;
4439 if (wp != NULL)
4440 {
4441#ifdef FEAT_GUI
4442 if (gui.in_use)
4443 {
4444 x += wp->w_wincol * gui.char_width;
4445 y += W_WINROW(wp) * gui.char_height;
4446 }
4447 else
4448#endif
4449 {
4450 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004451 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004452 x += wp->w_wincol * 7;
4453 y += W_WINROW(wp) * 10;
4454 }
4455 }
4456
4457 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4458 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4459 (char_u *)buf, len, NULL);
4460 return 1;
4461}
4462
Bram Moolenaard8637282020-05-20 18:41:41 +02004463static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004464 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004465 parse_csi, // csi
4466 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004467 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004468};
4469
4470/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004471 * Use Vim's allocation functions for vterm so profiling works.
4472 */
4473 static void *
4474vterm_malloc(size_t size, void *data UNUSED)
4475{
Bram Moolenaar88137392021-11-12 16:01:15 +00004476 // make sure that the length is not zero
4477 return alloc_clear(size == 0 ? 1L : size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004478}
4479
4480 static void
4481vterm_memfree(void *ptr, void *data UNUSED)
4482{
4483 vim_free(ptr);
4484}
4485
4486static VTermAllocatorFunctions vterm_allocator = {
4487 &vterm_malloc,
4488 &vterm_memfree
4489};
4490
4491/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004492 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004493 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004494 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004495 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004496create_vterm(term_T *term, int rows, int cols)
4497{
4498 VTerm *vterm;
4499 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004500 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004501 VTermValue value;
4502
Bram Moolenaar756ef112018-04-10 12:04:27 +02004503 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004504 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004505 if (vterm == NULL)
4506 return FAIL;
4507
4508 // Allocate screen and state here, so we can bail out if that fails.
4509 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004510 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004511 if (state == NULL || screen == NULL)
4512 {
4513 vterm_free(vterm);
4514 return FAIL;
4515 }
4516
Bram Moolenaar52acb112018-03-18 19:20:22 +01004517 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004518 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004519 vterm_set_utf8(vterm, 1);
4520
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004521 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004522
4523 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004524 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004525 &term->tl_default_color.fg,
4526 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004527
Bram Moolenaar9e587872019-05-13 20:27:23 +02004528 if (t_colors < 16)
4529 // Less than 16 colors: assume that bold means using a bright color for
4530 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004531 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4532
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004533 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004534 vterm_screen_reset(screen, 1 /* hard */);
4535
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004536 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004537 vterm_screen_enable_altscreen(screen, 1);
4538
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004539 // For unix do not use a blinking cursor. In an xterm this causes the
4540 // cursor to blink if it's blinking in the xterm.
4541 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004542#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004543 if (GetCaretBlinkTime() == INFINITE)
4544 value.boolean = 0;
4545 else
4546 value.boolean = 1;
4547#else
4548 value.boolean = 0;
4549#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004550 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004551 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004552
4553 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004554}
4555
4556/*
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004557 * Called when 'wincolor' was set.
4558 */
4559 void
Bram Moolenaarad431992021-05-03 20:40:38 +02004560term_update_colors(term_T *term)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004561{
Bram Moolenaarad431992021-05-03 20:40:38 +02004562 win_T *wp;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004563
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01004564 if (term->tl_vterm == NULL)
4565 return;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004566 init_default_colors(term, curwin);
4567 vterm_state_set_default_colors(
4568 vterm_obtain_state(term->tl_vterm),
4569 &term->tl_default_color.fg,
4570 &term->tl_default_color.bg);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01004571
Bram Moolenaarad431992021-05-03 20:40:38 +02004572 FOR_ALL_WINDOWS(wp)
4573 if (wp->w_buffer == term->tl_buffer)
4574 redraw_win_later(wp, NOT_VALID);
4575}
4576
4577/*
4578 * Called when 'background' was set.
4579 */
4580 void
4581term_update_colors_all(void)
4582{
4583 term_T *tp;
4584
4585 FOR_ALL_TERMS(tp)
4586 term_update_colors(tp);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004587}
4588
4589/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004590 * Return the text to show for the buffer name and status.
4591 */
4592 char_u *
4593term_get_status_text(term_T *term)
4594{
4595 if (term->tl_status_text == NULL)
4596 {
4597 char_u *txt;
4598 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004599 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004600
4601 if (term->tl_normal_mode)
4602 {
4603 if (term_job_running(term))
4604 txt = (char_u *)_("Terminal");
4605 else
4606 txt = (char_u *)_("Terminal-finished");
4607 }
4608 else if (term->tl_title != NULL)
4609 txt = term->tl_title;
4610 else if (term_none_open(term))
4611 txt = (char_u *)_("active");
4612 else if (term_job_running(term))
4613 txt = (char_u *)_("running");
4614 else
4615 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004616 fname = buf_get_fname(term->tl_buffer);
4617 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004618 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004619 if (term->tl_status_text != NULL)
4620 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004621 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004622 }
4623 return term->tl_status_text;
4624}
4625
4626/*
4627 * Mark references in jobs of terminals.
4628 */
4629 int
4630set_ref_in_term(int copyID)
4631{
4632 int abort = FALSE;
4633 term_T *term;
4634 typval_T tv;
4635
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004636 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004637 if (term->tl_job != NULL)
4638 {
4639 tv.v_type = VAR_JOB;
4640 tv.vval.v_job = term->tl_job;
4641 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4642 }
4643 return abort;
4644}
4645
4646/*
4647 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004648 * Returns NULL when the buffer is not for a terminal window and logs a message
4649 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004650 */
4651 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004652term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004653{
4654 buf_T *buf;
4655
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004656 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004657 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004658 --emsg_off;
4659 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004660 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004661 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004662 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004663 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004664 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004665 return buf;
4666}
4667
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004668 static void
4669clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004670{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004671 CLEAR_FIELD(*cell);
4672 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4673 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004674}
4675
4676 static void
4677dump_term_color(FILE *fd, VTermColor *color)
4678{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004679 int index;
4680
4681 if (VTERM_COLOR_IS_INDEXED(color))
4682 index = color->index + 1;
4683 else if (color->type == 0)
4684 // use RGB values
4685 index = 255;
4686 else
4687 // default color
4688 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004689 fprintf(fd, "%02x%02x%02x%d",
4690 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004691 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004692}
4693
4694/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004695 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004696 *
4697 * Each screen cell in full is:
4698 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4699 * {characters} is a space for an empty cell
4700 * For a double-width character "+" is changed to "*" and the next cell is
4701 * skipped.
4702 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4703 * when "&" use the same as the previous cell.
4704 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4705 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4706 * {color-idx} is a number from 0 to 255
4707 *
4708 * Screen cell with same width, attributes and color as the previous one:
4709 * |{characters}
4710 *
4711 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4712 *
4713 * Repeating the previous screen cell:
4714 * @{count}
4715 */
4716 void
4717f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4718{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004719 buf_T *buf;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004720 term_T *term;
4721 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004722 int max_height = 0;
4723 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004724 stat_T st;
4725 FILE *fd;
4726 VTermPos pos;
4727 VTermScreen *screen;
4728 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004729 VTermState *state;
4730 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004731
4732 if (check_restricted() || check_secure())
4733 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004734
4735 if (in_vim9script()
4736 && (check_for_buffer_arg(argvars, 0) == FAIL
4737 || check_for_string_arg(argvars, 1) == FAIL
4738 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4739 return;
4740
4741 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004742 if (buf == NULL)
4743 return;
4744 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004745 if (term->tl_vterm == NULL)
4746 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004747 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004748 return;
4749 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004750
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004751 if (argvars[2].v_type != VAR_UNKNOWN)
4752 {
4753 dict_T *d;
4754
4755 if (argvars[2].v_type != VAR_DICT)
4756 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004757 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004758 return;
4759 }
4760 d = argvars[2].vval.v_dict;
4761 if (d != NULL)
4762 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004763 max_height = dict_get_number(d, (char_u *)"rows");
4764 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004765 }
4766 }
4767
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004768 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004769 if (fname == NULL)
4770 return;
4771 if (mch_stat((char *)fname, &st) >= 0)
4772 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004773 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004774 return;
4775 }
4776
Bram Moolenaard96ff162018-02-18 22:13:29 +01004777 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4778 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004779 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004780 return;
4781 }
4782
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004783 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004784
4785 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004786 state = vterm_obtain_state(term->tl_vterm);
4787 vterm_state_get_cursorpos(state, &cursor_pos);
4788
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004789 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4790 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004791 {
4792 int repeat = 0;
4793
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004794 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4795 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004796 {
4797 VTermScreenCell cell;
4798 int same_attr;
4799 int same_chars = TRUE;
4800 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004801 int is_cursor_pos = (pos.col == cursor_pos.col
4802 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004803
4804 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004805 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004806
4807 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4808 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004809 int c = cell.chars[i];
4810 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004811 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004812
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004813 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004814 if (i == 0)
4815 {
4816 c = (c == NUL) ? ' ' : c;
4817 pc = (pc == NUL) ? ' ' : pc;
4818 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004819 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004820 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004821 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004822 break;
4823 }
4824 same_attr = vtermAttr2hl(cell.attrs)
4825 == vtermAttr2hl(prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004826 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4827 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004828 if (same_chars && cell.width == prev_cell.width && same_attr
4829 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004830 {
4831 ++repeat;
4832 }
4833 else
4834 {
4835 if (repeat > 0)
4836 {
4837 fprintf(fd, "@%d", repeat);
4838 repeat = 0;
4839 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004840 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004841
4842 if (cell.chars[0] == NUL)
4843 fputs(" ", fd);
4844 else
4845 {
4846 char_u charbuf[10];
4847 int len;
4848
4849 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4850 && cell.chars[i] != NUL; ++i)
4851 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004852 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004853 fwrite(charbuf, len, 1, fd);
4854 }
4855 }
4856
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004857 // When only the characters differ we don't write anything, the
4858 // following "|", "@" or NL will indicate using the same
4859 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004860 if (cell.width != prev_cell.width || !same_attr)
4861 {
4862 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004863 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004864 else
4865 fputs("+", fd);
4866
4867 if (same_attr)
4868 {
4869 fputs("&", fd);
4870 }
4871 else
4872 {
4873 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004874 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004875 fputs("&", fd);
4876 else
4877 {
4878 fputs("#", fd);
4879 dump_term_color(fd, &cell.fg);
4880 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004881 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004882 fputs("&", fd);
4883 else
4884 {
4885 fputs("#", fd);
4886 dump_term_color(fd, &cell.bg);
4887 }
4888 }
4889 }
4890
4891 prev_cell = cell;
4892 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004893
4894 if (cell.width == 2)
4895 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004896 }
4897 if (repeat > 0)
4898 fprintf(fd, "@%d", repeat);
4899 fputs("\n", fd);
4900 }
4901
4902 fclose(fd);
4903}
4904
4905/*
4906 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4907 */
4908 static void
4909dump_is_corrupt(garray_T *gap)
4910{
4911 ga_concat(gap, (char_u *)"CORRUPT");
4912}
4913
4914 static void
4915append_cell(garray_T *gap, cellattr_T *cell)
4916{
4917 if (ga_grow(gap, 1) == OK)
4918 {
4919 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4920 ++gap->ga_len;
4921 }
4922}
4923
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004924 static void
4925clear_cellattr(cellattr_T *cell)
4926{
4927 CLEAR_FIELD(*cell);
4928 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4929 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4930}
4931
Bram Moolenaard96ff162018-02-18 22:13:29 +01004932/*
4933 * Read the dump file from "fd" and append lines to the current buffer.
4934 * Return the cell width of the longest line.
4935 */
4936 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004937read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004938{
4939 int c;
4940 garray_T ga_text;
4941 garray_T ga_cell;
4942 char_u *prev_char = NULL;
4943 int attr = 0;
4944 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004945 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004946 term_T *term = curbuf->b_term;
4947 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004948 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004949
4950 ga_init2(&ga_text, 1, 90);
4951 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004952 clear_cellattr(&cell);
4953 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004954 cursor_pos->row = -1;
4955 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004956
4957 c = fgetc(fd);
4958 for (;;)
4959 {
4960 if (c == EOF)
4961 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004962 if (c == '\r')
4963 {
4964 // DOS line endings? Ignore.
4965 c = fgetc(fd);
4966 }
4967 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004968 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004969 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004970 if (ga_text.ga_data == NULL)
4971 dump_is_corrupt(&ga_text);
4972 if (ga_grow(&term->tl_scrollback, 1) == OK)
4973 {
4974 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
4975 + term->tl_scrollback.ga_len;
4976
4977 if (max_cells < ga_cell.ga_len)
4978 max_cells = ga_cell.ga_len;
4979 line->sb_cols = ga_cell.ga_len;
4980 line->sb_cells = ga_cell.ga_data;
4981 line->sb_fill_attr = term->tl_default_color;
4982 ++term->tl_scrollback.ga_len;
4983 ga_init(&ga_cell);
4984
4985 ga_append(&ga_text, NUL);
4986 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4987 ga_text.ga_len, FALSE);
4988 }
4989 else
4990 ga_clear(&ga_cell);
4991 ga_text.ga_len = 0;
4992
4993 c = fgetc(fd);
4994 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004995 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004996 {
4997 int prev_len = ga_text.ga_len;
4998
Bram Moolenaar9271d052018-02-25 21:39:46 +01004999 if (c == '>')
5000 {
5001 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005002 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005003 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5004 cursor_pos->col = ga_cell.ga_len;
5005 }
5006
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005007 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005008 c = fgetc(fd);
5009 if (c != EOF)
5010 ga_append(&ga_text, c);
5011 for (;;)
5012 {
5013 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005014 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005015 || c == EOF || c == '\n')
5016 break;
5017 ga_append(&ga_text, c);
5018 }
5019
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005020 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005021 vim_free(prev_char);
5022 if (ga_text.ga_data != NULL)
5023 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5024 ga_text.ga_len - prev_len);
5025
Bram Moolenaar9271d052018-02-25 21:39:46 +01005026 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005027 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005028 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005029 }
5030 else if (c == '+' || c == '*')
5031 {
5032 int is_bg;
5033
5034 cell.width = c == '+' ? 1 : 2;
5035
5036 c = fgetc(fd);
5037 if (c == '&')
5038 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005039 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005040 c = fgetc(fd);
5041 }
5042 else if (isdigit(c))
5043 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005044 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005045 attr = 0;
5046 while (isdigit(c))
5047 {
5048 attr = attr * 10 + (c - '0');
5049 c = fgetc(fd);
5050 }
5051 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005052
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005053 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005054 for (is_bg = 0; is_bg <= 1; ++is_bg)
5055 {
5056 if (c == '&')
5057 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005058 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005059 c = fgetc(fd);
5060 }
5061 else if (c == '#')
5062 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005063 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005064
5065 c = fgetc(fd);
5066 red = hex2nr(c);
5067 c = fgetc(fd);
5068 red = (red << 4) + hex2nr(c);
5069 c = fgetc(fd);
5070 green = hex2nr(c);
5071 c = fgetc(fd);
5072 green = (green << 4) + hex2nr(c);
5073 c = fgetc(fd);
5074 blue = hex2nr(c);
5075 c = fgetc(fd);
5076 blue = (blue << 4) + hex2nr(c);
5077 c = fgetc(fd);
5078 if (!isdigit(c))
5079 dump_is_corrupt(&ga_text);
5080 while (isdigit(c))
5081 {
5082 index = index * 10 + (c - '0');
5083 c = fgetc(fd);
5084 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005085 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005086 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005087 type = VTERM_COLOR_RGB;
5088 if (index == 0)
5089 {
5090 if (is_bg)
5091 type |= VTERM_COLOR_DEFAULT_BG;
5092 else
5093 type |= VTERM_COLOR_DEFAULT_FG;
5094 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005095 }
5096 else
5097 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005098 type = VTERM_COLOR_INDEXED;
5099 index -= 1;
5100 }
5101 if (is_bg)
5102 {
5103 cell.bg.type = type;
5104 cell.bg.red = red;
5105 cell.bg.green = green;
5106 cell.bg.blue = blue;
5107 cell.bg.index = index;
5108 }
5109 else
5110 {
5111 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005112 cell.fg.red = red;
5113 cell.fg.green = green;
5114 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005115 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005116 }
5117 }
5118 else
5119 dump_is_corrupt(&ga_text);
5120 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005121 }
5122 else
5123 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005124 }
5125 else
5126 dump_is_corrupt(&ga_text);
5127
5128 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005129 if (cell.width == 2)
5130 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005131 }
5132 else if (c == '@')
5133 {
5134 if (prev_char == NULL)
5135 dump_is_corrupt(&ga_text);
5136 else
5137 {
5138 int count = 0;
5139
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005140 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005141 for (;;)
5142 {
5143 c = fgetc(fd);
5144 if (!isdigit(c))
5145 break;
5146 count = count * 10 + (c - '0');
5147 }
5148
5149 while (count-- > 0)
5150 {
5151 ga_concat(&ga_text, prev_char);
5152 append_cell(&ga_cell, &cell);
5153 }
5154 }
5155 }
5156 else
5157 {
5158 dump_is_corrupt(&ga_text);
5159 c = fgetc(fd);
5160 }
5161 }
5162
5163 if (ga_text.ga_len > 0)
5164 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005165 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005166 dump_is_corrupt(&ga_text);
5167 ga_append(&ga_text, NUL);
5168 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5169 ga_text.ga_len, FALSE);
5170 }
5171
5172 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005173 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005174 vim_free(prev_char);
5175
5176 return max_cells;
5177}
5178
5179/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005180 * Return an allocated string with at least "text_width" "=" characters and
5181 * "fname" inserted in the middle.
5182 */
5183 static char_u *
5184get_separator(int text_width, char_u *fname)
5185{
5186 int width = MAX(text_width, curwin->w_width);
5187 char_u *textline;
5188 int fname_size;
5189 char_u *p = fname;
5190 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005191 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005192
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005193 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005194 if (textline == NULL)
5195 return NULL;
5196
5197 fname_size = vim_strsize(fname);
5198 if (fname_size < width - 8)
5199 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005200 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005201 width = MAX(text_width, fname_size + 8);
5202 }
5203 else if (fname_size > width - 8)
5204 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005205 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005206 p = gettail(fname);
5207 fname_size = vim_strsize(p);
5208 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005209 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005210 while (fname_size > width - 8)
5211 {
5212 p += (*mb_ptr2len)(p);
5213 fname_size = vim_strsize(p);
5214 }
5215
5216 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5217 textline[i] = '=';
5218 textline[i++] = ' ';
5219
5220 STRCPY(textline + i, p);
5221 off = STRLEN(textline);
5222 textline[off] = ' ';
5223 for (i = 1; i < (width - fname_size) / 2; ++i)
5224 textline[off + i] = '=';
5225 textline[off + i] = NUL;
5226
5227 return textline;
5228}
5229
5230/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005231 * Common for "term_dumpdiff()" and "term_dumpload()".
5232 */
5233 static void
5234term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5235{
5236 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005237 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005238 char_u buf1[NUMBUFLEN];
5239 char_u buf2[NUMBUFLEN];
5240 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005241 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005242 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005243 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005244 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005245 char_u *textline = NULL;
5246
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005247 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005248 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005249 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005250 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005251 if (fname1 == NULL || (do_diff && fname2 == NULL))
5252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005253 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005254 return;
5255 }
5256 fd1 = mch_fopen((char *)fname1, READBIN);
5257 if (fd1 == NULL)
5258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005259 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005260 return;
5261 }
5262 if (do_diff)
5263 {
5264 fd2 = mch_fopen((char *)fname2, READBIN);
5265 if (fd2 == NULL)
5266 {
5267 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005268 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005269 return;
5270 }
5271 }
5272
5273 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005274 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5275 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5276 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5277 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5278 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005279
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005280 if (opt.jo_term_name == NULL)
5281 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005282 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005283
Bram Moolenaar51e14382019-05-25 20:21:28 +02005284 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005285 if (fname_tofree != NULL)
5286 {
5287 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5288 opt.jo_term_name = fname_tofree;
5289 }
5290 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005291
Bram Moolenaar87abab92019-06-03 21:14:59 +02005292 if (opt.jo_bufnr_buf != NULL)
5293 {
5294 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5295
5296 // With "bufnr" argument: enter the window with this buffer and make it
5297 // empty.
5298 if (wp == NULL)
5299 semsg(_(e_invarg2), "bufnr");
5300 else
5301 {
5302 buf = curbuf;
5303 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005304 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005305 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005306 redraw_later(NOT_VALID);
5307 }
5308 }
5309 else
5310 // Create a new terminal window.
5311 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5312
Bram Moolenaard96ff162018-02-18 22:13:29 +01005313 if (buf != NULL && buf->b_term != NULL)
5314 {
5315 int i;
5316 linenr_T bot_lnum;
5317 linenr_T lnum;
5318 term_T *term = buf->b_term;
5319 int width;
5320 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005321 VTermPos cursor_pos1;
5322 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005323
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005324 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005325
Bram Moolenaard96ff162018-02-18 22:13:29 +01005326 rettv->vval.v_number = buf->b_fnum;
5327
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005328 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005329 width = read_dump_file(fd1, &cursor_pos1);
5330
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005331 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005332 if (cursor_pos1.row >= 0)
5333 {
5334 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5335 coladvance(cursor_pos1.col);
5336 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005337
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005338 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005339 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005340
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005341 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005342 if (!do_diff)
5343 goto theend;
5344
5345 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5346
Bram Moolenaar4a696342018-04-05 18:45:26 +02005347 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005348 if (textline == NULL)
5349 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005350 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5351 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5352 vim_free(textline);
5353
5354 textline = get_separator(width, fname2);
5355 if (textline == NULL)
5356 goto theend;
5357 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5358 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005359 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005360
5361 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005362 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005363 if (width2 > width)
5364 {
5365 vim_free(textline);
5366 textline = alloc(width2 + 1);
5367 if (textline == NULL)
5368 goto theend;
5369 width = width2;
5370 textline[width] = NUL;
5371 }
5372 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5373
5374 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5375 {
5376 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5377 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005378 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005379 for (i = 0; i < width; ++i)
5380 textline[i] = '-';
5381 }
5382 else
5383 {
5384 char_u *line1;
5385 char_u *line2;
5386 char_u *p1;
5387 char_u *p2;
5388 int col;
5389 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5390 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5391 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5392 ->sb_cells;
5393
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005394 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005395 line1 = vim_strsave(ml_get(lnum));
5396 if (line1 == NULL)
5397 break;
5398 p1 = line1;
5399
5400 line2 = ml_get(lnum + bot_lnum);
5401 p2 = line2;
5402 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5403 {
5404 int len1 = utfc_ptr2len(p1);
5405 int len2 = utfc_ptr2len(p2);
5406
5407 textline[col] = ' ';
5408 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005409 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005410 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005411 else if (lnum == cursor_pos1.row + 1
5412 && col == cursor_pos1.col
5413 && (cursor_pos1.row != cursor_pos2.row
5414 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005415 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005416 textline[col] = '>';
5417 else if (lnum == cursor_pos2.row + 1
5418 && col == cursor_pos2.col
5419 && (cursor_pos1.row != cursor_pos2.row
5420 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005421 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005422 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005423 else if (cellattr1 != NULL && cellattr2 != NULL)
5424 {
5425 if ((cellattr1 + col)->width
5426 != (cellattr2 + col)->width)
5427 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005428 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005429 &(cellattr2 + col)->fg))
5430 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005431 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005432 &(cellattr2 + col)->bg))
5433 textline[col] = 'b';
5434 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5435 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5436 textline[col] = 'a';
5437 }
5438 p1 += len1;
5439 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005440 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005441 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005442
5443 while (col < width)
5444 {
5445 if (*p1 == NUL && *p2 == NUL)
5446 textline[col] = '?';
5447 else if (*p1 == NUL)
5448 {
5449 textline[col] = '+';
5450 p2 += utfc_ptr2len(p2);
5451 }
5452 else
5453 {
5454 textline[col] = '-';
5455 p1 += utfc_ptr2len(p1);
5456 }
5457 ++col;
5458 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005459
5460 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005461 }
5462 if (add_empty_scrollback(term, &term->tl_default_color,
5463 term->tl_top_diff_rows) == OK)
5464 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5465 ++bot_lnum;
5466 }
5467
5468 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5469 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005470 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005471 for (i = 0; i < width; ++i)
5472 textline[i] = '+';
5473 if (add_empty_scrollback(term, &term->tl_default_color,
5474 term->tl_top_diff_rows) == OK)
5475 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5476 ++lnum;
5477 ++bot_lnum;
5478 }
5479
5480 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005481
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005482 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005483 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005484 }
5485
5486theend:
5487 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005488 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005489 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005490 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005491 fclose(fd2);
5492}
5493
5494/*
5495 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5496 * bottom files.
5497 * Return FAIL when this is not possible.
5498 */
5499 int
5500term_swap_diff()
5501{
5502 term_T *term = curbuf->b_term;
5503 linenr_T line_count;
5504 linenr_T top_rows;
5505 linenr_T bot_rows;
5506 linenr_T bot_start;
5507 linenr_T lnum;
5508 char_u *p;
5509 sb_line_T *sb_line;
5510
5511 if (term == NULL
5512 || !term_is_finished(curbuf)
5513 || term->tl_top_diff_rows == 0
5514 || term->tl_scrollback.ga_len == 0)
5515 return FAIL;
5516
5517 line_count = curbuf->b_ml.ml_line_count;
5518 top_rows = term->tl_top_diff_rows;
5519 bot_rows = term->tl_bot_diff_rows;
5520 bot_start = line_count - bot_rows;
5521 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5522
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005523 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005524 for (lnum = 1; lnum <= top_rows; ++lnum)
5525 {
5526 p = vim_strsave(ml_get(1));
5527 if (p == NULL)
5528 return OK;
5529 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005530 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005531 vim_free(p);
5532 }
5533
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005534 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005535 for (lnum = 1; lnum <= bot_rows; ++lnum)
5536 {
5537 p = vim_strsave(ml_get(bot_start + lnum));
5538 if (p == NULL)
5539 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005540 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005541 ml_append(lnum - 1, p, 0, FALSE);
5542 vim_free(p);
5543 }
5544
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005545 // move top title to bottom
5546 p = vim_strsave(ml_get(bot_rows + 1));
5547 if (p == NULL)
5548 return OK;
5549 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005550 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005551 vim_free(p);
5552
5553 // move bottom title to top
5554 p = vim_strsave(ml_get(line_count - top_rows));
5555 if (p == NULL)
5556 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005557 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005558 ml_append(bot_rows, p, 0, FALSE);
5559 vim_free(p);
5560
Bram Moolenaard96ff162018-02-18 22:13:29 +01005561 if (top_rows == bot_rows)
5562 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005563 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005564 for (lnum = 0; lnum < top_rows; ++lnum)
5565 {
5566 sb_line_T temp;
5567
5568 temp = *(sb_line + lnum);
5569 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5570 *(sb_line + bot_start + lnum) = temp;
5571 }
5572 }
5573 else
5574 {
5575 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005576 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005577
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005578 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005579 if (temp != NULL)
5580 {
5581 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5582 mch_memmove(term->tl_scrollback.ga_data,
5583 temp + bot_start,
5584 sizeof(sb_line_T) * bot_rows);
5585 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5586 temp + top_rows,
5587 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5588 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5589 + line_count - top_rows,
5590 temp,
5591 sizeof(sb_line_T) * top_rows);
5592 vim_free(temp);
5593 }
5594 }
5595
5596 term->tl_top_diff_rows = bot_rows;
5597 term->tl_bot_diff_rows = top_rows;
5598
5599 update_screen(NOT_VALID);
5600 return OK;
5601}
5602
5603/*
5604 * "term_dumpdiff(filename, filename, options)" function
5605 */
5606 void
5607f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5608{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005609 if (in_vim9script()
5610 && (check_for_string_arg(argvars, 0) == FAIL
5611 || check_for_string_arg(argvars, 1) == FAIL
5612 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5613 return;
5614
Bram Moolenaard96ff162018-02-18 22:13:29 +01005615 term_load_dump(argvars, rettv, TRUE);
5616}
5617
5618/*
5619 * "term_dumpload(filename, options)" function
5620 */
5621 void
5622f_term_dumpload(typval_T *argvars, typval_T *rettv)
5623{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005624 if (in_vim9script()
5625 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005626 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005627 return;
5628
Bram Moolenaard96ff162018-02-18 22:13:29 +01005629 term_load_dump(argvars, rettv, FALSE);
5630}
5631
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005632/*
5633 * "term_getaltscreen(buf)" function
5634 */
5635 void
5636f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5637{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005638 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005639
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005640 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5641 return;
5642
5643 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005644 if (buf == NULL)
5645 return;
5646 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5647}
5648
5649/*
5650 * "term_getattr(attr, name)" function
5651 */
5652 void
5653f_term_getattr(typval_T *argvars, typval_T *rettv)
5654{
5655 int attr;
5656 size_t i;
5657 char_u *name;
5658
5659 static struct {
5660 char *name;
5661 int attr;
5662 } attrs[] = {
5663 {"bold", HL_BOLD},
5664 {"italic", HL_ITALIC},
5665 {"underline", HL_UNDERLINE},
5666 {"strike", HL_STRIKETHROUGH},
5667 {"reverse", HL_INVERSE},
5668 };
5669
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005670 if (in_vim9script()
5671 && (check_for_number_arg(argvars, 0) == FAIL
5672 || check_for_string_arg(argvars, 1) == FAIL))
5673 return;
5674
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005675 attr = tv_get_number(&argvars[0]);
5676 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005677 if (name == NULL)
5678 return;
5679
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005680 if (attr > HL_ALL)
5681 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02005682 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005683 if (STRCMP(name, attrs[i].name) == 0)
5684 {
5685 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5686 break;
5687 }
5688}
5689
5690/*
5691 * "term_getcursor(buf)" function
5692 */
5693 void
5694f_term_getcursor(typval_T *argvars, typval_T *rettv)
5695{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005696 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005697 term_T *term;
5698 list_T *l;
5699 dict_T *d;
5700
5701 if (rettv_list_alloc(rettv) == FAIL)
5702 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005703
5704 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5705 return;
5706
5707 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005708 if (buf == NULL)
5709 return;
5710 term = buf->b_term;
5711
5712 l = rettv->vval.v_list;
5713 list_append_number(l, term->tl_cursor_pos.row + 1);
5714 list_append_number(l, term->tl_cursor_pos.col + 1);
5715
5716 d = dict_alloc();
5717 if (d != NULL)
5718 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005719 dict_add_number(d, "visible", term->tl_cursor_visible);
5720 dict_add_number(d, "blink", blink_state_is_inverted()
5721 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5722 dict_add_number(d, "shape", term->tl_cursor_shape);
5723 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005724 list_append_dict(l, d);
5725 }
5726}
5727
5728/*
5729 * "term_getjob(buf)" function
5730 */
5731 void
5732f_term_getjob(typval_T *argvars, typval_T *rettv)
5733{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005734 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005735
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005736 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5737 return;
5738
5739 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005740 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005741 {
5742 rettv->v_type = VAR_SPECIAL;
5743 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005744 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005745 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005746
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005747 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005748 rettv->vval.v_job = buf->b_term->tl_job;
5749 if (rettv->vval.v_job != NULL)
5750 ++rettv->vval.v_job->jv_refcount;
5751}
5752
5753 static int
5754get_row_number(typval_T *tv, term_T *term)
5755{
5756 if (tv->v_type == VAR_STRING
5757 && tv->vval.v_string != NULL
5758 && STRCMP(tv->vval.v_string, ".") == 0)
5759 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005760 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005761}
5762
5763/*
5764 * "term_getline(buf, row)" function
5765 */
5766 void
5767f_term_getline(typval_T *argvars, typval_T *rettv)
5768{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005769 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005770 term_T *term;
5771 int row;
5772
5773 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005774
5775 if (in_vim9script()
5776 && (check_for_buffer_arg(argvars, 0) == FAIL
5777 || check_for_lnum_arg(argvars, 1) == FAIL))
5778 return;
5779
5780 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005781 if (buf == NULL)
5782 return;
5783 term = buf->b_term;
5784 row = get_row_number(&argvars[1], term);
5785
5786 if (term->tl_vterm == NULL)
5787 {
5788 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5789
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005790 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005791 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5792 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5793 }
5794 else
5795 {
5796 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5797 VTermRect rect;
5798 int len;
5799 char_u *p;
5800
5801 if (row < 0 || row >= term->tl_rows)
5802 return;
5803 len = term->tl_cols * MB_MAXBYTES + 1;
5804 p = alloc(len);
5805 if (p == NULL)
5806 return;
5807 rettv->vval.v_string = p;
5808
5809 rect.start_col = 0;
5810 rect.end_col = term->tl_cols;
5811 rect.start_row = row;
5812 rect.end_row = row + 1;
5813 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5814 }
5815}
5816
5817/*
5818 * "term_getscrolled(buf)" function
5819 */
5820 void
5821f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5822{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005823 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005824
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005825 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5826 return;
5827
5828 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005829 if (buf == NULL)
5830 return;
5831 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5832}
5833
5834/*
5835 * "term_getsize(buf)" function
5836 */
5837 void
5838f_term_getsize(typval_T *argvars, typval_T *rettv)
5839{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005840 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005841 list_T *l;
5842
5843 if (rettv_list_alloc(rettv) == FAIL)
5844 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005845
5846 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5847 return;
5848
5849 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005850 if (buf == NULL)
5851 return;
5852
5853 l = rettv->vval.v_list;
5854 list_append_number(l, buf->b_term->tl_rows);
5855 list_append_number(l, buf->b_term->tl_cols);
5856}
5857
5858/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005859 * "term_setsize(buf, rows, cols)" function
5860 */
5861 void
5862f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5863{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005864 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02005865 term_T *term;
5866 varnumber_T rows, cols;
5867
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005868 if (in_vim9script()
5869 && (check_for_buffer_arg(argvars, 0) == FAIL
5870 || check_for_number_arg(argvars, 1) == FAIL
5871 || check_for_number_arg(argvars, 2) == FAIL))
5872 return;
5873
5874 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005875 if (buf == NULL)
5876 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005877 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005878 return;
5879 }
5880 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005881 return;
5882 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005883 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005884 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005885 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005886 cols = cols <= 0 ? term->tl_cols : cols;
5887 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005888 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005889
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005890 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005891 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5892 term_report_winsize(term, term->tl_rows, term->tl_cols);
5893}
5894
5895/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005896 * "term_getstatus(buf)" function
5897 */
5898 void
5899f_term_getstatus(typval_T *argvars, typval_T *rettv)
5900{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005901 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005902 term_T *term;
5903 char_u val[100];
5904
5905 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005906
5907 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5908 return;
5909
5910 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005911 if (buf == NULL)
5912 return;
5913 term = buf->b_term;
5914
5915 if (term_job_running(term))
5916 STRCPY(val, "running");
5917 else
5918 STRCPY(val, "finished");
5919 if (term->tl_normal_mode)
5920 STRCAT(val, ",normal");
5921 rettv->vval.v_string = vim_strsave(val);
5922}
5923
5924/*
5925 * "term_gettitle(buf)" function
5926 */
5927 void
5928f_term_gettitle(typval_T *argvars, typval_T *rettv)
5929{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005930 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005931
5932 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005933
5934 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5935 return;
5936
5937 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005938 if (buf == NULL)
5939 return;
5940
5941 if (buf->b_term->tl_title != NULL)
5942 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5943}
5944
5945/*
5946 * "term_gettty(buf)" function
5947 */
5948 void
5949f_term_gettty(typval_T *argvars, typval_T *rettv)
5950{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005951 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005952 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005953 int num = 0;
5954
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005955 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005956 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005957 || check_for_opt_bool_arg(argvars, 1) == FAIL))
5958 return;
5959
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005960 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005961 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005962 if (buf == NULL)
5963 return;
5964 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02005965 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005966
5967 switch (num)
5968 {
5969 case 0:
5970 if (buf->b_term->tl_job != NULL)
5971 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005972 break;
5973 case 1:
5974 if (buf->b_term->tl_job != NULL)
5975 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005976 break;
5977 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005978 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005979 return;
5980 }
5981 if (p != NULL)
5982 rettv->vval.v_string = vim_strsave(p);
5983}
5984
5985/*
5986 * "term_list()" function
5987 */
5988 void
5989f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
5990{
5991 term_T *tp;
5992 list_T *l;
5993
5994 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
5995 return;
5996
5997 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005998 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02005999 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006000 if (list_append_number(l,
6001 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6002 return;
6003}
6004
6005/*
6006 * "term_scrape(buf, row)" function
6007 */
6008 void
6009f_term_scrape(typval_T *argvars, typval_T *rettv)
6010{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006011 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006012 VTermScreen *screen = NULL;
6013 VTermPos pos;
6014 list_T *l;
6015 term_T *term;
6016 char_u *p;
6017 sb_line_T *line;
6018
6019 if (rettv_list_alloc(rettv) == FAIL)
6020 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006021
6022 if (in_vim9script()
6023 && (check_for_buffer_arg(argvars, 0) == FAIL
6024 || check_for_lnum_arg(argvars, 1) == FAIL))
6025 return;
6026
6027 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006028 if (buf == NULL)
6029 return;
6030 term = buf->b_term;
6031
6032 l = rettv->vval.v_list;
6033 pos.row = get_row_number(&argvars[1], term);
6034
6035 if (term->tl_vterm != NULL)
6036 {
6037 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006038 if (screen == NULL) // can't really happen
6039 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006040 p = NULL;
6041 line = NULL;
6042 }
6043 else
6044 {
6045 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6046
6047 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6048 return;
6049 p = ml_get_buf(buf, lnum + 1, FALSE);
6050 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6051 }
6052
6053 for (pos.col = 0; pos.col < term->tl_cols; )
6054 {
6055 dict_T *dcell;
6056 int width;
6057 VTermScreenCellAttrs attrs;
6058 VTermColor fg, bg;
6059 char_u rgb[8];
6060 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6061 int off = 0;
6062 int i;
6063
6064 if (screen == NULL)
6065 {
6066 cellattr_T *cellattr;
6067 int len;
6068
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006069 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006070 if (pos.col >= line->sb_cols)
6071 break;
6072 cellattr = line->sb_cells + pos.col;
6073 width = cellattr->width;
6074 attrs = cellattr->attrs;
6075 fg = cellattr->fg;
6076 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006077 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006078 mch_memmove(mbs, p, len);
6079 mbs[len] = NUL;
6080 p += len;
6081 }
6082 else
6083 {
6084 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006085
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006086 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6087 break;
6088 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6089 {
6090 if (cell.chars[i] == 0)
6091 break;
6092 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6093 }
6094 mbs[off] = NUL;
6095 width = cell.width;
6096 attrs = cell.attrs;
6097 fg = cell.fg;
6098 bg = cell.bg;
6099 }
6100 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006101 if (dcell == NULL)
6102 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006103 list_append_dict(l, dcell);
6104
Bram Moolenaare0be1672018-07-08 16:50:37 +02006105 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006106
6107 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6108 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006109 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006110 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6111 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006112 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006113
Bram Moolenaar83d47902020-03-26 20:34:00 +01006114 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006115 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006116
6117 ++pos.col;
6118 if (width == 2)
6119 ++pos.col;
6120 }
6121}
6122
6123/*
6124 * "term_sendkeys(buf, keys)" function
6125 */
6126 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006127f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006128{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006129 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006130 char_u *msg;
6131 term_T *term;
6132
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006133 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006134 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006135 || check_for_string_arg(argvars, 1) == FAIL))
6136 return;
6137
6138 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006139 if (buf == NULL)
6140 return;
6141
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006142 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006143 if (msg == NULL)
6144 return;
6145 term = buf->b_term;
6146 if (term->tl_vterm == NULL)
6147 return;
6148
6149 while (*msg != NUL)
6150 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006151 int c;
6152
6153 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6154 {
6155 c = TO_SPECIAL(msg[1], msg[2]);
6156 msg += 3;
6157 }
6158 else
6159 {
6160 c = PTR2CHAR(msg);
6161 msg += MB_CPTR2LEN(msg);
6162 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006163 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006164 }
6165}
6166
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006167#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6168/*
6169 * "term_getansicolors(buf)" function
6170 */
6171 void
6172f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6173{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006174 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006175 term_T *term;
6176 VTermState *state;
6177 VTermColor color;
6178 char_u hexbuf[10];
6179 int index;
6180 list_T *list;
6181
6182 if (rettv_list_alloc(rettv) == FAIL)
6183 return;
6184
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006185 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6186 return;
6187
6188 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006189 if (buf == NULL)
6190 return;
6191 term = buf->b_term;
6192 if (term->tl_vterm == NULL)
6193 return;
6194
6195 list = rettv->vval.v_list;
6196 state = vterm_obtain_state(term->tl_vterm);
6197 for (index = 0; index < 16; index++)
6198 {
6199 vterm_state_get_palette_color(state, index, &color);
6200 sprintf((char *)hexbuf, "#%02x%02x%02x",
6201 color.red, color.green, color.blue);
6202 if (list_append_string(list, hexbuf, 7) == FAIL)
6203 return;
6204 }
6205}
6206
6207/*
6208 * "term_setansicolors(buf, list)" function
6209 */
6210 void
6211f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6212{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006213 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006214 term_T *term;
6215
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006216 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006217 && (check_for_buffer_arg(argvars, 0) == FAIL
6218 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006219 return;
6220
6221 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006222 if (buf == NULL)
6223 return;
6224 term = buf->b_term;
6225 if (term->tl_vterm == NULL)
6226 return;
6227
6228 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6229 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006230 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006231 return;
6232 }
6233
6234 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006235 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006236}
6237#endif
6238
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006239/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006240 * "term_setapi(buf, api)" function
6241 */
6242 void
6243f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6244{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006245 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006246 term_T *term;
6247 char_u *api;
6248
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006249 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006250 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006251 || check_for_string_arg(argvars, 1) == FAIL))
6252 return;
6253
6254 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006255 if (buf == NULL)
6256 return;
6257 term = buf->b_term;
6258 vim_free(term->tl_api);
6259 api = tv_get_string_chk(&argvars[1]);
6260 if (api != NULL)
6261 term->tl_api = vim_strsave(api);
6262 else
6263 term->tl_api = NULL;
6264}
6265
6266/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006267 * "term_setrestore(buf, command)" function
6268 */
6269 void
6270f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6271{
6272#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006273 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006274 term_T *term;
6275 char_u *cmd;
6276
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006277 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006278 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006279 || check_for_string_arg(argvars, 1) == FAIL))
6280 return;
6281
6282 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006283 if (buf == NULL)
6284 return;
6285 term = buf->b_term;
6286 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006287 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006288 if (cmd != NULL)
6289 term->tl_command = vim_strsave(cmd);
6290 else
6291 term->tl_command = NULL;
6292#endif
6293}
6294
6295/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006296 * "term_setkill(buf, how)" function
6297 */
6298 void
6299f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6300{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006301 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006302 term_T *term;
6303 char_u *how;
6304
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006305 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006306 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006307 || check_for_string_arg(argvars, 1) == FAIL))
6308 return;
6309
6310 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006311 if (buf == NULL)
6312 return;
6313 term = buf->b_term;
6314 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006315 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006316 if (how != NULL)
6317 term->tl_kill = vim_strsave(how);
6318 else
6319 term->tl_kill = NULL;
6320}
6321
6322/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006323 * "term_start(command, options)" function
6324 */
6325 void
6326f_term_start(typval_T *argvars, typval_T *rettv)
6327{
6328 jobopt_T opt;
6329 buf_T *buf;
6330
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006331 if (in_vim9script()
6332 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6333 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6334 return;
6335
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006336 init_job_options(&opt);
6337 if (argvars[1].v_type != VAR_UNKNOWN
6338 && get_job_options(&argvars[1], &opt,
6339 JO_TIMEOUT_ALL + JO_STOPONEXIT
6340 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6341 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6342 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6343 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006344 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006345 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006346 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006347 return;
6348
Bram Moolenaar13568252018-03-16 20:46:58 +01006349 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006350
6351 if (buf != NULL && buf->b_term != NULL)
6352 rettv->vval.v_number = buf->b_fnum;
6353}
6354
6355/*
6356 * "term_wait" function
6357 */
6358 void
6359f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6360{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006361 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006362
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006363 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006364 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006365 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006366 return;
6367
6368 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006369 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006370 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006371 if (buf->b_term->tl_job == NULL)
6372 {
6373 ch_log(NULL, "term_wait(): no job to wait for");
6374 return;
6375 }
6376 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006377 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006378 return;
6379
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006380 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006381 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006382 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6383 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006384 // The job is dead, keep reading channel I/O until the channel is
6385 // closed. buf->b_term may become NULL if the terminal was closed while
6386 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006387 ch_log(NULL, "term_wait(): waiting for channel to close");
6388 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6389 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006390 term_flush_messages();
6391
Bram Moolenaard45aa552018-05-21 22:50:29 +02006392 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006393 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006394 // If the terminal is closed when the channel is closed the
6395 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006396 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006397 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006398
6399 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006400 }
6401 else
6402 {
6403 long wait = 10L;
6404
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006405 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006406
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006407 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006408 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006409 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006410 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006411
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006412 // Flushing messages on channels is hopefully sufficient.
6413 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006414 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006415 }
6416}
6417
6418/*
6419 * Called when a channel has sent all the lines to a terminal.
6420 * Send a CTRL-D to mark the end of the text.
6421 */
6422 void
6423term_send_eof(channel_T *ch)
6424{
6425 term_T *term;
6426
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006427 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006428 if (term->tl_job == ch->ch_job)
6429 {
6430 if (term->tl_eof_chars != NULL)
6431 {
6432 channel_send(ch, PART_IN, term->tl_eof_chars,
6433 (int)STRLEN(term->tl_eof_chars), NULL);
6434 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6435 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006436# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006437 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006438 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006439 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6440# endif
6441 }
6442}
6443
Bram Moolenaar113e1072019-01-20 15:30:40 +01006444#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006445 job_T *
6446term_getjob(term_T *term)
6447{
6448 return term != NULL ? term->tl_job : NULL;
6449}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006450#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006451
Bram Moolenaar4f974752019-02-17 17:44:42 +01006452# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006453
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006454///////////////////////////////////////
6455// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006456#ifdef PROTO
6457typedef int COORD;
6458typedef int DWORD;
6459typedef int HANDLE;
6460typedef int *DWORD_PTR;
6461typedef int HPCON;
6462typedef int HRESULT;
6463typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006464typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006465typedef int PSIZE_T;
6466typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006467typedef int BOOL;
6468# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006469#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006470
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006471HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6472HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6473HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006474BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6475BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6476void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006477
6478 static int
6479dyn_conpty_init(int verbose)
6480{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006481 static HMODULE hKerneldll = NULL;
6482 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006483 static struct
6484 {
6485 char *name;
6486 FARPROC *ptr;
6487 } conpty_entry[] =
6488 {
6489 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6490 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6491 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6492 {"InitializeProcThreadAttributeList",
6493 (FARPROC*)&pInitializeProcThreadAttributeList},
6494 {"UpdateProcThreadAttribute",
6495 (FARPROC*)&pUpdateProcThreadAttribute},
6496 {"DeleteProcThreadAttributeList",
6497 (FARPROC*)&pDeleteProcThreadAttributeList},
6498 {NULL, NULL}
6499 };
6500
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006501 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006502 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006503 if (verbose)
6504 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006505 return FAIL;
6506 }
6507
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006508 // No need to initialize twice.
6509 if (hKerneldll)
6510 return OK;
6511
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006512 hKerneldll = vimLoadLib("kernel32.dll");
6513 for (i = 0; conpty_entry[i].name != NULL
6514 && conpty_entry[i].ptr != NULL; ++i)
6515 {
6516 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6517 conpty_entry[i].name)) == NULL)
6518 {
6519 if (verbose)
6520 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006521 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006522 return FAIL;
6523 }
6524 }
6525
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006526 return OK;
6527}
6528
6529 static int
6530conpty_term_and_job_init(
6531 term_T *term,
6532 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006533 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006534 jobopt_T *opt,
6535 jobopt_T *orig_opt)
6536{
6537 WCHAR *cmd_wchar = NULL;
6538 WCHAR *cmd_wchar_copy = NULL;
6539 WCHAR *cwd_wchar = NULL;
6540 WCHAR *env_wchar = NULL;
6541 channel_T *channel = NULL;
6542 job_T *job = NULL;
6543 HANDLE jo = NULL;
6544 garray_T ga_cmd, ga_env;
6545 char_u *cmd = NULL;
6546 HRESULT hr;
6547 COORD consize;
6548 SIZE_T breq;
6549 PROCESS_INFORMATION proc_info;
6550 HANDLE i_theirs = NULL;
6551 HANDLE o_theirs = NULL;
6552 HANDLE i_ours = NULL;
6553 HANDLE o_ours = NULL;
6554
6555 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6556 ga_init2(&ga_env, (int)sizeof(char*), 20);
6557
6558 if (argvar->v_type == VAR_STRING)
6559 {
6560 cmd = argvar->vval.v_string;
6561 }
6562 else if (argvar->v_type == VAR_LIST)
6563 {
6564 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6565 goto failed;
6566 cmd = ga_cmd.ga_data;
6567 }
6568 if (cmd == NULL || *cmd == NUL)
6569 {
6570 emsg(_(e_invarg));
6571 goto failed;
6572 }
6573
6574 term->tl_arg0_cmd = vim_strsave(cmd);
6575
6576 cmd_wchar = enc_to_utf16(cmd, NULL);
6577
6578 if (cmd_wchar != NULL)
6579 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006580 // Request by CreateProcessW
6581 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006582 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006583 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6584 }
6585
6586 ga_clear(&ga_cmd);
6587 if (cmd_wchar == NULL)
6588 goto failed;
6589 if (opt->jo_cwd != NULL)
6590 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6591
6592 win32_build_env(opt->jo_env, &ga_env, TRUE);
6593 env_wchar = ga_env.ga_data;
6594
6595 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6596 goto failed;
6597 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6598 goto failed;
6599
6600 consize.X = term->tl_cols;
6601 consize.Y = term->tl_rows;
6602 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6603 &term->tl_conpty);
6604 if (FAILED(hr))
6605 goto failed;
6606
6607 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6608
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006609 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006610 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006611 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006612 if (!term->tl_siex.lpAttributeList)
6613 goto failed;
6614 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6615 0, &breq))
6616 goto failed;
6617 if (!pUpdateProcThreadAttribute(
6618 term->tl_siex.lpAttributeList, 0,
6619 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6620 sizeof(HPCON), NULL, NULL))
6621 goto failed;
6622
6623 channel = add_channel();
6624 if (channel == NULL)
6625 goto failed;
6626
6627 job = job_alloc();
6628 if (job == NULL)
6629 goto failed;
6630 if (argvar->v_type == VAR_STRING)
6631 {
6632 int argc;
6633
6634 build_argv_from_string(cmd, &job->jv_argv, &argc);
6635 }
6636 else
6637 {
6638 int argc;
6639
6640 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6641 }
6642
6643 if (opt->jo_set & JO_IN_BUF)
6644 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6645
6646 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6647 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006648 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006649 env_wchar, cwd_wchar,
6650 &term->tl_siex.StartupInfo, &proc_info))
6651 goto failed;
6652
6653 CloseHandle(i_theirs);
6654 CloseHandle(o_theirs);
6655
6656 channel_set_pipes(channel,
6657 (sock_T)i_ours,
6658 (sock_T)o_ours,
6659 (sock_T)o_ours);
6660
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006661 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006662 channel->ch_write_text_mode = TRUE;
6663
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006664 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006665 channel->ch_anonymous_pipe = TRUE;
6666
6667 jo = CreateJobObject(NULL, NULL);
6668 if (jo == NULL)
6669 goto failed;
6670
6671 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6672 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006673 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006674 CloseHandle(jo);
6675 jo = NULL;
6676 }
6677
6678 ResumeThread(proc_info.hThread);
6679 CloseHandle(proc_info.hThread);
6680
6681 vim_free(cmd_wchar);
6682 vim_free(cmd_wchar_copy);
6683 vim_free(cwd_wchar);
6684 vim_free(env_wchar);
6685
6686 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6687 goto failed;
6688
6689#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6690 if (opt->jo_set2 & JO2_ANSI_COLORS)
6691 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6692 else
6693 init_vterm_ansi_colors(term->tl_vterm);
6694#endif
6695
6696 channel_set_job(channel, job, opt);
6697 job_set_options(job, opt);
6698
6699 job->jv_channel = channel;
6700 job->jv_proc_info = proc_info;
6701 job->jv_job_object = jo;
6702 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006703 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006704 ++job->jv_refcount;
6705 term->tl_job = job;
6706
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006707 // Redirecting stdout and stderr doesn't work at the job level. Instead
6708 // open the file here and handle it in. opt->jo_io was changed in
6709 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006710 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6711 {
6712 char_u *fname = opt->jo_io_name[PART_OUT];
6713
6714 ch_log(channel, "Opening output file %s", fname);
6715 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6716 if (term->tl_out_fd == NULL)
6717 semsg(_(e_notopen), fname);
6718 }
6719
6720 return OK;
6721
6722failed:
6723 ga_clear(&ga_cmd);
6724 ga_clear(&ga_env);
6725 vim_free(cmd_wchar);
6726 vim_free(cmd_wchar_copy);
6727 vim_free(cwd_wchar);
6728 if (channel != NULL)
6729 channel_clear(channel);
6730 if (job != NULL)
6731 {
6732 job->jv_channel = NULL;
6733 job_cleanup(job);
6734 }
6735 term->tl_job = NULL;
6736 if (jo != NULL)
6737 CloseHandle(jo);
6738
6739 if (term->tl_siex.lpAttributeList != NULL)
6740 {
6741 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6742 vim_free(term->tl_siex.lpAttributeList);
6743 }
6744 term->tl_siex.lpAttributeList = NULL;
6745 if (o_theirs != NULL)
6746 CloseHandle(o_theirs);
6747 if (o_ours != NULL)
6748 CloseHandle(o_ours);
6749 if (i_ours != NULL)
6750 CloseHandle(i_ours);
6751 if (i_theirs != NULL)
6752 CloseHandle(i_theirs);
6753 if (term->tl_conpty != NULL)
6754 pClosePseudoConsole(term->tl_conpty);
6755 term->tl_conpty = NULL;
6756 return FAIL;
6757}
6758
6759 static void
6760conpty_term_report_winsize(term_T *term, int rows, int cols)
6761{
6762 COORD consize;
6763
6764 consize.X = cols;
6765 consize.Y = rows;
6766 pResizePseudoConsole(term->tl_conpty, consize);
6767}
6768
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006769 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006770term_free_conpty(term_T *term)
6771{
6772 if (term->tl_siex.lpAttributeList != NULL)
6773 {
6774 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6775 vim_free(term->tl_siex.lpAttributeList);
6776 }
6777 term->tl_siex.lpAttributeList = NULL;
6778 if (term->tl_conpty != NULL)
6779 pClosePseudoConsole(term->tl_conpty);
6780 term->tl_conpty = NULL;
6781}
6782
6783 int
6784use_conpty(void)
6785{
6786 return has_conpty;
6787}
6788
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006789# ifndef PROTO
6790
6791#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6792#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006793#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006794
6795void* (*winpty_config_new)(UINT64, void*);
6796void* (*winpty_open)(void*, void*);
6797void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6798BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6799void (*winpty_config_set_mouse_mode)(void*, int);
6800void (*winpty_config_set_initial_size)(void*, int, int);
6801LPCWSTR (*winpty_conin_name)(void*);
6802LPCWSTR (*winpty_conout_name)(void*);
6803LPCWSTR (*winpty_conerr_name)(void*);
6804void (*winpty_free)(void*);
6805void (*winpty_config_free)(void*);
6806void (*winpty_spawn_config_free)(void*);
6807void (*winpty_error_free)(void*);
6808LPCWSTR (*winpty_error_msg)(void*);
6809BOOL (*winpty_set_size)(void*, int, int, void*);
6810HANDLE (*winpty_agent_process)(void*);
6811
6812#define WINPTY_DLL "winpty.dll"
6813
6814static HINSTANCE hWinPtyDLL = NULL;
6815# endif
6816
6817 static int
6818dyn_winpty_init(int verbose)
6819{
6820 int i;
6821 static struct
6822 {
6823 char *name;
6824 FARPROC *ptr;
6825 } winpty_entry[] =
6826 {
6827 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6828 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6829 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6830 {"winpty_config_set_mouse_mode",
6831 (FARPROC*)&winpty_config_set_mouse_mode},
6832 {"winpty_config_set_initial_size",
6833 (FARPROC*)&winpty_config_set_initial_size},
6834 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6835 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6836 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6837 {"winpty_free", (FARPROC*)&winpty_free},
6838 {"winpty_open", (FARPROC*)&winpty_open},
6839 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6840 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6841 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6842 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6843 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6844 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6845 {NULL, NULL}
6846 };
6847
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006848 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006849 if (hWinPtyDLL)
6850 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006851 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6852 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006853 if (*p_winptydll != NUL)
6854 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6855 if (!hWinPtyDLL)
6856 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6857 if (!hWinPtyDLL)
6858 {
6859 if (verbose)
Martin Tournoij1a3e5742021-07-24 13:57:29 +02006860 semsg(_(e_loadlib),
6861 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
6862 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006863 return FAIL;
6864 }
6865 for (i = 0; winpty_entry[i].name != NULL
6866 && winpty_entry[i].ptr != NULL; ++i)
6867 {
6868 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6869 winpty_entry[i].name)) == NULL)
6870 {
6871 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006872 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006873 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006874 return FAIL;
6875 }
6876 }
6877
6878 return OK;
6879}
6880
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006881 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006882winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006883 term_T *term,
6884 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006885 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006886 jobopt_T *opt,
6887 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006888{
6889 WCHAR *cmd_wchar = NULL;
6890 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006891 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006892 channel_T *channel = NULL;
6893 job_T *job = NULL;
6894 DWORD error;
6895 HANDLE jo = NULL;
6896 HANDLE child_process_handle;
6897 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006898 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006899 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006900 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006901 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006902
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006903 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6904 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006905
6906 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006907 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006908 cmd = argvar->vval.v_string;
6909 }
6910 else if (argvar->v_type == VAR_LIST)
6911 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006912 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006913 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006914 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006915 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006916 if (cmd == NULL || *cmd == NUL)
6917 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006918 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006919 goto failed;
6920 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006921
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006922 term->tl_arg0_cmd = vim_strsave(cmd);
6923
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006924 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006925 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006926 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006927 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006928 if (opt->jo_cwd != NULL)
6929 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006930
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006931 win32_build_env(opt->jo_env, &ga_env, TRUE);
6932 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006933
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006934 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6935 if (term->tl_winpty_config == NULL)
6936 goto failed;
6937
6938 winpty_config_set_mouse_mode(term->tl_winpty_config,
6939 WINPTY_MOUSE_MODE_FORCE);
6940 winpty_config_set_initial_size(term->tl_winpty_config,
6941 term->tl_cols, term->tl_rows);
6942 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6943 if (term->tl_winpty == NULL)
6944 goto failed;
6945
6946 spawn_config = winpty_spawn_config_new(
6947 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6948 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6949 NULL,
6950 cmd_wchar,
6951 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006952 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006953 &winpty_err);
6954 if (spawn_config == NULL)
6955 goto failed;
6956
6957 channel = add_channel();
6958 if (channel == NULL)
6959 goto failed;
6960
6961 job = job_alloc();
6962 if (job == NULL)
6963 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006964 if (argvar->v_type == VAR_STRING)
6965 {
6966 int argc;
6967
6968 build_argv_from_string(cmd, &job->jv_argv, &argc);
6969 }
6970 else
6971 {
6972 int argc;
6973
6974 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6975 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006976
6977 if (opt->jo_set & JO_IN_BUF)
6978 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6979
6980 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
6981 &child_thread_handle, &error, &winpty_err))
6982 goto failed;
6983
6984 channel_set_pipes(channel,
6985 (sock_T)CreateFileW(
6986 winpty_conin_name(term->tl_winpty),
6987 GENERIC_WRITE, 0, NULL,
6988 OPEN_EXISTING, 0, NULL),
6989 (sock_T)CreateFileW(
6990 winpty_conout_name(term->tl_winpty),
6991 GENERIC_READ, 0, NULL,
6992 OPEN_EXISTING, 0, NULL),
6993 (sock_T)CreateFileW(
6994 winpty_conerr_name(term->tl_winpty),
6995 GENERIC_READ, 0, NULL,
6996 OPEN_EXISTING, 0, NULL));
6997
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006998 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006999 channel->ch_write_text_mode = TRUE;
7000
7001 jo = CreateJobObject(NULL, NULL);
7002 if (jo == NULL)
7003 goto failed;
7004
7005 if (!AssignProcessToJobObject(jo, child_process_handle))
7006 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007007 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007008 CloseHandle(jo);
7009 jo = NULL;
7010 }
7011
7012 winpty_spawn_config_free(spawn_config);
7013 vim_free(cmd_wchar);
7014 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007015 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007016
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007017 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7018 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007019
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007020#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7021 if (opt->jo_set2 & JO2_ANSI_COLORS)
7022 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7023 else
7024 init_vterm_ansi_colors(term->tl_vterm);
7025#endif
7026
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007027 channel_set_job(channel, job, opt);
7028 job_set_options(job, opt);
7029
7030 job->jv_channel = channel;
7031 job->jv_proc_info.hProcess = child_process_handle;
7032 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7033 job->jv_job_object = jo;
7034 job->jv_status = JOB_STARTED;
7035 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007036 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007037 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007038 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007039 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007040 ++job->jv_refcount;
7041 term->tl_job = job;
7042
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007043 // Redirecting stdout and stderr doesn't work at the job level. Instead
7044 // open the file here and handle it in. opt->jo_io was changed in
7045 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007046 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7047 {
7048 char_u *fname = opt->jo_io_name[PART_OUT];
7049
7050 ch_log(channel, "Opening output file %s", fname);
7051 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7052 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007053 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007054 }
7055
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007056 return OK;
7057
7058failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007059 ga_clear(&ga_cmd);
7060 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007061 vim_free(cmd_wchar);
7062 vim_free(cwd_wchar);
7063 if (spawn_config != NULL)
7064 winpty_spawn_config_free(spawn_config);
7065 if (channel != NULL)
7066 channel_clear(channel);
7067 if (job != NULL)
7068 {
7069 job->jv_channel = NULL;
7070 job_cleanup(job);
7071 }
7072 term->tl_job = NULL;
7073 if (jo != NULL)
7074 CloseHandle(jo);
7075 if (term->tl_winpty != NULL)
7076 winpty_free(term->tl_winpty);
7077 term->tl_winpty = NULL;
7078 if (term->tl_winpty_config != NULL)
7079 winpty_config_free(term->tl_winpty_config);
7080 term->tl_winpty_config = NULL;
7081 if (winpty_err != NULL)
7082 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007083 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007084 (short_u *)winpty_error_msg(winpty_err), NULL);
7085
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007086 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007087 winpty_error_free(winpty_err);
7088 }
7089 return FAIL;
7090}
7091
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007092/*
7093 * Create a new terminal of "rows" by "cols" cells.
7094 * Store a reference in "term".
7095 * Return OK or FAIL.
7096 */
7097 static int
7098term_and_job_init(
7099 term_T *term,
7100 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007101 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007102 jobopt_T *opt,
7103 jobopt_T *orig_opt)
7104{
7105 int use_winpty = FALSE;
7106 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007107 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007108
7109 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7110 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7111
7112 if (!has_winpty && !has_conpty)
7113 // If neither is available give the errors for winpty, since when
7114 // conpty is not available it can't be installed either.
7115 return dyn_winpty_init(TRUE);
7116
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007117 if (opt->jo_tty_type != NUL)
7118 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007119
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007120 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007121 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007122 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007123 use_conpty = TRUE;
7124 else if (has_winpty)
7125 use_winpty = TRUE;
7126 // else: error
7127 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007128 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007129 {
7130 if (has_winpty)
7131 use_winpty = TRUE;
7132 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007133 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007134 {
7135 if (has_conpty)
7136 use_conpty = TRUE;
7137 else
7138 return dyn_conpty_init(TRUE);
7139 }
7140
7141 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007142 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007143
7144 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007145 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007146
7147 // error
7148 return dyn_winpty_init(TRUE);
7149}
7150
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007151 static int
7152create_pty_only(term_T *term, jobopt_T *options)
7153{
7154 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7155 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7156 char in_name[80], out_name[80];
7157 channel_T *channel = NULL;
7158
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007159 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7160 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007161
7162 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7163 GetCurrentProcessId(),
7164 curbuf->b_fnum);
7165 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7166 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7167 PIPE_UNLIMITED_INSTANCES,
7168 0, 0, NMPWAIT_NOWAIT, NULL);
7169 if (hPipeIn == INVALID_HANDLE_VALUE)
7170 goto failed;
7171
7172 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7173 GetCurrentProcessId(),
7174 curbuf->b_fnum);
7175 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7176 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7177 PIPE_UNLIMITED_INSTANCES,
7178 0, 0, 0, NULL);
7179 if (hPipeOut == INVALID_HANDLE_VALUE)
7180 goto failed;
7181
7182 ConnectNamedPipe(hPipeIn, NULL);
7183 ConnectNamedPipe(hPipeOut, NULL);
7184
7185 term->tl_job = job_alloc();
7186 if (term->tl_job == NULL)
7187 goto failed;
7188 ++term->tl_job->jv_refcount;
7189
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007190 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007191 term->tl_job->jv_status = JOB_FINISHED;
7192
7193 channel = add_channel();
7194 if (channel == NULL)
7195 goto failed;
7196 term->tl_job->jv_channel = channel;
7197 channel->ch_keep_open = TRUE;
7198 channel->ch_named_pipe = TRUE;
7199
7200 channel_set_pipes(channel,
7201 (sock_T)hPipeIn,
7202 (sock_T)hPipeOut,
7203 (sock_T)hPipeOut);
7204 channel_set_job(channel, term->tl_job, options);
7205 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7206 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7207
7208 return OK;
7209
7210failed:
7211 if (hPipeIn != NULL)
7212 CloseHandle(hPipeIn);
7213 if (hPipeOut != NULL)
7214 CloseHandle(hPipeOut);
7215 return FAIL;
7216}
7217
7218/*
7219 * Free the terminal emulator part of "term".
7220 */
7221 static void
7222term_free_vterm(term_T *term)
7223{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007224 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007225 if (term->tl_winpty != NULL)
7226 winpty_free(term->tl_winpty);
7227 term->tl_winpty = NULL;
7228 if (term->tl_winpty_config != NULL)
7229 winpty_config_free(term->tl_winpty_config);
7230 term->tl_winpty_config = NULL;
7231 if (term->tl_vterm != NULL)
7232 vterm_free(term->tl_vterm);
7233 term->tl_vterm = NULL;
7234}
7235
7236/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007237 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007238 */
7239 static void
7240term_report_winsize(term_T *term, int rows, int cols)
7241{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007242 if (term->tl_conpty)
7243 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007244 if (term->tl_winpty)
7245 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7246}
7247
7248 int
7249terminal_enabled(void)
7250{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007251 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007252}
7253
7254# else
7255
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007256///////////////////////////////////////
7257// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007258
7259/*
7260 * Create a new terminal of "rows" by "cols" cells.
7261 * Start job for "cmd".
7262 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007263 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007264 * Return OK or FAIL.
7265 */
7266 static int
7267term_and_job_init(
7268 term_T *term,
7269 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007270 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007271 jobopt_T *opt,
7272 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007273{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007274 term->tl_arg0_cmd = NULL;
7275
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007276 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7277 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007278
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007279#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7280 if (opt->jo_set2 & JO2_ANSI_COLORS)
7281 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7282 else
7283 init_vterm_ansi_colors(term->tl_vterm);
7284#endif
7285
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007286 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007287 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007288 if (term->tl_job != NULL)
7289 ++term->tl_job->jv_refcount;
7290
7291 return term->tl_job != NULL
7292 && term->tl_job->jv_channel != NULL
7293 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7294}
7295
7296 static int
7297create_pty_only(term_T *term, jobopt_T *opt)
7298{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007299 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7300 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007301
7302 term->tl_job = job_alloc();
7303 if (term->tl_job == NULL)
7304 return FAIL;
7305 ++term->tl_job->jv_refcount;
7306
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007307 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007308 term->tl_job->jv_status = JOB_FINISHED;
7309
7310 return mch_create_pty_channel(term->tl_job, opt);
7311}
7312
7313/*
7314 * Free the terminal emulator part of "term".
7315 */
7316 static void
7317term_free_vterm(term_T *term)
7318{
7319 if (term->tl_vterm != NULL)
7320 vterm_free(term->tl_vterm);
7321 term->tl_vterm = NULL;
7322}
7323
7324/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007325 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007326 */
7327 static void
7328term_report_winsize(term_T *term, int rows, int cols)
7329{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007330 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007331 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7332 {
7333 int fd = -1;
7334 int part;
7335
7336 for (part = PART_OUT; part < PART_COUNT; ++part)
7337 {
7338 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007339 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007340 break;
7341 }
7342 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7343 mch_signal_job(term->tl_job, (char_u *)"winch");
7344 }
7345}
7346
7347# endif
7348
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007349#endif // FEAT_TERMINAL