blob: a33a3a345b134b97a2572be25f7b68477fad7628 [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 Moolenaareea32af2021-11-21 14:51:13 +0000102 int tl_channel_closing;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200103 int tl_channel_closed;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200104 int tl_channel_recently_closed; // still need to handle tl_finish
105
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100106 int tl_finish;
107#define TL_FINISH_UNSET NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100108#define TL_FINISH_CLOSE 'c' // ++close or :terminal without argument
109#define TL_FINISH_NOCLOSE 'n' // ++noclose
110#define TL_FINISH_OPEN 'o' // ++open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200111 char_u *tl_opencmd;
112 char_u *tl_eof_chars;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200113 char_u *tl_api; // prefix for terminal API function
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200114
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100115 char_u *tl_arg0_cmd; // To format the status bar
116
Bram Moolenaar4f974752019-02-17 17:44:42 +0100117#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200118 void *tl_winpty_config;
119 void *tl_winpty;
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200120
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100121 HPCON tl_conpty;
122 DYN_STARTUPINFOEXW tl_siex; // Structure that always needs to be hold
123
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200124 FILE *tl_out_fd;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200125#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100126#if defined(FEAT_SESSION)
127 char_u *tl_command;
128#endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100129 char_u *tl_kill;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200130
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100131 // last known vterm size
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200132 int tl_rows;
133 int tl_cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200134
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100135 char_u *tl_title; // NULL or allocated
136 char_u *tl_status_text; // NULL or allocated
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200137
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100138 // Range of screen rows to update. Zero based.
139 int tl_dirty_row_start; // MAX_ROW if nothing dirty
140 int tl_dirty_row_end; // row below last one to update
141 int tl_dirty_snapshot; // text updated after making snapshot
Bram Moolenaar56bc8e22018-05-10 18:05:56 +0200142#ifdef FEAT_TIMERS
143 int tl_timer_set;
144 proftime_T tl_timer_due;
145#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100146 int tl_postponed_scroll; // to be scrolled up
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200147
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200148 garray_T tl_scrollback;
149 int tl_scrollback_scrolled;
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100150 garray_T tl_scrollback_postponed;
151
Bram Moolenaar83d47902020-03-26 20:34:00 +0100152 char_u *tl_highlight_name; // replaces "Terminal"; allocated
153
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200154 cellattr_T tl_default_color;
155
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100156 linenr_T tl_top_diff_rows; // rows of top diff file or zero
157 linenr_T tl_bot_diff_rows; // rows of bottom diff file
Bram Moolenaard96ff162018-02-18 22:13:29 +0100158
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200159 VTermPos tl_cursor_pos;
160 int tl_cursor_visible;
161 int tl_cursor_blink;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100162 int tl_cursor_shape; // 1: block, 2: underline, 3: bar
163 char_u *tl_cursor_color; // NULL or allocated
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200164
165 int tl_using_altscreen;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200166 garray_T tl_osc_buf; // incomplete OSC string
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200167};
168
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100169#define TMODE_ONCE 1 // CTRL-\ CTRL-N used
170#define TMODE_LOOP 2 // CTRL-W N used
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200171
172/*
173 * List of all active terminals.
174 */
175static term_T *first_term = NULL;
176
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100177// Terminal active in terminal_loop().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200178static term_T *in_terminal_loop = NULL;
179
Bram Moolenaar4f974752019-02-17 17:44:42 +0100180#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100181static BOOL has_winpty = FALSE;
182static BOOL has_conpty = FALSE;
183#endif
184
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100185#define MAX_ROW 999999 // used for tl_dirty_row_end to update all rows
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200186#define KEY_BUF_LEN 200
187
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200188#define FOR_ALL_TERMS(term) \
189 for ((term) = first_term; (term) != NULL; (term) = (term)->tl_next)
190
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200191/*
192 * Functions with separate implementation for MS-Windows and Unix-like systems.
193 */
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200194static 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 +0200195static int create_pty_only(term_T *term, jobopt_T *opt);
196static void term_report_winsize(term_T *term, int rows, int cols);
197static void term_free_vterm(term_T *term);
Bram Moolenaar13568252018-03-16 20:46:58 +0100198#ifdef FEAT_GUI
199static void update_system_term(term_T *term);
200#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200201
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100202static void handle_postponed_scrollback(term_T *term);
203
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100204// The character that we know (or assume) that the terminal expects for the
205// backspace key.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200206static int term_backspace_char = BS;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100208// Store the last set and the desired cursor properties, so that we only update
209// them when needed. Doing it unnecessary may result in flicker.
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200210static char_u *last_set_cursor_color = NULL;
211static char_u *desired_cursor_color = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +0100212static int last_set_cursor_shape = -1;
213static int desired_cursor_shape = -1;
214static int last_set_cursor_blink = -1;
215static int desired_cursor_blink = -1;
216
217
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100218///////////////////////////////////////
219// 1. Generic code for all systems.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200220
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200221 static int
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200222cursor_color_equal(char_u *lhs_color, char_u *rhs_color)
223{
224 if (lhs_color != NULL && rhs_color != NULL)
225 return STRCMP(lhs_color, rhs_color) == 0;
226 return lhs_color == NULL && rhs_color == NULL;
227}
228
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200229 static void
230cursor_color_copy(char_u **to_color, char_u *from_color)
231{
232 // Avoid a free & alloc if the value is already right.
233 if (cursor_color_equal(*to_color, from_color))
234 return;
235 vim_free(*to_color);
236 *to_color = (from_color == NULL) ? NULL : vim_strsave(from_color);
237}
238
239 static char_u *
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200240cursor_color_get(char_u *color)
241{
242 return (color == NULL) ? (char_u *)"" : color;
243}
244
245
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200246/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200247 * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
Bram Moolenaar498c2562018-04-15 23:45:15 +0200248 * current window.
249 * Sets "rows" and/or "cols" to zero when it should follow the window size.
250 * Return TRUE if the size is the minimum size: "24*80".
251 */
252 static int
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200253parse_termwinsize(win_T *wp, int *rows, int *cols)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200254{
255 int minsize = FALSE;
256
257 *rows = 0;
258 *cols = 0;
259
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200260 if (*wp->w_p_tws != NUL)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200261 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200262 char_u *p = vim_strchr(wp->w_p_tws, 'x');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200263
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100264 // Syntax of value was already checked when it's set.
Bram Moolenaar498c2562018-04-15 23:45:15 +0200265 if (p == NULL)
266 {
267 minsize = TRUE;
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200268 p = vim_strchr(wp->w_p_tws, '*');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200269 }
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200270 *rows = atoi((char *)wp->w_p_tws);
Bram Moolenaar498c2562018-04-15 23:45:15 +0200271 *cols = atoi((char *)p + 1);
272 }
273 return minsize;
274}
275
276/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200277 * Determine the terminal size from 'termwinsize' and the current window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200278 */
279 static void
Bram Moolenaarb936b792020-09-04 18:34:09 +0200280set_term_and_win_size(term_T *term, jobopt_T *opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200281{
Bram Moolenaarb936b792020-09-04 18:34:09 +0200282 int rows, cols;
283 int minsize;
284
Bram Moolenaar13568252018-03-16 20:46:58 +0100285#ifdef FEAT_GUI
286 if (term->tl_system)
287 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100288 // Use the whole screen for the system command. However, it will start
289 // at the command line and scroll up as needed, using tl_toprow.
Bram Moolenaar13568252018-03-16 20:46:58 +0100290 term->tl_rows = Rows;
291 term->tl_cols = Columns;
Bram Moolenaar07b46af2018-04-10 14:56:18 +0200292 return;
Bram Moolenaar13568252018-03-16 20:46:58 +0100293 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100294#endif
Bram Moolenaarb936b792020-09-04 18:34:09 +0200295 term->tl_rows = curwin->w_height;
296 term->tl_cols = curwin->w_width;
297
298 minsize = parse_termwinsize(curwin, &rows, &cols);
299 if (minsize)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200300 {
Bram Moolenaarb936b792020-09-04 18:34:09 +0200301 if (term->tl_rows < rows)
302 term->tl_rows = rows;
303 if (term->tl_cols < cols)
304 term->tl_cols = cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200305 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200306 if ((opt->jo_set2 & JO2_TERM_ROWS))
307 term->tl_rows = opt->jo_term_rows;
308 else if (rows != 0)
309 term->tl_rows = rows;
310 if ((opt->jo_set2 & JO2_TERM_COLS))
311 term->tl_cols = opt->jo_term_cols;
312 else if (cols != 0)
313 term->tl_cols = cols;
314
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200315 if (!opt->jo_hidden)
Bram Moolenaarb936b792020-09-04 18:34:09 +0200316 {
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200317 if (term->tl_rows != curwin->w_height)
318 win_setheight_win(term->tl_rows, curwin);
319 if (term->tl_cols != curwin->w_width)
320 win_setwidth_win(term->tl_cols, curwin);
Bram Moolenaarb936b792020-09-04 18:34:09 +0200321
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200322 // Set 'winsize' now to avoid a resize at the next redraw.
323 if (!minsize && *curwin->w_p_tws != NUL)
324 {
325 char_u buf[100];
326
327 vim_snprintf((char *)buf, 100, "%dx%d",
328 term->tl_rows, term->tl_cols);
Bram Moolenaarac4174e2022-05-07 16:38:24 +0100329 set_option_value_give_err((char_u *)"termwinsize",
330 0L, buf, OPT_LOCAL);
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200331 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200332 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200333}
334
335/*
336 * Initialize job options for a terminal job.
337 * Caller may overrule some of them.
338 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100339 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200340init_job_options(jobopt_T *opt)
341{
342 clear_job_options(opt);
343
Bram Moolenaarac4174e2022-05-07 16:38:24 +0100344 opt->jo_mode = CH_MODE_RAW;
345 opt->jo_out_mode = CH_MODE_RAW;
346 opt->jo_err_mode = CH_MODE_RAW;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200347 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
348}
349
350/*
351 * Set job options mandatory for a terminal job.
352 */
353 static void
354setup_job_options(jobopt_T *opt, int rows, int cols)
355{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100356#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100357 // Win32: Redirecting the job output won't work, thus always connect stdout
358 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200359 if (!(opt->jo_set & JO_OUT_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200360#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200361 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100362 // Connect stdout to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200363 opt->jo_io[PART_OUT] = JIO_BUFFER;
364 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
365 opt->jo_modifiable[PART_OUT] = 0;
366 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
367 }
368
Bram Moolenaar4f974752019-02-17 17:44:42 +0100369#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100370 // Win32: Redirecting the job output won't work, thus always connect stderr
371 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200372 if (!(opt->jo_set & JO_ERR_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200373#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200374 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100375 // Connect stderr to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200376 opt->jo_io[PART_ERR] = JIO_BUFFER;
377 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
378 opt->jo_modifiable[PART_ERR] = 0;
379 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
380 }
381
382 opt->jo_pty = TRUE;
383 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
384 opt->jo_term_rows = rows;
385 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
386 opt->jo_term_cols = cols;
387}
388
389/*
Bram Moolenaar5c381eb2019-06-25 06:50:31 +0200390 * Flush messages on channels.
391 */
392 static void
393term_flush_messages()
394{
395 mch_check_messages();
396 parse_queued_messages();
397}
398
399/*
Bram Moolenaard96ff162018-02-18 22:13:29 +0100400 * Close a terminal buffer (and its window). Used when creating the terminal
401 * fails.
402 */
403 static void
404term_close_buffer(buf_T *buf, buf_T *old_curbuf)
405{
406 free_terminal(buf);
407 if (old_curbuf != NULL)
408 {
409 --curbuf->b_nwindows;
410 curbuf = old_curbuf;
411 curwin->w_buffer = curbuf;
412 ++curbuf->b_nwindows;
413 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100414 CHECK_CURBUF;
Bram Moolenaard96ff162018-02-18 22:13:29 +0100415
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100416 // Wiping out the buffer will also close the window and call
417 // free_terminal().
Bram Moolenaard96ff162018-02-18 22:13:29 +0100418 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
419}
420
421/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200422 * Start a terminal window and return its buffer.
Bram Moolenaar13568252018-03-16 20:46:58 +0100423 * Use either "argvar" or "argv", the other must be NULL.
424 * When "flags" has TERM_START_NOJOB only create the buffer, b_term and open
425 * the window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200426 * Returns NULL when failed.
427 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100428 buf_T *
429term_start(
430 typval_T *argvar,
431 char **argv,
432 jobopt_T *opt,
433 int flags)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200434{
435 exarg_T split_ea;
436 win_T *old_curwin = curwin;
437 term_T *term;
438 buf_T *old_curbuf = NULL;
439 int res;
440 buf_T *newbuf;
Bram Moolenaare1004402020-10-24 20:49:43 +0200441 int vertical = opt->jo_vertical || (cmdmod.cmod_split & WSP_VERT);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200442 jobopt_T orig_opt; // only partly filled
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200443
444 if (check_restricted() || check_secure())
445 return NULL;
Bram Moolenaare5b44862021-05-30 13:54:03 +0200446#ifdef FEAT_CMDWIN
447 if (cmdwin_type != 0)
448 {
449 emsg(_(e_cannot_open_terminal_from_command_line_window));
450 return NULL;
451 }
452#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200453
454 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
455 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
456 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
Bram Moolenaarb0992022020-01-30 14:55:42 +0100457 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))
458 || (argvar != NULL
459 && argvar->v_type == VAR_LIST
460 && argvar->vval.v_list != NULL
461 && argvar->vval.v_list->lv_first == &range_list_item))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200462 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000463 emsg(_(e_invalid_argument));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200464 return NULL;
465 }
466
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200467 term = ALLOC_CLEAR_ONE(term_T);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200468 if (term == NULL)
469 return NULL;
470 term->tl_dirty_row_end = MAX_ROW;
471 term->tl_cursor_visible = TRUE;
472 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
473 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100474#ifdef FEAT_GUI
475 term->tl_system = (flags & TERM_START_SYSTEM);
476#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200477 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100478 ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200479 ga_init2(&term->tl_osc_buf, sizeof(char), 300);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200480
Bram Moolenaaraeed2a62021-04-29 20:18:45 +0200481 setpcmark();
Bram Moolenaara80faa82020-04-12 19:37:17 +0200482 CLEAR_FIELD(split_ea);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200483 if (opt->jo_curwin)
484 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100485 // Create a new buffer in the current window.
Bram Moolenaar13568252018-03-16 20:46:58 +0100486 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200487 {
488 no_write_message();
489 vim_free(term);
490 return NULL;
491 }
492 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaarb1009092020-05-31 16:04:42 +0200493 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
494 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
495 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200496 {
497 vim_free(term);
498 return NULL;
499 }
500 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100501 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200502 {
503 buf_T *buf;
504
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100505 // Create a new buffer without a window. Make it the current buffer for
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100506 // a moment to be able to do the initializations.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200507 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
508 BLN_NEW | BLN_LISTED);
509 if (buf == NULL || ml_open(buf) == FAIL)
510 {
511 vim_free(term);
512 return NULL;
513 }
514 old_curbuf = curbuf;
515 --curbuf->b_nwindows;
516 curbuf = buf;
517 curwin->w_buffer = buf;
518 ++curbuf->b_nwindows;
519 }
520 else
521 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100522 // Open a new window or tab.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200523 split_ea.cmdidx = CMD_new;
524 split_ea.cmd = (char_u *)"new";
525 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100526 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200527 {
528 split_ea.line2 = opt->jo_term_rows;
529 split_ea.addr_count = 1;
530 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100531 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200532 {
533 split_ea.line2 = opt->jo_term_cols;
534 split_ea.addr_count = 1;
535 }
536
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100537 if (vertical)
Bram Moolenaare1004402020-10-24 20:49:43 +0200538 cmdmod.cmod_split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200539 ex_splitview(&split_ea);
540 if (curwin == old_curwin)
541 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100542 // split failed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200543 vim_free(term);
544 return NULL;
545 }
546 }
547 term->tl_buffer = curbuf;
548 curbuf->b_term = term;
549
550 if (!opt->jo_hidden)
551 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100552 // Only one size was taken care of with :new, do the other one. With
553 // "curwin" both need to be done.
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100554 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200555 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100556 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200557 win_setwidth(opt->jo_term_cols);
558 }
559
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100560 // Link the new terminal in the list of active terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200561 term->tl_next = first_term;
562 first_term = term;
563
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100564 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
565
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200566 if (opt->jo_term_name != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100567 {
568 vim_free(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200569 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100570 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100571 else if (argv != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100572 {
573 vim_free(curbuf->b_ffname);
Bram Moolenaar13568252018-03-16 20:46:58 +0100574 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100575 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200576 else
577 {
578 int i;
579 size_t len;
580 char_u *cmd, *p;
581
582 if (argvar->v_type == VAR_STRING)
583 {
584 cmd = argvar->vval.v_string;
585 if (cmd == NULL)
586 cmd = (char_u *)"";
587 else if (STRCMP(cmd, "NONE") == 0)
588 cmd = (char_u *)"pty";
589 }
590 else if (argvar->v_type != VAR_LIST
591 || argvar->vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +0100592 || argvar->vval.v_list->lv_len == 0
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100593 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200594 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
595 cmd = (char_u*)"";
596
597 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200598 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200599
600 for (i = 0; p != NULL; ++i)
601 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100602 // Prepend a ! to the command name to avoid the buffer name equals
603 // the executable, otherwise ":w!" would overwrite it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200604 if (i == 0)
605 vim_snprintf((char *)p, len, "!%s", cmd);
606 else
607 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
608 if (buflist_findname(p) == NULL)
609 {
610 vim_free(curbuf->b_ffname);
611 curbuf->b_ffname = p;
612 break;
613 }
614 }
615 }
Bram Moolenaare010c722020-02-24 21:37:54 +0100616 vim_free(curbuf->b_sfname);
617 curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200618 curbuf->b_fname = curbuf->b_ffname;
619
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100620 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
621
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200622 if (opt->jo_term_opencmd != NULL)
623 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
624
625 if (opt->jo_eof_chars != NULL)
626 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
627
628 set_string_option_direct((char_u *)"buftype", -1,
629 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200630 // Avoid that 'buftype' is reset when this buffer is entered.
631 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200632
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100633 // Mark the buffer as not modifiable. It can only be made modifiable after
634 // the job finished.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200635 curbuf->b_p_ma = FALSE;
636
Bram Moolenaarb936b792020-09-04 18:34:09 +0200637 set_term_and_win_size(term, opt);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100638#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200639 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
640#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200641 setup_job_options(opt, term->tl_rows, term->tl_cols);
642
Bram Moolenaar13568252018-03-16 20:46:58 +0100643 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100644 return curbuf;
645
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100646#if defined(FEAT_SESSION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100647 // Remember the command for the session file.
Bram Moolenaar13568252018-03-16 20:46:58 +0100648 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100649 term->tl_command = vim_strsave((char_u *)"NONE");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100650 else if (argvar->v_type == VAR_STRING)
651 {
652 char_u *cmd = argvar->vval.v_string;
653
654 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
655 term->tl_command = vim_strsave(cmd);
656 }
657 else if (argvar->v_type == VAR_LIST
658 && argvar->vval.v_list != NULL
659 && argvar->vval.v_list->lv_len > 0)
660 {
661 garray_T ga;
662 listitem_T *item;
663
664 ga_init2(&ga, 1, 100);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200665 FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100666 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100667 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100668 char_u *p;
669
670 if (s == NULL)
671 break;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100672 p = vim_strsave_fnameescape(s, VSE_NONE);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100673 if (p == NULL)
674 break;
675 ga_concat(&ga, p);
676 vim_free(p);
677 ga_append(&ga, ' ');
678 }
679 if (item == NULL)
680 {
681 ga_append(&ga, NUL);
682 term->tl_command = ga.ga_data;
683 }
684 else
685 ga_clear(&ga);
686 }
687#endif
688
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100689 if (opt->jo_term_kill != NULL)
690 {
691 char_u *p = skiptowhite(opt->jo_term_kill);
692
693 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
694 }
695
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200696 if (opt->jo_term_api != NULL)
Bram Moolenaar21109272020-01-30 16:27:20 +0100697 {
698 char_u *p = skiptowhite(opt->jo_term_api);
699
700 term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
701 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200702 else
703 term->tl_api = vim_strsave((char_u *)"Tapi_");
704
Bram Moolenaar83d47902020-03-26 20:34:00 +0100705 if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
706 term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
707
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100708 // System dependent: setup the vterm and maybe start the job in it.
Bram Moolenaar13568252018-03-16 20:46:58 +0100709 if (argv == NULL
710 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200711 && argvar->vval.v_string != NULL
712 && STRCMP(argvar->vval.v_string, "NONE") == 0)
713 res = create_pty_only(term, opt);
714 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200715 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200716
717 newbuf = curbuf;
718 if (res == OK)
719 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100720 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200721 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
722 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100723#ifdef FEAT_GUI
724 if (term->tl_system)
725 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100726 // display first line below typed command
Bram Moolenaar13568252018-03-16 20:46:58 +0100727 term->tl_toprow = msg_row + 1;
728 term->tl_dirty_row_end = 0;
729 }
730#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200731
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100732 // Make sure we don't get stuck on sending keys to the job, it leads to
733 // a deadlock if the job is waiting for Vim to read.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200734 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
735
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200736 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200737 {
738 --curbuf->b_nwindows;
739 curbuf = old_curbuf;
740 curwin->w_buffer = curbuf;
741 ++curbuf->b_nwindows;
742 }
Bram Moolenaar81035272021-12-16 18:02:07 +0000743 else if (vgetc_busy
744#ifdef FEAT_TIMERS
745 || timer_busy
746#endif
747 || input_busy)
748 {
749 char_u ignore[4];
750
751 // When waiting for input need to return and possibly end up in
752 // terminal_loop() instead.
753 ignore[0] = K_SPECIAL;
754 ignore[1] = KS_EXTRA;
755 ignore[2] = KE_IGNORE;
756 ignore[3] = NUL;
757 ins_typebuf(ignore, REMAP_NONE, 0, TRUE, FALSE);
758 typebuf_was_filled = TRUE;
759 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200760 }
761 else
762 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100763 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200764 return NULL;
765 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100766
Bram Moolenaar13568252018-03-16 20:46:58 +0100767 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar28ed4df2019-10-26 16:21:40 +0200768 if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
769 apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200770 return newbuf;
771}
772
773/*
774 * ":terminal": open a terminal window and execute a job in it.
775 */
776 void
777ex_terminal(exarg_T *eap)
778{
779 typval_T argvar[2];
780 jobopt_T opt;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100781 int opt_shell = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200782 char_u *cmd;
783 char_u *tofree = NULL;
784
785 init_job_options(&opt);
786
787 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100788 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200789 {
790 char_u *p, *ep;
791
792 cmd += 2;
793 p = skiptowhite(cmd);
794 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200795 if (ep != NULL)
796 {
797 if (ep < p)
798 p = ep;
799 else
800 ep = NULL;
801 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200802
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200803# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
804 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
805 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200806 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200807 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100808 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200809 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200810 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200811 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200812 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200813 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200814 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200815 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100816 opt.jo_term_norestore = 1;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100817 else if (OPTARG_HAS("shell"))
818 opt_shell = TRUE;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200819 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100820 {
821 opt.jo_set2 |= JO2_TERM_KILL;
822 opt.jo_term_kill = ep + 1;
823 p = skiptowhite(cmd);
824 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200825 else if (OPTARG_HAS("api"))
826 {
827 opt.jo_set2 |= JO2_TERM_API;
828 if (ep != NULL)
829 {
830 opt.jo_term_api = ep + 1;
831 p = skiptowhite(cmd);
832 }
833 else
834 opt.jo_term_api = NULL;
835 }
836 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200837 {
838 opt.jo_set2 |= JO2_TERM_ROWS;
839 opt.jo_term_rows = atoi((char *)ep + 1);
840 p = skiptowhite(cmd);
841 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200842 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200843 {
844 opt.jo_set2 |= JO2_TERM_COLS;
845 opt.jo_term_cols = atoi((char *)ep + 1);
846 p = skiptowhite(cmd);
847 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200848 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200849 {
850 char_u *buf = NULL;
851 char_u *keys;
852
Bram Moolenaar21109272020-01-30 16:27:20 +0100853 vim_free(opt.jo_eof_chars);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200854 p = skiptowhite(cmd);
855 *p = NUL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200856 keys = replace_termcodes(ep + 1, &buf,
857 REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200858 opt.jo_set2 |= JO2_EOF_CHARS;
859 opt.jo_eof_chars = vim_strsave(keys);
860 vim_free(buf);
861 *p = ' ';
862 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100863#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100864 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
865 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100866 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100867 int tty_type = NUL;
868
869 p = skiptowhite(cmd);
870 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
871 tty_type = 'w';
872 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
873 tty_type = 'c';
874 else
875 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000876 semsg(e_invalid_value_for_argument_str, "type");
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100877 goto theend;
878 }
879 opt.jo_set2 |= JO2_TTY_TYPE;
880 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100881 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100882#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200883 else
884 {
885 if (*p)
886 *p = NUL;
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000887 semsg(_(e_invalid_attribute_str), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100888 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200889 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200890# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200891 cmd = skipwhite(p);
892 }
893 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100894 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100895 // Make a copy of 'shell', an autocommand may change the option.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200896 tofree = cmd = vim_strsave(p_sh);
897
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100898 // default to close when the shell exits
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100899 if (opt.jo_term_finish == NUL)
Bram Moolenaare2978022020-04-26 14:47:44 +0200900 opt.jo_term_finish = TL_FINISH_CLOSE;
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100901 }
902
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200903 if (eap->addr_count > 0)
904 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100905 // Write lines from current buffer to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200906 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
907 opt.jo_io[PART_IN] = JIO_BUFFER;
908 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
909 opt.jo_in_top = eap->line1;
910 opt.jo_in_bot = eap->line2;
911 }
912
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100913 if (opt_shell && tofree == NULL)
914 {
915#ifdef UNIX
916 char **argv = NULL;
917 char_u *tofree1 = NULL;
918 char_u *tofree2 = NULL;
919
920 // :term ++shell command
921 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
922 term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaaradf4aa22019-11-10 22:36:44 +0100923 vim_free(argv);
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100924 vim_free(tofree1);
925 vim_free(tofree2);
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100926 goto theend;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100927#else
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100928# ifdef MSWIN
929 long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
930 char_u *newcmd;
931
932 newcmd = alloc(cmdlen);
933 if (newcmd == NULL)
934 goto theend;
935 tofree = newcmd;
936 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
937 cmd = newcmd;
938# else
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000939 emsg(_(e_sorry_plusplusshell_not_supported_on_this_system));
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100940 goto theend;
941# endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100942#endif
943 }
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100944 argvar[0].v_type = VAR_STRING;
945 argvar[0].vval.v_string = cmd;
946 argvar[1].v_type = VAR_UNKNOWN;
947 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100948
949theend:
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100950 vim_free(tofree);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200951 vim_free(opt.jo_eof_chars);
952}
953
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100954#if defined(FEAT_SESSION) || defined(PROTO)
955/*
956 * Write a :terminal command to the session file to restore the terminal in
957 * window "wp".
958 * Return FAIL if writing fails.
959 */
960 int
Bram Moolenaar0e655112020-09-11 20:36:36 +0200961term_write_session(FILE *fd, win_T *wp, hashtab_T *terminal_bufs)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100962{
Bram Moolenaar0e655112020-09-11 20:36:36 +0200963 const int bufnr = wp->w_buffer->b_fnum;
964 term_T *term = wp->w_buffer->b_term;
965
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200966 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200967 {
968 // There are multiple views into this terminal buffer. We don't want to
969 // create the terminal multiple times. If it's the first time, create,
970 // otherwise link to the first buffer.
971 char id_as_str[NUMBUFLEN];
972 hashitem_T *entry;
973
974 vim_snprintf(id_as_str, sizeof(id_as_str), "%d", bufnr);
975
976 entry = hash_find(terminal_bufs, (char_u *)id_as_str);
977 if (!HASHITEM_EMPTY(entry))
978 {
979 // we've already opened this terminal buffer
980 if (fprintf(fd, "execute 'buffer ' . s:term_buf_%d", bufnr) < 0)
981 return FAIL;
982 return put_eol(fd);
983 }
984 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100985
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100986 // Create the terminal and run the command. This is not without
987 // risk, but let's assume the user only creates a session when this
988 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100989 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
990 term->tl_cols, term->tl_rows) < 0)
991 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100992#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100993 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
994 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100995#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100996 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
997 return FAIL;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200998 if (put_eol(fd) != OK)
999 return FAIL;
1000
1001 if (fprintf(fd, "let s:term_buf_%d = bufnr()", bufnr) < 0)
1002 return FAIL;
1003
Bram Moolenaarc2c82052020-09-11 22:10:22 +02001004 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +02001005 {
1006 char *hash_key = alloc(NUMBUFLEN);
1007
1008 vim_snprintf(hash_key, NUMBUFLEN, "%d", bufnr);
1009 hash_add(terminal_bufs, (char_u *)hash_key);
1010 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001011
1012 return put_eol(fd);
1013}
1014
1015/*
1016 * Return TRUE if "buf" has a terminal that should be restored.
1017 */
1018 int
1019term_should_restore(buf_T *buf)
1020{
1021 term_T *term = buf->b_term;
1022
1023 return term != NULL && (term->tl_command == NULL
1024 || STRCMP(term->tl_command, "NONE") != 0);
1025}
1026#endif
1027
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001028/*
1029 * Free the scrollback buffer for "term".
1030 */
1031 static void
1032free_scrollback(term_T *term)
1033{
1034 int i;
1035
1036 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
1037 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
1038 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001039 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
1040 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
1041 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001042}
1043
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001044
1045// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +02001046static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001047
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001048/*
1049 * Free a terminal and everything it refers to.
1050 * Kills the job if there is one.
1051 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001052 * The actual terminal structure is freed later in free_unused_terminals(),
1053 * because callbacks may wipe out a buffer while the terminal is still
1054 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001055 */
1056 void
1057free_terminal(buf_T *buf)
1058{
1059 term_T *term = buf->b_term;
1060 term_T *tp;
1061
1062 if (term == NULL)
1063 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001064
1065 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001066 if (first_term == term)
1067 first_term = term->tl_next;
1068 else
1069 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
1070 if (tp->tl_next == term)
1071 {
1072 tp->tl_next = term->tl_next;
1073 break;
1074 }
1075
1076 if (term->tl_job != NULL)
1077 {
1078 if (term->tl_job->jv_status != JOB_ENDED
1079 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +01001080 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001081 job_stop(term->tl_job, NULL, "kill");
1082 job_unref(term->tl_job);
1083 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001084 term->tl_next = terminals_to_free;
1085 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001086
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001087 buf->b_term = NULL;
1088 if (in_terminal_loop == term)
1089 in_terminal_loop = NULL;
1090}
1091
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001092 void
1093free_unused_terminals()
1094{
1095 while (terminals_to_free != NULL)
1096 {
1097 term_T *term = terminals_to_free;
1098
1099 terminals_to_free = term->tl_next;
1100
1101 free_scrollback(term);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02001102 ga_clear(&term->tl_osc_buf);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001103
1104 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001105 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001106 vim_free(term->tl_title);
1107#ifdef FEAT_SESSION
1108 vim_free(term->tl_command);
1109#endif
1110 vim_free(term->tl_kill);
1111 vim_free(term->tl_status_text);
1112 vim_free(term->tl_opencmd);
1113 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001114 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001115#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001116 if (term->tl_out_fd != NULL)
1117 fclose(term->tl_out_fd);
1118#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001119 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001120 vim_free(term->tl_cursor_color);
1121 vim_free(term);
1122 }
1123}
1124
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001125/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001126 * Get the part that is connected to the tty. Normally this is PART_IN, but
1127 * when writing buffer lines to the job it can be another. This makes it
1128 * possible to do "1,5term vim -".
1129 */
1130 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001131get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001132{
1133#ifdef UNIX
1134 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1135 int i;
1136
1137 for (i = 0; i < 3; ++i)
1138 {
1139 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1140
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001141 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001142 return parts[i];
1143 }
1144#endif
1145 return PART_IN;
1146}
1147
1148/*
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001149 * Read any vterm output and send it on the channel.
1150 */
1151 static void
1152term_forward_output(term_T *term)
1153{
1154 VTerm *vterm = term->tl_vterm;
1155 char buf[KEY_BUF_LEN];
1156 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1157
1158 if (curlen > 0)
1159 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1160 (char_u *)buf, (int)curlen, NULL);
1161}
1162
1163/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001164 * Write job output "msg[len]" to the vterm.
1165 */
1166 static void
Bram Moolenaar36968af2021-11-15 17:13:11 +00001167term_write_job_output(term_T *term, char_u *msg_arg, size_t len_arg)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001168{
Bram Moolenaar36968af2021-11-15 17:13:11 +00001169 char_u *msg = msg_arg;
1170 size_t len = len_arg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001171 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001172 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar36968af2021-11-15 17:13:11 +00001173 size_t limit = term->tl_buffer->b_p_twsl * term->tl_cols * 3;
1174
1175 // Limit the length to 'termwinscroll' * cols * 3 bytes. Keep the text at
1176 // the end.
1177 if (len > limit)
1178 {
1179 char_u *p = msg + len - limit;
1180
1181 p -= (*mb_head_off)(msg, p);
1182 len -= p - msg;
1183 msg = p;
1184 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001185
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001186 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001187
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001188 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001189 if (prevlen != vterm_output_get_buffer_current(vterm))
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001190 term_forward_output(term);
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001191
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001192 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001193 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1194}
1195
1196 static void
1197update_cursor(term_T *term, int redraw)
1198{
1199 if (term->tl_normal_mode)
1200 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001201#ifdef FEAT_GUI
1202 if (term->tl_system)
1203 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1204 term->tl_cursor_pos.col);
1205 else
1206#endif
1207 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001208 if (redraw)
1209 {
1210 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1211 cursor_on();
1212 out_flush();
1213#ifdef FEAT_GUI
1214 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001215 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001216 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001217 gui_mch_flush();
1218 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001219#endif
1220 }
1221}
1222
1223/*
1224 * Invoked when "msg" output from a job was received. Write it to the terminal
1225 * of "buffer".
1226 */
1227 void
1228write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1229{
1230 size_t len = STRLEN(msg);
1231 term_T *term = buffer->b_term;
1232
Bram Moolenaar4f974752019-02-17 17:44:42 +01001233#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001234 // Win32: Cannot redirect output of the job, intercept it here and write to
1235 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001236 if (term->tl_out_fd != NULL)
1237 {
1238 ch_log(channel, "Writing %d bytes to output file", (int)len);
1239 fwrite(msg, len, 1, term->tl_out_fd);
1240 return;
1241 }
1242#endif
1243
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001244 if (term->tl_vterm == NULL)
1245 {
1246 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1247 return;
1248 }
1249 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01001250 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001251 term_write_job_output(term, msg, len);
1252
Bram Moolenaar13568252018-03-16 20:46:58 +01001253#ifdef FEAT_GUI
1254 if (term->tl_system)
1255 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001256 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001257 update_system_term(term);
1258 update_cursor(term, TRUE);
1259 }
1260 else
1261#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001262 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1263 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001264 if (!term->tl_normal_mode)
1265 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001266 // Don't use update_screen() when editing the command line, it gets
1267 // cleared.
1268 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001269 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar24959102022-05-07 20:01:16 +01001270 if (buffer == curbuf && (State & MODE_CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001271 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001272 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001273 // update_screen() can be slow, check the terminal wasn't closed
1274 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001275 if (buffer == curbuf && curbuf->b_term != NULL)
1276 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001277 }
1278 else
Bram Moolenaare5050712021-12-09 10:51:05 +00001279 redraw_after_callback(TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001280 }
1281}
1282
1283/*
1284 * Send a mouse position and click to the vterm
1285 */
1286 static int
1287term_send_mouse(VTerm *vterm, int button, int pressed)
1288{
1289 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001290 int row = mouse_row - W_WINROW(curwin);
1291 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001292
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001293#ifdef FEAT_PROP_POPUP
1294 if (popup_is_popup(curwin))
1295 {
1296 row -= popup_top_extra(curwin);
1297 col -= popup_left_extra(curwin);
1298 }
1299#endif
1300 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001301 if (button != 0)
1302 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001303 return TRUE;
1304}
1305
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001306static int enter_mouse_col = -1;
1307static int enter_mouse_row = -1;
1308
1309/*
1310 * Handle a mouse click, drag or release.
1311 * Return TRUE when a mouse event is sent to the terminal.
1312 */
1313 static int
1314term_mouse_click(VTerm *vterm, int key)
1315{
1316#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001317 // For modeless selection mouse drag and release events are ignored, unless
1318 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001319 static int ignore_drag_release = TRUE;
1320 VTermMouseState mouse_state;
1321
1322 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1323 if (mouse_state.flags == 0)
1324 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001325 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001326 switch (key)
1327 {
1328 case K_LEFTDRAG:
1329 case K_LEFTRELEASE:
1330 case K_RIGHTDRAG:
1331 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001332 // Ignore drag and release events when the button-down wasn't
1333 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001334 if (ignore_drag_release)
1335 {
1336 int save_mouse_col, save_mouse_row;
1337
1338 if (enter_mouse_col < 0)
1339 break;
1340
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001341 // mouse click in the window gave us focus, handle that
1342 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001343 save_mouse_col = mouse_col;
1344 save_mouse_row = mouse_row;
1345 mouse_col = enter_mouse_col;
1346 mouse_row = enter_mouse_row;
1347 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1348 mouse_col = save_mouse_col;
1349 mouse_row = save_mouse_row;
1350 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001351 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001352 case K_LEFTMOUSE:
1353 case K_RIGHTMOUSE:
1354 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1355 ignore_drag_release = TRUE;
1356 else
1357 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001358 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001359 if (clip_star.available)
1360 {
1361 int button, is_click, is_drag;
1362
1363 button = get_mouse_button(KEY2TERMCAP1(key),
1364 &is_click, &is_drag);
1365 if (mouse_model_popup() && button == MOUSE_LEFT
1366 && (mod_mask & MOD_MASK_SHIFT))
1367 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001368 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001369 button = MOUSE_RIGHT;
1370 mod_mask &= ~MOD_MASK_SHIFT;
1371 }
1372 clip_modeless(button, is_click, is_drag);
1373 }
1374 break;
1375
1376 case K_MIDDLEMOUSE:
1377 if (clip_star.available)
1378 insert_reg('*', TRUE);
1379 break;
1380 }
1381 enter_mouse_col = -1;
1382 return FALSE;
1383 }
1384#endif
1385 enter_mouse_col = -1;
1386
1387 switch (key)
1388 {
1389 case K_LEFTMOUSE:
1390 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1391 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1392 case K_LEFTRELEASE:
1393 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1394 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1395 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1396 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1397 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1398 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1399 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1400 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1401 }
1402 return TRUE;
1403}
1404
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001405/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001406 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1407 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001408 * Return the number of bytes in "buf".
1409 */
1410 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001411term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001412{
1413 VTerm *vterm = term->tl_vterm;
1414 VTermKey key = VTERM_KEY_NONE;
1415 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001416 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001417
1418 switch (c)
1419 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001420 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001421
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001422 // don't use VTERM_KEY_BACKSPACE, it always
1423 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001424 case K_BS: c = term_backspace_char; break;
1425
1426 case ESC: key = VTERM_KEY_ESCAPE; break;
1427 case K_DEL: key = VTERM_KEY_DEL; break;
1428 case K_DOWN: key = VTERM_KEY_DOWN; break;
1429 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1430 key = VTERM_KEY_DOWN; break;
1431 case K_END: key = VTERM_KEY_END; break;
1432 case K_S_END: mod = VTERM_MOD_SHIFT;
1433 key = VTERM_KEY_END; break;
1434 case K_C_END: mod = VTERM_MOD_CTRL;
1435 key = VTERM_KEY_END; break;
1436 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1437 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1438 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1439 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1440 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1441 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1442 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1443 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1444 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1445 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1446 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1447 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1448 case K_HOME: key = VTERM_KEY_HOME; break;
1449 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1450 key = VTERM_KEY_HOME; break;
1451 case K_C_HOME: mod = VTERM_MOD_CTRL;
1452 key = VTERM_KEY_HOME; break;
1453 case K_INS: key = VTERM_KEY_INS; break;
1454 case K_K0: key = VTERM_KEY_KP_0; break;
1455 case K_K1: key = VTERM_KEY_KP_1; break;
1456 case K_K2: key = VTERM_KEY_KP_2; break;
1457 case K_K3: key = VTERM_KEY_KP_3; break;
1458 case K_K4: key = VTERM_KEY_KP_4; break;
1459 case K_K5: key = VTERM_KEY_KP_5; break;
1460 case K_K6: key = VTERM_KEY_KP_6; break;
1461 case K_K7: key = VTERM_KEY_KP_7; break;
1462 case K_K8: key = VTERM_KEY_KP_8; break;
1463 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001464 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001465 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001466 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001467 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001468 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1469 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001470 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1471 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001472 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1473 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001474 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1475 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1476 case K_LEFT: key = VTERM_KEY_LEFT; break;
1477 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1478 key = VTERM_KEY_LEFT; break;
1479 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1480 key = VTERM_KEY_LEFT; break;
1481 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1482 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1483 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1484 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1485 key = VTERM_KEY_RIGHT; break;
1486 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1487 key = VTERM_KEY_RIGHT; break;
1488 case K_UP: key = VTERM_KEY_UP; break;
1489 case K_S_UP: mod = VTERM_MOD_SHIFT;
1490 key = VTERM_KEY_UP; break;
1491 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001492 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1493 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001494
Bram Moolenaara42ad572017-11-16 13:08:04 +01001495 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1496 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001497 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1498 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001499
1500 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001501 case K_LEFTMOUSE_NM:
1502 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001503 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001504 case K_LEFTRELEASE_NM:
1505 case K_MOUSEMOVE:
1506 case K_MIDDLEMOUSE:
1507 case K_MIDDLEDRAG:
1508 case K_MIDDLERELEASE:
1509 case K_RIGHTMOUSE:
1510 case K_RIGHTDRAG:
1511 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1512 return 0;
1513 other = TRUE;
1514 break;
1515
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001516 case K_X1MOUSE: /* TODO */ return 0;
1517 case K_X1DRAG: /* TODO */ return 0;
1518 case K_X1RELEASE: /* TODO */ return 0;
1519 case K_X2MOUSE: /* TODO */ return 0;
1520 case K_X2DRAG: /* TODO */ return 0;
1521 case K_X2RELEASE: /* TODO */ return 0;
1522
1523 case K_IGNORE: return 0;
1524 case K_NOP: return 0;
1525 case K_UNDO: return 0;
1526 case K_HELP: return 0;
1527 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1528 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1529 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1530 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1531 case K_SELECT: return 0;
1532#ifdef FEAT_GUI
1533 case K_VER_SCROLLBAR: return 0;
1534 case K_HOR_SCROLLBAR: return 0;
1535#endif
1536#ifdef FEAT_GUI_TABLINE
1537 case K_TABLINE: return 0;
1538 case K_TABMENU: return 0;
1539#endif
1540#ifdef FEAT_NETBEANS_INTG
1541 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1542#endif
1543#ifdef FEAT_DND
1544 case K_DROP: return 0;
1545#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001546 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001547 case K_PS: vterm_keyboard_start_paste(vterm);
1548 other = TRUE;
1549 break;
1550 case K_PE: vterm_keyboard_end_paste(vterm);
1551 other = TRUE;
1552 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001553 }
1554
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001555 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001556 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001557 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001558 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001559 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001560 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001561 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001562
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001563 /*
1564 * Convert special keys to vterm keys:
1565 * - Write keys to vterm: vterm_keyboard_key()
1566 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001567 */
1568 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001569 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001570 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001571 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001572 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001573 vterm_keyboard_unichar(vterm, c, mod);
1574
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001575 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001576 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1577}
1578
1579/*
1580 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001581 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001582 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001583 */
1584 static int
1585term_job_running_check(term_T *term, int check_job_status)
1586{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001587 // Also consider the job finished when the channel is closed, to avoid a
1588 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001589 if (term != NULL
1590 && term->tl_job != NULL
1591 && channel_is_open(term->tl_job->jv_channel))
1592 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001593 job_T *job = term->tl_job;
1594
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001595 // Careful: Checking the job status may invoke callbacks, which close
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001596 // the buffer and terminate "term". However, "job" will not be freed
1597 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001598 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001599 job_status(job);
1600 return (job->jv_status == JOB_STARTED
1601 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001602 }
1603 return FALSE;
1604}
1605
1606/*
1607 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001608 */
1609 int
1610term_job_running(term_T *term)
1611{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001612 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001613}
1614
1615/*
1616 * Return TRUE if "term" has an active channel and used ":term NONE".
1617 */
1618 int
1619term_none_open(term_T *term)
1620{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001621 // Also consider the job finished when the channel is closed, to avoid a
1622 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001623 return term != NULL
1624 && term->tl_job != NULL
1625 && channel_is_open(term->tl_job->jv_channel)
1626 && term->tl_job->jv_channel->ch_keep_open;
1627}
1628
1629/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001630 * Used when exiting: kill the job in "buf" if so desired.
1631 * Return OK when the job finished.
1632 * Return FAIL when the job is still running.
1633 */
1634 int
1635term_try_stop_job(buf_T *buf)
1636{
1637 int count;
1638 char *how = (char *)buf->b_term->tl_kill;
1639
1640#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001641 if ((how == NULL || *how == NUL)
1642 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001643 {
1644 char_u buff[DIALOG_MSG_SIZE];
1645 int ret;
1646
Bram Moolenaar00806bc2020-11-05 19:36:38 +01001647 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001648 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1649 if (ret == VIM_YES)
1650 how = "kill";
1651 else if (ret == VIM_CANCEL)
1652 return FAIL;
1653 }
1654#endif
1655 if (how == NULL || *how == NUL)
1656 return FAIL;
1657
1658 job_stop(buf->b_term->tl_job, NULL, how);
1659
Bram Moolenaar9172d232019-01-29 23:06:54 +01001660 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001661 for (count = 0; count < 100; ++count)
1662 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001663 job_T *job;
1664
1665 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001666 if (!buf_valid(buf)
1667 || buf->b_term == NULL
1668 || buf->b_term->tl_job == NULL)
1669 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001670 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001671
Bram Moolenaar9172d232019-01-29 23:06:54 +01001672 // Call job_status() to update jv_status. It may cause the job to be
1673 // cleaned up but it won't be freed.
1674 job_status(job);
1675 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001676 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001677
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001678 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001679 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001680 }
1681 return FAIL;
1682}
1683
1684/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001685 * Add the last line of the scrollback buffer to the buffer in the window.
1686 */
1687 static void
1688add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1689{
1690 buf_T *buf = term->tl_buffer;
1691 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1692 linenr_T lnum = buf->b_ml.ml_line_count;
1693
Bram Moolenaar4f974752019-02-17 17:44:42 +01001694#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001695 if (!enc_utf8 && enc_codepage > 0)
1696 {
1697 WCHAR *ret = NULL;
1698 int length = 0;
1699
1700 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1701 &ret, &length);
1702 if (ret != NULL)
1703 {
1704 WideCharToMultiByte_alloc(enc_codepage, 0,
1705 ret, length, (char **)&text, &len, 0, 0);
1706 vim_free(ret);
1707 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1708 vim_free(text);
1709 }
1710 }
1711 else
1712#endif
1713 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1714 if (empty)
1715 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001716 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001717 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001718 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001719 curbuf = curwin->w_buffer;
1720 }
1721}
1722
1723 static void
1724cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1725{
1726 attr->width = cell->width;
1727 attr->attrs = cell->attrs;
1728 attr->fg = cell->fg;
1729 attr->bg = cell->bg;
1730}
1731
1732 static int
1733equal_celattr(cellattr_T *a, cellattr_T *b)
1734{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001735 // We only compare the RGB colors, ignoring the ANSI index and type.
1736 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001737 return a->fg.red == b->fg.red
1738 && a->fg.green == b->fg.green
1739 && a->fg.blue == b->fg.blue
1740 && a->bg.red == b->bg.red
1741 && a->bg.green == b->bg.green
1742 && a->bg.blue == b->bg.blue;
1743}
1744
Bram Moolenaard96ff162018-02-18 22:13:29 +01001745/*
1746 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1747 * line at this position. Otherwise at the end.
1748 */
1749 static int
1750add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1751{
1752 if (ga_grow(&term->tl_scrollback, 1) == OK)
1753 {
1754 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1755 + term->tl_scrollback.ga_len;
1756
1757 if (lnum > 0)
1758 {
1759 int i;
1760
1761 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1762 {
1763 *line = *(line - 1);
1764 --line;
1765 }
1766 }
1767 line->sb_cols = 0;
1768 line->sb_cells = NULL;
1769 line->sb_fill_attr = *fill_attr;
1770 ++term->tl_scrollback.ga_len;
1771 return OK;
1772 }
1773 return FALSE;
1774}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001775
1776/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001777 * Remove the terminal contents from the scrollback and the buffer.
1778 * Used before adding a new scrollback line or updating the buffer for lines
1779 * displayed in the terminal.
1780 */
1781 static void
1782cleanup_scrollback(term_T *term)
1783{
1784 sb_line_T *line;
1785 garray_T *gap;
1786
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001787 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001788 gap = &term->tl_scrollback;
1789 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1790 && gap->ga_len > 0)
1791 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001792 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001793 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1794 vim_free(line->sb_cells);
1795 --gap->ga_len;
1796 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001797 curbuf = curwin->w_buffer;
1798 if (curbuf == term->tl_buffer)
1799 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001800}
1801
1802/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001803 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001804 */
1805 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001806update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001807{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001808 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001809 int len;
1810 int lines_skipped = 0;
1811 VTermPos pos;
1812 VTermScreenCell cell;
1813 cellattr_T fill_attr, new_fill_attr;
1814 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001815
1816 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1817 "Adding terminal window snapshot to buffer");
1818
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001819 // First remove the lines that were appended before, they might be
1820 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001821 cleanup_scrollback(term);
1822
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001823 screen = vterm_obtain_screen(term->tl_vterm);
1824 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001825 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1826 {
1827 len = 0;
1828 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1829 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1830 && cell.chars[0] != NUL)
1831 {
1832 len = pos.col + 1;
1833 new_fill_attr = term->tl_default_color;
1834 }
1835 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001836 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001837 cell2cellattr(&cell, &new_fill_attr);
1838
1839 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1840 ++lines_skipped;
1841 else
1842 {
1843 while (lines_skipped > 0)
1844 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001845 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001846 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001847 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001848 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001849 }
1850
1851 if (len == 0)
1852 p = NULL;
1853 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001854 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001855 if ((p != NULL || len == 0)
1856 && ga_grow(&term->tl_scrollback, 1) == OK)
1857 {
1858 garray_T ga;
1859 int width;
1860 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1861 + term->tl_scrollback.ga_len;
1862
1863 ga_init2(&ga, 1, 100);
1864 for (pos.col = 0; pos.col < len; pos.col += width)
1865 {
1866 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1867 {
1868 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001869 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001870 if (ga_grow(&ga, 1) == OK)
1871 ga.ga_len += utf_char2bytes(' ',
1872 (char_u *)ga.ga_data + ga.ga_len);
1873 }
1874 else
1875 {
1876 width = cell.width;
1877
1878 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001879 if (width == 2)
1880 // second cell of double-width character has the
1881 // same attributes.
1882 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001883
Bram Moolenaara79fd562018-12-20 20:47:32 +01001884 // Each character can be up to 6 bytes.
1885 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001886 {
1887 int i;
1888 int c;
1889
1890 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1891 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1892 (char_u *)ga.ga_data + ga.ga_len);
1893 }
1894 }
1895 }
1896 line->sb_cols = len;
1897 line->sb_cells = p;
1898 line->sb_fill_attr = new_fill_attr;
1899 fill_attr = new_fill_attr;
1900 ++term->tl_scrollback.ga_len;
1901
1902 if (ga_grow(&ga, 1) == FAIL)
1903 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1904 else
1905 {
1906 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1907 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1908 }
1909 ga_clear(&ga);
1910 }
1911 else
1912 vim_free(p);
1913 }
1914 }
1915
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001916 // Add trailing empty lines.
1917 for (pos.row = term->tl_scrollback.ga_len;
1918 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1919 ++pos.row)
1920 {
1921 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1922 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1923 }
1924
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001925 term->tl_dirty_snapshot = FALSE;
1926#ifdef FEAT_TIMERS
1927 term->tl_timer_set = FALSE;
1928#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001929}
1930
1931/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001932 * Loop over all windows in the current tab, and also curwin, which is not
1933 * encountered when using a terminal in a popup window.
1934 * Return TRUE if "*wp" was set to the next window.
1935 */
1936 static int
1937for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1938{
1939 if (*wp == NULL)
1940 *wp = firstwin;
1941 else if ((*wp)->w_next != NULL)
1942 *wp = (*wp)->w_next;
1943 else if (!*did_curwin)
1944 *wp = curwin;
1945 else
1946 return FALSE;
1947 if (*wp == curwin)
1948 *did_curwin = TRUE;
1949 return TRUE;
1950}
1951
1952/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001953 * If needed, add the current lines of the terminal to scrollback and to the
1954 * buffer. Called after the job has ended and when switching to
1955 * Terminal-Normal mode.
1956 * When "redraw" is TRUE redraw the windows that show the terminal.
1957 */
1958 static void
1959may_move_terminal_to_buffer(term_T *term, int redraw)
1960{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001961 if (term->tl_vterm == NULL)
1962 return;
1963
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001964 // Update the snapshot only if something changes or the buffer does not
1965 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001966 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1967 <= term->tl_scrollback_scrolled)
1968 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001969
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001970 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001971 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1972 &term->tl_default_color.fg, &term->tl_default_color.bg);
1973
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001974 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001975 {
1976 win_T *wp = NULL;
1977 int did_curwin = FALSE;
1978
1979 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001980 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001981 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001982 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001983 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1984 wp->w_cursor.col = 0;
1985 wp->w_valid = 0;
1986 if (wp->w_cursor.lnum >= wp->w_height)
1987 {
1988 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001989
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001990 if (wp->w_topline < min_topline)
1991 wp->w_topline = min_topline;
1992 }
1993 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001994 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001995 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001996 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001997}
1998
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001999#if defined(FEAT_TIMERS) || defined(PROTO)
2000/*
2001 * Check if any terminal timer expired. If so, copy text from the terminal to
2002 * the buffer.
2003 * Return the time until the next timer will expire.
2004 */
2005 int
2006term_check_timers(int next_due_arg, proftime_T *now)
2007{
2008 term_T *term;
2009 int next_due = next_due_arg;
2010
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002011 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002012 {
2013 if (term->tl_timer_set && !term->tl_normal_mode)
2014 {
2015 long this_due = proftime_time_left(&term->tl_timer_due, now);
2016
2017 if (this_due <= 1)
2018 {
2019 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002020 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002021 }
2022 else if (next_due == -1 || next_due > this_due)
2023 next_due = this_due;
2024 }
2025 }
2026
2027 return next_due;
2028}
2029#endif
2030
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002031/*
2032 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
2033 * otherwise end it.
2034 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002035 static void
2036set_terminal_mode(term_T *term, int normal_mode)
2037{
2038 term->tl_normal_mode = normal_mode;
LemonBoy2bf52dd2022-04-09 18:17:34 +01002039 may_trigger_modechanged();
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002040 if (!normal_mode)
2041 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002042 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002043 if (term->tl_buffer == curbuf)
2044 maketitle();
2045}
2046
2047/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002048 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002049 * Move the vterm contents into the scrollback buffer and free the vterm.
2050 */
2051 static void
2052cleanup_vterm(term_T *term)
2053{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002054 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002055 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002056 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002057 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002058}
2059
2060/*
2061 * Switch from Terminal-Job mode to Terminal-Normal mode.
2062 * Suspends updating the terminal window.
2063 */
2064 static void
2065term_enter_normal_mode(void)
2066{
2067 term_T *term = curbuf->b_term;
2068
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002069 set_terminal_mode(term, TRUE);
2070
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002071 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002072 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002073
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002074 // Move the window cursor to the position of the cursor in the
2075 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002076 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2077 + term->tl_cursor_pos.row + 1;
2078 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002079 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2080 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002081 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002082
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002083 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002084 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2085}
2086
2087/*
2088 * Returns TRUE if the current window contains a terminal and we are in
2089 * Terminal-Normal mode.
2090 */
2091 int
2092term_in_normal_mode(void)
2093{
2094 term_T *term = curbuf->b_term;
2095
2096 return term != NULL && term->tl_normal_mode;
2097}
2098
2099/*
2100 * Switch from Terminal-Normal mode to Terminal-Job mode.
2101 * Restores updating the terminal window.
2102 */
2103 void
2104term_enter_job_mode()
2105{
2106 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002107
2108 set_terminal_mode(term, FALSE);
2109
2110 if (term->tl_channel_closed)
2111 cleanup_vterm(term);
2112 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002113#ifdef FEAT_PROP_POPUP
2114 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002115 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002116#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002117}
2118
2119/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002120 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002121 * Note: while waiting a terminal may be closed and freed if the channel is
Bram Moolenaar829c8e82021-12-14 08:41:38 +00002122 * closed and ++close was used. This may even happen before we get here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002123 */
2124 static int
2125term_vgetc()
2126{
2127 int c;
2128 int save_State = State;
Bram Moolenaar829c8e82021-12-14 08:41:38 +00002129 int modify_other_keys = curbuf->b_term->tl_vterm == NULL ? FALSE
2130 : vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002131
Bram Moolenaar24959102022-05-07 20:01:16 +01002132 State = MODE_TERMINAL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002133 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002134#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002135 ctrl_break_was_pressed = FALSE;
2136#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002137 if (modify_other_keys)
2138 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002139 c = vgetc();
2140 got_int = FALSE;
2141 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002142 if (modify_other_keys)
2143 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002144 return c;
2145}
2146
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002147static int mouse_was_outside = FALSE;
2148
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002149/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002150 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002151 * Return FAIL when the key needs to be handled in Normal mode.
2152 * Return OK when the key was dropped or sent to the terminal.
2153 */
2154 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002155send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002156{
2157 char msg[KEY_BUF_LEN];
2158 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002159 int dragging_outside = FALSE;
2160
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002161 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002162 switch (c)
2163 {
2164 case NUL:
2165 case K_ZERO:
2166 if (typed)
2167 stuffcharReadbuff(c);
2168 return FAIL;
2169
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002170 case K_TABLINE:
2171 stuffcharReadbuff(c);
2172 return FAIL;
2173
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002174 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002175 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002176 return FAIL;
2177
2178 case K_LEFTDRAG:
2179 case K_MIDDLEDRAG:
2180 case K_RIGHTDRAG:
2181 case K_X1DRAG:
2182 case K_X2DRAG:
2183 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002184 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002185 case K_LEFTMOUSE:
2186 case K_LEFTMOUSE_NM:
2187 case K_LEFTRELEASE:
2188 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002189 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002190 case K_MIDDLEMOUSE:
2191 case K_MIDDLERELEASE:
2192 case K_RIGHTMOUSE:
2193 case K_RIGHTRELEASE:
2194 case K_X1MOUSE:
2195 case K_X1RELEASE:
2196 case K_X2MOUSE:
2197 case K_X2RELEASE:
2198
2199 case K_MOUSEUP:
2200 case K_MOUSEDOWN:
2201 case K_MOUSELEFT:
2202 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002203 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002204 int row = mouse_row;
2205 int col = mouse_col;
2206
2207#ifdef FEAT_PROP_POPUP
2208 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002209 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002210 row -= popup_top_extra(curwin);
2211 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002212 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002213#endif
2214 if (row < W_WINROW(curwin)
2215 || row >= (W_WINROW(curwin) + curwin->w_height)
2216 || col < curwin->w_wincol
2217 || col >= W_ENDCOL(curwin)
2218 || dragging_outside)
2219 {
2220 // click or scroll outside the current window or on status
2221 // line or vertical separator
2222 if (typed)
2223 {
2224 stuffcharReadbuff(c);
2225 mouse_was_outside = TRUE;
2226 }
2227 return FAIL;
2228 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002229 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002230 break;
2231
2232 case K_COMMAND:
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002233 case K_SCRIPT_COMMAND:
2234 return do_cmdkey_command(c, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002235 }
2236 if (typed)
2237 mouse_was_outside = FALSE;
2238
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002239 // Convert the typed key to a sequence of bytes for the job.
2240 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002241 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002242 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002243 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2244 (char_u *)msg, (int)len, NULL);
2245
2246 return OK;
2247}
2248
2249 static void
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002250position_cursor(win_T *wp, VTermPos *pos)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002251{
2252 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2253 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002254#ifdef FEAT_PROP_POPUP
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002255 if (popup_is_popup(wp))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002256 {
Bram Moolenaarf5452692020-11-28 21:56:06 +01002257 wp->w_wrow += popup_top_extra(wp);
2258 wp->w_wcol += popup_left_extra(wp);
Bram Moolenaar6a076442020-11-15 20:32:58 +01002259 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002260 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01002261 else
2262 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002263#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002264 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2265}
2266
2267/*
2268 * Handle CTRL-W "": send register contents to the job.
2269 */
2270 static void
2271term_paste_register(int prev_c UNUSED)
2272{
2273 int c;
2274 list_T *l;
2275 listitem_T *item;
2276 long reglen = 0;
2277 int type;
2278
2279#ifdef FEAT_CMDL_INFO
2280 if (add_to_showcmd(prev_c))
2281 if (add_to_showcmd('"'))
2282 out_flush();
2283#endif
2284 c = term_vgetc();
2285#ifdef FEAT_CMDL_INFO
2286 clear_showcmd();
2287#endif
2288 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002289 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002290 return;
2291
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002292 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002293 if (c == '=' && get_expr_register() != '=')
2294 return;
2295 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002296 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002297 return;
2298
2299 l = (list_T *)get_reg_contents(c, GREG_LIST);
2300 if (l != NULL)
2301 {
2302 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002303 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002304 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002305 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002306#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002307 char_u *tmp = s;
2308
2309 if (!enc_utf8 && enc_codepage > 0)
2310 {
2311 WCHAR *ret = NULL;
2312 int length = 0;
2313
2314 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2315 (int)STRLEN(s), &ret, &length);
2316 if (ret != NULL)
2317 {
2318 WideCharToMultiByte_alloc(CP_UTF8, 0,
2319 ret, length, (char **)&s, &length, 0, 0);
2320 vim_free(ret);
2321 }
2322 }
2323#endif
2324 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2325 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002326#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002327 if (tmp != s)
2328 vim_free(s);
2329#endif
2330
2331 if (item->li_next != NULL || type == MLINE)
2332 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2333 (char_u *)"\r", 1, NULL);
2334 }
2335 list_free(l);
2336 }
2337}
2338
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002339/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002340 * Return TRUE when waiting for a character in the terminal, the cursor of the
2341 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002342 */
2343 int
2344terminal_is_active()
2345{
2346 return in_terminal_loop != NULL;
2347}
2348
Bram Moolenaar83d47902020-03-26 20:34:00 +01002349/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002350 * Return the highight group ID for the terminal and the window.
Bram Moolenaar83d47902020-03-26 20:34:00 +01002351 */
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002352 static int
2353term_get_highlight_id(term_T *term, win_T *wp)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002354{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002355 char_u *name;
2356
2357 if (wp != NULL && *wp->w_p_wcr != NUL)
2358 name = wp->w_p_wcr;
2359 else if (term->tl_highlight_name != NULL)
2360 name = term->tl_highlight_name;
2361 else
2362 name = (char_u*)"Terminal";
2363
2364 return syn_name2id(name);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002365}
2366
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002367#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002368 cursorentry_T *
2369term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2370{
2371 term_T *term = in_terminal_loop;
2372 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002373 int id;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002374 guicolor_T term_fg = INVALCOLOR;
2375 guicolor_T term_bg = INVALCOLOR;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002376
Bram Moolenaara80faa82020-04-12 19:37:17 +02002377 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002378 entry.shape = entry.mshape =
2379 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2380 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2381 SHAPE_BLOCK;
2382 entry.percentage = 20;
2383 if (term->tl_cursor_blink)
2384 {
2385 entry.blinkwait = 700;
2386 entry.blinkon = 400;
2387 entry.blinkoff = 250;
2388 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002389
Bram Moolenaar83d47902020-03-26 20:34:00 +01002390 // The highlight group overrules the defaults.
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002391 id = term_get_highlight_id(term, curwin);
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002392 if (id != 0)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002393 syn_id2colors(id, &term_fg, &term_bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002394 if (term_bg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002395 *fg = term_bg;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002396 else
2397 *fg = gui.back_pixel;
2398
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002399 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002400 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002401 if (term_fg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002402 *bg = term_fg;
2403 else
2404 *bg = gui.norm_pixel;
2405 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002406 else
2407 *bg = color_name2handle(term->tl_cursor_color);
2408 entry.name = "n";
2409 entry.used_for = SHAPE_CURSOR;
2410
2411 return &entry;
2412}
2413#endif
2414
Bram Moolenaard317b382018-02-08 22:33:31 +01002415 static void
2416may_output_cursor_props(void)
2417{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002418 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002419 || last_set_cursor_shape != desired_cursor_shape
2420 || last_set_cursor_blink != desired_cursor_blink)
2421 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002422 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002423 last_set_cursor_shape = desired_cursor_shape;
2424 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002425 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002426 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002427 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002428 ui_cursor_shape_forced(TRUE);
2429 else
2430 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2431 }
2432}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002433
Bram Moolenaard317b382018-02-08 22:33:31 +01002434/*
2435 * Set the cursor color and shape, if not last set to these.
2436 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002437 static void
2438may_set_cursor_props(term_T *term)
2439{
2440#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002441 // For the GUI the cursor properties are obtained with
2442 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002443 if (gui.in_use)
2444 return;
2445#endif
2446 if (in_terminal_loop == term)
2447 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002448 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002449 desired_cursor_shape = term->tl_cursor_shape;
2450 desired_cursor_blink = term->tl_cursor_blink;
2451 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002452 }
2453}
2454
Bram Moolenaard317b382018-02-08 22:33:31 +01002455/*
2456 * Reset the desired cursor properties and restore them when needed.
2457 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002458 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002459prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002460{
2461#ifdef FEAT_GUI
2462 if (gui.in_use)
2463 return;
2464#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002465 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002466 desired_cursor_shape = -1;
2467 desired_cursor_blink = -1;
2468 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002469}
2470
2471/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002472 * Returns TRUE if the current window contains a terminal and we are sending
2473 * keys to the job.
2474 * If "check_job_status" is TRUE update the job status.
2475 */
2476 static int
2477term_use_loop_check(int check_job_status)
2478{
2479 term_T *term = curbuf->b_term;
2480
2481 return term != NULL
2482 && !term->tl_normal_mode
2483 && term->tl_vterm != NULL
2484 && term_job_running_check(term, check_job_status);
2485}
2486
2487/*
2488 * Returns TRUE if the current window contains a terminal and we are sending
2489 * keys to the job.
2490 */
2491 int
2492term_use_loop(void)
2493{
2494 return term_use_loop_check(FALSE);
2495}
2496
2497/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002498 * Called when entering a window with the mouse. If this is a terminal window
2499 * we may want to change state.
2500 */
2501 void
2502term_win_entered()
2503{
2504 term_T *term = curbuf->b_term;
2505
2506 if (term != NULL)
2507 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002508 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002509 {
2510 reset_VIsual_and_resel();
Bram Moolenaar24959102022-05-07 20:01:16 +01002511 if (State & MODE_INSERT)
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002512 stop_insert_mode = TRUE;
2513 }
2514 mouse_was_outside = FALSE;
2515 enter_mouse_col = mouse_col;
2516 enter_mouse_row = mouse_row;
2517 }
2518}
2519
Bram Moolenaara48d4e42021-12-08 22:13:38 +00002520 void
2521term_focus_change(int in_focus)
2522{
2523 term_T *term = curbuf->b_term;
2524
2525 if (term != NULL && term->tl_vterm != NULL)
2526 {
2527 VTermState *state = vterm_obtain_state(term->tl_vterm);
2528
2529 if (in_focus)
2530 vterm_state_focus_in(state);
2531 else
2532 vterm_state_focus_out(state);
2533 term_forward_output(term);
2534 }
2535}
2536
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002537/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002538 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2539 * Return the Ctrl-key value in that case.
2540 */
2541 static int
2542raw_c_to_ctrl(int c)
2543{
2544 if ((mod_mask & MOD_MASK_CTRL)
2545 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2546 return c & 0x1f;
2547 return c;
2548}
2549
2550/*
2551 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2552 * May set "mod_mask".
2553 */
2554 static int
2555ctrl_to_raw_c(int c)
2556{
2557 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2558 {
2559 mod_mask |= MOD_MASK_CTRL;
2560 return c + '@';
2561 }
2562 return c;
2563}
2564
2565/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002566 * Wait for input and send it to the job.
2567 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2568 * when there is no more typahead.
2569 * Return when the start of a CTRL-W command is typed or anything else that
2570 * should be handled as a Normal mode command.
2571 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2572 * the terminal was closed.
2573 */
2574 int
2575terminal_loop(int blocking)
2576{
2577 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002578 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002579 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002580 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002581#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002582 int tty_fd = curbuf->b_term->tl_job->jv_channel
2583 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002584#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002585 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002586
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002587 // Remember the terminal we are sending keys to. However, the terminal
2588 // might be closed while waiting for a character, e.g. typing "exit" in a
2589 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2590 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002591 in_terminal_loop = curbuf->b_term;
2592
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002593 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002594 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002595 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002596 if (termwinkey == Ctrl_W)
2597 termwinkey = 0;
2598 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002599 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002600 may_set_cursor_props(curbuf->b_term);
2601
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002602 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002603 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002604#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002605 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002606#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002607 // TODO: skip screen update when handling a sequence of keys.
2608 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002609 while (must_redraw != 0)
2610 if (update_screen(0) == FAIL)
2611 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002612 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002613 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002614 break;
2615
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002616 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002617 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002618
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002619 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002620 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002621 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002622 // Job finished while waiting for a character. Push back the
2623 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002624 if (raw_c != K_IGNORE)
2625 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002626 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002627 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002628 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002629 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002630 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002631
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002632#ifdef UNIX
2633 /*
2634 * The shell or another program may change the tty settings. Getting
2635 * them for every typed character is a bit of overhead, but it's needed
2636 * for the first character typed, e.g. when Vim starts in a shell.
2637 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002638 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002639 {
2640 ttyinfo_T info;
2641
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002642 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002643 if (get_tty_info(tty_fd, &info) == OK)
2644 term_backspace_char = info.backspace;
2645 }
2646#endif
2647
Bram Moolenaar4f974752019-02-17 17:44:42 +01002648#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002649 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2650 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002651 if (ctrl_break_was_pressed)
2652 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2653#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002654 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2655 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002656 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002657#ifdef FEAT_GUI
2658 && !curbuf->b_term->tl_system
2659#endif
2660 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002661 {
2662 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002663 int prev_raw_c = raw_c;
2664 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002665
2666#ifdef FEAT_CMDL_INFO
2667 if (add_to_showcmd(c))
2668 out_flush();
2669#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002670 raw_c = term_vgetc();
2671 c = raw_c_to_ctrl(raw_c);
2672
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002673#ifdef FEAT_CMDL_INFO
2674 clear_showcmd();
2675#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002676 if (!term_use_loop_check(TRUE)
2677 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002678 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002679 break;
2680
2681 if (prev_c == Ctrl_BSL)
2682 {
2683 if (c == Ctrl_N)
2684 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002685 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002686 term_enter_normal_mode();
2687 ret = FAIL;
2688 goto theend;
2689 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002690 // Send both keys to the terminal, first one here, second one
2691 // below.
2692 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2693 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002694 }
2695 else if (c == Ctrl_C)
2696 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002697 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002698 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2699 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002700 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002701 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002702 // "CTRL-W .": send CTRL-W to the job
2703 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002704 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002705 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002706 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002707 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002708 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002709 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002710 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002711 else if (c == 'N')
2712 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002713 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002714 term_enter_normal_mode();
2715 ret = FAIL;
2716 goto theend;
2717 }
2718 else if (c == '"')
2719 {
2720 term_paste_register(prev_c);
2721 continue;
2722 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002723 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002724 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002725 // space for CTRL-W, modifier, multi-byte char and NUL
2726 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002727
2728 // Put the command into the typeahead buffer, when using the
2729 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2730 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002731 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002732 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002733 ret = OK;
2734 goto theend;
2735 }
2736 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002737# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002738 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002739 {
2740 WCHAR wc;
2741 char_u mb[3];
2742
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002743 mb[0] = (unsigned)raw_c >> 8;
2744 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002745 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002746 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002747 }
2748# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002749 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002750 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002751 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002752 // We are sure to come back here, don't reset the cursor color
2753 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002754 restore_cursor = FALSE;
2755
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002756 ret = OK;
2757 goto theend;
2758 }
2759 }
2760 ret = FAIL;
2761
2762theend:
2763 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002764 if (restore_cursor)
2765 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002766
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002767 // Move a snapshot of the screen contents to the buffer, so that completion
2768 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002769 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2770 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002771
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002772 return ret;
2773}
2774
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002775 static void
2776may_toggle_cursor(term_T *term)
2777{
2778 if (in_terminal_loop == term)
2779 {
2780 if (term->tl_cursor_visible)
2781 cursor_on();
2782 else
2783 cursor_off();
2784 }
2785}
2786
2787/*
2788 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002789 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002790 */
2791 static int
2792color2index(VTermColor *color, int fg, int *boldp)
2793{
2794 int red = color->red;
2795 int blue = color->blue;
2796 int green = color->green;
2797
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002798 if (VTERM_COLOR_IS_INVALID(color))
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002799 return 0;
2800 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002801 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002802 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002803 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002804 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002805 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002806 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2807 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2808 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002809 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002810 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2811 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2812 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2813 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2814 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2815 case 10: return lookup_color(20, fg, boldp) + 1; // red
2816 case 11: return lookup_color(16, fg, boldp) + 1; // green
2817 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2818 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2819 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2820 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2821 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002822 }
2823 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002824
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002825 if (t_colors >= 256)
2826 {
2827 if (red == blue && red == green)
2828 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002829 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002830 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002831 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2832 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2833 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002834 int i;
2835
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002836 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002837 return 17; // 00/00/00
2838 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002839 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002840 for (i = 0; i < 23; ++i)
2841 if (red < cutoff[i])
2842 return i + 233;
2843 return 256;
2844 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002845 {
2846 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2847 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002848
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002849 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002850 for (ri = 0; ri < 5; ++ri)
2851 if (red < cutoff[ri])
2852 break;
2853 for (gi = 0; gi < 5; ++gi)
2854 if (green < cutoff[gi])
2855 break;
2856 for (bi = 0; bi < 5; ++bi)
2857 if (blue < cutoff[bi])
2858 break;
2859 return 17 + ri * 36 + gi * 6 + bi;
2860 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002861 }
2862 return 0;
2863}
2864
2865/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002866 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002867 */
2868 static int
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002869vtermAttr2hl(VTermScreenCellAttrs *cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002870{
2871 int attr = 0;
2872
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002873 if (cellattrs->bold)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002874 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002875 if (cellattrs->underline)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002876 attr |= HL_UNDERLINE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002877 if (cellattrs->italic)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002878 attr |= HL_ITALIC;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002879 if (cellattrs->strike)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002880 attr |= HL_STRIKETHROUGH;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002881 if (cellattrs->reverse)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002882 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002883 return attr;
2884}
2885
2886/*
2887 * Store Vterm attributes in "cell" from highlight flags.
2888 */
2889 static void
2890hl2vtermAttr(int attr, cellattr_T *cell)
2891{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002892 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002893 if (attr & HL_BOLD)
2894 cell->attrs.bold = 1;
2895 if (attr & HL_UNDERLINE)
2896 cell->attrs.underline = 1;
2897 if (attr & HL_ITALIC)
2898 cell->attrs.italic = 1;
2899 if (attr & HL_STRIKETHROUGH)
2900 cell->attrs.strike = 1;
2901 if (attr & HL_INVERSE)
2902 cell->attrs.reverse = 1;
2903}
2904
2905/*
2906 * Convert the attributes of a vterm cell into an attribute index.
2907 */
2908 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002909cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002910 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002911 win_T *wp,
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002912 VTermScreenCellAttrs *cellattrs,
2913 VTermColor *cellfg,
2914 VTermColor *cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002915{
2916 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002917 VTermColor *fg = cellfg;
2918 VTermColor *bg = cellbg;
2919 int is_default_fg = VTERM_COLOR_IS_DEFAULT_FG(fg);
2920 int is_default_bg = VTERM_COLOR_IS_DEFAULT_BG(bg);
2921
2922 if (is_default_fg || is_default_bg)
2923 {
2924 if (wp != NULL && *wp->w_p_wcr != NUL)
2925 {
2926 if (is_default_fg)
2927 fg = &wp->w_term_wincolor.fg;
2928 if (is_default_bg)
2929 bg = &wp->w_term_wincolor.bg;
2930 }
2931 else
2932 {
2933 if (is_default_fg)
2934 fg = &term->tl_default_color.fg;
2935 if (is_default_bg)
2936 bg = &term->tl_default_color.bg;
2937 }
2938 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002939
2940#ifdef FEAT_GUI
2941 if (gui.in_use)
2942 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002943 guicolor_T guifg = gui_mch_get_rgb_color(fg->red, fg->green, fg->blue);
2944 guicolor_T guibg = gui_mch_get_rgb_color(bg->red, bg->green, bg->blue);
2945 return get_gui_attr_idx(attr, guifg, guibg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002946 }
2947 else
2948#endif
2949#ifdef FEAT_TERMGUICOLORS
2950 if (p_tgc)
2951 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002952 guicolor_T tgcfg = VTERM_COLOR_IS_INVALID(fg)
2953 ? INVALCOLOR
2954 : gui_get_rgb_color_cmn(fg->red, fg->green, fg->blue);
2955 guicolor_T tgcbg = VTERM_COLOR_IS_INVALID(bg)
2956 ? INVALCOLOR
2957 : gui_get_rgb_color_cmn(bg->red, bg->green, bg->blue);
2958 return get_tgc_attr_idx(attr, tgcfg, tgcbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002959 }
2960 else
2961#endif
2962 {
2963 int bold = MAYBE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002964 int ctermfg = color2index(fg, TRUE, &bold);
2965 int ctermbg = color2index(bg, FALSE, &bold);
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002966
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002967 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002968 if (bold == TRUE)
2969 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002970
2971 return get_cterm_attr_idx(attr, ctermfg, ctermbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002972 }
2973 return 0;
2974}
2975
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002976 static void
2977set_dirty_snapshot(term_T *term)
2978{
2979 term->tl_dirty_snapshot = TRUE;
2980#ifdef FEAT_TIMERS
2981 if (!term->tl_normal_mode)
2982 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002983 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002984 profile_setlimit(100L, &term->tl_timer_due);
2985 term->tl_timer_set = TRUE;
2986 }
2987#endif
2988}
2989
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002990 static int
2991handle_damage(VTermRect rect, void *user)
2992{
2993 term_T *term = (term_T *)user;
2994
2995 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2996 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002997 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002998 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002999 return 1;
3000}
3001
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003002 static void
3003term_scroll_up(term_T *term, int start_row, int count)
3004{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003005 win_T *wp = NULL;
3006 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003007 VTermColor fg, bg;
3008 VTermScreenCellAttrs attr;
3009 int clear_attr;
3010
Bram Moolenaara80faa82020-04-12 19:37:17 +02003011 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003012
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003013 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003014 {
3015 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003016 {
3017 // Set the color to clear lines with.
3018 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
3019 &fg, &bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003020 clear_attr = cell2attr(term, wp, &attr, &fg, &bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003021 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003022 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003023 }
3024}
3025
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003026 static int
3027handle_moverect(VTermRect dest, VTermRect src, void *user)
3028{
3029 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003030 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003031
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003032 // Scrolling up is done much more efficiently by deleting lines instead of
3033 // redrawing the text. But avoid doing this multiple times, postpone until
3034 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003035 if (dest.start_col == src.start_col
3036 && dest.end_col == src.end_col
3037 && dest.start_row < src.start_row)
3038 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003039 if (dest.start_row == 0)
3040 term->tl_postponed_scroll += count;
3041 else
3042 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003043 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003044
3045 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3046 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003047 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003048
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003049 // Note sure if the scrolling will work correctly, let's do a complete
3050 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003051 redraw_buf_later(term->tl_buffer, NOT_VALID);
3052 return 1;
3053}
3054
3055 static int
3056handle_movecursor(
3057 VTermPos pos,
3058 VTermPos oldpos UNUSED,
3059 int visible,
3060 void *user)
3061{
3062 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003063 win_T *wp = NULL;
3064 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003065
3066 term->tl_cursor_pos = pos;
3067 term->tl_cursor_visible = visible;
3068
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003069 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003070 {
3071 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003072 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003073 }
3074 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003075 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003076
3077 return 1;
3078}
3079
3080 static int
3081handle_settermprop(
3082 VTermProp prop,
3083 VTermValue *value,
3084 void *user)
3085{
3086 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003087 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003088
3089 switch (prop)
3090 {
3091 case VTERM_PROP_TITLE:
ichizokae1bd872022-01-20 14:57:29 +00003092 if (disable_vterm_title_for_testing)
3093 break;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003094 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003095 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003096 if (strval == NULL)
3097 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003098 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003099 // a blank title isn't useful, make it empty, so that "running" is
3100 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003101 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003102 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003103 // Same as blank
3104 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003105 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003106 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3107 term->tl_title = NULL;
3108 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003109 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003110 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003111#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003112 else if (!enc_utf8 && enc_codepage > 0)
3113 {
3114 WCHAR *ret = NULL;
3115 int length = 0;
3116
3117 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003118 (char*)value->string.str,
3119 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003120 if (ret != NULL)
3121 {
3122 WideCharToMultiByte_alloc(enc_codepage, 0,
3123 ret, length, (char**)&term->tl_title,
3124 &length, 0, 0);
3125 vim_free(ret);
3126 }
3127 }
3128#endif
3129 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003130 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003131 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003132 strval = NULL;
3133 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003134 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003135 if (term == curbuf->b_term)
3136 maketitle();
3137 break;
3138
3139 case VTERM_PROP_CURSORVISIBLE:
3140 term->tl_cursor_visible = value->boolean;
3141 may_toggle_cursor(term);
3142 out_flush();
3143 break;
3144
3145 case VTERM_PROP_CURSORBLINK:
3146 term->tl_cursor_blink = value->boolean;
3147 may_set_cursor_props(term);
3148 break;
3149
3150 case VTERM_PROP_CURSORSHAPE:
3151 term->tl_cursor_shape = value->number;
3152 may_set_cursor_props(term);
3153 break;
3154
3155 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003156 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003157 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003158 if (strval == NULL)
3159 break;
3160 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003161 may_set_cursor_props(term);
3162 break;
3163
3164 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003165 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003166 term->tl_using_altscreen = value->boolean;
3167 break;
3168
3169 default:
3170 break;
3171 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003172 vim_free(strval);
3173
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003174 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003175 return 1;
3176}
3177
3178/*
3179 * The job running in the terminal resized the terminal.
3180 */
3181 static int
3182handle_resize(int rows, int cols, void *user)
3183{
3184 term_T *term = (term_T *)user;
3185 win_T *wp;
3186
3187 term->tl_rows = rows;
3188 term->tl_cols = cols;
3189 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003190 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003191 term->tl_vterm_size_changed = FALSE;
3192 else
3193 {
3194 FOR_ALL_WINDOWS(wp)
3195 {
3196 if (wp->w_buffer == term->tl_buffer)
3197 {
3198 win_setheight_win(rows, wp);
3199 win_setwidth_win(cols, wp);
3200 }
3201 }
3202 redraw_buf_later(term->tl_buffer, NOT_VALID);
3203 }
3204 return 1;
3205}
3206
3207/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003208 * If the number of lines that are stored goes over 'termscrollback' then
3209 * delete the first 10%.
3210 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3211 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003212 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003213 static void
3214limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003215{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003216 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003217 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003218 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003219 int i;
3220
3221 curbuf = term->tl_buffer;
3222 for (i = 0; i < todo; ++i)
3223 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003224 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3225 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003226 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003227 }
3228 curbuf = curwin->w_buffer;
3229
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003230 gap->ga_len -= todo;
3231 mch_memmove(gap->ga_data,
3232 (sb_line_T *)gap->ga_data + todo,
3233 sizeof(sb_line_T) * gap->ga_len);
3234 if (update_buffer)
3235 term->tl_scrollback_scrolled -= todo;
3236 }
3237}
3238
3239/*
3240 * Handle a line that is pushed off the top of the screen.
3241 */
3242 static int
3243handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3244{
3245 term_T *term = (term_T *)user;
3246 garray_T *gap;
3247 int update_buffer;
3248
3249 if (term->tl_normal_mode)
3250 {
3251 // In Terminal-Normal mode the user interacts with the buffer, thus we
3252 // must not change it. Postpone adding the scrollback lines.
3253 gap = &term->tl_scrollback_postponed;
3254 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003255 }
3256 else
3257 {
3258 // First remove the lines that were appended before, the pushed line
3259 // goes above it.
3260 cleanup_scrollback(term);
3261 gap = &term->tl_scrollback;
3262 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003263 }
3264
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003265 limit_scrollback(term, gap, update_buffer);
3266
3267 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003268 {
3269 cellattr_T *p = NULL;
3270 int len = 0;
3271 int i;
3272 int c;
3273 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003274 int text_len;
3275 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003276 sb_line_T *line;
3277 garray_T ga;
3278 cellattr_T fill_attr = term->tl_default_color;
3279
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003280 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003281 for (i = 0; i < cols; ++i)
3282 if (cells[i].chars[0] != 0)
3283 len = i + 1;
3284 else
3285 cell2cellattr(&cells[i], &fill_attr);
3286
3287 ga_init2(&ga, 1, 100);
3288 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003289 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003290 if (p != NULL)
3291 {
3292 for (col = 0; col < len; col += cells[col].width)
3293 {
3294 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3295 {
3296 ga.ga_len = 0;
3297 break;
3298 }
3299 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3300 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3301 (char_u *)ga.ga_data + ga.ga_len);
3302 cell2cellattr(&cells[col], &p[col]);
3303 }
3304 }
3305 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003306 {
3307 if (update_buffer)
3308 text = (char_u *)"";
3309 else
3310 text = vim_strsave((char_u *)"");
3311 text_len = 0;
3312 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003313 else
3314 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003315 text = ga.ga_data;
3316 text_len = ga.ga_len;
3317 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003318 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003319 if (update_buffer)
3320 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003321
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003322 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003323 line->sb_cols = len;
3324 line->sb_cells = p;
3325 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003326 if (update_buffer)
3327 {
3328 line->sb_text = NULL;
3329 ++term->tl_scrollback_scrolled;
3330 ga_clear(&ga); // free the text
3331 }
3332 else
3333 {
3334 line->sb_text = text;
3335 ga_init(&ga); // text is kept in tl_scrollback_postponed
3336 }
3337 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003338 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003339 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003340}
3341
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003342/*
3343 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3344 * received and stored in tl_scrollback_postponed.
3345 */
3346 static void
3347handle_postponed_scrollback(term_T *term)
3348{
3349 int i;
3350
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003351 if (term->tl_scrollback_postponed.ga_len == 0)
3352 return;
3353 ch_log(NULL, "Moving postponed scrollback to scrollback");
3354
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003355 // First remove the lines that were appended before, the pushed lines go
3356 // above it.
3357 cleanup_scrollback(term);
3358
3359 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3360 {
3361 char_u *text;
3362 sb_line_T *pp_line;
3363 sb_line_T *line;
3364
3365 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3366 break;
3367 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3368
3369 text = pp_line->sb_text;
3370 if (text == NULL)
3371 text = (char_u *)"";
3372 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3373 vim_free(pp_line->sb_text);
3374
3375 line = (sb_line_T *)term->tl_scrollback.ga_data
3376 + term->tl_scrollback.ga_len;
3377 line->sb_cols = pp_line->sb_cols;
3378 line->sb_cells = pp_line->sb_cells;
3379 line->sb_fill_attr = pp_line->sb_fill_attr;
3380 line->sb_text = NULL;
3381 ++term->tl_scrollback_scrolled;
3382 ++term->tl_scrollback.ga_len;
3383 }
3384
3385 ga_clear(&term->tl_scrollback_postponed);
3386 limit_scrollback(term, &term->tl_scrollback, TRUE);
3387}
3388
LemonBoy77771d32022-04-13 11:47:25 +01003389/*
3390 * Called when the terminal wants to ring the system bell.
3391 */
3392 static int
3393handle_bell(void *user UNUSED)
3394{
Bram Moolenaaraae97622022-04-13 14:28:07 +01003395 vim_beep(BO_TERM);
LemonBoy77771d32022-04-13 11:47:25 +01003396 return 0;
3397}
3398
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003399static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003400 handle_damage, // damage
3401 handle_moverect, // moverect
3402 handle_movecursor, // movecursor
3403 handle_settermprop, // settermprop
LemonBoy77771d32022-04-13 11:47:25 +01003404 handle_bell, // bell
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003405 handle_resize, // resize
3406 handle_pushline, // sb_pushline
3407 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003408};
3409
3410/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003411 * Do the work after the channel of a terminal was closed.
3412 * Must be called only when updating_screen is FALSE.
3413 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3414 */
3415 static int
3416term_after_channel_closed(term_T *term)
3417{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003418 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003419 if (!term->tl_normal_mode)
3420 {
3421 int fnum = term->tl_buffer->b_fnum;
3422
3423 cleanup_vterm(term);
3424
3425 if (term->tl_finish == TL_FINISH_CLOSE)
3426 {
3427 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003428 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003429#ifdef FEAT_PROP_POPUP
3430 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003431
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003432 // If this was a terminal in a popup window, go back to the
3433 // previous window.
3434 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3435 {
3436 pwin = curwin;
3437 if (win_valid(prevwin))
3438 win_enter(prevwin, FALSE);
3439 }
3440 else
3441#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003442 // If this is the last normal window: exit Vim.
3443 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3444 {
3445 exarg_T ea;
3446
Bram Moolenaara80faa82020-04-12 19:37:17 +02003447 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003448 ex_quit(&ea);
3449 return TRUE;
3450 }
3451
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003452 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003453 ch_log(NULL, "terminal job finished, closing window");
3454 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003455 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003456 if (curwin == aucmd_win)
3457 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003458 if (do_set_w_closing)
3459 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003460 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003461 if (do_set_w_closing)
3462 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003463 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003464#ifdef FEAT_PROP_POPUP
3465 if (pwin != NULL)
3466 popup_close_with_retval(pwin, 0);
3467#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003468 return TRUE;
3469 }
3470 if (term->tl_finish == TL_FINISH_OPEN
3471 && term->tl_buffer->b_nwindows == 0)
3472 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003473 char *cmd = term->tl_opencmd == NULL
3474 ? "botright sbuf %d"
3475 : (char *)term->tl_opencmd;
3476 size_t len = strlen(cmd) + 50;
3477 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003478
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003479 if (buf != NULL)
3480 {
3481 ch_log(NULL, "terminal job finished, opening window");
3482 vim_snprintf(buf, len, cmd, fnum);
3483 do_cmdline_cmd((char_u *)buf);
3484 vim_free(buf);
3485 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003486 }
3487 else
3488 ch_log(NULL, "terminal job finished");
3489 }
3490
3491 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3492 return FALSE;
3493}
3494
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003495#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3496/*
3497 * If the current window is a terminal in a popup window and the job has
3498 * finished, close the popup window and to back to the previous window.
3499 * Otherwise return FAIL.
3500 */
3501 int
3502may_close_term_popup(void)
3503{
3504 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3505 && !term_job_running(curbuf->b_term))
3506 {
3507 win_T *pwin = curwin;
3508
3509 if (win_valid(prevwin))
3510 win_enter(prevwin, FALSE);
3511 popup_close_with_retval(pwin, 0);
3512 return OK;
3513 }
3514 return FAIL;
3515}
3516#endif
3517
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003518/*
Bram Moolenaareea32af2021-11-21 14:51:13 +00003519 * Called when a channel is going to be closed, before invoking the close
3520 * callback.
3521 */
3522 void
3523term_channel_closing(channel_T *ch)
3524{
3525 term_T *term;
3526
3527 for (term = first_term; term != NULL; term = term->tl_next)
3528 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
3529 term->tl_channel_closing = TRUE;
3530}
3531
3532/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003533 * Called when a channel has been closed.
3534 * If this was a channel for a terminal window then finish it up.
3535 */
3536 void
3537term_channel_closed(channel_T *ch)
3538{
3539 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003540 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003541 int did_one = FALSE;
3542
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003543 for (term = first_term; term != NULL; term = next_term)
3544 {
3545 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003546 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003547 {
3548 term->tl_channel_closed = TRUE;
3549 did_one = TRUE;
3550
Bram Moolenaard23a8232018-02-10 18:45:26 +01003551 VIM_CLEAR(term->tl_title);
3552 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003553#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003554 if (term->tl_out_fd != NULL)
3555 {
3556 fclose(term->tl_out_fd);
3557 term->tl_out_fd = NULL;
3558 }
3559#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003560
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003561 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003562 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003563 // Cannot open or close windows now. Can happen when
3564 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003565 term->tl_channel_recently_closed = TRUE;
3566 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003567 }
3568
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003569 if (term_after_channel_closed(term))
3570 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003571 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003572 }
3573
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003574 if (did_one)
3575 {
3576 redraw_statuslines();
3577
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003578 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003579 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003580 typebuf_was_filled = TRUE;
3581
3582 term = curbuf->b_term;
3583 if (term != NULL)
3584 {
3585 if (term->tl_job == ch->ch_job)
3586 maketitle();
3587 update_cursor(term, term->tl_cursor_visible);
3588 }
3589 }
3590}
3591
3592/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003593 * To be called after resetting updating_screen: handle any terminal where the
3594 * channel was closed.
3595 */
3596 void
3597term_check_channel_closed_recently()
3598{
3599 term_T *term;
3600 term_T *next_term;
3601
3602 for (term = first_term; term != NULL; term = next_term)
3603 {
3604 next_term = term->tl_next;
3605 if (term->tl_channel_recently_closed)
3606 {
3607 term->tl_channel_recently_closed = FALSE;
3608 if (term_after_channel_closed(term))
3609 // start over, the list may have changed
3610 next_term = first_term;
3611 }
3612 }
3613}
3614
3615/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003616 * Fill one screen line from a line of the terminal.
3617 * Advances "pos" to past the last column.
3618 */
3619 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003620term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003621 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003622 win_T *wp,
3623 VTermScreen *screen,
3624 VTermPos *pos,
3625 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003626{
3627 int off = screen_get_current_line_off();
3628
3629 for (pos->col = 0; pos->col < max_col; )
3630 {
3631 VTermScreenCell cell;
3632 int c;
3633
3634 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003635 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003636
3637 c = cell.chars[0];
3638 if (c == NUL)
3639 {
3640 ScreenLines[off] = ' ';
3641 if (enc_utf8)
3642 ScreenLinesUC[off] = NUL;
3643 }
3644 else
3645 {
3646 if (enc_utf8)
3647 {
3648 int i;
3649
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003650 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003651 for (i = 0; i < Screen_mco
3652 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3653 {
3654 ScreenLinesC[i][off] = cell.chars[i + 1];
3655 if (cell.chars[i + 1] == 0)
3656 break;
3657 }
3658 if (c >= 0x80 || (Screen_mco > 0
3659 && ScreenLinesC[0][off] != 0))
3660 {
3661 ScreenLines[off] = ' ';
3662 ScreenLinesUC[off] = c;
3663 }
3664 else
3665 {
3666 ScreenLines[off] = c;
3667 ScreenLinesUC[off] = NUL;
3668 }
3669 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003670#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003671 else if (has_mbyte && c >= 0x80)
3672 {
3673 char_u mb[MB_MAXBYTES+1];
3674 WCHAR wc = c;
3675
3676 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3677 (char*)mb, 2, 0, 0) > 1)
3678 {
3679 ScreenLines[off] = mb[0];
3680 ScreenLines[off + 1] = mb[1];
3681 cell.width = mb_ptr2cells(mb);
3682 }
3683 else
3684 ScreenLines[off] = c;
3685 }
3686#endif
3687 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003688 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003689 ScreenLines[off] = c;
3690 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003691 ScreenAttrs[off] = cell2attr(term, wp, &cell.attrs, &cell.fg,
3692 &cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003693
3694 ++pos->col;
3695 ++off;
3696 if (cell.width == 2)
3697 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003698 // don't set the second byte to NUL for a DBCS encoding, it
3699 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003700 if (enc_utf8)
3701 {
3702 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003703 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003704 }
3705 else if (!has_mbyte)
3706 {
3707 // Can't show a double-width character with a single-byte
3708 // 'encoding', just use a space.
3709 ScreenLines[off] = ' ';
3710 ScreenAttrs[off] = ScreenAttrs[off - 1];
3711 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003712
3713 ++pos->col;
3714 ++off;
3715 }
3716 }
3717}
3718
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003719#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003720 static void
3721update_system_term(term_T *term)
3722{
3723 VTermPos pos;
3724 VTermScreen *screen;
3725
3726 if (term->tl_vterm == NULL)
3727 return;
3728 screen = vterm_obtain_screen(term->tl_vterm);
3729
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003730 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003731 while (term->tl_toprow > 0
3732 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3733 {
3734 int save_p_more = p_more;
3735
3736 p_more = FALSE;
3737 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003738 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003739 p_more = save_p_more;
3740 --term->tl_toprow;
3741 }
3742
3743 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3744 && pos.row < Rows; ++pos.row)
3745 {
3746 if (pos.row < term->tl_rows)
3747 {
3748 int max_col = MIN(Columns, term->tl_cols);
3749
Bram Moolenaar83d47902020-03-26 20:34:00 +01003750 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003751 }
3752 else
3753 pos.col = 0;
3754
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003755 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003756 }
3757
3758 term->tl_dirty_row_start = MAX_ROW;
3759 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003760}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003761#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003762
3763/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003764 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3765 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003766 * Terminal-Normal mode.
3767 */
3768 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003769term_do_update_window(win_T *wp)
3770{
3771 term_T *term = wp->w_buffer->b_term;
3772
3773 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3774}
3775
3776/*
3777 * Called to update a window that contains an active terminal.
3778 */
3779 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003780term_update_window(win_T *wp)
3781{
3782 term_T *term = wp->w_buffer->b_term;
3783 VTerm *vterm;
3784 VTermScreen *screen;
3785 VTermState *state;
3786 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003787 int rows, cols;
3788 int newrows, newcols;
3789 int minsize;
3790 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003791
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003792 vterm = term->tl_vterm;
3793 screen = vterm_obtain_screen(vterm);
3794 state = vterm_obtain_state(vterm);
3795
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003796 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3797 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003798 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003799 {
3800 term->tl_dirty_row_start = 0;
3801 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003802
3803 if (term->tl_postponed_scroll > 0
3804 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003805 // Scrolling is usually faster than redrawing, when there are only
3806 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003807 term_scroll_up(term, 0, term->tl_postponed_scroll);
3808 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003809 }
3810
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003811 /*
3812 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003813 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003814 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003815 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003816
Bram Moolenaar498c2562018-04-15 23:45:15 +02003817 newrows = 99999;
3818 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003819 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003820 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003821 // Always use curwin, it may be a popup window.
3822 win_T *wwp = twp == NULL ? curwin : twp;
3823
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003824 // When more than one window shows the same terminal, use the
3825 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003826 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003827 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003828 newrows = MIN(newrows, wwp->w_height);
3829 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003830 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003831 if (twp == NULL)
3832 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003833 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003834 if (newrows == 99999 || newcols == 99999)
3835 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003836 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3837 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3838
Bram Moolenaareba13e42021-02-23 17:47:23 +01003839 // If no cell is visible there is no point in resizing. Also, vterm can't
3840 // handle a zero height.
3841 if (newrows == 0 || newcols == 0)
3842 return;
3843
Bram Moolenaar498c2562018-04-15 23:45:15 +02003844 if (term->tl_rows != newrows || term->tl_cols != newcols)
3845 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003846 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003847 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003848 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003849 newrows);
3850 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003851
3852 // Updating the terminal size will cause the snapshot to be cleared.
3853 // When not in terminal_loop() we need to restore it.
3854 if (term != in_terminal_loop)
3855 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003856 }
3857
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003858 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003859 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003860 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003861
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003862 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3863 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003864 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003865 if (pos.row < term->tl_rows)
3866 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003867 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003868
Bram Moolenaar83d47902020-03-26 20:34:00 +01003869 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003870 }
3871 else
3872 pos.col = 0;
3873
Bram Moolenaarf118d482018-03-13 13:14:00 +01003874 screen_line(wp->w_winrow + pos.row
3875#ifdef FEAT_MENU
3876 + winbar_height(wp)
3877#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003878 , wp->w_wincol, pos.col, wp->w_width,
3879#ifdef FEAT_PROP_POPUP
3880 popup_is_popup(wp) ? SLF_POPUP :
3881#endif
3882 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003883 }
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00003884}
3885
3886/*
3887 * Called after updating all windows: may reset dirty rows.
3888 */
3889 void
3890term_did_update_window(win_T *wp)
3891{
3892 term_T *term = wp->w_buffer->b_term;
3893
3894 if (term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode
3895 && wp->w_redr_type == 0)
3896 {
3897 term->tl_dirty_row_start = MAX_ROW;
3898 term->tl_dirty_row_end = 0;
3899 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003900}
3901
3902/*
3903 * Return TRUE if "wp" is a terminal window where the job has finished.
3904 */
3905 int
3906term_is_finished(buf_T *buf)
3907{
3908 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3909}
3910
3911/*
3912 * Return TRUE if "wp" is a terminal window where the job has finished or we
3913 * are in Terminal-Normal mode, thus we show the buffer contents.
3914 */
3915 int
3916term_show_buffer(buf_T *buf)
3917{
3918 term_T *term = buf->b_term;
3919
3920 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3921}
3922
3923/*
3924 * The current buffer is going to be changed. If there is terminal
3925 * highlighting remove it now.
3926 */
3927 void
3928term_change_in_curbuf(void)
3929{
3930 term_T *term = curbuf->b_term;
3931
3932 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3933 {
3934 free_scrollback(term);
3935 redraw_buf_later(term->tl_buffer, NOT_VALID);
3936
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003937 // The buffer is now like a normal buffer, it cannot be easily
3938 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003939 set_string_option_direct((char_u *)"buftype", -1,
3940 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3941 }
3942}
3943
3944/*
3945 * Get the screen attribute for a position in the buffer.
3946 * Use a negative "col" to get the filler background color.
3947 */
3948 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003949term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003950{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003951 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003952 term_T *term = buf->b_term;
3953 sb_line_T *line;
3954 cellattr_T *cellattr;
3955
3956 if (lnum > term->tl_scrollback.ga_len)
3957 cellattr = &term->tl_default_color;
3958 else
3959 {
3960 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3961 if (col < 0 || col >= line->sb_cols)
3962 cellattr = &line->sb_fill_attr;
3963 else
3964 cellattr = line->sb_cells + col;
3965 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003966 return cell2attr(term, wp, &cellattr->attrs, &cellattr->fg, &cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003967}
3968
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003969/*
3970 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003971 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003972 */
3973 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003974cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003975{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003976 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3977 if (rgb->index == 0)
3978 rgb->type = VTERM_COLOR_RGB;
3979 else
3980 {
3981 rgb->type = VTERM_COLOR_INDEXED;
3982 --rgb->index;
3983 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003984}
3985
3986/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003987 * Initialize vterm color from the synID.
3988 * Returns TRUE if color is set to "fg" and "bg".
3989 * Otherwise returns FALSE.
3990 */
3991 static int
3992get_vterm_color_from_synid(int id, VTermColor *fg, VTermColor *bg)
3993{
3994#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3995 // Use the actual color for the GUI and when 'termguicolors' is set.
3996 if (0
3997# ifdef FEAT_GUI
3998 || gui.in_use
3999# endif
4000# ifdef FEAT_TERMGUICOLORS
4001 || p_tgc
4002# ifdef FEAT_VTP
4003 // Finally get INVALCOLOR on this execution path
4004 || (!p_tgc && t_colors >= 256)
4005# endif
4006# endif
4007 )
4008 {
4009 guicolor_T fg_rgb = INVALCOLOR;
4010 guicolor_T bg_rgb = INVALCOLOR;
4011
4012 if (id > 0)
4013 syn_id2colors(id, &fg_rgb, &bg_rgb);
4014
4015 if (fg_rgb != INVALCOLOR)
4016 {
4017 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
4018 fg->red = (unsigned)(rgb >> 16);
4019 fg->green = (unsigned)(rgb >> 8) & 255;
4020 fg->blue = (unsigned)rgb & 255;
4021 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
4022 }
4023 else
4024 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4025
4026 if (bg_rgb != INVALCOLOR)
4027 {
4028 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
4029 bg->red = (unsigned)(rgb >> 16);
4030 bg->green = (unsigned)(rgb >> 8) & 255;
4031 bg->blue = (unsigned)rgb & 255;
4032 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
4033 }
4034 else
4035 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4036
4037 return TRUE;
4038 }
4039 else
4040#endif
4041 if (t_colors >= 16)
4042 {
4043 int cterm_fg = -1;
4044 int cterm_bg = -1;
4045
4046 if (id > 0)
4047 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
4048
4049 if (cterm_fg >= 0)
4050 {
4051 cterm_color2vterm(cterm_fg, fg);
4052 fg->type |= VTERM_COLOR_DEFAULT_FG;
4053 }
4054 else
4055 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4056
4057 if (cterm_bg >= 0)
4058 {
4059 cterm_color2vterm(cterm_bg, bg);
4060 bg->type |= VTERM_COLOR_DEFAULT_BG;
4061 }
4062 else
4063 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4064
4065 return TRUE;
4066 }
4067
4068 return FALSE;
4069}
4070
4071 void
4072term_reset_wincolor(win_T *wp)
4073{
4074 wp->w_term_wincolor.fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4075 wp->w_term_wincolor.bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4076}
4077
4078/*
4079 * Cache the color of 'wincolor'.
4080 */
4081 void
4082term_update_wincolor(win_T *wp)
4083{
4084 int id = 0;
4085
4086 if (*wp->w_p_wcr != NUL)
4087 id = syn_name2id(wp->w_p_wcr);
4088 if (id == 0 || !get_vterm_color_from_synid(id, &wp->w_term_wincolor.fg,
4089 &wp->w_term_wincolor.bg))
4090 term_reset_wincolor(wp);
4091}
4092
4093/*
4094 * Called when option 'termguicolors' was set,
4095 * or when any highlight is changed.
4096 */
4097 void
4098term_update_wincolor_all()
4099{
4100 win_T *wp = NULL;
4101 int did_curwin = FALSE;
4102
4103 while (for_all_windows_and_curwin(&wp, &did_curwin))
4104 term_update_wincolor(wp);
4105}
4106
4107/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004108 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004109 */
4110 static void
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004111init_default_colors(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004112{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004113 VTermColor *fg, *bg;
4114 int fgval, bgval;
4115 int id;
4116
Bram Moolenaara80faa82020-04-12 19:37:17 +02004117 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004118 term->tl_default_color.width = 1;
4119 fg = &term->tl_default_color.fg;
4120 bg = &term->tl_default_color.bg;
4121
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004122 // Vterm uses a default black background. Set it to white when
4123 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004124 if (*p_bg == 'l')
4125 {
4126 fgval = 0;
4127 bgval = 255;
4128 }
4129 else
4130 {
4131 fgval = 255;
4132 bgval = 0;
4133 }
4134 fg->red = fg->green = fg->blue = fgval;
4135 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004136 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
4137 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004138
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004139 // The highlight group overrules the defaults.
4140 id = term_get_highlight_id(term, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004141
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004142 if (!get_vterm_color_from_synid(id, fg, bg))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004143 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004144#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004145 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004146#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004147
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004148 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004149 if (cterm_normal_fg_color > 0)
4150 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004151 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004152# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4153# ifdef VIMDLL
4154 if (!gui.in_use)
4155# endif
4156 {
4157 tmp = fg->red;
4158 fg->red = fg->blue;
4159 fg->blue = tmp;
4160 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004161# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004162 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004163# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004164 else
4165 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004166# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004167
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004168 if (cterm_normal_bg_color > 0)
4169 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004170 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004171# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4172# ifdef VIMDLL
4173 if (!gui.in_use)
4174# endif
4175 {
4176 tmp = fg->red;
4177 fg->red = fg->blue;
4178 fg->blue = tmp;
4179 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004180# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004181 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004182# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004183 else
4184 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004185# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004186 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004187}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004188
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004189#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4190/*
4191 * Set the 16 ANSI colors from array of RGB values
4192 */
4193 static void
4194set_vterm_palette(VTerm *vterm, long_u *rgb)
4195{
4196 int index = 0;
4197 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004198
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004199 for (; index < 16; index++)
4200 {
4201 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004202
Millyb771b6b2021-11-23 12:07:25 +00004203 color.type = VTERM_COLOR_RGB;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004204 color.red = (unsigned)(rgb[index] >> 16);
4205 color.green = (unsigned)(rgb[index] >> 8) & 255;
4206 color.blue = (unsigned)rgb[index] & 255;
Bram Moolenaar68afde42022-02-25 20:48:26 +00004207 color.index = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004208 vterm_state_set_palette_color(state, index, &color);
4209 }
4210}
4211
4212/*
4213 * Set the ANSI color palette from a list of colors
4214 */
4215 static int
4216set_ansi_colors_list(VTerm *vterm, list_T *list)
4217{
4218 int n = 0;
4219 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004220 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004221
Bram Moolenaarb0992022020-01-30 14:55:42 +01004222 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004223 {
4224 char_u *color_name;
4225 guicolor_T guicolor;
4226
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004227 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004228 if (color_name == NULL)
4229 return FAIL;
4230
4231 guicolor = GUI_GET_COLOR(color_name);
4232 if (guicolor == INVALCOLOR)
4233 return FAIL;
4234
4235 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4236 }
4237
4238 if (n != 16 || li != NULL)
4239 return FAIL;
4240
4241 set_vterm_palette(vterm, rgb);
4242
4243 return OK;
4244}
4245
4246/*
4247 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4248 */
4249 static void
4250init_vterm_ansi_colors(VTerm *vterm)
4251{
4252 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4253
4254 if (var != NULL
4255 && (var->di_tv.v_type != VAR_LIST
4256 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004257 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004258 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004259 semsg(_(e_invalid_argument_str), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004260}
4261#endif
4262
Bram Moolenaar52acb112018-03-18 19:20:22 +01004263/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004264 * Handles a "drop" command from the job in the terminal.
4265 * "item" is the file name, "item->li_next" may have options.
4266 */
4267 static void
4268handle_drop_command(listitem_T *item)
4269{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004270 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004271 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004272 int bufnr;
4273 win_T *wp;
4274 tabpage_T *tp;
4275 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004276 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004277
4278 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4279 FOR_ALL_TAB_WINDOWS(tp, wp)
4280 {
4281 if (wp->w_buffer->b_fnum == bufnr)
4282 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004283 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004284 goto_tabpage_win(tp, wp);
4285 return;
4286 }
4287 }
4288
Bram Moolenaara80faa82020-04-12 19:37:17 +02004289 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004290
4291 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4292 && opt_item->li_tv.vval.v_dict != NULL)
4293 {
4294 dict_T *dict = opt_item->li_tv.vval.v_dict;
4295 char_u *p;
4296
Bram Moolenaar8f667172018-12-14 15:38:31 +01004297 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004298 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004299 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004300 if (p != NULL)
4301 {
4302 if (check_ff_value(p) == FAIL)
4303 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4304 else
4305 ea.force_ff = *p;
4306 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004307 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004308 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004309 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004310 if (p != NULL)
4311 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004312 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004313 if (ea.cmd != NULL)
4314 {
4315 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4316 ea.force_enc = 11;
4317 tofree = ea.cmd;
4318 }
4319 }
4320
Bram Moolenaar8f667172018-12-14 15:38:31 +01004321 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004322 if (p != NULL)
4323 get_bad_opt(p, &ea);
4324
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004325 if (dict_has_key(dict, "bin"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004326 ea.force_bin = FORCE_BIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004327 if (dict_has_key(dict, "binary"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004328 ea.force_bin = FORCE_BIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004329 if (dict_has_key(dict, "nobin"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004330 ea.force_bin = FORCE_NOBIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004331 if (dict_has_key(dict, "nobinary"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004332 ea.force_bin = FORCE_NOBIN;
4333 }
4334
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004335 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004336 if (ea.cmd == NULL)
4337 ea.cmd = (char_u *)"split";
4338 ea.arg = fname;
4339 ea.cmdidx = CMD_split;
4340 ex_splitview(&ea);
4341
4342 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004343}
4344
4345/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004346 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4347 */
4348 static int
4349is_permitted_term_api(char_u *func, char_u *pat)
4350{
4351 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4352}
4353
4354/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004355 * Handles a function call from the job running in a terminal.
4356 * "item" is the function name, "item->li_next" has the arguments.
4357 */
4358 static void
4359handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4360{
4361 char_u *func;
4362 typval_T argvars[2];
4363 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004364 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004365
4366 if (item->li_next == NULL)
4367 {
4368 ch_log(channel, "Missing function arguments for call");
4369 return;
4370 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004371 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004372
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004373 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004374 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004375 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004376 return;
4377 }
4378
4379 argvars[0].v_type = VAR_NUMBER;
4380 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4381 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004382 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004383 funcexe.fe_firstline = 1L;
4384 funcexe.fe_lastline = 1L;
4385 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004386 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004387 {
4388 clear_tv(&rettv);
4389 ch_log(channel, "Function %s called", func);
4390 }
4391 else
4392 ch_log(channel, "Calling function %s failed", func);
4393}
4394
4395/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004396 * URL decoding (also know as Percent-encoding).
4397 *
4398 * Note this function currently is only used for decoding shell's
4399 * OSC 7 escape sequence which we can assume all bytes are valid
4400 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4401 * encoding bytes like 0xfe, 0xff.
4402 */
4403 static size_t
4404url_decode(const char *src, const size_t len, char_u *dst)
4405{
4406 size_t i = 0, j = 0;
4407
4408 while (i < len)
4409 {
4410 if (src[i] == '%' && i + 2 < len)
4411 {
4412 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4413 j++;
4414 i += 3;
4415 }
4416 else
4417 {
4418 dst[j] = src[i];
4419 i++;
4420 j++;
4421 }
4422 }
4423 dst[j] = '\0';
4424 return j;
4425}
4426
4427/*
4428 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4429 *
4430 * The OSC 7 sequence has the format of
4431 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4432 * and what VTerm provides via VTermStringFragment is
4433 * "file://HOSTNAME/CURRENT/DIR"
4434 */
4435 static void
4436sync_shell_dir(VTermStringFragment *frag)
4437{
4438 int offset = 7; // len of "file://" is 7
4439 char *pos = (char *)frag->str + offset;
4440 char_u *new_dir;
4441
4442 // remove HOSTNAME to get PWD
Bram Moolenaar918b0892021-05-08 20:09:24 +02004443 while (*pos != '/' && offset < (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004444 {
4445 offset += 1;
4446 pos += 1;
4447 }
4448
Bram Moolenaar918b0892021-05-08 20:09:24 +02004449 if (offset >= (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004450 {
4451 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
4452 frag->str);
4453 return;
4454 }
4455
4456 new_dir = alloc(frag->len - offset + 1);
4457 url_decode(pos, frag->len-offset, new_dir);
4458 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4459 vim_free(new_dir);
4460}
4461
4462/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004463 * Called by libvterm when it cannot recognize an OSC sequence.
4464 * We recognize a terminal API command.
4465 */
4466 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004467parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004468{
4469 term_T *term = (term_T *)user;
4470 js_read_T reader;
4471 typval_T tv;
4472 channel_T *channel = term->tl_job == NULL ? NULL
4473 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004474 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004475
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004476 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
4477 if (p_asd && command == 7)
4478 {
4479 sync_shell_dir(&frag);
4480 return 1;
4481 }
4482
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004483 if (command != 51)
4484 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004485
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004486 // Concatenate what was received until the final piece is found.
4487 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4488 {
4489 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004490 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004491 }
4492 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004493 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004494 if (!frag.final)
4495 return 1;
4496
4497 ((char *)gap->ga_data)[gap->ga_len] = 0;
4498 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004499 reader.js_fill = NULL;
4500 reader.js_used = 0;
4501 if (json_decode(&reader, &tv, 0) == OK
4502 && tv.v_type == VAR_LIST
4503 && tv.vval.v_list != NULL)
4504 {
4505 listitem_T *item = tv.vval.v_list->lv_first;
4506
4507 if (item == NULL)
4508 ch_log(channel, "Missing command");
4509 else
4510 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004511 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004512
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004513 // Make sure an invoked command doesn't delete the buffer (and the
4514 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004515 ++term->tl_buffer->b_locked;
4516
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004517 item = item->li_next;
4518 if (item == NULL)
4519 ch_log(channel, "Missing argument for %s", cmd);
4520 else if (STRCMP(cmd, "drop") == 0)
4521 handle_drop_command(item);
4522 else if (STRCMP(cmd, "call") == 0)
4523 handle_call_command(term, channel, item);
4524 else
4525 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004526 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004527 }
4528 }
4529 else
4530 ch_log(channel, "Invalid JSON received");
4531
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004532 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004533 clear_tv(&tv);
4534 return 1;
4535}
4536
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004537/*
4538 * Called by libvterm when it cannot recognize a CSI sequence.
4539 * We recognize the window position report.
4540 */
4541 static int
4542parse_csi(
4543 const char *leader UNUSED,
4544 const long args[],
4545 int argcount,
4546 const char *intermed UNUSED,
4547 char command,
4548 void *user)
4549{
4550 term_T *term = (term_T *)user;
4551 char buf[100];
4552 int len;
4553 int x = 0;
4554 int y = 0;
4555 win_T *wp;
4556
4557 // We recognize only CSI 13 t
4558 if (command != 't' || argcount != 1 || args[0] != 13)
4559 return 0; // not handled
4560
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004561 // When getting the window position is not possible or it fails it results
4562 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004563#if defined(FEAT_GUI) \
4564 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4565 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004566 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004567#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004568
4569 FOR_ALL_WINDOWS(wp)
4570 if (wp->w_buffer == term->tl_buffer)
4571 break;
4572 if (wp != NULL)
4573 {
4574#ifdef FEAT_GUI
4575 if (gui.in_use)
4576 {
4577 x += wp->w_wincol * gui.char_width;
4578 y += W_WINROW(wp) * gui.char_height;
4579 }
4580 else
4581#endif
4582 {
4583 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004584 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004585 x += wp->w_wincol * 7;
4586 y += W_WINROW(wp) * 10;
4587 }
4588 }
4589
4590 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4591 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4592 (char_u *)buf, len, NULL);
4593 return 1;
4594}
4595
Bram Moolenaard8637282020-05-20 18:41:41 +02004596static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004597 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004598 parse_csi, // csi
4599 parse_osc, // osc
Bram Moolenaar7da34152021-11-24 19:30:55 +00004600 NULL, // dcs
4601 NULL, // apc
4602 NULL, // pm
4603 NULL // sos
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004604};
4605
4606/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004607 * Use Vim's allocation functions for vterm so profiling works.
4608 */
4609 static void *
4610vterm_malloc(size_t size, void *data UNUSED)
4611{
Bram Moolenaar88137392021-11-12 16:01:15 +00004612 // make sure that the length is not zero
4613 return alloc_clear(size == 0 ? 1L : size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004614}
4615
4616 static void
4617vterm_memfree(void *ptr, void *data UNUSED)
4618{
4619 vim_free(ptr);
4620}
4621
4622static VTermAllocatorFunctions vterm_allocator = {
4623 &vterm_malloc,
4624 &vterm_memfree
4625};
4626
4627/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004628 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004629 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004630 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004631 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004632create_vterm(term_T *term, int rows, int cols)
4633{
4634 VTerm *vterm;
4635 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004636 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004637 VTermValue value;
4638
Bram Moolenaar756ef112018-04-10 12:04:27 +02004639 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004640 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004641 if (vterm == NULL)
4642 return FAIL;
4643
4644 // Allocate screen and state here, so we can bail out if that fails.
4645 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004646 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004647 if (state == NULL || screen == NULL)
4648 {
4649 vterm_free(vterm);
4650 return FAIL;
4651 }
4652
Bram Moolenaar52acb112018-03-18 19:20:22 +01004653 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004654 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004655 vterm_set_utf8(vterm, 1);
4656
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004657 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004658
4659 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004660 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004661 &term->tl_default_color.fg,
4662 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004663
Bram Moolenaar9e587872019-05-13 20:27:23 +02004664 if (t_colors < 16)
4665 // Less than 16 colors: assume that bold means using a bright color for
4666 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004667 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4668
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004669 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004670 vterm_screen_reset(screen, 1 /* hard */);
4671
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004672 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004673 vterm_screen_enable_altscreen(screen, 1);
4674
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004675 // For unix do not use a blinking cursor. In an xterm this causes the
4676 // cursor to blink if it's blinking in the xterm.
4677 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004678#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004679 if (GetCaretBlinkTime() == INFINITE)
4680 value.boolean = 0;
4681 else
4682 value.boolean = 1;
4683#else
4684 value.boolean = 0;
4685#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004686 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004687 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004688
4689 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004690}
4691
4692/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004693 * Called when option 'background' or 'termguicolors' was set,
4694 * or when any highlight is changed.
Bram Moolenaarad431992021-05-03 20:40:38 +02004695 */
4696 void
4697term_update_colors_all(void)
4698{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004699 term_T *term;
Bram Moolenaarad431992021-05-03 20:40:38 +02004700
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004701 FOR_ALL_TERMS(term)
4702 {
4703 if (term->tl_vterm == NULL)
4704 continue;
4705 init_default_colors(term);
4706 vterm_state_set_default_colors(
4707 vterm_obtain_state(term->tl_vterm),
4708 &term->tl_default_color.fg,
4709 &term->tl_default_color.bg);
4710 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004711}
4712
4713/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004714 * Return the text to show for the buffer name and status.
4715 */
4716 char_u *
4717term_get_status_text(term_T *term)
4718{
4719 if (term->tl_status_text == NULL)
4720 {
4721 char_u *txt;
4722 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004723 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004724
4725 if (term->tl_normal_mode)
4726 {
4727 if (term_job_running(term))
4728 txt = (char_u *)_("Terminal");
4729 else
4730 txt = (char_u *)_("Terminal-finished");
4731 }
4732 else if (term->tl_title != NULL)
4733 txt = term->tl_title;
4734 else if (term_none_open(term))
4735 txt = (char_u *)_("active");
4736 else if (term_job_running(term))
4737 txt = (char_u *)_("running");
4738 else
4739 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004740 fname = buf_get_fname(term->tl_buffer);
4741 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004742 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004743 if (term->tl_status_text != NULL)
4744 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004745 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004746 }
4747 return term->tl_status_text;
4748}
4749
4750/*
Bram Moolenaar3ad69532021-11-19 17:01:08 +00004751 * Clear the cached value of the status text.
4752 */
4753 void
4754term_clear_status_text(term_T *term)
4755{
4756 VIM_CLEAR(term->tl_status_text);
4757}
4758
4759/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004760 * Mark references in jobs of terminals.
4761 */
4762 int
4763set_ref_in_term(int copyID)
4764{
4765 int abort = FALSE;
4766 term_T *term;
4767 typval_T tv;
4768
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004769 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004770 if (term->tl_job != NULL)
4771 {
4772 tv.v_type = VAR_JOB;
4773 tv.vval.v_job = term->tl_job;
4774 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4775 }
4776 return abort;
4777}
4778
4779/*
4780 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004781 * Returns NULL when the buffer is not for a terminal window and logs a message
4782 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004783 */
4784 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004785term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004786{
4787 buf_T *buf;
4788
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004789 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004790 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004791 --emsg_off;
4792 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004793 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004794 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004795 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004796 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004797 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004798 return buf;
4799}
4800
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004801 static void
4802clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004803{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004804 CLEAR_FIELD(*cell);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004805 cell->fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4806 cell->bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004807}
4808
4809 static void
4810dump_term_color(FILE *fd, VTermColor *color)
4811{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004812 int index;
4813
4814 if (VTERM_COLOR_IS_INDEXED(color))
4815 index = color->index + 1;
4816 else if (color->type == 0)
4817 // use RGB values
4818 index = 255;
4819 else
4820 // default color
4821 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004822 fprintf(fd, "%02x%02x%02x%d",
4823 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004824 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004825}
4826
4827/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004828 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004829 *
4830 * Each screen cell in full is:
4831 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4832 * {characters} is a space for an empty cell
4833 * For a double-width character "+" is changed to "*" and the next cell is
4834 * skipped.
4835 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4836 * when "&" use the same as the previous cell.
4837 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4838 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4839 * {color-idx} is a number from 0 to 255
4840 *
4841 * Screen cell with same width, attributes and color as the previous one:
4842 * |{characters}
4843 *
4844 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4845 *
4846 * Repeating the previous screen cell:
4847 * @{count}
4848 */
4849 void
4850f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4851{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004852 buf_T *buf;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004853 term_T *term;
4854 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004855 int max_height = 0;
4856 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004857 stat_T st;
4858 FILE *fd;
4859 VTermPos pos;
4860 VTermScreen *screen;
4861 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004862 VTermState *state;
4863 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004864
4865 if (check_restricted() || check_secure())
4866 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004867
4868 if (in_vim9script()
4869 && (check_for_buffer_arg(argvars, 0) == FAIL
4870 || check_for_string_arg(argvars, 1) == FAIL
4871 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4872 return;
4873
4874 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004875 if (buf == NULL)
4876 return;
4877 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004878 if (term->tl_vterm == NULL)
4879 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004880 emsg(_(e_job_already_finished));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004881 return;
4882 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004883
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004884 if (argvars[2].v_type != VAR_UNKNOWN)
4885 {
4886 dict_T *d;
4887
4888 if (argvars[2].v_type != VAR_DICT)
4889 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004890 emsg(_(e_dictionary_required));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004891 return;
4892 }
4893 d = argvars[2].vval.v_dict;
4894 if (d != NULL)
4895 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004896 max_height = dict_get_number(d, (char_u *)"rows");
4897 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004898 }
4899 }
4900
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004901 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004902 if (fname == NULL)
4903 return;
4904 if (mch_stat((char *)fname, &st) >= 0)
4905 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004906 semsg(_(e_file_exists_str), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004907 return;
4908 }
4909
Bram Moolenaard96ff162018-02-18 22:13:29 +01004910 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4911 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004912 semsg(_(e_cant_create_file_str), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004913 return;
4914 }
4915
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004916 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004917
4918 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004919 state = vterm_obtain_state(term->tl_vterm);
4920 vterm_state_get_cursorpos(state, &cursor_pos);
4921
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004922 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4923 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004924 {
4925 int repeat = 0;
4926
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004927 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4928 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004929 {
4930 VTermScreenCell cell;
4931 int same_attr;
4932 int same_chars = TRUE;
4933 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004934 int is_cursor_pos = (pos.col == cursor_pos.col
4935 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004936
4937 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004938 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004939
4940 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4941 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004942 int c = cell.chars[i];
4943 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004944 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004945
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004946 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004947 if (i == 0)
4948 {
4949 c = (c == NUL) ? ' ' : c;
4950 pc = (pc == NUL) ? ' ' : pc;
4951 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004952 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004953 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004954 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004955 break;
4956 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004957 same_attr = vtermAttr2hl(&cell.attrs)
4958 == vtermAttr2hl(&prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004959 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4960 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004961 if (same_chars && cell.width == prev_cell.width && same_attr
4962 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004963 {
4964 ++repeat;
4965 }
4966 else
4967 {
4968 if (repeat > 0)
4969 {
4970 fprintf(fd, "@%d", repeat);
4971 repeat = 0;
4972 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004973 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004974
4975 if (cell.chars[0] == NUL)
4976 fputs(" ", fd);
4977 else
4978 {
4979 char_u charbuf[10];
4980 int len;
4981
4982 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4983 && cell.chars[i] != NUL; ++i)
4984 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004985 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004986 fwrite(charbuf, len, 1, fd);
4987 }
4988 }
4989
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004990 // When only the characters differ we don't write anything, the
4991 // following "|", "@" or NL will indicate using the same
4992 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004993 if (cell.width != prev_cell.width || !same_attr)
4994 {
4995 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004996 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004997 else
4998 fputs("+", fd);
4999
5000 if (same_attr)
5001 {
5002 fputs("&", fd);
5003 }
5004 else
5005 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005006 fprintf(fd, "%d", vtermAttr2hl(&cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005007 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005008 fputs("&", fd);
5009 else
5010 {
5011 fputs("#", fd);
5012 dump_term_color(fd, &cell.fg);
5013 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005014 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005015 fputs("&", fd);
5016 else
5017 {
5018 fputs("#", fd);
5019 dump_term_color(fd, &cell.bg);
5020 }
5021 }
5022 }
5023
5024 prev_cell = cell;
5025 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005026
5027 if (cell.width == 2)
5028 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005029 }
5030 if (repeat > 0)
5031 fprintf(fd, "@%d", repeat);
5032 fputs("\n", fd);
5033 }
5034
5035 fclose(fd);
5036}
5037
5038/*
5039 * Called when a dump is corrupted. Put a breakpoint here when debugging.
5040 */
5041 static void
5042dump_is_corrupt(garray_T *gap)
5043{
5044 ga_concat(gap, (char_u *)"CORRUPT");
5045}
5046
5047 static void
5048append_cell(garray_T *gap, cellattr_T *cell)
5049{
5050 if (ga_grow(gap, 1) == OK)
5051 {
5052 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
5053 ++gap->ga_len;
5054 }
5055}
5056
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005057 static void
5058clear_cellattr(cellattr_T *cell)
5059{
5060 CLEAR_FIELD(*cell);
5061 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
5062 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
5063}
5064
Bram Moolenaard96ff162018-02-18 22:13:29 +01005065/*
5066 * Read the dump file from "fd" and append lines to the current buffer.
5067 * Return the cell width of the longest line.
5068 */
5069 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01005070read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005071{
5072 int c;
5073 garray_T ga_text;
5074 garray_T ga_cell;
5075 char_u *prev_char = NULL;
5076 int attr = 0;
5077 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005078 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005079 term_T *term = curbuf->b_term;
5080 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005081 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005082
5083 ga_init2(&ga_text, 1, 90);
5084 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005085 clear_cellattr(&cell);
5086 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005087 cursor_pos->row = -1;
5088 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005089
5090 c = fgetc(fd);
5091 for (;;)
5092 {
5093 if (c == EOF)
5094 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02005095 if (c == '\r')
5096 {
5097 // DOS line endings? Ignore.
5098 c = fgetc(fd);
5099 }
5100 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005101 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005102 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005103 if (ga_text.ga_data == NULL)
5104 dump_is_corrupt(&ga_text);
5105 if (ga_grow(&term->tl_scrollback, 1) == OK)
5106 {
5107 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
5108 + term->tl_scrollback.ga_len;
5109
5110 if (max_cells < ga_cell.ga_len)
5111 max_cells = ga_cell.ga_len;
5112 line->sb_cols = ga_cell.ga_len;
5113 line->sb_cells = ga_cell.ga_data;
5114 line->sb_fill_attr = term->tl_default_color;
5115 ++term->tl_scrollback.ga_len;
5116 ga_init(&ga_cell);
5117
5118 ga_append(&ga_text, NUL);
5119 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5120 ga_text.ga_len, FALSE);
5121 }
5122 else
5123 ga_clear(&ga_cell);
5124 ga_text.ga_len = 0;
5125
5126 c = fgetc(fd);
5127 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005128 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005129 {
5130 int prev_len = ga_text.ga_len;
5131
Bram Moolenaar9271d052018-02-25 21:39:46 +01005132 if (c == '>')
5133 {
5134 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005135 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005136 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5137 cursor_pos->col = ga_cell.ga_len;
5138 }
5139
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005140 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005141 c = fgetc(fd);
5142 if (c != EOF)
5143 ga_append(&ga_text, c);
5144 for (;;)
5145 {
5146 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005147 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005148 || c == EOF || c == '\n')
5149 break;
5150 ga_append(&ga_text, c);
5151 }
5152
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005153 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005154 vim_free(prev_char);
5155 if (ga_text.ga_data != NULL)
5156 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5157 ga_text.ga_len - prev_len);
5158
Bram Moolenaar9271d052018-02-25 21:39:46 +01005159 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005160 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005161 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005162 }
5163 else if (c == '+' || c == '*')
5164 {
5165 int is_bg;
5166
5167 cell.width = c == '+' ? 1 : 2;
5168
5169 c = fgetc(fd);
5170 if (c == '&')
5171 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005172 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005173 c = fgetc(fd);
5174 }
5175 else if (isdigit(c))
5176 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005177 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005178 attr = 0;
5179 while (isdigit(c))
5180 {
5181 attr = attr * 10 + (c - '0');
5182 c = fgetc(fd);
5183 }
5184 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005185
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005186 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005187 for (is_bg = 0; is_bg <= 1; ++is_bg)
5188 {
5189 if (c == '&')
5190 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005191 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005192 c = fgetc(fd);
5193 }
5194 else if (c == '#')
5195 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005196 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005197
5198 c = fgetc(fd);
5199 red = hex2nr(c);
5200 c = fgetc(fd);
5201 red = (red << 4) + hex2nr(c);
5202 c = fgetc(fd);
5203 green = hex2nr(c);
5204 c = fgetc(fd);
5205 green = (green << 4) + hex2nr(c);
5206 c = fgetc(fd);
5207 blue = hex2nr(c);
5208 c = fgetc(fd);
5209 blue = (blue << 4) + hex2nr(c);
5210 c = fgetc(fd);
5211 if (!isdigit(c))
5212 dump_is_corrupt(&ga_text);
5213 while (isdigit(c))
5214 {
5215 index = index * 10 + (c - '0');
5216 c = fgetc(fd);
5217 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005218 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005219 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005220 type = VTERM_COLOR_RGB;
5221 if (index == 0)
5222 {
5223 if (is_bg)
5224 type |= VTERM_COLOR_DEFAULT_BG;
5225 else
5226 type |= VTERM_COLOR_DEFAULT_FG;
5227 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005228 }
5229 else
5230 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005231 type = VTERM_COLOR_INDEXED;
5232 index -= 1;
5233 }
5234 if (is_bg)
5235 {
5236 cell.bg.type = type;
5237 cell.bg.red = red;
5238 cell.bg.green = green;
5239 cell.bg.blue = blue;
5240 cell.bg.index = index;
5241 }
5242 else
5243 {
5244 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005245 cell.fg.red = red;
5246 cell.fg.green = green;
5247 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005248 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005249 }
5250 }
5251 else
5252 dump_is_corrupt(&ga_text);
5253 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005254 }
5255 else
5256 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005257 }
5258 else
5259 dump_is_corrupt(&ga_text);
5260
5261 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005262 if (cell.width == 2)
5263 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005264 }
5265 else if (c == '@')
5266 {
5267 if (prev_char == NULL)
5268 dump_is_corrupt(&ga_text);
5269 else
5270 {
5271 int count = 0;
5272
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005273 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005274 for (;;)
5275 {
5276 c = fgetc(fd);
5277 if (!isdigit(c))
5278 break;
5279 count = count * 10 + (c - '0');
5280 }
5281
5282 while (count-- > 0)
5283 {
5284 ga_concat(&ga_text, prev_char);
5285 append_cell(&ga_cell, &cell);
5286 }
5287 }
5288 }
5289 else
5290 {
5291 dump_is_corrupt(&ga_text);
5292 c = fgetc(fd);
5293 }
5294 }
5295
5296 if (ga_text.ga_len > 0)
5297 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005298 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005299 dump_is_corrupt(&ga_text);
5300 ga_append(&ga_text, NUL);
5301 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5302 ga_text.ga_len, FALSE);
5303 }
5304
5305 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005306 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005307 vim_free(prev_char);
5308
5309 return max_cells;
5310}
5311
5312/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005313 * Return an allocated string with at least "text_width" "=" characters and
5314 * "fname" inserted in the middle.
5315 */
5316 static char_u *
5317get_separator(int text_width, char_u *fname)
5318{
5319 int width = MAX(text_width, curwin->w_width);
5320 char_u *textline;
5321 int fname_size;
5322 char_u *p = fname;
5323 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005324 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005325
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005326 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005327 if (textline == NULL)
5328 return NULL;
5329
5330 fname_size = vim_strsize(fname);
5331 if (fname_size < width - 8)
5332 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005333 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005334 width = MAX(text_width, fname_size + 8);
5335 }
5336 else if (fname_size > width - 8)
5337 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005338 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005339 p = gettail(fname);
5340 fname_size = vim_strsize(p);
5341 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005342 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005343 while (fname_size > width - 8)
5344 {
5345 p += (*mb_ptr2len)(p);
5346 fname_size = vim_strsize(p);
5347 }
5348
5349 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5350 textline[i] = '=';
5351 textline[i++] = ' ';
5352
5353 STRCPY(textline + i, p);
5354 off = STRLEN(textline);
5355 textline[off] = ' ';
5356 for (i = 1; i < (width - fname_size) / 2; ++i)
5357 textline[off + i] = '=';
5358 textline[off + i] = NUL;
5359
5360 return textline;
5361}
5362
5363/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005364 * Common for "term_dumpdiff()" and "term_dumpload()".
5365 */
5366 static void
5367term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5368{
5369 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005370 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005371 char_u buf1[NUMBUFLEN];
5372 char_u buf2[NUMBUFLEN];
5373 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005374 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005375 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005376 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005377 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005378 char_u *textline = NULL;
5379
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005380 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005381 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005382 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005383 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005384 if (fname1 == NULL || (do_diff && fname2 == NULL))
5385 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005386 emsg(_(e_invalid_argument));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005387 return;
5388 }
5389 fd1 = mch_fopen((char *)fname1, READBIN);
5390 if (fd1 == NULL)
5391 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005392 semsg(_(e_cant_read_file_str), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005393 return;
5394 }
5395 if (do_diff)
5396 {
5397 fd2 = mch_fopen((char *)fname2, READBIN);
5398 if (fd2 == NULL)
5399 {
5400 fclose(fd1);
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005401 semsg(_(e_cant_read_file_str), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005402 return;
5403 }
5404 }
5405
5406 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005407 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5408 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5409 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5410 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5411 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005412
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005413 if (opt.jo_term_name == NULL)
5414 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005415 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005416
Bram Moolenaar51e14382019-05-25 20:21:28 +02005417 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005418 if (fname_tofree != NULL)
5419 {
5420 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5421 opt.jo_term_name = fname_tofree;
5422 }
5423 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005424
Bram Moolenaar87abab92019-06-03 21:14:59 +02005425 if (opt.jo_bufnr_buf != NULL)
5426 {
5427 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5428
5429 // With "bufnr" argument: enter the window with this buffer and make it
5430 // empty.
5431 if (wp == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005432 semsg(_(e_invalid_argument_str), "bufnr");
Bram Moolenaar87abab92019-06-03 21:14:59 +02005433 else
5434 {
5435 buf = curbuf;
5436 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005437 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005438 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005439 redraw_later(NOT_VALID);
5440 }
5441 }
5442 else
5443 // Create a new terminal window.
5444 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5445
Bram Moolenaard96ff162018-02-18 22:13:29 +01005446 if (buf != NULL && buf->b_term != NULL)
5447 {
5448 int i;
5449 linenr_T bot_lnum;
5450 linenr_T lnum;
5451 term_T *term = buf->b_term;
5452 int width;
5453 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005454 VTermPos cursor_pos1;
5455 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005456
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005457 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005458
Bram Moolenaard96ff162018-02-18 22:13:29 +01005459 rettv->vval.v_number = buf->b_fnum;
5460
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005461 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005462 width = read_dump_file(fd1, &cursor_pos1);
5463
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005464 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005465 if (cursor_pos1.row >= 0)
5466 {
5467 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5468 coladvance(cursor_pos1.col);
5469 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005470
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005471 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005472 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005473
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005474 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005475 if (!do_diff)
5476 goto theend;
5477
5478 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5479
Bram Moolenaar4a696342018-04-05 18:45:26 +02005480 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005481 if (textline == NULL)
5482 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005483 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5484 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5485 vim_free(textline);
5486
5487 textline = get_separator(width, fname2);
5488 if (textline == NULL)
5489 goto theend;
5490 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5491 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005492 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005493
5494 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005495 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005496 if (width2 > width)
5497 {
5498 vim_free(textline);
5499 textline = alloc(width2 + 1);
5500 if (textline == NULL)
5501 goto theend;
5502 width = width2;
5503 textline[width] = NUL;
5504 }
5505 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5506
5507 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5508 {
5509 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5510 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005511 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005512 for (i = 0; i < width; ++i)
5513 textline[i] = '-';
5514 }
5515 else
5516 {
5517 char_u *line1;
5518 char_u *line2;
5519 char_u *p1;
5520 char_u *p2;
5521 int col;
5522 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5523 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5524 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5525 ->sb_cells;
5526
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005527 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005528 line1 = vim_strsave(ml_get(lnum));
5529 if (line1 == NULL)
5530 break;
5531 p1 = line1;
5532
5533 line2 = ml_get(lnum + bot_lnum);
5534 p2 = line2;
5535 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5536 {
5537 int len1 = utfc_ptr2len(p1);
5538 int len2 = utfc_ptr2len(p2);
5539
5540 textline[col] = ' ';
5541 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005542 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005543 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005544 else if (lnum == cursor_pos1.row + 1
5545 && col == cursor_pos1.col
5546 && (cursor_pos1.row != cursor_pos2.row
5547 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005548 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005549 textline[col] = '>';
5550 else if (lnum == cursor_pos2.row + 1
5551 && col == cursor_pos2.col
5552 && (cursor_pos1.row != cursor_pos2.row
5553 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005554 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005555 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005556 else if (cellattr1 != NULL && cellattr2 != NULL)
5557 {
5558 if ((cellattr1 + col)->width
5559 != (cellattr2 + col)->width)
5560 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005561 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005562 &(cellattr2 + col)->fg))
5563 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005564 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005565 &(cellattr2 + col)->bg))
5566 textline[col] = 'b';
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005567 else if (vtermAttr2hl(&(cellattr1 + col)->attrs)
5568 != vtermAttr2hl(&((cellattr2 + col)->attrs)))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005569 textline[col] = 'a';
5570 }
5571 p1 += len1;
5572 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005573 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005574 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005575
5576 while (col < width)
5577 {
5578 if (*p1 == NUL && *p2 == NUL)
5579 textline[col] = '?';
5580 else if (*p1 == NUL)
5581 {
5582 textline[col] = '+';
5583 p2 += utfc_ptr2len(p2);
5584 }
5585 else
5586 {
5587 textline[col] = '-';
5588 p1 += utfc_ptr2len(p1);
5589 }
5590 ++col;
5591 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005592
5593 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005594 }
5595 if (add_empty_scrollback(term, &term->tl_default_color,
5596 term->tl_top_diff_rows) == OK)
5597 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5598 ++bot_lnum;
5599 }
5600
5601 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5602 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005603 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005604 for (i = 0; i < width; ++i)
5605 textline[i] = '+';
5606 if (add_empty_scrollback(term, &term->tl_default_color,
5607 term->tl_top_diff_rows) == OK)
5608 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5609 ++lnum;
5610 ++bot_lnum;
5611 }
5612
5613 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005614
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005615 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005616 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005617 }
5618
5619theend:
5620 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005621 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005622 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005623 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005624 fclose(fd2);
5625}
5626
5627/*
5628 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5629 * bottom files.
5630 * Return FAIL when this is not possible.
5631 */
5632 int
5633term_swap_diff()
5634{
5635 term_T *term = curbuf->b_term;
5636 linenr_T line_count;
5637 linenr_T top_rows;
5638 linenr_T bot_rows;
5639 linenr_T bot_start;
5640 linenr_T lnum;
5641 char_u *p;
5642 sb_line_T *sb_line;
5643
5644 if (term == NULL
5645 || !term_is_finished(curbuf)
5646 || term->tl_top_diff_rows == 0
5647 || term->tl_scrollback.ga_len == 0)
5648 return FAIL;
5649
5650 line_count = curbuf->b_ml.ml_line_count;
5651 top_rows = term->tl_top_diff_rows;
5652 bot_rows = term->tl_bot_diff_rows;
5653 bot_start = line_count - bot_rows;
5654 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5655
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005656 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005657 for (lnum = 1; lnum <= top_rows; ++lnum)
5658 {
5659 p = vim_strsave(ml_get(1));
5660 if (p == NULL)
5661 return OK;
5662 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005663 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005664 vim_free(p);
5665 }
5666
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005667 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005668 for (lnum = 1; lnum <= bot_rows; ++lnum)
5669 {
5670 p = vim_strsave(ml_get(bot_start + lnum));
5671 if (p == NULL)
5672 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005673 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005674 ml_append(lnum - 1, p, 0, FALSE);
5675 vim_free(p);
5676 }
5677
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005678 // move top title to bottom
5679 p = vim_strsave(ml_get(bot_rows + 1));
5680 if (p == NULL)
5681 return OK;
5682 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005683 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005684 vim_free(p);
5685
5686 // move bottom title to top
5687 p = vim_strsave(ml_get(line_count - top_rows));
5688 if (p == NULL)
5689 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005690 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005691 ml_append(bot_rows, p, 0, FALSE);
5692 vim_free(p);
5693
Bram Moolenaard96ff162018-02-18 22:13:29 +01005694 if (top_rows == bot_rows)
5695 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005696 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005697 for (lnum = 0; lnum < top_rows; ++lnum)
5698 {
5699 sb_line_T temp;
5700
5701 temp = *(sb_line + lnum);
5702 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5703 *(sb_line + bot_start + lnum) = temp;
5704 }
5705 }
5706 else
5707 {
5708 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005709 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005710
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005711 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005712 if (temp != NULL)
5713 {
5714 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5715 mch_memmove(term->tl_scrollback.ga_data,
5716 temp + bot_start,
5717 sizeof(sb_line_T) * bot_rows);
5718 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5719 temp + top_rows,
5720 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5721 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5722 + line_count - top_rows,
5723 temp,
5724 sizeof(sb_line_T) * top_rows);
5725 vim_free(temp);
5726 }
5727 }
5728
5729 term->tl_top_diff_rows = bot_rows;
5730 term->tl_bot_diff_rows = top_rows;
5731
5732 update_screen(NOT_VALID);
5733 return OK;
5734}
5735
5736/*
5737 * "term_dumpdiff(filename, filename, options)" function
5738 */
5739 void
5740f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5741{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005742 if (in_vim9script()
5743 && (check_for_string_arg(argvars, 0) == FAIL
5744 || check_for_string_arg(argvars, 1) == FAIL
5745 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5746 return;
5747
Bram Moolenaard96ff162018-02-18 22:13:29 +01005748 term_load_dump(argvars, rettv, TRUE);
5749}
5750
5751/*
5752 * "term_dumpload(filename, options)" function
5753 */
5754 void
5755f_term_dumpload(typval_T *argvars, typval_T *rettv)
5756{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005757 if (in_vim9script()
5758 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005759 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005760 return;
5761
Bram Moolenaard96ff162018-02-18 22:13:29 +01005762 term_load_dump(argvars, rettv, FALSE);
5763}
5764
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005765/*
5766 * "term_getaltscreen(buf)" function
5767 */
5768 void
5769f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5770{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005771 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005772
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005773 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5774 return;
5775
5776 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005777 if (buf == NULL)
5778 return;
5779 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5780}
5781
5782/*
5783 * "term_getattr(attr, name)" function
5784 */
5785 void
5786f_term_getattr(typval_T *argvars, typval_T *rettv)
5787{
5788 int attr;
5789 size_t i;
5790 char_u *name;
5791
5792 static struct {
5793 char *name;
5794 int attr;
5795 } attrs[] = {
5796 {"bold", HL_BOLD},
5797 {"italic", HL_ITALIC},
5798 {"underline", HL_UNDERLINE},
5799 {"strike", HL_STRIKETHROUGH},
5800 {"reverse", HL_INVERSE},
5801 };
5802
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005803 if (in_vim9script()
5804 && (check_for_number_arg(argvars, 0) == FAIL
5805 || check_for_string_arg(argvars, 1) == FAIL))
5806 return;
5807
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005808 attr = tv_get_number(&argvars[0]);
5809 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005810 if (name == NULL)
5811 return;
5812
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005813 if (attr > HL_ALL)
5814 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02005815 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005816 if (STRCMP(name, attrs[i].name) == 0)
5817 {
5818 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5819 break;
5820 }
5821}
5822
5823/*
5824 * "term_getcursor(buf)" function
5825 */
5826 void
5827f_term_getcursor(typval_T *argvars, typval_T *rettv)
5828{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005829 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005830 term_T *term;
5831 list_T *l;
5832 dict_T *d;
5833
5834 if (rettv_list_alloc(rettv) == FAIL)
5835 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005836
5837 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5838 return;
5839
5840 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005841 if (buf == NULL)
5842 return;
5843 term = buf->b_term;
5844
5845 l = rettv->vval.v_list;
5846 list_append_number(l, term->tl_cursor_pos.row + 1);
5847 list_append_number(l, term->tl_cursor_pos.col + 1);
5848
5849 d = dict_alloc();
5850 if (d != NULL)
5851 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005852 dict_add_number(d, "visible", term->tl_cursor_visible);
5853 dict_add_number(d, "blink", blink_state_is_inverted()
5854 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5855 dict_add_number(d, "shape", term->tl_cursor_shape);
5856 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005857 list_append_dict(l, d);
5858 }
5859}
5860
5861/*
5862 * "term_getjob(buf)" function
5863 */
5864 void
5865f_term_getjob(typval_T *argvars, typval_T *rettv)
5866{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005867 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005868
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005869 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5870 return;
5871
5872 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005873 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005874 {
5875 rettv->v_type = VAR_SPECIAL;
5876 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005877 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005878 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005879
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005880 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005881 rettv->vval.v_job = buf->b_term->tl_job;
5882 if (rettv->vval.v_job != NULL)
5883 ++rettv->vval.v_job->jv_refcount;
5884}
5885
5886 static int
5887get_row_number(typval_T *tv, term_T *term)
5888{
5889 if (tv->v_type == VAR_STRING
5890 && tv->vval.v_string != NULL
5891 && STRCMP(tv->vval.v_string, ".") == 0)
5892 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005893 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005894}
5895
5896/*
5897 * "term_getline(buf, row)" function
5898 */
5899 void
5900f_term_getline(typval_T *argvars, typval_T *rettv)
5901{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005902 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005903 term_T *term;
5904 int row;
5905
5906 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005907
5908 if (in_vim9script()
5909 && (check_for_buffer_arg(argvars, 0) == FAIL
5910 || check_for_lnum_arg(argvars, 1) == FAIL))
5911 return;
5912
5913 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005914 if (buf == NULL)
5915 return;
5916 term = buf->b_term;
5917 row = get_row_number(&argvars[1], term);
5918
5919 if (term->tl_vterm == NULL)
5920 {
5921 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5922
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005923 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005924 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5925 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5926 }
5927 else
5928 {
5929 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5930 VTermRect rect;
5931 int len;
5932 char_u *p;
5933
5934 if (row < 0 || row >= term->tl_rows)
5935 return;
5936 len = term->tl_cols * MB_MAXBYTES + 1;
5937 p = alloc(len);
5938 if (p == NULL)
5939 return;
5940 rettv->vval.v_string = p;
5941
5942 rect.start_col = 0;
5943 rect.end_col = term->tl_cols;
5944 rect.start_row = row;
5945 rect.end_row = row + 1;
5946 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5947 }
5948}
5949
5950/*
5951 * "term_getscrolled(buf)" function
5952 */
5953 void
5954f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5955{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005956 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005957
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005958 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5959 return;
5960
5961 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005962 if (buf == NULL)
5963 return;
5964 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5965}
5966
5967/*
5968 * "term_getsize(buf)" function
5969 */
5970 void
5971f_term_getsize(typval_T *argvars, typval_T *rettv)
5972{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005973 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005974 list_T *l;
5975
5976 if (rettv_list_alloc(rettv) == FAIL)
5977 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005978
5979 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5980 return;
5981
5982 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005983 if (buf == NULL)
5984 return;
5985
5986 l = rettv->vval.v_list;
5987 list_append_number(l, buf->b_term->tl_rows);
5988 list_append_number(l, buf->b_term->tl_cols);
5989}
5990
5991/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005992 * "term_setsize(buf, rows, cols)" function
5993 */
5994 void
5995f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5996{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005997 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02005998 term_T *term;
5999 varnumber_T rows, cols;
6000
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006001 if (in_vim9script()
6002 && (check_for_buffer_arg(argvars, 0) == FAIL
6003 || check_for_number_arg(argvars, 1) == FAIL
6004 || check_for_number_arg(argvars, 2) == FAIL))
6005 return;
6006
6007 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02006008 if (buf == NULL)
6009 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006010 emsg(_(e_not_terminal_buffer));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02006011 return;
6012 }
6013 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02006014 return;
6015 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006016 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02006017 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006018 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02006019 cols = cols <= 0 ? term->tl_cols : cols;
6020 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006021 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02006022
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006023 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02006024 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
6025 term_report_winsize(term, term->tl_rows, term->tl_cols);
6026}
6027
6028/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006029 * "term_getstatus(buf)" function
6030 */
6031 void
6032f_term_getstatus(typval_T *argvars, typval_T *rettv)
6033{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006034 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006035 term_T *term;
6036 char_u val[100];
6037
6038 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006039
6040 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6041 return;
6042
6043 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006044 if (buf == NULL)
6045 return;
6046 term = buf->b_term;
6047
6048 if (term_job_running(term))
6049 STRCPY(val, "running");
6050 else
6051 STRCPY(val, "finished");
6052 if (term->tl_normal_mode)
6053 STRCAT(val, ",normal");
6054 rettv->vval.v_string = vim_strsave(val);
6055}
6056
6057/*
6058 * "term_gettitle(buf)" function
6059 */
6060 void
6061f_term_gettitle(typval_T *argvars, typval_T *rettv)
6062{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006063 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006064
6065 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006066
6067 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6068 return;
6069
6070 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006071 if (buf == NULL)
6072 return;
6073
6074 if (buf->b_term->tl_title != NULL)
6075 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
6076}
6077
6078/*
6079 * "term_gettty(buf)" function
6080 */
6081 void
6082f_term_gettty(typval_T *argvars, typval_T *rettv)
6083{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006084 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02006085 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006086 int num = 0;
6087
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006088 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006089 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006090 || check_for_opt_bool_arg(argvars, 1) == FAIL))
6091 return;
6092
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006093 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006094 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006095 if (buf == NULL)
6096 return;
6097 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02006098 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006099
6100 switch (num)
6101 {
6102 case 0:
6103 if (buf->b_term->tl_job != NULL)
6104 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006105 break;
6106 case 1:
6107 if (buf->b_term->tl_job != NULL)
6108 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006109 break;
6110 default:
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006111 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006112 return;
6113 }
6114 if (p != NULL)
6115 rettv->vval.v_string = vim_strsave(p);
6116}
6117
6118/*
6119 * "term_list()" function
6120 */
6121 void
6122f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
6123{
6124 term_T *tp;
6125 list_T *l;
6126
6127 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
6128 return;
6129
6130 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006131 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02006132 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006133 if (list_append_number(l,
6134 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6135 return;
6136}
6137
6138/*
6139 * "term_scrape(buf, row)" function
6140 */
6141 void
6142f_term_scrape(typval_T *argvars, typval_T *rettv)
6143{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006144 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006145 VTermScreen *screen = NULL;
6146 VTermPos pos;
6147 list_T *l;
6148 term_T *term;
6149 char_u *p;
6150 sb_line_T *line;
6151
6152 if (rettv_list_alloc(rettv) == FAIL)
6153 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006154
6155 if (in_vim9script()
6156 && (check_for_buffer_arg(argvars, 0) == FAIL
6157 || check_for_lnum_arg(argvars, 1) == FAIL))
6158 return;
6159
6160 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006161 if (buf == NULL)
6162 return;
6163 term = buf->b_term;
6164
6165 l = rettv->vval.v_list;
6166 pos.row = get_row_number(&argvars[1], term);
6167
6168 if (term->tl_vterm != NULL)
6169 {
6170 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006171 if (screen == NULL) // can't really happen
6172 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006173 p = NULL;
6174 line = NULL;
6175 }
6176 else
6177 {
6178 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6179
6180 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6181 return;
6182 p = ml_get_buf(buf, lnum + 1, FALSE);
6183 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6184 }
6185
6186 for (pos.col = 0; pos.col < term->tl_cols; )
6187 {
6188 dict_T *dcell;
6189 int width;
6190 VTermScreenCellAttrs attrs;
6191 VTermColor fg, bg;
6192 char_u rgb[8];
6193 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6194 int off = 0;
6195 int i;
6196
6197 if (screen == NULL)
6198 {
6199 cellattr_T *cellattr;
6200 int len;
6201
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006202 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006203 if (pos.col >= line->sb_cols)
6204 break;
6205 cellattr = line->sb_cells + pos.col;
6206 width = cellattr->width;
6207 attrs = cellattr->attrs;
6208 fg = cellattr->fg;
6209 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006210 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006211 mch_memmove(mbs, p, len);
6212 mbs[len] = NUL;
6213 p += len;
6214 }
6215 else
6216 {
6217 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006218
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006219 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6220 break;
6221 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6222 {
6223 if (cell.chars[i] == 0)
6224 break;
6225 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6226 }
6227 mbs[off] = NUL;
6228 width = cell.width;
6229 attrs = cell.attrs;
6230 fg = cell.fg;
6231 bg = cell.bg;
6232 }
6233 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006234 if (dcell == NULL)
6235 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006236 list_append_dict(l, dcell);
6237
Bram Moolenaare0be1672018-07-08 16:50:37 +02006238 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006239
6240 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6241 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006242 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006243 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6244 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006245 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006246
Bram Moolenaar87fd0922021-11-20 13:47:45 +00006247 dict_add_number(dcell, "attr",
6248 cell2attr(term, NULL, &attrs, &fg, &bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006249 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006250
6251 ++pos.col;
6252 if (width == 2)
6253 ++pos.col;
6254 }
6255}
6256
6257/*
6258 * "term_sendkeys(buf, keys)" function
6259 */
6260 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006261f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006262{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006263 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006264 char_u *msg;
6265 term_T *term;
6266
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006267 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006268 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006269 || check_for_string_arg(argvars, 1) == FAIL))
6270 return;
6271
6272 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006273 if (buf == NULL)
6274 return;
6275
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006276 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006277 if (msg == NULL)
6278 return;
6279 term = buf->b_term;
6280 if (term->tl_vterm == NULL)
6281 return;
6282
6283 while (*msg != NUL)
6284 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006285 int c;
6286
6287 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6288 {
6289 c = TO_SPECIAL(msg[1], msg[2]);
6290 msg += 3;
6291 }
6292 else
6293 {
6294 c = PTR2CHAR(msg);
6295 msg += MB_CPTR2LEN(msg);
6296 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006297 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006298 }
6299}
6300
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006301#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6302/*
6303 * "term_getansicolors(buf)" function
6304 */
6305 void
6306f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6307{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006308 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006309 term_T *term;
6310 VTermState *state;
6311 VTermColor color;
6312 char_u hexbuf[10];
6313 int index;
6314 list_T *list;
6315
6316 if (rettv_list_alloc(rettv) == FAIL)
6317 return;
6318
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006319 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6320 return;
6321
6322 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006323 if (buf == NULL)
6324 return;
6325 term = buf->b_term;
6326 if (term->tl_vterm == NULL)
6327 return;
6328
6329 list = rettv->vval.v_list;
6330 state = vterm_obtain_state(term->tl_vterm);
6331 for (index = 0; index < 16; index++)
6332 {
6333 vterm_state_get_palette_color(state, index, &color);
6334 sprintf((char *)hexbuf, "#%02x%02x%02x",
6335 color.red, color.green, color.blue);
6336 if (list_append_string(list, hexbuf, 7) == FAIL)
6337 return;
6338 }
6339}
6340
6341/*
6342 * "term_setansicolors(buf, list)" function
6343 */
6344 void
6345f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6346{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006347 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006348 term_T *term;
6349
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006350 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006351 && (check_for_buffer_arg(argvars, 0) == FAIL
6352 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006353 return;
6354
6355 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006356 if (buf == NULL)
6357 return;
6358 term = buf->b_term;
6359 if (term->tl_vterm == NULL)
6360 return;
6361
6362 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6363 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006364 emsg(_(e_list_required));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006365 return;
6366 }
6367
6368 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006369 emsg(_(e_invalid_argument));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006370}
6371#endif
6372
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006373/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006374 * "term_setapi(buf, api)" function
6375 */
6376 void
6377f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6378{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006379 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006380 term_T *term;
6381 char_u *api;
6382
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006383 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006384 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006385 || check_for_string_arg(argvars, 1) == FAIL))
6386 return;
6387
6388 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006389 if (buf == NULL)
6390 return;
6391 term = buf->b_term;
6392 vim_free(term->tl_api);
6393 api = tv_get_string_chk(&argvars[1]);
6394 if (api != NULL)
6395 term->tl_api = vim_strsave(api);
6396 else
6397 term->tl_api = NULL;
6398}
6399
6400/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006401 * "term_setrestore(buf, command)" function
6402 */
6403 void
6404f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6405{
6406#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006407 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006408 term_T *term;
6409 char_u *cmd;
6410
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006411 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006412 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006413 || check_for_string_arg(argvars, 1) == FAIL))
6414 return;
6415
6416 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006417 if (buf == NULL)
6418 return;
6419 term = buf->b_term;
6420 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006421 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006422 if (cmd != NULL)
6423 term->tl_command = vim_strsave(cmd);
6424 else
6425 term->tl_command = NULL;
6426#endif
6427}
6428
6429/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006430 * "term_setkill(buf, how)" function
6431 */
6432 void
6433f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6434{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006435 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006436 term_T *term;
6437 char_u *how;
6438
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006439 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006440 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006441 || check_for_string_arg(argvars, 1) == FAIL))
6442 return;
6443
6444 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006445 if (buf == NULL)
6446 return;
6447 term = buf->b_term;
6448 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006449 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006450 if (how != NULL)
6451 term->tl_kill = vim_strsave(how);
6452 else
6453 term->tl_kill = NULL;
6454}
6455
6456/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006457 * "term_start(command, options)" function
6458 */
6459 void
6460f_term_start(typval_T *argvars, typval_T *rettv)
6461{
6462 jobopt_T opt;
6463 buf_T *buf;
6464
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006465 if (in_vim9script()
6466 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6467 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6468 return;
6469
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006470 init_job_options(&opt);
6471 if (argvars[1].v_type != VAR_UNKNOWN
6472 && get_job_options(&argvars[1], &opt,
6473 JO_TIMEOUT_ALL + JO_STOPONEXIT
6474 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6475 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6476 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6477 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006478 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006479 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006480 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006481 return;
6482
Bram Moolenaar13568252018-03-16 20:46:58 +01006483 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006484
6485 if (buf != NULL && buf->b_term != NULL)
6486 rettv->vval.v_number = buf->b_fnum;
6487}
6488
6489/*
6490 * "term_wait" function
6491 */
6492 void
6493f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6494{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006495 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006496
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006497 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006498 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006499 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006500 return;
6501
6502 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006503 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006504 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006505 if (buf->b_term->tl_job == NULL)
6506 {
6507 ch_log(NULL, "term_wait(): no job to wait for");
6508 return;
6509 }
6510 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006511 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006512 return;
6513
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006514 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006515 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006516 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6517 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006518 // The job is dead, keep reading channel I/O until the channel is
6519 // closed. buf->b_term may become NULL if the terminal was closed while
6520 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006521 ch_log(NULL, "term_wait(): waiting for channel to close");
6522 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6523 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006524 term_flush_messages();
6525
Bram Moolenaard45aa552018-05-21 22:50:29 +02006526 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006527 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006528 // If the terminal is closed when the channel is closed the
6529 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006530 break;
Bram Moolenaareea32af2021-11-21 14:51:13 +00006531 if (buf->b_term == NULL || buf->b_term->tl_channel_closing)
6532 // came here from a close callback, only wait one time
6533 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006534 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006535
6536 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006537 }
6538 else
6539 {
6540 long wait = 10L;
6541
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006542 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006543
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006544 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006545 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006546 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006547 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006548
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006549 // Flushing messages on channels is hopefully sufficient.
6550 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006551 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006552 }
6553}
6554
6555/*
6556 * Called when a channel has sent all the lines to a terminal.
6557 * Send a CTRL-D to mark the end of the text.
6558 */
6559 void
6560term_send_eof(channel_T *ch)
6561{
6562 term_T *term;
6563
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006564 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006565 if (term->tl_job == ch->ch_job)
6566 {
6567 if (term->tl_eof_chars != NULL)
6568 {
6569 channel_send(ch, PART_IN, term->tl_eof_chars,
6570 (int)STRLEN(term->tl_eof_chars), NULL);
6571 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6572 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006573# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006574 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006575 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006576 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6577# endif
6578 }
6579}
6580
Bram Moolenaar113e1072019-01-20 15:30:40 +01006581#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006582 job_T *
6583term_getjob(term_T *term)
6584{
6585 return term != NULL ? term->tl_job : NULL;
6586}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006587#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006588
Bram Moolenaar4f974752019-02-17 17:44:42 +01006589# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006590
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006591///////////////////////////////////////
6592// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006593#ifdef PROTO
6594typedef int COORD;
6595typedef int DWORD;
6596typedef int HANDLE;
6597typedef int *DWORD_PTR;
6598typedef int HPCON;
6599typedef int HRESULT;
6600typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006601typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006602typedef int PSIZE_T;
6603typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006604typedef int BOOL;
6605# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006606#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006607
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006608HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6609HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6610HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006611BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6612BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6613void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006614
6615 static int
6616dyn_conpty_init(int verbose)
6617{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006618 static HMODULE hKerneldll = NULL;
6619 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006620 static struct
6621 {
6622 char *name;
6623 FARPROC *ptr;
6624 } conpty_entry[] =
6625 {
6626 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6627 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6628 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6629 {"InitializeProcThreadAttributeList",
6630 (FARPROC*)&pInitializeProcThreadAttributeList},
6631 {"UpdateProcThreadAttribute",
6632 (FARPROC*)&pUpdateProcThreadAttribute},
6633 {"DeleteProcThreadAttributeList",
6634 (FARPROC*)&pDeleteProcThreadAttributeList},
6635 {NULL, NULL}
6636 };
6637
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006638 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006639 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006640 if (verbose)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006641 emsg(_(e_conpty_is_not_available));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006642 return FAIL;
6643 }
6644
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006645 // No need to initialize twice.
6646 if (hKerneldll)
6647 return OK;
6648
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006649 hKerneldll = vimLoadLib("kernel32.dll");
6650 for (i = 0; conpty_entry[i].name != NULL
6651 && conpty_entry[i].ptr != NULL; ++i)
6652 {
6653 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6654 conpty_entry[i].name)) == NULL)
6655 {
6656 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006657 semsg(_(e_could_not_load_library_function_str), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006658 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006659 return FAIL;
6660 }
6661 }
6662
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006663 return OK;
6664}
6665
6666 static int
6667conpty_term_and_job_init(
6668 term_T *term,
6669 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006670 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006671 jobopt_T *opt,
6672 jobopt_T *orig_opt)
6673{
6674 WCHAR *cmd_wchar = NULL;
6675 WCHAR *cmd_wchar_copy = NULL;
6676 WCHAR *cwd_wchar = NULL;
6677 WCHAR *env_wchar = NULL;
6678 channel_T *channel = NULL;
6679 job_T *job = NULL;
6680 HANDLE jo = NULL;
6681 garray_T ga_cmd, ga_env;
6682 char_u *cmd = NULL;
6683 HRESULT hr;
6684 COORD consize;
6685 SIZE_T breq;
6686 PROCESS_INFORMATION proc_info;
6687 HANDLE i_theirs = NULL;
6688 HANDLE o_theirs = NULL;
6689 HANDLE i_ours = NULL;
6690 HANDLE o_ours = NULL;
6691
Bram Moolenaar04935fb2022-01-08 16:19:22 +00006692 ga_init2(&ga_cmd, sizeof(char*), 20);
6693 ga_init2(&ga_env, sizeof(char*), 20);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006694
6695 if (argvar->v_type == VAR_STRING)
6696 {
6697 cmd = argvar->vval.v_string;
6698 }
6699 else if (argvar->v_type == VAR_LIST)
6700 {
6701 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6702 goto failed;
6703 cmd = ga_cmd.ga_data;
6704 }
6705 if (cmd == NULL || *cmd == NUL)
6706 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006707 emsg(_(e_invalid_argument));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006708 goto failed;
6709 }
6710
6711 term->tl_arg0_cmd = vim_strsave(cmd);
6712
6713 cmd_wchar = enc_to_utf16(cmd, NULL);
6714
6715 if (cmd_wchar != NULL)
6716 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006717 // Request by CreateProcessW
6718 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006719 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006720 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6721 }
6722
6723 ga_clear(&ga_cmd);
6724 if (cmd_wchar == NULL)
6725 goto failed;
6726 if (opt->jo_cwd != NULL)
6727 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6728
6729 win32_build_env(opt->jo_env, &ga_env, TRUE);
6730 env_wchar = ga_env.ga_data;
6731
6732 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6733 goto failed;
6734 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6735 goto failed;
6736
6737 consize.X = term->tl_cols;
6738 consize.Y = term->tl_rows;
6739 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6740 &term->tl_conpty);
6741 if (FAILED(hr))
6742 goto failed;
6743
6744 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6745
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006746 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006747 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006748 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006749 if (!term->tl_siex.lpAttributeList)
6750 goto failed;
6751 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6752 0, &breq))
6753 goto failed;
6754 if (!pUpdateProcThreadAttribute(
6755 term->tl_siex.lpAttributeList, 0,
6756 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6757 sizeof(HPCON), NULL, NULL))
6758 goto failed;
6759
6760 channel = add_channel();
6761 if (channel == NULL)
6762 goto failed;
6763
6764 job = job_alloc();
6765 if (job == NULL)
6766 goto failed;
6767 if (argvar->v_type == VAR_STRING)
6768 {
6769 int argc;
6770
6771 build_argv_from_string(cmd, &job->jv_argv, &argc);
6772 }
6773 else
6774 {
6775 int argc;
6776
6777 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6778 }
6779
6780 if (opt->jo_set & JO_IN_BUF)
6781 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6782
6783 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6784 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006785 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006786 env_wchar, cwd_wchar,
6787 &term->tl_siex.StartupInfo, &proc_info))
6788 goto failed;
6789
6790 CloseHandle(i_theirs);
6791 CloseHandle(o_theirs);
6792
6793 channel_set_pipes(channel,
6794 (sock_T)i_ours,
6795 (sock_T)o_ours,
6796 (sock_T)o_ours);
6797
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006798 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006799 channel->ch_write_text_mode = TRUE;
6800
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006801 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006802 channel->ch_anonymous_pipe = TRUE;
6803
6804 jo = CreateJobObject(NULL, NULL);
6805 if (jo == NULL)
6806 goto failed;
6807
6808 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6809 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006810 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006811 CloseHandle(jo);
6812 jo = NULL;
6813 }
6814
6815 ResumeThread(proc_info.hThread);
6816 CloseHandle(proc_info.hThread);
6817
6818 vim_free(cmd_wchar);
6819 vim_free(cmd_wchar_copy);
6820 vim_free(cwd_wchar);
6821 vim_free(env_wchar);
6822
6823 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6824 goto failed;
6825
6826#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6827 if (opt->jo_set2 & JO2_ANSI_COLORS)
6828 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6829 else
6830 init_vterm_ansi_colors(term->tl_vterm);
6831#endif
6832
6833 channel_set_job(channel, job, opt);
6834 job_set_options(job, opt);
6835
6836 job->jv_channel = channel;
6837 job->jv_proc_info = proc_info;
6838 job->jv_job_object = jo;
6839 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006840 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006841 ++job->jv_refcount;
6842 term->tl_job = job;
6843
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006844 // Redirecting stdout and stderr doesn't work at the job level. Instead
6845 // open the file here and handle it in. opt->jo_io was changed in
6846 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006847 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6848 {
6849 char_u *fname = opt->jo_io_name[PART_OUT];
6850
6851 ch_log(channel, "Opening output file %s", fname);
6852 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6853 if (term->tl_out_fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006854 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006855 }
6856
6857 return OK;
6858
6859failed:
6860 ga_clear(&ga_cmd);
6861 ga_clear(&ga_env);
6862 vim_free(cmd_wchar);
6863 vim_free(cmd_wchar_copy);
6864 vim_free(cwd_wchar);
6865 if (channel != NULL)
6866 channel_clear(channel);
6867 if (job != NULL)
6868 {
6869 job->jv_channel = NULL;
6870 job_cleanup(job);
6871 }
6872 term->tl_job = NULL;
6873 if (jo != NULL)
6874 CloseHandle(jo);
6875
6876 if (term->tl_siex.lpAttributeList != NULL)
6877 {
6878 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6879 vim_free(term->tl_siex.lpAttributeList);
6880 }
6881 term->tl_siex.lpAttributeList = NULL;
6882 if (o_theirs != NULL)
6883 CloseHandle(o_theirs);
6884 if (o_ours != NULL)
6885 CloseHandle(o_ours);
6886 if (i_ours != NULL)
6887 CloseHandle(i_ours);
6888 if (i_theirs != NULL)
6889 CloseHandle(i_theirs);
6890 if (term->tl_conpty != NULL)
6891 pClosePseudoConsole(term->tl_conpty);
6892 term->tl_conpty = NULL;
6893 return FAIL;
6894}
6895
6896 static void
6897conpty_term_report_winsize(term_T *term, int rows, int cols)
6898{
6899 COORD consize;
6900
6901 consize.X = cols;
6902 consize.Y = rows;
6903 pResizePseudoConsole(term->tl_conpty, consize);
6904}
6905
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006906 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006907term_free_conpty(term_T *term)
6908{
6909 if (term->tl_siex.lpAttributeList != NULL)
6910 {
6911 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6912 vim_free(term->tl_siex.lpAttributeList);
6913 }
6914 term->tl_siex.lpAttributeList = NULL;
6915 if (term->tl_conpty != NULL)
6916 pClosePseudoConsole(term->tl_conpty);
6917 term->tl_conpty = NULL;
6918}
6919
6920 int
6921use_conpty(void)
6922{
6923 return has_conpty;
6924}
6925
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006926# ifndef PROTO
6927
6928#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6929#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006930#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006931
6932void* (*winpty_config_new)(UINT64, void*);
6933void* (*winpty_open)(void*, void*);
6934void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6935BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6936void (*winpty_config_set_mouse_mode)(void*, int);
6937void (*winpty_config_set_initial_size)(void*, int, int);
6938LPCWSTR (*winpty_conin_name)(void*);
6939LPCWSTR (*winpty_conout_name)(void*);
6940LPCWSTR (*winpty_conerr_name)(void*);
6941void (*winpty_free)(void*);
6942void (*winpty_config_free)(void*);
6943void (*winpty_spawn_config_free)(void*);
6944void (*winpty_error_free)(void*);
6945LPCWSTR (*winpty_error_msg)(void*);
6946BOOL (*winpty_set_size)(void*, int, int, void*);
6947HANDLE (*winpty_agent_process)(void*);
6948
6949#define WINPTY_DLL "winpty.dll"
6950
6951static HINSTANCE hWinPtyDLL = NULL;
6952# endif
6953
6954 static int
6955dyn_winpty_init(int verbose)
6956{
6957 int i;
6958 static struct
6959 {
6960 char *name;
6961 FARPROC *ptr;
6962 } winpty_entry[] =
6963 {
6964 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6965 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6966 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6967 {"winpty_config_set_mouse_mode",
6968 (FARPROC*)&winpty_config_set_mouse_mode},
6969 {"winpty_config_set_initial_size",
6970 (FARPROC*)&winpty_config_set_initial_size},
6971 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6972 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6973 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6974 {"winpty_free", (FARPROC*)&winpty_free},
6975 {"winpty_open", (FARPROC*)&winpty_open},
6976 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6977 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6978 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6979 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6980 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6981 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6982 {NULL, NULL}
6983 };
6984
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006985 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006986 if (hWinPtyDLL)
6987 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006988 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6989 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006990 if (*p_winptydll != NUL)
6991 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6992 if (!hWinPtyDLL)
6993 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6994 if (!hWinPtyDLL)
6995 {
6996 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006997 semsg(_(e_could_not_load_library_str_str),
Martin Tournoij1a3e5742021-07-24 13:57:29 +02006998 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
6999 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007000 return FAIL;
7001 }
7002 for (i = 0; winpty_entry[i].name != NULL
7003 && winpty_entry[i].ptr != NULL; ++i)
7004 {
7005 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
7006 winpty_entry[i].name)) == NULL)
7007 {
7008 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007009 semsg(_(e_could_not_load_library_function_str), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01007010 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007011 return FAIL;
7012 }
7013 }
7014
7015 return OK;
7016}
7017
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007018 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007019winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007020 term_T *term,
7021 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007022 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007023 jobopt_T *opt,
7024 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007025{
7026 WCHAR *cmd_wchar = NULL;
7027 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007028 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007029 channel_T *channel = NULL;
7030 job_T *job = NULL;
7031 DWORD error;
7032 HANDLE jo = NULL;
7033 HANDLE child_process_handle;
7034 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01007035 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007036 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007037 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007038 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007039
Bram Moolenaar04935fb2022-01-08 16:19:22 +00007040 ga_init2(&ga_cmd, sizeof(char*), 20);
7041 ga_init2(&ga_env, sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007042
7043 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007044 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007045 cmd = argvar->vval.v_string;
7046 }
7047 else if (argvar->v_type == VAR_LIST)
7048 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007049 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007050 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007051 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007052 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007053 if (cmd == NULL || *cmd == NUL)
7054 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007055 emsg(_(e_invalid_argument));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007056 goto failed;
7057 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007058
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007059 term->tl_arg0_cmd = vim_strsave(cmd);
7060
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007061 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007062 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007063 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007064 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007065 if (opt->jo_cwd != NULL)
7066 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007067
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007068 win32_build_env(opt->jo_env, &ga_env, TRUE);
7069 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007070
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007071 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
7072 if (term->tl_winpty_config == NULL)
7073 goto failed;
7074
7075 winpty_config_set_mouse_mode(term->tl_winpty_config,
7076 WINPTY_MOUSE_MODE_FORCE);
7077 winpty_config_set_initial_size(term->tl_winpty_config,
7078 term->tl_cols, term->tl_rows);
7079 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
7080 if (term->tl_winpty == NULL)
7081 goto failed;
7082
7083 spawn_config = winpty_spawn_config_new(
7084 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
7085 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
7086 NULL,
7087 cmd_wchar,
7088 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007089 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007090 &winpty_err);
7091 if (spawn_config == NULL)
7092 goto failed;
7093
7094 channel = add_channel();
7095 if (channel == NULL)
7096 goto failed;
7097
7098 job = job_alloc();
7099 if (job == NULL)
7100 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02007101 if (argvar->v_type == VAR_STRING)
7102 {
7103 int argc;
7104
7105 build_argv_from_string(cmd, &job->jv_argv, &argc);
7106 }
7107 else
7108 {
7109 int argc;
7110
7111 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
7112 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007113
7114 if (opt->jo_set & JO_IN_BUF)
7115 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
7116
7117 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
7118 &child_thread_handle, &error, &winpty_err))
7119 goto failed;
7120
7121 channel_set_pipes(channel,
7122 (sock_T)CreateFileW(
7123 winpty_conin_name(term->tl_winpty),
7124 GENERIC_WRITE, 0, NULL,
7125 OPEN_EXISTING, 0, NULL),
7126 (sock_T)CreateFileW(
7127 winpty_conout_name(term->tl_winpty),
7128 GENERIC_READ, 0, NULL,
7129 OPEN_EXISTING, 0, NULL),
7130 (sock_T)CreateFileW(
7131 winpty_conerr_name(term->tl_winpty),
7132 GENERIC_READ, 0, NULL,
7133 OPEN_EXISTING, 0, NULL));
7134
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007135 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007136 channel->ch_write_text_mode = TRUE;
7137
7138 jo = CreateJobObject(NULL, NULL);
7139 if (jo == NULL)
7140 goto failed;
7141
7142 if (!AssignProcessToJobObject(jo, child_process_handle))
7143 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007144 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007145 CloseHandle(jo);
7146 jo = NULL;
7147 }
7148
7149 winpty_spawn_config_free(spawn_config);
7150 vim_free(cmd_wchar);
7151 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007152 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007153
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007154 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7155 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007156
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007157#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7158 if (opt->jo_set2 & JO2_ANSI_COLORS)
7159 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7160 else
7161 init_vterm_ansi_colors(term->tl_vterm);
7162#endif
7163
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007164 channel_set_job(channel, job, opt);
7165 job_set_options(job, opt);
7166
7167 job->jv_channel = channel;
7168 job->jv_proc_info.hProcess = child_process_handle;
7169 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7170 job->jv_job_object = jo;
7171 job->jv_status = JOB_STARTED;
7172 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007173 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007174 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007175 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007176 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007177 ++job->jv_refcount;
7178 term->tl_job = job;
7179
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007180 // Redirecting stdout and stderr doesn't work at the job level. Instead
7181 // open the file here and handle it in. opt->jo_io was changed in
7182 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007183 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7184 {
7185 char_u *fname = opt->jo_io_name[PART_OUT];
7186
7187 ch_log(channel, "Opening output file %s", fname);
7188 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7189 if (term->tl_out_fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007190 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007191 }
7192
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007193 return OK;
7194
7195failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007196 ga_clear(&ga_cmd);
7197 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007198 vim_free(cmd_wchar);
7199 vim_free(cwd_wchar);
7200 if (spawn_config != NULL)
7201 winpty_spawn_config_free(spawn_config);
7202 if (channel != NULL)
7203 channel_clear(channel);
7204 if (job != NULL)
7205 {
7206 job->jv_channel = NULL;
7207 job_cleanup(job);
7208 }
7209 term->tl_job = NULL;
7210 if (jo != NULL)
7211 CloseHandle(jo);
7212 if (term->tl_winpty != NULL)
7213 winpty_free(term->tl_winpty);
7214 term->tl_winpty = NULL;
7215 if (term->tl_winpty_config != NULL)
7216 winpty_config_free(term->tl_winpty_config);
7217 term->tl_winpty_config = NULL;
7218 if (winpty_err != NULL)
7219 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007220 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007221 (short_u *)winpty_error_msg(winpty_err), NULL);
7222
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007223 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007224 winpty_error_free(winpty_err);
7225 }
7226 return FAIL;
7227}
7228
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007229/*
7230 * Create a new terminal of "rows" by "cols" cells.
7231 * Store a reference in "term".
7232 * Return OK or FAIL.
7233 */
7234 static int
7235term_and_job_init(
7236 term_T *term,
7237 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007238 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007239 jobopt_T *opt,
7240 jobopt_T *orig_opt)
7241{
7242 int use_winpty = FALSE;
7243 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007244 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007245
7246 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7247 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7248
7249 if (!has_winpty && !has_conpty)
7250 // If neither is available give the errors for winpty, since when
7251 // conpty is not available it can't be installed either.
7252 return dyn_winpty_init(TRUE);
7253
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007254 if (opt->jo_tty_type != NUL)
7255 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007256
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007257 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007258 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007259 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007260 use_conpty = TRUE;
7261 else if (has_winpty)
7262 use_winpty = TRUE;
7263 // else: error
7264 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007265 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007266 {
7267 if (has_winpty)
7268 use_winpty = TRUE;
7269 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007270 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007271 {
7272 if (has_conpty)
7273 use_conpty = TRUE;
7274 else
7275 return dyn_conpty_init(TRUE);
7276 }
7277
7278 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007279 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007280
7281 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007282 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007283
7284 // error
7285 return dyn_winpty_init(TRUE);
7286}
7287
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007288 static int
7289create_pty_only(term_T *term, jobopt_T *options)
7290{
7291 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7292 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7293 char in_name[80], out_name[80];
7294 channel_T *channel = NULL;
7295
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007296 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7297 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007298
7299 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7300 GetCurrentProcessId(),
7301 curbuf->b_fnum);
7302 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7303 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7304 PIPE_UNLIMITED_INSTANCES,
7305 0, 0, NMPWAIT_NOWAIT, NULL);
7306 if (hPipeIn == INVALID_HANDLE_VALUE)
7307 goto failed;
7308
7309 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7310 GetCurrentProcessId(),
7311 curbuf->b_fnum);
7312 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7313 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7314 PIPE_UNLIMITED_INSTANCES,
7315 0, 0, 0, NULL);
7316 if (hPipeOut == INVALID_HANDLE_VALUE)
7317 goto failed;
7318
7319 ConnectNamedPipe(hPipeIn, NULL);
7320 ConnectNamedPipe(hPipeOut, NULL);
7321
7322 term->tl_job = job_alloc();
7323 if (term->tl_job == NULL)
7324 goto failed;
7325 ++term->tl_job->jv_refcount;
7326
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007327 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007328 term->tl_job->jv_status = JOB_FINISHED;
7329
7330 channel = add_channel();
7331 if (channel == NULL)
7332 goto failed;
7333 term->tl_job->jv_channel = channel;
7334 channel->ch_keep_open = TRUE;
7335 channel->ch_named_pipe = TRUE;
7336
7337 channel_set_pipes(channel,
7338 (sock_T)hPipeIn,
7339 (sock_T)hPipeOut,
7340 (sock_T)hPipeOut);
7341 channel_set_job(channel, term->tl_job, options);
7342 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7343 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7344
7345 return OK;
7346
7347failed:
7348 if (hPipeIn != NULL)
7349 CloseHandle(hPipeIn);
7350 if (hPipeOut != NULL)
7351 CloseHandle(hPipeOut);
7352 return FAIL;
7353}
7354
7355/*
7356 * Free the terminal emulator part of "term".
7357 */
7358 static void
7359term_free_vterm(term_T *term)
7360{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007361 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007362 if (term->tl_winpty != NULL)
7363 winpty_free(term->tl_winpty);
7364 term->tl_winpty = NULL;
7365 if (term->tl_winpty_config != NULL)
7366 winpty_config_free(term->tl_winpty_config);
7367 term->tl_winpty_config = NULL;
7368 if (term->tl_vterm != NULL)
7369 vterm_free(term->tl_vterm);
7370 term->tl_vterm = NULL;
7371}
7372
7373/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007374 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007375 */
7376 static void
7377term_report_winsize(term_T *term, int rows, int cols)
7378{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007379 if (term->tl_conpty)
7380 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007381 if (term->tl_winpty)
7382 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7383}
7384
7385 int
7386terminal_enabled(void)
7387{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007388 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007389}
7390
7391# else
7392
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007393///////////////////////////////////////
7394// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007395
7396/*
7397 * Create a new terminal of "rows" by "cols" cells.
7398 * Start job for "cmd".
7399 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007400 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007401 * Return OK or FAIL.
7402 */
7403 static int
7404term_and_job_init(
7405 term_T *term,
7406 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007407 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007408 jobopt_T *opt,
7409 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007410{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007411 term->tl_arg0_cmd = NULL;
7412
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007413 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7414 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007415
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007416#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7417 if (opt->jo_set2 & JO2_ANSI_COLORS)
7418 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7419 else
7420 init_vterm_ansi_colors(term->tl_vterm);
7421#endif
7422
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007423 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007424 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007425 if (term->tl_job != NULL)
7426 ++term->tl_job->jv_refcount;
7427
7428 return term->tl_job != NULL
7429 && term->tl_job->jv_channel != NULL
7430 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7431}
7432
7433 static int
7434create_pty_only(term_T *term, jobopt_T *opt)
7435{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007436 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7437 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007438
7439 term->tl_job = job_alloc();
7440 if (term->tl_job == NULL)
7441 return FAIL;
7442 ++term->tl_job->jv_refcount;
7443
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007444 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007445 term->tl_job->jv_status = JOB_FINISHED;
7446
7447 return mch_create_pty_channel(term->tl_job, opt);
7448}
7449
7450/*
7451 * Free the terminal emulator part of "term".
7452 */
7453 static void
7454term_free_vterm(term_T *term)
7455{
7456 if (term->tl_vterm != NULL)
7457 vterm_free(term->tl_vterm);
7458 term->tl_vterm = NULL;
7459}
7460
7461/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007462 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007463 */
7464 static void
7465term_report_winsize(term_T *term, int rows, int cols)
7466{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007467 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007468 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7469 {
7470 int fd = -1;
7471 int part;
7472
7473 for (part = PART_OUT; part < PART_COUNT; ++part)
7474 {
7475 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007476 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007477 break;
7478 }
7479 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7480 mch_signal_job(term->tl_job, (char_u *)"winch");
7481 }
7482}
7483
7484# endif
7485
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007486#endif // FEAT_TERMINAL