blob: 43a216f951af02eccce2e2cad74ceb57295f33bb [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);
329 set_option_value((char_u *)"termwinsize", 0L, buf, OPT_LOCAL);
330 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200331 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200332}
333
334/*
335 * Initialize job options for a terminal job.
336 * Caller may overrule some of them.
337 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100338 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200339init_job_options(jobopt_T *opt)
340{
341 clear_job_options(opt);
342
343 opt->jo_mode = MODE_RAW;
344 opt->jo_out_mode = MODE_RAW;
345 opt->jo_err_mode = MODE_RAW;
346 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
347}
348
349/*
350 * Set job options mandatory for a terminal job.
351 */
352 static void
353setup_job_options(jobopt_T *opt, int rows, int cols)
354{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100355#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100356 // Win32: Redirecting the job output won't work, thus always connect stdout
357 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200358 if (!(opt->jo_set & JO_OUT_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200359#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200360 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100361 // Connect stdout to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200362 opt->jo_io[PART_OUT] = JIO_BUFFER;
363 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
364 opt->jo_modifiable[PART_OUT] = 0;
365 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
366 }
367
Bram Moolenaar4f974752019-02-17 17:44:42 +0100368#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100369 // Win32: Redirecting the job output won't work, thus always connect stderr
370 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200371 if (!(opt->jo_set & JO_ERR_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200372#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200373 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100374 // Connect stderr to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200375 opt->jo_io[PART_ERR] = JIO_BUFFER;
376 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
377 opt->jo_modifiable[PART_ERR] = 0;
378 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
379 }
380
381 opt->jo_pty = TRUE;
382 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
383 opt->jo_term_rows = rows;
384 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
385 opt->jo_term_cols = cols;
386}
387
388/*
Bram Moolenaar5c381eb2019-06-25 06:50:31 +0200389 * Flush messages on channels.
390 */
391 static void
392term_flush_messages()
393{
394 mch_check_messages();
395 parse_queued_messages();
396}
397
398/*
Bram Moolenaard96ff162018-02-18 22:13:29 +0100399 * Close a terminal buffer (and its window). Used when creating the terminal
400 * fails.
401 */
402 static void
403term_close_buffer(buf_T *buf, buf_T *old_curbuf)
404{
405 free_terminal(buf);
406 if (old_curbuf != NULL)
407 {
408 --curbuf->b_nwindows;
409 curbuf = old_curbuf;
410 curwin->w_buffer = curbuf;
411 ++curbuf->b_nwindows;
412 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100413 CHECK_CURBUF;
Bram Moolenaard96ff162018-02-18 22:13:29 +0100414
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100415 // Wiping out the buffer will also close the window and call
416 // free_terminal().
Bram Moolenaard96ff162018-02-18 22:13:29 +0100417 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
418}
419
420/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200421 * Start a terminal window and return its buffer.
Bram Moolenaar13568252018-03-16 20:46:58 +0100422 * Use either "argvar" or "argv", the other must be NULL.
423 * When "flags" has TERM_START_NOJOB only create the buffer, b_term and open
424 * the window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200425 * Returns NULL when failed.
426 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100427 buf_T *
428term_start(
429 typval_T *argvar,
430 char **argv,
431 jobopt_T *opt,
432 int flags)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200433{
434 exarg_T split_ea;
435 win_T *old_curwin = curwin;
436 term_T *term;
437 buf_T *old_curbuf = NULL;
438 int res;
439 buf_T *newbuf;
Bram Moolenaare1004402020-10-24 20:49:43 +0200440 int vertical = opt->jo_vertical || (cmdmod.cmod_split & WSP_VERT);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200441 jobopt_T orig_opt; // only partly filled
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200442
443 if (check_restricted() || check_secure())
444 return NULL;
Bram Moolenaare5b44862021-05-30 13:54:03 +0200445#ifdef FEAT_CMDWIN
446 if (cmdwin_type != 0)
447 {
448 emsg(_(e_cannot_open_terminal_from_command_line_window));
449 return NULL;
450 }
451#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200452
453 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
454 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
455 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
Bram Moolenaarb0992022020-01-30 14:55:42 +0100456 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))
457 || (argvar != NULL
458 && argvar->v_type == VAR_LIST
459 && argvar->vval.v_list != NULL
460 && argvar->vval.v_list->lv_first == &range_list_item))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200461 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000462 emsg(_(e_invalid_argument));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200463 return NULL;
464 }
465
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200466 term = ALLOC_CLEAR_ONE(term_T);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200467 if (term == NULL)
468 return NULL;
469 term->tl_dirty_row_end = MAX_ROW;
470 term->tl_cursor_visible = TRUE;
471 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
472 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100473#ifdef FEAT_GUI
474 term->tl_system = (flags & TERM_START_SYSTEM);
475#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200476 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100477 ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200478 ga_init2(&term->tl_osc_buf, sizeof(char), 300);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200479
Bram Moolenaaraeed2a62021-04-29 20:18:45 +0200480 setpcmark();
Bram Moolenaara80faa82020-04-12 19:37:17 +0200481 CLEAR_FIELD(split_ea);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200482 if (opt->jo_curwin)
483 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100484 // Create a new buffer in the current window.
Bram Moolenaar13568252018-03-16 20:46:58 +0100485 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200486 {
487 no_write_message();
488 vim_free(term);
489 return NULL;
490 }
491 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaarb1009092020-05-31 16:04:42 +0200492 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
493 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
494 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200495 {
496 vim_free(term);
497 return NULL;
498 }
499 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100500 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200501 {
502 buf_T *buf;
503
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100504 // Create a new buffer without a window. Make it the current buffer for
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100505 // a moment to be able to do the initializations.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200506 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
507 BLN_NEW | BLN_LISTED);
508 if (buf == NULL || ml_open(buf) == FAIL)
509 {
510 vim_free(term);
511 return NULL;
512 }
513 old_curbuf = curbuf;
514 --curbuf->b_nwindows;
515 curbuf = buf;
516 curwin->w_buffer = buf;
517 ++curbuf->b_nwindows;
518 }
519 else
520 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100521 // Open a new window or tab.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200522 split_ea.cmdidx = CMD_new;
523 split_ea.cmd = (char_u *)"new";
524 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100525 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200526 {
527 split_ea.line2 = opt->jo_term_rows;
528 split_ea.addr_count = 1;
529 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100530 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200531 {
532 split_ea.line2 = opt->jo_term_cols;
533 split_ea.addr_count = 1;
534 }
535
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100536 if (vertical)
Bram Moolenaare1004402020-10-24 20:49:43 +0200537 cmdmod.cmod_split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200538 ex_splitview(&split_ea);
539 if (curwin == old_curwin)
540 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100541 // split failed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200542 vim_free(term);
543 return NULL;
544 }
545 }
546 term->tl_buffer = curbuf;
547 curbuf->b_term = term;
548
549 if (!opt->jo_hidden)
550 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100551 // Only one size was taken care of with :new, do the other one. With
552 // "curwin" both need to be done.
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100553 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200554 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100555 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200556 win_setwidth(opt->jo_term_cols);
557 }
558
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100559 // Link the new terminal in the list of active terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200560 term->tl_next = first_term;
561 first_term = term;
562
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100563 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
564
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200565 if (opt->jo_term_name != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100566 {
567 vim_free(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200568 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100569 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100570 else if (argv != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100571 {
572 vim_free(curbuf->b_ffname);
Bram Moolenaar13568252018-03-16 20:46:58 +0100573 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100574 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200575 else
576 {
577 int i;
578 size_t len;
579 char_u *cmd, *p;
580
581 if (argvar->v_type == VAR_STRING)
582 {
583 cmd = argvar->vval.v_string;
584 if (cmd == NULL)
585 cmd = (char_u *)"";
586 else if (STRCMP(cmd, "NONE") == 0)
587 cmd = (char_u *)"pty";
588 }
589 else if (argvar->v_type != VAR_LIST
590 || argvar->vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +0100591 || argvar->vval.v_list->lv_len == 0
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100592 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200593 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
594 cmd = (char_u*)"";
595
596 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200597 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200598
599 for (i = 0; p != NULL; ++i)
600 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100601 // Prepend a ! to the command name to avoid the buffer name equals
602 // the executable, otherwise ":w!" would overwrite it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200603 if (i == 0)
604 vim_snprintf((char *)p, len, "!%s", cmd);
605 else
606 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
607 if (buflist_findname(p) == NULL)
608 {
609 vim_free(curbuf->b_ffname);
610 curbuf->b_ffname = p;
611 break;
612 }
613 }
614 }
Bram Moolenaare010c722020-02-24 21:37:54 +0100615 vim_free(curbuf->b_sfname);
616 curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200617 curbuf->b_fname = curbuf->b_ffname;
618
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100619 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
620
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200621 if (opt->jo_term_opencmd != NULL)
622 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
623
624 if (opt->jo_eof_chars != NULL)
625 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
626
627 set_string_option_direct((char_u *)"buftype", -1,
628 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200629 // Avoid that 'buftype' is reset when this buffer is entered.
630 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200631
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100632 // Mark the buffer as not modifiable. It can only be made modifiable after
633 // the job finished.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200634 curbuf->b_p_ma = FALSE;
635
Bram Moolenaarb936b792020-09-04 18:34:09 +0200636 set_term_and_win_size(term, opt);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100637#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200638 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
639#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200640 setup_job_options(opt, term->tl_rows, term->tl_cols);
641
Bram Moolenaar13568252018-03-16 20:46:58 +0100642 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100643 return curbuf;
644
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100645#if defined(FEAT_SESSION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100646 // Remember the command for the session file.
Bram Moolenaar13568252018-03-16 20:46:58 +0100647 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100648 term->tl_command = vim_strsave((char_u *)"NONE");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100649 else if (argvar->v_type == VAR_STRING)
650 {
651 char_u *cmd = argvar->vval.v_string;
652
653 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
654 term->tl_command = vim_strsave(cmd);
655 }
656 else if (argvar->v_type == VAR_LIST
657 && argvar->vval.v_list != NULL
658 && argvar->vval.v_list->lv_len > 0)
659 {
660 garray_T ga;
661 listitem_T *item;
662
663 ga_init2(&ga, 1, 100);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200664 FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100665 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100666 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100667 char_u *p;
668
669 if (s == NULL)
670 break;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100671 p = vim_strsave_fnameescape(s, VSE_NONE);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100672 if (p == NULL)
673 break;
674 ga_concat(&ga, p);
675 vim_free(p);
676 ga_append(&ga, ' ');
677 }
678 if (item == NULL)
679 {
680 ga_append(&ga, NUL);
681 term->tl_command = ga.ga_data;
682 }
683 else
684 ga_clear(&ga);
685 }
686#endif
687
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100688 if (opt->jo_term_kill != NULL)
689 {
690 char_u *p = skiptowhite(opt->jo_term_kill);
691
692 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
693 }
694
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200695 if (opt->jo_term_api != NULL)
Bram Moolenaar21109272020-01-30 16:27:20 +0100696 {
697 char_u *p = skiptowhite(opt->jo_term_api);
698
699 term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
700 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200701 else
702 term->tl_api = vim_strsave((char_u *)"Tapi_");
703
Bram Moolenaar83d47902020-03-26 20:34:00 +0100704 if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
705 term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
706
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100707 // System dependent: setup the vterm and maybe start the job in it.
Bram Moolenaar13568252018-03-16 20:46:58 +0100708 if (argv == NULL
709 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200710 && argvar->vval.v_string != NULL
711 && STRCMP(argvar->vval.v_string, "NONE") == 0)
712 res = create_pty_only(term, opt);
713 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200714 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200715
716 newbuf = curbuf;
717 if (res == OK)
718 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100719 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200720 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
721 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100722#ifdef FEAT_GUI
723 if (term->tl_system)
724 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100725 // display first line below typed command
Bram Moolenaar13568252018-03-16 20:46:58 +0100726 term->tl_toprow = msg_row + 1;
727 term->tl_dirty_row_end = 0;
728 }
729#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200730
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100731 // Make sure we don't get stuck on sending keys to the job, it leads to
732 // a deadlock if the job is waiting for Vim to read.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200733 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
734
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200735 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200736 {
737 --curbuf->b_nwindows;
738 curbuf = old_curbuf;
739 curwin->w_buffer = curbuf;
740 ++curbuf->b_nwindows;
741 }
Bram Moolenaar81035272021-12-16 18:02:07 +0000742 else if (vgetc_busy
743#ifdef FEAT_TIMERS
744 || timer_busy
745#endif
746 || input_busy)
747 {
748 char_u ignore[4];
749
750 // When waiting for input need to return and possibly end up in
751 // terminal_loop() instead.
752 ignore[0] = K_SPECIAL;
753 ignore[1] = KS_EXTRA;
754 ignore[2] = KE_IGNORE;
755 ignore[3] = NUL;
756 ins_typebuf(ignore, REMAP_NONE, 0, TRUE, FALSE);
757 typebuf_was_filled = TRUE;
758 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200759 }
760 else
761 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100762 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200763 return NULL;
764 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100765
Bram Moolenaar13568252018-03-16 20:46:58 +0100766 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar28ed4df2019-10-26 16:21:40 +0200767 if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
768 apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200769 return newbuf;
770}
771
772/*
773 * ":terminal": open a terminal window and execute a job in it.
774 */
775 void
776ex_terminal(exarg_T *eap)
777{
778 typval_T argvar[2];
779 jobopt_T opt;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100780 int opt_shell = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200781 char_u *cmd;
782 char_u *tofree = NULL;
783
784 init_job_options(&opt);
785
786 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100787 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200788 {
789 char_u *p, *ep;
790
791 cmd += 2;
792 p = skiptowhite(cmd);
793 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200794 if (ep != NULL)
795 {
796 if (ep < p)
797 p = ep;
798 else
799 ep = NULL;
800 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200801
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200802# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
803 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
804 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200805 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200806 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100807 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200808 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200809 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200810 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200811 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200812 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200813 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200814 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100815 opt.jo_term_norestore = 1;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100816 else if (OPTARG_HAS("shell"))
817 opt_shell = TRUE;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200818 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100819 {
820 opt.jo_set2 |= JO2_TERM_KILL;
821 opt.jo_term_kill = ep + 1;
822 p = skiptowhite(cmd);
823 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200824 else if (OPTARG_HAS("api"))
825 {
826 opt.jo_set2 |= JO2_TERM_API;
827 if (ep != NULL)
828 {
829 opt.jo_term_api = ep + 1;
830 p = skiptowhite(cmd);
831 }
832 else
833 opt.jo_term_api = NULL;
834 }
835 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200836 {
837 opt.jo_set2 |= JO2_TERM_ROWS;
838 opt.jo_term_rows = atoi((char *)ep + 1);
839 p = skiptowhite(cmd);
840 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200841 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200842 {
843 opt.jo_set2 |= JO2_TERM_COLS;
844 opt.jo_term_cols = atoi((char *)ep + 1);
845 p = skiptowhite(cmd);
846 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200847 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200848 {
849 char_u *buf = NULL;
850 char_u *keys;
851
Bram Moolenaar21109272020-01-30 16:27:20 +0100852 vim_free(opt.jo_eof_chars);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200853 p = skiptowhite(cmd);
854 *p = NUL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200855 keys = replace_termcodes(ep + 1, &buf,
856 REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200857 opt.jo_set2 |= JO2_EOF_CHARS;
858 opt.jo_eof_chars = vim_strsave(keys);
859 vim_free(buf);
860 *p = ' ';
861 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100862#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100863 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
864 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100865 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100866 int tty_type = NUL;
867
868 p = skiptowhite(cmd);
869 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
870 tty_type = 'w';
871 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
872 tty_type = 'c';
873 else
874 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000875 semsg(e_invalid_value_for_argument_str, "type");
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100876 goto theend;
877 }
878 opt.jo_set2 |= JO2_TTY_TYPE;
879 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100880 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100881#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200882 else
883 {
884 if (*p)
885 *p = NUL;
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000886 semsg(_(e_invalid_attribute_str), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100887 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200888 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200889# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200890 cmd = skipwhite(p);
891 }
892 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100893 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100894 // Make a copy of 'shell', an autocommand may change the option.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200895 tofree = cmd = vim_strsave(p_sh);
896
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100897 // default to close when the shell exits
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100898 if (opt.jo_term_finish == NUL)
Bram Moolenaare2978022020-04-26 14:47:44 +0200899 opt.jo_term_finish = TL_FINISH_CLOSE;
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100900 }
901
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200902 if (eap->addr_count > 0)
903 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100904 // Write lines from current buffer to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200905 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
906 opt.jo_io[PART_IN] = JIO_BUFFER;
907 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
908 opt.jo_in_top = eap->line1;
909 opt.jo_in_bot = eap->line2;
910 }
911
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100912 if (opt_shell && tofree == NULL)
913 {
914#ifdef UNIX
915 char **argv = NULL;
916 char_u *tofree1 = NULL;
917 char_u *tofree2 = NULL;
918
919 // :term ++shell command
920 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
921 term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaaradf4aa22019-11-10 22:36:44 +0100922 vim_free(argv);
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100923 vim_free(tofree1);
924 vim_free(tofree2);
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100925 goto theend;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100926#else
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100927# ifdef MSWIN
928 long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
929 char_u *newcmd;
930
931 newcmd = alloc(cmdlen);
932 if (newcmd == NULL)
933 goto theend;
934 tofree = newcmd;
935 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
936 cmd = newcmd;
937# else
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000938 emsg(_(e_sorry_plusplusshell_not_supported_on_this_system));
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100939 goto theend;
940# endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100941#endif
942 }
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100943 argvar[0].v_type = VAR_STRING;
944 argvar[0].vval.v_string = cmd;
945 argvar[1].v_type = VAR_UNKNOWN;
946 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100947
948theend:
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100949 vim_free(tofree);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200950 vim_free(opt.jo_eof_chars);
951}
952
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100953#if defined(FEAT_SESSION) || defined(PROTO)
954/*
955 * Write a :terminal command to the session file to restore the terminal in
956 * window "wp".
957 * Return FAIL if writing fails.
958 */
959 int
Bram Moolenaar0e655112020-09-11 20:36:36 +0200960term_write_session(FILE *fd, win_T *wp, hashtab_T *terminal_bufs)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100961{
Bram Moolenaar0e655112020-09-11 20:36:36 +0200962 const int bufnr = wp->w_buffer->b_fnum;
963 term_T *term = wp->w_buffer->b_term;
964
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200965 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200966 {
967 // There are multiple views into this terminal buffer. We don't want to
968 // create the terminal multiple times. If it's the first time, create,
969 // otherwise link to the first buffer.
970 char id_as_str[NUMBUFLEN];
971 hashitem_T *entry;
972
973 vim_snprintf(id_as_str, sizeof(id_as_str), "%d", bufnr);
974
975 entry = hash_find(terminal_bufs, (char_u *)id_as_str);
976 if (!HASHITEM_EMPTY(entry))
977 {
978 // we've already opened this terminal buffer
979 if (fprintf(fd, "execute 'buffer ' . s:term_buf_%d", bufnr) < 0)
980 return FAIL;
981 return put_eol(fd);
982 }
983 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100984
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100985 // Create the terminal and run the command. This is not without
986 // risk, but let's assume the user only creates a session when this
987 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100988 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
989 term->tl_cols, term->tl_rows) < 0)
990 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100991#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100992 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
993 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100994#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100995 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
996 return FAIL;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200997 if (put_eol(fd) != OK)
998 return FAIL;
999
1000 if (fprintf(fd, "let s:term_buf_%d = bufnr()", bufnr) < 0)
1001 return FAIL;
1002
Bram Moolenaarc2c82052020-09-11 22:10:22 +02001003 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +02001004 {
1005 char *hash_key = alloc(NUMBUFLEN);
1006
1007 vim_snprintf(hash_key, NUMBUFLEN, "%d", bufnr);
1008 hash_add(terminal_bufs, (char_u *)hash_key);
1009 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001010
1011 return put_eol(fd);
1012}
1013
1014/*
1015 * Return TRUE if "buf" has a terminal that should be restored.
1016 */
1017 int
1018term_should_restore(buf_T *buf)
1019{
1020 term_T *term = buf->b_term;
1021
1022 return term != NULL && (term->tl_command == NULL
1023 || STRCMP(term->tl_command, "NONE") != 0);
1024}
1025#endif
1026
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001027/*
1028 * Free the scrollback buffer for "term".
1029 */
1030 static void
1031free_scrollback(term_T *term)
1032{
1033 int i;
1034
1035 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
1036 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
1037 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001038 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
1039 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
1040 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001041}
1042
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001043
1044// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +02001045static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001046
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001047/*
1048 * Free a terminal and everything it refers to.
1049 * Kills the job if there is one.
1050 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001051 * The actual terminal structure is freed later in free_unused_terminals(),
1052 * because callbacks may wipe out a buffer while the terminal is still
1053 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001054 */
1055 void
1056free_terminal(buf_T *buf)
1057{
1058 term_T *term = buf->b_term;
1059 term_T *tp;
1060
1061 if (term == NULL)
1062 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001063
1064 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001065 if (first_term == term)
1066 first_term = term->tl_next;
1067 else
1068 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
1069 if (tp->tl_next == term)
1070 {
1071 tp->tl_next = term->tl_next;
1072 break;
1073 }
1074
1075 if (term->tl_job != NULL)
1076 {
1077 if (term->tl_job->jv_status != JOB_ENDED
1078 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +01001079 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001080 job_stop(term->tl_job, NULL, "kill");
1081 job_unref(term->tl_job);
1082 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001083 term->tl_next = terminals_to_free;
1084 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001085
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001086 buf->b_term = NULL;
1087 if (in_terminal_loop == term)
1088 in_terminal_loop = NULL;
1089}
1090
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001091 void
1092free_unused_terminals()
1093{
1094 while (terminals_to_free != NULL)
1095 {
1096 term_T *term = terminals_to_free;
1097
1098 terminals_to_free = term->tl_next;
1099
1100 free_scrollback(term);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02001101 ga_clear(&term->tl_osc_buf);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001102
1103 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001104 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001105 vim_free(term->tl_title);
1106#ifdef FEAT_SESSION
1107 vim_free(term->tl_command);
1108#endif
1109 vim_free(term->tl_kill);
1110 vim_free(term->tl_status_text);
1111 vim_free(term->tl_opencmd);
1112 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001113 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001114#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001115 if (term->tl_out_fd != NULL)
1116 fclose(term->tl_out_fd);
1117#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001118 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001119 vim_free(term->tl_cursor_color);
1120 vim_free(term);
1121 }
1122}
1123
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001124/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001125 * Get the part that is connected to the tty. Normally this is PART_IN, but
1126 * when writing buffer lines to the job it can be another. This makes it
1127 * possible to do "1,5term vim -".
1128 */
1129 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001130get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001131{
1132#ifdef UNIX
1133 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1134 int i;
1135
1136 for (i = 0; i < 3; ++i)
1137 {
1138 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1139
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001140 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001141 return parts[i];
1142 }
1143#endif
1144 return PART_IN;
1145}
1146
1147/*
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001148 * Read any vterm output and send it on the channel.
1149 */
1150 static void
1151term_forward_output(term_T *term)
1152{
1153 VTerm *vterm = term->tl_vterm;
1154 char buf[KEY_BUF_LEN];
1155 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1156
1157 if (curlen > 0)
1158 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1159 (char_u *)buf, (int)curlen, NULL);
1160}
1161
1162/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001163 * Write job output "msg[len]" to the vterm.
1164 */
1165 static void
Bram Moolenaar36968af2021-11-15 17:13:11 +00001166term_write_job_output(term_T *term, char_u *msg_arg, size_t len_arg)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001167{
Bram Moolenaar36968af2021-11-15 17:13:11 +00001168 char_u *msg = msg_arg;
1169 size_t len = len_arg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001170 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001171 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar36968af2021-11-15 17:13:11 +00001172 size_t limit = term->tl_buffer->b_p_twsl * term->tl_cols * 3;
1173
1174 // Limit the length to 'termwinscroll' * cols * 3 bytes. Keep the text at
1175 // the end.
1176 if (len > limit)
1177 {
1178 char_u *p = msg + len - limit;
1179
1180 p -= (*mb_head_off)(msg, p);
1181 len -= p - msg;
1182 msg = p;
1183 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001184
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001185 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001186
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001187 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001188 if (prevlen != vterm_output_get_buffer_current(vterm))
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001189 term_forward_output(term);
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001190
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001191 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001192 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1193}
1194
1195 static void
1196update_cursor(term_T *term, int redraw)
1197{
1198 if (term->tl_normal_mode)
1199 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001200#ifdef FEAT_GUI
1201 if (term->tl_system)
1202 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1203 term->tl_cursor_pos.col);
1204 else
1205#endif
1206 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001207 if (redraw)
1208 {
1209 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1210 cursor_on();
1211 out_flush();
1212#ifdef FEAT_GUI
1213 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001214 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001215 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001216 gui_mch_flush();
1217 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001218#endif
1219 }
1220}
1221
1222/*
1223 * Invoked when "msg" output from a job was received. Write it to the terminal
1224 * of "buffer".
1225 */
1226 void
1227write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1228{
1229 size_t len = STRLEN(msg);
1230 term_T *term = buffer->b_term;
1231
Bram Moolenaar4f974752019-02-17 17:44:42 +01001232#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001233 // Win32: Cannot redirect output of the job, intercept it here and write to
1234 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001235 if (term->tl_out_fd != NULL)
1236 {
1237 ch_log(channel, "Writing %d bytes to output file", (int)len);
1238 fwrite(msg, len, 1, term->tl_out_fd);
1239 return;
1240 }
1241#endif
1242
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001243 if (term->tl_vterm == NULL)
1244 {
1245 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1246 return;
1247 }
1248 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01001249 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001250 term_write_job_output(term, msg, len);
1251
Bram Moolenaar13568252018-03-16 20:46:58 +01001252#ifdef FEAT_GUI
1253 if (term->tl_system)
1254 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001255 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001256 update_system_term(term);
1257 update_cursor(term, TRUE);
1258 }
1259 else
1260#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001261 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1262 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001263 if (!term->tl_normal_mode)
1264 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001265 // Don't use update_screen() when editing the command line, it gets
1266 // cleared.
1267 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001268 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001269 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001270 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001271 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001272 // update_screen() can be slow, check the terminal wasn't closed
1273 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001274 if (buffer == curbuf && curbuf->b_term != NULL)
1275 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001276 }
1277 else
Bram Moolenaare5050712021-12-09 10:51:05 +00001278 redraw_after_callback(TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001279 }
1280}
1281
1282/*
1283 * Send a mouse position and click to the vterm
1284 */
1285 static int
1286term_send_mouse(VTerm *vterm, int button, int pressed)
1287{
1288 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001289 int row = mouse_row - W_WINROW(curwin);
1290 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001291
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001292#ifdef FEAT_PROP_POPUP
1293 if (popup_is_popup(curwin))
1294 {
1295 row -= popup_top_extra(curwin);
1296 col -= popup_left_extra(curwin);
1297 }
1298#endif
1299 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001300 if (button != 0)
1301 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001302 return TRUE;
1303}
1304
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001305static int enter_mouse_col = -1;
1306static int enter_mouse_row = -1;
1307
1308/*
1309 * Handle a mouse click, drag or release.
1310 * Return TRUE when a mouse event is sent to the terminal.
1311 */
1312 static int
1313term_mouse_click(VTerm *vterm, int key)
1314{
1315#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001316 // For modeless selection mouse drag and release events are ignored, unless
1317 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001318 static int ignore_drag_release = TRUE;
1319 VTermMouseState mouse_state;
1320
1321 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1322 if (mouse_state.flags == 0)
1323 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001324 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001325 switch (key)
1326 {
1327 case K_LEFTDRAG:
1328 case K_LEFTRELEASE:
1329 case K_RIGHTDRAG:
1330 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001331 // Ignore drag and release events when the button-down wasn't
1332 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001333 if (ignore_drag_release)
1334 {
1335 int save_mouse_col, save_mouse_row;
1336
1337 if (enter_mouse_col < 0)
1338 break;
1339
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001340 // mouse click in the window gave us focus, handle that
1341 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001342 save_mouse_col = mouse_col;
1343 save_mouse_row = mouse_row;
1344 mouse_col = enter_mouse_col;
1345 mouse_row = enter_mouse_row;
1346 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1347 mouse_col = save_mouse_col;
1348 mouse_row = save_mouse_row;
1349 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001350 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001351 case K_LEFTMOUSE:
1352 case K_RIGHTMOUSE:
1353 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1354 ignore_drag_release = TRUE;
1355 else
1356 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001357 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001358 if (clip_star.available)
1359 {
1360 int button, is_click, is_drag;
1361
1362 button = get_mouse_button(KEY2TERMCAP1(key),
1363 &is_click, &is_drag);
1364 if (mouse_model_popup() && button == MOUSE_LEFT
1365 && (mod_mask & MOD_MASK_SHIFT))
1366 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001367 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001368 button = MOUSE_RIGHT;
1369 mod_mask &= ~MOD_MASK_SHIFT;
1370 }
1371 clip_modeless(button, is_click, is_drag);
1372 }
1373 break;
1374
1375 case K_MIDDLEMOUSE:
1376 if (clip_star.available)
1377 insert_reg('*', TRUE);
1378 break;
1379 }
1380 enter_mouse_col = -1;
1381 return FALSE;
1382 }
1383#endif
1384 enter_mouse_col = -1;
1385
1386 switch (key)
1387 {
1388 case K_LEFTMOUSE:
1389 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1390 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1391 case K_LEFTRELEASE:
1392 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1393 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1394 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1395 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1396 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1397 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1398 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1399 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1400 }
1401 return TRUE;
1402}
1403
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001404/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001405 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1406 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001407 * Return the number of bytes in "buf".
1408 */
1409 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001410term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001411{
1412 VTerm *vterm = term->tl_vterm;
1413 VTermKey key = VTERM_KEY_NONE;
1414 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001415 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001416
1417 switch (c)
1418 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001419 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001420
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001421 // don't use VTERM_KEY_BACKSPACE, it always
1422 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001423 case K_BS: c = term_backspace_char; break;
1424
1425 case ESC: key = VTERM_KEY_ESCAPE; break;
1426 case K_DEL: key = VTERM_KEY_DEL; break;
1427 case K_DOWN: key = VTERM_KEY_DOWN; break;
1428 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1429 key = VTERM_KEY_DOWN; break;
1430 case K_END: key = VTERM_KEY_END; break;
1431 case K_S_END: mod = VTERM_MOD_SHIFT;
1432 key = VTERM_KEY_END; break;
1433 case K_C_END: mod = VTERM_MOD_CTRL;
1434 key = VTERM_KEY_END; break;
1435 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1436 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1437 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1438 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1439 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1440 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1441 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1442 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1443 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1444 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1445 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1446 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1447 case K_HOME: key = VTERM_KEY_HOME; break;
1448 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1449 key = VTERM_KEY_HOME; break;
1450 case K_C_HOME: mod = VTERM_MOD_CTRL;
1451 key = VTERM_KEY_HOME; break;
1452 case K_INS: key = VTERM_KEY_INS; break;
1453 case K_K0: key = VTERM_KEY_KP_0; break;
1454 case K_K1: key = VTERM_KEY_KP_1; break;
1455 case K_K2: key = VTERM_KEY_KP_2; break;
1456 case K_K3: key = VTERM_KEY_KP_3; break;
1457 case K_K4: key = VTERM_KEY_KP_4; break;
1458 case K_K5: key = VTERM_KEY_KP_5; break;
1459 case K_K6: key = VTERM_KEY_KP_6; break;
1460 case K_K7: key = VTERM_KEY_KP_7; break;
1461 case K_K8: key = VTERM_KEY_KP_8; break;
1462 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001463 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001464 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001465 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001466 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001467 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1468 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001469 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1470 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001471 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1472 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001473 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1474 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1475 case K_LEFT: key = VTERM_KEY_LEFT; break;
1476 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1477 key = VTERM_KEY_LEFT; break;
1478 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1479 key = VTERM_KEY_LEFT; break;
1480 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1481 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1482 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1483 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1484 key = VTERM_KEY_RIGHT; break;
1485 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1486 key = VTERM_KEY_RIGHT; break;
1487 case K_UP: key = VTERM_KEY_UP; break;
1488 case K_S_UP: mod = VTERM_MOD_SHIFT;
1489 key = VTERM_KEY_UP; break;
1490 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001491 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1492 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001493
Bram Moolenaara42ad572017-11-16 13:08:04 +01001494 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1495 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001496 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1497 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001498
1499 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001500 case K_LEFTMOUSE_NM:
1501 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001502 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001503 case K_LEFTRELEASE_NM:
1504 case K_MOUSEMOVE:
1505 case K_MIDDLEMOUSE:
1506 case K_MIDDLEDRAG:
1507 case K_MIDDLERELEASE:
1508 case K_RIGHTMOUSE:
1509 case K_RIGHTDRAG:
1510 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1511 return 0;
1512 other = TRUE;
1513 break;
1514
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001515 case K_X1MOUSE: /* TODO */ return 0;
1516 case K_X1DRAG: /* TODO */ return 0;
1517 case K_X1RELEASE: /* TODO */ return 0;
1518 case K_X2MOUSE: /* TODO */ return 0;
1519 case K_X2DRAG: /* TODO */ return 0;
1520 case K_X2RELEASE: /* TODO */ return 0;
1521
1522 case K_IGNORE: return 0;
1523 case K_NOP: return 0;
1524 case K_UNDO: return 0;
1525 case K_HELP: return 0;
1526 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1527 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1528 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1529 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1530 case K_SELECT: return 0;
1531#ifdef FEAT_GUI
1532 case K_VER_SCROLLBAR: return 0;
1533 case K_HOR_SCROLLBAR: return 0;
1534#endif
1535#ifdef FEAT_GUI_TABLINE
1536 case K_TABLINE: return 0;
1537 case K_TABMENU: return 0;
1538#endif
1539#ifdef FEAT_NETBEANS_INTG
1540 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1541#endif
1542#ifdef FEAT_DND
1543 case K_DROP: return 0;
1544#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001545 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001546 case K_PS: vterm_keyboard_start_paste(vterm);
1547 other = TRUE;
1548 break;
1549 case K_PE: vterm_keyboard_end_paste(vterm);
1550 other = TRUE;
1551 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001552 }
1553
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001554 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001555 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001556 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001557 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001558 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001559 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001560 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001561
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001562 /*
1563 * Convert special keys to vterm keys:
1564 * - Write keys to vterm: vterm_keyboard_key()
1565 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001566 */
1567 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001568 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001569 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001570 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001571 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001572 vterm_keyboard_unichar(vterm, c, mod);
1573
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001574 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001575 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1576}
1577
1578/*
1579 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001580 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001581 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001582 */
1583 static int
1584term_job_running_check(term_T *term, int check_job_status)
1585{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001586 // Also consider the job finished when the channel is closed, to avoid a
1587 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001588 if (term != NULL
1589 && term->tl_job != NULL
1590 && channel_is_open(term->tl_job->jv_channel))
1591 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001592 job_T *job = term->tl_job;
1593
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001594 // Careful: Checking the job status may invoke callbacks, which close
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001595 // the buffer and terminate "term". However, "job" will not be freed
1596 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001597 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001598 job_status(job);
1599 return (job->jv_status == JOB_STARTED
1600 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001601 }
1602 return FALSE;
1603}
1604
1605/*
1606 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001607 */
1608 int
1609term_job_running(term_T *term)
1610{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001611 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001612}
1613
1614/*
1615 * Return TRUE if "term" has an active channel and used ":term NONE".
1616 */
1617 int
1618term_none_open(term_T *term)
1619{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001620 // Also consider the job finished when the channel is closed, to avoid a
1621 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001622 return term != NULL
1623 && term->tl_job != NULL
1624 && channel_is_open(term->tl_job->jv_channel)
1625 && term->tl_job->jv_channel->ch_keep_open;
1626}
1627
1628/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001629 * Used when exiting: kill the job in "buf" if so desired.
1630 * Return OK when the job finished.
1631 * Return FAIL when the job is still running.
1632 */
1633 int
1634term_try_stop_job(buf_T *buf)
1635{
1636 int count;
1637 char *how = (char *)buf->b_term->tl_kill;
1638
1639#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001640 if ((how == NULL || *how == NUL)
1641 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001642 {
1643 char_u buff[DIALOG_MSG_SIZE];
1644 int ret;
1645
Bram Moolenaar00806bc2020-11-05 19:36:38 +01001646 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001647 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1648 if (ret == VIM_YES)
1649 how = "kill";
1650 else if (ret == VIM_CANCEL)
1651 return FAIL;
1652 }
1653#endif
1654 if (how == NULL || *how == NUL)
1655 return FAIL;
1656
1657 job_stop(buf->b_term->tl_job, NULL, how);
1658
Bram Moolenaar9172d232019-01-29 23:06:54 +01001659 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001660 for (count = 0; count < 100; ++count)
1661 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001662 job_T *job;
1663
1664 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001665 if (!buf_valid(buf)
1666 || buf->b_term == NULL
1667 || buf->b_term->tl_job == NULL)
1668 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001669 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001670
Bram Moolenaar9172d232019-01-29 23:06:54 +01001671 // Call job_status() to update jv_status. It may cause the job to be
1672 // cleaned up but it won't be freed.
1673 job_status(job);
1674 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001675 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001676
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001677 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001678 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001679 }
1680 return FAIL;
1681}
1682
1683/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001684 * Add the last line of the scrollback buffer to the buffer in the window.
1685 */
1686 static void
1687add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1688{
1689 buf_T *buf = term->tl_buffer;
1690 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1691 linenr_T lnum = buf->b_ml.ml_line_count;
1692
Bram Moolenaar4f974752019-02-17 17:44:42 +01001693#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001694 if (!enc_utf8 && enc_codepage > 0)
1695 {
1696 WCHAR *ret = NULL;
1697 int length = 0;
1698
1699 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1700 &ret, &length);
1701 if (ret != NULL)
1702 {
1703 WideCharToMultiByte_alloc(enc_codepage, 0,
1704 ret, length, (char **)&text, &len, 0, 0);
1705 vim_free(ret);
1706 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1707 vim_free(text);
1708 }
1709 }
1710 else
1711#endif
1712 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1713 if (empty)
1714 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001715 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001716 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001717 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001718 curbuf = curwin->w_buffer;
1719 }
1720}
1721
1722 static void
1723cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1724{
1725 attr->width = cell->width;
1726 attr->attrs = cell->attrs;
1727 attr->fg = cell->fg;
1728 attr->bg = cell->bg;
1729}
1730
1731 static int
1732equal_celattr(cellattr_T *a, cellattr_T *b)
1733{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001734 // We only compare the RGB colors, ignoring the ANSI index and type.
1735 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001736 return a->fg.red == b->fg.red
1737 && a->fg.green == b->fg.green
1738 && a->fg.blue == b->fg.blue
1739 && a->bg.red == b->bg.red
1740 && a->bg.green == b->bg.green
1741 && a->bg.blue == b->bg.blue;
1742}
1743
Bram Moolenaard96ff162018-02-18 22:13:29 +01001744/*
1745 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1746 * line at this position. Otherwise at the end.
1747 */
1748 static int
1749add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1750{
1751 if (ga_grow(&term->tl_scrollback, 1) == OK)
1752 {
1753 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1754 + term->tl_scrollback.ga_len;
1755
1756 if (lnum > 0)
1757 {
1758 int i;
1759
1760 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1761 {
1762 *line = *(line - 1);
1763 --line;
1764 }
1765 }
1766 line->sb_cols = 0;
1767 line->sb_cells = NULL;
1768 line->sb_fill_attr = *fill_attr;
1769 ++term->tl_scrollback.ga_len;
1770 return OK;
1771 }
1772 return FALSE;
1773}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001774
1775/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001776 * Remove the terminal contents from the scrollback and the buffer.
1777 * Used before adding a new scrollback line or updating the buffer for lines
1778 * displayed in the terminal.
1779 */
1780 static void
1781cleanup_scrollback(term_T *term)
1782{
1783 sb_line_T *line;
1784 garray_T *gap;
1785
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001786 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001787 gap = &term->tl_scrollback;
1788 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1789 && gap->ga_len > 0)
1790 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001791 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001792 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1793 vim_free(line->sb_cells);
1794 --gap->ga_len;
1795 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001796 curbuf = curwin->w_buffer;
1797 if (curbuf == term->tl_buffer)
1798 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001799}
1800
1801/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001802 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001803 */
1804 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001805update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001806{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001807 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001808 int len;
1809 int lines_skipped = 0;
1810 VTermPos pos;
1811 VTermScreenCell cell;
1812 cellattr_T fill_attr, new_fill_attr;
1813 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001814
1815 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1816 "Adding terminal window snapshot to buffer");
1817
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001818 // First remove the lines that were appended before, they might be
1819 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001820 cleanup_scrollback(term);
1821
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001822 screen = vterm_obtain_screen(term->tl_vterm);
1823 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001824 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1825 {
1826 len = 0;
1827 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1828 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1829 && cell.chars[0] != NUL)
1830 {
1831 len = pos.col + 1;
1832 new_fill_attr = term->tl_default_color;
1833 }
1834 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001835 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001836 cell2cellattr(&cell, &new_fill_attr);
1837
1838 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1839 ++lines_skipped;
1840 else
1841 {
1842 while (lines_skipped > 0)
1843 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001844 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001845 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001846 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001847 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001848 }
1849
1850 if (len == 0)
1851 p = NULL;
1852 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001853 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001854 if ((p != NULL || len == 0)
1855 && ga_grow(&term->tl_scrollback, 1) == OK)
1856 {
1857 garray_T ga;
1858 int width;
1859 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1860 + term->tl_scrollback.ga_len;
1861
1862 ga_init2(&ga, 1, 100);
1863 for (pos.col = 0; pos.col < len; pos.col += width)
1864 {
1865 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1866 {
1867 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001868 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001869 if (ga_grow(&ga, 1) == OK)
1870 ga.ga_len += utf_char2bytes(' ',
1871 (char_u *)ga.ga_data + ga.ga_len);
1872 }
1873 else
1874 {
1875 width = cell.width;
1876
1877 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001878 if (width == 2)
1879 // second cell of double-width character has the
1880 // same attributes.
1881 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001882
Bram Moolenaara79fd562018-12-20 20:47:32 +01001883 // Each character can be up to 6 bytes.
1884 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001885 {
1886 int i;
1887 int c;
1888
1889 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1890 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1891 (char_u *)ga.ga_data + ga.ga_len);
1892 }
1893 }
1894 }
1895 line->sb_cols = len;
1896 line->sb_cells = p;
1897 line->sb_fill_attr = new_fill_attr;
1898 fill_attr = new_fill_attr;
1899 ++term->tl_scrollback.ga_len;
1900
1901 if (ga_grow(&ga, 1) == FAIL)
1902 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1903 else
1904 {
1905 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1906 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1907 }
1908 ga_clear(&ga);
1909 }
1910 else
1911 vim_free(p);
1912 }
1913 }
1914
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001915 // Add trailing empty lines.
1916 for (pos.row = term->tl_scrollback.ga_len;
1917 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1918 ++pos.row)
1919 {
1920 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1921 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1922 }
1923
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001924 term->tl_dirty_snapshot = FALSE;
1925#ifdef FEAT_TIMERS
1926 term->tl_timer_set = FALSE;
1927#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001928}
1929
1930/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001931 * Loop over all windows in the current tab, and also curwin, which is not
1932 * encountered when using a terminal in a popup window.
1933 * Return TRUE if "*wp" was set to the next window.
1934 */
1935 static int
1936for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1937{
1938 if (*wp == NULL)
1939 *wp = firstwin;
1940 else if ((*wp)->w_next != NULL)
1941 *wp = (*wp)->w_next;
1942 else if (!*did_curwin)
1943 *wp = curwin;
1944 else
1945 return FALSE;
1946 if (*wp == curwin)
1947 *did_curwin = TRUE;
1948 return TRUE;
1949}
1950
1951/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001952 * If needed, add the current lines of the terminal to scrollback and to the
1953 * buffer. Called after the job has ended and when switching to
1954 * Terminal-Normal mode.
1955 * When "redraw" is TRUE redraw the windows that show the terminal.
1956 */
1957 static void
1958may_move_terminal_to_buffer(term_T *term, int redraw)
1959{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001960 if (term->tl_vterm == NULL)
1961 return;
1962
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001963 // Update the snapshot only if something changes or the buffer does not
1964 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001965 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1966 <= term->tl_scrollback_scrolled)
1967 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001968
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001969 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001970 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1971 &term->tl_default_color.fg, &term->tl_default_color.bg);
1972
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001973 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001974 {
1975 win_T *wp = NULL;
1976 int did_curwin = FALSE;
1977
1978 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001979 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001980 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001981 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001982 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1983 wp->w_cursor.col = 0;
1984 wp->w_valid = 0;
1985 if (wp->w_cursor.lnum >= wp->w_height)
1986 {
1987 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001988
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001989 if (wp->w_topline < min_topline)
1990 wp->w_topline = min_topline;
1991 }
1992 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001993 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001994 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001995 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001996}
1997
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001998#if defined(FEAT_TIMERS) || defined(PROTO)
1999/*
2000 * Check if any terminal timer expired. If so, copy text from the terminal to
2001 * the buffer.
2002 * Return the time until the next timer will expire.
2003 */
2004 int
2005term_check_timers(int next_due_arg, proftime_T *now)
2006{
2007 term_T *term;
2008 int next_due = next_due_arg;
2009
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002010 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002011 {
2012 if (term->tl_timer_set && !term->tl_normal_mode)
2013 {
2014 long this_due = proftime_time_left(&term->tl_timer_due, now);
2015
2016 if (this_due <= 1)
2017 {
2018 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002019 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002020 }
2021 else if (next_due == -1 || next_due > this_due)
2022 next_due = this_due;
2023 }
2024 }
2025
2026 return next_due;
2027}
2028#endif
2029
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002030/*
2031 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
2032 * otherwise end it.
2033 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002034 static void
2035set_terminal_mode(term_T *term, int normal_mode)
2036{
2037 term->tl_normal_mode = normal_mode;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002038 trigger_modechanged();
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002039 if (!normal_mode)
2040 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002041 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002042 if (term->tl_buffer == curbuf)
2043 maketitle();
2044}
2045
2046/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002047 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002048 * Move the vterm contents into the scrollback buffer and free the vterm.
2049 */
2050 static void
2051cleanup_vterm(term_T *term)
2052{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002053 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002054 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002055 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002056 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002057}
2058
2059/*
2060 * Switch from Terminal-Job mode to Terminal-Normal mode.
2061 * Suspends updating the terminal window.
2062 */
2063 static void
2064term_enter_normal_mode(void)
2065{
2066 term_T *term = curbuf->b_term;
2067
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002068 set_terminal_mode(term, TRUE);
2069
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002070 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002071 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002072
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002073 // Move the window cursor to the position of the cursor in the
2074 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002075 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2076 + term->tl_cursor_pos.row + 1;
2077 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002078 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2079 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002080 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002081
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002082 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002083 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2084}
2085
2086/*
2087 * Returns TRUE if the current window contains a terminal and we are in
2088 * Terminal-Normal mode.
2089 */
2090 int
2091term_in_normal_mode(void)
2092{
2093 term_T *term = curbuf->b_term;
2094
2095 return term != NULL && term->tl_normal_mode;
2096}
2097
2098/*
2099 * Switch from Terminal-Normal mode to Terminal-Job mode.
2100 * Restores updating the terminal window.
2101 */
2102 void
2103term_enter_job_mode()
2104{
2105 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002106
2107 set_terminal_mode(term, FALSE);
2108
2109 if (term->tl_channel_closed)
2110 cleanup_vterm(term);
2111 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002112#ifdef FEAT_PROP_POPUP
2113 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002114 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002115#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002116}
2117
2118/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002119 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002120 * Note: while waiting a terminal may be closed and freed if the channel is
Bram Moolenaar829c8e82021-12-14 08:41:38 +00002121 * closed and ++close was used. This may even happen before we get here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002122 */
2123 static int
2124term_vgetc()
2125{
2126 int c;
2127 int save_State = State;
Bram Moolenaar829c8e82021-12-14 08:41:38 +00002128 int modify_other_keys = curbuf->b_term->tl_vterm == NULL ? FALSE
2129 : vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002130
2131 State = TERMINAL;
2132 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002133#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002134 ctrl_break_was_pressed = FALSE;
2135#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002136 if (modify_other_keys)
2137 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002138 c = vgetc();
2139 got_int = FALSE;
2140 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002141 if (modify_other_keys)
2142 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002143 return c;
2144}
2145
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002146static int mouse_was_outside = FALSE;
2147
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002148/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002149 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002150 * Return FAIL when the key needs to be handled in Normal mode.
2151 * Return OK when the key was dropped or sent to the terminal.
2152 */
2153 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002154send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002155{
2156 char msg[KEY_BUF_LEN];
2157 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002158 int dragging_outside = FALSE;
2159
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002160 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002161 switch (c)
2162 {
2163 case NUL:
2164 case K_ZERO:
2165 if (typed)
2166 stuffcharReadbuff(c);
2167 return FAIL;
2168
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002169 case K_TABLINE:
2170 stuffcharReadbuff(c);
2171 return FAIL;
2172
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002173 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002174 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002175 return FAIL;
2176
2177 case K_LEFTDRAG:
2178 case K_MIDDLEDRAG:
2179 case K_RIGHTDRAG:
2180 case K_X1DRAG:
2181 case K_X2DRAG:
2182 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002183 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002184 case K_LEFTMOUSE:
2185 case K_LEFTMOUSE_NM:
2186 case K_LEFTRELEASE:
2187 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002188 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002189 case K_MIDDLEMOUSE:
2190 case K_MIDDLERELEASE:
2191 case K_RIGHTMOUSE:
2192 case K_RIGHTRELEASE:
2193 case K_X1MOUSE:
2194 case K_X1RELEASE:
2195 case K_X2MOUSE:
2196 case K_X2RELEASE:
2197
2198 case K_MOUSEUP:
2199 case K_MOUSEDOWN:
2200 case K_MOUSELEFT:
2201 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002202 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002203 int row = mouse_row;
2204 int col = mouse_col;
2205
2206#ifdef FEAT_PROP_POPUP
2207 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002208 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002209 row -= popup_top_extra(curwin);
2210 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002211 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002212#endif
2213 if (row < W_WINROW(curwin)
2214 || row >= (W_WINROW(curwin) + curwin->w_height)
2215 || col < curwin->w_wincol
2216 || col >= W_ENDCOL(curwin)
2217 || dragging_outside)
2218 {
2219 // click or scroll outside the current window or on status
2220 // line or vertical separator
2221 if (typed)
2222 {
2223 stuffcharReadbuff(c);
2224 mouse_was_outside = TRUE;
2225 }
2226 return FAIL;
2227 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002228 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002229 break;
2230
2231 case K_COMMAND:
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002232 case K_SCRIPT_COMMAND:
2233 return do_cmdkey_command(c, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002234 }
2235 if (typed)
2236 mouse_was_outside = FALSE;
2237
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002238 // Convert the typed key to a sequence of bytes for the job.
2239 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002240 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002241 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002242 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2243 (char_u *)msg, (int)len, NULL);
2244
2245 return OK;
2246}
2247
2248 static void
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002249position_cursor(win_T *wp, VTermPos *pos)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002250{
2251 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2252 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002253#ifdef FEAT_PROP_POPUP
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002254 if (popup_is_popup(wp))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002255 {
Bram Moolenaarf5452692020-11-28 21:56:06 +01002256 wp->w_wrow += popup_top_extra(wp);
2257 wp->w_wcol += popup_left_extra(wp);
Bram Moolenaar6a076442020-11-15 20:32:58 +01002258 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002259 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01002260 else
2261 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002262#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002263 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2264}
2265
2266/*
2267 * Handle CTRL-W "": send register contents to the job.
2268 */
2269 static void
2270term_paste_register(int prev_c UNUSED)
2271{
2272 int c;
2273 list_T *l;
2274 listitem_T *item;
2275 long reglen = 0;
2276 int type;
2277
2278#ifdef FEAT_CMDL_INFO
2279 if (add_to_showcmd(prev_c))
2280 if (add_to_showcmd('"'))
2281 out_flush();
2282#endif
2283 c = term_vgetc();
2284#ifdef FEAT_CMDL_INFO
2285 clear_showcmd();
2286#endif
2287 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002288 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002289 return;
2290
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002291 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002292 if (c == '=' && get_expr_register() != '=')
2293 return;
2294 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002295 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002296 return;
2297
2298 l = (list_T *)get_reg_contents(c, GREG_LIST);
2299 if (l != NULL)
2300 {
2301 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002302 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002304 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002305#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002306 char_u *tmp = s;
2307
2308 if (!enc_utf8 && enc_codepage > 0)
2309 {
2310 WCHAR *ret = NULL;
2311 int length = 0;
2312
2313 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2314 (int)STRLEN(s), &ret, &length);
2315 if (ret != NULL)
2316 {
2317 WideCharToMultiByte_alloc(CP_UTF8, 0,
2318 ret, length, (char **)&s, &length, 0, 0);
2319 vim_free(ret);
2320 }
2321 }
2322#endif
2323 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2324 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002325#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002326 if (tmp != s)
2327 vim_free(s);
2328#endif
2329
2330 if (item->li_next != NULL || type == MLINE)
2331 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2332 (char_u *)"\r", 1, NULL);
2333 }
2334 list_free(l);
2335 }
2336}
2337
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002338/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002339 * Return TRUE when waiting for a character in the terminal, the cursor of the
2340 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002341 */
2342 int
2343terminal_is_active()
2344{
2345 return in_terminal_loop != NULL;
2346}
2347
Bram Moolenaar83d47902020-03-26 20:34:00 +01002348/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002349 * Return the highight group ID for the terminal and the window.
Bram Moolenaar83d47902020-03-26 20:34:00 +01002350 */
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002351 static int
2352term_get_highlight_id(term_T *term, win_T *wp)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002353{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002354 char_u *name;
2355
2356 if (wp != NULL && *wp->w_p_wcr != NUL)
2357 name = wp->w_p_wcr;
2358 else if (term->tl_highlight_name != NULL)
2359 name = term->tl_highlight_name;
2360 else
2361 name = (char_u*)"Terminal";
2362
2363 return syn_name2id(name);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002364}
2365
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002366#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002367 cursorentry_T *
2368term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2369{
2370 term_T *term = in_terminal_loop;
2371 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002372 int id;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002373 guicolor_T term_fg = INVALCOLOR;
2374 guicolor_T term_bg = INVALCOLOR;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002375
Bram Moolenaara80faa82020-04-12 19:37:17 +02002376 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002377 entry.shape = entry.mshape =
2378 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2379 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2380 SHAPE_BLOCK;
2381 entry.percentage = 20;
2382 if (term->tl_cursor_blink)
2383 {
2384 entry.blinkwait = 700;
2385 entry.blinkon = 400;
2386 entry.blinkoff = 250;
2387 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002388
Bram Moolenaar83d47902020-03-26 20:34:00 +01002389 // The highlight group overrules the defaults.
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002390 id = term_get_highlight_id(term, curwin);
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002391 if (id != 0)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002392 syn_id2colors(id, &term_fg, &term_bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002393 if (term_bg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002394 *fg = term_bg;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002395 else
2396 *fg = gui.back_pixel;
2397
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002398 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002399 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002400 if (term_fg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002401 *bg = term_fg;
2402 else
2403 *bg = gui.norm_pixel;
2404 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002405 else
2406 *bg = color_name2handle(term->tl_cursor_color);
2407 entry.name = "n";
2408 entry.used_for = SHAPE_CURSOR;
2409
2410 return &entry;
2411}
2412#endif
2413
Bram Moolenaard317b382018-02-08 22:33:31 +01002414 static void
2415may_output_cursor_props(void)
2416{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002417 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002418 || last_set_cursor_shape != desired_cursor_shape
2419 || last_set_cursor_blink != desired_cursor_blink)
2420 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002421 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002422 last_set_cursor_shape = desired_cursor_shape;
2423 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002424 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002425 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002426 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002427 ui_cursor_shape_forced(TRUE);
2428 else
2429 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2430 }
2431}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002432
Bram Moolenaard317b382018-02-08 22:33:31 +01002433/*
2434 * Set the cursor color and shape, if not last set to these.
2435 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002436 static void
2437may_set_cursor_props(term_T *term)
2438{
2439#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002440 // For the GUI the cursor properties are obtained with
2441 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002442 if (gui.in_use)
2443 return;
2444#endif
2445 if (in_terminal_loop == term)
2446 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002447 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002448 desired_cursor_shape = term->tl_cursor_shape;
2449 desired_cursor_blink = term->tl_cursor_blink;
2450 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002451 }
2452}
2453
Bram Moolenaard317b382018-02-08 22:33:31 +01002454/*
2455 * Reset the desired cursor properties and restore them when needed.
2456 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002457 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002458prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002459{
2460#ifdef FEAT_GUI
2461 if (gui.in_use)
2462 return;
2463#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002464 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002465 desired_cursor_shape = -1;
2466 desired_cursor_blink = -1;
2467 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002468}
2469
2470/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002471 * Returns TRUE if the current window contains a terminal and we are sending
2472 * keys to the job.
2473 * If "check_job_status" is TRUE update the job status.
2474 */
2475 static int
2476term_use_loop_check(int check_job_status)
2477{
2478 term_T *term = curbuf->b_term;
2479
2480 return term != NULL
2481 && !term->tl_normal_mode
2482 && term->tl_vterm != NULL
2483 && term_job_running_check(term, check_job_status);
2484}
2485
2486/*
2487 * Returns TRUE if the current window contains a terminal and we are sending
2488 * keys to the job.
2489 */
2490 int
2491term_use_loop(void)
2492{
2493 return term_use_loop_check(FALSE);
2494}
2495
2496/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002497 * Called when entering a window with the mouse. If this is a terminal window
2498 * we may want to change state.
2499 */
2500 void
2501term_win_entered()
2502{
2503 term_T *term = curbuf->b_term;
2504
2505 if (term != NULL)
2506 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002507 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002508 {
2509 reset_VIsual_and_resel();
2510 if (State & INSERT)
2511 stop_insert_mode = TRUE;
2512 }
2513 mouse_was_outside = FALSE;
2514 enter_mouse_col = mouse_col;
2515 enter_mouse_row = mouse_row;
2516 }
2517}
2518
Bram Moolenaara48d4e42021-12-08 22:13:38 +00002519 void
2520term_focus_change(int in_focus)
2521{
2522 term_T *term = curbuf->b_term;
2523
2524 if (term != NULL && term->tl_vterm != NULL)
2525 {
2526 VTermState *state = vterm_obtain_state(term->tl_vterm);
2527
2528 if (in_focus)
2529 vterm_state_focus_in(state);
2530 else
2531 vterm_state_focus_out(state);
2532 term_forward_output(term);
2533 }
2534}
2535
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002536/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002537 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2538 * Return the Ctrl-key value in that case.
2539 */
2540 static int
2541raw_c_to_ctrl(int c)
2542{
2543 if ((mod_mask & MOD_MASK_CTRL)
2544 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2545 return c & 0x1f;
2546 return c;
2547}
2548
2549/*
2550 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2551 * May set "mod_mask".
2552 */
2553 static int
2554ctrl_to_raw_c(int c)
2555{
2556 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2557 {
2558 mod_mask |= MOD_MASK_CTRL;
2559 return c + '@';
2560 }
2561 return c;
2562}
2563
2564/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002565 * Wait for input and send it to the job.
2566 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2567 * when there is no more typahead.
2568 * Return when the start of a CTRL-W command is typed or anything else that
2569 * should be handled as a Normal mode command.
2570 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2571 * the terminal was closed.
2572 */
2573 int
2574terminal_loop(int blocking)
2575{
2576 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002577 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002578 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002579 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002580#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002581 int tty_fd = curbuf->b_term->tl_job->jv_channel
2582 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002583#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002584 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002585
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002586 // Remember the terminal we are sending keys to. However, the terminal
2587 // might be closed while waiting for a character, e.g. typing "exit" in a
2588 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2589 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002590 in_terminal_loop = curbuf->b_term;
2591
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002592 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002593 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002594 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002595 if (termwinkey == Ctrl_W)
2596 termwinkey = 0;
2597 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002598 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002599 may_set_cursor_props(curbuf->b_term);
2600
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002601 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002602 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002603#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002604 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002605#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002606 // TODO: skip screen update when handling a sequence of keys.
2607 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002608 while (must_redraw != 0)
2609 if (update_screen(0) == FAIL)
2610 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002611 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002612 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002613 break;
2614
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002615 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002616 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002617
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002618 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002619 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002620 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002621 // Job finished while waiting for a character. Push back the
2622 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002623 if (raw_c != K_IGNORE)
2624 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002625 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002626 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002627 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002628 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002629 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002630
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002631#ifdef UNIX
2632 /*
2633 * The shell or another program may change the tty settings. Getting
2634 * them for every typed character is a bit of overhead, but it's needed
2635 * for the first character typed, e.g. when Vim starts in a shell.
2636 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002637 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002638 {
2639 ttyinfo_T info;
2640
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002641 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002642 if (get_tty_info(tty_fd, &info) == OK)
2643 term_backspace_char = info.backspace;
2644 }
2645#endif
2646
Bram Moolenaar4f974752019-02-17 17:44:42 +01002647#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002648 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2649 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002650 if (ctrl_break_was_pressed)
2651 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2652#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002653 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2654 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002655 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002656#ifdef FEAT_GUI
2657 && !curbuf->b_term->tl_system
2658#endif
2659 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002660 {
2661 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002662 int prev_raw_c = raw_c;
2663 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002664
2665#ifdef FEAT_CMDL_INFO
2666 if (add_to_showcmd(c))
2667 out_flush();
2668#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002669 raw_c = term_vgetc();
2670 c = raw_c_to_ctrl(raw_c);
2671
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002672#ifdef FEAT_CMDL_INFO
2673 clear_showcmd();
2674#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002675 if (!term_use_loop_check(TRUE)
2676 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002677 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002678 break;
2679
2680 if (prev_c == Ctrl_BSL)
2681 {
2682 if (c == Ctrl_N)
2683 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002684 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002685 term_enter_normal_mode();
2686 ret = FAIL;
2687 goto theend;
2688 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002689 // Send both keys to the terminal, first one here, second one
2690 // below.
2691 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2692 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002693 }
2694 else if (c == Ctrl_C)
2695 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002696 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002697 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2698 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002699 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002700 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002701 // "CTRL-W .": send CTRL-W to the job
2702 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002703 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002704 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002705 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002706 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002707 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002708 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002709 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002710 else if (c == 'N')
2711 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002712 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002713 term_enter_normal_mode();
2714 ret = FAIL;
2715 goto theend;
2716 }
2717 else if (c == '"')
2718 {
2719 term_paste_register(prev_c);
2720 continue;
2721 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002722 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002723 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002724 // space for CTRL-W, modifier, multi-byte char and NUL
2725 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002726
2727 // Put the command into the typeahead buffer, when using the
2728 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2729 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002730 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002731 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002732 ret = OK;
2733 goto theend;
2734 }
2735 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002736# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002737 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002738 {
2739 WCHAR wc;
2740 char_u mb[3];
2741
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002742 mb[0] = (unsigned)raw_c >> 8;
2743 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002744 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002745 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002746 }
2747# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002748 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002749 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002750 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002751 // We are sure to come back here, don't reset the cursor color
2752 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002753 restore_cursor = FALSE;
2754
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002755 ret = OK;
2756 goto theend;
2757 }
2758 }
2759 ret = FAIL;
2760
2761theend:
2762 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002763 if (restore_cursor)
2764 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002765
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002766 // Move a snapshot of the screen contents to the buffer, so that completion
2767 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002768 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2769 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002770
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002771 return ret;
2772}
2773
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002774 static void
2775may_toggle_cursor(term_T *term)
2776{
2777 if (in_terminal_loop == term)
2778 {
2779 if (term->tl_cursor_visible)
2780 cursor_on();
2781 else
2782 cursor_off();
2783 }
2784}
2785
2786/*
2787 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002788 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002789 */
2790 static int
2791color2index(VTermColor *color, int fg, int *boldp)
2792{
2793 int red = color->red;
2794 int blue = color->blue;
2795 int green = color->green;
2796
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002797 if (VTERM_COLOR_IS_INVALID(color))
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002798 return 0;
2799 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002800 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002801 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002802 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002803 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002804 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002805 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2806 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2807 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002808 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002809 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2810 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2811 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2812 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2813 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2814 case 10: return lookup_color(20, fg, boldp) + 1; // red
2815 case 11: return lookup_color(16, fg, boldp) + 1; // green
2816 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2817 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2818 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2819 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2820 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002821 }
2822 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002823
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002824 if (t_colors >= 256)
2825 {
2826 if (red == blue && red == green)
2827 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002828 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002829 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002830 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2831 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2832 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002833 int i;
2834
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002835 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002836 return 17; // 00/00/00
2837 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002838 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002839 for (i = 0; i < 23; ++i)
2840 if (red < cutoff[i])
2841 return i + 233;
2842 return 256;
2843 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002844 {
2845 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2846 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002847
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002848 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002849 for (ri = 0; ri < 5; ++ri)
2850 if (red < cutoff[ri])
2851 break;
2852 for (gi = 0; gi < 5; ++gi)
2853 if (green < cutoff[gi])
2854 break;
2855 for (bi = 0; bi < 5; ++bi)
2856 if (blue < cutoff[bi])
2857 break;
2858 return 17 + ri * 36 + gi * 6 + bi;
2859 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002860 }
2861 return 0;
2862}
2863
2864/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002865 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002866 */
2867 static int
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002868vtermAttr2hl(VTermScreenCellAttrs *cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002869{
2870 int attr = 0;
2871
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002872 if (cellattrs->bold)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002873 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002874 if (cellattrs->underline)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002875 attr |= HL_UNDERLINE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002876 if (cellattrs->italic)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002877 attr |= HL_ITALIC;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002878 if (cellattrs->strike)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002879 attr |= HL_STRIKETHROUGH;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002880 if (cellattrs->reverse)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002881 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002882 return attr;
2883}
2884
2885/*
2886 * Store Vterm attributes in "cell" from highlight flags.
2887 */
2888 static void
2889hl2vtermAttr(int attr, cellattr_T *cell)
2890{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002891 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002892 if (attr & HL_BOLD)
2893 cell->attrs.bold = 1;
2894 if (attr & HL_UNDERLINE)
2895 cell->attrs.underline = 1;
2896 if (attr & HL_ITALIC)
2897 cell->attrs.italic = 1;
2898 if (attr & HL_STRIKETHROUGH)
2899 cell->attrs.strike = 1;
2900 if (attr & HL_INVERSE)
2901 cell->attrs.reverse = 1;
2902}
2903
2904/*
2905 * Convert the attributes of a vterm cell into an attribute index.
2906 */
2907 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002908cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002909 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002910 win_T *wp,
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002911 VTermScreenCellAttrs *cellattrs,
2912 VTermColor *cellfg,
2913 VTermColor *cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002914{
2915 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002916 VTermColor *fg = cellfg;
2917 VTermColor *bg = cellbg;
2918 int is_default_fg = VTERM_COLOR_IS_DEFAULT_FG(fg);
2919 int is_default_bg = VTERM_COLOR_IS_DEFAULT_BG(bg);
2920
2921 if (is_default_fg || is_default_bg)
2922 {
2923 if (wp != NULL && *wp->w_p_wcr != NUL)
2924 {
2925 if (is_default_fg)
2926 fg = &wp->w_term_wincolor.fg;
2927 if (is_default_bg)
2928 bg = &wp->w_term_wincolor.bg;
2929 }
2930 else
2931 {
2932 if (is_default_fg)
2933 fg = &term->tl_default_color.fg;
2934 if (is_default_bg)
2935 bg = &term->tl_default_color.bg;
2936 }
2937 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002938
2939#ifdef FEAT_GUI
2940 if (gui.in_use)
2941 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002942 guicolor_T guifg = gui_mch_get_rgb_color(fg->red, fg->green, fg->blue);
2943 guicolor_T guibg = gui_mch_get_rgb_color(bg->red, bg->green, bg->blue);
2944 return get_gui_attr_idx(attr, guifg, guibg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002945 }
2946 else
2947#endif
2948#ifdef FEAT_TERMGUICOLORS
2949 if (p_tgc)
2950 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002951 guicolor_T tgcfg = VTERM_COLOR_IS_INVALID(fg)
2952 ? INVALCOLOR
2953 : gui_get_rgb_color_cmn(fg->red, fg->green, fg->blue);
2954 guicolor_T tgcbg = VTERM_COLOR_IS_INVALID(bg)
2955 ? INVALCOLOR
2956 : gui_get_rgb_color_cmn(bg->red, bg->green, bg->blue);
2957 return get_tgc_attr_idx(attr, tgcfg, tgcbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002958 }
2959 else
2960#endif
2961 {
2962 int bold = MAYBE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002963 int ctermfg = color2index(fg, TRUE, &bold);
2964 int ctermbg = color2index(bg, FALSE, &bold);
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002965
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002966 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002967 if (bold == TRUE)
2968 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002969
2970 return get_cterm_attr_idx(attr, ctermfg, ctermbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002971 }
2972 return 0;
2973}
2974
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002975 static void
2976set_dirty_snapshot(term_T *term)
2977{
2978 term->tl_dirty_snapshot = TRUE;
2979#ifdef FEAT_TIMERS
2980 if (!term->tl_normal_mode)
2981 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002982 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002983 profile_setlimit(100L, &term->tl_timer_due);
2984 term->tl_timer_set = TRUE;
2985 }
2986#endif
2987}
2988
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002989 static int
2990handle_damage(VTermRect rect, void *user)
2991{
2992 term_T *term = (term_T *)user;
2993
2994 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2995 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002996 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002997 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002998 return 1;
2999}
3000
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003001 static void
3002term_scroll_up(term_T *term, int start_row, int count)
3003{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003004 win_T *wp = NULL;
3005 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003006 VTermColor fg, bg;
3007 VTermScreenCellAttrs attr;
3008 int clear_attr;
3009
Bram Moolenaara80faa82020-04-12 19:37:17 +02003010 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003011
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003012 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003013 {
3014 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003015 {
3016 // Set the color to clear lines with.
3017 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
3018 &fg, &bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003019 clear_attr = cell2attr(term, wp, &attr, &fg, &bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003020 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003021 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003022 }
3023}
3024
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003025 static int
3026handle_moverect(VTermRect dest, VTermRect src, void *user)
3027{
3028 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003029 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003030
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003031 // Scrolling up is done much more efficiently by deleting lines instead of
3032 // redrawing the text. But avoid doing this multiple times, postpone until
3033 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003034 if (dest.start_col == src.start_col
3035 && dest.end_col == src.end_col
3036 && dest.start_row < src.start_row)
3037 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003038 if (dest.start_row == 0)
3039 term->tl_postponed_scroll += count;
3040 else
3041 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003042 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003043
3044 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3045 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003046 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003047
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003048 // Note sure if the scrolling will work correctly, let's do a complete
3049 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003050 redraw_buf_later(term->tl_buffer, NOT_VALID);
3051 return 1;
3052}
3053
3054 static int
3055handle_movecursor(
3056 VTermPos pos,
3057 VTermPos oldpos UNUSED,
3058 int visible,
3059 void *user)
3060{
3061 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003062 win_T *wp = NULL;
3063 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003064
3065 term->tl_cursor_pos = pos;
3066 term->tl_cursor_visible = visible;
3067
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003068 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003069 {
3070 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003071 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003072 }
3073 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003074 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003075
3076 return 1;
3077}
3078
3079 static int
3080handle_settermprop(
3081 VTermProp prop,
3082 VTermValue *value,
3083 void *user)
3084{
3085 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003086 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003087
3088 switch (prop)
3089 {
3090 case VTERM_PROP_TITLE:
ichizokae1bd872022-01-20 14:57:29 +00003091 if (disable_vterm_title_for_testing)
3092 break;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003093 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003094 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003095 if (strval == NULL)
3096 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003097 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003098 // a blank title isn't useful, make it empty, so that "running" is
3099 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003100 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003101 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003102 // Same as blank
3103 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003104 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003105 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3106 term->tl_title = NULL;
3107 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003108 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003109 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003110#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003111 else if (!enc_utf8 && enc_codepage > 0)
3112 {
3113 WCHAR *ret = NULL;
3114 int length = 0;
3115
3116 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003117 (char*)value->string.str,
3118 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003119 if (ret != NULL)
3120 {
3121 WideCharToMultiByte_alloc(enc_codepage, 0,
3122 ret, length, (char**)&term->tl_title,
3123 &length, 0, 0);
3124 vim_free(ret);
3125 }
3126 }
3127#endif
3128 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003129 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003130 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003131 strval = NULL;
3132 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003133 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003134 if (term == curbuf->b_term)
3135 maketitle();
3136 break;
3137
3138 case VTERM_PROP_CURSORVISIBLE:
3139 term->tl_cursor_visible = value->boolean;
3140 may_toggle_cursor(term);
3141 out_flush();
3142 break;
3143
3144 case VTERM_PROP_CURSORBLINK:
3145 term->tl_cursor_blink = value->boolean;
3146 may_set_cursor_props(term);
3147 break;
3148
3149 case VTERM_PROP_CURSORSHAPE:
3150 term->tl_cursor_shape = value->number;
3151 may_set_cursor_props(term);
3152 break;
3153
3154 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003155 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003156 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003157 if (strval == NULL)
3158 break;
3159 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003160 may_set_cursor_props(term);
3161 break;
3162
3163 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003164 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003165 term->tl_using_altscreen = value->boolean;
3166 break;
3167
3168 default:
3169 break;
3170 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003171 vim_free(strval);
3172
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003173 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003174 return 1;
3175}
3176
3177/*
3178 * The job running in the terminal resized the terminal.
3179 */
3180 static int
3181handle_resize(int rows, int cols, void *user)
3182{
3183 term_T *term = (term_T *)user;
3184 win_T *wp;
3185
3186 term->tl_rows = rows;
3187 term->tl_cols = cols;
3188 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003189 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003190 term->tl_vterm_size_changed = FALSE;
3191 else
3192 {
3193 FOR_ALL_WINDOWS(wp)
3194 {
3195 if (wp->w_buffer == term->tl_buffer)
3196 {
3197 win_setheight_win(rows, wp);
3198 win_setwidth_win(cols, wp);
3199 }
3200 }
3201 redraw_buf_later(term->tl_buffer, NOT_VALID);
3202 }
3203 return 1;
3204}
3205
3206/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003207 * If the number of lines that are stored goes over 'termscrollback' then
3208 * delete the first 10%.
3209 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3210 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003211 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003212 static void
3213limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003214{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003215 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003216 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003217 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003218 int i;
3219
3220 curbuf = term->tl_buffer;
3221 for (i = 0; i < todo; ++i)
3222 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003223 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3224 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003225 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003226 }
3227 curbuf = curwin->w_buffer;
3228
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003229 gap->ga_len -= todo;
3230 mch_memmove(gap->ga_data,
3231 (sb_line_T *)gap->ga_data + todo,
3232 sizeof(sb_line_T) * gap->ga_len);
3233 if (update_buffer)
3234 term->tl_scrollback_scrolled -= todo;
3235 }
3236}
3237
3238/*
3239 * Handle a line that is pushed off the top of the screen.
3240 */
3241 static int
3242handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3243{
3244 term_T *term = (term_T *)user;
3245 garray_T *gap;
3246 int update_buffer;
3247
3248 if (term->tl_normal_mode)
3249 {
3250 // In Terminal-Normal mode the user interacts with the buffer, thus we
3251 // must not change it. Postpone adding the scrollback lines.
3252 gap = &term->tl_scrollback_postponed;
3253 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003254 }
3255 else
3256 {
3257 // First remove the lines that were appended before, the pushed line
3258 // goes above it.
3259 cleanup_scrollback(term);
3260 gap = &term->tl_scrollback;
3261 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003262 }
3263
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003264 limit_scrollback(term, gap, update_buffer);
3265
3266 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003267 {
3268 cellattr_T *p = NULL;
3269 int len = 0;
3270 int i;
3271 int c;
3272 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003273 int text_len;
3274 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003275 sb_line_T *line;
3276 garray_T ga;
3277 cellattr_T fill_attr = term->tl_default_color;
3278
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003279 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003280 for (i = 0; i < cols; ++i)
3281 if (cells[i].chars[0] != 0)
3282 len = i + 1;
3283 else
3284 cell2cellattr(&cells[i], &fill_attr);
3285
3286 ga_init2(&ga, 1, 100);
3287 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003288 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003289 if (p != NULL)
3290 {
3291 for (col = 0; col < len; col += cells[col].width)
3292 {
3293 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3294 {
3295 ga.ga_len = 0;
3296 break;
3297 }
3298 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3299 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3300 (char_u *)ga.ga_data + ga.ga_len);
3301 cell2cellattr(&cells[col], &p[col]);
3302 }
3303 }
3304 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003305 {
3306 if (update_buffer)
3307 text = (char_u *)"";
3308 else
3309 text = vim_strsave((char_u *)"");
3310 text_len = 0;
3311 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003312 else
3313 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003314 text = ga.ga_data;
3315 text_len = ga.ga_len;
3316 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003317 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003318 if (update_buffer)
3319 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003320
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003321 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003322 line->sb_cols = len;
3323 line->sb_cells = p;
3324 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003325 if (update_buffer)
3326 {
3327 line->sb_text = NULL;
3328 ++term->tl_scrollback_scrolled;
3329 ga_clear(&ga); // free the text
3330 }
3331 else
3332 {
3333 line->sb_text = text;
3334 ga_init(&ga); // text is kept in tl_scrollback_postponed
3335 }
3336 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003337 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003338 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003339}
3340
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003341/*
3342 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3343 * received and stored in tl_scrollback_postponed.
3344 */
3345 static void
3346handle_postponed_scrollback(term_T *term)
3347{
3348 int i;
3349
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003350 if (term->tl_scrollback_postponed.ga_len == 0)
3351 return;
3352 ch_log(NULL, "Moving postponed scrollback to scrollback");
3353
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003354 // First remove the lines that were appended before, the pushed lines go
3355 // above it.
3356 cleanup_scrollback(term);
3357
3358 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3359 {
3360 char_u *text;
3361 sb_line_T *pp_line;
3362 sb_line_T *line;
3363
3364 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3365 break;
3366 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3367
3368 text = pp_line->sb_text;
3369 if (text == NULL)
3370 text = (char_u *)"";
3371 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3372 vim_free(pp_line->sb_text);
3373
3374 line = (sb_line_T *)term->tl_scrollback.ga_data
3375 + term->tl_scrollback.ga_len;
3376 line->sb_cols = pp_line->sb_cols;
3377 line->sb_cells = pp_line->sb_cells;
3378 line->sb_fill_attr = pp_line->sb_fill_attr;
3379 line->sb_text = NULL;
3380 ++term->tl_scrollback_scrolled;
3381 ++term->tl_scrollback.ga_len;
3382 }
3383
3384 ga_clear(&term->tl_scrollback_postponed);
3385 limit_scrollback(term, &term->tl_scrollback, TRUE);
3386}
3387
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003388static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003389 handle_damage, // damage
3390 handle_moverect, // moverect
3391 handle_movecursor, // movecursor
3392 handle_settermprop, // settermprop
3393 NULL, // bell
3394 handle_resize, // resize
3395 handle_pushline, // sb_pushline
3396 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003397};
3398
3399/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003400 * Do the work after the channel of a terminal was closed.
3401 * Must be called only when updating_screen is FALSE.
3402 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3403 */
3404 static int
3405term_after_channel_closed(term_T *term)
3406{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003407 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003408 if (!term->tl_normal_mode)
3409 {
3410 int fnum = term->tl_buffer->b_fnum;
3411
3412 cleanup_vterm(term);
3413
3414 if (term->tl_finish == TL_FINISH_CLOSE)
3415 {
3416 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003417 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003418#ifdef FEAT_PROP_POPUP
3419 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003420
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003421 // If this was a terminal in a popup window, go back to the
3422 // previous window.
3423 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3424 {
3425 pwin = curwin;
3426 if (win_valid(prevwin))
3427 win_enter(prevwin, FALSE);
3428 }
3429 else
3430#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003431 // If this is the last normal window: exit Vim.
3432 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3433 {
3434 exarg_T ea;
3435
Bram Moolenaara80faa82020-04-12 19:37:17 +02003436 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003437 ex_quit(&ea);
3438 return TRUE;
3439 }
3440
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003441 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003442 ch_log(NULL, "terminal job finished, closing window");
3443 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003444 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003445 if (curwin == aucmd_win)
3446 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003447 if (do_set_w_closing)
3448 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003449 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003450 if (do_set_w_closing)
3451 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003452 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003453#ifdef FEAT_PROP_POPUP
3454 if (pwin != NULL)
3455 popup_close_with_retval(pwin, 0);
3456#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003457 return TRUE;
3458 }
3459 if (term->tl_finish == TL_FINISH_OPEN
3460 && term->tl_buffer->b_nwindows == 0)
3461 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003462 char *cmd = term->tl_opencmd == NULL
3463 ? "botright sbuf %d"
3464 : (char *)term->tl_opencmd;
3465 size_t len = strlen(cmd) + 50;
3466 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003467
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003468 if (buf != NULL)
3469 {
3470 ch_log(NULL, "terminal job finished, opening window");
3471 vim_snprintf(buf, len, cmd, fnum);
3472 do_cmdline_cmd((char_u *)buf);
3473 vim_free(buf);
3474 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003475 }
3476 else
3477 ch_log(NULL, "terminal job finished");
3478 }
3479
3480 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3481 return FALSE;
3482}
3483
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003484#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3485/*
3486 * If the current window is a terminal in a popup window and the job has
3487 * finished, close the popup window and to back to the previous window.
3488 * Otherwise return FAIL.
3489 */
3490 int
3491may_close_term_popup(void)
3492{
3493 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3494 && !term_job_running(curbuf->b_term))
3495 {
3496 win_T *pwin = curwin;
3497
3498 if (win_valid(prevwin))
3499 win_enter(prevwin, FALSE);
3500 popup_close_with_retval(pwin, 0);
3501 return OK;
3502 }
3503 return FAIL;
3504}
3505#endif
3506
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003507/*
Bram Moolenaareea32af2021-11-21 14:51:13 +00003508 * Called when a channel is going to be closed, before invoking the close
3509 * callback.
3510 */
3511 void
3512term_channel_closing(channel_T *ch)
3513{
3514 term_T *term;
3515
3516 for (term = first_term; term != NULL; term = term->tl_next)
3517 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
3518 term->tl_channel_closing = TRUE;
3519}
3520
3521/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003522 * Called when a channel has been closed.
3523 * If this was a channel for a terminal window then finish it up.
3524 */
3525 void
3526term_channel_closed(channel_T *ch)
3527{
3528 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003529 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003530 int did_one = FALSE;
3531
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003532 for (term = first_term; term != NULL; term = next_term)
3533 {
3534 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003535 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003536 {
3537 term->tl_channel_closed = TRUE;
3538 did_one = TRUE;
3539
Bram Moolenaard23a8232018-02-10 18:45:26 +01003540 VIM_CLEAR(term->tl_title);
3541 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003542#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003543 if (term->tl_out_fd != NULL)
3544 {
3545 fclose(term->tl_out_fd);
3546 term->tl_out_fd = NULL;
3547 }
3548#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003549
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003550 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003551 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003552 // Cannot open or close windows now. Can happen when
3553 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003554 term->tl_channel_recently_closed = TRUE;
3555 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003556 }
3557
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003558 if (term_after_channel_closed(term))
3559 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003560 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003561 }
3562
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003563 if (did_one)
3564 {
3565 redraw_statuslines();
3566
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003567 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003568 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003569 typebuf_was_filled = TRUE;
3570
3571 term = curbuf->b_term;
3572 if (term != NULL)
3573 {
3574 if (term->tl_job == ch->ch_job)
3575 maketitle();
3576 update_cursor(term, term->tl_cursor_visible);
3577 }
3578 }
3579}
3580
3581/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003582 * To be called after resetting updating_screen: handle any terminal where the
3583 * channel was closed.
3584 */
3585 void
3586term_check_channel_closed_recently()
3587{
3588 term_T *term;
3589 term_T *next_term;
3590
3591 for (term = first_term; term != NULL; term = next_term)
3592 {
3593 next_term = term->tl_next;
3594 if (term->tl_channel_recently_closed)
3595 {
3596 term->tl_channel_recently_closed = FALSE;
3597 if (term_after_channel_closed(term))
3598 // start over, the list may have changed
3599 next_term = first_term;
3600 }
3601 }
3602}
3603
3604/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003605 * Fill one screen line from a line of the terminal.
3606 * Advances "pos" to past the last column.
3607 */
3608 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003609term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003610 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003611 win_T *wp,
3612 VTermScreen *screen,
3613 VTermPos *pos,
3614 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003615{
3616 int off = screen_get_current_line_off();
3617
3618 for (pos->col = 0; pos->col < max_col; )
3619 {
3620 VTermScreenCell cell;
3621 int c;
3622
3623 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003624 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003625
3626 c = cell.chars[0];
3627 if (c == NUL)
3628 {
3629 ScreenLines[off] = ' ';
3630 if (enc_utf8)
3631 ScreenLinesUC[off] = NUL;
3632 }
3633 else
3634 {
3635 if (enc_utf8)
3636 {
3637 int i;
3638
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003639 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003640 for (i = 0; i < Screen_mco
3641 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3642 {
3643 ScreenLinesC[i][off] = cell.chars[i + 1];
3644 if (cell.chars[i + 1] == 0)
3645 break;
3646 }
3647 if (c >= 0x80 || (Screen_mco > 0
3648 && ScreenLinesC[0][off] != 0))
3649 {
3650 ScreenLines[off] = ' ';
3651 ScreenLinesUC[off] = c;
3652 }
3653 else
3654 {
3655 ScreenLines[off] = c;
3656 ScreenLinesUC[off] = NUL;
3657 }
3658 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003659#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003660 else if (has_mbyte && c >= 0x80)
3661 {
3662 char_u mb[MB_MAXBYTES+1];
3663 WCHAR wc = c;
3664
3665 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3666 (char*)mb, 2, 0, 0) > 1)
3667 {
3668 ScreenLines[off] = mb[0];
3669 ScreenLines[off + 1] = mb[1];
3670 cell.width = mb_ptr2cells(mb);
3671 }
3672 else
3673 ScreenLines[off] = c;
3674 }
3675#endif
3676 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003677 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003678 ScreenLines[off] = c;
3679 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003680 ScreenAttrs[off] = cell2attr(term, wp, &cell.attrs, &cell.fg,
3681 &cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003682
3683 ++pos->col;
3684 ++off;
3685 if (cell.width == 2)
3686 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003687 // don't set the second byte to NUL for a DBCS encoding, it
3688 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003689 if (enc_utf8)
3690 {
3691 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003692 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003693 }
3694 else if (!has_mbyte)
3695 {
3696 // Can't show a double-width character with a single-byte
3697 // 'encoding', just use a space.
3698 ScreenLines[off] = ' ';
3699 ScreenAttrs[off] = ScreenAttrs[off - 1];
3700 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003701
3702 ++pos->col;
3703 ++off;
3704 }
3705 }
3706}
3707
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003708#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003709 static void
3710update_system_term(term_T *term)
3711{
3712 VTermPos pos;
3713 VTermScreen *screen;
3714
3715 if (term->tl_vterm == NULL)
3716 return;
3717 screen = vterm_obtain_screen(term->tl_vterm);
3718
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003719 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003720 while (term->tl_toprow > 0
3721 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3722 {
3723 int save_p_more = p_more;
3724
3725 p_more = FALSE;
3726 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003727 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003728 p_more = save_p_more;
3729 --term->tl_toprow;
3730 }
3731
3732 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3733 && pos.row < Rows; ++pos.row)
3734 {
3735 if (pos.row < term->tl_rows)
3736 {
3737 int max_col = MIN(Columns, term->tl_cols);
3738
Bram Moolenaar83d47902020-03-26 20:34:00 +01003739 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003740 }
3741 else
3742 pos.col = 0;
3743
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003744 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003745 }
3746
3747 term->tl_dirty_row_start = MAX_ROW;
3748 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003749}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003750#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003751
3752/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003753 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3754 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003755 * Terminal-Normal mode.
3756 */
3757 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003758term_do_update_window(win_T *wp)
3759{
3760 term_T *term = wp->w_buffer->b_term;
3761
3762 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3763}
3764
3765/*
3766 * Called to update a window that contains an active terminal.
3767 */
3768 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003769term_update_window(win_T *wp)
3770{
3771 term_T *term = wp->w_buffer->b_term;
3772 VTerm *vterm;
3773 VTermScreen *screen;
3774 VTermState *state;
3775 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003776 int rows, cols;
3777 int newrows, newcols;
3778 int minsize;
3779 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003780
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003781 vterm = term->tl_vterm;
3782 screen = vterm_obtain_screen(vterm);
3783 state = vterm_obtain_state(vterm);
3784
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003785 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3786 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003787 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003788 {
3789 term->tl_dirty_row_start = 0;
3790 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003791
3792 if (term->tl_postponed_scroll > 0
3793 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003794 // Scrolling is usually faster than redrawing, when there are only
3795 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003796 term_scroll_up(term, 0, term->tl_postponed_scroll);
3797 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003798 }
3799
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003800 /*
3801 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003802 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003803 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003804 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003805
Bram Moolenaar498c2562018-04-15 23:45:15 +02003806 newrows = 99999;
3807 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003808 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003809 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003810 // Always use curwin, it may be a popup window.
3811 win_T *wwp = twp == NULL ? curwin : twp;
3812
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003813 // When more than one window shows the same terminal, use the
3814 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003815 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003816 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003817 newrows = MIN(newrows, wwp->w_height);
3818 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003819 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003820 if (twp == NULL)
3821 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003822 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003823 if (newrows == 99999 || newcols == 99999)
3824 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003825 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3826 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3827
Bram Moolenaareba13e42021-02-23 17:47:23 +01003828 // If no cell is visible there is no point in resizing. Also, vterm can't
3829 // handle a zero height.
3830 if (newrows == 0 || newcols == 0)
3831 return;
3832
Bram Moolenaar498c2562018-04-15 23:45:15 +02003833 if (term->tl_rows != newrows || term->tl_cols != newcols)
3834 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003835 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003836 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003837 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003838 newrows);
3839 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003840
3841 // Updating the terminal size will cause the snapshot to be cleared.
3842 // When not in terminal_loop() we need to restore it.
3843 if (term != in_terminal_loop)
3844 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003845 }
3846
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003847 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003848 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003849 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003850
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003851 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3852 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003853 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003854 if (pos.row < term->tl_rows)
3855 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003856 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003857
Bram Moolenaar83d47902020-03-26 20:34:00 +01003858 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003859 }
3860 else
3861 pos.col = 0;
3862
Bram Moolenaarf118d482018-03-13 13:14:00 +01003863 screen_line(wp->w_winrow + pos.row
3864#ifdef FEAT_MENU
3865 + winbar_height(wp)
3866#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003867 , wp->w_wincol, pos.col, wp->w_width,
3868#ifdef FEAT_PROP_POPUP
3869 popup_is_popup(wp) ? SLF_POPUP :
3870#endif
3871 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003872 }
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00003873}
3874
3875/*
3876 * Called after updating all windows: may reset dirty rows.
3877 */
3878 void
3879term_did_update_window(win_T *wp)
3880{
3881 term_T *term = wp->w_buffer->b_term;
3882
3883 if (term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode
3884 && wp->w_redr_type == 0)
3885 {
3886 term->tl_dirty_row_start = MAX_ROW;
3887 term->tl_dirty_row_end = 0;
3888 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003889}
3890
3891/*
3892 * Return TRUE if "wp" is a terminal window where the job has finished.
3893 */
3894 int
3895term_is_finished(buf_T *buf)
3896{
3897 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3898}
3899
3900/*
3901 * Return TRUE if "wp" is a terminal window where the job has finished or we
3902 * are in Terminal-Normal mode, thus we show the buffer contents.
3903 */
3904 int
3905term_show_buffer(buf_T *buf)
3906{
3907 term_T *term = buf->b_term;
3908
3909 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3910}
3911
3912/*
3913 * The current buffer is going to be changed. If there is terminal
3914 * highlighting remove it now.
3915 */
3916 void
3917term_change_in_curbuf(void)
3918{
3919 term_T *term = curbuf->b_term;
3920
3921 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3922 {
3923 free_scrollback(term);
3924 redraw_buf_later(term->tl_buffer, NOT_VALID);
3925
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003926 // The buffer is now like a normal buffer, it cannot be easily
3927 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003928 set_string_option_direct((char_u *)"buftype", -1,
3929 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3930 }
3931}
3932
3933/*
3934 * Get the screen attribute for a position in the buffer.
3935 * Use a negative "col" to get the filler background color.
3936 */
3937 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003938term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003939{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003940 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003941 term_T *term = buf->b_term;
3942 sb_line_T *line;
3943 cellattr_T *cellattr;
3944
3945 if (lnum > term->tl_scrollback.ga_len)
3946 cellattr = &term->tl_default_color;
3947 else
3948 {
3949 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3950 if (col < 0 || col >= line->sb_cols)
3951 cellattr = &line->sb_fill_attr;
3952 else
3953 cellattr = line->sb_cells + col;
3954 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003955 return cell2attr(term, wp, &cellattr->attrs, &cellattr->fg, &cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003956}
3957
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003958/*
3959 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003960 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003961 */
3962 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003963cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003964{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003965 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3966 if (rgb->index == 0)
3967 rgb->type = VTERM_COLOR_RGB;
3968 else
3969 {
3970 rgb->type = VTERM_COLOR_INDEXED;
3971 --rgb->index;
3972 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003973}
3974
3975/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003976 * Initialize vterm color from the synID.
3977 * Returns TRUE if color is set to "fg" and "bg".
3978 * Otherwise returns FALSE.
3979 */
3980 static int
3981get_vterm_color_from_synid(int id, VTermColor *fg, VTermColor *bg)
3982{
3983#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3984 // Use the actual color for the GUI and when 'termguicolors' is set.
3985 if (0
3986# ifdef FEAT_GUI
3987 || gui.in_use
3988# endif
3989# ifdef FEAT_TERMGUICOLORS
3990 || p_tgc
3991# ifdef FEAT_VTP
3992 // Finally get INVALCOLOR on this execution path
3993 || (!p_tgc && t_colors >= 256)
3994# endif
3995# endif
3996 )
3997 {
3998 guicolor_T fg_rgb = INVALCOLOR;
3999 guicolor_T bg_rgb = INVALCOLOR;
4000
4001 if (id > 0)
4002 syn_id2colors(id, &fg_rgb, &bg_rgb);
4003
4004 if (fg_rgb != INVALCOLOR)
4005 {
4006 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
4007 fg->red = (unsigned)(rgb >> 16);
4008 fg->green = (unsigned)(rgb >> 8) & 255;
4009 fg->blue = (unsigned)rgb & 255;
4010 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
4011 }
4012 else
4013 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4014
4015 if (bg_rgb != INVALCOLOR)
4016 {
4017 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
4018 bg->red = (unsigned)(rgb >> 16);
4019 bg->green = (unsigned)(rgb >> 8) & 255;
4020 bg->blue = (unsigned)rgb & 255;
4021 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
4022 }
4023 else
4024 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4025
4026 return TRUE;
4027 }
4028 else
4029#endif
4030 if (t_colors >= 16)
4031 {
4032 int cterm_fg = -1;
4033 int cterm_bg = -1;
4034
4035 if (id > 0)
4036 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
4037
4038 if (cterm_fg >= 0)
4039 {
4040 cterm_color2vterm(cterm_fg, fg);
4041 fg->type |= VTERM_COLOR_DEFAULT_FG;
4042 }
4043 else
4044 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4045
4046 if (cterm_bg >= 0)
4047 {
4048 cterm_color2vterm(cterm_bg, bg);
4049 bg->type |= VTERM_COLOR_DEFAULT_BG;
4050 }
4051 else
4052 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4053
4054 return TRUE;
4055 }
4056
4057 return FALSE;
4058}
4059
4060 void
4061term_reset_wincolor(win_T *wp)
4062{
4063 wp->w_term_wincolor.fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4064 wp->w_term_wincolor.bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4065}
4066
4067/*
4068 * Cache the color of 'wincolor'.
4069 */
4070 void
4071term_update_wincolor(win_T *wp)
4072{
4073 int id = 0;
4074
4075 if (*wp->w_p_wcr != NUL)
4076 id = syn_name2id(wp->w_p_wcr);
4077 if (id == 0 || !get_vterm_color_from_synid(id, &wp->w_term_wincolor.fg,
4078 &wp->w_term_wincolor.bg))
4079 term_reset_wincolor(wp);
4080}
4081
4082/*
4083 * Called when option 'termguicolors' was set,
4084 * or when any highlight is changed.
4085 */
4086 void
4087term_update_wincolor_all()
4088{
4089 win_T *wp = NULL;
4090 int did_curwin = FALSE;
4091
4092 while (for_all_windows_and_curwin(&wp, &did_curwin))
4093 term_update_wincolor(wp);
4094}
4095
4096/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004097 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004098 */
4099 static void
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004100init_default_colors(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004101{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004102 VTermColor *fg, *bg;
4103 int fgval, bgval;
4104 int id;
4105
Bram Moolenaara80faa82020-04-12 19:37:17 +02004106 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004107 term->tl_default_color.width = 1;
4108 fg = &term->tl_default_color.fg;
4109 bg = &term->tl_default_color.bg;
4110
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004111 // Vterm uses a default black background. Set it to white when
4112 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004113 if (*p_bg == 'l')
4114 {
4115 fgval = 0;
4116 bgval = 255;
4117 }
4118 else
4119 {
4120 fgval = 255;
4121 bgval = 0;
4122 }
4123 fg->red = fg->green = fg->blue = fgval;
4124 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004125 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
4126 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004127
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004128 // The highlight group overrules the defaults.
4129 id = term_get_highlight_id(term, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004130
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004131 if (!get_vterm_color_from_synid(id, fg, bg))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004132 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004133#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004134 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004135#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004136
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004137 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004138 if (cterm_normal_fg_color > 0)
4139 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004140 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004141# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4142# ifdef VIMDLL
4143 if (!gui.in_use)
4144# endif
4145 {
4146 tmp = fg->red;
4147 fg->red = fg->blue;
4148 fg->blue = tmp;
4149 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004150# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004151 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004152# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004153 else
4154 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004155# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004156
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004157 if (cterm_normal_bg_color > 0)
4158 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004159 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004160# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4161# ifdef VIMDLL
4162 if (!gui.in_use)
4163# endif
4164 {
4165 tmp = fg->red;
4166 fg->red = fg->blue;
4167 fg->blue = tmp;
4168 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004169# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004170 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004171# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004172 else
4173 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004174# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004175 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004176}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004177
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004178#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4179/*
4180 * Set the 16 ANSI colors from array of RGB values
4181 */
4182 static void
4183set_vterm_palette(VTerm *vterm, long_u *rgb)
4184{
4185 int index = 0;
4186 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004187
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004188 for (; index < 16; index++)
4189 {
4190 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004191
Millyb771b6b2021-11-23 12:07:25 +00004192 color.type = VTERM_COLOR_RGB;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004193 color.red = (unsigned)(rgb[index] >> 16);
4194 color.green = (unsigned)(rgb[index] >> 8) & 255;
4195 color.blue = (unsigned)rgb[index] & 255;
4196 vterm_state_set_palette_color(state, index, &color);
4197 }
4198}
4199
4200/*
4201 * Set the ANSI color palette from a list of colors
4202 */
4203 static int
4204set_ansi_colors_list(VTerm *vterm, list_T *list)
4205{
4206 int n = 0;
4207 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004208 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004209
Bram Moolenaarb0992022020-01-30 14:55:42 +01004210 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004211 {
4212 char_u *color_name;
4213 guicolor_T guicolor;
4214
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004215 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004216 if (color_name == NULL)
4217 return FAIL;
4218
4219 guicolor = GUI_GET_COLOR(color_name);
4220 if (guicolor == INVALCOLOR)
4221 return FAIL;
4222
4223 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4224 }
4225
4226 if (n != 16 || li != NULL)
4227 return FAIL;
4228
4229 set_vterm_palette(vterm, rgb);
4230
4231 return OK;
4232}
4233
4234/*
4235 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4236 */
4237 static void
4238init_vterm_ansi_colors(VTerm *vterm)
4239{
4240 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4241
4242 if (var != NULL
4243 && (var->di_tv.v_type != VAR_LIST
4244 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004245 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004246 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004247 semsg(_(e_invalid_argument_str), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004248}
4249#endif
4250
Bram Moolenaar52acb112018-03-18 19:20:22 +01004251/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004252 * Handles a "drop" command from the job in the terminal.
4253 * "item" is the file name, "item->li_next" may have options.
4254 */
4255 static void
4256handle_drop_command(listitem_T *item)
4257{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004258 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004259 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004260 int bufnr;
4261 win_T *wp;
4262 tabpage_T *tp;
4263 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004264 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004265
4266 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4267 FOR_ALL_TAB_WINDOWS(tp, wp)
4268 {
4269 if (wp->w_buffer->b_fnum == bufnr)
4270 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004271 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004272 goto_tabpage_win(tp, wp);
4273 return;
4274 }
4275 }
4276
Bram Moolenaara80faa82020-04-12 19:37:17 +02004277 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004278
4279 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4280 && opt_item->li_tv.vval.v_dict != NULL)
4281 {
4282 dict_T *dict = opt_item->li_tv.vval.v_dict;
4283 char_u *p;
4284
Bram Moolenaar8f667172018-12-14 15:38:31 +01004285 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004286 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004287 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004288 if (p != NULL)
4289 {
4290 if (check_ff_value(p) == FAIL)
4291 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4292 else
4293 ea.force_ff = *p;
4294 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004295 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004296 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004297 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004298 if (p != NULL)
4299 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004300 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004301 if (ea.cmd != NULL)
4302 {
4303 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4304 ea.force_enc = 11;
4305 tofree = ea.cmd;
4306 }
4307 }
4308
Bram Moolenaar8f667172018-12-14 15:38:31 +01004309 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004310 if (p != NULL)
4311 get_bad_opt(p, &ea);
4312
4313 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4314 ea.force_bin = FORCE_BIN;
4315 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4316 ea.force_bin = FORCE_BIN;
4317 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4318 ea.force_bin = FORCE_NOBIN;
4319 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4320 ea.force_bin = FORCE_NOBIN;
4321 }
4322
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004323 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004324 if (ea.cmd == NULL)
4325 ea.cmd = (char_u *)"split";
4326 ea.arg = fname;
4327 ea.cmdidx = CMD_split;
4328 ex_splitview(&ea);
4329
4330 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004331}
4332
4333/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004334 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4335 */
4336 static int
4337is_permitted_term_api(char_u *func, char_u *pat)
4338{
4339 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4340}
4341
4342/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004343 * Handles a function call from the job running in a terminal.
4344 * "item" is the function name, "item->li_next" has the arguments.
4345 */
4346 static void
4347handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4348{
4349 char_u *func;
4350 typval_T argvars[2];
4351 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004352 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004353
4354 if (item->li_next == NULL)
4355 {
4356 ch_log(channel, "Missing function arguments for call");
4357 return;
4358 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004359 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004360
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004361 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004362 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004363 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004364 return;
4365 }
4366
4367 argvars[0].v_type = VAR_NUMBER;
4368 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4369 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004370 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004371 funcexe.fe_firstline = 1L;
4372 funcexe.fe_lastline = 1L;
4373 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004374 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004375 {
4376 clear_tv(&rettv);
4377 ch_log(channel, "Function %s called", func);
4378 }
4379 else
4380 ch_log(channel, "Calling function %s failed", func);
4381}
4382
4383/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004384 * URL decoding (also know as Percent-encoding).
4385 *
4386 * Note this function currently is only used for decoding shell's
4387 * OSC 7 escape sequence which we can assume all bytes are valid
4388 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4389 * encoding bytes like 0xfe, 0xff.
4390 */
4391 static size_t
4392url_decode(const char *src, const size_t len, char_u *dst)
4393{
4394 size_t i = 0, j = 0;
4395
4396 while (i < len)
4397 {
4398 if (src[i] == '%' && i + 2 < len)
4399 {
4400 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4401 j++;
4402 i += 3;
4403 }
4404 else
4405 {
4406 dst[j] = src[i];
4407 i++;
4408 j++;
4409 }
4410 }
4411 dst[j] = '\0';
4412 return j;
4413}
4414
4415/*
4416 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4417 *
4418 * The OSC 7 sequence has the format of
4419 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4420 * and what VTerm provides via VTermStringFragment is
4421 * "file://HOSTNAME/CURRENT/DIR"
4422 */
4423 static void
4424sync_shell_dir(VTermStringFragment *frag)
4425{
4426 int offset = 7; // len of "file://" is 7
4427 char *pos = (char *)frag->str + offset;
4428 char_u *new_dir;
4429
4430 // remove HOSTNAME to get PWD
Bram Moolenaar918b0892021-05-08 20:09:24 +02004431 while (*pos != '/' && offset < (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004432 {
4433 offset += 1;
4434 pos += 1;
4435 }
4436
Bram Moolenaar918b0892021-05-08 20:09:24 +02004437 if (offset >= (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004438 {
4439 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
4440 frag->str);
4441 return;
4442 }
4443
4444 new_dir = alloc(frag->len - offset + 1);
4445 url_decode(pos, frag->len-offset, new_dir);
4446 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4447 vim_free(new_dir);
4448}
4449
4450/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004451 * Called by libvterm when it cannot recognize an OSC sequence.
4452 * We recognize a terminal API command.
4453 */
4454 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004455parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004456{
4457 term_T *term = (term_T *)user;
4458 js_read_T reader;
4459 typval_T tv;
4460 channel_T *channel = term->tl_job == NULL ? NULL
4461 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004462 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004463
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004464 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
4465 if (p_asd && command == 7)
4466 {
4467 sync_shell_dir(&frag);
4468 return 1;
4469 }
4470
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004471 if (command != 51)
4472 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004473
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004474 // Concatenate what was received until the final piece is found.
4475 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4476 {
4477 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004478 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004479 }
4480 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004481 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004482 if (!frag.final)
4483 return 1;
4484
4485 ((char *)gap->ga_data)[gap->ga_len] = 0;
4486 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004487 reader.js_fill = NULL;
4488 reader.js_used = 0;
4489 if (json_decode(&reader, &tv, 0) == OK
4490 && tv.v_type == VAR_LIST
4491 && tv.vval.v_list != NULL)
4492 {
4493 listitem_T *item = tv.vval.v_list->lv_first;
4494
4495 if (item == NULL)
4496 ch_log(channel, "Missing command");
4497 else
4498 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004499 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004500
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004501 // Make sure an invoked command doesn't delete the buffer (and the
4502 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004503 ++term->tl_buffer->b_locked;
4504
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004505 item = item->li_next;
4506 if (item == NULL)
4507 ch_log(channel, "Missing argument for %s", cmd);
4508 else if (STRCMP(cmd, "drop") == 0)
4509 handle_drop_command(item);
4510 else if (STRCMP(cmd, "call") == 0)
4511 handle_call_command(term, channel, item);
4512 else
4513 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004514 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004515 }
4516 }
4517 else
4518 ch_log(channel, "Invalid JSON received");
4519
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004520 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004521 clear_tv(&tv);
4522 return 1;
4523}
4524
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004525/*
4526 * Called by libvterm when it cannot recognize a CSI sequence.
4527 * We recognize the window position report.
4528 */
4529 static int
4530parse_csi(
4531 const char *leader UNUSED,
4532 const long args[],
4533 int argcount,
4534 const char *intermed UNUSED,
4535 char command,
4536 void *user)
4537{
4538 term_T *term = (term_T *)user;
4539 char buf[100];
4540 int len;
4541 int x = 0;
4542 int y = 0;
4543 win_T *wp;
4544
4545 // We recognize only CSI 13 t
4546 if (command != 't' || argcount != 1 || args[0] != 13)
4547 return 0; // not handled
4548
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004549 // When getting the window position is not possible or it fails it results
4550 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004551#if defined(FEAT_GUI) \
4552 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4553 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004554 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004555#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004556
4557 FOR_ALL_WINDOWS(wp)
4558 if (wp->w_buffer == term->tl_buffer)
4559 break;
4560 if (wp != NULL)
4561 {
4562#ifdef FEAT_GUI
4563 if (gui.in_use)
4564 {
4565 x += wp->w_wincol * gui.char_width;
4566 y += W_WINROW(wp) * gui.char_height;
4567 }
4568 else
4569#endif
4570 {
4571 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004572 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004573 x += wp->w_wincol * 7;
4574 y += W_WINROW(wp) * 10;
4575 }
4576 }
4577
4578 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4579 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4580 (char_u *)buf, len, NULL);
4581 return 1;
4582}
4583
Bram Moolenaard8637282020-05-20 18:41:41 +02004584static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004585 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004586 parse_csi, // csi
4587 parse_osc, // osc
Bram Moolenaar7da34152021-11-24 19:30:55 +00004588 NULL, // dcs
4589 NULL, // apc
4590 NULL, // pm
4591 NULL // sos
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004592};
4593
4594/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004595 * Use Vim's allocation functions for vterm so profiling works.
4596 */
4597 static void *
4598vterm_malloc(size_t size, void *data UNUSED)
4599{
Bram Moolenaar88137392021-11-12 16:01:15 +00004600 // make sure that the length is not zero
4601 return alloc_clear(size == 0 ? 1L : size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004602}
4603
4604 static void
4605vterm_memfree(void *ptr, void *data UNUSED)
4606{
4607 vim_free(ptr);
4608}
4609
4610static VTermAllocatorFunctions vterm_allocator = {
4611 &vterm_malloc,
4612 &vterm_memfree
4613};
4614
4615/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004616 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004617 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004618 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004619 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004620create_vterm(term_T *term, int rows, int cols)
4621{
4622 VTerm *vterm;
4623 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004624 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004625 VTermValue value;
4626
Bram Moolenaar756ef112018-04-10 12:04:27 +02004627 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004628 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004629 if (vterm == NULL)
4630 return FAIL;
4631
4632 // Allocate screen and state here, so we can bail out if that fails.
4633 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004634 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004635 if (state == NULL || screen == NULL)
4636 {
4637 vterm_free(vterm);
4638 return FAIL;
4639 }
4640
Bram Moolenaar52acb112018-03-18 19:20:22 +01004641 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004642 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004643 vterm_set_utf8(vterm, 1);
4644
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004645 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004646
4647 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004648 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004649 &term->tl_default_color.fg,
4650 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004651
Bram Moolenaar9e587872019-05-13 20:27:23 +02004652 if (t_colors < 16)
4653 // Less than 16 colors: assume that bold means using a bright color for
4654 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004655 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4656
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004657 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004658 vterm_screen_reset(screen, 1 /* hard */);
4659
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004660 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004661 vterm_screen_enable_altscreen(screen, 1);
4662
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004663 // For unix do not use a blinking cursor. In an xterm this causes the
4664 // cursor to blink if it's blinking in the xterm.
4665 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004666#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004667 if (GetCaretBlinkTime() == INFINITE)
4668 value.boolean = 0;
4669 else
4670 value.boolean = 1;
4671#else
4672 value.boolean = 0;
4673#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004674 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004675 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004676
4677 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004678}
4679
4680/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004681 * Called when option 'background' or 'termguicolors' was set,
4682 * or when any highlight is changed.
Bram Moolenaarad431992021-05-03 20:40:38 +02004683 */
4684 void
4685term_update_colors_all(void)
4686{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004687 term_T *term;
Bram Moolenaarad431992021-05-03 20:40:38 +02004688
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004689 FOR_ALL_TERMS(term)
4690 {
4691 if (term->tl_vterm == NULL)
4692 continue;
4693 init_default_colors(term);
4694 vterm_state_set_default_colors(
4695 vterm_obtain_state(term->tl_vterm),
4696 &term->tl_default_color.fg,
4697 &term->tl_default_color.bg);
4698 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004699}
4700
4701/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004702 * Return the text to show for the buffer name and status.
4703 */
4704 char_u *
4705term_get_status_text(term_T *term)
4706{
4707 if (term->tl_status_text == NULL)
4708 {
4709 char_u *txt;
4710 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004711 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004712
4713 if (term->tl_normal_mode)
4714 {
4715 if (term_job_running(term))
4716 txt = (char_u *)_("Terminal");
4717 else
4718 txt = (char_u *)_("Terminal-finished");
4719 }
4720 else if (term->tl_title != NULL)
4721 txt = term->tl_title;
4722 else if (term_none_open(term))
4723 txt = (char_u *)_("active");
4724 else if (term_job_running(term))
4725 txt = (char_u *)_("running");
4726 else
4727 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004728 fname = buf_get_fname(term->tl_buffer);
4729 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004730 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004731 if (term->tl_status_text != NULL)
4732 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004733 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004734 }
4735 return term->tl_status_text;
4736}
4737
4738/*
Bram Moolenaar3ad69532021-11-19 17:01:08 +00004739 * Clear the cached value of the status text.
4740 */
4741 void
4742term_clear_status_text(term_T *term)
4743{
4744 VIM_CLEAR(term->tl_status_text);
4745}
4746
4747/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004748 * Mark references in jobs of terminals.
4749 */
4750 int
4751set_ref_in_term(int copyID)
4752{
4753 int abort = FALSE;
4754 term_T *term;
4755 typval_T tv;
4756
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004757 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004758 if (term->tl_job != NULL)
4759 {
4760 tv.v_type = VAR_JOB;
4761 tv.vval.v_job = term->tl_job;
4762 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4763 }
4764 return abort;
4765}
4766
4767/*
4768 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004769 * Returns NULL when the buffer is not for a terminal window and logs a message
4770 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004771 */
4772 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004773term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004774{
4775 buf_T *buf;
4776
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004777 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004778 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004779 --emsg_off;
4780 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004781 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004782 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004783 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004784 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004785 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004786 return buf;
4787}
4788
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004789 static void
4790clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004791{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004792 CLEAR_FIELD(*cell);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004793 cell->fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4794 cell->bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004795}
4796
4797 static void
4798dump_term_color(FILE *fd, VTermColor *color)
4799{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004800 int index;
4801
4802 if (VTERM_COLOR_IS_INDEXED(color))
4803 index = color->index + 1;
4804 else if (color->type == 0)
4805 // use RGB values
4806 index = 255;
4807 else
4808 // default color
4809 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004810 fprintf(fd, "%02x%02x%02x%d",
4811 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004812 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004813}
4814
4815/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004816 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004817 *
4818 * Each screen cell in full is:
4819 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4820 * {characters} is a space for an empty cell
4821 * For a double-width character "+" is changed to "*" and the next cell is
4822 * skipped.
4823 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4824 * when "&" use the same as the previous cell.
4825 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4826 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4827 * {color-idx} is a number from 0 to 255
4828 *
4829 * Screen cell with same width, attributes and color as the previous one:
4830 * |{characters}
4831 *
4832 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4833 *
4834 * Repeating the previous screen cell:
4835 * @{count}
4836 */
4837 void
4838f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4839{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004840 buf_T *buf;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004841 term_T *term;
4842 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004843 int max_height = 0;
4844 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004845 stat_T st;
4846 FILE *fd;
4847 VTermPos pos;
4848 VTermScreen *screen;
4849 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004850 VTermState *state;
4851 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004852
4853 if (check_restricted() || check_secure())
4854 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004855
4856 if (in_vim9script()
4857 && (check_for_buffer_arg(argvars, 0) == FAIL
4858 || check_for_string_arg(argvars, 1) == FAIL
4859 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4860 return;
4861
4862 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004863 if (buf == NULL)
4864 return;
4865 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004866 if (term->tl_vterm == NULL)
4867 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004868 emsg(_(e_job_already_finished));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004869 return;
4870 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004871
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004872 if (argvars[2].v_type != VAR_UNKNOWN)
4873 {
4874 dict_T *d;
4875
4876 if (argvars[2].v_type != VAR_DICT)
4877 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004878 emsg(_(e_dictionary_required));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004879 return;
4880 }
4881 d = argvars[2].vval.v_dict;
4882 if (d != NULL)
4883 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004884 max_height = dict_get_number(d, (char_u *)"rows");
4885 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004886 }
4887 }
4888
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004889 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004890 if (fname == NULL)
4891 return;
4892 if (mch_stat((char *)fname, &st) >= 0)
4893 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004894 semsg(_(e_file_exists_str), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004895 return;
4896 }
4897
Bram Moolenaard96ff162018-02-18 22:13:29 +01004898 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4899 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004900 semsg(_(e_cant_create_file_str), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004901 return;
4902 }
4903
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004904 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004905
4906 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004907 state = vterm_obtain_state(term->tl_vterm);
4908 vterm_state_get_cursorpos(state, &cursor_pos);
4909
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004910 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4911 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004912 {
4913 int repeat = 0;
4914
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004915 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4916 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004917 {
4918 VTermScreenCell cell;
4919 int same_attr;
4920 int same_chars = TRUE;
4921 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004922 int is_cursor_pos = (pos.col == cursor_pos.col
4923 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004924
4925 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004926 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004927
4928 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4929 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004930 int c = cell.chars[i];
4931 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004932 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004933
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004934 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004935 if (i == 0)
4936 {
4937 c = (c == NUL) ? ' ' : c;
4938 pc = (pc == NUL) ? ' ' : pc;
4939 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004940 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004941 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004942 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004943 break;
4944 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004945 same_attr = vtermAttr2hl(&cell.attrs)
4946 == vtermAttr2hl(&prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004947 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4948 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004949 if (same_chars && cell.width == prev_cell.width && same_attr
4950 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004951 {
4952 ++repeat;
4953 }
4954 else
4955 {
4956 if (repeat > 0)
4957 {
4958 fprintf(fd, "@%d", repeat);
4959 repeat = 0;
4960 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004961 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004962
4963 if (cell.chars[0] == NUL)
4964 fputs(" ", fd);
4965 else
4966 {
4967 char_u charbuf[10];
4968 int len;
4969
4970 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4971 && cell.chars[i] != NUL; ++i)
4972 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004973 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004974 fwrite(charbuf, len, 1, fd);
4975 }
4976 }
4977
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004978 // When only the characters differ we don't write anything, the
4979 // following "|", "@" or NL will indicate using the same
4980 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004981 if (cell.width != prev_cell.width || !same_attr)
4982 {
4983 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004984 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004985 else
4986 fputs("+", fd);
4987
4988 if (same_attr)
4989 {
4990 fputs("&", fd);
4991 }
4992 else
4993 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004994 fprintf(fd, "%d", vtermAttr2hl(&cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004995 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004996 fputs("&", fd);
4997 else
4998 {
4999 fputs("#", fd);
5000 dump_term_color(fd, &cell.fg);
5001 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005002 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005003 fputs("&", fd);
5004 else
5005 {
5006 fputs("#", fd);
5007 dump_term_color(fd, &cell.bg);
5008 }
5009 }
5010 }
5011
5012 prev_cell = cell;
5013 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005014
5015 if (cell.width == 2)
5016 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005017 }
5018 if (repeat > 0)
5019 fprintf(fd, "@%d", repeat);
5020 fputs("\n", fd);
5021 }
5022
5023 fclose(fd);
5024}
5025
5026/*
5027 * Called when a dump is corrupted. Put a breakpoint here when debugging.
5028 */
5029 static void
5030dump_is_corrupt(garray_T *gap)
5031{
5032 ga_concat(gap, (char_u *)"CORRUPT");
5033}
5034
5035 static void
5036append_cell(garray_T *gap, cellattr_T *cell)
5037{
5038 if (ga_grow(gap, 1) == OK)
5039 {
5040 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
5041 ++gap->ga_len;
5042 }
5043}
5044
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005045 static void
5046clear_cellattr(cellattr_T *cell)
5047{
5048 CLEAR_FIELD(*cell);
5049 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
5050 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
5051}
5052
Bram Moolenaard96ff162018-02-18 22:13:29 +01005053/*
5054 * Read the dump file from "fd" and append lines to the current buffer.
5055 * Return the cell width of the longest line.
5056 */
5057 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01005058read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005059{
5060 int c;
5061 garray_T ga_text;
5062 garray_T ga_cell;
5063 char_u *prev_char = NULL;
5064 int attr = 0;
5065 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005066 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005067 term_T *term = curbuf->b_term;
5068 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005069 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005070
5071 ga_init2(&ga_text, 1, 90);
5072 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005073 clear_cellattr(&cell);
5074 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005075 cursor_pos->row = -1;
5076 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005077
5078 c = fgetc(fd);
5079 for (;;)
5080 {
5081 if (c == EOF)
5082 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02005083 if (c == '\r')
5084 {
5085 // DOS line endings? Ignore.
5086 c = fgetc(fd);
5087 }
5088 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005089 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005090 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005091 if (ga_text.ga_data == NULL)
5092 dump_is_corrupt(&ga_text);
5093 if (ga_grow(&term->tl_scrollback, 1) == OK)
5094 {
5095 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
5096 + term->tl_scrollback.ga_len;
5097
5098 if (max_cells < ga_cell.ga_len)
5099 max_cells = ga_cell.ga_len;
5100 line->sb_cols = ga_cell.ga_len;
5101 line->sb_cells = ga_cell.ga_data;
5102 line->sb_fill_attr = term->tl_default_color;
5103 ++term->tl_scrollback.ga_len;
5104 ga_init(&ga_cell);
5105
5106 ga_append(&ga_text, NUL);
5107 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5108 ga_text.ga_len, FALSE);
5109 }
5110 else
5111 ga_clear(&ga_cell);
5112 ga_text.ga_len = 0;
5113
5114 c = fgetc(fd);
5115 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005116 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005117 {
5118 int prev_len = ga_text.ga_len;
5119
Bram Moolenaar9271d052018-02-25 21:39:46 +01005120 if (c == '>')
5121 {
5122 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005123 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005124 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5125 cursor_pos->col = ga_cell.ga_len;
5126 }
5127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005128 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005129 c = fgetc(fd);
5130 if (c != EOF)
5131 ga_append(&ga_text, c);
5132 for (;;)
5133 {
5134 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005135 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005136 || c == EOF || c == '\n')
5137 break;
5138 ga_append(&ga_text, c);
5139 }
5140
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005141 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005142 vim_free(prev_char);
5143 if (ga_text.ga_data != NULL)
5144 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5145 ga_text.ga_len - prev_len);
5146
Bram Moolenaar9271d052018-02-25 21:39:46 +01005147 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005148 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005149 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005150 }
5151 else if (c == '+' || c == '*')
5152 {
5153 int is_bg;
5154
5155 cell.width = c == '+' ? 1 : 2;
5156
5157 c = fgetc(fd);
5158 if (c == '&')
5159 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005160 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005161 c = fgetc(fd);
5162 }
5163 else if (isdigit(c))
5164 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005165 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005166 attr = 0;
5167 while (isdigit(c))
5168 {
5169 attr = attr * 10 + (c - '0');
5170 c = fgetc(fd);
5171 }
5172 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005173
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005174 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005175 for (is_bg = 0; is_bg <= 1; ++is_bg)
5176 {
5177 if (c == '&')
5178 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005179 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005180 c = fgetc(fd);
5181 }
5182 else if (c == '#')
5183 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005184 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005185
5186 c = fgetc(fd);
5187 red = hex2nr(c);
5188 c = fgetc(fd);
5189 red = (red << 4) + hex2nr(c);
5190 c = fgetc(fd);
5191 green = hex2nr(c);
5192 c = fgetc(fd);
5193 green = (green << 4) + hex2nr(c);
5194 c = fgetc(fd);
5195 blue = hex2nr(c);
5196 c = fgetc(fd);
5197 blue = (blue << 4) + hex2nr(c);
5198 c = fgetc(fd);
5199 if (!isdigit(c))
5200 dump_is_corrupt(&ga_text);
5201 while (isdigit(c))
5202 {
5203 index = index * 10 + (c - '0');
5204 c = fgetc(fd);
5205 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005206 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005207 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005208 type = VTERM_COLOR_RGB;
5209 if (index == 0)
5210 {
5211 if (is_bg)
5212 type |= VTERM_COLOR_DEFAULT_BG;
5213 else
5214 type |= VTERM_COLOR_DEFAULT_FG;
5215 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005216 }
5217 else
5218 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005219 type = VTERM_COLOR_INDEXED;
5220 index -= 1;
5221 }
5222 if (is_bg)
5223 {
5224 cell.bg.type = type;
5225 cell.bg.red = red;
5226 cell.bg.green = green;
5227 cell.bg.blue = blue;
5228 cell.bg.index = index;
5229 }
5230 else
5231 {
5232 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005233 cell.fg.red = red;
5234 cell.fg.green = green;
5235 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005236 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005237 }
5238 }
5239 else
5240 dump_is_corrupt(&ga_text);
5241 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005242 }
5243 else
5244 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005245 }
5246 else
5247 dump_is_corrupt(&ga_text);
5248
5249 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005250 if (cell.width == 2)
5251 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005252 }
5253 else if (c == '@')
5254 {
5255 if (prev_char == NULL)
5256 dump_is_corrupt(&ga_text);
5257 else
5258 {
5259 int count = 0;
5260
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005261 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005262 for (;;)
5263 {
5264 c = fgetc(fd);
5265 if (!isdigit(c))
5266 break;
5267 count = count * 10 + (c - '0');
5268 }
5269
5270 while (count-- > 0)
5271 {
5272 ga_concat(&ga_text, prev_char);
5273 append_cell(&ga_cell, &cell);
5274 }
5275 }
5276 }
5277 else
5278 {
5279 dump_is_corrupt(&ga_text);
5280 c = fgetc(fd);
5281 }
5282 }
5283
5284 if (ga_text.ga_len > 0)
5285 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005286 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005287 dump_is_corrupt(&ga_text);
5288 ga_append(&ga_text, NUL);
5289 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5290 ga_text.ga_len, FALSE);
5291 }
5292
5293 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005294 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005295 vim_free(prev_char);
5296
5297 return max_cells;
5298}
5299
5300/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005301 * Return an allocated string with at least "text_width" "=" characters and
5302 * "fname" inserted in the middle.
5303 */
5304 static char_u *
5305get_separator(int text_width, char_u *fname)
5306{
5307 int width = MAX(text_width, curwin->w_width);
5308 char_u *textline;
5309 int fname_size;
5310 char_u *p = fname;
5311 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005312 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005313
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005314 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005315 if (textline == NULL)
5316 return NULL;
5317
5318 fname_size = vim_strsize(fname);
5319 if (fname_size < width - 8)
5320 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005321 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005322 width = MAX(text_width, fname_size + 8);
5323 }
5324 else if (fname_size > width - 8)
5325 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005326 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005327 p = gettail(fname);
5328 fname_size = vim_strsize(p);
5329 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005330 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005331 while (fname_size > width - 8)
5332 {
5333 p += (*mb_ptr2len)(p);
5334 fname_size = vim_strsize(p);
5335 }
5336
5337 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5338 textline[i] = '=';
5339 textline[i++] = ' ';
5340
5341 STRCPY(textline + i, p);
5342 off = STRLEN(textline);
5343 textline[off] = ' ';
5344 for (i = 1; i < (width - fname_size) / 2; ++i)
5345 textline[off + i] = '=';
5346 textline[off + i] = NUL;
5347
5348 return textline;
5349}
5350
5351/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005352 * Common for "term_dumpdiff()" and "term_dumpload()".
5353 */
5354 static void
5355term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5356{
5357 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005358 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005359 char_u buf1[NUMBUFLEN];
5360 char_u buf2[NUMBUFLEN];
5361 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005362 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005363 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005364 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005365 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005366 char_u *textline = NULL;
5367
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005368 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005369 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005370 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005371 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005372 if (fname1 == NULL || (do_diff && fname2 == NULL))
5373 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005374 emsg(_(e_invalid_argument));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005375 return;
5376 }
5377 fd1 = mch_fopen((char *)fname1, READBIN);
5378 if (fd1 == NULL)
5379 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005380 semsg(_(e_cant_read_file_str), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005381 return;
5382 }
5383 if (do_diff)
5384 {
5385 fd2 = mch_fopen((char *)fname2, READBIN);
5386 if (fd2 == NULL)
5387 {
5388 fclose(fd1);
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005389 semsg(_(e_cant_read_file_str), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005390 return;
5391 }
5392 }
5393
5394 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005395 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5396 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5397 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5398 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5399 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005400
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005401 if (opt.jo_term_name == NULL)
5402 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005403 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005404
Bram Moolenaar51e14382019-05-25 20:21:28 +02005405 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005406 if (fname_tofree != NULL)
5407 {
5408 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5409 opt.jo_term_name = fname_tofree;
5410 }
5411 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005412
Bram Moolenaar87abab92019-06-03 21:14:59 +02005413 if (opt.jo_bufnr_buf != NULL)
5414 {
5415 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5416
5417 // With "bufnr" argument: enter the window with this buffer and make it
5418 // empty.
5419 if (wp == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005420 semsg(_(e_invalid_argument_str), "bufnr");
Bram Moolenaar87abab92019-06-03 21:14:59 +02005421 else
5422 {
5423 buf = curbuf;
5424 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005425 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005426 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005427 redraw_later(NOT_VALID);
5428 }
5429 }
5430 else
5431 // Create a new terminal window.
5432 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5433
Bram Moolenaard96ff162018-02-18 22:13:29 +01005434 if (buf != NULL && buf->b_term != NULL)
5435 {
5436 int i;
5437 linenr_T bot_lnum;
5438 linenr_T lnum;
5439 term_T *term = buf->b_term;
5440 int width;
5441 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005442 VTermPos cursor_pos1;
5443 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005444
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005445 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005446
Bram Moolenaard96ff162018-02-18 22:13:29 +01005447 rettv->vval.v_number = buf->b_fnum;
5448
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005449 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005450 width = read_dump_file(fd1, &cursor_pos1);
5451
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005452 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005453 if (cursor_pos1.row >= 0)
5454 {
5455 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5456 coladvance(cursor_pos1.col);
5457 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005458
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005459 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005460 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005461
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005462 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005463 if (!do_diff)
5464 goto theend;
5465
5466 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5467
Bram Moolenaar4a696342018-04-05 18:45:26 +02005468 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005469 if (textline == NULL)
5470 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005471 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5472 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5473 vim_free(textline);
5474
5475 textline = get_separator(width, fname2);
5476 if (textline == NULL)
5477 goto theend;
5478 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5479 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005480 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005481
5482 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005483 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005484 if (width2 > width)
5485 {
5486 vim_free(textline);
5487 textline = alloc(width2 + 1);
5488 if (textline == NULL)
5489 goto theend;
5490 width = width2;
5491 textline[width] = NUL;
5492 }
5493 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5494
5495 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5496 {
5497 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5498 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005499 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005500 for (i = 0; i < width; ++i)
5501 textline[i] = '-';
5502 }
5503 else
5504 {
5505 char_u *line1;
5506 char_u *line2;
5507 char_u *p1;
5508 char_u *p2;
5509 int col;
5510 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5511 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5512 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5513 ->sb_cells;
5514
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005515 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005516 line1 = vim_strsave(ml_get(lnum));
5517 if (line1 == NULL)
5518 break;
5519 p1 = line1;
5520
5521 line2 = ml_get(lnum + bot_lnum);
5522 p2 = line2;
5523 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5524 {
5525 int len1 = utfc_ptr2len(p1);
5526 int len2 = utfc_ptr2len(p2);
5527
5528 textline[col] = ' ';
5529 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005530 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005531 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005532 else if (lnum == cursor_pos1.row + 1
5533 && col == cursor_pos1.col
5534 && (cursor_pos1.row != cursor_pos2.row
5535 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005536 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005537 textline[col] = '>';
5538 else if (lnum == cursor_pos2.row + 1
5539 && col == cursor_pos2.col
5540 && (cursor_pos1.row != cursor_pos2.row
5541 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005542 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005543 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005544 else if (cellattr1 != NULL && cellattr2 != NULL)
5545 {
5546 if ((cellattr1 + col)->width
5547 != (cellattr2 + col)->width)
5548 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005549 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005550 &(cellattr2 + col)->fg))
5551 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005552 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005553 &(cellattr2 + col)->bg))
5554 textline[col] = 'b';
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005555 else if (vtermAttr2hl(&(cellattr1 + col)->attrs)
5556 != vtermAttr2hl(&((cellattr2 + col)->attrs)))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005557 textline[col] = 'a';
5558 }
5559 p1 += len1;
5560 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005561 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005562 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005563
5564 while (col < width)
5565 {
5566 if (*p1 == NUL && *p2 == NUL)
5567 textline[col] = '?';
5568 else if (*p1 == NUL)
5569 {
5570 textline[col] = '+';
5571 p2 += utfc_ptr2len(p2);
5572 }
5573 else
5574 {
5575 textline[col] = '-';
5576 p1 += utfc_ptr2len(p1);
5577 }
5578 ++col;
5579 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005580
5581 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005582 }
5583 if (add_empty_scrollback(term, &term->tl_default_color,
5584 term->tl_top_diff_rows) == OK)
5585 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5586 ++bot_lnum;
5587 }
5588
5589 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5590 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005591 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005592 for (i = 0; i < width; ++i)
5593 textline[i] = '+';
5594 if (add_empty_scrollback(term, &term->tl_default_color,
5595 term->tl_top_diff_rows) == OK)
5596 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5597 ++lnum;
5598 ++bot_lnum;
5599 }
5600
5601 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005602
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005603 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005604 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005605 }
5606
5607theend:
5608 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005609 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005610 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005611 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005612 fclose(fd2);
5613}
5614
5615/*
5616 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5617 * bottom files.
5618 * Return FAIL when this is not possible.
5619 */
5620 int
5621term_swap_diff()
5622{
5623 term_T *term = curbuf->b_term;
5624 linenr_T line_count;
5625 linenr_T top_rows;
5626 linenr_T bot_rows;
5627 linenr_T bot_start;
5628 linenr_T lnum;
5629 char_u *p;
5630 sb_line_T *sb_line;
5631
5632 if (term == NULL
5633 || !term_is_finished(curbuf)
5634 || term->tl_top_diff_rows == 0
5635 || term->tl_scrollback.ga_len == 0)
5636 return FAIL;
5637
5638 line_count = curbuf->b_ml.ml_line_count;
5639 top_rows = term->tl_top_diff_rows;
5640 bot_rows = term->tl_bot_diff_rows;
5641 bot_start = line_count - bot_rows;
5642 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5643
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005644 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005645 for (lnum = 1; lnum <= top_rows; ++lnum)
5646 {
5647 p = vim_strsave(ml_get(1));
5648 if (p == NULL)
5649 return OK;
5650 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005651 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005652 vim_free(p);
5653 }
5654
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005655 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005656 for (lnum = 1; lnum <= bot_rows; ++lnum)
5657 {
5658 p = vim_strsave(ml_get(bot_start + lnum));
5659 if (p == NULL)
5660 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005661 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005662 ml_append(lnum - 1, p, 0, FALSE);
5663 vim_free(p);
5664 }
5665
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005666 // move top title to bottom
5667 p = vim_strsave(ml_get(bot_rows + 1));
5668 if (p == NULL)
5669 return OK;
5670 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005671 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005672 vim_free(p);
5673
5674 // move bottom title to top
5675 p = vim_strsave(ml_get(line_count - top_rows));
5676 if (p == NULL)
5677 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005678 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005679 ml_append(bot_rows, p, 0, FALSE);
5680 vim_free(p);
5681
Bram Moolenaard96ff162018-02-18 22:13:29 +01005682 if (top_rows == bot_rows)
5683 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005684 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005685 for (lnum = 0; lnum < top_rows; ++lnum)
5686 {
5687 sb_line_T temp;
5688
5689 temp = *(sb_line + lnum);
5690 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5691 *(sb_line + bot_start + lnum) = temp;
5692 }
5693 }
5694 else
5695 {
5696 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005697 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005698
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005699 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005700 if (temp != NULL)
5701 {
5702 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5703 mch_memmove(term->tl_scrollback.ga_data,
5704 temp + bot_start,
5705 sizeof(sb_line_T) * bot_rows);
5706 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5707 temp + top_rows,
5708 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5709 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5710 + line_count - top_rows,
5711 temp,
5712 sizeof(sb_line_T) * top_rows);
5713 vim_free(temp);
5714 }
5715 }
5716
5717 term->tl_top_diff_rows = bot_rows;
5718 term->tl_bot_diff_rows = top_rows;
5719
5720 update_screen(NOT_VALID);
5721 return OK;
5722}
5723
5724/*
5725 * "term_dumpdiff(filename, filename, options)" function
5726 */
5727 void
5728f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5729{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005730 if (in_vim9script()
5731 && (check_for_string_arg(argvars, 0) == FAIL
5732 || check_for_string_arg(argvars, 1) == FAIL
5733 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5734 return;
5735
Bram Moolenaard96ff162018-02-18 22:13:29 +01005736 term_load_dump(argvars, rettv, TRUE);
5737}
5738
5739/*
5740 * "term_dumpload(filename, options)" function
5741 */
5742 void
5743f_term_dumpload(typval_T *argvars, typval_T *rettv)
5744{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005745 if (in_vim9script()
5746 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005747 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005748 return;
5749
Bram Moolenaard96ff162018-02-18 22:13:29 +01005750 term_load_dump(argvars, rettv, FALSE);
5751}
5752
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005753/*
5754 * "term_getaltscreen(buf)" function
5755 */
5756 void
5757f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5758{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005759 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005760
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005761 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5762 return;
5763
5764 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005765 if (buf == NULL)
5766 return;
5767 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5768}
5769
5770/*
5771 * "term_getattr(attr, name)" function
5772 */
5773 void
5774f_term_getattr(typval_T *argvars, typval_T *rettv)
5775{
5776 int attr;
5777 size_t i;
5778 char_u *name;
5779
5780 static struct {
5781 char *name;
5782 int attr;
5783 } attrs[] = {
5784 {"bold", HL_BOLD},
5785 {"italic", HL_ITALIC},
5786 {"underline", HL_UNDERLINE},
5787 {"strike", HL_STRIKETHROUGH},
5788 {"reverse", HL_INVERSE},
5789 };
5790
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005791 if (in_vim9script()
5792 && (check_for_number_arg(argvars, 0) == FAIL
5793 || check_for_string_arg(argvars, 1) == FAIL))
5794 return;
5795
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005796 attr = tv_get_number(&argvars[0]);
5797 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005798 if (name == NULL)
5799 return;
5800
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005801 if (attr > HL_ALL)
5802 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02005803 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005804 if (STRCMP(name, attrs[i].name) == 0)
5805 {
5806 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5807 break;
5808 }
5809}
5810
5811/*
5812 * "term_getcursor(buf)" function
5813 */
5814 void
5815f_term_getcursor(typval_T *argvars, typval_T *rettv)
5816{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005817 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005818 term_T *term;
5819 list_T *l;
5820 dict_T *d;
5821
5822 if (rettv_list_alloc(rettv) == FAIL)
5823 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005824
5825 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5826 return;
5827
5828 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005829 if (buf == NULL)
5830 return;
5831 term = buf->b_term;
5832
5833 l = rettv->vval.v_list;
5834 list_append_number(l, term->tl_cursor_pos.row + 1);
5835 list_append_number(l, term->tl_cursor_pos.col + 1);
5836
5837 d = dict_alloc();
5838 if (d != NULL)
5839 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005840 dict_add_number(d, "visible", term->tl_cursor_visible);
5841 dict_add_number(d, "blink", blink_state_is_inverted()
5842 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5843 dict_add_number(d, "shape", term->tl_cursor_shape);
5844 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005845 list_append_dict(l, d);
5846 }
5847}
5848
5849/*
5850 * "term_getjob(buf)" function
5851 */
5852 void
5853f_term_getjob(typval_T *argvars, typval_T *rettv)
5854{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005855 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005856
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005857 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5858 return;
5859
5860 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005861 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005862 {
5863 rettv->v_type = VAR_SPECIAL;
5864 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005865 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005866 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005867
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005868 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005869 rettv->vval.v_job = buf->b_term->tl_job;
5870 if (rettv->vval.v_job != NULL)
5871 ++rettv->vval.v_job->jv_refcount;
5872}
5873
5874 static int
5875get_row_number(typval_T *tv, term_T *term)
5876{
5877 if (tv->v_type == VAR_STRING
5878 && tv->vval.v_string != NULL
5879 && STRCMP(tv->vval.v_string, ".") == 0)
5880 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005881 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005882}
5883
5884/*
5885 * "term_getline(buf, row)" function
5886 */
5887 void
5888f_term_getline(typval_T *argvars, typval_T *rettv)
5889{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005890 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005891 term_T *term;
5892 int row;
5893
5894 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005895
5896 if (in_vim9script()
5897 && (check_for_buffer_arg(argvars, 0) == FAIL
5898 || check_for_lnum_arg(argvars, 1) == FAIL))
5899 return;
5900
5901 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005902 if (buf == NULL)
5903 return;
5904 term = buf->b_term;
5905 row = get_row_number(&argvars[1], term);
5906
5907 if (term->tl_vterm == NULL)
5908 {
5909 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5910
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005911 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005912 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5913 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5914 }
5915 else
5916 {
5917 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5918 VTermRect rect;
5919 int len;
5920 char_u *p;
5921
5922 if (row < 0 || row >= term->tl_rows)
5923 return;
5924 len = term->tl_cols * MB_MAXBYTES + 1;
5925 p = alloc(len);
5926 if (p == NULL)
5927 return;
5928 rettv->vval.v_string = p;
5929
5930 rect.start_col = 0;
5931 rect.end_col = term->tl_cols;
5932 rect.start_row = row;
5933 rect.end_row = row + 1;
5934 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5935 }
5936}
5937
5938/*
5939 * "term_getscrolled(buf)" function
5940 */
5941 void
5942f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5943{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005944 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005945
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005946 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5947 return;
5948
5949 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005950 if (buf == NULL)
5951 return;
5952 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5953}
5954
5955/*
5956 * "term_getsize(buf)" function
5957 */
5958 void
5959f_term_getsize(typval_T *argvars, typval_T *rettv)
5960{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005961 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005962 list_T *l;
5963
5964 if (rettv_list_alloc(rettv) == FAIL)
5965 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005966
5967 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5968 return;
5969
5970 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005971 if (buf == NULL)
5972 return;
5973
5974 l = rettv->vval.v_list;
5975 list_append_number(l, buf->b_term->tl_rows);
5976 list_append_number(l, buf->b_term->tl_cols);
5977}
5978
5979/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005980 * "term_setsize(buf, rows, cols)" function
5981 */
5982 void
5983f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5984{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005985 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02005986 term_T *term;
5987 varnumber_T rows, cols;
5988
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005989 if (in_vim9script()
5990 && (check_for_buffer_arg(argvars, 0) == FAIL
5991 || check_for_number_arg(argvars, 1) == FAIL
5992 || check_for_number_arg(argvars, 2) == FAIL))
5993 return;
5994
5995 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005996 if (buf == NULL)
5997 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005998 emsg(_(e_not_terminal_buffer));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005999 return;
6000 }
6001 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02006002 return;
6003 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006004 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02006005 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006006 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02006007 cols = cols <= 0 ? term->tl_cols : cols;
6008 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006009 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02006010
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006011 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02006012 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
6013 term_report_winsize(term, term->tl_rows, term->tl_cols);
6014}
6015
6016/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006017 * "term_getstatus(buf)" function
6018 */
6019 void
6020f_term_getstatus(typval_T *argvars, typval_T *rettv)
6021{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006022 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006023 term_T *term;
6024 char_u val[100];
6025
6026 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006027
6028 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6029 return;
6030
6031 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006032 if (buf == NULL)
6033 return;
6034 term = buf->b_term;
6035
6036 if (term_job_running(term))
6037 STRCPY(val, "running");
6038 else
6039 STRCPY(val, "finished");
6040 if (term->tl_normal_mode)
6041 STRCAT(val, ",normal");
6042 rettv->vval.v_string = vim_strsave(val);
6043}
6044
6045/*
6046 * "term_gettitle(buf)" function
6047 */
6048 void
6049f_term_gettitle(typval_T *argvars, typval_T *rettv)
6050{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006051 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006052
6053 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006054
6055 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6056 return;
6057
6058 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006059 if (buf == NULL)
6060 return;
6061
6062 if (buf->b_term->tl_title != NULL)
6063 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
6064}
6065
6066/*
6067 * "term_gettty(buf)" function
6068 */
6069 void
6070f_term_gettty(typval_T *argvars, typval_T *rettv)
6071{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006072 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02006073 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006074 int num = 0;
6075
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006076 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006077 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006078 || check_for_opt_bool_arg(argvars, 1) == FAIL))
6079 return;
6080
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006081 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006082 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006083 if (buf == NULL)
6084 return;
6085 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02006086 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006087
6088 switch (num)
6089 {
6090 case 0:
6091 if (buf->b_term->tl_job != NULL)
6092 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006093 break;
6094 case 1:
6095 if (buf->b_term->tl_job != NULL)
6096 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006097 break;
6098 default:
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006099 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006100 return;
6101 }
6102 if (p != NULL)
6103 rettv->vval.v_string = vim_strsave(p);
6104}
6105
6106/*
6107 * "term_list()" function
6108 */
6109 void
6110f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
6111{
6112 term_T *tp;
6113 list_T *l;
6114
6115 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
6116 return;
6117
6118 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006119 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02006120 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006121 if (list_append_number(l,
6122 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6123 return;
6124}
6125
6126/*
6127 * "term_scrape(buf, row)" function
6128 */
6129 void
6130f_term_scrape(typval_T *argvars, typval_T *rettv)
6131{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006132 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006133 VTermScreen *screen = NULL;
6134 VTermPos pos;
6135 list_T *l;
6136 term_T *term;
6137 char_u *p;
6138 sb_line_T *line;
6139
6140 if (rettv_list_alloc(rettv) == FAIL)
6141 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006142
6143 if (in_vim9script()
6144 && (check_for_buffer_arg(argvars, 0) == FAIL
6145 || check_for_lnum_arg(argvars, 1) == FAIL))
6146 return;
6147
6148 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006149 if (buf == NULL)
6150 return;
6151 term = buf->b_term;
6152
6153 l = rettv->vval.v_list;
6154 pos.row = get_row_number(&argvars[1], term);
6155
6156 if (term->tl_vterm != NULL)
6157 {
6158 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006159 if (screen == NULL) // can't really happen
6160 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006161 p = NULL;
6162 line = NULL;
6163 }
6164 else
6165 {
6166 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6167
6168 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6169 return;
6170 p = ml_get_buf(buf, lnum + 1, FALSE);
6171 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6172 }
6173
6174 for (pos.col = 0; pos.col < term->tl_cols; )
6175 {
6176 dict_T *dcell;
6177 int width;
6178 VTermScreenCellAttrs attrs;
6179 VTermColor fg, bg;
6180 char_u rgb[8];
6181 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6182 int off = 0;
6183 int i;
6184
6185 if (screen == NULL)
6186 {
6187 cellattr_T *cellattr;
6188 int len;
6189
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006190 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006191 if (pos.col >= line->sb_cols)
6192 break;
6193 cellattr = line->sb_cells + pos.col;
6194 width = cellattr->width;
6195 attrs = cellattr->attrs;
6196 fg = cellattr->fg;
6197 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006198 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006199 mch_memmove(mbs, p, len);
6200 mbs[len] = NUL;
6201 p += len;
6202 }
6203 else
6204 {
6205 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006206
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006207 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6208 break;
6209 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6210 {
6211 if (cell.chars[i] == 0)
6212 break;
6213 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6214 }
6215 mbs[off] = NUL;
6216 width = cell.width;
6217 attrs = cell.attrs;
6218 fg = cell.fg;
6219 bg = cell.bg;
6220 }
6221 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006222 if (dcell == NULL)
6223 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006224 list_append_dict(l, dcell);
6225
Bram Moolenaare0be1672018-07-08 16:50:37 +02006226 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006227
6228 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6229 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006230 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006231 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6232 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006233 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006234
Bram Moolenaar87fd0922021-11-20 13:47:45 +00006235 dict_add_number(dcell, "attr",
6236 cell2attr(term, NULL, &attrs, &fg, &bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006237 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006238
6239 ++pos.col;
6240 if (width == 2)
6241 ++pos.col;
6242 }
6243}
6244
6245/*
6246 * "term_sendkeys(buf, keys)" function
6247 */
6248 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006249f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006250{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006251 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006252 char_u *msg;
6253 term_T *term;
6254
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006255 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006256 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006257 || check_for_string_arg(argvars, 1) == FAIL))
6258 return;
6259
6260 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006261 if (buf == NULL)
6262 return;
6263
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006264 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006265 if (msg == NULL)
6266 return;
6267 term = buf->b_term;
6268 if (term->tl_vterm == NULL)
6269 return;
6270
6271 while (*msg != NUL)
6272 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006273 int c;
6274
6275 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6276 {
6277 c = TO_SPECIAL(msg[1], msg[2]);
6278 msg += 3;
6279 }
6280 else
6281 {
6282 c = PTR2CHAR(msg);
6283 msg += MB_CPTR2LEN(msg);
6284 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006285 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006286 }
6287}
6288
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006289#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6290/*
6291 * "term_getansicolors(buf)" function
6292 */
6293 void
6294f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6295{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006296 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006297 term_T *term;
6298 VTermState *state;
6299 VTermColor color;
6300 char_u hexbuf[10];
6301 int index;
6302 list_T *list;
6303
6304 if (rettv_list_alloc(rettv) == FAIL)
6305 return;
6306
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006307 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6308 return;
6309
6310 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006311 if (buf == NULL)
6312 return;
6313 term = buf->b_term;
6314 if (term->tl_vterm == NULL)
6315 return;
6316
6317 list = rettv->vval.v_list;
6318 state = vterm_obtain_state(term->tl_vterm);
6319 for (index = 0; index < 16; index++)
6320 {
6321 vterm_state_get_palette_color(state, index, &color);
6322 sprintf((char *)hexbuf, "#%02x%02x%02x",
6323 color.red, color.green, color.blue);
6324 if (list_append_string(list, hexbuf, 7) == FAIL)
6325 return;
6326 }
6327}
6328
6329/*
6330 * "term_setansicolors(buf, list)" function
6331 */
6332 void
6333f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6334{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006335 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006336 term_T *term;
6337
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006338 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006339 && (check_for_buffer_arg(argvars, 0) == FAIL
6340 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006341 return;
6342
6343 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006344 if (buf == NULL)
6345 return;
6346 term = buf->b_term;
6347 if (term->tl_vterm == NULL)
6348 return;
6349
6350 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6351 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006352 emsg(_(e_list_required));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006353 return;
6354 }
6355
6356 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006357 emsg(_(e_invalid_argument));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006358}
6359#endif
6360
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006361/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006362 * "term_setapi(buf, api)" function
6363 */
6364 void
6365f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6366{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006367 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006368 term_T *term;
6369 char_u *api;
6370
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006371 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006372 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006373 || check_for_string_arg(argvars, 1) == FAIL))
6374 return;
6375
6376 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006377 if (buf == NULL)
6378 return;
6379 term = buf->b_term;
6380 vim_free(term->tl_api);
6381 api = tv_get_string_chk(&argvars[1]);
6382 if (api != NULL)
6383 term->tl_api = vim_strsave(api);
6384 else
6385 term->tl_api = NULL;
6386}
6387
6388/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006389 * "term_setrestore(buf, command)" function
6390 */
6391 void
6392f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6393{
6394#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006395 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006396 term_T *term;
6397 char_u *cmd;
6398
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006399 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006400 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006401 || check_for_string_arg(argvars, 1) == FAIL))
6402 return;
6403
6404 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006405 if (buf == NULL)
6406 return;
6407 term = buf->b_term;
6408 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006409 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006410 if (cmd != NULL)
6411 term->tl_command = vim_strsave(cmd);
6412 else
6413 term->tl_command = NULL;
6414#endif
6415}
6416
6417/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006418 * "term_setkill(buf, how)" function
6419 */
6420 void
6421f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6422{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006423 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006424 term_T *term;
6425 char_u *how;
6426
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006427 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006428 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006429 || check_for_string_arg(argvars, 1) == FAIL))
6430 return;
6431
6432 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006433 if (buf == NULL)
6434 return;
6435 term = buf->b_term;
6436 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006437 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006438 if (how != NULL)
6439 term->tl_kill = vim_strsave(how);
6440 else
6441 term->tl_kill = NULL;
6442}
6443
6444/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006445 * "term_start(command, options)" function
6446 */
6447 void
6448f_term_start(typval_T *argvars, typval_T *rettv)
6449{
6450 jobopt_T opt;
6451 buf_T *buf;
6452
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006453 if (in_vim9script()
6454 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6455 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6456 return;
6457
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006458 init_job_options(&opt);
6459 if (argvars[1].v_type != VAR_UNKNOWN
6460 && get_job_options(&argvars[1], &opt,
6461 JO_TIMEOUT_ALL + JO_STOPONEXIT
6462 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6463 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6464 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6465 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006466 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006467 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006468 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006469 return;
6470
Bram Moolenaar13568252018-03-16 20:46:58 +01006471 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006472
6473 if (buf != NULL && buf->b_term != NULL)
6474 rettv->vval.v_number = buf->b_fnum;
6475}
6476
6477/*
6478 * "term_wait" function
6479 */
6480 void
6481f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6482{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006483 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006484
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006485 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006486 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006487 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006488 return;
6489
6490 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006491 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006492 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006493 if (buf->b_term->tl_job == NULL)
6494 {
6495 ch_log(NULL, "term_wait(): no job to wait for");
6496 return;
6497 }
6498 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006499 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006500 return;
6501
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006502 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006503 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006504 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6505 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006506 // The job is dead, keep reading channel I/O until the channel is
6507 // closed. buf->b_term may become NULL if the terminal was closed while
6508 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006509 ch_log(NULL, "term_wait(): waiting for channel to close");
6510 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6511 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006512 term_flush_messages();
6513
Bram Moolenaard45aa552018-05-21 22:50:29 +02006514 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006515 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006516 // If the terminal is closed when the channel is closed the
6517 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006518 break;
Bram Moolenaareea32af2021-11-21 14:51:13 +00006519 if (buf->b_term == NULL || buf->b_term->tl_channel_closing)
6520 // came here from a close callback, only wait one time
6521 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006522 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006523
6524 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006525 }
6526 else
6527 {
6528 long wait = 10L;
6529
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006530 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006531
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006532 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006533 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006534 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006535 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006536
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006537 // Flushing messages on channels is hopefully sufficient.
6538 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006539 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006540 }
6541}
6542
6543/*
6544 * Called when a channel has sent all the lines to a terminal.
6545 * Send a CTRL-D to mark the end of the text.
6546 */
6547 void
6548term_send_eof(channel_T *ch)
6549{
6550 term_T *term;
6551
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006552 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006553 if (term->tl_job == ch->ch_job)
6554 {
6555 if (term->tl_eof_chars != NULL)
6556 {
6557 channel_send(ch, PART_IN, term->tl_eof_chars,
6558 (int)STRLEN(term->tl_eof_chars), NULL);
6559 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6560 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006561# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006562 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006563 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006564 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6565# endif
6566 }
6567}
6568
Bram Moolenaar113e1072019-01-20 15:30:40 +01006569#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006570 job_T *
6571term_getjob(term_T *term)
6572{
6573 return term != NULL ? term->tl_job : NULL;
6574}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006575#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006576
Bram Moolenaar4f974752019-02-17 17:44:42 +01006577# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006578
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006579///////////////////////////////////////
6580// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006581#ifdef PROTO
6582typedef int COORD;
6583typedef int DWORD;
6584typedef int HANDLE;
6585typedef int *DWORD_PTR;
6586typedef int HPCON;
6587typedef int HRESULT;
6588typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006589typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006590typedef int PSIZE_T;
6591typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006592typedef int BOOL;
6593# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006594#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006595
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006596HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6597HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6598HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006599BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6600BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6601void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006602
6603 static int
6604dyn_conpty_init(int verbose)
6605{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006606 static HMODULE hKerneldll = NULL;
6607 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006608 static struct
6609 {
6610 char *name;
6611 FARPROC *ptr;
6612 } conpty_entry[] =
6613 {
6614 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6615 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6616 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6617 {"InitializeProcThreadAttributeList",
6618 (FARPROC*)&pInitializeProcThreadAttributeList},
6619 {"UpdateProcThreadAttribute",
6620 (FARPROC*)&pUpdateProcThreadAttribute},
6621 {"DeleteProcThreadAttributeList",
6622 (FARPROC*)&pDeleteProcThreadAttributeList},
6623 {NULL, NULL}
6624 };
6625
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006626 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006627 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006628 if (verbose)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006629 emsg(_(e_conpty_is_not_available));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006630 return FAIL;
6631 }
6632
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006633 // No need to initialize twice.
6634 if (hKerneldll)
6635 return OK;
6636
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006637 hKerneldll = vimLoadLib("kernel32.dll");
6638 for (i = 0; conpty_entry[i].name != NULL
6639 && conpty_entry[i].ptr != NULL; ++i)
6640 {
6641 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6642 conpty_entry[i].name)) == NULL)
6643 {
6644 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006645 semsg(_(e_could_not_load_library_function_str), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006646 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006647 return FAIL;
6648 }
6649 }
6650
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006651 return OK;
6652}
6653
6654 static int
6655conpty_term_and_job_init(
6656 term_T *term,
6657 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006658 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006659 jobopt_T *opt,
6660 jobopt_T *orig_opt)
6661{
6662 WCHAR *cmd_wchar = NULL;
6663 WCHAR *cmd_wchar_copy = NULL;
6664 WCHAR *cwd_wchar = NULL;
6665 WCHAR *env_wchar = NULL;
6666 channel_T *channel = NULL;
6667 job_T *job = NULL;
6668 HANDLE jo = NULL;
6669 garray_T ga_cmd, ga_env;
6670 char_u *cmd = NULL;
6671 HRESULT hr;
6672 COORD consize;
6673 SIZE_T breq;
6674 PROCESS_INFORMATION proc_info;
6675 HANDLE i_theirs = NULL;
6676 HANDLE o_theirs = NULL;
6677 HANDLE i_ours = NULL;
6678 HANDLE o_ours = NULL;
6679
Bram Moolenaar04935fb2022-01-08 16:19:22 +00006680 ga_init2(&ga_cmd, sizeof(char*), 20);
6681 ga_init2(&ga_env, sizeof(char*), 20);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006682
6683 if (argvar->v_type == VAR_STRING)
6684 {
6685 cmd = argvar->vval.v_string;
6686 }
6687 else if (argvar->v_type == VAR_LIST)
6688 {
6689 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6690 goto failed;
6691 cmd = ga_cmd.ga_data;
6692 }
6693 if (cmd == NULL || *cmd == NUL)
6694 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006695 emsg(_(e_invalid_argument));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006696 goto failed;
6697 }
6698
6699 term->tl_arg0_cmd = vim_strsave(cmd);
6700
6701 cmd_wchar = enc_to_utf16(cmd, NULL);
6702
6703 if (cmd_wchar != NULL)
6704 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006705 // Request by CreateProcessW
6706 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006707 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006708 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6709 }
6710
6711 ga_clear(&ga_cmd);
6712 if (cmd_wchar == NULL)
6713 goto failed;
6714 if (opt->jo_cwd != NULL)
6715 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6716
6717 win32_build_env(opt->jo_env, &ga_env, TRUE);
6718 env_wchar = ga_env.ga_data;
6719
6720 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6721 goto failed;
6722 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6723 goto failed;
6724
6725 consize.X = term->tl_cols;
6726 consize.Y = term->tl_rows;
6727 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6728 &term->tl_conpty);
6729 if (FAILED(hr))
6730 goto failed;
6731
6732 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6733
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006734 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006735 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006736 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006737 if (!term->tl_siex.lpAttributeList)
6738 goto failed;
6739 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6740 0, &breq))
6741 goto failed;
6742 if (!pUpdateProcThreadAttribute(
6743 term->tl_siex.lpAttributeList, 0,
6744 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6745 sizeof(HPCON), NULL, NULL))
6746 goto failed;
6747
6748 channel = add_channel();
6749 if (channel == NULL)
6750 goto failed;
6751
6752 job = job_alloc();
6753 if (job == NULL)
6754 goto failed;
6755 if (argvar->v_type == VAR_STRING)
6756 {
6757 int argc;
6758
6759 build_argv_from_string(cmd, &job->jv_argv, &argc);
6760 }
6761 else
6762 {
6763 int argc;
6764
6765 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6766 }
6767
6768 if (opt->jo_set & JO_IN_BUF)
6769 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6770
6771 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6772 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006773 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006774 env_wchar, cwd_wchar,
6775 &term->tl_siex.StartupInfo, &proc_info))
6776 goto failed;
6777
6778 CloseHandle(i_theirs);
6779 CloseHandle(o_theirs);
6780
6781 channel_set_pipes(channel,
6782 (sock_T)i_ours,
6783 (sock_T)o_ours,
6784 (sock_T)o_ours);
6785
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006786 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006787 channel->ch_write_text_mode = TRUE;
6788
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006789 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006790 channel->ch_anonymous_pipe = TRUE;
6791
6792 jo = CreateJobObject(NULL, NULL);
6793 if (jo == NULL)
6794 goto failed;
6795
6796 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6797 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006798 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006799 CloseHandle(jo);
6800 jo = NULL;
6801 }
6802
6803 ResumeThread(proc_info.hThread);
6804 CloseHandle(proc_info.hThread);
6805
6806 vim_free(cmd_wchar);
6807 vim_free(cmd_wchar_copy);
6808 vim_free(cwd_wchar);
6809 vim_free(env_wchar);
6810
6811 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6812 goto failed;
6813
6814#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6815 if (opt->jo_set2 & JO2_ANSI_COLORS)
6816 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6817 else
6818 init_vterm_ansi_colors(term->tl_vterm);
6819#endif
6820
6821 channel_set_job(channel, job, opt);
6822 job_set_options(job, opt);
6823
6824 job->jv_channel = channel;
6825 job->jv_proc_info = proc_info;
6826 job->jv_job_object = jo;
6827 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006828 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006829 ++job->jv_refcount;
6830 term->tl_job = job;
6831
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006832 // Redirecting stdout and stderr doesn't work at the job level. Instead
6833 // open the file here and handle it in. opt->jo_io was changed in
6834 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006835 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6836 {
6837 char_u *fname = opt->jo_io_name[PART_OUT];
6838
6839 ch_log(channel, "Opening output file %s", fname);
6840 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6841 if (term->tl_out_fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006842 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006843 }
6844
6845 return OK;
6846
6847failed:
6848 ga_clear(&ga_cmd);
6849 ga_clear(&ga_env);
6850 vim_free(cmd_wchar);
6851 vim_free(cmd_wchar_copy);
6852 vim_free(cwd_wchar);
6853 if (channel != NULL)
6854 channel_clear(channel);
6855 if (job != NULL)
6856 {
6857 job->jv_channel = NULL;
6858 job_cleanup(job);
6859 }
6860 term->tl_job = NULL;
6861 if (jo != NULL)
6862 CloseHandle(jo);
6863
6864 if (term->tl_siex.lpAttributeList != NULL)
6865 {
6866 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6867 vim_free(term->tl_siex.lpAttributeList);
6868 }
6869 term->tl_siex.lpAttributeList = NULL;
6870 if (o_theirs != NULL)
6871 CloseHandle(o_theirs);
6872 if (o_ours != NULL)
6873 CloseHandle(o_ours);
6874 if (i_ours != NULL)
6875 CloseHandle(i_ours);
6876 if (i_theirs != NULL)
6877 CloseHandle(i_theirs);
6878 if (term->tl_conpty != NULL)
6879 pClosePseudoConsole(term->tl_conpty);
6880 term->tl_conpty = NULL;
6881 return FAIL;
6882}
6883
6884 static void
6885conpty_term_report_winsize(term_T *term, int rows, int cols)
6886{
6887 COORD consize;
6888
6889 consize.X = cols;
6890 consize.Y = rows;
6891 pResizePseudoConsole(term->tl_conpty, consize);
6892}
6893
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006894 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006895term_free_conpty(term_T *term)
6896{
6897 if (term->tl_siex.lpAttributeList != NULL)
6898 {
6899 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6900 vim_free(term->tl_siex.lpAttributeList);
6901 }
6902 term->tl_siex.lpAttributeList = NULL;
6903 if (term->tl_conpty != NULL)
6904 pClosePseudoConsole(term->tl_conpty);
6905 term->tl_conpty = NULL;
6906}
6907
6908 int
6909use_conpty(void)
6910{
6911 return has_conpty;
6912}
6913
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006914# ifndef PROTO
6915
6916#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6917#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006918#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006919
6920void* (*winpty_config_new)(UINT64, void*);
6921void* (*winpty_open)(void*, void*);
6922void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6923BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6924void (*winpty_config_set_mouse_mode)(void*, int);
6925void (*winpty_config_set_initial_size)(void*, int, int);
6926LPCWSTR (*winpty_conin_name)(void*);
6927LPCWSTR (*winpty_conout_name)(void*);
6928LPCWSTR (*winpty_conerr_name)(void*);
6929void (*winpty_free)(void*);
6930void (*winpty_config_free)(void*);
6931void (*winpty_spawn_config_free)(void*);
6932void (*winpty_error_free)(void*);
6933LPCWSTR (*winpty_error_msg)(void*);
6934BOOL (*winpty_set_size)(void*, int, int, void*);
6935HANDLE (*winpty_agent_process)(void*);
6936
6937#define WINPTY_DLL "winpty.dll"
6938
6939static HINSTANCE hWinPtyDLL = NULL;
6940# endif
6941
6942 static int
6943dyn_winpty_init(int verbose)
6944{
6945 int i;
6946 static struct
6947 {
6948 char *name;
6949 FARPROC *ptr;
6950 } winpty_entry[] =
6951 {
6952 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6953 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6954 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6955 {"winpty_config_set_mouse_mode",
6956 (FARPROC*)&winpty_config_set_mouse_mode},
6957 {"winpty_config_set_initial_size",
6958 (FARPROC*)&winpty_config_set_initial_size},
6959 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6960 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6961 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6962 {"winpty_free", (FARPROC*)&winpty_free},
6963 {"winpty_open", (FARPROC*)&winpty_open},
6964 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6965 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6966 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6967 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6968 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6969 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6970 {NULL, NULL}
6971 };
6972
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006973 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006974 if (hWinPtyDLL)
6975 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006976 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6977 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006978 if (*p_winptydll != NUL)
6979 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6980 if (!hWinPtyDLL)
6981 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6982 if (!hWinPtyDLL)
6983 {
6984 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006985 semsg(_(e_could_not_load_library_str_str),
Martin Tournoij1a3e5742021-07-24 13:57:29 +02006986 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
6987 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006988 return FAIL;
6989 }
6990 for (i = 0; winpty_entry[i].name != NULL
6991 && winpty_entry[i].ptr != NULL; ++i)
6992 {
6993 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6994 winpty_entry[i].name)) == NULL)
6995 {
6996 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006997 semsg(_(e_could_not_load_library_function_str), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006998 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006999 return FAIL;
7000 }
7001 }
7002
7003 return OK;
7004}
7005
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007006 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007007winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007008 term_T *term,
7009 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007010 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007011 jobopt_T *opt,
7012 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007013{
7014 WCHAR *cmd_wchar = NULL;
7015 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007016 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007017 channel_T *channel = NULL;
7018 job_T *job = NULL;
7019 DWORD error;
7020 HANDLE jo = NULL;
7021 HANDLE child_process_handle;
7022 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01007023 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007024 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007025 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007026 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007027
Bram Moolenaar04935fb2022-01-08 16:19:22 +00007028 ga_init2(&ga_cmd, sizeof(char*), 20);
7029 ga_init2(&ga_env, sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007030
7031 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007032 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007033 cmd = argvar->vval.v_string;
7034 }
7035 else if (argvar->v_type == VAR_LIST)
7036 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007037 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007038 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007039 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007040 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007041 if (cmd == NULL || *cmd == NUL)
7042 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007043 emsg(_(e_invalid_argument));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007044 goto failed;
7045 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007046
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007047 term->tl_arg0_cmd = vim_strsave(cmd);
7048
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007049 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007050 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007051 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007052 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007053 if (opt->jo_cwd != NULL)
7054 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007055
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007056 win32_build_env(opt->jo_env, &ga_env, TRUE);
7057 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007058
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007059 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
7060 if (term->tl_winpty_config == NULL)
7061 goto failed;
7062
7063 winpty_config_set_mouse_mode(term->tl_winpty_config,
7064 WINPTY_MOUSE_MODE_FORCE);
7065 winpty_config_set_initial_size(term->tl_winpty_config,
7066 term->tl_cols, term->tl_rows);
7067 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
7068 if (term->tl_winpty == NULL)
7069 goto failed;
7070
7071 spawn_config = winpty_spawn_config_new(
7072 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
7073 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
7074 NULL,
7075 cmd_wchar,
7076 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007077 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007078 &winpty_err);
7079 if (spawn_config == NULL)
7080 goto failed;
7081
7082 channel = add_channel();
7083 if (channel == NULL)
7084 goto failed;
7085
7086 job = job_alloc();
7087 if (job == NULL)
7088 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02007089 if (argvar->v_type == VAR_STRING)
7090 {
7091 int argc;
7092
7093 build_argv_from_string(cmd, &job->jv_argv, &argc);
7094 }
7095 else
7096 {
7097 int argc;
7098
7099 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
7100 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007101
7102 if (opt->jo_set & JO_IN_BUF)
7103 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
7104
7105 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
7106 &child_thread_handle, &error, &winpty_err))
7107 goto failed;
7108
7109 channel_set_pipes(channel,
7110 (sock_T)CreateFileW(
7111 winpty_conin_name(term->tl_winpty),
7112 GENERIC_WRITE, 0, NULL,
7113 OPEN_EXISTING, 0, NULL),
7114 (sock_T)CreateFileW(
7115 winpty_conout_name(term->tl_winpty),
7116 GENERIC_READ, 0, NULL,
7117 OPEN_EXISTING, 0, NULL),
7118 (sock_T)CreateFileW(
7119 winpty_conerr_name(term->tl_winpty),
7120 GENERIC_READ, 0, NULL,
7121 OPEN_EXISTING, 0, NULL));
7122
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007123 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007124 channel->ch_write_text_mode = TRUE;
7125
7126 jo = CreateJobObject(NULL, NULL);
7127 if (jo == NULL)
7128 goto failed;
7129
7130 if (!AssignProcessToJobObject(jo, child_process_handle))
7131 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007132 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007133 CloseHandle(jo);
7134 jo = NULL;
7135 }
7136
7137 winpty_spawn_config_free(spawn_config);
7138 vim_free(cmd_wchar);
7139 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007140 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007141
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007142 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7143 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007144
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007145#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7146 if (opt->jo_set2 & JO2_ANSI_COLORS)
7147 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7148 else
7149 init_vterm_ansi_colors(term->tl_vterm);
7150#endif
7151
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007152 channel_set_job(channel, job, opt);
7153 job_set_options(job, opt);
7154
7155 job->jv_channel = channel;
7156 job->jv_proc_info.hProcess = child_process_handle;
7157 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7158 job->jv_job_object = jo;
7159 job->jv_status = JOB_STARTED;
7160 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007161 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007162 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007163 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007164 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007165 ++job->jv_refcount;
7166 term->tl_job = job;
7167
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007168 // Redirecting stdout and stderr doesn't work at the job level. Instead
7169 // open the file here and handle it in. opt->jo_io was changed in
7170 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007171 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7172 {
7173 char_u *fname = opt->jo_io_name[PART_OUT];
7174
7175 ch_log(channel, "Opening output file %s", fname);
7176 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7177 if (term->tl_out_fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007178 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007179 }
7180
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007181 return OK;
7182
7183failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007184 ga_clear(&ga_cmd);
7185 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007186 vim_free(cmd_wchar);
7187 vim_free(cwd_wchar);
7188 if (spawn_config != NULL)
7189 winpty_spawn_config_free(spawn_config);
7190 if (channel != NULL)
7191 channel_clear(channel);
7192 if (job != NULL)
7193 {
7194 job->jv_channel = NULL;
7195 job_cleanup(job);
7196 }
7197 term->tl_job = NULL;
7198 if (jo != NULL)
7199 CloseHandle(jo);
7200 if (term->tl_winpty != NULL)
7201 winpty_free(term->tl_winpty);
7202 term->tl_winpty = NULL;
7203 if (term->tl_winpty_config != NULL)
7204 winpty_config_free(term->tl_winpty_config);
7205 term->tl_winpty_config = NULL;
7206 if (winpty_err != NULL)
7207 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007208 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007209 (short_u *)winpty_error_msg(winpty_err), NULL);
7210
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007211 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007212 winpty_error_free(winpty_err);
7213 }
7214 return FAIL;
7215}
7216
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007217/*
7218 * Create a new terminal of "rows" by "cols" cells.
7219 * Store a reference in "term".
7220 * Return OK or FAIL.
7221 */
7222 static int
7223term_and_job_init(
7224 term_T *term,
7225 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007226 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007227 jobopt_T *opt,
7228 jobopt_T *orig_opt)
7229{
7230 int use_winpty = FALSE;
7231 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007232 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007233
7234 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7235 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7236
7237 if (!has_winpty && !has_conpty)
7238 // If neither is available give the errors for winpty, since when
7239 // conpty is not available it can't be installed either.
7240 return dyn_winpty_init(TRUE);
7241
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007242 if (opt->jo_tty_type != NUL)
7243 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007244
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007245 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007246 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007247 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007248 use_conpty = TRUE;
7249 else if (has_winpty)
7250 use_winpty = TRUE;
7251 // else: error
7252 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007253 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007254 {
7255 if (has_winpty)
7256 use_winpty = TRUE;
7257 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007258 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007259 {
7260 if (has_conpty)
7261 use_conpty = TRUE;
7262 else
7263 return dyn_conpty_init(TRUE);
7264 }
7265
7266 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007267 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007268
7269 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007270 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007271
7272 // error
7273 return dyn_winpty_init(TRUE);
7274}
7275
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007276 static int
7277create_pty_only(term_T *term, jobopt_T *options)
7278{
7279 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7280 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7281 char in_name[80], out_name[80];
7282 channel_T *channel = NULL;
7283
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007284 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7285 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007286
7287 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7288 GetCurrentProcessId(),
7289 curbuf->b_fnum);
7290 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7291 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7292 PIPE_UNLIMITED_INSTANCES,
7293 0, 0, NMPWAIT_NOWAIT, NULL);
7294 if (hPipeIn == INVALID_HANDLE_VALUE)
7295 goto failed;
7296
7297 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7298 GetCurrentProcessId(),
7299 curbuf->b_fnum);
7300 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7301 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7302 PIPE_UNLIMITED_INSTANCES,
7303 0, 0, 0, NULL);
7304 if (hPipeOut == INVALID_HANDLE_VALUE)
7305 goto failed;
7306
7307 ConnectNamedPipe(hPipeIn, NULL);
7308 ConnectNamedPipe(hPipeOut, NULL);
7309
7310 term->tl_job = job_alloc();
7311 if (term->tl_job == NULL)
7312 goto failed;
7313 ++term->tl_job->jv_refcount;
7314
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007315 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007316 term->tl_job->jv_status = JOB_FINISHED;
7317
7318 channel = add_channel();
7319 if (channel == NULL)
7320 goto failed;
7321 term->tl_job->jv_channel = channel;
7322 channel->ch_keep_open = TRUE;
7323 channel->ch_named_pipe = TRUE;
7324
7325 channel_set_pipes(channel,
7326 (sock_T)hPipeIn,
7327 (sock_T)hPipeOut,
7328 (sock_T)hPipeOut);
7329 channel_set_job(channel, term->tl_job, options);
7330 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7331 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7332
7333 return OK;
7334
7335failed:
7336 if (hPipeIn != NULL)
7337 CloseHandle(hPipeIn);
7338 if (hPipeOut != NULL)
7339 CloseHandle(hPipeOut);
7340 return FAIL;
7341}
7342
7343/*
7344 * Free the terminal emulator part of "term".
7345 */
7346 static void
7347term_free_vterm(term_T *term)
7348{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007349 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007350 if (term->tl_winpty != NULL)
7351 winpty_free(term->tl_winpty);
7352 term->tl_winpty = NULL;
7353 if (term->tl_winpty_config != NULL)
7354 winpty_config_free(term->tl_winpty_config);
7355 term->tl_winpty_config = NULL;
7356 if (term->tl_vterm != NULL)
7357 vterm_free(term->tl_vterm);
7358 term->tl_vterm = NULL;
7359}
7360
7361/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007362 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007363 */
7364 static void
7365term_report_winsize(term_T *term, int rows, int cols)
7366{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007367 if (term->tl_conpty)
7368 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007369 if (term->tl_winpty)
7370 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7371}
7372
7373 int
7374terminal_enabled(void)
7375{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007376 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007377}
7378
7379# else
7380
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007381///////////////////////////////////////
7382// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007383
7384/*
7385 * Create a new terminal of "rows" by "cols" cells.
7386 * Start job for "cmd".
7387 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007388 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007389 * Return OK or FAIL.
7390 */
7391 static int
7392term_and_job_init(
7393 term_T *term,
7394 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007395 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007396 jobopt_T *opt,
7397 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007398{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007399 term->tl_arg0_cmd = NULL;
7400
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007401 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7402 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007403
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007404#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7405 if (opt->jo_set2 & JO2_ANSI_COLORS)
7406 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7407 else
7408 init_vterm_ansi_colors(term->tl_vterm);
7409#endif
7410
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007411 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007412 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007413 if (term->tl_job != NULL)
7414 ++term->tl_job->jv_refcount;
7415
7416 return term->tl_job != NULL
7417 && term->tl_job->jv_channel != NULL
7418 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7419}
7420
7421 static int
7422create_pty_only(term_T *term, jobopt_T *opt)
7423{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007424 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7425 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007426
7427 term->tl_job = job_alloc();
7428 if (term->tl_job == NULL)
7429 return FAIL;
7430 ++term->tl_job->jv_refcount;
7431
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007432 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007433 term->tl_job->jv_status = JOB_FINISHED;
7434
7435 return mch_create_pty_channel(term->tl_job, opt);
7436}
7437
7438/*
7439 * Free the terminal emulator part of "term".
7440 */
7441 static void
7442term_free_vterm(term_T *term)
7443{
7444 if (term->tl_vterm != NULL)
7445 vterm_free(term->tl_vterm);
7446 term->tl_vterm = NULL;
7447}
7448
7449/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007450 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007451 */
7452 static void
7453term_report_winsize(term_T *term, int rows, int cols)
7454{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007455 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007456 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7457 {
7458 int fd = -1;
7459 int part;
7460
7461 for (part = PART_OUT; part < PART_COUNT; ++part)
7462 {
7463 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007464 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007465 break;
7466 }
7467 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7468 mch_signal_job(term->tl_job, (char_u *)"winch");
7469 }
7470}
7471
7472# endif
7473
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007474#endif // FEAT_TERMINAL