blob: 0849f2c007b73c74fba59f496c35597b6f685f98 [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;
Bram Moolenaar68afde42022-02-25 20:48:26 +00004196 color.index = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004197 vterm_state_set_palette_color(state, index, &color);
4198 }
4199}
4200
4201/*
4202 * Set the ANSI color palette from a list of colors
4203 */
4204 static int
4205set_ansi_colors_list(VTerm *vterm, list_T *list)
4206{
4207 int n = 0;
4208 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004209 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004210
Bram Moolenaarb0992022020-01-30 14:55:42 +01004211 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004212 {
4213 char_u *color_name;
4214 guicolor_T guicolor;
4215
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004216 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004217 if (color_name == NULL)
4218 return FAIL;
4219
4220 guicolor = GUI_GET_COLOR(color_name);
4221 if (guicolor == INVALCOLOR)
4222 return FAIL;
4223
4224 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4225 }
4226
4227 if (n != 16 || li != NULL)
4228 return FAIL;
4229
4230 set_vterm_palette(vterm, rgb);
4231
4232 return OK;
4233}
4234
4235/*
4236 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4237 */
4238 static void
4239init_vterm_ansi_colors(VTerm *vterm)
4240{
4241 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4242
4243 if (var != NULL
4244 && (var->di_tv.v_type != VAR_LIST
4245 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004246 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004247 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004248 semsg(_(e_invalid_argument_str), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004249}
4250#endif
4251
Bram Moolenaar52acb112018-03-18 19:20:22 +01004252/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004253 * Handles a "drop" command from the job in the terminal.
4254 * "item" is the file name, "item->li_next" may have options.
4255 */
4256 static void
4257handle_drop_command(listitem_T *item)
4258{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004259 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004260 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004261 int bufnr;
4262 win_T *wp;
4263 tabpage_T *tp;
4264 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004265 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004266
4267 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4268 FOR_ALL_TAB_WINDOWS(tp, wp)
4269 {
4270 if (wp->w_buffer->b_fnum == bufnr)
4271 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004272 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004273 goto_tabpage_win(tp, wp);
4274 return;
4275 }
4276 }
4277
Bram Moolenaara80faa82020-04-12 19:37:17 +02004278 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004279
4280 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4281 && opt_item->li_tv.vval.v_dict != NULL)
4282 {
4283 dict_T *dict = opt_item->li_tv.vval.v_dict;
4284 char_u *p;
4285
Bram Moolenaar8f667172018-12-14 15:38:31 +01004286 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004287 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004288 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004289 if (p != NULL)
4290 {
4291 if (check_ff_value(p) == FAIL)
4292 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4293 else
4294 ea.force_ff = *p;
4295 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004296 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004297 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004298 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004299 if (p != NULL)
4300 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004301 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004302 if (ea.cmd != NULL)
4303 {
4304 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4305 ea.force_enc = 11;
4306 tofree = ea.cmd;
4307 }
4308 }
4309
Bram Moolenaar8f667172018-12-14 15:38:31 +01004310 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004311 if (p != NULL)
4312 get_bad_opt(p, &ea);
4313
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004314 if (dict_has_key(dict, "bin"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004315 ea.force_bin = FORCE_BIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004316 if (dict_has_key(dict, "binary"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004317 ea.force_bin = FORCE_BIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004318 if (dict_has_key(dict, "nobin"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004319 ea.force_bin = FORCE_NOBIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004320 if (dict_has_key(dict, "nobinary"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004321 ea.force_bin = FORCE_NOBIN;
4322 }
4323
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004324 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004325 if (ea.cmd == NULL)
4326 ea.cmd = (char_u *)"split";
4327 ea.arg = fname;
4328 ea.cmdidx = CMD_split;
4329 ex_splitview(&ea);
4330
4331 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004332}
4333
4334/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004335 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4336 */
4337 static int
4338is_permitted_term_api(char_u *func, char_u *pat)
4339{
4340 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4341}
4342
4343/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004344 * Handles a function call from the job running in a terminal.
4345 * "item" is the function name, "item->li_next" has the arguments.
4346 */
4347 static void
4348handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4349{
4350 char_u *func;
4351 typval_T argvars[2];
4352 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004353 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004354
4355 if (item->li_next == NULL)
4356 {
4357 ch_log(channel, "Missing function arguments for call");
4358 return;
4359 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004360 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004361
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004362 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004363 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004364 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004365 return;
4366 }
4367
4368 argvars[0].v_type = VAR_NUMBER;
4369 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4370 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004371 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004372 funcexe.fe_firstline = 1L;
4373 funcexe.fe_lastline = 1L;
4374 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004375 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004376 {
4377 clear_tv(&rettv);
4378 ch_log(channel, "Function %s called", func);
4379 }
4380 else
4381 ch_log(channel, "Calling function %s failed", func);
4382}
4383
4384/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004385 * URL decoding (also know as Percent-encoding).
4386 *
4387 * Note this function currently is only used for decoding shell's
4388 * OSC 7 escape sequence which we can assume all bytes are valid
4389 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4390 * encoding bytes like 0xfe, 0xff.
4391 */
4392 static size_t
4393url_decode(const char *src, const size_t len, char_u *dst)
4394{
4395 size_t i = 0, j = 0;
4396
4397 while (i < len)
4398 {
4399 if (src[i] == '%' && i + 2 < len)
4400 {
4401 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4402 j++;
4403 i += 3;
4404 }
4405 else
4406 {
4407 dst[j] = src[i];
4408 i++;
4409 j++;
4410 }
4411 }
4412 dst[j] = '\0';
4413 return j;
4414}
4415
4416/*
4417 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4418 *
4419 * The OSC 7 sequence has the format of
4420 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4421 * and what VTerm provides via VTermStringFragment is
4422 * "file://HOSTNAME/CURRENT/DIR"
4423 */
4424 static void
4425sync_shell_dir(VTermStringFragment *frag)
4426{
4427 int offset = 7; // len of "file://" is 7
4428 char *pos = (char *)frag->str + offset;
4429 char_u *new_dir;
4430
4431 // remove HOSTNAME to get PWD
Bram Moolenaar918b0892021-05-08 20:09:24 +02004432 while (*pos != '/' && offset < (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004433 {
4434 offset += 1;
4435 pos += 1;
4436 }
4437
Bram Moolenaar918b0892021-05-08 20:09:24 +02004438 if (offset >= (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004439 {
4440 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
4441 frag->str);
4442 return;
4443 }
4444
4445 new_dir = alloc(frag->len - offset + 1);
4446 url_decode(pos, frag->len-offset, new_dir);
4447 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4448 vim_free(new_dir);
4449}
4450
4451/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004452 * Called by libvterm when it cannot recognize an OSC sequence.
4453 * We recognize a terminal API command.
4454 */
4455 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004456parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004457{
4458 term_T *term = (term_T *)user;
4459 js_read_T reader;
4460 typval_T tv;
4461 channel_T *channel = term->tl_job == NULL ? NULL
4462 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004463 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004464
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004465 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
4466 if (p_asd && command == 7)
4467 {
4468 sync_shell_dir(&frag);
4469 return 1;
4470 }
4471
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004472 if (command != 51)
4473 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004474
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004475 // Concatenate what was received until the final piece is found.
4476 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4477 {
4478 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004479 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004480 }
4481 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004482 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004483 if (!frag.final)
4484 return 1;
4485
4486 ((char *)gap->ga_data)[gap->ga_len] = 0;
4487 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004488 reader.js_fill = NULL;
4489 reader.js_used = 0;
4490 if (json_decode(&reader, &tv, 0) == OK
4491 && tv.v_type == VAR_LIST
4492 && tv.vval.v_list != NULL)
4493 {
4494 listitem_T *item = tv.vval.v_list->lv_first;
4495
4496 if (item == NULL)
4497 ch_log(channel, "Missing command");
4498 else
4499 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004500 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004501
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004502 // Make sure an invoked command doesn't delete the buffer (and the
4503 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004504 ++term->tl_buffer->b_locked;
4505
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004506 item = item->li_next;
4507 if (item == NULL)
4508 ch_log(channel, "Missing argument for %s", cmd);
4509 else if (STRCMP(cmd, "drop") == 0)
4510 handle_drop_command(item);
4511 else if (STRCMP(cmd, "call") == 0)
4512 handle_call_command(term, channel, item);
4513 else
4514 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004515 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004516 }
4517 }
4518 else
4519 ch_log(channel, "Invalid JSON received");
4520
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004521 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004522 clear_tv(&tv);
4523 return 1;
4524}
4525
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004526/*
4527 * Called by libvterm when it cannot recognize a CSI sequence.
4528 * We recognize the window position report.
4529 */
4530 static int
4531parse_csi(
4532 const char *leader UNUSED,
4533 const long args[],
4534 int argcount,
4535 const char *intermed UNUSED,
4536 char command,
4537 void *user)
4538{
4539 term_T *term = (term_T *)user;
4540 char buf[100];
4541 int len;
4542 int x = 0;
4543 int y = 0;
4544 win_T *wp;
4545
4546 // We recognize only CSI 13 t
4547 if (command != 't' || argcount != 1 || args[0] != 13)
4548 return 0; // not handled
4549
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004550 // When getting the window position is not possible or it fails it results
4551 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004552#if defined(FEAT_GUI) \
4553 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4554 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004555 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004556#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004557
4558 FOR_ALL_WINDOWS(wp)
4559 if (wp->w_buffer == term->tl_buffer)
4560 break;
4561 if (wp != NULL)
4562 {
4563#ifdef FEAT_GUI
4564 if (gui.in_use)
4565 {
4566 x += wp->w_wincol * gui.char_width;
4567 y += W_WINROW(wp) * gui.char_height;
4568 }
4569 else
4570#endif
4571 {
4572 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004573 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004574 x += wp->w_wincol * 7;
4575 y += W_WINROW(wp) * 10;
4576 }
4577 }
4578
4579 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4580 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4581 (char_u *)buf, len, NULL);
4582 return 1;
4583}
4584
Bram Moolenaard8637282020-05-20 18:41:41 +02004585static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004586 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004587 parse_csi, // csi
4588 parse_osc, // osc
Bram Moolenaar7da34152021-11-24 19:30:55 +00004589 NULL, // dcs
4590 NULL, // apc
4591 NULL, // pm
4592 NULL // sos
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004593};
4594
4595/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004596 * Use Vim's allocation functions for vterm so profiling works.
4597 */
4598 static void *
4599vterm_malloc(size_t size, void *data UNUSED)
4600{
Bram Moolenaar88137392021-11-12 16:01:15 +00004601 // make sure that the length is not zero
4602 return alloc_clear(size == 0 ? 1L : size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004603}
4604
4605 static void
4606vterm_memfree(void *ptr, void *data UNUSED)
4607{
4608 vim_free(ptr);
4609}
4610
4611static VTermAllocatorFunctions vterm_allocator = {
4612 &vterm_malloc,
4613 &vterm_memfree
4614};
4615
4616/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004617 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004618 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004619 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004620 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004621create_vterm(term_T *term, int rows, int cols)
4622{
4623 VTerm *vterm;
4624 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004625 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004626 VTermValue value;
4627
Bram Moolenaar756ef112018-04-10 12:04:27 +02004628 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004629 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004630 if (vterm == NULL)
4631 return FAIL;
4632
4633 // Allocate screen and state here, so we can bail out if that fails.
4634 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004635 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004636 if (state == NULL || screen == NULL)
4637 {
4638 vterm_free(vterm);
4639 return FAIL;
4640 }
4641
Bram Moolenaar52acb112018-03-18 19:20:22 +01004642 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004643 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004644 vterm_set_utf8(vterm, 1);
4645
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004646 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004647
4648 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004649 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004650 &term->tl_default_color.fg,
4651 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004652
Bram Moolenaar9e587872019-05-13 20:27:23 +02004653 if (t_colors < 16)
4654 // Less than 16 colors: assume that bold means using a bright color for
4655 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004656 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4657
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004658 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004659 vterm_screen_reset(screen, 1 /* hard */);
4660
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004661 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004662 vterm_screen_enable_altscreen(screen, 1);
4663
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004664 // For unix do not use a blinking cursor. In an xterm this causes the
4665 // cursor to blink if it's blinking in the xterm.
4666 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004667#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004668 if (GetCaretBlinkTime() == INFINITE)
4669 value.boolean = 0;
4670 else
4671 value.boolean = 1;
4672#else
4673 value.boolean = 0;
4674#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004675 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004676 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004677
4678 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004679}
4680
4681/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004682 * Called when option 'background' or 'termguicolors' was set,
4683 * or when any highlight is changed.
Bram Moolenaarad431992021-05-03 20:40:38 +02004684 */
4685 void
4686term_update_colors_all(void)
4687{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004688 term_T *term;
Bram Moolenaarad431992021-05-03 20:40:38 +02004689
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004690 FOR_ALL_TERMS(term)
4691 {
4692 if (term->tl_vterm == NULL)
4693 continue;
4694 init_default_colors(term);
4695 vterm_state_set_default_colors(
4696 vterm_obtain_state(term->tl_vterm),
4697 &term->tl_default_color.fg,
4698 &term->tl_default_color.bg);
4699 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004700}
4701
4702/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004703 * Return the text to show for the buffer name and status.
4704 */
4705 char_u *
4706term_get_status_text(term_T *term)
4707{
4708 if (term->tl_status_text == NULL)
4709 {
4710 char_u *txt;
4711 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004712 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004713
4714 if (term->tl_normal_mode)
4715 {
4716 if (term_job_running(term))
4717 txt = (char_u *)_("Terminal");
4718 else
4719 txt = (char_u *)_("Terminal-finished");
4720 }
4721 else if (term->tl_title != NULL)
4722 txt = term->tl_title;
4723 else if (term_none_open(term))
4724 txt = (char_u *)_("active");
4725 else if (term_job_running(term))
4726 txt = (char_u *)_("running");
4727 else
4728 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004729 fname = buf_get_fname(term->tl_buffer);
4730 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004731 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004732 if (term->tl_status_text != NULL)
4733 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004734 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004735 }
4736 return term->tl_status_text;
4737}
4738
4739/*
Bram Moolenaar3ad69532021-11-19 17:01:08 +00004740 * Clear the cached value of the status text.
4741 */
4742 void
4743term_clear_status_text(term_T *term)
4744{
4745 VIM_CLEAR(term->tl_status_text);
4746}
4747
4748/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004749 * Mark references in jobs of terminals.
4750 */
4751 int
4752set_ref_in_term(int copyID)
4753{
4754 int abort = FALSE;
4755 term_T *term;
4756 typval_T tv;
4757
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004758 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004759 if (term->tl_job != NULL)
4760 {
4761 tv.v_type = VAR_JOB;
4762 tv.vval.v_job = term->tl_job;
4763 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4764 }
4765 return abort;
4766}
4767
4768/*
4769 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004770 * Returns NULL when the buffer is not for a terminal window and logs a message
4771 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004772 */
4773 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004774term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004775{
4776 buf_T *buf;
4777
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004778 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004779 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004780 --emsg_off;
4781 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004782 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004783 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004784 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004785 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004786 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004787 return buf;
4788}
4789
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004790 static void
4791clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004792{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004793 CLEAR_FIELD(*cell);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004794 cell->fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4795 cell->bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004796}
4797
4798 static void
4799dump_term_color(FILE *fd, VTermColor *color)
4800{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004801 int index;
4802
4803 if (VTERM_COLOR_IS_INDEXED(color))
4804 index = color->index + 1;
4805 else if (color->type == 0)
4806 // use RGB values
4807 index = 255;
4808 else
4809 // default color
4810 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004811 fprintf(fd, "%02x%02x%02x%d",
4812 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004813 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004814}
4815
4816/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004817 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004818 *
4819 * Each screen cell in full is:
4820 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4821 * {characters} is a space for an empty cell
4822 * For a double-width character "+" is changed to "*" and the next cell is
4823 * skipped.
4824 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4825 * when "&" use the same as the previous cell.
4826 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4827 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4828 * {color-idx} is a number from 0 to 255
4829 *
4830 * Screen cell with same width, attributes and color as the previous one:
4831 * |{characters}
4832 *
4833 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4834 *
4835 * Repeating the previous screen cell:
4836 * @{count}
4837 */
4838 void
4839f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4840{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004841 buf_T *buf;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004842 term_T *term;
4843 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004844 int max_height = 0;
4845 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004846 stat_T st;
4847 FILE *fd;
4848 VTermPos pos;
4849 VTermScreen *screen;
4850 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004851 VTermState *state;
4852 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004853
4854 if (check_restricted() || check_secure())
4855 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004856
4857 if (in_vim9script()
4858 && (check_for_buffer_arg(argvars, 0) == FAIL
4859 || check_for_string_arg(argvars, 1) == FAIL
4860 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4861 return;
4862
4863 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004864 if (buf == NULL)
4865 return;
4866 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004867 if (term->tl_vterm == NULL)
4868 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004869 emsg(_(e_job_already_finished));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004870 return;
4871 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004872
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004873 if (argvars[2].v_type != VAR_UNKNOWN)
4874 {
4875 dict_T *d;
4876
4877 if (argvars[2].v_type != VAR_DICT)
4878 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004879 emsg(_(e_dictionary_required));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004880 return;
4881 }
4882 d = argvars[2].vval.v_dict;
4883 if (d != NULL)
4884 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004885 max_height = dict_get_number(d, (char_u *)"rows");
4886 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004887 }
4888 }
4889
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004890 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004891 if (fname == NULL)
4892 return;
4893 if (mch_stat((char *)fname, &st) >= 0)
4894 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004895 semsg(_(e_file_exists_str), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004896 return;
4897 }
4898
Bram Moolenaard96ff162018-02-18 22:13:29 +01004899 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4900 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004901 semsg(_(e_cant_create_file_str), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004902 return;
4903 }
4904
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004905 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004906
4907 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004908 state = vterm_obtain_state(term->tl_vterm);
4909 vterm_state_get_cursorpos(state, &cursor_pos);
4910
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004911 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4912 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004913 {
4914 int repeat = 0;
4915
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004916 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4917 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004918 {
4919 VTermScreenCell cell;
4920 int same_attr;
4921 int same_chars = TRUE;
4922 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004923 int is_cursor_pos = (pos.col == cursor_pos.col
4924 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004925
4926 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004927 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004928
4929 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4930 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004931 int c = cell.chars[i];
4932 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004933 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004934
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004935 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004936 if (i == 0)
4937 {
4938 c = (c == NUL) ? ' ' : c;
4939 pc = (pc == NUL) ? ' ' : pc;
4940 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004941 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004942 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004943 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004944 break;
4945 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004946 same_attr = vtermAttr2hl(&cell.attrs)
4947 == vtermAttr2hl(&prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004948 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4949 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004950 if (same_chars && cell.width == prev_cell.width && same_attr
4951 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004952 {
4953 ++repeat;
4954 }
4955 else
4956 {
4957 if (repeat > 0)
4958 {
4959 fprintf(fd, "@%d", repeat);
4960 repeat = 0;
4961 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004962 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004963
4964 if (cell.chars[0] == NUL)
4965 fputs(" ", fd);
4966 else
4967 {
4968 char_u charbuf[10];
4969 int len;
4970
4971 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4972 && cell.chars[i] != NUL; ++i)
4973 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004974 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004975 fwrite(charbuf, len, 1, fd);
4976 }
4977 }
4978
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004979 // When only the characters differ we don't write anything, the
4980 // following "|", "@" or NL will indicate using the same
4981 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004982 if (cell.width != prev_cell.width || !same_attr)
4983 {
4984 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004985 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004986 else
4987 fputs("+", fd);
4988
4989 if (same_attr)
4990 {
4991 fputs("&", fd);
4992 }
4993 else
4994 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004995 fprintf(fd, "%d", vtermAttr2hl(&cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004996 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004997 fputs("&", fd);
4998 else
4999 {
5000 fputs("#", fd);
5001 dump_term_color(fd, &cell.fg);
5002 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005003 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005004 fputs("&", fd);
5005 else
5006 {
5007 fputs("#", fd);
5008 dump_term_color(fd, &cell.bg);
5009 }
5010 }
5011 }
5012
5013 prev_cell = cell;
5014 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005015
5016 if (cell.width == 2)
5017 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005018 }
5019 if (repeat > 0)
5020 fprintf(fd, "@%d", repeat);
5021 fputs("\n", fd);
5022 }
5023
5024 fclose(fd);
5025}
5026
5027/*
5028 * Called when a dump is corrupted. Put a breakpoint here when debugging.
5029 */
5030 static void
5031dump_is_corrupt(garray_T *gap)
5032{
5033 ga_concat(gap, (char_u *)"CORRUPT");
5034}
5035
5036 static void
5037append_cell(garray_T *gap, cellattr_T *cell)
5038{
5039 if (ga_grow(gap, 1) == OK)
5040 {
5041 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
5042 ++gap->ga_len;
5043 }
5044}
5045
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005046 static void
5047clear_cellattr(cellattr_T *cell)
5048{
5049 CLEAR_FIELD(*cell);
5050 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
5051 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
5052}
5053
Bram Moolenaard96ff162018-02-18 22:13:29 +01005054/*
5055 * Read the dump file from "fd" and append lines to the current buffer.
5056 * Return the cell width of the longest line.
5057 */
5058 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01005059read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005060{
5061 int c;
5062 garray_T ga_text;
5063 garray_T ga_cell;
5064 char_u *prev_char = NULL;
5065 int attr = 0;
5066 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005067 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005068 term_T *term = curbuf->b_term;
5069 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005070 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005071
5072 ga_init2(&ga_text, 1, 90);
5073 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005074 clear_cellattr(&cell);
5075 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005076 cursor_pos->row = -1;
5077 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005078
5079 c = fgetc(fd);
5080 for (;;)
5081 {
5082 if (c == EOF)
5083 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02005084 if (c == '\r')
5085 {
5086 // DOS line endings? Ignore.
5087 c = fgetc(fd);
5088 }
5089 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005090 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005091 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005092 if (ga_text.ga_data == NULL)
5093 dump_is_corrupt(&ga_text);
5094 if (ga_grow(&term->tl_scrollback, 1) == OK)
5095 {
5096 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
5097 + term->tl_scrollback.ga_len;
5098
5099 if (max_cells < ga_cell.ga_len)
5100 max_cells = ga_cell.ga_len;
5101 line->sb_cols = ga_cell.ga_len;
5102 line->sb_cells = ga_cell.ga_data;
5103 line->sb_fill_attr = term->tl_default_color;
5104 ++term->tl_scrollback.ga_len;
5105 ga_init(&ga_cell);
5106
5107 ga_append(&ga_text, NUL);
5108 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5109 ga_text.ga_len, FALSE);
5110 }
5111 else
5112 ga_clear(&ga_cell);
5113 ga_text.ga_len = 0;
5114
5115 c = fgetc(fd);
5116 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005117 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005118 {
5119 int prev_len = ga_text.ga_len;
5120
Bram Moolenaar9271d052018-02-25 21:39:46 +01005121 if (c == '>')
5122 {
5123 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005124 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005125 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5126 cursor_pos->col = ga_cell.ga_len;
5127 }
5128
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005129 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005130 c = fgetc(fd);
5131 if (c != EOF)
5132 ga_append(&ga_text, c);
5133 for (;;)
5134 {
5135 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005136 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005137 || c == EOF || c == '\n')
5138 break;
5139 ga_append(&ga_text, c);
5140 }
5141
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005142 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005143 vim_free(prev_char);
5144 if (ga_text.ga_data != NULL)
5145 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5146 ga_text.ga_len - prev_len);
5147
Bram Moolenaar9271d052018-02-25 21:39:46 +01005148 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005149 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005150 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005151 }
5152 else if (c == '+' || c == '*')
5153 {
5154 int is_bg;
5155
5156 cell.width = c == '+' ? 1 : 2;
5157
5158 c = fgetc(fd);
5159 if (c == '&')
5160 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005161 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005162 c = fgetc(fd);
5163 }
5164 else if (isdigit(c))
5165 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005166 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005167 attr = 0;
5168 while (isdigit(c))
5169 {
5170 attr = attr * 10 + (c - '0');
5171 c = fgetc(fd);
5172 }
5173 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005174
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005175 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005176 for (is_bg = 0; is_bg <= 1; ++is_bg)
5177 {
5178 if (c == '&')
5179 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005180 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005181 c = fgetc(fd);
5182 }
5183 else if (c == '#')
5184 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005185 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005186
5187 c = fgetc(fd);
5188 red = hex2nr(c);
5189 c = fgetc(fd);
5190 red = (red << 4) + hex2nr(c);
5191 c = fgetc(fd);
5192 green = hex2nr(c);
5193 c = fgetc(fd);
5194 green = (green << 4) + hex2nr(c);
5195 c = fgetc(fd);
5196 blue = hex2nr(c);
5197 c = fgetc(fd);
5198 blue = (blue << 4) + hex2nr(c);
5199 c = fgetc(fd);
5200 if (!isdigit(c))
5201 dump_is_corrupt(&ga_text);
5202 while (isdigit(c))
5203 {
5204 index = index * 10 + (c - '0');
5205 c = fgetc(fd);
5206 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005207 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005208 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005209 type = VTERM_COLOR_RGB;
5210 if (index == 0)
5211 {
5212 if (is_bg)
5213 type |= VTERM_COLOR_DEFAULT_BG;
5214 else
5215 type |= VTERM_COLOR_DEFAULT_FG;
5216 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005217 }
5218 else
5219 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005220 type = VTERM_COLOR_INDEXED;
5221 index -= 1;
5222 }
5223 if (is_bg)
5224 {
5225 cell.bg.type = type;
5226 cell.bg.red = red;
5227 cell.bg.green = green;
5228 cell.bg.blue = blue;
5229 cell.bg.index = index;
5230 }
5231 else
5232 {
5233 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005234 cell.fg.red = red;
5235 cell.fg.green = green;
5236 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005237 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005238 }
5239 }
5240 else
5241 dump_is_corrupt(&ga_text);
5242 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005243 }
5244 else
5245 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005246 }
5247 else
5248 dump_is_corrupt(&ga_text);
5249
5250 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005251 if (cell.width == 2)
5252 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005253 }
5254 else if (c == '@')
5255 {
5256 if (prev_char == NULL)
5257 dump_is_corrupt(&ga_text);
5258 else
5259 {
5260 int count = 0;
5261
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005262 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005263 for (;;)
5264 {
5265 c = fgetc(fd);
5266 if (!isdigit(c))
5267 break;
5268 count = count * 10 + (c - '0');
5269 }
5270
5271 while (count-- > 0)
5272 {
5273 ga_concat(&ga_text, prev_char);
5274 append_cell(&ga_cell, &cell);
5275 }
5276 }
5277 }
5278 else
5279 {
5280 dump_is_corrupt(&ga_text);
5281 c = fgetc(fd);
5282 }
5283 }
5284
5285 if (ga_text.ga_len > 0)
5286 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005287 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005288 dump_is_corrupt(&ga_text);
5289 ga_append(&ga_text, NUL);
5290 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5291 ga_text.ga_len, FALSE);
5292 }
5293
5294 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005295 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005296 vim_free(prev_char);
5297
5298 return max_cells;
5299}
5300
5301/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005302 * Return an allocated string with at least "text_width" "=" characters and
5303 * "fname" inserted in the middle.
5304 */
5305 static char_u *
5306get_separator(int text_width, char_u *fname)
5307{
5308 int width = MAX(text_width, curwin->w_width);
5309 char_u *textline;
5310 int fname_size;
5311 char_u *p = fname;
5312 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005313 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005314
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005315 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005316 if (textline == NULL)
5317 return NULL;
5318
5319 fname_size = vim_strsize(fname);
5320 if (fname_size < width - 8)
5321 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005322 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005323 width = MAX(text_width, fname_size + 8);
5324 }
5325 else if (fname_size > width - 8)
5326 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005327 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005328 p = gettail(fname);
5329 fname_size = vim_strsize(p);
5330 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005331 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005332 while (fname_size > width - 8)
5333 {
5334 p += (*mb_ptr2len)(p);
5335 fname_size = vim_strsize(p);
5336 }
5337
5338 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5339 textline[i] = '=';
5340 textline[i++] = ' ';
5341
5342 STRCPY(textline + i, p);
5343 off = STRLEN(textline);
5344 textline[off] = ' ';
5345 for (i = 1; i < (width - fname_size) / 2; ++i)
5346 textline[off + i] = '=';
5347 textline[off + i] = NUL;
5348
5349 return textline;
5350}
5351
5352/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005353 * Common for "term_dumpdiff()" and "term_dumpload()".
5354 */
5355 static void
5356term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5357{
5358 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005359 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005360 char_u buf1[NUMBUFLEN];
5361 char_u buf2[NUMBUFLEN];
5362 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005363 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005364 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005365 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005366 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005367 char_u *textline = NULL;
5368
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005369 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005370 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005371 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005372 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005373 if (fname1 == NULL || (do_diff && fname2 == NULL))
5374 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005375 emsg(_(e_invalid_argument));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005376 return;
5377 }
5378 fd1 = mch_fopen((char *)fname1, READBIN);
5379 if (fd1 == NULL)
5380 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005381 semsg(_(e_cant_read_file_str), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005382 return;
5383 }
5384 if (do_diff)
5385 {
5386 fd2 = mch_fopen((char *)fname2, READBIN);
5387 if (fd2 == NULL)
5388 {
5389 fclose(fd1);
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005390 semsg(_(e_cant_read_file_str), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005391 return;
5392 }
5393 }
5394
5395 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005396 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5397 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5398 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5399 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5400 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005401
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005402 if (opt.jo_term_name == NULL)
5403 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005404 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005405
Bram Moolenaar51e14382019-05-25 20:21:28 +02005406 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005407 if (fname_tofree != NULL)
5408 {
5409 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5410 opt.jo_term_name = fname_tofree;
5411 }
5412 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005413
Bram Moolenaar87abab92019-06-03 21:14:59 +02005414 if (opt.jo_bufnr_buf != NULL)
5415 {
5416 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5417
5418 // With "bufnr" argument: enter the window with this buffer and make it
5419 // empty.
5420 if (wp == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005421 semsg(_(e_invalid_argument_str), "bufnr");
Bram Moolenaar87abab92019-06-03 21:14:59 +02005422 else
5423 {
5424 buf = curbuf;
5425 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005426 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005427 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005428 redraw_later(NOT_VALID);
5429 }
5430 }
5431 else
5432 // Create a new terminal window.
5433 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5434
Bram Moolenaard96ff162018-02-18 22:13:29 +01005435 if (buf != NULL && buf->b_term != NULL)
5436 {
5437 int i;
5438 linenr_T bot_lnum;
5439 linenr_T lnum;
5440 term_T *term = buf->b_term;
5441 int width;
5442 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005443 VTermPos cursor_pos1;
5444 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005445
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005446 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005447
Bram Moolenaard96ff162018-02-18 22:13:29 +01005448 rettv->vval.v_number = buf->b_fnum;
5449
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005450 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005451 width = read_dump_file(fd1, &cursor_pos1);
5452
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005453 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005454 if (cursor_pos1.row >= 0)
5455 {
5456 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5457 coladvance(cursor_pos1.col);
5458 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005459
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005460 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005461 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005462
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005463 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005464 if (!do_diff)
5465 goto theend;
5466
5467 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5468
Bram Moolenaar4a696342018-04-05 18:45:26 +02005469 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005470 if (textline == NULL)
5471 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005472 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5473 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5474 vim_free(textline);
5475
5476 textline = get_separator(width, fname2);
5477 if (textline == NULL)
5478 goto theend;
5479 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5480 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005481 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005482
5483 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005484 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005485 if (width2 > width)
5486 {
5487 vim_free(textline);
5488 textline = alloc(width2 + 1);
5489 if (textline == NULL)
5490 goto theend;
5491 width = width2;
5492 textline[width] = NUL;
5493 }
5494 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5495
5496 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5497 {
5498 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5499 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005500 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005501 for (i = 0; i < width; ++i)
5502 textline[i] = '-';
5503 }
5504 else
5505 {
5506 char_u *line1;
5507 char_u *line2;
5508 char_u *p1;
5509 char_u *p2;
5510 int col;
5511 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5512 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5513 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5514 ->sb_cells;
5515
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005516 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005517 line1 = vim_strsave(ml_get(lnum));
5518 if (line1 == NULL)
5519 break;
5520 p1 = line1;
5521
5522 line2 = ml_get(lnum + bot_lnum);
5523 p2 = line2;
5524 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5525 {
5526 int len1 = utfc_ptr2len(p1);
5527 int len2 = utfc_ptr2len(p2);
5528
5529 textline[col] = ' ';
5530 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005531 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005532 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005533 else if (lnum == cursor_pos1.row + 1
5534 && col == cursor_pos1.col
5535 && (cursor_pos1.row != cursor_pos2.row
5536 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005537 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005538 textline[col] = '>';
5539 else if (lnum == cursor_pos2.row + 1
5540 && col == cursor_pos2.col
5541 && (cursor_pos1.row != cursor_pos2.row
5542 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005543 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005544 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005545 else if (cellattr1 != NULL && cellattr2 != NULL)
5546 {
5547 if ((cellattr1 + col)->width
5548 != (cellattr2 + col)->width)
5549 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005550 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005551 &(cellattr2 + col)->fg))
5552 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005553 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005554 &(cellattr2 + col)->bg))
5555 textline[col] = 'b';
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005556 else if (vtermAttr2hl(&(cellattr1 + col)->attrs)
5557 != vtermAttr2hl(&((cellattr2 + col)->attrs)))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005558 textline[col] = 'a';
5559 }
5560 p1 += len1;
5561 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005562 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005563 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005564
5565 while (col < width)
5566 {
5567 if (*p1 == NUL && *p2 == NUL)
5568 textline[col] = '?';
5569 else if (*p1 == NUL)
5570 {
5571 textline[col] = '+';
5572 p2 += utfc_ptr2len(p2);
5573 }
5574 else
5575 {
5576 textline[col] = '-';
5577 p1 += utfc_ptr2len(p1);
5578 }
5579 ++col;
5580 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005581
5582 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005583 }
5584 if (add_empty_scrollback(term, &term->tl_default_color,
5585 term->tl_top_diff_rows) == OK)
5586 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5587 ++bot_lnum;
5588 }
5589
5590 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5591 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005592 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005593 for (i = 0; i < width; ++i)
5594 textline[i] = '+';
5595 if (add_empty_scrollback(term, &term->tl_default_color,
5596 term->tl_top_diff_rows) == OK)
5597 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5598 ++lnum;
5599 ++bot_lnum;
5600 }
5601
5602 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005603
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005604 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005605 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005606 }
5607
5608theend:
5609 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005610 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005611 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005612 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005613 fclose(fd2);
5614}
5615
5616/*
5617 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5618 * bottom files.
5619 * Return FAIL when this is not possible.
5620 */
5621 int
5622term_swap_diff()
5623{
5624 term_T *term = curbuf->b_term;
5625 linenr_T line_count;
5626 linenr_T top_rows;
5627 linenr_T bot_rows;
5628 linenr_T bot_start;
5629 linenr_T lnum;
5630 char_u *p;
5631 sb_line_T *sb_line;
5632
5633 if (term == NULL
5634 || !term_is_finished(curbuf)
5635 || term->tl_top_diff_rows == 0
5636 || term->tl_scrollback.ga_len == 0)
5637 return FAIL;
5638
5639 line_count = curbuf->b_ml.ml_line_count;
5640 top_rows = term->tl_top_diff_rows;
5641 bot_rows = term->tl_bot_diff_rows;
5642 bot_start = line_count - bot_rows;
5643 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5644
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005645 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005646 for (lnum = 1; lnum <= top_rows; ++lnum)
5647 {
5648 p = vim_strsave(ml_get(1));
5649 if (p == NULL)
5650 return OK;
5651 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005652 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005653 vim_free(p);
5654 }
5655
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005656 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005657 for (lnum = 1; lnum <= bot_rows; ++lnum)
5658 {
5659 p = vim_strsave(ml_get(bot_start + lnum));
5660 if (p == NULL)
5661 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005662 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005663 ml_append(lnum - 1, p, 0, FALSE);
5664 vim_free(p);
5665 }
5666
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005667 // move top title to bottom
5668 p = vim_strsave(ml_get(bot_rows + 1));
5669 if (p == NULL)
5670 return OK;
5671 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005672 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005673 vim_free(p);
5674
5675 // move bottom title to top
5676 p = vim_strsave(ml_get(line_count - top_rows));
5677 if (p == NULL)
5678 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005679 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005680 ml_append(bot_rows, p, 0, FALSE);
5681 vim_free(p);
5682
Bram Moolenaard96ff162018-02-18 22:13:29 +01005683 if (top_rows == bot_rows)
5684 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005685 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005686 for (lnum = 0; lnum < top_rows; ++lnum)
5687 {
5688 sb_line_T temp;
5689
5690 temp = *(sb_line + lnum);
5691 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5692 *(sb_line + bot_start + lnum) = temp;
5693 }
5694 }
5695 else
5696 {
5697 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005698 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005699
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005700 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005701 if (temp != NULL)
5702 {
5703 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5704 mch_memmove(term->tl_scrollback.ga_data,
5705 temp + bot_start,
5706 sizeof(sb_line_T) * bot_rows);
5707 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5708 temp + top_rows,
5709 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5710 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5711 + line_count - top_rows,
5712 temp,
5713 sizeof(sb_line_T) * top_rows);
5714 vim_free(temp);
5715 }
5716 }
5717
5718 term->tl_top_diff_rows = bot_rows;
5719 term->tl_bot_diff_rows = top_rows;
5720
5721 update_screen(NOT_VALID);
5722 return OK;
5723}
5724
5725/*
5726 * "term_dumpdiff(filename, filename, options)" function
5727 */
5728 void
5729f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5730{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005731 if (in_vim9script()
5732 && (check_for_string_arg(argvars, 0) == FAIL
5733 || check_for_string_arg(argvars, 1) == FAIL
5734 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5735 return;
5736
Bram Moolenaard96ff162018-02-18 22:13:29 +01005737 term_load_dump(argvars, rettv, TRUE);
5738}
5739
5740/*
5741 * "term_dumpload(filename, options)" function
5742 */
5743 void
5744f_term_dumpload(typval_T *argvars, typval_T *rettv)
5745{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005746 if (in_vim9script()
5747 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005748 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005749 return;
5750
Bram Moolenaard96ff162018-02-18 22:13:29 +01005751 term_load_dump(argvars, rettv, FALSE);
5752}
5753
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005754/*
5755 * "term_getaltscreen(buf)" function
5756 */
5757 void
5758f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5759{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005760 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005761
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005762 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5763 return;
5764
5765 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005766 if (buf == NULL)
5767 return;
5768 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5769}
5770
5771/*
5772 * "term_getattr(attr, name)" function
5773 */
5774 void
5775f_term_getattr(typval_T *argvars, typval_T *rettv)
5776{
5777 int attr;
5778 size_t i;
5779 char_u *name;
5780
5781 static struct {
5782 char *name;
5783 int attr;
5784 } attrs[] = {
5785 {"bold", HL_BOLD},
5786 {"italic", HL_ITALIC},
5787 {"underline", HL_UNDERLINE},
5788 {"strike", HL_STRIKETHROUGH},
5789 {"reverse", HL_INVERSE},
5790 };
5791
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005792 if (in_vim9script()
5793 && (check_for_number_arg(argvars, 0) == FAIL
5794 || check_for_string_arg(argvars, 1) == FAIL))
5795 return;
5796
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005797 attr = tv_get_number(&argvars[0]);
5798 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005799 if (name == NULL)
5800 return;
5801
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005802 if (attr > HL_ALL)
5803 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02005804 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005805 if (STRCMP(name, attrs[i].name) == 0)
5806 {
5807 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5808 break;
5809 }
5810}
5811
5812/*
5813 * "term_getcursor(buf)" function
5814 */
5815 void
5816f_term_getcursor(typval_T *argvars, typval_T *rettv)
5817{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005818 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005819 term_T *term;
5820 list_T *l;
5821 dict_T *d;
5822
5823 if (rettv_list_alloc(rettv) == FAIL)
5824 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005825
5826 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5827 return;
5828
5829 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005830 if (buf == NULL)
5831 return;
5832 term = buf->b_term;
5833
5834 l = rettv->vval.v_list;
5835 list_append_number(l, term->tl_cursor_pos.row + 1);
5836 list_append_number(l, term->tl_cursor_pos.col + 1);
5837
5838 d = dict_alloc();
5839 if (d != NULL)
5840 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005841 dict_add_number(d, "visible", term->tl_cursor_visible);
5842 dict_add_number(d, "blink", blink_state_is_inverted()
5843 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5844 dict_add_number(d, "shape", term->tl_cursor_shape);
5845 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005846 list_append_dict(l, d);
5847 }
5848}
5849
5850/*
5851 * "term_getjob(buf)" function
5852 */
5853 void
5854f_term_getjob(typval_T *argvars, typval_T *rettv)
5855{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005856 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005857
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005858 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5859 return;
5860
5861 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005862 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005863 {
5864 rettv->v_type = VAR_SPECIAL;
5865 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005866 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005867 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005868
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005869 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005870 rettv->vval.v_job = buf->b_term->tl_job;
5871 if (rettv->vval.v_job != NULL)
5872 ++rettv->vval.v_job->jv_refcount;
5873}
5874
5875 static int
5876get_row_number(typval_T *tv, term_T *term)
5877{
5878 if (tv->v_type == VAR_STRING
5879 && tv->vval.v_string != NULL
5880 && STRCMP(tv->vval.v_string, ".") == 0)
5881 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005882 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005883}
5884
5885/*
5886 * "term_getline(buf, row)" function
5887 */
5888 void
5889f_term_getline(typval_T *argvars, typval_T *rettv)
5890{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005891 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005892 term_T *term;
5893 int row;
5894
5895 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005896
5897 if (in_vim9script()
5898 && (check_for_buffer_arg(argvars, 0) == FAIL
5899 || check_for_lnum_arg(argvars, 1) == FAIL))
5900 return;
5901
5902 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005903 if (buf == NULL)
5904 return;
5905 term = buf->b_term;
5906 row = get_row_number(&argvars[1], term);
5907
5908 if (term->tl_vterm == NULL)
5909 {
5910 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5911
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005912 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005913 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5914 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5915 }
5916 else
5917 {
5918 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5919 VTermRect rect;
5920 int len;
5921 char_u *p;
5922
5923 if (row < 0 || row >= term->tl_rows)
5924 return;
5925 len = term->tl_cols * MB_MAXBYTES + 1;
5926 p = alloc(len);
5927 if (p == NULL)
5928 return;
5929 rettv->vval.v_string = p;
5930
5931 rect.start_col = 0;
5932 rect.end_col = term->tl_cols;
5933 rect.start_row = row;
5934 rect.end_row = row + 1;
5935 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5936 }
5937}
5938
5939/*
5940 * "term_getscrolled(buf)" function
5941 */
5942 void
5943f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5944{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005945 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005946
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005947 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5948 return;
5949
5950 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005951 if (buf == NULL)
5952 return;
5953 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5954}
5955
5956/*
5957 * "term_getsize(buf)" function
5958 */
5959 void
5960f_term_getsize(typval_T *argvars, typval_T *rettv)
5961{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005962 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005963 list_T *l;
5964
5965 if (rettv_list_alloc(rettv) == FAIL)
5966 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005967
5968 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5969 return;
5970
5971 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005972 if (buf == NULL)
5973 return;
5974
5975 l = rettv->vval.v_list;
5976 list_append_number(l, buf->b_term->tl_rows);
5977 list_append_number(l, buf->b_term->tl_cols);
5978}
5979
5980/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005981 * "term_setsize(buf, rows, cols)" function
5982 */
5983 void
5984f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5985{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005986 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02005987 term_T *term;
5988 varnumber_T rows, cols;
5989
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005990 if (in_vim9script()
5991 && (check_for_buffer_arg(argvars, 0) == FAIL
5992 || check_for_number_arg(argvars, 1) == FAIL
5993 || check_for_number_arg(argvars, 2) == FAIL))
5994 return;
5995
5996 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005997 if (buf == NULL)
5998 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005999 emsg(_(e_not_terminal_buffer));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02006000 return;
6001 }
6002 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02006003 return;
6004 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006005 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02006006 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006007 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02006008 cols = cols <= 0 ? term->tl_cols : cols;
6009 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006010 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02006011
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006012 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02006013 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
6014 term_report_winsize(term, term->tl_rows, term->tl_cols);
6015}
6016
6017/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006018 * "term_getstatus(buf)" function
6019 */
6020 void
6021f_term_getstatus(typval_T *argvars, typval_T *rettv)
6022{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006023 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006024 term_T *term;
6025 char_u val[100];
6026
6027 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006028
6029 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6030 return;
6031
6032 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006033 if (buf == NULL)
6034 return;
6035 term = buf->b_term;
6036
6037 if (term_job_running(term))
6038 STRCPY(val, "running");
6039 else
6040 STRCPY(val, "finished");
6041 if (term->tl_normal_mode)
6042 STRCAT(val, ",normal");
6043 rettv->vval.v_string = vim_strsave(val);
6044}
6045
6046/*
6047 * "term_gettitle(buf)" function
6048 */
6049 void
6050f_term_gettitle(typval_T *argvars, typval_T *rettv)
6051{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006052 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006053
6054 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006055
6056 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6057 return;
6058
6059 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006060 if (buf == NULL)
6061 return;
6062
6063 if (buf->b_term->tl_title != NULL)
6064 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
6065}
6066
6067/*
6068 * "term_gettty(buf)" function
6069 */
6070 void
6071f_term_gettty(typval_T *argvars, typval_T *rettv)
6072{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006073 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02006074 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006075 int num = 0;
6076
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006077 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006078 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006079 || check_for_opt_bool_arg(argvars, 1) == FAIL))
6080 return;
6081
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006082 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006083 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006084 if (buf == NULL)
6085 return;
6086 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02006087 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006088
6089 switch (num)
6090 {
6091 case 0:
6092 if (buf->b_term->tl_job != NULL)
6093 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006094 break;
6095 case 1:
6096 if (buf->b_term->tl_job != NULL)
6097 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006098 break;
6099 default:
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006100 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006101 return;
6102 }
6103 if (p != NULL)
6104 rettv->vval.v_string = vim_strsave(p);
6105}
6106
6107/*
6108 * "term_list()" function
6109 */
6110 void
6111f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
6112{
6113 term_T *tp;
6114 list_T *l;
6115
6116 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
6117 return;
6118
6119 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006120 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02006121 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006122 if (list_append_number(l,
6123 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6124 return;
6125}
6126
6127/*
6128 * "term_scrape(buf, row)" function
6129 */
6130 void
6131f_term_scrape(typval_T *argvars, typval_T *rettv)
6132{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006133 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006134 VTermScreen *screen = NULL;
6135 VTermPos pos;
6136 list_T *l;
6137 term_T *term;
6138 char_u *p;
6139 sb_line_T *line;
6140
6141 if (rettv_list_alloc(rettv) == FAIL)
6142 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006143
6144 if (in_vim9script()
6145 && (check_for_buffer_arg(argvars, 0) == FAIL
6146 || check_for_lnum_arg(argvars, 1) == FAIL))
6147 return;
6148
6149 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006150 if (buf == NULL)
6151 return;
6152 term = buf->b_term;
6153
6154 l = rettv->vval.v_list;
6155 pos.row = get_row_number(&argvars[1], term);
6156
6157 if (term->tl_vterm != NULL)
6158 {
6159 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006160 if (screen == NULL) // can't really happen
6161 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006162 p = NULL;
6163 line = NULL;
6164 }
6165 else
6166 {
6167 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6168
6169 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6170 return;
6171 p = ml_get_buf(buf, lnum + 1, FALSE);
6172 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6173 }
6174
6175 for (pos.col = 0; pos.col < term->tl_cols; )
6176 {
6177 dict_T *dcell;
6178 int width;
6179 VTermScreenCellAttrs attrs;
6180 VTermColor fg, bg;
6181 char_u rgb[8];
6182 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6183 int off = 0;
6184 int i;
6185
6186 if (screen == NULL)
6187 {
6188 cellattr_T *cellattr;
6189 int len;
6190
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006191 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006192 if (pos.col >= line->sb_cols)
6193 break;
6194 cellattr = line->sb_cells + pos.col;
6195 width = cellattr->width;
6196 attrs = cellattr->attrs;
6197 fg = cellattr->fg;
6198 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006199 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006200 mch_memmove(mbs, p, len);
6201 mbs[len] = NUL;
6202 p += len;
6203 }
6204 else
6205 {
6206 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006207
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006208 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6209 break;
6210 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6211 {
6212 if (cell.chars[i] == 0)
6213 break;
6214 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6215 }
6216 mbs[off] = NUL;
6217 width = cell.width;
6218 attrs = cell.attrs;
6219 fg = cell.fg;
6220 bg = cell.bg;
6221 }
6222 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006223 if (dcell == NULL)
6224 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006225 list_append_dict(l, dcell);
6226
Bram Moolenaare0be1672018-07-08 16:50:37 +02006227 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006228
6229 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6230 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006231 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006232 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6233 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006234 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006235
Bram Moolenaar87fd0922021-11-20 13:47:45 +00006236 dict_add_number(dcell, "attr",
6237 cell2attr(term, NULL, &attrs, &fg, &bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006238 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006239
6240 ++pos.col;
6241 if (width == 2)
6242 ++pos.col;
6243 }
6244}
6245
6246/*
6247 * "term_sendkeys(buf, keys)" function
6248 */
6249 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006250f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006251{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006252 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006253 char_u *msg;
6254 term_T *term;
6255
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006256 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006257 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006258 || check_for_string_arg(argvars, 1) == FAIL))
6259 return;
6260
6261 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006262 if (buf == NULL)
6263 return;
6264
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006265 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006266 if (msg == NULL)
6267 return;
6268 term = buf->b_term;
6269 if (term->tl_vterm == NULL)
6270 return;
6271
6272 while (*msg != NUL)
6273 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006274 int c;
6275
6276 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6277 {
6278 c = TO_SPECIAL(msg[1], msg[2]);
6279 msg += 3;
6280 }
6281 else
6282 {
6283 c = PTR2CHAR(msg);
6284 msg += MB_CPTR2LEN(msg);
6285 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006286 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006287 }
6288}
6289
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006290#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6291/*
6292 * "term_getansicolors(buf)" function
6293 */
6294 void
6295f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6296{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006297 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006298 term_T *term;
6299 VTermState *state;
6300 VTermColor color;
6301 char_u hexbuf[10];
6302 int index;
6303 list_T *list;
6304
6305 if (rettv_list_alloc(rettv) == FAIL)
6306 return;
6307
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006308 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6309 return;
6310
6311 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006312 if (buf == NULL)
6313 return;
6314 term = buf->b_term;
6315 if (term->tl_vterm == NULL)
6316 return;
6317
6318 list = rettv->vval.v_list;
6319 state = vterm_obtain_state(term->tl_vterm);
6320 for (index = 0; index < 16; index++)
6321 {
6322 vterm_state_get_palette_color(state, index, &color);
6323 sprintf((char *)hexbuf, "#%02x%02x%02x",
6324 color.red, color.green, color.blue);
6325 if (list_append_string(list, hexbuf, 7) == FAIL)
6326 return;
6327 }
6328}
6329
6330/*
6331 * "term_setansicolors(buf, list)" function
6332 */
6333 void
6334f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6335{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006336 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006337 term_T *term;
6338
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006339 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006340 && (check_for_buffer_arg(argvars, 0) == FAIL
6341 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006342 return;
6343
6344 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006345 if (buf == NULL)
6346 return;
6347 term = buf->b_term;
6348 if (term->tl_vterm == NULL)
6349 return;
6350
6351 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6352 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006353 emsg(_(e_list_required));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006354 return;
6355 }
6356
6357 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006358 emsg(_(e_invalid_argument));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006359}
6360#endif
6361
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006362/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006363 * "term_setapi(buf, api)" function
6364 */
6365 void
6366f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6367{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006368 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006369 term_T *term;
6370 char_u *api;
6371
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006372 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006373 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006374 || check_for_string_arg(argvars, 1) == FAIL))
6375 return;
6376
6377 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006378 if (buf == NULL)
6379 return;
6380 term = buf->b_term;
6381 vim_free(term->tl_api);
6382 api = tv_get_string_chk(&argvars[1]);
6383 if (api != NULL)
6384 term->tl_api = vim_strsave(api);
6385 else
6386 term->tl_api = NULL;
6387}
6388
6389/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006390 * "term_setrestore(buf, command)" function
6391 */
6392 void
6393f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6394{
6395#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006396 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006397 term_T *term;
6398 char_u *cmd;
6399
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006400 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006401 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006402 || check_for_string_arg(argvars, 1) == FAIL))
6403 return;
6404
6405 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006406 if (buf == NULL)
6407 return;
6408 term = buf->b_term;
6409 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006410 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006411 if (cmd != NULL)
6412 term->tl_command = vim_strsave(cmd);
6413 else
6414 term->tl_command = NULL;
6415#endif
6416}
6417
6418/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006419 * "term_setkill(buf, how)" function
6420 */
6421 void
6422f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6423{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006424 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006425 term_T *term;
6426 char_u *how;
6427
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006428 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006429 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006430 || check_for_string_arg(argvars, 1) == FAIL))
6431 return;
6432
6433 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006434 if (buf == NULL)
6435 return;
6436 term = buf->b_term;
6437 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006438 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006439 if (how != NULL)
6440 term->tl_kill = vim_strsave(how);
6441 else
6442 term->tl_kill = NULL;
6443}
6444
6445/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006446 * "term_start(command, options)" function
6447 */
6448 void
6449f_term_start(typval_T *argvars, typval_T *rettv)
6450{
6451 jobopt_T opt;
6452 buf_T *buf;
6453
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006454 if (in_vim9script()
6455 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6456 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6457 return;
6458
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006459 init_job_options(&opt);
6460 if (argvars[1].v_type != VAR_UNKNOWN
6461 && get_job_options(&argvars[1], &opt,
6462 JO_TIMEOUT_ALL + JO_STOPONEXIT
6463 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6464 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6465 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6466 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006467 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006468 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006469 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006470 return;
6471
Bram Moolenaar13568252018-03-16 20:46:58 +01006472 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006473
6474 if (buf != NULL && buf->b_term != NULL)
6475 rettv->vval.v_number = buf->b_fnum;
6476}
6477
6478/*
6479 * "term_wait" function
6480 */
6481 void
6482f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6483{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006484 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006485
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006486 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006487 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006488 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006489 return;
6490
6491 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006492 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006493 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006494 if (buf->b_term->tl_job == NULL)
6495 {
6496 ch_log(NULL, "term_wait(): no job to wait for");
6497 return;
6498 }
6499 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006500 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006501 return;
6502
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006503 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006504 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006505 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6506 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006507 // The job is dead, keep reading channel I/O until the channel is
6508 // closed. buf->b_term may become NULL if the terminal was closed while
6509 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006510 ch_log(NULL, "term_wait(): waiting for channel to close");
6511 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6512 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006513 term_flush_messages();
6514
Bram Moolenaard45aa552018-05-21 22:50:29 +02006515 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006516 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006517 // If the terminal is closed when the channel is closed the
6518 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006519 break;
Bram Moolenaareea32af2021-11-21 14:51:13 +00006520 if (buf->b_term == NULL || buf->b_term->tl_channel_closing)
6521 // came here from a close callback, only wait one time
6522 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006523 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006524
6525 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006526 }
6527 else
6528 {
6529 long wait = 10L;
6530
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006531 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006532
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006533 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006534 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006535 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006536 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006537
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006538 // Flushing messages on channels is hopefully sufficient.
6539 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006540 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006541 }
6542}
6543
6544/*
6545 * Called when a channel has sent all the lines to a terminal.
6546 * Send a CTRL-D to mark the end of the text.
6547 */
6548 void
6549term_send_eof(channel_T *ch)
6550{
6551 term_T *term;
6552
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006553 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006554 if (term->tl_job == ch->ch_job)
6555 {
6556 if (term->tl_eof_chars != NULL)
6557 {
6558 channel_send(ch, PART_IN, term->tl_eof_chars,
6559 (int)STRLEN(term->tl_eof_chars), NULL);
6560 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6561 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006562# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006563 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006564 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006565 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6566# endif
6567 }
6568}
6569
Bram Moolenaar113e1072019-01-20 15:30:40 +01006570#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006571 job_T *
6572term_getjob(term_T *term)
6573{
6574 return term != NULL ? term->tl_job : NULL;
6575}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006576#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006577
Bram Moolenaar4f974752019-02-17 17:44:42 +01006578# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006579
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006580///////////////////////////////////////
6581// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006582#ifdef PROTO
6583typedef int COORD;
6584typedef int DWORD;
6585typedef int HANDLE;
6586typedef int *DWORD_PTR;
6587typedef int HPCON;
6588typedef int HRESULT;
6589typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006590typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006591typedef int PSIZE_T;
6592typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006593typedef int BOOL;
6594# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006595#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006596
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006597HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6598HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6599HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006600BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6601BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6602void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006603
6604 static int
6605dyn_conpty_init(int verbose)
6606{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006607 static HMODULE hKerneldll = NULL;
6608 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006609 static struct
6610 {
6611 char *name;
6612 FARPROC *ptr;
6613 } conpty_entry[] =
6614 {
6615 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6616 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6617 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6618 {"InitializeProcThreadAttributeList",
6619 (FARPROC*)&pInitializeProcThreadAttributeList},
6620 {"UpdateProcThreadAttribute",
6621 (FARPROC*)&pUpdateProcThreadAttribute},
6622 {"DeleteProcThreadAttributeList",
6623 (FARPROC*)&pDeleteProcThreadAttributeList},
6624 {NULL, NULL}
6625 };
6626
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006627 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006628 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006629 if (verbose)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006630 emsg(_(e_conpty_is_not_available));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006631 return FAIL;
6632 }
6633
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006634 // No need to initialize twice.
6635 if (hKerneldll)
6636 return OK;
6637
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006638 hKerneldll = vimLoadLib("kernel32.dll");
6639 for (i = 0; conpty_entry[i].name != NULL
6640 && conpty_entry[i].ptr != NULL; ++i)
6641 {
6642 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6643 conpty_entry[i].name)) == NULL)
6644 {
6645 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006646 semsg(_(e_could_not_load_library_function_str), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006647 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006648 return FAIL;
6649 }
6650 }
6651
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006652 return OK;
6653}
6654
6655 static int
6656conpty_term_and_job_init(
6657 term_T *term,
6658 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006659 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006660 jobopt_T *opt,
6661 jobopt_T *orig_opt)
6662{
6663 WCHAR *cmd_wchar = NULL;
6664 WCHAR *cmd_wchar_copy = NULL;
6665 WCHAR *cwd_wchar = NULL;
6666 WCHAR *env_wchar = NULL;
6667 channel_T *channel = NULL;
6668 job_T *job = NULL;
6669 HANDLE jo = NULL;
6670 garray_T ga_cmd, ga_env;
6671 char_u *cmd = NULL;
6672 HRESULT hr;
6673 COORD consize;
6674 SIZE_T breq;
6675 PROCESS_INFORMATION proc_info;
6676 HANDLE i_theirs = NULL;
6677 HANDLE o_theirs = NULL;
6678 HANDLE i_ours = NULL;
6679 HANDLE o_ours = NULL;
6680
Bram Moolenaar04935fb2022-01-08 16:19:22 +00006681 ga_init2(&ga_cmd, sizeof(char*), 20);
6682 ga_init2(&ga_env, sizeof(char*), 20);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006683
6684 if (argvar->v_type == VAR_STRING)
6685 {
6686 cmd = argvar->vval.v_string;
6687 }
6688 else if (argvar->v_type == VAR_LIST)
6689 {
6690 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6691 goto failed;
6692 cmd = ga_cmd.ga_data;
6693 }
6694 if (cmd == NULL || *cmd == NUL)
6695 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006696 emsg(_(e_invalid_argument));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006697 goto failed;
6698 }
6699
6700 term->tl_arg0_cmd = vim_strsave(cmd);
6701
6702 cmd_wchar = enc_to_utf16(cmd, NULL);
6703
6704 if (cmd_wchar != NULL)
6705 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006706 // Request by CreateProcessW
6707 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006708 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006709 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6710 }
6711
6712 ga_clear(&ga_cmd);
6713 if (cmd_wchar == NULL)
6714 goto failed;
6715 if (opt->jo_cwd != NULL)
6716 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6717
6718 win32_build_env(opt->jo_env, &ga_env, TRUE);
6719 env_wchar = ga_env.ga_data;
6720
6721 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6722 goto failed;
6723 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6724 goto failed;
6725
6726 consize.X = term->tl_cols;
6727 consize.Y = term->tl_rows;
6728 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6729 &term->tl_conpty);
6730 if (FAILED(hr))
6731 goto failed;
6732
6733 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6734
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006735 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006736 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006737 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006738 if (!term->tl_siex.lpAttributeList)
6739 goto failed;
6740 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6741 0, &breq))
6742 goto failed;
6743 if (!pUpdateProcThreadAttribute(
6744 term->tl_siex.lpAttributeList, 0,
6745 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6746 sizeof(HPCON), NULL, NULL))
6747 goto failed;
6748
6749 channel = add_channel();
6750 if (channel == NULL)
6751 goto failed;
6752
6753 job = job_alloc();
6754 if (job == NULL)
6755 goto failed;
6756 if (argvar->v_type == VAR_STRING)
6757 {
6758 int argc;
6759
6760 build_argv_from_string(cmd, &job->jv_argv, &argc);
6761 }
6762 else
6763 {
6764 int argc;
6765
6766 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6767 }
6768
6769 if (opt->jo_set & JO_IN_BUF)
6770 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6771
6772 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6773 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006774 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006775 env_wchar, cwd_wchar,
6776 &term->tl_siex.StartupInfo, &proc_info))
6777 goto failed;
6778
6779 CloseHandle(i_theirs);
6780 CloseHandle(o_theirs);
6781
6782 channel_set_pipes(channel,
6783 (sock_T)i_ours,
6784 (sock_T)o_ours,
6785 (sock_T)o_ours);
6786
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006787 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006788 channel->ch_write_text_mode = TRUE;
6789
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006790 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006791 channel->ch_anonymous_pipe = TRUE;
6792
6793 jo = CreateJobObject(NULL, NULL);
6794 if (jo == NULL)
6795 goto failed;
6796
6797 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6798 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006799 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006800 CloseHandle(jo);
6801 jo = NULL;
6802 }
6803
6804 ResumeThread(proc_info.hThread);
6805 CloseHandle(proc_info.hThread);
6806
6807 vim_free(cmd_wchar);
6808 vim_free(cmd_wchar_copy);
6809 vim_free(cwd_wchar);
6810 vim_free(env_wchar);
6811
6812 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6813 goto failed;
6814
6815#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6816 if (opt->jo_set2 & JO2_ANSI_COLORS)
6817 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6818 else
6819 init_vterm_ansi_colors(term->tl_vterm);
6820#endif
6821
6822 channel_set_job(channel, job, opt);
6823 job_set_options(job, opt);
6824
6825 job->jv_channel = channel;
6826 job->jv_proc_info = proc_info;
6827 job->jv_job_object = jo;
6828 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006829 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006830 ++job->jv_refcount;
6831 term->tl_job = job;
6832
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006833 // Redirecting stdout and stderr doesn't work at the job level. Instead
6834 // open the file here and handle it in. opt->jo_io was changed in
6835 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006836 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6837 {
6838 char_u *fname = opt->jo_io_name[PART_OUT];
6839
6840 ch_log(channel, "Opening output file %s", fname);
6841 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6842 if (term->tl_out_fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006843 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006844 }
6845
6846 return OK;
6847
6848failed:
6849 ga_clear(&ga_cmd);
6850 ga_clear(&ga_env);
6851 vim_free(cmd_wchar);
6852 vim_free(cmd_wchar_copy);
6853 vim_free(cwd_wchar);
6854 if (channel != NULL)
6855 channel_clear(channel);
6856 if (job != NULL)
6857 {
6858 job->jv_channel = NULL;
6859 job_cleanup(job);
6860 }
6861 term->tl_job = NULL;
6862 if (jo != NULL)
6863 CloseHandle(jo);
6864
6865 if (term->tl_siex.lpAttributeList != NULL)
6866 {
6867 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6868 vim_free(term->tl_siex.lpAttributeList);
6869 }
6870 term->tl_siex.lpAttributeList = NULL;
6871 if (o_theirs != NULL)
6872 CloseHandle(o_theirs);
6873 if (o_ours != NULL)
6874 CloseHandle(o_ours);
6875 if (i_ours != NULL)
6876 CloseHandle(i_ours);
6877 if (i_theirs != NULL)
6878 CloseHandle(i_theirs);
6879 if (term->tl_conpty != NULL)
6880 pClosePseudoConsole(term->tl_conpty);
6881 term->tl_conpty = NULL;
6882 return FAIL;
6883}
6884
6885 static void
6886conpty_term_report_winsize(term_T *term, int rows, int cols)
6887{
6888 COORD consize;
6889
6890 consize.X = cols;
6891 consize.Y = rows;
6892 pResizePseudoConsole(term->tl_conpty, consize);
6893}
6894
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006895 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006896term_free_conpty(term_T *term)
6897{
6898 if (term->tl_siex.lpAttributeList != NULL)
6899 {
6900 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6901 vim_free(term->tl_siex.lpAttributeList);
6902 }
6903 term->tl_siex.lpAttributeList = NULL;
6904 if (term->tl_conpty != NULL)
6905 pClosePseudoConsole(term->tl_conpty);
6906 term->tl_conpty = NULL;
6907}
6908
6909 int
6910use_conpty(void)
6911{
6912 return has_conpty;
6913}
6914
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006915# ifndef PROTO
6916
6917#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6918#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006919#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006920
6921void* (*winpty_config_new)(UINT64, void*);
6922void* (*winpty_open)(void*, void*);
6923void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6924BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6925void (*winpty_config_set_mouse_mode)(void*, int);
6926void (*winpty_config_set_initial_size)(void*, int, int);
6927LPCWSTR (*winpty_conin_name)(void*);
6928LPCWSTR (*winpty_conout_name)(void*);
6929LPCWSTR (*winpty_conerr_name)(void*);
6930void (*winpty_free)(void*);
6931void (*winpty_config_free)(void*);
6932void (*winpty_spawn_config_free)(void*);
6933void (*winpty_error_free)(void*);
6934LPCWSTR (*winpty_error_msg)(void*);
6935BOOL (*winpty_set_size)(void*, int, int, void*);
6936HANDLE (*winpty_agent_process)(void*);
6937
6938#define WINPTY_DLL "winpty.dll"
6939
6940static HINSTANCE hWinPtyDLL = NULL;
6941# endif
6942
6943 static int
6944dyn_winpty_init(int verbose)
6945{
6946 int i;
6947 static struct
6948 {
6949 char *name;
6950 FARPROC *ptr;
6951 } winpty_entry[] =
6952 {
6953 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6954 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6955 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6956 {"winpty_config_set_mouse_mode",
6957 (FARPROC*)&winpty_config_set_mouse_mode},
6958 {"winpty_config_set_initial_size",
6959 (FARPROC*)&winpty_config_set_initial_size},
6960 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6961 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6962 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6963 {"winpty_free", (FARPROC*)&winpty_free},
6964 {"winpty_open", (FARPROC*)&winpty_open},
6965 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6966 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6967 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6968 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6969 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6970 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6971 {NULL, NULL}
6972 };
6973
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006974 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006975 if (hWinPtyDLL)
6976 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006977 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6978 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006979 if (*p_winptydll != NUL)
6980 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6981 if (!hWinPtyDLL)
6982 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6983 if (!hWinPtyDLL)
6984 {
6985 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006986 semsg(_(e_could_not_load_library_str_str),
Martin Tournoij1a3e5742021-07-24 13:57:29 +02006987 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
6988 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006989 return FAIL;
6990 }
6991 for (i = 0; winpty_entry[i].name != NULL
6992 && winpty_entry[i].ptr != NULL; ++i)
6993 {
6994 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6995 winpty_entry[i].name)) == NULL)
6996 {
6997 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006998 semsg(_(e_could_not_load_library_function_str), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006999 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007000 return FAIL;
7001 }
7002 }
7003
7004 return OK;
7005}
7006
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007007 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007008winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007009 term_T *term,
7010 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007011 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007012 jobopt_T *opt,
7013 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007014{
7015 WCHAR *cmd_wchar = NULL;
7016 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007017 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007018 channel_T *channel = NULL;
7019 job_T *job = NULL;
7020 DWORD error;
7021 HANDLE jo = NULL;
7022 HANDLE child_process_handle;
7023 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01007024 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007025 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007026 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007027 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007028
Bram Moolenaar04935fb2022-01-08 16:19:22 +00007029 ga_init2(&ga_cmd, sizeof(char*), 20);
7030 ga_init2(&ga_env, sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007031
7032 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007033 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007034 cmd = argvar->vval.v_string;
7035 }
7036 else if (argvar->v_type == VAR_LIST)
7037 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007038 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007039 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007040 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007041 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007042 if (cmd == NULL || *cmd == NUL)
7043 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007044 emsg(_(e_invalid_argument));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007045 goto failed;
7046 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007047
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007048 term->tl_arg0_cmd = vim_strsave(cmd);
7049
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007050 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007051 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007052 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007053 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007054 if (opt->jo_cwd != NULL)
7055 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007056
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007057 win32_build_env(opt->jo_env, &ga_env, TRUE);
7058 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007059
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007060 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
7061 if (term->tl_winpty_config == NULL)
7062 goto failed;
7063
7064 winpty_config_set_mouse_mode(term->tl_winpty_config,
7065 WINPTY_MOUSE_MODE_FORCE);
7066 winpty_config_set_initial_size(term->tl_winpty_config,
7067 term->tl_cols, term->tl_rows);
7068 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
7069 if (term->tl_winpty == NULL)
7070 goto failed;
7071
7072 spawn_config = winpty_spawn_config_new(
7073 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
7074 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
7075 NULL,
7076 cmd_wchar,
7077 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007078 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007079 &winpty_err);
7080 if (spawn_config == NULL)
7081 goto failed;
7082
7083 channel = add_channel();
7084 if (channel == NULL)
7085 goto failed;
7086
7087 job = job_alloc();
7088 if (job == NULL)
7089 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02007090 if (argvar->v_type == VAR_STRING)
7091 {
7092 int argc;
7093
7094 build_argv_from_string(cmd, &job->jv_argv, &argc);
7095 }
7096 else
7097 {
7098 int argc;
7099
7100 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
7101 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007102
7103 if (opt->jo_set & JO_IN_BUF)
7104 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
7105
7106 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
7107 &child_thread_handle, &error, &winpty_err))
7108 goto failed;
7109
7110 channel_set_pipes(channel,
7111 (sock_T)CreateFileW(
7112 winpty_conin_name(term->tl_winpty),
7113 GENERIC_WRITE, 0, NULL,
7114 OPEN_EXISTING, 0, NULL),
7115 (sock_T)CreateFileW(
7116 winpty_conout_name(term->tl_winpty),
7117 GENERIC_READ, 0, NULL,
7118 OPEN_EXISTING, 0, NULL),
7119 (sock_T)CreateFileW(
7120 winpty_conerr_name(term->tl_winpty),
7121 GENERIC_READ, 0, NULL,
7122 OPEN_EXISTING, 0, NULL));
7123
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007124 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007125 channel->ch_write_text_mode = TRUE;
7126
7127 jo = CreateJobObject(NULL, NULL);
7128 if (jo == NULL)
7129 goto failed;
7130
7131 if (!AssignProcessToJobObject(jo, child_process_handle))
7132 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007133 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007134 CloseHandle(jo);
7135 jo = NULL;
7136 }
7137
7138 winpty_spawn_config_free(spawn_config);
7139 vim_free(cmd_wchar);
7140 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007141 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007142
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007143 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7144 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007145
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007146#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7147 if (opt->jo_set2 & JO2_ANSI_COLORS)
7148 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7149 else
7150 init_vterm_ansi_colors(term->tl_vterm);
7151#endif
7152
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007153 channel_set_job(channel, job, opt);
7154 job_set_options(job, opt);
7155
7156 job->jv_channel = channel;
7157 job->jv_proc_info.hProcess = child_process_handle;
7158 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7159 job->jv_job_object = jo;
7160 job->jv_status = JOB_STARTED;
7161 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007162 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007163 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007164 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007165 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007166 ++job->jv_refcount;
7167 term->tl_job = job;
7168
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007169 // Redirecting stdout and stderr doesn't work at the job level. Instead
7170 // open the file here and handle it in. opt->jo_io was changed in
7171 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007172 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7173 {
7174 char_u *fname = opt->jo_io_name[PART_OUT];
7175
7176 ch_log(channel, "Opening output file %s", fname);
7177 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7178 if (term->tl_out_fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007179 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007180 }
7181
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007182 return OK;
7183
7184failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007185 ga_clear(&ga_cmd);
7186 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007187 vim_free(cmd_wchar);
7188 vim_free(cwd_wchar);
7189 if (spawn_config != NULL)
7190 winpty_spawn_config_free(spawn_config);
7191 if (channel != NULL)
7192 channel_clear(channel);
7193 if (job != NULL)
7194 {
7195 job->jv_channel = NULL;
7196 job_cleanup(job);
7197 }
7198 term->tl_job = NULL;
7199 if (jo != NULL)
7200 CloseHandle(jo);
7201 if (term->tl_winpty != NULL)
7202 winpty_free(term->tl_winpty);
7203 term->tl_winpty = NULL;
7204 if (term->tl_winpty_config != NULL)
7205 winpty_config_free(term->tl_winpty_config);
7206 term->tl_winpty_config = NULL;
7207 if (winpty_err != NULL)
7208 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007209 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007210 (short_u *)winpty_error_msg(winpty_err), NULL);
7211
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007212 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007213 winpty_error_free(winpty_err);
7214 }
7215 return FAIL;
7216}
7217
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007218/*
7219 * Create a new terminal of "rows" by "cols" cells.
7220 * Store a reference in "term".
7221 * Return OK or FAIL.
7222 */
7223 static int
7224term_and_job_init(
7225 term_T *term,
7226 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007227 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007228 jobopt_T *opt,
7229 jobopt_T *orig_opt)
7230{
7231 int use_winpty = FALSE;
7232 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007233 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007234
7235 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7236 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7237
7238 if (!has_winpty && !has_conpty)
7239 // If neither is available give the errors for winpty, since when
7240 // conpty is not available it can't be installed either.
7241 return dyn_winpty_init(TRUE);
7242
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007243 if (opt->jo_tty_type != NUL)
7244 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007245
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007246 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007247 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007248 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007249 use_conpty = TRUE;
7250 else if (has_winpty)
7251 use_winpty = TRUE;
7252 // else: error
7253 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007254 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007255 {
7256 if (has_winpty)
7257 use_winpty = TRUE;
7258 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007259 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007260 {
7261 if (has_conpty)
7262 use_conpty = TRUE;
7263 else
7264 return dyn_conpty_init(TRUE);
7265 }
7266
7267 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007268 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007269
7270 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007271 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007272
7273 // error
7274 return dyn_winpty_init(TRUE);
7275}
7276
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007277 static int
7278create_pty_only(term_T *term, jobopt_T *options)
7279{
7280 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7281 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7282 char in_name[80], out_name[80];
7283 channel_T *channel = NULL;
7284
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007285 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7286 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007287
7288 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7289 GetCurrentProcessId(),
7290 curbuf->b_fnum);
7291 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7292 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7293 PIPE_UNLIMITED_INSTANCES,
7294 0, 0, NMPWAIT_NOWAIT, NULL);
7295 if (hPipeIn == INVALID_HANDLE_VALUE)
7296 goto failed;
7297
7298 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7299 GetCurrentProcessId(),
7300 curbuf->b_fnum);
7301 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7302 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7303 PIPE_UNLIMITED_INSTANCES,
7304 0, 0, 0, NULL);
7305 if (hPipeOut == INVALID_HANDLE_VALUE)
7306 goto failed;
7307
7308 ConnectNamedPipe(hPipeIn, NULL);
7309 ConnectNamedPipe(hPipeOut, NULL);
7310
7311 term->tl_job = job_alloc();
7312 if (term->tl_job == NULL)
7313 goto failed;
7314 ++term->tl_job->jv_refcount;
7315
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007316 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007317 term->tl_job->jv_status = JOB_FINISHED;
7318
7319 channel = add_channel();
7320 if (channel == NULL)
7321 goto failed;
7322 term->tl_job->jv_channel = channel;
7323 channel->ch_keep_open = TRUE;
7324 channel->ch_named_pipe = TRUE;
7325
7326 channel_set_pipes(channel,
7327 (sock_T)hPipeIn,
7328 (sock_T)hPipeOut,
7329 (sock_T)hPipeOut);
7330 channel_set_job(channel, term->tl_job, options);
7331 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7332 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7333
7334 return OK;
7335
7336failed:
7337 if (hPipeIn != NULL)
7338 CloseHandle(hPipeIn);
7339 if (hPipeOut != NULL)
7340 CloseHandle(hPipeOut);
7341 return FAIL;
7342}
7343
7344/*
7345 * Free the terminal emulator part of "term".
7346 */
7347 static void
7348term_free_vterm(term_T *term)
7349{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007350 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007351 if (term->tl_winpty != NULL)
7352 winpty_free(term->tl_winpty);
7353 term->tl_winpty = NULL;
7354 if (term->tl_winpty_config != NULL)
7355 winpty_config_free(term->tl_winpty_config);
7356 term->tl_winpty_config = NULL;
7357 if (term->tl_vterm != NULL)
7358 vterm_free(term->tl_vterm);
7359 term->tl_vterm = NULL;
7360}
7361
7362/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007363 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007364 */
7365 static void
7366term_report_winsize(term_T *term, int rows, int cols)
7367{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007368 if (term->tl_conpty)
7369 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007370 if (term->tl_winpty)
7371 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7372}
7373
7374 int
7375terminal_enabled(void)
7376{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007377 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007378}
7379
7380# else
7381
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007382///////////////////////////////////////
7383// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007384
7385/*
7386 * Create a new terminal of "rows" by "cols" cells.
7387 * Start job for "cmd".
7388 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007389 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007390 * Return OK or FAIL.
7391 */
7392 static int
7393term_and_job_init(
7394 term_T *term,
7395 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007396 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007397 jobopt_T *opt,
7398 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007399{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007400 term->tl_arg0_cmd = NULL;
7401
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007402 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7403 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007404
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007405#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7406 if (opt->jo_set2 & JO2_ANSI_COLORS)
7407 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7408 else
7409 init_vterm_ansi_colors(term->tl_vterm);
7410#endif
7411
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007412 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007413 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007414 if (term->tl_job != NULL)
7415 ++term->tl_job->jv_refcount;
7416
7417 return term->tl_job != NULL
7418 && term->tl_job->jv_channel != NULL
7419 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7420}
7421
7422 static int
7423create_pty_only(term_T *term, jobopt_T *opt)
7424{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007425 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7426 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007427
7428 term->tl_job = job_alloc();
7429 if (term->tl_job == NULL)
7430 return FAIL;
7431 ++term->tl_job->jv_refcount;
7432
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007433 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007434 term->tl_job->jv_status = JOB_FINISHED;
7435
7436 return mch_create_pty_channel(term->tl_job, opt);
7437}
7438
7439/*
7440 * Free the terminal emulator part of "term".
7441 */
7442 static void
7443term_free_vterm(term_T *term)
7444{
7445 if (term->tl_vterm != NULL)
7446 vterm_free(term->tl_vterm);
7447 term->tl_vterm = NULL;
7448}
7449
7450/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007451 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007452 */
7453 static void
7454term_report_winsize(term_T *term, int rows, int cols)
7455{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007456 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007457 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7458 {
7459 int fd = -1;
7460 int part;
7461
7462 for (part = PART_OUT; part < PART_COUNT; ++part)
7463 {
7464 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007465 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007466 break;
7467 }
7468 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7469 mch_signal_job(term->tl_job, (char_u *)"winch");
7470 }
7471}
7472
7473# endif
7474
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007475#endif // FEAT_TERMINAL