blob: fb8b2881fb0bad8ea732e323065d3d8ce8579387 [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
Bram Moolenaar36968af2021-11-15 17:13:11 +00001133term_write_job_output(term_T *term, char_u *msg_arg, size_t len_arg)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001134{
Bram Moolenaar36968af2021-11-15 17:13:11 +00001135 char_u *msg = msg_arg;
1136 size_t len = len_arg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001137 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001138 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar36968af2021-11-15 17:13:11 +00001139 size_t limit = term->tl_buffer->b_p_twsl * term->tl_cols * 3;
1140
1141 // Limit the length to 'termwinscroll' * cols * 3 bytes. Keep the text at
1142 // the end.
1143 if (len > limit)
1144 {
1145 char_u *p = msg + len - limit;
1146
1147 p -= (*mb_head_off)(msg, p);
1148 len -= p - msg;
1149 msg = p;
1150 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001151
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001152 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001153
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001154 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001155 if (prevlen != vterm_output_get_buffer_current(vterm))
1156 {
1157 char buf[KEY_BUF_LEN];
1158 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1159
1160 if (curlen > 0)
1161 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1162 (char_u *)buf, (int)curlen, NULL);
1163 }
1164
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001165 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001166 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1167}
1168
1169 static void
1170update_cursor(term_T *term, int redraw)
1171{
1172 if (term->tl_normal_mode)
1173 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001174#ifdef FEAT_GUI
1175 if (term->tl_system)
1176 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1177 term->tl_cursor_pos.col);
1178 else
1179#endif
1180 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001181 if (redraw)
1182 {
1183 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1184 cursor_on();
1185 out_flush();
1186#ifdef FEAT_GUI
1187 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001188 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001189 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001190 gui_mch_flush();
1191 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001192#endif
1193 }
1194}
1195
1196/*
1197 * Invoked when "msg" output from a job was received. Write it to the terminal
1198 * of "buffer".
1199 */
1200 void
1201write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1202{
1203 size_t len = STRLEN(msg);
1204 term_T *term = buffer->b_term;
1205
Bram Moolenaar4f974752019-02-17 17:44:42 +01001206#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001207 // Win32: Cannot redirect output of the job, intercept it here and write to
1208 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001209 if (term->tl_out_fd != NULL)
1210 {
1211 ch_log(channel, "Writing %d bytes to output file", (int)len);
1212 fwrite(msg, len, 1, term->tl_out_fd);
1213 return;
1214 }
1215#endif
1216
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001217 if (term->tl_vterm == NULL)
1218 {
1219 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1220 return;
1221 }
1222 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01001223 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001224 term_write_job_output(term, msg, len);
1225
Bram Moolenaar13568252018-03-16 20:46:58 +01001226#ifdef FEAT_GUI
1227 if (term->tl_system)
1228 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001229 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001230 update_system_term(term);
1231 update_cursor(term, TRUE);
1232 }
1233 else
1234#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001235 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1236 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001237 if (!term->tl_normal_mode)
1238 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001239 // Don't use update_screen() when editing the command line, it gets
1240 // cleared.
1241 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001242 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001243 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001244 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001245 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001246 // update_screen() can be slow, check the terminal wasn't closed
1247 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001248 if (buffer == curbuf && curbuf->b_term != NULL)
1249 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001250 }
1251 else
1252 redraw_after_callback(TRUE);
1253 }
1254}
1255
1256/*
1257 * Send a mouse position and click to the vterm
1258 */
1259 static int
1260term_send_mouse(VTerm *vterm, int button, int pressed)
1261{
1262 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001263 int row = mouse_row - W_WINROW(curwin);
1264 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001265
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001266#ifdef FEAT_PROP_POPUP
1267 if (popup_is_popup(curwin))
1268 {
1269 row -= popup_top_extra(curwin);
1270 col -= popup_left_extra(curwin);
1271 }
1272#endif
1273 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001274 if (button != 0)
1275 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001276 return TRUE;
1277}
1278
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001279static int enter_mouse_col = -1;
1280static int enter_mouse_row = -1;
1281
1282/*
1283 * Handle a mouse click, drag or release.
1284 * Return TRUE when a mouse event is sent to the terminal.
1285 */
1286 static int
1287term_mouse_click(VTerm *vterm, int key)
1288{
1289#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001290 // For modeless selection mouse drag and release events are ignored, unless
1291 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001292 static int ignore_drag_release = TRUE;
1293 VTermMouseState mouse_state;
1294
1295 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1296 if (mouse_state.flags == 0)
1297 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001298 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001299 switch (key)
1300 {
1301 case K_LEFTDRAG:
1302 case K_LEFTRELEASE:
1303 case K_RIGHTDRAG:
1304 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001305 // Ignore drag and release events when the button-down wasn't
1306 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001307 if (ignore_drag_release)
1308 {
1309 int save_mouse_col, save_mouse_row;
1310
1311 if (enter_mouse_col < 0)
1312 break;
1313
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001314 // mouse click in the window gave us focus, handle that
1315 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001316 save_mouse_col = mouse_col;
1317 save_mouse_row = mouse_row;
1318 mouse_col = enter_mouse_col;
1319 mouse_row = enter_mouse_row;
1320 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1321 mouse_col = save_mouse_col;
1322 mouse_row = save_mouse_row;
1323 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001324 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001325 case K_LEFTMOUSE:
1326 case K_RIGHTMOUSE:
1327 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1328 ignore_drag_release = TRUE;
1329 else
1330 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001331 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001332 if (clip_star.available)
1333 {
1334 int button, is_click, is_drag;
1335
1336 button = get_mouse_button(KEY2TERMCAP1(key),
1337 &is_click, &is_drag);
1338 if (mouse_model_popup() && button == MOUSE_LEFT
1339 && (mod_mask & MOD_MASK_SHIFT))
1340 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001341 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001342 button = MOUSE_RIGHT;
1343 mod_mask &= ~MOD_MASK_SHIFT;
1344 }
1345 clip_modeless(button, is_click, is_drag);
1346 }
1347 break;
1348
1349 case K_MIDDLEMOUSE:
1350 if (clip_star.available)
1351 insert_reg('*', TRUE);
1352 break;
1353 }
1354 enter_mouse_col = -1;
1355 return FALSE;
1356 }
1357#endif
1358 enter_mouse_col = -1;
1359
1360 switch (key)
1361 {
1362 case K_LEFTMOUSE:
1363 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1364 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1365 case K_LEFTRELEASE:
1366 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1367 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1368 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1369 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1370 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1371 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1372 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1373 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1374 }
1375 return TRUE;
1376}
1377
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001378/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001379 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1380 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001381 * Return the number of bytes in "buf".
1382 */
1383 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001384term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001385{
1386 VTerm *vterm = term->tl_vterm;
1387 VTermKey key = VTERM_KEY_NONE;
1388 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001389 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001390
1391 switch (c)
1392 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001393 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001394
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001395 // don't use VTERM_KEY_BACKSPACE, it always
1396 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001397 case K_BS: c = term_backspace_char; break;
1398
1399 case ESC: key = VTERM_KEY_ESCAPE; break;
1400 case K_DEL: key = VTERM_KEY_DEL; break;
1401 case K_DOWN: key = VTERM_KEY_DOWN; break;
1402 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1403 key = VTERM_KEY_DOWN; break;
1404 case K_END: key = VTERM_KEY_END; break;
1405 case K_S_END: mod = VTERM_MOD_SHIFT;
1406 key = VTERM_KEY_END; break;
1407 case K_C_END: mod = VTERM_MOD_CTRL;
1408 key = VTERM_KEY_END; break;
1409 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1410 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1411 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1412 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1413 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1414 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1415 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1416 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1417 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1418 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1419 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1420 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1421 case K_HOME: key = VTERM_KEY_HOME; break;
1422 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1423 key = VTERM_KEY_HOME; break;
1424 case K_C_HOME: mod = VTERM_MOD_CTRL;
1425 key = VTERM_KEY_HOME; break;
1426 case K_INS: key = VTERM_KEY_INS; break;
1427 case K_K0: key = VTERM_KEY_KP_0; break;
1428 case K_K1: key = VTERM_KEY_KP_1; break;
1429 case K_K2: key = VTERM_KEY_KP_2; break;
1430 case K_K3: key = VTERM_KEY_KP_3; break;
1431 case K_K4: key = VTERM_KEY_KP_4; break;
1432 case K_K5: key = VTERM_KEY_KP_5; break;
1433 case K_K6: key = VTERM_KEY_KP_6; break;
1434 case K_K7: key = VTERM_KEY_KP_7; break;
1435 case K_K8: key = VTERM_KEY_KP_8; break;
1436 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001437 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001438 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001439 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001440 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001441 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1442 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001443 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1444 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001445 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1446 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001447 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1448 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1449 case K_LEFT: key = VTERM_KEY_LEFT; break;
1450 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1451 key = VTERM_KEY_LEFT; break;
1452 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1453 key = VTERM_KEY_LEFT; break;
1454 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1455 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1456 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1457 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1458 key = VTERM_KEY_RIGHT; break;
1459 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1460 key = VTERM_KEY_RIGHT; break;
1461 case K_UP: key = VTERM_KEY_UP; break;
1462 case K_S_UP: mod = VTERM_MOD_SHIFT;
1463 key = VTERM_KEY_UP; break;
1464 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001465 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1466 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001467
Bram Moolenaara42ad572017-11-16 13:08:04 +01001468 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1469 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001470 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1471 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001472
1473 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001474 case K_LEFTMOUSE_NM:
1475 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001476 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001477 case K_LEFTRELEASE_NM:
1478 case K_MOUSEMOVE:
1479 case K_MIDDLEMOUSE:
1480 case K_MIDDLEDRAG:
1481 case K_MIDDLERELEASE:
1482 case K_RIGHTMOUSE:
1483 case K_RIGHTDRAG:
1484 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1485 return 0;
1486 other = TRUE;
1487 break;
1488
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001489 case K_X1MOUSE: /* TODO */ return 0;
1490 case K_X1DRAG: /* TODO */ return 0;
1491 case K_X1RELEASE: /* TODO */ return 0;
1492 case K_X2MOUSE: /* TODO */ return 0;
1493 case K_X2DRAG: /* TODO */ return 0;
1494 case K_X2RELEASE: /* TODO */ return 0;
1495
1496 case K_IGNORE: return 0;
1497 case K_NOP: return 0;
1498 case K_UNDO: return 0;
1499 case K_HELP: return 0;
1500 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1501 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1502 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1503 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1504 case K_SELECT: return 0;
1505#ifdef FEAT_GUI
1506 case K_VER_SCROLLBAR: return 0;
1507 case K_HOR_SCROLLBAR: return 0;
1508#endif
1509#ifdef FEAT_GUI_TABLINE
1510 case K_TABLINE: return 0;
1511 case K_TABMENU: return 0;
1512#endif
1513#ifdef FEAT_NETBEANS_INTG
1514 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1515#endif
1516#ifdef FEAT_DND
1517 case K_DROP: return 0;
1518#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001519 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001520 case K_PS: vterm_keyboard_start_paste(vterm);
1521 other = TRUE;
1522 break;
1523 case K_PE: vterm_keyboard_end_paste(vterm);
1524 other = TRUE;
1525 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001526 }
1527
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001528 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001529 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001530 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001531 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001532 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001533 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001534 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001535
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001536 /*
1537 * Convert special keys to vterm keys:
1538 * - Write keys to vterm: vterm_keyboard_key()
1539 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001540 */
1541 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001542 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001543 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001544 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001545 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001546 vterm_keyboard_unichar(vterm, c, mod);
1547
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001548 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001549 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1550}
1551
1552/*
1553 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001554 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001555 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001556 */
1557 static int
1558term_job_running_check(term_T *term, int check_job_status)
1559{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001560 // Also consider the job finished when the channel is closed, to avoid a
1561 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001562 if (term != NULL
1563 && term->tl_job != NULL
1564 && channel_is_open(term->tl_job->jv_channel))
1565 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001566 job_T *job = term->tl_job;
1567
1568 // Careful: Checking the job status may invoked callbacks, which close
1569 // the buffer and terminate "term". However, "job" will not be freed
1570 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001571 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001572 job_status(job);
1573 return (job->jv_status == JOB_STARTED
1574 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001575 }
1576 return FALSE;
1577}
1578
1579/*
1580 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001581 */
1582 int
1583term_job_running(term_T *term)
1584{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001585 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001586}
1587
1588/*
1589 * Return TRUE if "term" has an active channel and used ":term NONE".
1590 */
1591 int
1592term_none_open(term_T *term)
1593{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001594 // Also consider the job finished when the channel is closed, to avoid a
1595 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001596 return term != NULL
1597 && term->tl_job != NULL
1598 && channel_is_open(term->tl_job->jv_channel)
1599 && term->tl_job->jv_channel->ch_keep_open;
1600}
1601
1602/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001603 * Used when exiting: kill the job in "buf" if so desired.
1604 * Return OK when the job finished.
1605 * Return FAIL when the job is still running.
1606 */
1607 int
1608term_try_stop_job(buf_T *buf)
1609{
1610 int count;
1611 char *how = (char *)buf->b_term->tl_kill;
1612
1613#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001614 if ((how == NULL || *how == NUL)
1615 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001616 {
1617 char_u buff[DIALOG_MSG_SIZE];
1618 int ret;
1619
Bram Moolenaar00806bc2020-11-05 19:36:38 +01001620 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001621 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1622 if (ret == VIM_YES)
1623 how = "kill";
1624 else if (ret == VIM_CANCEL)
1625 return FAIL;
1626 }
1627#endif
1628 if (how == NULL || *how == NUL)
1629 return FAIL;
1630
1631 job_stop(buf->b_term->tl_job, NULL, how);
1632
Bram Moolenaar9172d232019-01-29 23:06:54 +01001633 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001634 for (count = 0; count < 100; ++count)
1635 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001636 job_T *job;
1637
1638 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001639 if (!buf_valid(buf)
1640 || buf->b_term == NULL
1641 || buf->b_term->tl_job == NULL)
1642 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001643 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001644
Bram Moolenaar9172d232019-01-29 23:06:54 +01001645 // Call job_status() to update jv_status. It may cause the job to be
1646 // cleaned up but it won't be freed.
1647 job_status(job);
1648 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001649 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001650
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001651 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001652 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001653 }
1654 return FAIL;
1655}
1656
1657/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001658 * Add the last line of the scrollback buffer to the buffer in the window.
1659 */
1660 static void
1661add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1662{
1663 buf_T *buf = term->tl_buffer;
1664 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1665 linenr_T lnum = buf->b_ml.ml_line_count;
1666
Bram Moolenaar4f974752019-02-17 17:44:42 +01001667#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001668 if (!enc_utf8 && enc_codepage > 0)
1669 {
1670 WCHAR *ret = NULL;
1671 int length = 0;
1672
1673 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1674 &ret, &length);
1675 if (ret != NULL)
1676 {
1677 WideCharToMultiByte_alloc(enc_codepage, 0,
1678 ret, length, (char **)&text, &len, 0, 0);
1679 vim_free(ret);
1680 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1681 vim_free(text);
1682 }
1683 }
1684 else
1685#endif
1686 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1687 if (empty)
1688 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001689 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001690 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001691 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001692 curbuf = curwin->w_buffer;
1693 }
1694}
1695
1696 static void
1697cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1698{
1699 attr->width = cell->width;
1700 attr->attrs = cell->attrs;
1701 attr->fg = cell->fg;
1702 attr->bg = cell->bg;
1703}
1704
1705 static int
1706equal_celattr(cellattr_T *a, cellattr_T *b)
1707{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001708 // We only compare the RGB colors, ignoring the ANSI index and type.
1709 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001710 return a->fg.red == b->fg.red
1711 && a->fg.green == b->fg.green
1712 && a->fg.blue == b->fg.blue
1713 && a->bg.red == b->bg.red
1714 && a->bg.green == b->bg.green
1715 && a->bg.blue == b->bg.blue;
1716}
1717
Bram Moolenaard96ff162018-02-18 22:13:29 +01001718/*
1719 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1720 * line at this position. Otherwise at the end.
1721 */
1722 static int
1723add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1724{
1725 if (ga_grow(&term->tl_scrollback, 1) == OK)
1726 {
1727 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1728 + term->tl_scrollback.ga_len;
1729
1730 if (lnum > 0)
1731 {
1732 int i;
1733
1734 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1735 {
1736 *line = *(line - 1);
1737 --line;
1738 }
1739 }
1740 line->sb_cols = 0;
1741 line->sb_cells = NULL;
1742 line->sb_fill_attr = *fill_attr;
1743 ++term->tl_scrollback.ga_len;
1744 return OK;
1745 }
1746 return FALSE;
1747}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001748
1749/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001750 * Remove the terminal contents from the scrollback and the buffer.
1751 * Used before adding a new scrollback line or updating the buffer for lines
1752 * displayed in the terminal.
1753 */
1754 static void
1755cleanup_scrollback(term_T *term)
1756{
1757 sb_line_T *line;
1758 garray_T *gap;
1759
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001760 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001761 gap = &term->tl_scrollback;
1762 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1763 && gap->ga_len > 0)
1764 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001765 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001766 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1767 vim_free(line->sb_cells);
1768 --gap->ga_len;
1769 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001770 curbuf = curwin->w_buffer;
1771 if (curbuf == term->tl_buffer)
1772 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001773}
1774
1775/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001776 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001777 */
1778 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001779update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001780{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001781 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001782 int len;
1783 int lines_skipped = 0;
1784 VTermPos pos;
1785 VTermScreenCell cell;
1786 cellattr_T fill_attr, new_fill_attr;
1787 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001788
1789 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1790 "Adding terminal window snapshot to buffer");
1791
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001792 // First remove the lines that were appended before, they might be
1793 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001794 cleanup_scrollback(term);
1795
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001796 screen = vterm_obtain_screen(term->tl_vterm);
1797 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001798 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1799 {
1800 len = 0;
1801 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1802 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1803 && cell.chars[0] != NUL)
1804 {
1805 len = pos.col + 1;
1806 new_fill_attr = term->tl_default_color;
1807 }
1808 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001809 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001810 cell2cellattr(&cell, &new_fill_attr);
1811
1812 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1813 ++lines_skipped;
1814 else
1815 {
1816 while (lines_skipped > 0)
1817 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001818 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001819 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001820 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001821 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001822 }
1823
1824 if (len == 0)
1825 p = NULL;
1826 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001827 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001828 if ((p != NULL || len == 0)
1829 && ga_grow(&term->tl_scrollback, 1) == OK)
1830 {
1831 garray_T ga;
1832 int width;
1833 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1834 + term->tl_scrollback.ga_len;
1835
1836 ga_init2(&ga, 1, 100);
1837 for (pos.col = 0; pos.col < len; pos.col += width)
1838 {
1839 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1840 {
1841 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001842 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001843 if (ga_grow(&ga, 1) == OK)
1844 ga.ga_len += utf_char2bytes(' ',
1845 (char_u *)ga.ga_data + ga.ga_len);
1846 }
1847 else
1848 {
1849 width = cell.width;
1850
1851 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001852 if (width == 2)
1853 // second cell of double-width character has the
1854 // same attributes.
1855 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001856
Bram Moolenaara79fd562018-12-20 20:47:32 +01001857 // Each character can be up to 6 bytes.
1858 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001859 {
1860 int i;
1861 int c;
1862
1863 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1864 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1865 (char_u *)ga.ga_data + ga.ga_len);
1866 }
1867 }
1868 }
1869 line->sb_cols = len;
1870 line->sb_cells = p;
1871 line->sb_fill_attr = new_fill_attr;
1872 fill_attr = new_fill_attr;
1873 ++term->tl_scrollback.ga_len;
1874
1875 if (ga_grow(&ga, 1) == FAIL)
1876 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1877 else
1878 {
1879 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1880 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1881 }
1882 ga_clear(&ga);
1883 }
1884 else
1885 vim_free(p);
1886 }
1887 }
1888
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001889 // Add trailing empty lines.
1890 for (pos.row = term->tl_scrollback.ga_len;
1891 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1892 ++pos.row)
1893 {
1894 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1895 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1896 }
1897
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001898 term->tl_dirty_snapshot = FALSE;
1899#ifdef FEAT_TIMERS
1900 term->tl_timer_set = FALSE;
1901#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001902}
1903
1904/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001905 * Loop over all windows in the current tab, and also curwin, which is not
1906 * encountered when using a terminal in a popup window.
1907 * Return TRUE if "*wp" was set to the next window.
1908 */
1909 static int
1910for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1911{
1912 if (*wp == NULL)
1913 *wp = firstwin;
1914 else if ((*wp)->w_next != NULL)
1915 *wp = (*wp)->w_next;
1916 else if (!*did_curwin)
1917 *wp = curwin;
1918 else
1919 return FALSE;
1920 if (*wp == curwin)
1921 *did_curwin = TRUE;
1922 return TRUE;
1923}
1924
1925/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001926 * If needed, add the current lines of the terminal to scrollback and to the
1927 * buffer. Called after the job has ended and when switching to
1928 * Terminal-Normal mode.
1929 * When "redraw" is TRUE redraw the windows that show the terminal.
1930 */
1931 static void
1932may_move_terminal_to_buffer(term_T *term, int redraw)
1933{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001934 if (term->tl_vterm == NULL)
1935 return;
1936
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001937 // Update the snapshot only if something changes or the buffer does not
1938 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001939 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1940 <= term->tl_scrollback_scrolled)
1941 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001942
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001943 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001944 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1945 &term->tl_default_color.fg, &term->tl_default_color.bg);
1946
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001947 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001948 {
1949 win_T *wp = NULL;
1950 int did_curwin = FALSE;
1951
1952 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001953 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001954 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001955 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001956 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1957 wp->w_cursor.col = 0;
1958 wp->w_valid = 0;
1959 if (wp->w_cursor.lnum >= wp->w_height)
1960 {
1961 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001962
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001963 if (wp->w_topline < min_topline)
1964 wp->w_topline = min_topline;
1965 }
1966 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001967 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001968 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001969 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001970}
1971
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001972#if defined(FEAT_TIMERS) || defined(PROTO)
1973/*
1974 * Check if any terminal timer expired. If so, copy text from the terminal to
1975 * the buffer.
1976 * Return the time until the next timer will expire.
1977 */
1978 int
1979term_check_timers(int next_due_arg, proftime_T *now)
1980{
1981 term_T *term;
1982 int next_due = next_due_arg;
1983
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001984 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001985 {
1986 if (term->tl_timer_set && !term->tl_normal_mode)
1987 {
1988 long this_due = proftime_time_left(&term->tl_timer_due, now);
1989
1990 if (this_due <= 1)
1991 {
1992 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001993 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001994 }
1995 else if (next_due == -1 || next_due > this_due)
1996 next_due = this_due;
1997 }
1998 }
1999
2000 return next_due;
2001}
2002#endif
2003
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002004/*
2005 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
2006 * otherwise end it.
2007 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002008 static void
2009set_terminal_mode(term_T *term, int normal_mode)
2010{
2011 term->tl_normal_mode = normal_mode;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002012 trigger_modechanged();
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002013 if (!normal_mode)
2014 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002015 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002016 if (term->tl_buffer == curbuf)
2017 maketitle();
2018}
2019
2020/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002021 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002022 * Move the vterm contents into the scrollback buffer and free the vterm.
2023 */
2024 static void
2025cleanup_vterm(term_T *term)
2026{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002027 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002028 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002029 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002030 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002031}
2032
2033/*
2034 * Switch from Terminal-Job mode to Terminal-Normal mode.
2035 * Suspends updating the terminal window.
2036 */
2037 static void
2038term_enter_normal_mode(void)
2039{
2040 term_T *term = curbuf->b_term;
2041
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002042 set_terminal_mode(term, TRUE);
2043
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002044 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002045 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002046
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002047 // Move the window cursor to the position of the cursor in the
2048 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002049 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2050 + term->tl_cursor_pos.row + 1;
2051 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002052 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2053 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002054 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002055
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002056 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002057 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2058}
2059
2060/*
2061 * Returns TRUE if the current window contains a terminal and we are in
2062 * Terminal-Normal mode.
2063 */
2064 int
2065term_in_normal_mode(void)
2066{
2067 term_T *term = curbuf->b_term;
2068
2069 return term != NULL && term->tl_normal_mode;
2070}
2071
2072/*
2073 * Switch from Terminal-Normal mode to Terminal-Job mode.
2074 * Restores updating the terminal window.
2075 */
2076 void
2077term_enter_job_mode()
2078{
2079 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002080
2081 set_terminal_mode(term, FALSE);
2082
2083 if (term->tl_channel_closed)
2084 cleanup_vterm(term);
2085 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002086#ifdef FEAT_PROP_POPUP
2087 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002088 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002089#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002090}
2091
2092/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002093 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002094 * Note: while waiting a terminal may be closed and freed if the channel is
2095 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002096 */
2097 static int
2098term_vgetc()
2099{
2100 int c;
2101 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002102 int modify_other_keys =
2103 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002104
2105 State = TERMINAL;
2106 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002107#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002108 ctrl_break_was_pressed = FALSE;
2109#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002110 if (modify_other_keys)
2111 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002112 c = vgetc();
2113 got_int = FALSE;
2114 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002115 if (modify_other_keys)
2116 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002117 return c;
2118}
2119
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002120static int mouse_was_outside = FALSE;
2121
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002122/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002123 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002124 * Return FAIL when the key needs to be handled in Normal mode.
2125 * Return OK when the key was dropped or sent to the terminal.
2126 */
2127 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002128send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002129{
2130 char msg[KEY_BUF_LEN];
2131 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002132 int dragging_outside = FALSE;
2133
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002134 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002135 switch (c)
2136 {
2137 case NUL:
2138 case K_ZERO:
2139 if (typed)
2140 stuffcharReadbuff(c);
2141 return FAIL;
2142
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002143 case K_TABLINE:
2144 stuffcharReadbuff(c);
2145 return FAIL;
2146
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002147 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002148 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002149 return FAIL;
2150
2151 case K_LEFTDRAG:
2152 case K_MIDDLEDRAG:
2153 case K_RIGHTDRAG:
2154 case K_X1DRAG:
2155 case K_X2DRAG:
2156 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002157 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002158 case K_LEFTMOUSE:
2159 case K_LEFTMOUSE_NM:
2160 case K_LEFTRELEASE:
2161 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002162 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002163 case K_MIDDLEMOUSE:
2164 case K_MIDDLERELEASE:
2165 case K_RIGHTMOUSE:
2166 case K_RIGHTRELEASE:
2167 case K_X1MOUSE:
2168 case K_X1RELEASE:
2169 case K_X2MOUSE:
2170 case K_X2RELEASE:
2171
2172 case K_MOUSEUP:
2173 case K_MOUSEDOWN:
2174 case K_MOUSELEFT:
2175 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002176 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002177 int row = mouse_row;
2178 int col = mouse_col;
2179
2180#ifdef FEAT_PROP_POPUP
2181 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002182 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002183 row -= popup_top_extra(curwin);
2184 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002185 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002186#endif
2187 if (row < W_WINROW(curwin)
2188 || row >= (W_WINROW(curwin) + curwin->w_height)
2189 || col < curwin->w_wincol
2190 || col >= W_ENDCOL(curwin)
2191 || dragging_outside)
2192 {
2193 // click or scroll outside the current window or on status
2194 // line or vertical separator
2195 if (typed)
2196 {
2197 stuffcharReadbuff(c);
2198 mouse_was_outside = TRUE;
2199 }
2200 return FAIL;
2201 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002202 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002203 break;
2204
2205 case K_COMMAND:
2206 return do_cmdline(NULL, getcmdkeycmd, NULL, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002207 }
2208 if (typed)
2209 mouse_was_outside = FALSE;
2210
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002211 // Convert the typed key to a sequence of bytes for the job.
2212 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002213 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002214 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002215 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2216 (char_u *)msg, (int)len, NULL);
2217
2218 return OK;
2219}
2220
2221 static void
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002222position_cursor(win_T *wp, VTermPos *pos)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002223{
2224 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2225 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002226#ifdef FEAT_PROP_POPUP
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002227 if (popup_is_popup(wp))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002228 {
Bram Moolenaarf5452692020-11-28 21:56:06 +01002229 wp->w_wrow += popup_top_extra(wp);
2230 wp->w_wcol += popup_left_extra(wp);
Bram Moolenaar6a076442020-11-15 20:32:58 +01002231 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002232 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01002233 else
2234 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002235#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002236 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2237}
2238
2239/*
2240 * Handle CTRL-W "": send register contents to the job.
2241 */
2242 static void
2243term_paste_register(int prev_c UNUSED)
2244{
2245 int c;
2246 list_T *l;
2247 listitem_T *item;
2248 long reglen = 0;
2249 int type;
2250
2251#ifdef FEAT_CMDL_INFO
2252 if (add_to_showcmd(prev_c))
2253 if (add_to_showcmd('"'))
2254 out_flush();
2255#endif
2256 c = term_vgetc();
2257#ifdef FEAT_CMDL_INFO
2258 clear_showcmd();
2259#endif
2260 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002261 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002262 return;
2263
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002264 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002265 if (c == '=' && get_expr_register() != '=')
2266 return;
2267 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002268 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002269 return;
2270
2271 l = (list_T *)get_reg_contents(c, GREG_LIST);
2272 if (l != NULL)
2273 {
2274 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002275 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002276 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002277 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002278#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002279 char_u *tmp = s;
2280
2281 if (!enc_utf8 && enc_codepage > 0)
2282 {
2283 WCHAR *ret = NULL;
2284 int length = 0;
2285
2286 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2287 (int)STRLEN(s), &ret, &length);
2288 if (ret != NULL)
2289 {
2290 WideCharToMultiByte_alloc(CP_UTF8, 0,
2291 ret, length, (char **)&s, &length, 0, 0);
2292 vim_free(ret);
2293 }
2294 }
2295#endif
2296 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2297 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002298#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002299 if (tmp != s)
2300 vim_free(s);
2301#endif
2302
2303 if (item->li_next != NULL || type == MLINE)
2304 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2305 (char_u *)"\r", 1, NULL);
2306 }
2307 list_free(l);
2308 }
2309}
2310
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002311/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002312 * Return TRUE when waiting for a character in the terminal, the cursor of the
2313 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002314 */
2315 int
2316terminal_is_active()
2317{
2318 return in_terminal_loop != NULL;
2319}
2320
Bram Moolenaar83d47902020-03-26 20:34:00 +01002321/*
2322 * Return the highight group name for the terminal; "Terminal" if not set.
2323 */
2324 static char_u *
2325term_get_highlight_name(term_T *term)
2326{
2327 if (term->tl_highlight_name == NULL)
2328 return (char_u *)"Terminal";
2329 return term->tl_highlight_name;
2330}
2331
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002332#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002333 cursorentry_T *
2334term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2335{
2336 term_T *term = in_terminal_loop;
2337 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002338 int id;
2339 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002340
Bram Moolenaara80faa82020-04-12 19:37:17 +02002341 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002342 entry.shape = entry.mshape =
2343 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2344 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2345 SHAPE_BLOCK;
2346 entry.percentage = 20;
2347 if (term->tl_cursor_blink)
2348 {
2349 entry.blinkwait = 700;
2350 entry.blinkon = 400;
2351 entry.blinkoff = 250;
2352 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002353
Bram Moolenaar83d47902020-03-26 20:34:00 +01002354 // The highlight group overrules the defaults.
2355 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002356 if (id != 0)
2357 {
2358 syn_id2colors(id, &term_fg, &term_bg);
2359 *fg = term_bg;
2360 }
2361 else
2362 *fg = gui.back_pixel;
2363
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002364 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002365 {
2366 if (id != 0)
2367 *bg = term_fg;
2368 else
2369 *bg = gui.norm_pixel;
2370 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002371 else
2372 *bg = color_name2handle(term->tl_cursor_color);
2373 entry.name = "n";
2374 entry.used_for = SHAPE_CURSOR;
2375
2376 return &entry;
2377}
2378#endif
2379
Bram Moolenaard317b382018-02-08 22:33:31 +01002380 static void
2381may_output_cursor_props(void)
2382{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002383 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002384 || last_set_cursor_shape != desired_cursor_shape
2385 || last_set_cursor_blink != desired_cursor_blink)
2386 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002387 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002388 last_set_cursor_shape = desired_cursor_shape;
2389 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002390 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002391 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002392 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002393 ui_cursor_shape_forced(TRUE);
2394 else
2395 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2396 }
2397}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002398
Bram Moolenaard317b382018-02-08 22:33:31 +01002399/*
2400 * Set the cursor color and shape, if not last set to these.
2401 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002402 static void
2403may_set_cursor_props(term_T *term)
2404{
2405#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002406 // For the GUI the cursor properties are obtained with
2407 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002408 if (gui.in_use)
2409 return;
2410#endif
2411 if (in_terminal_loop == term)
2412 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002413 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002414 desired_cursor_shape = term->tl_cursor_shape;
2415 desired_cursor_blink = term->tl_cursor_blink;
2416 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002417 }
2418}
2419
Bram Moolenaard317b382018-02-08 22:33:31 +01002420/*
2421 * Reset the desired cursor properties and restore them when needed.
2422 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002423 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002424prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002425{
2426#ifdef FEAT_GUI
2427 if (gui.in_use)
2428 return;
2429#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002430 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002431 desired_cursor_shape = -1;
2432 desired_cursor_blink = -1;
2433 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002434}
2435
2436/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002437 * Returns TRUE if the current window contains a terminal and we are sending
2438 * keys to the job.
2439 * If "check_job_status" is TRUE update the job status.
2440 */
2441 static int
2442term_use_loop_check(int check_job_status)
2443{
2444 term_T *term = curbuf->b_term;
2445
2446 return term != NULL
2447 && !term->tl_normal_mode
2448 && term->tl_vterm != NULL
2449 && term_job_running_check(term, check_job_status);
2450}
2451
2452/*
2453 * Returns TRUE if the current window contains a terminal and we are sending
2454 * keys to the job.
2455 */
2456 int
2457term_use_loop(void)
2458{
2459 return term_use_loop_check(FALSE);
2460}
2461
2462/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002463 * Called when entering a window with the mouse. If this is a terminal window
2464 * we may want to change state.
2465 */
2466 void
2467term_win_entered()
2468{
2469 term_T *term = curbuf->b_term;
2470
2471 if (term != NULL)
2472 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002473 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002474 {
2475 reset_VIsual_and_resel();
2476 if (State & INSERT)
2477 stop_insert_mode = TRUE;
2478 }
2479 mouse_was_outside = FALSE;
2480 enter_mouse_col = mouse_col;
2481 enter_mouse_row = mouse_row;
2482 }
2483}
2484
2485/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002486 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2487 * Return the Ctrl-key value in that case.
2488 */
2489 static int
2490raw_c_to_ctrl(int c)
2491{
2492 if ((mod_mask & MOD_MASK_CTRL)
2493 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2494 return c & 0x1f;
2495 return c;
2496}
2497
2498/*
2499 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2500 * May set "mod_mask".
2501 */
2502 static int
2503ctrl_to_raw_c(int c)
2504{
2505 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2506 {
2507 mod_mask |= MOD_MASK_CTRL;
2508 return c + '@';
2509 }
2510 return c;
2511}
2512
2513/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002514 * Wait for input and send it to the job.
2515 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2516 * when there is no more typahead.
2517 * Return when the start of a CTRL-W command is typed or anything else that
2518 * should be handled as a Normal mode command.
2519 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2520 * the terminal was closed.
2521 */
2522 int
2523terminal_loop(int blocking)
2524{
2525 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002526 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002527 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002528 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002529#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002530 int tty_fd = curbuf->b_term->tl_job->jv_channel
2531 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002532#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002533 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002534
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002535 // Remember the terminal we are sending keys to. However, the terminal
2536 // might be closed while waiting for a character, e.g. typing "exit" in a
2537 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2538 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002539 in_terminal_loop = curbuf->b_term;
2540
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002541 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002542 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002543 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002544 if (termwinkey == Ctrl_W)
2545 termwinkey = 0;
2546 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002547 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002548 may_set_cursor_props(curbuf->b_term);
2549
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002550 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002551 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002552#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002553 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002554#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002555 // TODO: skip screen update when handling a sequence of keys.
2556 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002557 while (must_redraw != 0)
2558 if (update_screen(0) == FAIL)
2559 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002560 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002561 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002562 break;
2563
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002564 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002565 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002566
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002567 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002568 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002569 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002570 // Job finished while waiting for a character. Push back the
2571 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002572 if (raw_c != K_IGNORE)
2573 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002574 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002575 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002576 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002577 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002578 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002579
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002580#ifdef UNIX
2581 /*
2582 * The shell or another program may change the tty settings. Getting
2583 * them for every typed character is a bit of overhead, but it's needed
2584 * for the first character typed, e.g. when Vim starts in a shell.
2585 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002586 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002587 {
2588 ttyinfo_T info;
2589
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002590 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002591 if (get_tty_info(tty_fd, &info) == OK)
2592 term_backspace_char = info.backspace;
2593 }
2594#endif
2595
Bram Moolenaar4f974752019-02-17 17:44:42 +01002596#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002597 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2598 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002599 if (ctrl_break_was_pressed)
2600 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2601#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002602 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2603 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002604 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002605#ifdef FEAT_GUI
2606 && !curbuf->b_term->tl_system
2607#endif
2608 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002609 {
2610 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002611 int prev_raw_c = raw_c;
2612 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002613
2614#ifdef FEAT_CMDL_INFO
2615 if (add_to_showcmd(c))
2616 out_flush();
2617#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002618 raw_c = term_vgetc();
2619 c = raw_c_to_ctrl(raw_c);
2620
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002621#ifdef FEAT_CMDL_INFO
2622 clear_showcmd();
2623#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002624 if (!term_use_loop_check(TRUE)
2625 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002626 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002627 break;
2628
2629 if (prev_c == Ctrl_BSL)
2630 {
2631 if (c == Ctrl_N)
2632 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002633 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002634 term_enter_normal_mode();
2635 ret = FAIL;
2636 goto theend;
2637 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002638 // Send both keys to the terminal, first one here, second one
2639 // below.
2640 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2641 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002642 }
2643 else if (c == Ctrl_C)
2644 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002645 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002646 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2647 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002648 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002649 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002650 // "CTRL-W .": send CTRL-W to the job
2651 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002652 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002653 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002654 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002655 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002656 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002657 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002658 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002659 else if (c == 'N')
2660 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002661 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002662 term_enter_normal_mode();
2663 ret = FAIL;
2664 goto theend;
2665 }
2666 else if (c == '"')
2667 {
2668 term_paste_register(prev_c);
2669 continue;
2670 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002671 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002672 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002673 // space for CTRL-W, modifier, multi-byte char and NUL
2674 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002675
2676 // Put the command into the typeahead buffer, when using the
2677 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2678 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002679 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002680 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002681 ret = OK;
2682 goto theend;
2683 }
2684 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002685# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002686 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002687 {
2688 WCHAR wc;
2689 char_u mb[3];
2690
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002691 mb[0] = (unsigned)raw_c >> 8;
2692 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002693 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002694 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002695 }
2696# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002697 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002698 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002699 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002700 // We are sure to come back here, don't reset the cursor color
2701 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002702 restore_cursor = FALSE;
2703
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002704 ret = OK;
2705 goto theend;
2706 }
2707 }
2708 ret = FAIL;
2709
2710theend:
2711 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002712 if (restore_cursor)
2713 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002714
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002715 // Move a snapshot of the screen contents to the buffer, so that completion
2716 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002717 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2718 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002719
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002720 return ret;
2721}
2722
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002723 static void
2724may_toggle_cursor(term_T *term)
2725{
2726 if (in_terminal_loop == term)
2727 {
2728 if (term->tl_cursor_visible)
2729 cursor_on();
2730 else
2731 cursor_off();
2732 }
2733}
2734
2735/*
2736 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002737 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002738 */
2739 static int
2740color2index(VTermColor *color, int fg, int *boldp)
2741{
2742 int red = color->red;
2743 int blue = color->blue;
2744 int green = color->green;
2745
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002746 if (VTERM_COLOR_IS_DEFAULT_FG(color)
2747 || VTERM_COLOR_IS_DEFAULT_BG(color))
2748 return 0;
2749 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002750 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002751 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002752 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002753 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002754 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002755 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2756 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2757 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002758 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002759 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2760 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2761 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2762 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2763 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2764 case 10: return lookup_color(20, fg, boldp) + 1; // red
2765 case 11: return lookup_color(16, fg, boldp) + 1; // green
2766 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2767 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2768 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2769 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2770 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002771 }
2772 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002773
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002774 if (t_colors >= 256)
2775 {
2776 if (red == blue && red == green)
2777 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002778 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002779 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002780 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2781 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2782 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002783 int i;
2784
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002785 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002786 return 17; // 00/00/00
2787 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002788 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002789 for (i = 0; i < 23; ++i)
2790 if (red < cutoff[i])
2791 return i + 233;
2792 return 256;
2793 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002794 {
2795 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2796 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002797
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002798 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002799 for (ri = 0; ri < 5; ++ri)
2800 if (red < cutoff[ri])
2801 break;
2802 for (gi = 0; gi < 5; ++gi)
2803 if (green < cutoff[gi])
2804 break;
2805 for (bi = 0; bi < 5; ++bi)
2806 if (blue < cutoff[bi])
2807 break;
2808 return 17 + ri * 36 + gi * 6 + bi;
2809 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002810 }
2811 return 0;
2812}
2813
2814/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002815 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002816 */
2817 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002818vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002819{
2820 int attr = 0;
2821
2822 if (cellattrs.bold)
2823 attr |= HL_BOLD;
2824 if (cellattrs.underline)
2825 attr |= HL_UNDERLINE;
2826 if (cellattrs.italic)
2827 attr |= HL_ITALIC;
2828 if (cellattrs.strike)
2829 attr |= HL_STRIKETHROUGH;
2830 if (cellattrs.reverse)
2831 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002832 return attr;
2833}
2834
2835/*
2836 * Store Vterm attributes in "cell" from highlight flags.
2837 */
2838 static void
2839hl2vtermAttr(int attr, cellattr_T *cell)
2840{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002841 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002842 if (attr & HL_BOLD)
2843 cell->attrs.bold = 1;
2844 if (attr & HL_UNDERLINE)
2845 cell->attrs.underline = 1;
2846 if (attr & HL_ITALIC)
2847 cell->attrs.italic = 1;
2848 if (attr & HL_STRIKETHROUGH)
2849 cell->attrs.strike = 1;
2850 if (attr & HL_INVERSE)
2851 cell->attrs.reverse = 1;
2852}
2853
2854/*
2855 * Convert the attributes of a vterm cell into an attribute index.
2856 */
2857 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002858cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002859 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002860 win_T *wp,
2861 VTermScreenCellAttrs cellattrs,
2862 VTermColor cellfg,
2863 VTermColor cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002864{
2865 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002866
2867#ifdef FEAT_GUI
2868 if (gui.in_use)
2869 {
2870 guicolor_T fg, bg;
2871
2872 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2873 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2874 return get_gui_attr_idx(attr, fg, bg);
2875 }
2876 else
2877#endif
2878#ifdef FEAT_TERMGUICOLORS
2879 if (p_tgc)
2880 {
Milly7b5f45b2021-10-15 22:25:43 +01002881 guicolor_T fg = INVALCOLOR;
2882 guicolor_T bg = INVALCOLOR;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002883
Milly7b5f45b2021-10-15 22:25:43 +01002884 // Use the 'wincolor' or "Terminal" highlighting for the default
2885 // colors.
2886 if (VTERM_COLOR_IS_DEFAULT_FG(&cellfg)
2887 || VTERM_COLOR_IS_DEFAULT_BG(&cellbg))
2888 {
2889 int id = 0;
2890
2891 if (wp != NULL && *wp->w_p_wcr != NUL)
2892 id = syn_name2id(wp->w_p_wcr);
2893 if (id == 0)
2894 id = syn_name2id(term_get_highlight_name(term));
2895 if (id > 0)
2896 syn_id2colors(id, &fg, &bg);
2897 if (!VTERM_COLOR_IS_DEFAULT_FG(&cellfg))
2898 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green,
2899 cellfg.blue);
2900 if (!VTERM_COLOR_IS_DEFAULT_BG(&cellbg))
2901 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green,
2902 cellbg.blue);
2903 }
2904 else
2905 {
2906 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2907 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2908 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002909
2910 return get_tgc_attr_idx(attr, fg, bg);
2911 }
2912 else
2913#endif
2914 {
2915 int bold = MAYBE;
2916 int fg = color2index(&cellfg, TRUE, &bold);
2917 int bg = color2index(&cellbg, FALSE, &bold);
2918
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002919 // Use the 'wincolor' or "Terminal" highlighting for the default
2920 // colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002921 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002922 {
Milly7b5f45b2021-10-15 22:25:43 +01002923 int cterm_fg = -1;
2924 int cterm_bg = -1;
2925 int id = 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002926
2927 if (wp != NULL && *wp->w_p_wcr != NUL)
Milly7b5f45b2021-10-15 22:25:43 +01002928 id = syn_name2id(wp->w_p_wcr);
2929 if (id == 0)
2930 id = syn_name2id(term_get_highlight_name(term));
2931 if (id > 0)
2932 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
2933 if (fg == 0 && cterm_fg >= 0)
2934 fg = cterm_fg + 1;
2935 if (bg == 0 && cterm_bg >= 0)
2936 bg = cterm_bg + 1;
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002937 }
2938
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002939 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002940 if (bold == TRUE)
2941 attr |= HL_BOLD;
2942 return get_cterm_attr_idx(attr, fg, bg);
2943 }
2944 return 0;
2945}
2946
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002947 static void
2948set_dirty_snapshot(term_T *term)
2949{
2950 term->tl_dirty_snapshot = TRUE;
2951#ifdef FEAT_TIMERS
2952 if (!term->tl_normal_mode)
2953 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002954 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002955 profile_setlimit(100L, &term->tl_timer_due);
2956 term->tl_timer_set = TRUE;
2957 }
2958#endif
2959}
2960
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002961 static int
2962handle_damage(VTermRect rect, void *user)
2963{
2964 term_T *term = (term_T *)user;
2965
2966 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2967 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002968 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002969 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002970 return 1;
2971}
2972
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002973 static void
2974term_scroll_up(term_T *term, int start_row, int count)
2975{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002976 win_T *wp = NULL;
2977 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002978 VTermColor fg, bg;
2979 VTermScreenCellAttrs attr;
2980 int clear_attr;
2981
Bram Moolenaara80faa82020-04-12 19:37:17 +02002982 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002983
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002984 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002985 {
2986 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002987 {
2988 // Set the color to clear lines with.
2989 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2990 &fg, &bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002991 clear_attr = cell2attr(term, wp, attr, fg, bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002992 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002993 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002994 }
2995}
2996
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002997 static int
2998handle_moverect(VTermRect dest, VTermRect src, void *user)
2999{
3000 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003001 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003002
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003003 // Scrolling up is done much more efficiently by deleting lines instead of
3004 // redrawing the text. But avoid doing this multiple times, postpone until
3005 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003006 if (dest.start_col == src.start_col
3007 && dest.end_col == src.end_col
3008 && dest.start_row < src.start_row)
3009 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003010 if (dest.start_row == 0)
3011 term->tl_postponed_scroll += count;
3012 else
3013 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003014 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003015
3016 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3017 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003018 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003019
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003020 // Note sure if the scrolling will work correctly, let's do a complete
3021 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003022 redraw_buf_later(term->tl_buffer, NOT_VALID);
3023 return 1;
3024}
3025
3026 static int
3027handle_movecursor(
3028 VTermPos pos,
3029 VTermPos oldpos UNUSED,
3030 int visible,
3031 void *user)
3032{
3033 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003034 win_T *wp = NULL;
3035 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003036
3037 term->tl_cursor_pos = pos;
3038 term->tl_cursor_visible = visible;
3039
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003040 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003041 {
3042 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003043 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003044 }
3045 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003046 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003047
3048 return 1;
3049}
3050
3051 static int
3052handle_settermprop(
3053 VTermProp prop,
3054 VTermValue *value,
3055 void *user)
3056{
3057 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003058 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003059
3060 switch (prop)
3061 {
3062 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003063 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003064 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003065 if (strval == NULL)
3066 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003067 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003068 // a blank title isn't useful, make it empty, so that "running" is
3069 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003070 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003071 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003072 // Same as blank
3073 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003074 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003075 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3076 term->tl_title = NULL;
3077 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003078 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003079 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003080#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003081 else if (!enc_utf8 && enc_codepage > 0)
3082 {
3083 WCHAR *ret = NULL;
3084 int length = 0;
3085
3086 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003087 (char*)value->string.str,
3088 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003089 if (ret != NULL)
3090 {
3091 WideCharToMultiByte_alloc(enc_codepage, 0,
3092 ret, length, (char**)&term->tl_title,
3093 &length, 0, 0);
3094 vim_free(ret);
3095 }
3096 }
3097#endif
3098 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003099 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003100 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003101 strval = NULL;
3102 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003103 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003104 if (term == curbuf->b_term)
3105 maketitle();
3106 break;
3107
3108 case VTERM_PROP_CURSORVISIBLE:
3109 term->tl_cursor_visible = value->boolean;
3110 may_toggle_cursor(term);
3111 out_flush();
3112 break;
3113
3114 case VTERM_PROP_CURSORBLINK:
3115 term->tl_cursor_blink = value->boolean;
3116 may_set_cursor_props(term);
3117 break;
3118
3119 case VTERM_PROP_CURSORSHAPE:
3120 term->tl_cursor_shape = value->number;
3121 may_set_cursor_props(term);
3122 break;
3123
3124 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003125 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003126 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003127 if (strval == NULL)
3128 break;
3129 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003130 may_set_cursor_props(term);
3131 break;
3132
3133 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003134 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003135 term->tl_using_altscreen = value->boolean;
3136 break;
3137
3138 default:
3139 break;
3140 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003141 vim_free(strval);
3142
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003143 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003144 return 1;
3145}
3146
3147/*
3148 * The job running in the terminal resized the terminal.
3149 */
3150 static int
3151handle_resize(int rows, int cols, void *user)
3152{
3153 term_T *term = (term_T *)user;
3154 win_T *wp;
3155
3156 term->tl_rows = rows;
3157 term->tl_cols = cols;
3158 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003159 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003160 term->tl_vterm_size_changed = FALSE;
3161 else
3162 {
3163 FOR_ALL_WINDOWS(wp)
3164 {
3165 if (wp->w_buffer == term->tl_buffer)
3166 {
3167 win_setheight_win(rows, wp);
3168 win_setwidth_win(cols, wp);
3169 }
3170 }
3171 redraw_buf_later(term->tl_buffer, NOT_VALID);
3172 }
3173 return 1;
3174}
3175
3176/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003177 * If the number of lines that are stored goes over 'termscrollback' then
3178 * delete the first 10%.
3179 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3180 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003181 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003182 static void
3183limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003184{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003185 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003186 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003187 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003188 int i;
3189
3190 curbuf = term->tl_buffer;
3191 for (i = 0; i < todo; ++i)
3192 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003193 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3194 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003195 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003196 }
3197 curbuf = curwin->w_buffer;
3198
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003199 gap->ga_len -= todo;
3200 mch_memmove(gap->ga_data,
3201 (sb_line_T *)gap->ga_data + todo,
3202 sizeof(sb_line_T) * gap->ga_len);
3203 if (update_buffer)
3204 term->tl_scrollback_scrolled -= todo;
3205 }
3206}
3207
3208/*
3209 * Handle a line that is pushed off the top of the screen.
3210 */
3211 static int
3212handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3213{
3214 term_T *term = (term_T *)user;
3215 garray_T *gap;
3216 int update_buffer;
3217
3218 if (term->tl_normal_mode)
3219 {
3220 // In Terminal-Normal mode the user interacts with the buffer, thus we
3221 // must not change it. Postpone adding the scrollback lines.
3222 gap = &term->tl_scrollback_postponed;
3223 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003224 }
3225 else
3226 {
3227 // First remove the lines that were appended before, the pushed line
3228 // goes above it.
3229 cleanup_scrollback(term);
3230 gap = &term->tl_scrollback;
3231 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003232 }
3233
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003234 limit_scrollback(term, gap, update_buffer);
3235
3236 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003237 {
3238 cellattr_T *p = NULL;
3239 int len = 0;
3240 int i;
3241 int c;
3242 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003243 int text_len;
3244 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003245 sb_line_T *line;
3246 garray_T ga;
3247 cellattr_T fill_attr = term->tl_default_color;
3248
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003249 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003250 for (i = 0; i < cols; ++i)
3251 if (cells[i].chars[0] != 0)
3252 len = i + 1;
3253 else
3254 cell2cellattr(&cells[i], &fill_attr);
3255
3256 ga_init2(&ga, 1, 100);
3257 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003258 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003259 if (p != NULL)
3260 {
3261 for (col = 0; col < len; col += cells[col].width)
3262 {
3263 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3264 {
3265 ga.ga_len = 0;
3266 break;
3267 }
3268 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3269 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3270 (char_u *)ga.ga_data + ga.ga_len);
3271 cell2cellattr(&cells[col], &p[col]);
3272 }
3273 }
3274 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003275 {
3276 if (update_buffer)
3277 text = (char_u *)"";
3278 else
3279 text = vim_strsave((char_u *)"");
3280 text_len = 0;
3281 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003282 else
3283 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003284 text = ga.ga_data;
3285 text_len = ga.ga_len;
3286 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003287 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003288 if (update_buffer)
3289 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003290
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003291 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003292 line->sb_cols = len;
3293 line->sb_cells = p;
3294 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003295 if (update_buffer)
3296 {
3297 line->sb_text = NULL;
3298 ++term->tl_scrollback_scrolled;
3299 ga_clear(&ga); // free the text
3300 }
3301 else
3302 {
3303 line->sb_text = text;
3304 ga_init(&ga); // text is kept in tl_scrollback_postponed
3305 }
3306 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003307 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003308 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003309}
3310
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003311/*
3312 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3313 * received and stored in tl_scrollback_postponed.
3314 */
3315 static void
3316handle_postponed_scrollback(term_T *term)
3317{
3318 int i;
3319
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003320 if (term->tl_scrollback_postponed.ga_len == 0)
3321 return;
3322 ch_log(NULL, "Moving postponed scrollback to scrollback");
3323
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003324 // First remove the lines that were appended before, the pushed lines go
3325 // above it.
3326 cleanup_scrollback(term);
3327
3328 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3329 {
3330 char_u *text;
3331 sb_line_T *pp_line;
3332 sb_line_T *line;
3333
3334 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3335 break;
3336 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3337
3338 text = pp_line->sb_text;
3339 if (text == NULL)
3340 text = (char_u *)"";
3341 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3342 vim_free(pp_line->sb_text);
3343
3344 line = (sb_line_T *)term->tl_scrollback.ga_data
3345 + term->tl_scrollback.ga_len;
3346 line->sb_cols = pp_line->sb_cols;
3347 line->sb_cells = pp_line->sb_cells;
3348 line->sb_fill_attr = pp_line->sb_fill_attr;
3349 line->sb_text = NULL;
3350 ++term->tl_scrollback_scrolled;
3351 ++term->tl_scrollback.ga_len;
3352 }
3353
3354 ga_clear(&term->tl_scrollback_postponed);
3355 limit_scrollback(term, &term->tl_scrollback, TRUE);
3356}
3357
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003358static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003359 handle_damage, // damage
3360 handle_moverect, // moverect
3361 handle_movecursor, // movecursor
3362 handle_settermprop, // settermprop
3363 NULL, // bell
3364 handle_resize, // resize
3365 handle_pushline, // sb_pushline
3366 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003367};
3368
3369/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003370 * Do the work after the channel of a terminal was closed.
3371 * Must be called only when updating_screen is FALSE.
3372 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3373 */
3374 static int
3375term_after_channel_closed(term_T *term)
3376{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003377 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003378 if (!term->tl_normal_mode)
3379 {
3380 int fnum = term->tl_buffer->b_fnum;
3381
3382 cleanup_vterm(term);
3383
3384 if (term->tl_finish == TL_FINISH_CLOSE)
3385 {
3386 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003387 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003388#ifdef FEAT_PROP_POPUP
3389 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003390
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003391 // If this was a terminal in a popup window, go back to the
3392 // previous window.
3393 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3394 {
3395 pwin = curwin;
3396 if (win_valid(prevwin))
3397 win_enter(prevwin, FALSE);
3398 }
3399 else
3400#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003401 // If this is the last normal window: exit Vim.
3402 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3403 {
3404 exarg_T ea;
3405
Bram Moolenaara80faa82020-04-12 19:37:17 +02003406 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003407 ex_quit(&ea);
3408 return TRUE;
3409 }
3410
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003411 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003412 ch_log(NULL, "terminal job finished, closing window");
3413 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003414 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003415 if (curwin == aucmd_win)
3416 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003417 if (do_set_w_closing)
3418 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003419 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003420 if (do_set_w_closing)
3421 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003422 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003423#ifdef FEAT_PROP_POPUP
3424 if (pwin != NULL)
3425 popup_close_with_retval(pwin, 0);
3426#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003427 return TRUE;
3428 }
3429 if (term->tl_finish == TL_FINISH_OPEN
3430 && term->tl_buffer->b_nwindows == 0)
3431 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003432 char *cmd = term->tl_opencmd == NULL
3433 ? "botright sbuf %d"
3434 : (char *)term->tl_opencmd;
3435 size_t len = strlen(cmd) + 50;
3436 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003437
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003438 if (buf != NULL)
3439 {
3440 ch_log(NULL, "terminal job finished, opening window");
3441 vim_snprintf(buf, len, cmd, fnum);
3442 do_cmdline_cmd((char_u *)buf);
3443 vim_free(buf);
3444 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003445 }
3446 else
3447 ch_log(NULL, "terminal job finished");
3448 }
3449
3450 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3451 return FALSE;
3452}
3453
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003454#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3455/*
3456 * If the current window is a terminal in a popup window and the job has
3457 * finished, close the popup window and to back to the previous window.
3458 * Otherwise return FAIL.
3459 */
3460 int
3461may_close_term_popup(void)
3462{
3463 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3464 && !term_job_running(curbuf->b_term))
3465 {
3466 win_T *pwin = curwin;
3467
3468 if (win_valid(prevwin))
3469 win_enter(prevwin, FALSE);
3470 popup_close_with_retval(pwin, 0);
3471 return OK;
3472 }
3473 return FAIL;
3474}
3475#endif
3476
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003477/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003478 * Called when a channel has been closed.
3479 * If this was a channel for a terminal window then finish it up.
3480 */
3481 void
3482term_channel_closed(channel_T *ch)
3483{
3484 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003485 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003486 int did_one = FALSE;
3487
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003488 for (term = first_term; term != NULL; term = next_term)
3489 {
3490 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003491 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003492 {
3493 term->tl_channel_closed = TRUE;
3494 did_one = TRUE;
3495
Bram Moolenaard23a8232018-02-10 18:45:26 +01003496 VIM_CLEAR(term->tl_title);
3497 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003498#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003499 if (term->tl_out_fd != NULL)
3500 {
3501 fclose(term->tl_out_fd);
3502 term->tl_out_fd = NULL;
3503 }
3504#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003505
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003506 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003507 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003508 // Cannot open or close windows now. Can happen when
3509 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003510 term->tl_channel_recently_closed = TRUE;
3511 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003512 }
3513
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003514 if (term_after_channel_closed(term))
3515 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003516 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003517 }
3518
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003519 if (did_one)
3520 {
3521 redraw_statuslines();
3522
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003523 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003524 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003525 typebuf_was_filled = TRUE;
3526
3527 term = curbuf->b_term;
3528 if (term != NULL)
3529 {
3530 if (term->tl_job == ch->ch_job)
3531 maketitle();
3532 update_cursor(term, term->tl_cursor_visible);
3533 }
3534 }
3535}
3536
3537/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003538 * To be called after resetting updating_screen: handle any terminal where the
3539 * channel was closed.
3540 */
3541 void
3542term_check_channel_closed_recently()
3543{
3544 term_T *term;
3545 term_T *next_term;
3546
3547 for (term = first_term; term != NULL; term = next_term)
3548 {
3549 next_term = term->tl_next;
3550 if (term->tl_channel_recently_closed)
3551 {
3552 term->tl_channel_recently_closed = FALSE;
3553 if (term_after_channel_closed(term))
3554 // start over, the list may have changed
3555 next_term = first_term;
3556 }
3557 }
3558}
3559
3560/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003561 * Fill one screen line from a line of the terminal.
3562 * Advances "pos" to past the last column.
3563 */
3564 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003565term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003566 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003567 win_T *wp,
3568 VTermScreen *screen,
3569 VTermPos *pos,
3570 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003571{
3572 int off = screen_get_current_line_off();
3573
3574 for (pos->col = 0; pos->col < max_col; )
3575 {
3576 VTermScreenCell cell;
3577 int c;
3578
3579 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003580 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003581
3582 c = cell.chars[0];
3583 if (c == NUL)
3584 {
3585 ScreenLines[off] = ' ';
3586 if (enc_utf8)
3587 ScreenLinesUC[off] = NUL;
3588 }
3589 else
3590 {
3591 if (enc_utf8)
3592 {
3593 int i;
3594
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003595 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003596 for (i = 0; i < Screen_mco
3597 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3598 {
3599 ScreenLinesC[i][off] = cell.chars[i + 1];
3600 if (cell.chars[i + 1] == 0)
3601 break;
3602 }
3603 if (c >= 0x80 || (Screen_mco > 0
3604 && ScreenLinesC[0][off] != 0))
3605 {
3606 ScreenLines[off] = ' ';
3607 ScreenLinesUC[off] = c;
3608 }
3609 else
3610 {
3611 ScreenLines[off] = c;
3612 ScreenLinesUC[off] = NUL;
3613 }
3614 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003615#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003616 else if (has_mbyte && c >= 0x80)
3617 {
3618 char_u mb[MB_MAXBYTES+1];
3619 WCHAR wc = c;
3620
3621 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3622 (char*)mb, 2, 0, 0) > 1)
3623 {
3624 ScreenLines[off] = mb[0];
3625 ScreenLines[off + 1] = mb[1];
3626 cell.width = mb_ptr2cells(mb);
3627 }
3628 else
3629 ScreenLines[off] = c;
3630 }
3631#endif
3632 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003633 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003634 ScreenLines[off] = c;
3635 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003636 ScreenAttrs[off] = cell2attr(term, wp, cell.attrs, cell.fg, cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003637
3638 ++pos->col;
3639 ++off;
3640 if (cell.width == 2)
3641 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003642 // don't set the second byte to NUL for a DBCS encoding, it
3643 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003644 if (enc_utf8)
3645 {
3646 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003647 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003648 }
3649 else if (!has_mbyte)
3650 {
3651 // Can't show a double-width character with a single-byte
3652 // 'encoding', just use a space.
3653 ScreenLines[off] = ' ';
3654 ScreenAttrs[off] = ScreenAttrs[off - 1];
3655 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003656
3657 ++pos->col;
3658 ++off;
3659 }
3660 }
3661}
3662
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003663#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003664 static void
3665update_system_term(term_T *term)
3666{
3667 VTermPos pos;
3668 VTermScreen *screen;
3669
3670 if (term->tl_vterm == NULL)
3671 return;
3672 screen = vterm_obtain_screen(term->tl_vterm);
3673
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003674 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003675 while (term->tl_toprow > 0
3676 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3677 {
3678 int save_p_more = p_more;
3679
3680 p_more = FALSE;
3681 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003682 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003683 p_more = save_p_more;
3684 --term->tl_toprow;
3685 }
3686
3687 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3688 && pos.row < Rows; ++pos.row)
3689 {
3690 if (pos.row < term->tl_rows)
3691 {
3692 int max_col = MIN(Columns, term->tl_cols);
3693
Bram Moolenaar83d47902020-03-26 20:34:00 +01003694 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003695 }
3696 else
3697 pos.col = 0;
3698
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003699 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003700 }
3701
3702 term->tl_dirty_row_start = MAX_ROW;
3703 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003704}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003705#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003706
3707/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003708 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3709 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003710 * Terminal-Normal mode.
3711 */
3712 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003713term_do_update_window(win_T *wp)
3714{
3715 term_T *term = wp->w_buffer->b_term;
3716
3717 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3718}
3719
3720/*
3721 * Called to update a window that contains an active terminal.
3722 */
3723 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003724term_update_window(win_T *wp)
3725{
3726 term_T *term = wp->w_buffer->b_term;
3727 VTerm *vterm;
3728 VTermScreen *screen;
3729 VTermState *state;
3730 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003731 int rows, cols;
3732 int newrows, newcols;
3733 int minsize;
3734 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003735
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003736 vterm = term->tl_vterm;
3737 screen = vterm_obtain_screen(vterm);
3738 state = vterm_obtain_state(vterm);
3739
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003740 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3741 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003742 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003743 {
3744 term->tl_dirty_row_start = 0;
3745 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003746
3747 if (term->tl_postponed_scroll > 0
3748 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003749 // Scrolling is usually faster than redrawing, when there are only
3750 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003751 term_scroll_up(term, 0, term->tl_postponed_scroll);
3752 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003753 }
3754
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003755 /*
3756 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003757 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003758 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003759 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003760
Bram Moolenaar498c2562018-04-15 23:45:15 +02003761 newrows = 99999;
3762 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003763 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003764 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003765 // Always use curwin, it may be a popup window.
3766 win_T *wwp = twp == NULL ? curwin : twp;
3767
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003768 // When more than one window shows the same terminal, use the
3769 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003770 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003771 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003772 newrows = MIN(newrows, wwp->w_height);
3773 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003774 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003775 if (twp == NULL)
3776 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003777 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003778 if (newrows == 99999 || newcols == 99999)
3779 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003780 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3781 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3782
Bram Moolenaareba13e42021-02-23 17:47:23 +01003783 // If no cell is visible there is no point in resizing. Also, vterm can't
3784 // handle a zero height.
3785 if (newrows == 0 || newcols == 0)
3786 return;
3787
Bram Moolenaar498c2562018-04-15 23:45:15 +02003788 if (term->tl_rows != newrows || term->tl_cols != newcols)
3789 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003790 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003791 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003792 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003793 newrows);
3794 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003795
3796 // Updating the terminal size will cause the snapshot to be cleared.
3797 // When not in terminal_loop() we need to restore it.
3798 if (term != in_terminal_loop)
3799 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003800 }
3801
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003802 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003803 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003804 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003805
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003806 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3807 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003808 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003809 if (pos.row < term->tl_rows)
3810 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003811 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003812
Bram Moolenaar83d47902020-03-26 20:34:00 +01003813 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003814 }
3815 else
3816 pos.col = 0;
3817
Bram Moolenaarf118d482018-03-13 13:14:00 +01003818 screen_line(wp->w_winrow + pos.row
3819#ifdef FEAT_MENU
3820 + winbar_height(wp)
3821#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003822 , wp->w_wincol, pos.col, wp->w_width,
3823#ifdef FEAT_PROP_POPUP
3824 popup_is_popup(wp) ? SLF_POPUP :
3825#endif
3826 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003827 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003828 term->tl_dirty_row_start = MAX_ROW;
3829 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003830}
3831
3832/*
3833 * Return TRUE if "wp" is a terminal window where the job has finished.
3834 */
3835 int
3836term_is_finished(buf_T *buf)
3837{
3838 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3839}
3840
3841/*
3842 * Return TRUE if "wp" is a terminal window where the job has finished or we
3843 * are in Terminal-Normal mode, thus we show the buffer contents.
3844 */
3845 int
3846term_show_buffer(buf_T *buf)
3847{
3848 term_T *term = buf->b_term;
3849
3850 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3851}
3852
3853/*
3854 * The current buffer is going to be changed. If there is terminal
3855 * highlighting remove it now.
3856 */
3857 void
3858term_change_in_curbuf(void)
3859{
3860 term_T *term = curbuf->b_term;
3861
3862 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3863 {
3864 free_scrollback(term);
3865 redraw_buf_later(term->tl_buffer, NOT_VALID);
3866
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003867 // The buffer is now like a normal buffer, it cannot be easily
3868 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003869 set_string_option_direct((char_u *)"buftype", -1,
3870 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3871 }
3872}
3873
3874/*
3875 * Get the screen attribute for a position in the buffer.
3876 * Use a negative "col" to get the filler background color.
3877 */
3878 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003879term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003880{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003881 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003882 term_T *term = buf->b_term;
3883 sb_line_T *line;
3884 cellattr_T *cellattr;
3885
3886 if (lnum > term->tl_scrollback.ga_len)
3887 cellattr = &term->tl_default_color;
3888 else
3889 {
3890 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3891 if (col < 0 || col >= line->sb_cols)
3892 cellattr = &line->sb_fill_attr;
3893 else
3894 cellattr = line->sb_cells + col;
3895 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003896 return cell2attr(term, wp, cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003897}
3898
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003899/*
3900 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003901 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003902 */
3903 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003904cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003905{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003906 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3907 if (rgb->index == 0)
3908 rgb->type = VTERM_COLOR_RGB;
3909 else
3910 {
3911 rgb->type = VTERM_COLOR_INDEXED;
3912 --rgb->index;
3913 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003914}
3915
3916/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003917 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003918 */
3919 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003920init_default_colors(term_T *term, win_T *wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003921{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003922 VTermColor *fg, *bg;
3923 int fgval, bgval;
3924 int id;
3925
Bram Moolenaara80faa82020-04-12 19:37:17 +02003926 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003927 term->tl_default_color.width = 1;
3928 fg = &term->tl_default_color.fg;
3929 bg = &term->tl_default_color.bg;
3930
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003931 // Vterm uses a default black background. Set it to white when
3932 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003933 if (*p_bg == 'l')
3934 {
3935 fgval = 0;
3936 bgval = 255;
3937 }
3938 else
3939 {
3940 fgval = 255;
3941 bgval = 0;
3942 }
3943 fg->red = fg->green = fg->blue = fgval;
3944 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003945 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3946 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003947
Bram Moolenaar83d47902020-03-26 20:34:00 +01003948 // The 'wincolor' or the highlight group overrules the defaults.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003949 if (wp != NULL && *wp->w_p_wcr != NUL)
3950 id = syn_name2id(wp->w_p_wcr);
3951 else
Bram Moolenaar83d47902020-03-26 20:34:00 +01003952 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003953
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003954 // Use the actual color for the GUI and when 'termguicolors' is set.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003955#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3956 if (0
3957# ifdef FEAT_GUI
3958 || gui.in_use
3959# endif
3960# ifdef FEAT_TERMGUICOLORS
3961 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003962# ifdef FEAT_VTP
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003963 // Finally get INVALCOLOR on this execution path
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003964 || (!p_tgc && t_colors >= 256)
3965# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003966# endif
3967 )
3968 {
3969 guicolor_T fg_rgb = INVALCOLOR;
3970 guicolor_T bg_rgb = INVALCOLOR;
3971
3972 if (id != 0)
3973 syn_id2colors(id, &fg_rgb, &bg_rgb);
3974
3975# ifdef FEAT_GUI
3976 if (gui.in_use)
3977 {
3978 if (fg_rgb == INVALCOLOR)
3979 fg_rgb = gui.norm_pixel;
3980 if (bg_rgb == INVALCOLOR)
3981 bg_rgb = gui.back_pixel;
3982 }
3983# ifdef FEAT_TERMGUICOLORS
3984 else
3985# endif
3986# endif
3987# ifdef FEAT_TERMGUICOLORS
3988 {
3989 if (fg_rgb == INVALCOLOR)
3990 fg_rgb = cterm_normal_fg_gui_color;
3991 if (bg_rgb == INVALCOLOR)
3992 bg_rgb = cterm_normal_bg_gui_color;
3993 }
3994# endif
3995 if (fg_rgb != INVALCOLOR)
3996 {
3997 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3998
3999 fg->red = (unsigned)(rgb >> 16);
4000 fg->green = (unsigned)(rgb >> 8) & 255;
4001 fg->blue = (unsigned)rgb & 255;
4002 }
4003 if (bg_rgb != INVALCOLOR)
4004 {
4005 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
4006
4007 bg->red = (unsigned)(rgb >> 16);
4008 bg->green = (unsigned)(rgb >> 8) & 255;
4009 bg->blue = (unsigned)rgb & 255;
4010 }
4011 }
4012 else
4013#endif
4014 if (id != 0 && t_colors >= 16)
4015 {
Milly7b5f45b2021-10-15 22:25:43 +01004016 int cterm_fg = -1;
4017 int cterm_bg = -1;
4018 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01004019
4020 if (cterm_fg >= 0)
4021 cterm_color2vterm(cterm_fg, fg);
4022 if (cterm_bg >= 0)
4023 cterm_color2vterm(cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004024 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004025 else
4026 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004027#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004028 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004029#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004030
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004031 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004032 if (cterm_normal_fg_color > 0)
4033 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004034 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004035# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4036# ifdef VIMDLL
4037 if (!gui.in_use)
4038# endif
4039 {
4040 tmp = fg->red;
4041 fg->red = fg->blue;
4042 fg->blue = tmp;
4043 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004044# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004045 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004046# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004047 else
4048 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004049# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004050
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004051 if (cterm_normal_bg_color > 0)
4052 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004053 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004054# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4055# ifdef VIMDLL
4056 if (!gui.in_use)
4057# endif
4058 {
4059 tmp = fg->red;
4060 fg->red = fg->blue;
4061 fg->blue = tmp;
4062 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004063# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004064 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004065# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004066 else
4067 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004068# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004069 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004070}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004071
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004072#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4073/*
4074 * Set the 16 ANSI colors from array of RGB values
4075 */
4076 static void
4077set_vterm_palette(VTerm *vterm, long_u *rgb)
4078{
4079 int index = 0;
4080 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004081
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004082 for (; index < 16; index++)
4083 {
4084 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004085
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004086 color.red = (unsigned)(rgb[index] >> 16);
4087 color.green = (unsigned)(rgb[index] >> 8) & 255;
4088 color.blue = (unsigned)rgb[index] & 255;
4089 vterm_state_set_palette_color(state, index, &color);
4090 }
4091}
4092
4093/*
4094 * Set the ANSI color palette from a list of colors
4095 */
4096 static int
4097set_ansi_colors_list(VTerm *vterm, list_T *list)
4098{
4099 int n = 0;
4100 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004101 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004102
Bram Moolenaarb0992022020-01-30 14:55:42 +01004103 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004104 {
4105 char_u *color_name;
4106 guicolor_T guicolor;
4107
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004108 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004109 if (color_name == NULL)
4110 return FAIL;
4111
4112 guicolor = GUI_GET_COLOR(color_name);
4113 if (guicolor == INVALCOLOR)
4114 return FAIL;
4115
4116 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4117 }
4118
4119 if (n != 16 || li != NULL)
4120 return FAIL;
4121
4122 set_vterm_palette(vterm, rgb);
4123
4124 return OK;
4125}
4126
4127/*
4128 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4129 */
4130 static void
4131init_vterm_ansi_colors(VTerm *vterm)
4132{
4133 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4134
4135 if (var != NULL
4136 && (var->di_tv.v_type != VAR_LIST
4137 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004138 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004139 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004140 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004141}
4142#endif
4143
Bram Moolenaar52acb112018-03-18 19:20:22 +01004144/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004145 * Handles a "drop" command from the job in the terminal.
4146 * "item" is the file name, "item->li_next" may have options.
4147 */
4148 static void
4149handle_drop_command(listitem_T *item)
4150{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004151 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004152 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004153 int bufnr;
4154 win_T *wp;
4155 tabpage_T *tp;
4156 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004157 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004158
4159 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4160 FOR_ALL_TAB_WINDOWS(tp, wp)
4161 {
4162 if (wp->w_buffer->b_fnum == bufnr)
4163 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004164 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004165 goto_tabpage_win(tp, wp);
4166 return;
4167 }
4168 }
4169
Bram Moolenaara80faa82020-04-12 19:37:17 +02004170 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004171
4172 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4173 && opt_item->li_tv.vval.v_dict != NULL)
4174 {
4175 dict_T *dict = opt_item->li_tv.vval.v_dict;
4176 char_u *p;
4177
Bram Moolenaar8f667172018-12-14 15:38:31 +01004178 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004179 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004180 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004181 if (p != NULL)
4182 {
4183 if (check_ff_value(p) == FAIL)
4184 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4185 else
4186 ea.force_ff = *p;
4187 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004188 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004189 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004190 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004191 if (p != NULL)
4192 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004193 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004194 if (ea.cmd != NULL)
4195 {
4196 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4197 ea.force_enc = 11;
4198 tofree = ea.cmd;
4199 }
4200 }
4201
Bram Moolenaar8f667172018-12-14 15:38:31 +01004202 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004203 if (p != NULL)
4204 get_bad_opt(p, &ea);
4205
4206 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4207 ea.force_bin = FORCE_BIN;
4208 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4209 ea.force_bin = FORCE_BIN;
4210 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4211 ea.force_bin = FORCE_NOBIN;
4212 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4213 ea.force_bin = FORCE_NOBIN;
4214 }
4215
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004216 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004217 if (ea.cmd == NULL)
4218 ea.cmd = (char_u *)"split";
4219 ea.arg = fname;
4220 ea.cmdidx = CMD_split;
4221 ex_splitview(&ea);
4222
4223 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004224}
4225
4226/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004227 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4228 */
4229 static int
4230is_permitted_term_api(char_u *func, char_u *pat)
4231{
4232 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4233}
4234
4235/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004236 * Handles a function call from the job running in a terminal.
4237 * "item" is the function name, "item->li_next" has the arguments.
4238 */
4239 static void
4240handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4241{
4242 char_u *func;
4243 typval_T argvars[2];
4244 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004245 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004246
4247 if (item->li_next == NULL)
4248 {
4249 ch_log(channel, "Missing function arguments for call");
4250 return;
4251 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004252 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004253
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004254 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004255 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004256 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004257 return;
4258 }
4259
4260 argvars[0].v_type = VAR_NUMBER;
4261 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4262 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004263 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004264 funcexe.firstline = 1L;
4265 funcexe.lastline = 1L;
4266 funcexe.evaluate = TRUE;
4267 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004268 {
4269 clear_tv(&rettv);
4270 ch_log(channel, "Function %s called", func);
4271 }
4272 else
4273 ch_log(channel, "Calling function %s failed", func);
4274}
4275
4276/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004277 * URL decoding (also know as Percent-encoding).
4278 *
4279 * Note this function currently is only used for decoding shell's
4280 * OSC 7 escape sequence which we can assume all bytes are valid
4281 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4282 * encoding bytes like 0xfe, 0xff.
4283 */
4284 static size_t
4285url_decode(const char *src, const size_t len, char_u *dst)
4286{
4287 size_t i = 0, j = 0;
4288
4289 while (i < len)
4290 {
4291 if (src[i] == '%' && i + 2 < len)
4292 {
4293 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4294 j++;
4295 i += 3;
4296 }
4297 else
4298 {
4299 dst[j] = src[i];
4300 i++;
4301 j++;
4302 }
4303 }
4304 dst[j] = '\0';
4305 return j;
4306}
4307
4308/*
4309 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4310 *
4311 * The OSC 7 sequence has the format of
4312 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4313 * and what VTerm provides via VTermStringFragment is
4314 * "file://HOSTNAME/CURRENT/DIR"
4315 */
4316 static void
4317sync_shell_dir(VTermStringFragment *frag)
4318{
4319 int offset = 7; // len of "file://" is 7
4320 char *pos = (char *)frag->str + offset;
4321 char_u *new_dir;
4322
4323 // remove HOSTNAME to get PWD
Bram Moolenaar918b0892021-05-08 20:09:24 +02004324 while (*pos != '/' && offset < (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004325 {
4326 offset += 1;
4327 pos += 1;
4328 }
4329
Bram Moolenaar918b0892021-05-08 20:09:24 +02004330 if (offset >= (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004331 {
4332 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
4333 frag->str);
4334 return;
4335 }
4336
4337 new_dir = alloc(frag->len - offset + 1);
4338 url_decode(pos, frag->len-offset, new_dir);
4339 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4340 vim_free(new_dir);
4341}
4342
4343/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004344 * Called by libvterm when it cannot recognize an OSC sequence.
4345 * We recognize a terminal API command.
4346 */
4347 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004348parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004349{
4350 term_T *term = (term_T *)user;
4351 js_read_T reader;
4352 typval_T tv;
4353 channel_T *channel = term->tl_job == NULL ? NULL
4354 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004355 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004356
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004357 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
4358 if (p_asd && command == 7)
4359 {
4360 sync_shell_dir(&frag);
4361 return 1;
4362 }
4363
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004364 if (command != 51)
4365 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004366
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004367 // Concatenate what was received until the final piece is found.
4368 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4369 {
4370 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004371 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004372 }
4373 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004374 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004375 if (!frag.final)
4376 return 1;
4377
4378 ((char *)gap->ga_data)[gap->ga_len] = 0;
4379 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004380 reader.js_fill = NULL;
4381 reader.js_used = 0;
4382 if (json_decode(&reader, &tv, 0) == OK
4383 && tv.v_type == VAR_LIST
4384 && tv.vval.v_list != NULL)
4385 {
4386 listitem_T *item = tv.vval.v_list->lv_first;
4387
4388 if (item == NULL)
4389 ch_log(channel, "Missing command");
4390 else
4391 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004392 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004393
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004394 // Make sure an invoked command doesn't delete the buffer (and the
4395 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004396 ++term->tl_buffer->b_locked;
4397
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004398 item = item->li_next;
4399 if (item == NULL)
4400 ch_log(channel, "Missing argument for %s", cmd);
4401 else if (STRCMP(cmd, "drop") == 0)
4402 handle_drop_command(item);
4403 else if (STRCMP(cmd, "call") == 0)
4404 handle_call_command(term, channel, item);
4405 else
4406 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004407 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004408 }
4409 }
4410 else
4411 ch_log(channel, "Invalid JSON received");
4412
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004413 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004414 clear_tv(&tv);
4415 return 1;
4416}
4417
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004418/*
4419 * Called by libvterm when it cannot recognize a CSI sequence.
4420 * We recognize the window position report.
4421 */
4422 static int
4423parse_csi(
4424 const char *leader UNUSED,
4425 const long args[],
4426 int argcount,
4427 const char *intermed UNUSED,
4428 char command,
4429 void *user)
4430{
4431 term_T *term = (term_T *)user;
4432 char buf[100];
4433 int len;
4434 int x = 0;
4435 int y = 0;
4436 win_T *wp;
4437
4438 // We recognize only CSI 13 t
4439 if (command != 't' || argcount != 1 || args[0] != 13)
4440 return 0; // not handled
4441
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004442 // When getting the window position is not possible or it fails it results
4443 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004444#if defined(FEAT_GUI) \
4445 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4446 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004447 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004448#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004449
4450 FOR_ALL_WINDOWS(wp)
4451 if (wp->w_buffer == term->tl_buffer)
4452 break;
4453 if (wp != NULL)
4454 {
4455#ifdef FEAT_GUI
4456 if (gui.in_use)
4457 {
4458 x += wp->w_wincol * gui.char_width;
4459 y += W_WINROW(wp) * gui.char_height;
4460 }
4461 else
4462#endif
4463 {
4464 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004465 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004466 x += wp->w_wincol * 7;
4467 y += W_WINROW(wp) * 10;
4468 }
4469 }
4470
4471 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4472 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4473 (char_u *)buf, len, NULL);
4474 return 1;
4475}
4476
Bram Moolenaard8637282020-05-20 18:41:41 +02004477static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004478 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004479 parse_csi, // csi
4480 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004481 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004482};
4483
4484/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004485 * Use Vim's allocation functions for vterm so profiling works.
4486 */
4487 static void *
4488vterm_malloc(size_t size, void *data UNUSED)
4489{
Bram Moolenaar88137392021-11-12 16:01:15 +00004490 // make sure that the length is not zero
4491 return alloc_clear(size == 0 ? 1L : size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004492}
4493
4494 static void
4495vterm_memfree(void *ptr, void *data UNUSED)
4496{
4497 vim_free(ptr);
4498}
4499
4500static VTermAllocatorFunctions vterm_allocator = {
4501 &vterm_malloc,
4502 &vterm_memfree
4503};
4504
4505/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004506 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004507 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004508 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004509 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004510create_vterm(term_T *term, int rows, int cols)
4511{
4512 VTerm *vterm;
4513 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004514 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004515 VTermValue value;
4516
Bram Moolenaar756ef112018-04-10 12:04:27 +02004517 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004518 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004519 if (vterm == NULL)
4520 return FAIL;
4521
4522 // Allocate screen and state here, so we can bail out if that fails.
4523 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004524 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004525 if (state == NULL || screen == NULL)
4526 {
4527 vterm_free(vterm);
4528 return FAIL;
4529 }
4530
Bram Moolenaar52acb112018-03-18 19:20:22 +01004531 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004532 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004533 vterm_set_utf8(vterm, 1);
4534
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004535 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004536
4537 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004538 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004539 &term->tl_default_color.fg,
4540 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004541
Bram Moolenaar9e587872019-05-13 20:27:23 +02004542 if (t_colors < 16)
4543 // Less than 16 colors: assume that bold means using a bright color for
4544 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004545 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4546
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004547 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004548 vterm_screen_reset(screen, 1 /* hard */);
4549
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004550 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004551 vterm_screen_enable_altscreen(screen, 1);
4552
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004553 // For unix do not use a blinking cursor. In an xterm this causes the
4554 // cursor to blink if it's blinking in the xterm.
4555 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004556#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004557 if (GetCaretBlinkTime() == INFINITE)
4558 value.boolean = 0;
4559 else
4560 value.boolean = 1;
4561#else
4562 value.boolean = 0;
4563#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004564 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004565 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004566
4567 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004568}
4569
4570/*
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004571 * Called when 'wincolor' was set.
4572 */
4573 void
Bram Moolenaarad431992021-05-03 20:40:38 +02004574term_update_colors(term_T *term)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004575{
Bram Moolenaarad431992021-05-03 20:40:38 +02004576 win_T *wp;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004577
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01004578 if (term->tl_vterm == NULL)
4579 return;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004580 init_default_colors(term, curwin);
4581 vterm_state_set_default_colors(
4582 vterm_obtain_state(term->tl_vterm),
4583 &term->tl_default_color.fg,
4584 &term->tl_default_color.bg);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01004585
Bram Moolenaarad431992021-05-03 20:40:38 +02004586 FOR_ALL_WINDOWS(wp)
4587 if (wp->w_buffer == term->tl_buffer)
4588 redraw_win_later(wp, NOT_VALID);
4589}
4590
4591/*
4592 * Called when 'background' was set.
4593 */
4594 void
4595term_update_colors_all(void)
4596{
4597 term_T *tp;
4598
4599 FOR_ALL_TERMS(tp)
4600 term_update_colors(tp);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004601}
4602
4603/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004604 * Return the text to show for the buffer name and status.
4605 */
4606 char_u *
4607term_get_status_text(term_T *term)
4608{
4609 if (term->tl_status_text == NULL)
4610 {
4611 char_u *txt;
4612 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004613 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004614
4615 if (term->tl_normal_mode)
4616 {
4617 if (term_job_running(term))
4618 txt = (char_u *)_("Terminal");
4619 else
4620 txt = (char_u *)_("Terminal-finished");
4621 }
4622 else if (term->tl_title != NULL)
4623 txt = term->tl_title;
4624 else if (term_none_open(term))
4625 txt = (char_u *)_("active");
4626 else if (term_job_running(term))
4627 txt = (char_u *)_("running");
4628 else
4629 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004630 fname = buf_get_fname(term->tl_buffer);
4631 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004632 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004633 if (term->tl_status_text != NULL)
4634 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004635 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004636 }
4637 return term->tl_status_text;
4638}
4639
4640/*
4641 * Mark references in jobs of terminals.
4642 */
4643 int
4644set_ref_in_term(int copyID)
4645{
4646 int abort = FALSE;
4647 term_T *term;
4648 typval_T tv;
4649
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004650 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004651 if (term->tl_job != NULL)
4652 {
4653 tv.v_type = VAR_JOB;
4654 tv.vval.v_job = term->tl_job;
4655 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4656 }
4657 return abort;
4658}
4659
4660/*
4661 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004662 * Returns NULL when the buffer is not for a terminal window and logs a message
4663 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004664 */
4665 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004666term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004667{
4668 buf_T *buf;
4669
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004670 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004671 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004672 --emsg_off;
4673 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004674 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004675 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004676 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004677 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004678 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004679 return buf;
4680}
4681
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004682 static void
4683clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004684{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004685 CLEAR_FIELD(*cell);
4686 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4687 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004688}
4689
4690 static void
4691dump_term_color(FILE *fd, VTermColor *color)
4692{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004693 int index;
4694
4695 if (VTERM_COLOR_IS_INDEXED(color))
4696 index = color->index + 1;
4697 else if (color->type == 0)
4698 // use RGB values
4699 index = 255;
4700 else
4701 // default color
4702 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004703 fprintf(fd, "%02x%02x%02x%d",
4704 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004705 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004706}
4707
4708/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004709 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004710 *
4711 * Each screen cell in full is:
4712 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4713 * {characters} is a space for an empty cell
4714 * For a double-width character "+" is changed to "*" and the next cell is
4715 * skipped.
4716 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4717 * when "&" use the same as the previous cell.
4718 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4719 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4720 * {color-idx} is a number from 0 to 255
4721 *
4722 * Screen cell with same width, attributes and color as the previous one:
4723 * |{characters}
4724 *
4725 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4726 *
4727 * Repeating the previous screen cell:
4728 * @{count}
4729 */
4730 void
4731f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4732{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004733 buf_T *buf;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004734 term_T *term;
4735 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004736 int max_height = 0;
4737 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004738 stat_T st;
4739 FILE *fd;
4740 VTermPos pos;
4741 VTermScreen *screen;
4742 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004743 VTermState *state;
4744 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004745
4746 if (check_restricted() || check_secure())
4747 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004748
4749 if (in_vim9script()
4750 && (check_for_buffer_arg(argvars, 0) == FAIL
4751 || check_for_string_arg(argvars, 1) == FAIL
4752 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4753 return;
4754
4755 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004756 if (buf == NULL)
4757 return;
4758 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004759 if (term->tl_vterm == NULL)
4760 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004761 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004762 return;
4763 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004764
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004765 if (argvars[2].v_type != VAR_UNKNOWN)
4766 {
4767 dict_T *d;
4768
4769 if (argvars[2].v_type != VAR_DICT)
4770 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004771 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004772 return;
4773 }
4774 d = argvars[2].vval.v_dict;
4775 if (d != NULL)
4776 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004777 max_height = dict_get_number(d, (char_u *)"rows");
4778 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004779 }
4780 }
4781
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004782 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004783 if (fname == NULL)
4784 return;
4785 if (mch_stat((char *)fname, &st) >= 0)
4786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004787 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004788 return;
4789 }
4790
Bram Moolenaard96ff162018-02-18 22:13:29 +01004791 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4792 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004793 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004794 return;
4795 }
4796
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004797 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004798
4799 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004800 state = vterm_obtain_state(term->tl_vterm);
4801 vterm_state_get_cursorpos(state, &cursor_pos);
4802
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004803 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4804 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004805 {
4806 int repeat = 0;
4807
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004808 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4809 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004810 {
4811 VTermScreenCell cell;
4812 int same_attr;
4813 int same_chars = TRUE;
4814 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004815 int is_cursor_pos = (pos.col == cursor_pos.col
4816 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004817
4818 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004819 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004820
4821 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4822 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004823 int c = cell.chars[i];
4824 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004825 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004826
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004827 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004828 if (i == 0)
4829 {
4830 c = (c == NUL) ? ' ' : c;
4831 pc = (pc == NUL) ? ' ' : pc;
4832 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004833 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004834 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004835 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004836 break;
4837 }
4838 same_attr = vtermAttr2hl(cell.attrs)
4839 == vtermAttr2hl(prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004840 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4841 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004842 if (same_chars && cell.width == prev_cell.width && same_attr
4843 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004844 {
4845 ++repeat;
4846 }
4847 else
4848 {
4849 if (repeat > 0)
4850 {
4851 fprintf(fd, "@%d", repeat);
4852 repeat = 0;
4853 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004854 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004855
4856 if (cell.chars[0] == NUL)
4857 fputs(" ", fd);
4858 else
4859 {
4860 char_u charbuf[10];
4861 int len;
4862
4863 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4864 && cell.chars[i] != NUL; ++i)
4865 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004866 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004867 fwrite(charbuf, len, 1, fd);
4868 }
4869 }
4870
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004871 // When only the characters differ we don't write anything, the
4872 // following "|", "@" or NL will indicate using the same
4873 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004874 if (cell.width != prev_cell.width || !same_attr)
4875 {
4876 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004877 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004878 else
4879 fputs("+", fd);
4880
4881 if (same_attr)
4882 {
4883 fputs("&", fd);
4884 }
4885 else
4886 {
4887 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004888 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004889 fputs("&", fd);
4890 else
4891 {
4892 fputs("#", fd);
4893 dump_term_color(fd, &cell.fg);
4894 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004895 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004896 fputs("&", fd);
4897 else
4898 {
4899 fputs("#", fd);
4900 dump_term_color(fd, &cell.bg);
4901 }
4902 }
4903 }
4904
4905 prev_cell = cell;
4906 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004907
4908 if (cell.width == 2)
4909 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004910 }
4911 if (repeat > 0)
4912 fprintf(fd, "@%d", repeat);
4913 fputs("\n", fd);
4914 }
4915
4916 fclose(fd);
4917}
4918
4919/*
4920 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4921 */
4922 static void
4923dump_is_corrupt(garray_T *gap)
4924{
4925 ga_concat(gap, (char_u *)"CORRUPT");
4926}
4927
4928 static void
4929append_cell(garray_T *gap, cellattr_T *cell)
4930{
4931 if (ga_grow(gap, 1) == OK)
4932 {
4933 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4934 ++gap->ga_len;
4935 }
4936}
4937
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004938 static void
4939clear_cellattr(cellattr_T *cell)
4940{
4941 CLEAR_FIELD(*cell);
4942 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4943 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4944}
4945
Bram Moolenaard96ff162018-02-18 22:13:29 +01004946/*
4947 * Read the dump file from "fd" and append lines to the current buffer.
4948 * Return the cell width of the longest line.
4949 */
4950 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004951read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004952{
4953 int c;
4954 garray_T ga_text;
4955 garray_T ga_cell;
4956 char_u *prev_char = NULL;
4957 int attr = 0;
4958 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004959 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004960 term_T *term = curbuf->b_term;
4961 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004962 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004963
4964 ga_init2(&ga_text, 1, 90);
4965 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004966 clear_cellattr(&cell);
4967 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004968 cursor_pos->row = -1;
4969 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004970
4971 c = fgetc(fd);
4972 for (;;)
4973 {
4974 if (c == EOF)
4975 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004976 if (c == '\r')
4977 {
4978 // DOS line endings? Ignore.
4979 c = fgetc(fd);
4980 }
4981 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004982 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004983 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004984 if (ga_text.ga_data == NULL)
4985 dump_is_corrupt(&ga_text);
4986 if (ga_grow(&term->tl_scrollback, 1) == OK)
4987 {
4988 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
4989 + term->tl_scrollback.ga_len;
4990
4991 if (max_cells < ga_cell.ga_len)
4992 max_cells = ga_cell.ga_len;
4993 line->sb_cols = ga_cell.ga_len;
4994 line->sb_cells = ga_cell.ga_data;
4995 line->sb_fill_attr = term->tl_default_color;
4996 ++term->tl_scrollback.ga_len;
4997 ga_init(&ga_cell);
4998
4999 ga_append(&ga_text, NUL);
5000 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5001 ga_text.ga_len, FALSE);
5002 }
5003 else
5004 ga_clear(&ga_cell);
5005 ga_text.ga_len = 0;
5006
5007 c = fgetc(fd);
5008 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005009 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005010 {
5011 int prev_len = ga_text.ga_len;
5012
Bram Moolenaar9271d052018-02-25 21:39:46 +01005013 if (c == '>')
5014 {
5015 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005016 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005017 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5018 cursor_pos->col = ga_cell.ga_len;
5019 }
5020
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005021 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005022 c = fgetc(fd);
5023 if (c != EOF)
5024 ga_append(&ga_text, c);
5025 for (;;)
5026 {
5027 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005028 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005029 || c == EOF || c == '\n')
5030 break;
5031 ga_append(&ga_text, c);
5032 }
5033
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005034 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005035 vim_free(prev_char);
5036 if (ga_text.ga_data != NULL)
5037 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5038 ga_text.ga_len - prev_len);
5039
Bram Moolenaar9271d052018-02-25 21:39:46 +01005040 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005041 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005042 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005043 }
5044 else if (c == '+' || c == '*')
5045 {
5046 int is_bg;
5047
5048 cell.width = c == '+' ? 1 : 2;
5049
5050 c = fgetc(fd);
5051 if (c == '&')
5052 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005053 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005054 c = fgetc(fd);
5055 }
5056 else if (isdigit(c))
5057 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005058 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005059 attr = 0;
5060 while (isdigit(c))
5061 {
5062 attr = attr * 10 + (c - '0');
5063 c = fgetc(fd);
5064 }
5065 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005066
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005067 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005068 for (is_bg = 0; is_bg <= 1; ++is_bg)
5069 {
5070 if (c == '&')
5071 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005072 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005073 c = fgetc(fd);
5074 }
5075 else if (c == '#')
5076 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005077 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005078
5079 c = fgetc(fd);
5080 red = hex2nr(c);
5081 c = fgetc(fd);
5082 red = (red << 4) + hex2nr(c);
5083 c = fgetc(fd);
5084 green = hex2nr(c);
5085 c = fgetc(fd);
5086 green = (green << 4) + hex2nr(c);
5087 c = fgetc(fd);
5088 blue = hex2nr(c);
5089 c = fgetc(fd);
5090 blue = (blue << 4) + hex2nr(c);
5091 c = fgetc(fd);
5092 if (!isdigit(c))
5093 dump_is_corrupt(&ga_text);
5094 while (isdigit(c))
5095 {
5096 index = index * 10 + (c - '0');
5097 c = fgetc(fd);
5098 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005099 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005100 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005101 type = VTERM_COLOR_RGB;
5102 if (index == 0)
5103 {
5104 if (is_bg)
5105 type |= VTERM_COLOR_DEFAULT_BG;
5106 else
5107 type |= VTERM_COLOR_DEFAULT_FG;
5108 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005109 }
5110 else
5111 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005112 type = VTERM_COLOR_INDEXED;
5113 index -= 1;
5114 }
5115 if (is_bg)
5116 {
5117 cell.bg.type = type;
5118 cell.bg.red = red;
5119 cell.bg.green = green;
5120 cell.bg.blue = blue;
5121 cell.bg.index = index;
5122 }
5123 else
5124 {
5125 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005126 cell.fg.red = red;
5127 cell.fg.green = green;
5128 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005129 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005130 }
5131 }
5132 else
5133 dump_is_corrupt(&ga_text);
5134 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005135 }
5136 else
5137 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005138 }
5139 else
5140 dump_is_corrupt(&ga_text);
5141
5142 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005143 if (cell.width == 2)
5144 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005145 }
5146 else if (c == '@')
5147 {
5148 if (prev_char == NULL)
5149 dump_is_corrupt(&ga_text);
5150 else
5151 {
5152 int count = 0;
5153
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005154 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005155 for (;;)
5156 {
5157 c = fgetc(fd);
5158 if (!isdigit(c))
5159 break;
5160 count = count * 10 + (c - '0');
5161 }
5162
5163 while (count-- > 0)
5164 {
5165 ga_concat(&ga_text, prev_char);
5166 append_cell(&ga_cell, &cell);
5167 }
5168 }
5169 }
5170 else
5171 {
5172 dump_is_corrupt(&ga_text);
5173 c = fgetc(fd);
5174 }
5175 }
5176
5177 if (ga_text.ga_len > 0)
5178 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005179 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005180 dump_is_corrupt(&ga_text);
5181 ga_append(&ga_text, NUL);
5182 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5183 ga_text.ga_len, FALSE);
5184 }
5185
5186 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005187 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005188 vim_free(prev_char);
5189
5190 return max_cells;
5191}
5192
5193/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005194 * Return an allocated string with at least "text_width" "=" characters and
5195 * "fname" inserted in the middle.
5196 */
5197 static char_u *
5198get_separator(int text_width, char_u *fname)
5199{
5200 int width = MAX(text_width, curwin->w_width);
5201 char_u *textline;
5202 int fname_size;
5203 char_u *p = fname;
5204 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005205 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005206
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005207 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005208 if (textline == NULL)
5209 return NULL;
5210
5211 fname_size = vim_strsize(fname);
5212 if (fname_size < width - 8)
5213 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005214 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005215 width = MAX(text_width, fname_size + 8);
5216 }
5217 else if (fname_size > width - 8)
5218 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005219 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005220 p = gettail(fname);
5221 fname_size = vim_strsize(p);
5222 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005223 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005224 while (fname_size > width - 8)
5225 {
5226 p += (*mb_ptr2len)(p);
5227 fname_size = vim_strsize(p);
5228 }
5229
5230 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5231 textline[i] = '=';
5232 textline[i++] = ' ';
5233
5234 STRCPY(textline + i, p);
5235 off = STRLEN(textline);
5236 textline[off] = ' ';
5237 for (i = 1; i < (width - fname_size) / 2; ++i)
5238 textline[off + i] = '=';
5239 textline[off + i] = NUL;
5240
5241 return textline;
5242}
5243
5244/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005245 * Common for "term_dumpdiff()" and "term_dumpload()".
5246 */
5247 static void
5248term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5249{
5250 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005251 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005252 char_u buf1[NUMBUFLEN];
5253 char_u buf2[NUMBUFLEN];
5254 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005255 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005256 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005257 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005258 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005259 char_u *textline = NULL;
5260
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005261 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005262 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005263 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005264 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005265 if (fname1 == NULL || (do_diff && fname2 == NULL))
5266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005267 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005268 return;
5269 }
5270 fd1 = mch_fopen((char *)fname1, READBIN);
5271 if (fd1 == NULL)
5272 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005273 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005274 return;
5275 }
5276 if (do_diff)
5277 {
5278 fd2 = mch_fopen((char *)fname2, READBIN);
5279 if (fd2 == NULL)
5280 {
5281 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005282 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005283 return;
5284 }
5285 }
5286
5287 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005288 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5289 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5290 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5291 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5292 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005293
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005294 if (opt.jo_term_name == NULL)
5295 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005296 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005297
Bram Moolenaar51e14382019-05-25 20:21:28 +02005298 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005299 if (fname_tofree != NULL)
5300 {
5301 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5302 opt.jo_term_name = fname_tofree;
5303 }
5304 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005305
Bram Moolenaar87abab92019-06-03 21:14:59 +02005306 if (opt.jo_bufnr_buf != NULL)
5307 {
5308 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5309
5310 // With "bufnr" argument: enter the window with this buffer and make it
5311 // empty.
5312 if (wp == NULL)
5313 semsg(_(e_invarg2), "bufnr");
5314 else
5315 {
5316 buf = curbuf;
5317 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005318 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005319 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005320 redraw_later(NOT_VALID);
5321 }
5322 }
5323 else
5324 // Create a new terminal window.
5325 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5326
Bram Moolenaard96ff162018-02-18 22:13:29 +01005327 if (buf != NULL && buf->b_term != NULL)
5328 {
5329 int i;
5330 linenr_T bot_lnum;
5331 linenr_T lnum;
5332 term_T *term = buf->b_term;
5333 int width;
5334 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005335 VTermPos cursor_pos1;
5336 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005337
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005338 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005339
Bram Moolenaard96ff162018-02-18 22:13:29 +01005340 rettv->vval.v_number = buf->b_fnum;
5341
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005342 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005343 width = read_dump_file(fd1, &cursor_pos1);
5344
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005345 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005346 if (cursor_pos1.row >= 0)
5347 {
5348 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5349 coladvance(cursor_pos1.col);
5350 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005351
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005352 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005353 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005354
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005355 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005356 if (!do_diff)
5357 goto theend;
5358
5359 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5360
Bram Moolenaar4a696342018-04-05 18:45:26 +02005361 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005362 if (textline == NULL)
5363 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005364 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5365 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5366 vim_free(textline);
5367
5368 textline = get_separator(width, fname2);
5369 if (textline == NULL)
5370 goto theend;
5371 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5372 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005373 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005374
5375 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005376 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005377 if (width2 > width)
5378 {
5379 vim_free(textline);
5380 textline = alloc(width2 + 1);
5381 if (textline == NULL)
5382 goto theend;
5383 width = width2;
5384 textline[width] = NUL;
5385 }
5386 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5387
5388 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5389 {
5390 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5391 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005392 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005393 for (i = 0; i < width; ++i)
5394 textline[i] = '-';
5395 }
5396 else
5397 {
5398 char_u *line1;
5399 char_u *line2;
5400 char_u *p1;
5401 char_u *p2;
5402 int col;
5403 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5404 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5405 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5406 ->sb_cells;
5407
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005408 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005409 line1 = vim_strsave(ml_get(lnum));
5410 if (line1 == NULL)
5411 break;
5412 p1 = line1;
5413
5414 line2 = ml_get(lnum + bot_lnum);
5415 p2 = line2;
5416 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5417 {
5418 int len1 = utfc_ptr2len(p1);
5419 int len2 = utfc_ptr2len(p2);
5420
5421 textline[col] = ' ';
5422 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005423 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005424 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005425 else if (lnum == cursor_pos1.row + 1
5426 && col == cursor_pos1.col
5427 && (cursor_pos1.row != cursor_pos2.row
5428 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005429 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005430 textline[col] = '>';
5431 else if (lnum == cursor_pos2.row + 1
5432 && col == cursor_pos2.col
5433 && (cursor_pos1.row != cursor_pos2.row
5434 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005435 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005436 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005437 else if (cellattr1 != NULL && cellattr2 != NULL)
5438 {
5439 if ((cellattr1 + col)->width
5440 != (cellattr2 + col)->width)
5441 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005442 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005443 &(cellattr2 + col)->fg))
5444 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005445 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005446 &(cellattr2 + col)->bg))
5447 textline[col] = 'b';
5448 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5449 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5450 textline[col] = 'a';
5451 }
5452 p1 += len1;
5453 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005454 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005455 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005456
5457 while (col < width)
5458 {
5459 if (*p1 == NUL && *p2 == NUL)
5460 textline[col] = '?';
5461 else if (*p1 == NUL)
5462 {
5463 textline[col] = '+';
5464 p2 += utfc_ptr2len(p2);
5465 }
5466 else
5467 {
5468 textline[col] = '-';
5469 p1 += utfc_ptr2len(p1);
5470 }
5471 ++col;
5472 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005473
5474 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005475 }
5476 if (add_empty_scrollback(term, &term->tl_default_color,
5477 term->tl_top_diff_rows) == OK)
5478 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5479 ++bot_lnum;
5480 }
5481
5482 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5483 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005484 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005485 for (i = 0; i < width; ++i)
5486 textline[i] = '+';
5487 if (add_empty_scrollback(term, &term->tl_default_color,
5488 term->tl_top_diff_rows) == OK)
5489 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5490 ++lnum;
5491 ++bot_lnum;
5492 }
5493
5494 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005495
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005496 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005497 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005498 }
5499
5500theend:
5501 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005502 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005503 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005504 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005505 fclose(fd2);
5506}
5507
5508/*
5509 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5510 * bottom files.
5511 * Return FAIL when this is not possible.
5512 */
5513 int
5514term_swap_diff()
5515{
5516 term_T *term = curbuf->b_term;
5517 linenr_T line_count;
5518 linenr_T top_rows;
5519 linenr_T bot_rows;
5520 linenr_T bot_start;
5521 linenr_T lnum;
5522 char_u *p;
5523 sb_line_T *sb_line;
5524
5525 if (term == NULL
5526 || !term_is_finished(curbuf)
5527 || term->tl_top_diff_rows == 0
5528 || term->tl_scrollback.ga_len == 0)
5529 return FAIL;
5530
5531 line_count = curbuf->b_ml.ml_line_count;
5532 top_rows = term->tl_top_diff_rows;
5533 bot_rows = term->tl_bot_diff_rows;
5534 bot_start = line_count - bot_rows;
5535 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5536
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005537 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005538 for (lnum = 1; lnum <= top_rows; ++lnum)
5539 {
5540 p = vim_strsave(ml_get(1));
5541 if (p == NULL)
5542 return OK;
5543 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005544 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005545 vim_free(p);
5546 }
5547
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005548 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005549 for (lnum = 1; lnum <= bot_rows; ++lnum)
5550 {
5551 p = vim_strsave(ml_get(bot_start + lnum));
5552 if (p == NULL)
5553 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005554 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005555 ml_append(lnum - 1, p, 0, FALSE);
5556 vim_free(p);
5557 }
5558
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005559 // move top title to bottom
5560 p = vim_strsave(ml_get(bot_rows + 1));
5561 if (p == NULL)
5562 return OK;
5563 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005564 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005565 vim_free(p);
5566
5567 // move bottom title to top
5568 p = vim_strsave(ml_get(line_count - top_rows));
5569 if (p == NULL)
5570 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005571 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005572 ml_append(bot_rows, p, 0, FALSE);
5573 vim_free(p);
5574
Bram Moolenaard96ff162018-02-18 22:13:29 +01005575 if (top_rows == bot_rows)
5576 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005577 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005578 for (lnum = 0; lnum < top_rows; ++lnum)
5579 {
5580 sb_line_T temp;
5581
5582 temp = *(sb_line + lnum);
5583 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5584 *(sb_line + bot_start + lnum) = temp;
5585 }
5586 }
5587 else
5588 {
5589 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005590 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005591
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005592 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005593 if (temp != NULL)
5594 {
5595 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5596 mch_memmove(term->tl_scrollback.ga_data,
5597 temp + bot_start,
5598 sizeof(sb_line_T) * bot_rows);
5599 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5600 temp + top_rows,
5601 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5602 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5603 + line_count - top_rows,
5604 temp,
5605 sizeof(sb_line_T) * top_rows);
5606 vim_free(temp);
5607 }
5608 }
5609
5610 term->tl_top_diff_rows = bot_rows;
5611 term->tl_bot_diff_rows = top_rows;
5612
5613 update_screen(NOT_VALID);
5614 return OK;
5615}
5616
5617/*
5618 * "term_dumpdiff(filename, filename, options)" function
5619 */
5620 void
5621f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5622{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005623 if (in_vim9script()
5624 && (check_for_string_arg(argvars, 0) == FAIL
5625 || check_for_string_arg(argvars, 1) == FAIL
5626 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5627 return;
5628
Bram Moolenaard96ff162018-02-18 22:13:29 +01005629 term_load_dump(argvars, rettv, TRUE);
5630}
5631
5632/*
5633 * "term_dumpload(filename, options)" function
5634 */
5635 void
5636f_term_dumpload(typval_T *argvars, typval_T *rettv)
5637{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005638 if (in_vim9script()
5639 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005640 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005641 return;
5642
Bram Moolenaard96ff162018-02-18 22:13:29 +01005643 term_load_dump(argvars, rettv, FALSE);
5644}
5645
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005646/*
5647 * "term_getaltscreen(buf)" function
5648 */
5649 void
5650f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5651{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005652 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005653
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005654 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5655 return;
5656
5657 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005658 if (buf == NULL)
5659 return;
5660 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5661}
5662
5663/*
5664 * "term_getattr(attr, name)" function
5665 */
5666 void
5667f_term_getattr(typval_T *argvars, typval_T *rettv)
5668{
5669 int attr;
5670 size_t i;
5671 char_u *name;
5672
5673 static struct {
5674 char *name;
5675 int attr;
5676 } attrs[] = {
5677 {"bold", HL_BOLD},
5678 {"italic", HL_ITALIC},
5679 {"underline", HL_UNDERLINE},
5680 {"strike", HL_STRIKETHROUGH},
5681 {"reverse", HL_INVERSE},
5682 };
5683
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005684 if (in_vim9script()
5685 && (check_for_number_arg(argvars, 0) == FAIL
5686 || check_for_string_arg(argvars, 1) == FAIL))
5687 return;
5688
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005689 attr = tv_get_number(&argvars[0]);
5690 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005691 if (name == NULL)
5692 return;
5693
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005694 if (attr > HL_ALL)
5695 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02005696 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005697 if (STRCMP(name, attrs[i].name) == 0)
5698 {
5699 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5700 break;
5701 }
5702}
5703
5704/*
5705 * "term_getcursor(buf)" function
5706 */
5707 void
5708f_term_getcursor(typval_T *argvars, typval_T *rettv)
5709{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005710 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005711 term_T *term;
5712 list_T *l;
5713 dict_T *d;
5714
5715 if (rettv_list_alloc(rettv) == FAIL)
5716 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005717
5718 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5719 return;
5720
5721 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005722 if (buf == NULL)
5723 return;
5724 term = buf->b_term;
5725
5726 l = rettv->vval.v_list;
5727 list_append_number(l, term->tl_cursor_pos.row + 1);
5728 list_append_number(l, term->tl_cursor_pos.col + 1);
5729
5730 d = dict_alloc();
5731 if (d != NULL)
5732 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005733 dict_add_number(d, "visible", term->tl_cursor_visible);
5734 dict_add_number(d, "blink", blink_state_is_inverted()
5735 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5736 dict_add_number(d, "shape", term->tl_cursor_shape);
5737 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005738 list_append_dict(l, d);
5739 }
5740}
5741
5742/*
5743 * "term_getjob(buf)" function
5744 */
5745 void
5746f_term_getjob(typval_T *argvars, typval_T *rettv)
5747{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005748 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005749
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005750 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5751 return;
5752
5753 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005754 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005755 {
5756 rettv->v_type = VAR_SPECIAL;
5757 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005758 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005759 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005760
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005761 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005762 rettv->vval.v_job = buf->b_term->tl_job;
5763 if (rettv->vval.v_job != NULL)
5764 ++rettv->vval.v_job->jv_refcount;
5765}
5766
5767 static int
5768get_row_number(typval_T *tv, term_T *term)
5769{
5770 if (tv->v_type == VAR_STRING
5771 && tv->vval.v_string != NULL
5772 && STRCMP(tv->vval.v_string, ".") == 0)
5773 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005774 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005775}
5776
5777/*
5778 * "term_getline(buf, row)" function
5779 */
5780 void
5781f_term_getline(typval_T *argvars, typval_T *rettv)
5782{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005783 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005784 term_T *term;
5785 int row;
5786
5787 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005788
5789 if (in_vim9script()
5790 && (check_for_buffer_arg(argvars, 0) == FAIL
5791 || check_for_lnum_arg(argvars, 1) == FAIL))
5792 return;
5793
5794 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005795 if (buf == NULL)
5796 return;
5797 term = buf->b_term;
5798 row = get_row_number(&argvars[1], term);
5799
5800 if (term->tl_vterm == NULL)
5801 {
5802 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5803
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005804 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005805 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5806 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5807 }
5808 else
5809 {
5810 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5811 VTermRect rect;
5812 int len;
5813 char_u *p;
5814
5815 if (row < 0 || row >= term->tl_rows)
5816 return;
5817 len = term->tl_cols * MB_MAXBYTES + 1;
5818 p = alloc(len);
5819 if (p == NULL)
5820 return;
5821 rettv->vval.v_string = p;
5822
5823 rect.start_col = 0;
5824 rect.end_col = term->tl_cols;
5825 rect.start_row = row;
5826 rect.end_row = row + 1;
5827 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5828 }
5829}
5830
5831/*
5832 * "term_getscrolled(buf)" function
5833 */
5834 void
5835f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5836{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005837 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005838
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005839 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5840 return;
5841
5842 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005843 if (buf == NULL)
5844 return;
5845 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5846}
5847
5848/*
5849 * "term_getsize(buf)" function
5850 */
5851 void
5852f_term_getsize(typval_T *argvars, typval_T *rettv)
5853{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005854 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005855 list_T *l;
5856
5857 if (rettv_list_alloc(rettv) == FAIL)
5858 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005859
5860 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5861 return;
5862
5863 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005864 if (buf == NULL)
5865 return;
5866
5867 l = rettv->vval.v_list;
5868 list_append_number(l, buf->b_term->tl_rows);
5869 list_append_number(l, buf->b_term->tl_cols);
5870}
5871
5872/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005873 * "term_setsize(buf, rows, cols)" function
5874 */
5875 void
5876f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5877{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005878 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02005879 term_T *term;
5880 varnumber_T rows, cols;
5881
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005882 if (in_vim9script()
5883 && (check_for_buffer_arg(argvars, 0) == FAIL
5884 || check_for_number_arg(argvars, 1) == FAIL
5885 || check_for_number_arg(argvars, 2) == FAIL))
5886 return;
5887
5888 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005889 if (buf == NULL)
5890 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005891 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005892 return;
5893 }
5894 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005895 return;
5896 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005897 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005898 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005899 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005900 cols = cols <= 0 ? term->tl_cols : cols;
5901 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005902 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005903
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005904 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005905 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5906 term_report_winsize(term, term->tl_rows, term->tl_cols);
5907}
5908
5909/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005910 * "term_getstatus(buf)" function
5911 */
5912 void
5913f_term_getstatus(typval_T *argvars, typval_T *rettv)
5914{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005915 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005916 term_T *term;
5917 char_u val[100];
5918
5919 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005920
5921 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5922 return;
5923
5924 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005925 if (buf == NULL)
5926 return;
5927 term = buf->b_term;
5928
5929 if (term_job_running(term))
5930 STRCPY(val, "running");
5931 else
5932 STRCPY(val, "finished");
5933 if (term->tl_normal_mode)
5934 STRCAT(val, ",normal");
5935 rettv->vval.v_string = vim_strsave(val);
5936}
5937
5938/*
5939 * "term_gettitle(buf)" function
5940 */
5941 void
5942f_term_gettitle(typval_T *argvars, typval_T *rettv)
5943{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005944 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005945
5946 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005947
5948 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5949 return;
5950
5951 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005952 if (buf == NULL)
5953 return;
5954
5955 if (buf->b_term->tl_title != NULL)
5956 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5957}
5958
5959/*
5960 * "term_gettty(buf)" function
5961 */
5962 void
5963f_term_gettty(typval_T *argvars, typval_T *rettv)
5964{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005965 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005966 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005967 int num = 0;
5968
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005969 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005970 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005971 || check_for_opt_bool_arg(argvars, 1) == FAIL))
5972 return;
5973
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005974 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005975 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005976 if (buf == NULL)
5977 return;
5978 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02005979 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005980
5981 switch (num)
5982 {
5983 case 0:
5984 if (buf->b_term->tl_job != NULL)
5985 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005986 break;
5987 case 1:
5988 if (buf->b_term->tl_job != NULL)
5989 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005990 break;
5991 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005992 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005993 return;
5994 }
5995 if (p != NULL)
5996 rettv->vval.v_string = vim_strsave(p);
5997}
5998
5999/*
6000 * "term_list()" function
6001 */
6002 void
6003f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
6004{
6005 term_T *tp;
6006 list_T *l;
6007
6008 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
6009 return;
6010
6011 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006012 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02006013 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006014 if (list_append_number(l,
6015 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6016 return;
6017}
6018
6019/*
6020 * "term_scrape(buf, row)" function
6021 */
6022 void
6023f_term_scrape(typval_T *argvars, typval_T *rettv)
6024{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006025 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006026 VTermScreen *screen = NULL;
6027 VTermPos pos;
6028 list_T *l;
6029 term_T *term;
6030 char_u *p;
6031 sb_line_T *line;
6032
6033 if (rettv_list_alloc(rettv) == FAIL)
6034 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006035
6036 if (in_vim9script()
6037 && (check_for_buffer_arg(argvars, 0) == FAIL
6038 || check_for_lnum_arg(argvars, 1) == FAIL))
6039 return;
6040
6041 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006042 if (buf == NULL)
6043 return;
6044 term = buf->b_term;
6045
6046 l = rettv->vval.v_list;
6047 pos.row = get_row_number(&argvars[1], term);
6048
6049 if (term->tl_vterm != NULL)
6050 {
6051 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006052 if (screen == NULL) // can't really happen
6053 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006054 p = NULL;
6055 line = NULL;
6056 }
6057 else
6058 {
6059 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6060
6061 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6062 return;
6063 p = ml_get_buf(buf, lnum + 1, FALSE);
6064 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6065 }
6066
6067 for (pos.col = 0; pos.col < term->tl_cols; )
6068 {
6069 dict_T *dcell;
6070 int width;
6071 VTermScreenCellAttrs attrs;
6072 VTermColor fg, bg;
6073 char_u rgb[8];
6074 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6075 int off = 0;
6076 int i;
6077
6078 if (screen == NULL)
6079 {
6080 cellattr_T *cellattr;
6081 int len;
6082
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006083 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006084 if (pos.col >= line->sb_cols)
6085 break;
6086 cellattr = line->sb_cells + pos.col;
6087 width = cellattr->width;
6088 attrs = cellattr->attrs;
6089 fg = cellattr->fg;
6090 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006091 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006092 mch_memmove(mbs, p, len);
6093 mbs[len] = NUL;
6094 p += len;
6095 }
6096 else
6097 {
6098 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006099
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006100 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6101 break;
6102 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6103 {
6104 if (cell.chars[i] == 0)
6105 break;
6106 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6107 }
6108 mbs[off] = NUL;
6109 width = cell.width;
6110 attrs = cell.attrs;
6111 fg = cell.fg;
6112 bg = cell.bg;
6113 }
6114 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006115 if (dcell == NULL)
6116 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006117 list_append_dict(l, dcell);
6118
Bram Moolenaare0be1672018-07-08 16:50:37 +02006119 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006120
6121 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6122 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006123 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006124 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6125 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006126 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006127
Bram Moolenaar83d47902020-03-26 20:34:00 +01006128 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006129 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006130
6131 ++pos.col;
6132 if (width == 2)
6133 ++pos.col;
6134 }
6135}
6136
6137/*
6138 * "term_sendkeys(buf, keys)" function
6139 */
6140 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006141f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006142{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006143 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006144 char_u *msg;
6145 term_T *term;
6146
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006147 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006148 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006149 || check_for_string_arg(argvars, 1) == FAIL))
6150 return;
6151
6152 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006153 if (buf == NULL)
6154 return;
6155
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006156 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006157 if (msg == NULL)
6158 return;
6159 term = buf->b_term;
6160 if (term->tl_vterm == NULL)
6161 return;
6162
6163 while (*msg != NUL)
6164 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006165 int c;
6166
6167 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6168 {
6169 c = TO_SPECIAL(msg[1], msg[2]);
6170 msg += 3;
6171 }
6172 else
6173 {
6174 c = PTR2CHAR(msg);
6175 msg += MB_CPTR2LEN(msg);
6176 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006177 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006178 }
6179}
6180
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006181#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6182/*
6183 * "term_getansicolors(buf)" function
6184 */
6185 void
6186f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6187{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006188 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006189 term_T *term;
6190 VTermState *state;
6191 VTermColor color;
6192 char_u hexbuf[10];
6193 int index;
6194 list_T *list;
6195
6196 if (rettv_list_alloc(rettv) == FAIL)
6197 return;
6198
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006199 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6200 return;
6201
6202 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006203 if (buf == NULL)
6204 return;
6205 term = buf->b_term;
6206 if (term->tl_vterm == NULL)
6207 return;
6208
6209 list = rettv->vval.v_list;
6210 state = vterm_obtain_state(term->tl_vterm);
6211 for (index = 0; index < 16; index++)
6212 {
6213 vterm_state_get_palette_color(state, index, &color);
6214 sprintf((char *)hexbuf, "#%02x%02x%02x",
6215 color.red, color.green, color.blue);
6216 if (list_append_string(list, hexbuf, 7) == FAIL)
6217 return;
6218 }
6219}
6220
6221/*
6222 * "term_setansicolors(buf, list)" function
6223 */
6224 void
6225f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6226{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006227 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006228 term_T *term;
6229
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006230 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006231 && (check_for_buffer_arg(argvars, 0) == FAIL
6232 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006233 return;
6234
6235 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006236 if (buf == NULL)
6237 return;
6238 term = buf->b_term;
6239 if (term->tl_vterm == NULL)
6240 return;
6241
6242 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6243 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006244 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006245 return;
6246 }
6247
6248 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006249 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006250}
6251#endif
6252
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006253/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006254 * "term_setapi(buf, api)" function
6255 */
6256 void
6257f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6258{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006259 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006260 term_T *term;
6261 char_u *api;
6262
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006263 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006264 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006265 || check_for_string_arg(argvars, 1) == FAIL))
6266 return;
6267
6268 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006269 if (buf == NULL)
6270 return;
6271 term = buf->b_term;
6272 vim_free(term->tl_api);
6273 api = tv_get_string_chk(&argvars[1]);
6274 if (api != NULL)
6275 term->tl_api = vim_strsave(api);
6276 else
6277 term->tl_api = NULL;
6278}
6279
6280/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006281 * "term_setrestore(buf, command)" function
6282 */
6283 void
6284f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6285{
6286#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006287 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006288 term_T *term;
6289 char_u *cmd;
6290
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006291 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006292 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006293 || check_for_string_arg(argvars, 1) == FAIL))
6294 return;
6295
6296 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006297 if (buf == NULL)
6298 return;
6299 term = buf->b_term;
6300 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006301 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006302 if (cmd != NULL)
6303 term->tl_command = vim_strsave(cmd);
6304 else
6305 term->tl_command = NULL;
6306#endif
6307}
6308
6309/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006310 * "term_setkill(buf, how)" function
6311 */
6312 void
6313f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6314{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006315 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006316 term_T *term;
6317 char_u *how;
6318
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006319 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006320 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006321 || check_for_string_arg(argvars, 1) == FAIL))
6322 return;
6323
6324 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006325 if (buf == NULL)
6326 return;
6327 term = buf->b_term;
6328 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006329 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006330 if (how != NULL)
6331 term->tl_kill = vim_strsave(how);
6332 else
6333 term->tl_kill = NULL;
6334}
6335
6336/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006337 * "term_start(command, options)" function
6338 */
6339 void
6340f_term_start(typval_T *argvars, typval_T *rettv)
6341{
6342 jobopt_T opt;
6343 buf_T *buf;
6344
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006345 if (in_vim9script()
6346 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6347 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6348 return;
6349
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006350 init_job_options(&opt);
6351 if (argvars[1].v_type != VAR_UNKNOWN
6352 && get_job_options(&argvars[1], &opt,
6353 JO_TIMEOUT_ALL + JO_STOPONEXIT
6354 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6355 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6356 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6357 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006358 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006359 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006360 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006361 return;
6362
Bram Moolenaar13568252018-03-16 20:46:58 +01006363 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006364
6365 if (buf != NULL && buf->b_term != NULL)
6366 rettv->vval.v_number = buf->b_fnum;
6367}
6368
6369/*
6370 * "term_wait" function
6371 */
6372 void
6373f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6374{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006375 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006376
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006377 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006378 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006379 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006380 return;
6381
6382 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006383 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006384 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006385 if (buf->b_term->tl_job == NULL)
6386 {
6387 ch_log(NULL, "term_wait(): no job to wait for");
6388 return;
6389 }
6390 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006391 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006392 return;
6393
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006394 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006395 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006396 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6397 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006398 // The job is dead, keep reading channel I/O until the channel is
6399 // closed. buf->b_term may become NULL if the terminal was closed while
6400 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006401 ch_log(NULL, "term_wait(): waiting for channel to close");
6402 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6403 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006404 term_flush_messages();
6405
Bram Moolenaard45aa552018-05-21 22:50:29 +02006406 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006407 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006408 // If the terminal is closed when the channel is closed the
6409 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006410 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006411 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006412
6413 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006414 }
6415 else
6416 {
6417 long wait = 10L;
6418
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006419 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006420
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006421 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006422 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006423 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006424 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006425
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006426 // Flushing messages on channels is hopefully sufficient.
6427 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006428 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006429 }
6430}
6431
6432/*
6433 * Called when a channel has sent all the lines to a terminal.
6434 * Send a CTRL-D to mark the end of the text.
6435 */
6436 void
6437term_send_eof(channel_T *ch)
6438{
6439 term_T *term;
6440
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006441 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006442 if (term->tl_job == ch->ch_job)
6443 {
6444 if (term->tl_eof_chars != NULL)
6445 {
6446 channel_send(ch, PART_IN, term->tl_eof_chars,
6447 (int)STRLEN(term->tl_eof_chars), NULL);
6448 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6449 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006450# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006451 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006452 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006453 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6454# endif
6455 }
6456}
6457
Bram Moolenaar113e1072019-01-20 15:30:40 +01006458#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006459 job_T *
6460term_getjob(term_T *term)
6461{
6462 return term != NULL ? term->tl_job : NULL;
6463}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006464#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006465
Bram Moolenaar4f974752019-02-17 17:44:42 +01006466# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006467
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006468///////////////////////////////////////
6469// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006470#ifdef PROTO
6471typedef int COORD;
6472typedef int DWORD;
6473typedef int HANDLE;
6474typedef int *DWORD_PTR;
6475typedef int HPCON;
6476typedef int HRESULT;
6477typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006478typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006479typedef int PSIZE_T;
6480typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006481typedef int BOOL;
6482# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006483#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006484
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006485HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6486HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6487HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006488BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6489BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6490void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006491
6492 static int
6493dyn_conpty_init(int verbose)
6494{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006495 static HMODULE hKerneldll = NULL;
6496 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006497 static struct
6498 {
6499 char *name;
6500 FARPROC *ptr;
6501 } conpty_entry[] =
6502 {
6503 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6504 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6505 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6506 {"InitializeProcThreadAttributeList",
6507 (FARPROC*)&pInitializeProcThreadAttributeList},
6508 {"UpdateProcThreadAttribute",
6509 (FARPROC*)&pUpdateProcThreadAttribute},
6510 {"DeleteProcThreadAttributeList",
6511 (FARPROC*)&pDeleteProcThreadAttributeList},
6512 {NULL, NULL}
6513 };
6514
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006515 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006516 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006517 if (verbose)
6518 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006519 return FAIL;
6520 }
6521
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006522 // No need to initialize twice.
6523 if (hKerneldll)
6524 return OK;
6525
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006526 hKerneldll = vimLoadLib("kernel32.dll");
6527 for (i = 0; conpty_entry[i].name != NULL
6528 && conpty_entry[i].ptr != NULL; ++i)
6529 {
6530 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6531 conpty_entry[i].name)) == NULL)
6532 {
6533 if (verbose)
6534 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006535 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006536 return FAIL;
6537 }
6538 }
6539
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006540 return OK;
6541}
6542
6543 static int
6544conpty_term_and_job_init(
6545 term_T *term,
6546 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006547 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006548 jobopt_T *opt,
6549 jobopt_T *orig_opt)
6550{
6551 WCHAR *cmd_wchar = NULL;
6552 WCHAR *cmd_wchar_copy = NULL;
6553 WCHAR *cwd_wchar = NULL;
6554 WCHAR *env_wchar = NULL;
6555 channel_T *channel = NULL;
6556 job_T *job = NULL;
6557 HANDLE jo = NULL;
6558 garray_T ga_cmd, ga_env;
6559 char_u *cmd = NULL;
6560 HRESULT hr;
6561 COORD consize;
6562 SIZE_T breq;
6563 PROCESS_INFORMATION proc_info;
6564 HANDLE i_theirs = NULL;
6565 HANDLE o_theirs = NULL;
6566 HANDLE i_ours = NULL;
6567 HANDLE o_ours = NULL;
6568
6569 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6570 ga_init2(&ga_env, (int)sizeof(char*), 20);
6571
6572 if (argvar->v_type == VAR_STRING)
6573 {
6574 cmd = argvar->vval.v_string;
6575 }
6576 else if (argvar->v_type == VAR_LIST)
6577 {
6578 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6579 goto failed;
6580 cmd = ga_cmd.ga_data;
6581 }
6582 if (cmd == NULL || *cmd == NUL)
6583 {
6584 emsg(_(e_invarg));
6585 goto failed;
6586 }
6587
6588 term->tl_arg0_cmd = vim_strsave(cmd);
6589
6590 cmd_wchar = enc_to_utf16(cmd, NULL);
6591
6592 if (cmd_wchar != NULL)
6593 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006594 // Request by CreateProcessW
6595 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006596 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006597 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6598 }
6599
6600 ga_clear(&ga_cmd);
6601 if (cmd_wchar == NULL)
6602 goto failed;
6603 if (opt->jo_cwd != NULL)
6604 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6605
6606 win32_build_env(opt->jo_env, &ga_env, TRUE);
6607 env_wchar = ga_env.ga_data;
6608
6609 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6610 goto failed;
6611 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6612 goto failed;
6613
6614 consize.X = term->tl_cols;
6615 consize.Y = term->tl_rows;
6616 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6617 &term->tl_conpty);
6618 if (FAILED(hr))
6619 goto failed;
6620
6621 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6622
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006623 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006624 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006625 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006626 if (!term->tl_siex.lpAttributeList)
6627 goto failed;
6628 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6629 0, &breq))
6630 goto failed;
6631 if (!pUpdateProcThreadAttribute(
6632 term->tl_siex.lpAttributeList, 0,
6633 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6634 sizeof(HPCON), NULL, NULL))
6635 goto failed;
6636
6637 channel = add_channel();
6638 if (channel == NULL)
6639 goto failed;
6640
6641 job = job_alloc();
6642 if (job == NULL)
6643 goto failed;
6644 if (argvar->v_type == VAR_STRING)
6645 {
6646 int argc;
6647
6648 build_argv_from_string(cmd, &job->jv_argv, &argc);
6649 }
6650 else
6651 {
6652 int argc;
6653
6654 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6655 }
6656
6657 if (opt->jo_set & JO_IN_BUF)
6658 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6659
6660 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6661 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006662 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006663 env_wchar, cwd_wchar,
6664 &term->tl_siex.StartupInfo, &proc_info))
6665 goto failed;
6666
6667 CloseHandle(i_theirs);
6668 CloseHandle(o_theirs);
6669
6670 channel_set_pipes(channel,
6671 (sock_T)i_ours,
6672 (sock_T)o_ours,
6673 (sock_T)o_ours);
6674
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006675 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006676 channel->ch_write_text_mode = TRUE;
6677
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006678 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006679 channel->ch_anonymous_pipe = TRUE;
6680
6681 jo = CreateJobObject(NULL, NULL);
6682 if (jo == NULL)
6683 goto failed;
6684
6685 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6686 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006687 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006688 CloseHandle(jo);
6689 jo = NULL;
6690 }
6691
6692 ResumeThread(proc_info.hThread);
6693 CloseHandle(proc_info.hThread);
6694
6695 vim_free(cmd_wchar);
6696 vim_free(cmd_wchar_copy);
6697 vim_free(cwd_wchar);
6698 vim_free(env_wchar);
6699
6700 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6701 goto failed;
6702
6703#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6704 if (opt->jo_set2 & JO2_ANSI_COLORS)
6705 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6706 else
6707 init_vterm_ansi_colors(term->tl_vterm);
6708#endif
6709
6710 channel_set_job(channel, job, opt);
6711 job_set_options(job, opt);
6712
6713 job->jv_channel = channel;
6714 job->jv_proc_info = proc_info;
6715 job->jv_job_object = jo;
6716 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006717 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006718 ++job->jv_refcount;
6719 term->tl_job = job;
6720
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006721 // Redirecting stdout and stderr doesn't work at the job level. Instead
6722 // open the file here and handle it in. opt->jo_io was changed in
6723 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006724 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6725 {
6726 char_u *fname = opt->jo_io_name[PART_OUT];
6727
6728 ch_log(channel, "Opening output file %s", fname);
6729 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6730 if (term->tl_out_fd == NULL)
6731 semsg(_(e_notopen), fname);
6732 }
6733
6734 return OK;
6735
6736failed:
6737 ga_clear(&ga_cmd);
6738 ga_clear(&ga_env);
6739 vim_free(cmd_wchar);
6740 vim_free(cmd_wchar_copy);
6741 vim_free(cwd_wchar);
6742 if (channel != NULL)
6743 channel_clear(channel);
6744 if (job != NULL)
6745 {
6746 job->jv_channel = NULL;
6747 job_cleanup(job);
6748 }
6749 term->tl_job = NULL;
6750 if (jo != NULL)
6751 CloseHandle(jo);
6752
6753 if (term->tl_siex.lpAttributeList != NULL)
6754 {
6755 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6756 vim_free(term->tl_siex.lpAttributeList);
6757 }
6758 term->tl_siex.lpAttributeList = NULL;
6759 if (o_theirs != NULL)
6760 CloseHandle(o_theirs);
6761 if (o_ours != NULL)
6762 CloseHandle(o_ours);
6763 if (i_ours != NULL)
6764 CloseHandle(i_ours);
6765 if (i_theirs != NULL)
6766 CloseHandle(i_theirs);
6767 if (term->tl_conpty != NULL)
6768 pClosePseudoConsole(term->tl_conpty);
6769 term->tl_conpty = NULL;
6770 return FAIL;
6771}
6772
6773 static void
6774conpty_term_report_winsize(term_T *term, int rows, int cols)
6775{
6776 COORD consize;
6777
6778 consize.X = cols;
6779 consize.Y = rows;
6780 pResizePseudoConsole(term->tl_conpty, consize);
6781}
6782
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006783 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006784term_free_conpty(term_T *term)
6785{
6786 if (term->tl_siex.lpAttributeList != NULL)
6787 {
6788 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6789 vim_free(term->tl_siex.lpAttributeList);
6790 }
6791 term->tl_siex.lpAttributeList = NULL;
6792 if (term->tl_conpty != NULL)
6793 pClosePseudoConsole(term->tl_conpty);
6794 term->tl_conpty = NULL;
6795}
6796
6797 int
6798use_conpty(void)
6799{
6800 return has_conpty;
6801}
6802
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006803# ifndef PROTO
6804
6805#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6806#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006807#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006808
6809void* (*winpty_config_new)(UINT64, void*);
6810void* (*winpty_open)(void*, void*);
6811void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6812BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6813void (*winpty_config_set_mouse_mode)(void*, int);
6814void (*winpty_config_set_initial_size)(void*, int, int);
6815LPCWSTR (*winpty_conin_name)(void*);
6816LPCWSTR (*winpty_conout_name)(void*);
6817LPCWSTR (*winpty_conerr_name)(void*);
6818void (*winpty_free)(void*);
6819void (*winpty_config_free)(void*);
6820void (*winpty_spawn_config_free)(void*);
6821void (*winpty_error_free)(void*);
6822LPCWSTR (*winpty_error_msg)(void*);
6823BOOL (*winpty_set_size)(void*, int, int, void*);
6824HANDLE (*winpty_agent_process)(void*);
6825
6826#define WINPTY_DLL "winpty.dll"
6827
6828static HINSTANCE hWinPtyDLL = NULL;
6829# endif
6830
6831 static int
6832dyn_winpty_init(int verbose)
6833{
6834 int i;
6835 static struct
6836 {
6837 char *name;
6838 FARPROC *ptr;
6839 } winpty_entry[] =
6840 {
6841 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6842 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6843 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6844 {"winpty_config_set_mouse_mode",
6845 (FARPROC*)&winpty_config_set_mouse_mode},
6846 {"winpty_config_set_initial_size",
6847 (FARPROC*)&winpty_config_set_initial_size},
6848 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6849 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6850 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6851 {"winpty_free", (FARPROC*)&winpty_free},
6852 {"winpty_open", (FARPROC*)&winpty_open},
6853 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6854 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6855 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6856 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6857 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6858 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6859 {NULL, NULL}
6860 };
6861
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006862 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006863 if (hWinPtyDLL)
6864 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006865 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6866 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006867 if (*p_winptydll != NUL)
6868 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6869 if (!hWinPtyDLL)
6870 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6871 if (!hWinPtyDLL)
6872 {
6873 if (verbose)
Martin Tournoij1a3e5742021-07-24 13:57:29 +02006874 semsg(_(e_loadlib),
6875 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
6876 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006877 return FAIL;
6878 }
6879 for (i = 0; winpty_entry[i].name != NULL
6880 && winpty_entry[i].ptr != NULL; ++i)
6881 {
6882 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6883 winpty_entry[i].name)) == NULL)
6884 {
6885 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006886 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006887 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006888 return FAIL;
6889 }
6890 }
6891
6892 return OK;
6893}
6894
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006895 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006896winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006897 term_T *term,
6898 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006899 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006900 jobopt_T *opt,
6901 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006902{
6903 WCHAR *cmd_wchar = NULL;
6904 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006905 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006906 channel_T *channel = NULL;
6907 job_T *job = NULL;
6908 DWORD error;
6909 HANDLE jo = NULL;
6910 HANDLE child_process_handle;
6911 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006912 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006913 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006914 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006915 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006916
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006917 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6918 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006919
6920 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006921 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006922 cmd = argvar->vval.v_string;
6923 }
6924 else if (argvar->v_type == VAR_LIST)
6925 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006926 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006927 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006928 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006929 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006930 if (cmd == NULL || *cmd == NUL)
6931 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006932 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006933 goto failed;
6934 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006935
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006936 term->tl_arg0_cmd = vim_strsave(cmd);
6937
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006938 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006939 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006940 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006941 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006942 if (opt->jo_cwd != NULL)
6943 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006944
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006945 win32_build_env(opt->jo_env, &ga_env, TRUE);
6946 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006947
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006948 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6949 if (term->tl_winpty_config == NULL)
6950 goto failed;
6951
6952 winpty_config_set_mouse_mode(term->tl_winpty_config,
6953 WINPTY_MOUSE_MODE_FORCE);
6954 winpty_config_set_initial_size(term->tl_winpty_config,
6955 term->tl_cols, term->tl_rows);
6956 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6957 if (term->tl_winpty == NULL)
6958 goto failed;
6959
6960 spawn_config = winpty_spawn_config_new(
6961 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6962 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6963 NULL,
6964 cmd_wchar,
6965 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006966 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006967 &winpty_err);
6968 if (spawn_config == NULL)
6969 goto failed;
6970
6971 channel = add_channel();
6972 if (channel == NULL)
6973 goto failed;
6974
6975 job = job_alloc();
6976 if (job == NULL)
6977 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006978 if (argvar->v_type == VAR_STRING)
6979 {
6980 int argc;
6981
6982 build_argv_from_string(cmd, &job->jv_argv, &argc);
6983 }
6984 else
6985 {
6986 int argc;
6987
6988 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6989 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006990
6991 if (opt->jo_set & JO_IN_BUF)
6992 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6993
6994 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
6995 &child_thread_handle, &error, &winpty_err))
6996 goto failed;
6997
6998 channel_set_pipes(channel,
6999 (sock_T)CreateFileW(
7000 winpty_conin_name(term->tl_winpty),
7001 GENERIC_WRITE, 0, NULL,
7002 OPEN_EXISTING, 0, NULL),
7003 (sock_T)CreateFileW(
7004 winpty_conout_name(term->tl_winpty),
7005 GENERIC_READ, 0, NULL,
7006 OPEN_EXISTING, 0, NULL),
7007 (sock_T)CreateFileW(
7008 winpty_conerr_name(term->tl_winpty),
7009 GENERIC_READ, 0, NULL,
7010 OPEN_EXISTING, 0, NULL));
7011
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007012 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007013 channel->ch_write_text_mode = TRUE;
7014
7015 jo = CreateJobObject(NULL, NULL);
7016 if (jo == NULL)
7017 goto failed;
7018
7019 if (!AssignProcessToJobObject(jo, child_process_handle))
7020 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007021 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007022 CloseHandle(jo);
7023 jo = NULL;
7024 }
7025
7026 winpty_spawn_config_free(spawn_config);
7027 vim_free(cmd_wchar);
7028 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007029 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007030
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007031 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7032 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007033
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007034#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7035 if (opt->jo_set2 & JO2_ANSI_COLORS)
7036 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7037 else
7038 init_vterm_ansi_colors(term->tl_vterm);
7039#endif
7040
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007041 channel_set_job(channel, job, opt);
7042 job_set_options(job, opt);
7043
7044 job->jv_channel = channel;
7045 job->jv_proc_info.hProcess = child_process_handle;
7046 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7047 job->jv_job_object = jo;
7048 job->jv_status = JOB_STARTED;
7049 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007050 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007051 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007052 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007053 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007054 ++job->jv_refcount;
7055 term->tl_job = job;
7056
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007057 // Redirecting stdout and stderr doesn't work at the job level. Instead
7058 // open the file here and handle it in. opt->jo_io was changed in
7059 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007060 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7061 {
7062 char_u *fname = opt->jo_io_name[PART_OUT];
7063
7064 ch_log(channel, "Opening output file %s", fname);
7065 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7066 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007067 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007068 }
7069
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007070 return OK;
7071
7072failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007073 ga_clear(&ga_cmd);
7074 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007075 vim_free(cmd_wchar);
7076 vim_free(cwd_wchar);
7077 if (spawn_config != NULL)
7078 winpty_spawn_config_free(spawn_config);
7079 if (channel != NULL)
7080 channel_clear(channel);
7081 if (job != NULL)
7082 {
7083 job->jv_channel = NULL;
7084 job_cleanup(job);
7085 }
7086 term->tl_job = NULL;
7087 if (jo != NULL)
7088 CloseHandle(jo);
7089 if (term->tl_winpty != NULL)
7090 winpty_free(term->tl_winpty);
7091 term->tl_winpty = NULL;
7092 if (term->tl_winpty_config != NULL)
7093 winpty_config_free(term->tl_winpty_config);
7094 term->tl_winpty_config = NULL;
7095 if (winpty_err != NULL)
7096 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007097 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007098 (short_u *)winpty_error_msg(winpty_err), NULL);
7099
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007100 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007101 winpty_error_free(winpty_err);
7102 }
7103 return FAIL;
7104}
7105
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007106/*
7107 * Create a new terminal of "rows" by "cols" cells.
7108 * Store a reference in "term".
7109 * Return OK or FAIL.
7110 */
7111 static int
7112term_and_job_init(
7113 term_T *term,
7114 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007115 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007116 jobopt_T *opt,
7117 jobopt_T *orig_opt)
7118{
7119 int use_winpty = FALSE;
7120 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007121 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007122
7123 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7124 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7125
7126 if (!has_winpty && !has_conpty)
7127 // If neither is available give the errors for winpty, since when
7128 // conpty is not available it can't be installed either.
7129 return dyn_winpty_init(TRUE);
7130
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007131 if (opt->jo_tty_type != NUL)
7132 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007133
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007134 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007135 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007136 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007137 use_conpty = TRUE;
7138 else if (has_winpty)
7139 use_winpty = TRUE;
7140 // else: error
7141 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007142 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007143 {
7144 if (has_winpty)
7145 use_winpty = TRUE;
7146 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007147 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007148 {
7149 if (has_conpty)
7150 use_conpty = TRUE;
7151 else
7152 return dyn_conpty_init(TRUE);
7153 }
7154
7155 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007156 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007157
7158 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007159 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007160
7161 // error
7162 return dyn_winpty_init(TRUE);
7163}
7164
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007165 static int
7166create_pty_only(term_T *term, jobopt_T *options)
7167{
7168 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7169 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7170 char in_name[80], out_name[80];
7171 channel_T *channel = NULL;
7172
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007173 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7174 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007175
7176 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7177 GetCurrentProcessId(),
7178 curbuf->b_fnum);
7179 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7180 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7181 PIPE_UNLIMITED_INSTANCES,
7182 0, 0, NMPWAIT_NOWAIT, NULL);
7183 if (hPipeIn == INVALID_HANDLE_VALUE)
7184 goto failed;
7185
7186 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7187 GetCurrentProcessId(),
7188 curbuf->b_fnum);
7189 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7190 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7191 PIPE_UNLIMITED_INSTANCES,
7192 0, 0, 0, NULL);
7193 if (hPipeOut == INVALID_HANDLE_VALUE)
7194 goto failed;
7195
7196 ConnectNamedPipe(hPipeIn, NULL);
7197 ConnectNamedPipe(hPipeOut, NULL);
7198
7199 term->tl_job = job_alloc();
7200 if (term->tl_job == NULL)
7201 goto failed;
7202 ++term->tl_job->jv_refcount;
7203
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007204 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007205 term->tl_job->jv_status = JOB_FINISHED;
7206
7207 channel = add_channel();
7208 if (channel == NULL)
7209 goto failed;
7210 term->tl_job->jv_channel = channel;
7211 channel->ch_keep_open = TRUE;
7212 channel->ch_named_pipe = TRUE;
7213
7214 channel_set_pipes(channel,
7215 (sock_T)hPipeIn,
7216 (sock_T)hPipeOut,
7217 (sock_T)hPipeOut);
7218 channel_set_job(channel, term->tl_job, options);
7219 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7220 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7221
7222 return OK;
7223
7224failed:
7225 if (hPipeIn != NULL)
7226 CloseHandle(hPipeIn);
7227 if (hPipeOut != NULL)
7228 CloseHandle(hPipeOut);
7229 return FAIL;
7230}
7231
7232/*
7233 * Free the terminal emulator part of "term".
7234 */
7235 static void
7236term_free_vterm(term_T *term)
7237{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007238 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007239 if (term->tl_winpty != NULL)
7240 winpty_free(term->tl_winpty);
7241 term->tl_winpty = NULL;
7242 if (term->tl_winpty_config != NULL)
7243 winpty_config_free(term->tl_winpty_config);
7244 term->tl_winpty_config = NULL;
7245 if (term->tl_vterm != NULL)
7246 vterm_free(term->tl_vterm);
7247 term->tl_vterm = NULL;
7248}
7249
7250/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007251 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007252 */
7253 static void
7254term_report_winsize(term_T *term, int rows, int cols)
7255{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007256 if (term->tl_conpty)
7257 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007258 if (term->tl_winpty)
7259 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7260}
7261
7262 int
7263terminal_enabled(void)
7264{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007265 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007266}
7267
7268# else
7269
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007270///////////////////////////////////////
7271// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007272
7273/*
7274 * Create a new terminal of "rows" by "cols" cells.
7275 * Start job for "cmd".
7276 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007277 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007278 * Return OK or FAIL.
7279 */
7280 static int
7281term_and_job_init(
7282 term_T *term,
7283 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007284 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007285 jobopt_T *opt,
7286 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007287{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007288 term->tl_arg0_cmd = NULL;
7289
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007290 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7291 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007292
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007293#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7294 if (opt->jo_set2 & JO2_ANSI_COLORS)
7295 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7296 else
7297 init_vterm_ansi_colors(term->tl_vterm);
7298#endif
7299
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007300 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007301 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007302 if (term->tl_job != NULL)
7303 ++term->tl_job->jv_refcount;
7304
7305 return term->tl_job != NULL
7306 && term->tl_job->jv_channel != NULL
7307 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7308}
7309
7310 static int
7311create_pty_only(term_T *term, jobopt_T *opt)
7312{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007313 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7314 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007315
7316 term->tl_job = job_alloc();
7317 if (term->tl_job == NULL)
7318 return FAIL;
7319 ++term->tl_job->jv_refcount;
7320
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007321 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007322 term->tl_job->jv_status = JOB_FINISHED;
7323
7324 return mch_create_pty_channel(term->tl_job, opt);
7325}
7326
7327/*
7328 * Free the terminal emulator part of "term".
7329 */
7330 static void
7331term_free_vterm(term_T *term)
7332{
7333 if (term->tl_vterm != NULL)
7334 vterm_free(term->tl_vterm);
7335 term->tl_vterm = NULL;
7336}
7337
7338/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007339 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007340 */
7341 static void
7342term_report_winsize(term_T *term, int rows, int cols)
7343{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007344 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007345 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7346 {
7347 int fd = -1;
7348 int part;
7349
7350 for (part = PART_OUT; part < PART_COUNT; ++part)
7351 {
7352 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007353 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007354 break;
7355 }
7356 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7357 mch_signal_job(term->tl_job, (char_u *)"winch");
7358 }
7359}
7360
7361# endif
7362
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007363#endif // FEAT_TERMINAL