blob: d9f1e0f82162ce54e9549432c20328c5d3685fdc [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
LemonBoyb2b3acb2022-05-20 10:10:34 +0100165 long_u *tl_palette; // array of 16 colors specified by term_start, can
166 // be NULL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200167 int tl_using_altscreen;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200168 garray_T tl_osc_buf; // incomplete OSC string
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200169};
170
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100171#define TMODE_ONCE 1 // CTRL-\ CTRL-N used
172#define TMODE_LOOP 2 // CTRL-W N used
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200173
174/*
175 * List of all active terminals.
176 */
177static term_T *first_term = NULL;
178
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100179// Terminal active in terminal_loop().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200180static term_T *in_terminal_loop = NULL;
181
Bram Moolenaar4f974752019-02-17 17:44:42 +0100182#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100183static BOOL has_winpty = FALSE;
184static BOOL has_conpty = FALSE;
185#endif
186
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100187#define MAX_ROW 999999 // used for tl_dirty_row_end to update all rows
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200188#define KEY_BUF_LEN 200
189
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200190#define FOR_ALL_TERMS(term) \
191 for ((term) = first_term; (term) != NULL; (term) = (term)->tl_next)
192
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200193/*
194 * Functions with separate implementation for MS-Windows and Unix-like systems.
195 */
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200196static 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 +0200197static int create_pty_only(term_T *term, jobopt_T *opt);
198static void term_report_winsize(term_T *term, int rows, int cols);
199static void term_free_vterm(term_T *term);
Bram Moolenaar13568252018-03-16 20:46:58 +0100200#ifdef FEAT_GUI
201static void update_system_term(term_T *term);
202#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200203
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100204static void handle_postponed_scrollback(term_T *term);
205
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100206// The character that we know (or assume) that the terminal expects for the
207// backspace key.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200208static int term_backspace_char = BS;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200209
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100210// Store the last set and the desired cursor properties, so that we only update
211// them when needed. Doing it unnecessary may result in flicker.
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200212static char_u *last_set_cursor_color = NULL;
213static char_u *desired_cursor_color = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +0100214static int last_set_cursor_shape = -1;
215static int desired_cursor_shape = -1;
216static int last_set_cursor_blink = -1;
217static int desired_cursor_blink = -1;
218
219
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100220///////////////////////////////////////
221// 1. Generic code for all systems.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200222
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200223 static int
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200224cursor_color_equal(char_u *lhs_color, char_u *rhs_color)
225{
226 if (lhs_color != NULL && rhs_color != NULL)
227 return STRCMP(lhs_color, rhs_color) == 0;
228 return lhs_color == NULL && rhs_color == NULL;
229}
230
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200231 static void
232cursor_color_copy(char_u **to_color, char_u *from_color)
233{
234 // Avoid a free & alloc if the value is already right.
235 if (cursor_color_equal(*to_color, from_color))
236 return;
237 vim_free(*to_color);
238 *to_color = (from_color == NULL) ? NULL : vim_strsave(from_color);
239}
240
241 static char_u *
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200242cursor_color_get(char_u *color)
243{
244 return (color == NULL) ? (char_u *)"" : color;
245}
246
247
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200248/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200249 * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
Bram Moolenaar498c2562018-04-15 23:45:15 +0200250 * current window.
251 * Sets "rows" and/or "cols" to zero when it should follow the window size.
252 * Return TRUE if the size is the minimum size: "24*80".
253 */
254 static int
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200255parse_termwinsize(win_T *wp, int *rows, int *cols)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200256{
257 int minsize = FALSE;
258
259 *rows = 0;
260 *cols = 0;
261
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200262 if (*wp->w_p_tws != NUL)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200263 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200264 char_u *p = vim_strchr(wp->w_p_tws, 'x');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200265
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100266 // Syntax of value was already checked when it's set.
Bram Moolenaar498c2562018-04-15 23:45:15 +0200267 if (p == NULL)
268 {
269 minsize = TRUE;
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200270 p = vim_strchr(wp->w_p_tws, '*');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200271 }
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200272 *rows = atoi((char *)wp->w_p_tws);
Bram Moolenaar498c2562018-04-15 23:45:15 +0200273 *cols = atoi((char *)p + 1);
274 }
275 return minsize;
276}
277
278/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200279 * Determine the terminal size from 'termwinsize' and the current window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200280 */
281 static void
Bram Moolenaarb936b792020-09-04 18:34:09 +0200282set_term_and_win_size(term_T *term, jobopt_T *opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200283{
Bram Moolenaarb936b792020-09-04 18:34:09 +0200284 int rows, cols;
285 int minsize;
286
Bram Moolenaar13568252018-03-16 20:46:58 +0100287#ifdef FEAT_GUI
288 if (term->tl_system)
289 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100290 // Use the whole screen for the system command. However, it will start
291 // at the command line and scroll up as needed, using tl_toprow.
Bram Moolenaar13568252018-03-16 20:46:58 +0100292 term->tl_rows = Rows;
293 term->tl_cols = Columns;
Bram Moolenaar07b46af2018-04-10 14:56:18 +0200294 return;
Bram Moolenaar13568252018-03-16 20:46:58 +0100295 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100296#endif
Bram Moolenaarb936b792020-09-04 18:34:09 +0200297 term->tl_rows = curwin->w_height;
298 term->tl_cols = curwin->w_width;
299
300 minsize = parse_termwinsize(curwin, &rows, &cols);
301 if (minsize)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200302 {
Bram Moolenaarb936b792020-09-04 18:34:09 +0200303 if (term->tl_rows < rows)
304 term->tl_rows = rows;
305 if (term->tl_cols < cols)
306 term->tl_cols = cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200307 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200308 if ((opt->jo_set2 & JO2_TERM_ROWS))
309 term->tl_rows = opt->jo_term_rows;
310 else if (rows != 0)
311 term->tl_rows = rows;
312 if ((opt->jo_set2 & JO2_TERM_COLS))
313 term->tl_cols = opt->jo_term_cols;
314 else if (cols != 0)
315 term->tl_cols = cols;
316
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200317 if (!opt->jo_hidden)
Bram Moolenaarb936b792020-09-04 18:34:09 +0200318 {
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200319 if (term->tl_rows != curwin->w_height)
320 win_setheight_win(term->tl_rows, curwin);
321 if (term->tl_cols != curwin->w_width)
322 win_setwidth_win(term->tl_cols, curwin);
Bram Moolenaarb936b792020-09-04 18:34:09 +0200323
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200324 // Set 'winsize' now to avoid a resize at the next redraw.
325 if (!minsize && *curwin->w_p_tws != NUL)
326 {
327 char_u buf[100];
328
329 vim_snprintf((char *)buf, 100, "%dx%d",
330 term->tl_rows, term->tl_cols);
Bram Moolenaarac4174e2022-05-07 16:38:24 +0100331 set_option_value_give_err((char_u *)"termwinsize",
332 0L, buf, OPT_LOCAL);
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200333 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200334 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200335}
336
337/*
338 * Initialize job options for a terminal job.
339 * Caller may overrule some of them.
340 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100341 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200342init_job_options(jobopt_T *opt)
343{
344 clear_job_options(opt);
345
Bram Moolenaarac4174e2022-05-07 16:38:24 +0100346 opt->jo_mode = CH_MODE_RAW;
347 opt->jo_out_mode = CH_MODE_RAW;
348 opt->jo_err_mode = CH_MODE_RAW;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200349 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
350}
351
352/*
353 * Set job options mandatory for a terminal job.
354 */
355 static void
356setup_job_options(jobopt_T *opt, int rows, int cols)
357{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100358#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100359 // Win32: Redirecting the job output won't work, thus always connect stdout
360 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200361 if (!(opt->jo_set & JO_OUT_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200362#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200363 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100364 // Connect stdout to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200365 opt->jo_io[PART_OUT] = JIO_BUFFER;
366 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
367 opt->jo_modifiable[PART_OUT] = 0;
368 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
369 }
370
Bram Moolenaar4f974752019-02-17 17:44:42 +0100371#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100372 // Win32: Redirecting the job output won't work, thus always connect stderr
373 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200374 if (!(opt->jo_set & JO_ERR_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200375#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200376 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100377 // Connect stderr to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200378 opt->jo_io[PART_ERR] = JIO_BUFFER;
379 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
380 opt->jo_modifiable[PART_ERR] = 0;
381 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
382 }
383
384 opt->jo_pty = TRUE;
385 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
386 opt->jo_term_rows = rows;
387 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
388 opt->jo_term_cols = cols;
389}
390
391/*
Bram Moolenaar5c381eb2019-06-25 06:50:31 +0200392 * Flush messages on channels.
393 */
394 static void
395term_flush_messages()
396{
397 mch_check_messages();
398 parse_queued_messages();
399}
400
401/*
Bram Moolenaard96ff162018-02-18 22:13:29 +0100402 * Close a terminal buffer (and its window). Used when creating the terminal
403 * fails.
404 */
405 static void
406term_close_buffer(buf_T *buf, buf_T *old_curbuf)
407{
408 free_terminal(buf);
409 if (old_curbuf != NULL)
410 {
411 --curbuf->b_nwindows;
412 curbuf = old_curbuf;
413 curwin->w_buffer = curbuf;
414 ++curbuf->b_nwindows;
415 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100416 CHECK_CURBUF;
Bram Moolenaard96ff162018-02-18 22:13:29 +0100417
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100418 // Wiping out the buffer will also close the window and call
419 // free_terminal().
Bram Moolenaard96ff162018-02-18 22:13:29 +0100420 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
421}
422
423/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200424 * Start a terminal window and return its buffer.
Bram Moolenaar13568252018-03-16 20:46:58 +0100425 * Use either "argvar" or "argv", the other must be NULL.
426 * When "flags" has TERM_START_NOJOB only create the buffer, b_term and open
427 * the window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200428 * Returns NULL when failed.
429 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100430 buf_T *
431term_start(
432 typval_T *argvar,
433 char **argv,
434 jobopt_T *opt,
435 int flags)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200436{
437 exarg_T split_ea;
438 win_T *old_curwin = curwin;
439 term_T *term;
440 buf_T *old_curbuf = NULL;
441 int res;
442 buf_T *newbuf;
Bram Moolenaare1004402020-10-24 20:49:43 +0200443 int vertical = opt->jo_vertical || (cmdmod.cmod_split & WSP_VERT);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200444 jobopt_T orig_opt; // only partly filled
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200445
446 if (check_restricted() || check_secure())
447 return NULL;
Bram Moolenaare5b44862021-05-30 13:54:03 +0200448 if (cmdwin_type != 0)
449 {
450 emsg(_(e_cannot_open_terminal_from_command_line_window));
451 return NULL;
452 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200453
454 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
455 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
456 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
Bram Moolenaarb0992022020-01-30 14:55:42 +0100457 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))
458 || (argvar != NULL
459 && argvar->v_type == VAR_LIST
460 && argvar->vval.v_list != NULL
461 && argvar->vval.v_list->lv_first == &range_list_item))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200462 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000463 emsg(_(e_invalid_argument));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200464 return NULL;
465 }
466
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200467 term = ALLOC_CLEAR_ONE(term_T);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200468 if (term == NULL)
469 return NULL;
470 term->tl_dirty_row_end = MAX_ROW;
471 term->tl_cursor_visible = TRUE;
472 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
473 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100474#ifdef FEAT_GUI
475 term->tl_system = (flags & TERM_START_SYSTEM);
476#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200477 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100478 ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200479 ga_init2(&term->tl_osc_buf, sizeof(char), 300);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200480
Bram Moolenaaraeed2a62021-04-29 20:18:45 +0200481 setpcmark();
Bram Moolenaara80faa82020-04-12 19:37:17 +0200482 CLEAR_FIELD(split_ea);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200483 if (opt->jo_curwin)
484 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100485 // Create a new buffer in the current window.
Bram Moolenaar13568252018-03-16 20:46:58 +0100486 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200487 {
488 no_write_message();
489 vim_free(term);
490 return NULL;
491 }
492 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaarb1009092020-05-31 16:04:42 +0200493 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
494 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
495 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200496 {
497 vim_free(term);
498 return NULL;
499 }
500 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100501 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200502 {
503 buf_T *buf;
504
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100505 // Create a new buffer without a window. Make it the current buffer for
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100506 // a moment to be able to do the initializations.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200507 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
508 BLN_NEW | BLN_LISTED);
509 if (buf == NULL || ml_open(buf) == FAIL)
510 {
511 vim_free(term);
512 return NULL;
513 }
514 old_curbuf = curbuf;
515 --curbuf->b_nwindows;
516 curbuf = buf;
517 curwin->w_buffer = buf;
518 ++curbuf->b_nwindows;
519 }
520 else
521 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100522 // Open a new window or tab.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200523 split_ea.cmdidx = CMD_new;
524 split_ea.cmd = (char_u *)"new";
525 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100526 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200527 {
528 split_ea.line2 = opt->jo_term_rows;
529 split_ea.addr_count = 1;
530 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100531 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200532 {
533 split_ea.line2 = opt->jo_term_cols;
534 split_ea.addr_count = 1;
535 }
536
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100537 if (vertical)
Bram Moolenaare1004402020-10-24 20:49:43 +0200538 cmdmod.cmod_split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200539 ex_splitview(&split_ea);
540 if (curwin == old_curwin)
541 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100542 // split failed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200543 vim_free(term);
544 return NULL;
545 }
546 }
547 term->tl_buffer = curbuf;
548 curbuf->b_term = term;
549
550 if (!opt->jo_hidden)
551 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100552 // Only one size was taken care of with :new, do the other one. With
553 // "curwin" both need to be done.
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100554 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200555 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100556 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200557 win_setwidth(opt->jo_term_cols);
558 }
559
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100560 // Link the new terminal in the list of active terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200561 term->tl_next = first_term;
562 first_term = term;
563
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100564 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
565
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200566 if (opt->jo_term_name != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100567 {
568 vim_free(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200569 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100570 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100571 else if (argv != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100572 {
573 vim_free(curbuf->b_ffname);
Bram Moolenaar13568252018-03-16 20:46:58 +0100574 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100575 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200576 else
577 {
578 int i;
579 size_t len;
580 char_u *cmd, *p;
581
582 if (argvar->v_type == VAR_STRING)
583 {
584 cmd = argvar->vval.v_string;
585 if (cmd == NULL)
586 cmd = (char_u *)"";
587 else if (STRCMP(cmd, "NONE") == 0)
588 cmd = (char_u *)"pty";
589 }
590 else if (argvar->v_type != VAR_LIST
591 || argvar->vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +0100592 || argvar->vval.v_list->lv_len == 0
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100593 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200594 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
595 cmd = (char_u*)"";
596
597 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200598 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200599
600 for (i = 0; p != NULL; ++i)
601 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100602 // Prepend a ! to the command name to avoid the buffer name equals
603 // the executable, otherwise ":w!" would overwrite it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200604 if (i == 0)
605 vim_snprintf((char *)p, len, "!%s", cmd);
606 else
607 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
608 if (buflist_findname(p) == NULL)
609 {
610 vim_free(curbuf->b_ffname);
611 curbuf->b_ffname = p;
612 break;
613 }
614 }
615 }
Bram Moolenaare010c722020-02-24 21:37:54 +0100616 vim_free(curbuf->b_sfname);
617 curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200618 curbuf->b_fname = curbuf->b_ffname;
619
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100620 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
621
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200622 if (opt->jo_term_opencmd != NULL)
623 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
624
625 if (opt->jo_eof_chars != NULL)
626 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
627
628 set_string_option_direct((char_u *)"buftype", -1,
629 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200630 // Avoid that 'buftype' is reset when this buffer is entered.
631 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200632
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100633 // Mark the buffer as not modifiable. It can only be made modifiable after
634 // the job finished.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200635 curbuf->b_p_ma = FALSE;
636
Bram Moolenaarb936b792020-09-04 18:34:09 +0200637 set_term_and_win_size(term, opt);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100638#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200639 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
640#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200641 setup_job_options(opt, term->tl_rows, term->tl_cols);
642
Bram Moolenaar13568252018-03-16 20:46:58 +0100643 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100644 return curbuf;
645
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100646#if defined(FEAT_SESSION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100647 // Remember the command for the session file.
Bram Moolenaar13568252018-03-16 20:46:58 +0100648 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100649 term->tl_command = vim_strsave((char_u *)"NONE");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100650 else if (argvar->v_type == VAR_STRING)
651 {
652 char_u *cmd = argvar->vval.v_string;
653
654 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
655 term->tl_command = vim_strsave(cmd);
656 }
657 else if (argvar->v_type == VAR_LIST
658 && argvar->vval.v_list != NULL
659 && argvar->vval.v_list->lv_len > 0)
660 {
661 garray_T ga;
662 listitem_T *item;
663
664 ga_init2(&ga, 1, 100);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200665 FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100666 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100667 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100668 char_u *p;
669
670 if (s == NULL)
671 break;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100672 p = vim_strsave_fnameescape(s, VSE_NONE);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100673 if (p == NULL)
674 break;
675 ga_concat(&ga, p);
676 vim_free(p);
677 ga_append(&ga, ' ');
678 }
679 if (item == NULL)
680 {
681 ga_append(&ga, NUL);
682 term->tl_command = ga.ga_data;
683 }
684 else
685 ga_clear(&ga);
686 }
687#endif
688
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100689 if (opt->jo_term_kill != NULL)
690 {
691 char_u *p = skiptowhite(opt->jo_term_kill);
692
693 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
694 }
695
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200696 if (opt->jo_term_api != NULL)
Bram Moolenaar21109272020-01-30 16:27:20 +0100697 {
698 char_u *p = skiptowhite(opt->jo_term_api);
699
700 term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
701 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200702 else
703 term->tl_api = vim_strsave((char_u *)"Tapi_");
704
Bram Moolenaar83d47902020-03-26 20:34:00 +0100705 if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
706 term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
707
Bram Moolenaar30b9a412022-05-26 14:06:37 +0100708#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
LemonBoyb2b3acb2022-05-20 10:10:34 +0100709 // Save the user-defined palette, it is only used in GUI (or 'tgc' is on).
710 if (opt->jo_set2 & JO2_ANSI_COLORS)
711 {
712 term->tl_palette = ALLOC_MULT(long_u, 16);
713 if (term->tl_palette == NULL)
714 {
715 vim_free(term);
716 return NULL;
717 }
718 memcpy(term->tl_palette, opt->jo_ansi_colors, sizeof(long_u) * 16);
719 }
Bram Moolenaar30b9a412022-05-26 14:06:37 +0100720#endif
LemonBoyb2b3acb2022-05-20 10:10:34 +0100721
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100722 // System dependent: setup the vterm and maybe start the job in it.
Bram Moolenaar13568252018-03-16 20:46:58 +0100723 if (argv == NULL
724 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200725 && argvar->vval.v_string != NULL
726 && STRCMP(argvar->vval.v_string, "NONE") == 0)
727 res = create_pty_only(term, opt);
728 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200729 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200730
731 newbuf = curbuf;
732 if (res == OK)
733 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100734 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200735 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
736 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100737#ifdef FEAT_GUI
738 if (term->tl_system)
739 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100740 // display first line below typed command
Bram Moolenaar13568252018-03-16 20:46:58 +0100741 term->tl_toprow = msg_row + 1;
742 term->tl_dirty_row_end = 0;
743 }
744#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200745
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100746 // Make sure we don't get stuck on sending keys to the job, it leads to
747 // a deadlock if the job is waiting for Vim to read.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200748 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
749
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200750 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200751 {
752 --curbuf->b_nwindows;
753 curbuf = old_curbuf;
754 curwin->w_buffer = curbuf;
755 ++curbuf->b_nwindows;
756 }
Bram Moolenaar81035272021-12-16 18:02:07 +0000757 else if (vgetc_busy
758#ifdef FEAT_TIMERS
759 || timer_busy
760#endif
761 || input_busy)
762 {
763 char_u ignore[4];
764
765 // When waiting for input need to return and possibly end up in
766 // terminal_loop() instead.
767 ignore[0] = K_SPECIAL;
768 ignore[1] = KS_EXTRA;
769 ignore[2] = KE_IGNORE;
770 ignore[3] = NUL;
771 ins_typebuf(ignore, REMAP_NONE, 0, TRUE, FALSE);
772 typebuf_was_filled = TRUE;
773 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200774 }
775 else
776 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100777 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200778 return NULL;
779 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100780
Bram Moolenaar13568252018-03-16 20:46:58 +0100781 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar28ed4df2019-10-26 16:21:40 +0200782 if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
783 apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200784 return newbuf;
785}
786
787/*
788 * ":terminal": open a terminal window and execute a job in it.
789 */
790 void
791ex_terminal(exarg_T *eap)
792{
793 typval_T argvar[2];
794 jobopt_T opt;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100795 int opt_shell = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200796 char_u *cmd;
797 char_u *tofree = NULL;
798
799 init_job_options(&opt);
800
801 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100802 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200803 {
804 char_u *p, *ep;
805
806 cmd += 2;
807 p = skiptowhite(cmd);
808 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200809 if (ep != NULL)
810 {
811 if (ep < p)
812 p = ep;
813 else
814 ep = NULL;
815 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200816
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200817# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
818 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
819 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200820 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200821 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100822 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200823 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200824 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200825 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200826 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200827 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200828 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200829 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100830 opt.jo_term_norestore = 1;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100831 else if (OPTARG_HAS("shell"))
832 opt_shell = TRUE;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200833 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100834 {
835 opt.jo_set2 |= JO2_TERM_KILL;
836 opt.jo_term_kill = ep + 1;
837 p = skiptowhite(cmd);
838 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200839 else if (OPTARG_HAS("api"))
840 {
841 opt.jo_set2 |= JO2_TERM_API;
842 if (ep != NULL)
843 {
844 opt.jo_term_api = ep + 1;
845 p = skiptowhite(cmd);
846 }
847 else
848 opt.jo_term_api = NULL;
849 }
850 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200851 {
852 opt.jo_set2 |= JO2_TERM_ROWS;
853 opt.jo_term_rows = atoi((char *)ep + 1);
854 p = skiptowhite(cmd);
855 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200856 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200857 {
858 opt.jo_set2 |= JO2_TERM_COLS;
859 opt.jo_term_cols = atoi((char *)ep + 1);
860 p = skiptowhite(cmd);
861 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200862 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200863 {
864 char_u *buf = NULL;
865 char_u *keys;
866
Bram Moolenaar21109272020-01-30 16:27:20 +0100867 vim_free(opt.jo_eof_chars);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200868 p = skiptowhite(cmd);
869 *p = NUL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200870 keys = replace_termcodes(ep + 1, &buf,
871 REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200872 opt.jo_set2 |= JO2_EOF_CHARS;
873 opt.jo_eof_chars = vim_strsave(keys);
874 vim_free(buf);
875 *p = ' ';
876 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100877#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100878 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
879 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100880 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100881 int tty_type = NUL;
882
883 p = skiptowhite(cmd);
884 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
885 tty_type = 'w';
886 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
887 tty_type = 'c';
888 else
889 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000890 semsg(e_invalid_value_for_argument_str, "type");
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100891 goto theend;
892 }
893 opt.jo_set2 |= JO2_TTY_TYPE;
894 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100895 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100896#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200897 else
898 {
899 if (*p)
900 *p = NUL;
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000901 semsg(_(e_invalid_attribute_str), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100902 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200903 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200904# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200905 cmd = skipwhite(p);
906 }
907 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100908 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100909 // Make a copy of 'shell', an autocommand may change the option.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200910 tofree = cmd = vim_strsave(p_sh);
911
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100912 // default to close when the shell exits
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100913 if (opt.jo_term_finish == NUL)
Bram Moolenaare2978022020-04-26 14:47:44 +0200914 opt.jo_term_finish = TL_FINISH_CLOSE;
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100915 }
916
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200917 if (eap->addr_count > 0)
918 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100919 // Write lines from current buffer to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200920 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
921 opt.jo_io[PART_IN] = JIO_BUFFER;
922 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
923 opt.jo_in_top = eap->line1;
924 opt.jo_in_bot = eap->line2;
925 }
926
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100927 if (opt_shell && tofree == NULL)
928 {
929#ifdef UNIX
930 char **argv = NULL;
931 char_u *tofree1 = NULL;
932 char_u *tofree2 = NULL;
933
934 // :term ++shell command
935 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
936 term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaaradf4aa22019-11-10 22:36:44 +0100937 vim_free(argv);
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100938 vim_free(tofree1);
939 vim_free(tofree2);
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100940 goto theend;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100941#else
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100942# ifdef MSWIN
943 long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
944 char_u *newcmd;
945
946 newcmd = alloc(cmdlen);
947 if (newcmd == NULL)
948 goto theend;
949 tofree = newcmd;
950 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
951 cmd = newcmd;
952# else
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000953 emsg(_(e_sorry_plusplusshell_not_supported_on_this_system));
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100954 goto theend;
955# endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100956#endif
957 }
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100958 argvar[0].v_type = VAR_STRING;
959 argvar[0].vval.v_string = cmd;
960 argvar[1].v_type = VAR_UNKNOWN;
961 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100962
963theend:
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100964 vim_free(tofree);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200965 vim_free(opt.jo_eof_chars);
966}
967
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100968#if defined(FEAT_SESSION) || defined(PROTO)
969/*
970 * Write a :terminal command to the session file to restore the terminal in
971 * window "wp".
972 * Return FAIL if writing fails.
973 */
974 int
Bram Moolenaar0e655112020-09-11 20:36:36 +0200975term_write_session(FILE *fd, win_T *wp, hashtab_T *terminal_bufs)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100976{
Bram Moolenaar0e655112020-09-11 20:36:36 +0200977 const int bufnr = wp->w_buffer->b_fnum;
978 term_T *term = wp->w_buffer->b_term;
979
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200980 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200981 {
982 // There are multiple views into this terminal buffer. We don't want to
983 // create the terminal multiple times. If it's the first time, create,
984 // otherwise link to the first buffer.
985 char id_as_str[NUMBUFLEN];
986 hashitem_T *entry;
987
988 vim_snprintf(id_as_str, sizeof(id_as_str), "%d", bufnr);
989
990 entry = hash_find(terminal_bufs, (char_u *)id_as_str);
991 if (!HASHITEM_EMPTY(entry))
992 {
993 // we've already opened this terminal buffer
994 if (fprintf(fd, "execute 'buffer ' . s:term_buf_%d", bufnr) < 0)
995 return FAIL;
996 return put_eol(fd);
997 }
998 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100999
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001000 // Create the terminal and run the command. This is not without
1001 // risk, but let's assume the user only creates a session when this
1002 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001003 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
1004 term->tl_cols, term->tl_rows) < 0)
1005 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001006#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01001007 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
1008 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001009#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001010 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
1011 return FAIL;
Bram Moolenaar0e655112020-09-11 20:36:36 +02001012 if (put_eol(fd) != OK)
1013 return FAIL;
1014
1015 if (fprintf(fd, "let s:term_buf_%d = bufnr()", bufnr) < 0)
1016 return FAIL;
1017
Bram Moolenaarc2c82052020-09-11 22:10:22 +02001018 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +02001019 {
1020 char *hash_key = alloc(NUMBUFLEN);
1021
1022 vim_snprintf(hash_key, NUMBUFLEN, "%d", bufnr);
1023 hash_add(terminal_bufs, (char_u *)hash_key);
1024 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001025
1026 return put_eol(fd);
1027}
1028
1029/*
1030 * Return TRUE if "buf" has a terminal that should be restored.
1031 */
1032 int
1033term_should_restore(buf_T *buf)
1034{
1035 term_T *term = buf->b_term;
1036
1037 return term != NULL && (term->tl_command == NULL
1038 || STRCMP(term->tl_command, "NONE") != 0);
1039}
1040#endif
1041
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001042/*
1043 * Free the scrollback buffer for "term".
1044 */
1045 static void
1046free_scrollback(term_T *term)
1047{
1048 int i;
1049
1050 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
1051 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
1052 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001053 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
1054 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
1055 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001056}
1057
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001058
1059// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +02001060static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001061
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001062/*
1063 * Free a terminal and everything it refers to.
1064 * Kills the job if there is one.
1065 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001066 * The actual terminal structure is freed later in free_unused_terminals(),
1067 * because callbacks may wipe out a buffer while the terminal is still
1068 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001069 */
1070 void
1071free_terminal(buf_T *buf)
1072{
1073 term_T *term = buf->b_term;
1074 term_T *tp;
1075
1076 if (term == NULL)
1077 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001078
1079 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001080 if (first_term == term)
1081 first_term = term->tl_next;
1082 else
1083 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
1084 if (tp->tl_next == term)
1085 {
1086 tp->tl_next = term->tl_next;
1087 break;
1088 }
1089
1090 if (term->tl_job != NULL)
1091 {
1092 if (term->tl_job->jv_status != JOB_ENDED
1093 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +01001094 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001095 job_stop(term->tl_job, NULL, "kill");
1096 job_unref(term->tl_job);
1097 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001098 term->tl_next = terminals_to_free;
1099 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001100
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001101 buf->b_term = NULL;
1102 if (in_terminal_loop == term)
1103 in_terminal_loop = NULL;
1104}
1105
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001106 void
1107free_unused_terminals()
1108{
1109 while (terminals_to_free != NULL)
1110 {
1111 term_T *term = terminals_to_free;
1112
1113 terminals_to_free = term->tl_next;
1114
1115 free_scrollback(term);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02001116 ga_clear(&term->tl_osc_buf);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001117
1118 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001119 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001120 vim_free(term->tl_title);
1121#ifdef FEAT_SESSION
1122 vim_free(term->tl_command);
1123#endif
1124 vim_free(term->tl_kill);
1125 vim_free(term->tl_status_text);
1126 vim_free(term->tl_opencmd);
1127 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001128 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001129#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001130 if (term->tl_out_fd != NULL)
1131 fclose(term->tl_out_fd);
1132#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001133 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001134 vim_free(term->tl_cursor_color);
LemonBoyb2b3acb2022-05-20 10:10:34 +01001135 vim_free(term->tl_palette);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001136 vim_free(term);
1137 }
1138}
1139
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001140/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001141 * Get the part that is connected to the tty. Normally this is PART_IN, but
1142 * when writing buffer lines to the job it can be another. This makes it
1143 * possible to do "1,5term vim -".
1144 */
1145 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001146get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001147{
1148#ifdef UNIX
1149 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1150 int i;
1151
1152 for (i = 0; i < 3; ++i)
1153 {
1154 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1155
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001156 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001157 return parts[i];
1158 }
1159#endif
1160 return PART_IN;
1161}
1162
1163/*
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001164 * Read any vterm output and send it on the channel.
1165 */
1166 static void
1167term_forward_output(term_T *term)
1168{
1169 VTerm *vterm = term->tl_vterm;
1170 char buf[KEY_BUF_LEN];
1171 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1172
1173 if (curlen > 0)
1174 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1175 (char_u *)buf, (int)curlen, NULL);
1176}
1177
1178/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001179 * Write job output "msg[len]" to the vterm.
1180 */
1181 static void
Bram Moolenaar36968af2021-11-15 17:13:11 +00001182term_write_job_output(term_T *term, char_u *msg_arg, size_t len_arg)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001183{
Bram Moolenaar36968af2021-11-15 17:13:11 +00001184 char_u *msg = msg_arg;
1185 size_t len = len_arg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001186 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001187 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar36968af2021-11-15 17:13:11 +00001188 size_t limit = term->tl_buffer->b_p_twsl * term->tl_cols * 3;
1189
1190 // Limit the length to 'termwinscroll' * cols * 3 bytes. Keep the text at
1191 // the end.
1192 if (len > limit)
1193 {
1194 char_u *p = msg + len - limit;
1195
1196 p -= (*mb_head_off)(msg, p);
1197 len -= p - msg;
1198 msg = p;
1199 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001200
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001201 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001203 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001204 if (prevlen != vterm_output_get_buffer_current(vterm))
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001205 term_forward_output(term);
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001206
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001207 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001208 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1209}
1210
1211 static void
1212update_cursor(term_T *term, int redraw)
1213{
1214 if (term->tl_normal_mode)
1215 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001216#ifdef FEAT_GUI
1217 if (term->tl_system)
1218 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1219 term->tl_cursor_pos.col);
1220 else
1221#endif
1222 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001223 if (redraw)
1224 {
Shougo Matsushita4ccaedf2022-10-15 11:48:00 +01001225 aco_save_T aco;
1226
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001227 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1228 cursor_on();
1229 out_flush();
1230#ifdef FEAT_GUI
1231 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001232 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001233 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001234 gui_mch_flush();
1235 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001236#endif
Shougo Matsushita4ccaedf2022-10-15 11:48:00 +01001237 // Make sure an invoked autocmd doesn't delete the buffer (and the
1238 // terminal) under our fingers.
1239 ++term->tl_buffer->b_locked;
1240
1241 // save and restore curwin and curbuf, in case the autocmd changes them
1242 aucmd_prepbuf(&aco, curbuf);
1243 apply_autocmds(EVENT_TEXTCHANGEDT, NULL, NULL, FALSE, term->tl_buffer);
1244 aucmd_restbuf(&aco);
1245
1246 --term->tl_buffer->b_locked;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001247 }
1248}
1249
1250/*
1251 * Invoked when "msg" output from a job was received. Write it to the terminal
1252 * of "buffer".
1253 */
1254 void
1255write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1256{
1257 size_t len = STRLEN(msg);
1258 term_T *term = buffer->b_term;
1259
Bram Moolenaar4f974752019-02-17 17:44:42 +01001260#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001261 // Win32: Cannot redirect output of the job, intercept it here and write to
1262 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001263 if (term->tl_out_fd != NULL)
1264 {
1265 ch_log(channel, "Writing %d bytes to output file", (int)len);
1266 fwrite(msg, len, 1, term->tl_out_fd);
1267 return;
1268 }
1269#endif
1270
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001271 if (term->tl_vterm == NULL)
1272 {
1273 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1274 return;
1275 }
1276 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01001277 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001278 term_write_job_output(term, msg, len);
1279
Bram Moolenaar13568252018-03-16 20:46:58 +01001280#ifdef FEAT_GUI
1281 if (term->tl_system)
1282 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001283 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001284 update_system_term(term);
1285 update_cursor(term, TRUE);
1286 }
1287 else
1288#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001289 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1290 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001291 if (!term->tl_normal_mode)
1292 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001293 // Don't use update_screen() when editing the command line, it gets
1294 // cleared.
1295 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001296 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar24959102022-05-07 20:01:16 +01001297 if (buffer == curbuf && (State & MODE_CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001298 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001299 update_screen(UPD_VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001300 // update_screen() can be slow, check the terminal wasn't closed
1301 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001302 if (buffer == curbuf && curbuf->b_term != NULL)
1303 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001304 }
1305 else
Bram Moolenaare5050712021-12-09 10:51:05 +00001306 redraw_after_callback(TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001307 }
1308}
1309
1310/*
1311 * Send a mouse position and click to the vterm
1312 */
1313 static int
1314term_send_mouse(VTerm *vterm, int button, int pressed)
1315{
1316 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001317 int row = mouse_row - W_WINROW(curwin);
1318 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001319
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001320#ifdef FEAT_PROP_POPUP
1321 if (popup_is_popup(curwin))
1322 {
1323 row -= popup_top_extra(curwin);
1324 col -= popup_left_extra(curwin);
1325 }
1326#endif
1327 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001328 if (button != 0)
1329 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001330 return TRUE;
1331}
1332
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001333static int enter_mouse_col = -1;
1334static int enter_mouse_row = -1;
1335
1336/*
1337 * Handle a mouse click, drag or release.
1338 * Return TRUE when a mouse event is sent to the terminal.
1339 */
1340 static int
1341term_mouse_click(VTerm *vterm, int key)
1342{
1343#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001344 // For modeless selection mouse drag and release events are ignored, unless
1345 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001346 static int ignore_drag_release = TRUE;
1347 VTermMouseState mouse_state;
1348
1349 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1350 if (mouse_state.flags == 0)
1351 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001352 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001353 switch (key)
1354 {
1355 case K_LEFTDRAG:
1356 case K_LEFTRELEASE:
1357 case K_RIGHTDRAG:
1358 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001359 // Ignore drag and release events when the button-down wasn't
1360 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001361 if (ignore_drag_release)
1362 {
1363 int save_mouse_col, save_mouse_row;
1364
1365 if (enter_mouse_col < 0)
1366 break;
1367
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001368 // mouse click in the window gave us focus, handle that
1369 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001370 save_mouse_col = mouse_col;
1371 save_mouse_row = mouse_row;
1372 mouse_col = enter_mouse_col;
1373 mouse_row = enter_mouse_row;
1374 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1375 mouse_col = save_mouse_col;
1376 mouse_row = save_mouse_row;
1377 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001378 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001379 case K_LEFTMOUSE:
1380 case K_RIGHTMOUSE:
1381 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1382 ignore_drag_release = TRUE;
1383 else
1384 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001385 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001386 if (clip_star.available)
1387 {
1388 int button, is_click, is_drag;
1389
1390 button = get_mouse_button(KEY2TERMCAP1(key),
1391 &is_click, &is_drag);
1392 if (mouse_model_popup() && button == MOUSE_LEFT
1393 && (mod_mask & MOD_MASK_SHIFT))
1394 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001395 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001396 button = MOUSE_RIGHT;
1397 mod_mask &= ~MOD_MASK_SHIFT;
1398 }
1399 clip_modeless(button, is_click, is_drag);
1400 }
1401 break;
1402
1403 case K_MIDDLEMOUSE:
1404 if (clip_star.available)
1405 insert_reg('*', TRUE);
1406 break;
1407 }
1408 enter_mouse_col = -1;
1409 return FALSE;
1410 }
1411#endif
1412 enter_mouse_col = -1;
1413
1414 switch (key)
1415 {
1416 case K_LEFTMOUSE:
1417 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1418 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1419 case K_LEFTRELEASE:
1420 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1421 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1422 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1423 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1424 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1425 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1426 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1427 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1428 }
1429 return TRUE;
1430}
1431
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001432/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001433 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1434 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001435 * Return the number of bytes in "buf".
1436 */
1437 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001438term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001439{
1440 VTerm *vterm = term->tl_vterm;
1441 VTermKey key = VTERM_KEY_NONE;
1442 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001443 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001444
1445 switch (c)
1446 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001447 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001448
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001449 // don't use VTERM_KEY_BACKSPACE, it always
1450 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001451 case K_BS: c = term_backspace_char; break;
1452
1453 case ESC: key = VTERM_KEY_ESCAPE; break;
1454 case K_DEL: key = VTERM_KEY_DEL; break;
1455 case K_DOWN: key = VTERM_KEY_DOWN; break;
1456 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1457 key = VTERM_KEY_DOWN; break;
1458 case K_END: key = VTERM_KEY_END; break;
1459 case K_S_END: mod = VTERM_MOD_SHIFT;
1460 key = VTERM_KEY_END; break;
1461 case K_C_END: mod = VTERM_MOD_CTRL;
1462 key = VTERM_KEY_END; break;
1463 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1464 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1465 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1466 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1467 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1468 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1469 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1470 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1471 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1472 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1473 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1474 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1475 case K_HOME: key = VTERM_KEY_HOME; break;
1476 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1477 key = VTERM_KEY_HOME; break;
1478 case K_C_HOME: mod = VTERM_MOD_CTRL;
1479 key = VTERM_KEY_HOME; break;
1480 case K_INS: key = VTERM_KEY_INS; break;
1481 case K_K0: key = VTERM_KEY_KP_0; break;
1482 case K_K1: key = VTERM_KEY_KP_1; break;
1483 case K_K2: key = VTERM_KEY_KP_2; break;
1484 case K_K3: key = VTERM_KEY_KP_3; break;
1485 case K_K4: key = VTERM_KEY_KP_4; break;
1486 case K_K5: key = VTERM_KEY_KP_5; break;
1487 case K_K6: key = VTERM_KEY_KP_6; break;
1488 case K_K7: key = VTERM_KEY_KP_7; break;
1489 case K_K8: key = VTERM_KEY_KP_8; break;
1490 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001491 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001492 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001493 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001494 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001495 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1496 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001497 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1498 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001499 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1500 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001501 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1502 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1503 case K_LEFT: key = VTERM_KEY_LEFT; break;
1504 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1505 key = VTERM_KEY_LEFT; break;
1506 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1507 key = VTERM_KEY_LEFT; break;
1508 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1509 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1510 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1511 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1512 key = VTERM_KEY_RIGHT; break;
1513 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1514 key = VTERM_KEY_RIGHT; break;
1515 case K_UP: key = VTERM_KEY_UP; break;
1516 case K_S_UP: mod = VTERM_MOD_SHIFT;
1517 key = VTERM_KEY_UP; break;
1518 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001519 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1520 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001521
Bram Moolenaara42ad572017-11-16 13:08:04 +01001522 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1523 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001524 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1525 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001526
1527 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001528 case K_LEFTMOUSE_NM:
1529 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001530 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001531 case K_LEFTRELEASE_NM:
1532 case K_MOUSEMOVE:
1533 case K_MIDDLEMOUSE:
1534 case K_MIDDLEDRAG:
1535 case K_MIDDLERELEASE:
1536 case K_RIGHTMOUSE:
1537 case K_RIGHTDRAG:
1538 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1539 return 0;
1540 other = TRUE;
1541 break;
1542
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001543 case K_X1MOUSE: /* TODO */ return 0;
1544 case K_X1DRAG: /* TODO */ return 0;
1545 case K_X1RELEASE: /* TODO */ return 0;
1546 case K_X2MOUSE: /* TODO */ return 0;
1547 case K_X2DRAG: /* TODO */ return 0;
1548 case K_X2RELEASE: /* TODO */ return 0;
1549
1550 case K_IGNORE: return 0;
1551 case K_NOP: return 0;
1552 case K_UNDO: return 0;
1553 case K_HELP: return 0;
1554 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1555 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1556 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1557 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1558 case K_SELECT: return 0;
1559#ifdef FEAT_GUI
1560 case K_VER_SCROLLBAR: return 0;
1561 case K_HOR_SCROLLBAR: return 0;
1562#endif
1563#ifdef FEAT_GUI_TABLINE
1564 case K_TABLINE: return 0;
1565 case K_TABMENU: return 0;
1566#endif
1567#ifdef FEAT_NETBEANS_INTG
1568 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1569#endif
1570#ifdef FEAT_DND
1571 case K_DROP: return 0;
1572#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001573 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001574 case K_PS: vterm_keyboard_start_paste(vterm);
1575 other = TRUE;
1576 break;
1577 case K_PE: vterm_keyboard_end_paste(vterm);
1578 other = TRUE;
1579 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001580 }
1581
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001582 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001583 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001584 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001585 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001586 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001587 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001588 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001589
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001590 /*
1591 * Convert special keys to vterm keys:
1592 * - Write keys to vterm: vterm_keyboard_key()
1593 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001594 */
1595 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001596 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001597 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001598 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001599 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001600 vterm_keyboard_unichar(vterm, c, mod);
1601
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001602 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001603 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1604}
1605
1606/*
1607 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001608 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001609 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001610 */
1611 static int
1612term_job_running_check(term_T *term, int check_job_status)
1613{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001614 // Also consider the job finished when the channel is closed, to avoid a
1615 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001616 if (term != NULL
1617 && term->tl_job != NULL
1618 && channel_is_open(term->tl_job->jv_channel))
1619 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001620 job_T *job = term->tl_job;
1621
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001622 // Careful: Checking the job status may invoke callbacks, which close
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001623 // the buffer and terminate "term". However, "job" will not be freed
1624 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001625 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001626 job_status(job);
1627 return (job->jv_status == JOB_STARTED
1628 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001629 }
1630 return FALSE;
1631}
1632
1633/*
1634 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001635 */
1636 int
1637term_job_running(term_T *term)
1638{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001639 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001640}
1641
1642/*
Bram Moolenaar9e636b92022-05-29 22:37:05 +01001643 * Return TRUE if the job for "term" is still running, ignoring the job was
1644 * "NONE".
1645 */
1646 int
1647term_job_running_not_none(term_T *term)
1648{
1649 return term_job_running(term) && !term_none_open(term);
1650}
1651
1652/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001653 * Return TRUE if "term" has an active channel and used ":term NONE".
1654 */
1655 int
1656term_none_open(term_T *term)
1657{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001658 // Also consider the job finished when the channel is closed, to avoid a
1659 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001660 return term != NULL
1661 && term->tl_job != NULL
1662 && channel_is_open(term->tl_job->jv_channel)
1663 && term->tl_job->jv_channel->ch_keep_open;
1664}
1665
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001666//
1667// Used to confirm whether we would like to kill a terminal.
1668// Return OK when the user confirms to kill it.
1669// Return FAIL if the user selects otherwise.
1670//
1671 int
1672term_confirm_stop(buf_T *buf)
1673{
1674 char_u buff[DIALOG_MSG_SIZE];
1675 int ret;
1676
1677 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
1678 ret = vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1);
1679 if (ret == VIM_YES)
1680 return OK;
1681 else
1682 return FAIL;
1683}
1684
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001685/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001686 * Used when exiting: kill the job in "buf" if so desired.
1687 * Return OK when the job finished.
1688 * Return FAIL when the job is still running.
1689 */
1690 int
1691term_try_stop_job(buf_T *buf)
1692{
1693 int count;
1694 char *how = (char *)buf->b_term->tl_kill;
1695
1696#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001697 if ((how == NULL || *how == NUL)
1698 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001699 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001700 if (term_confirm_stop(buf) == OK)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001701 how = "kill";
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001702 else
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001703 return FAIL;
1704 }
1705#endif
1706 if (how == NULL || *how == NUL)
1707 return FAIL;
1708
1709 job_stop(buf->b_term->tl_job, NULL, how);
1710
Bram Moolenaar9172d232019-01-29 23:06:54 +01001711 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001712 for (count = 0; count < 100; ++count)
1713 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001714 job_T *job;
1715
1716 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001717 if (!buf_valid(buf)
1718 || buf->b_term == NULL
1719 || buf->b_term->tl_job == NULL)
1720 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001721 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001722
Bram Moolenaar9172d232019-01-29 23:06:54 +01001723 // Call job_status() to update jv_status. It may cause the job to be
1724 // cleaned up but it won't be freed.
1725 job_status(job);
1726 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001727 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001728
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001729 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001730 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001731 }
1732 return FAIL;
1733}
1734
1735/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001736 * Add the last line of the scrollback buffer to the buffer in the window.
1737 */
1738 static void
1739add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1740{
1741 buf_T *buf = term->tl_buffer;
1742 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1743 linenr_T lnum = buf->b_ml.ml_line_count;
1744
Bram Moolenaar4f974752019-02-17 17:44:42 +01001745#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001746 if (!enc_utf8 && enc_codepage > 0)
1747 {
1748 WCHAR *ret = NULL;
1749 int length = 0;
1750
1751 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1752 &ret, &length);
1753 if (ret != NULL)
1754 {
1755 WideCharToMultiByte_alloc(enc_codepage, 0,
1756 ret, length, (char **)&text, &len, 0, 0);
1757 vim_free(ret);
1758 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1759 vim_free(text);
1760 }
1761 }
1762 else
1763#endif
1764 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1765 if (empty)
1766 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001767 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001768 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001769 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001770 curbuf = curwin->w_buffer;
1771 }
1772}
1773
1774 static void
1775cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1776{
1777 attr->width = cell->width;
1778 attr->attrs = cell->attrs;
1779 attr->fg = cell->fg;
1780 attr->bg = cell->bg;
1781}
1782
1783 static int
1784equal_celattr(cellattr_T *a, cellattr_T *b)
1785{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001786 // We only compare the RGB colors, ignoring the ANSI index and type.
1787 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001788 return a->fg.red == b->fg.red
1789 && a->fg.green == b->fg.green
1790 && a->fg.blue == b->fg.blue
1791 && a->bg.red == b->bg.red
1792 && a->bg.green == b->bg.green
1793 && a->bg.blue == b->bg.blue;
1794}
1795
Bram Moolenaard96ff162018-02-18 22:13:29 +01001796/*
1797 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1798 * line at this position. Otherwise at the end.
1799 */
1800 static int
1801add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1802{
1803 if (ga_grow(&term->tl_scrollback, 1) == OK)
1804 {
1805 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1806 + term->tl_scrollback.ga_len;
1807
1808 if (lnum > 0)
1809 {
1810 int i;
1811
1812 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1813 {
1814 *line = *(line - 1);
1815 --line;
1816 }
1817 }
1818 line->sb_cols = 0;
1819 line->sb_cells = NULL;
1820 line->sb_fill_attr = *fill_attr;
1821 ++term->tl_scrollback.ga_len;
1822 return OK;
1823 }
1824 return FALSE;
1825}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001826
1827/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001828 * Remove the terminal contents from the scrollback and the buffer.
1829 * Used before adding a new scrollback line or updating the buffer for lines
1830 * displayed in the terminal.
1831 */
1832 static void
1833cleanup_scrollback(term_T *term)
1834{
1835 sb_line_T *line;
1836 garray_T *gap;
1837
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001838 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001839 gap = &term->tl_scrollback;
1840 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1841 && gap->ga_len > 0)
1842 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001843 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001844 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1845 vim_free(line->sb_cells);
1846 --gap->ga_len;
1847 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001848 curbuf = curwin->w_buffer;
1849 if (curbuf == term->tl_buffer)
1850 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001851}
1852
1853/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001854 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001855 */
1856 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001857update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001858{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001859 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001860 int len;
1861 int lines_skipped = 0;
1862 VTermPos pos;
1863 VTermScreenCell cell;
1864 cellattr_T fill_attr, new_fill_attr;
1865 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001866
1867 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1868 "Adding terminal window snapshot to buffer");
1869
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001870 // First remove the lines that were appended before, they might be
1871 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001872 cleanup_scrollback(term);
1873
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001874 screen = vterm_obtain_screen(term->tl_vterm);
1875 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001876 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1877 {
1878 len = 0;
1879 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1880 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1881 && cell.chars[0] != NUL)
1882 {
1883 len = pos.col + 1;
1884 new_fill_attr = term->tl_default_color;
1885 }
1886 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001887 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001888 cell2cellattr(&cell, &new_fill_attr);
1889
1890 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1891 ++lines_skipped;
1892 else
1893 {
1894 while (lines_skipped > 0)
1895 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001896 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001897 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001898 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001899 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001900 }
1901
1902 if (len == 0)
1903 p = NULL;
1904 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001905 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001906 if ((p != NULL || len == 0)
1907 && ga_grow(&term->tl_scrollback, 1) == OK)
1908 {
1909 garray_T ga;
1910 int width;
1911 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1912 + term->tl_scrollback.ga_len;
1913
1914 ga_init2(&ga, 1, 100);
1915 for (pos.col = 0; pos.col < len; pos.col += width)
1916 {
1917 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1918 {
1919 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001920 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001921 if (ga_grow(&ga, 1) == OK)
1922 ga.ga_len += utf_char2bytes(' ',
1923 (char_u *)ga.ga_data + ga.ga_len);
1924 }
1925 else
1926 {
1927 width = cell.width;
1928
1929 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001930 if (width == 2)
1931 // second cell of double-width character has the
1932 // same attributes.
1933 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001934
Bram Moolenaara79fd562018-12-20 20:47:32 +01001935 // Each character can be up to 6 bytes.
1936 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001937 {
1938 int i;
1939 int c;
1940
1941 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1942 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1943 (char_u *)ga.ga_data + ga.ga_len);
1944 }
1945 }
1946 }
1947 line->sb_cols = len;
1948 line->sb_cells = p;
1949 line->sb_fill_attr = new_fill_attr;
1950 fill_attr = new_fill_attr;
1951 ++term->tl_scrollback.ga_len;
1952
1953 if (ga_grow(&ga, 1) == FAIL)
1954 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1955 else
1956 {
1957 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1958 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1959 }
1960 ga_clear(&ga);
1961 }
1962 else
1963 vim_free(p);
1964 }
1965 }
1966
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001967 // Add trailing empty lines.
1968 for (pos.row = term->tl_scrollback.ga_len;
1969 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1970 ++pos.row)
1971 {
1972 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1973 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1974 }
1975
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001976 term->tl_dirty_snapshot = FALSE;
1977#ifdef FEAT_TIMERS
1978 term->tl_timer_set = FALSE;
1979#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001980}
1981
1982/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001983 * Loop over all windows in the current tab, and also curwin, which is not
1984 * encountered when using a terminal in a popup window.
1985 * Return TRUE if "*wp" was set to the next window.
1986 */
1987 static int
1988for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1989{
1990 if (*wp == NULL)
1991 *wp = firstwin;
1992 else if ((*wp)->w_next != NULL)
1993 *wp = (*wp)->w_next;
1994 else if (!*did_curwin)
1995 *wp = curwin;
1996 else
1997 return FALSE;
1998 if (*wp == curwin)
1999 *did_curwin = TRUE;
2000 return TRUE;
2001}
2002
2003/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002004 * If needed, add the current lines of the terminal to scrollback and to the
2005 * buffer. Called after the job has ended and when switching to
2006 * Terminal-Normal mode.
2007 * When "redraw" is TRUE redraw the windows that show the terminal.
2008 */
2009 static void
2010may_move_terminal_to_buffer(term_T *term, int redraw)
2011{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002012 if (term->tl_vterm == NULL)
2013 return;
2014
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002015 // Update the snapshot only if something changes or the buffer does not
2016 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002017 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
2018 <= term->tl_scrollback_scrolled)
2019 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002020
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002021 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002022 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2023 &term->tl_default_color.fg, &term->tl_default_color.bg);
2024
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002025 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002026 {
2027 win_T *wp = NULL;
2028 int did_curwin = FALSE;
2029
2030 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002031 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002032 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002033 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002034 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
2035 wp->w_cursor.col = 0;
2036 wp->w_valid = 0;
2037 if (wp->w_cursor.lnum >= wp->w_height)
2038 {
2039 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002040
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002041 if (wp->w_topline < min_topline)
2042 wp->w_topline = min_topline;
2043 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002044 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002045 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002046 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002047 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002048}
2049
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002050#if defined(FEAT_TIMERS) || defined(PROTO)
2051/*
2052 * Check if any terminal timer expired. If so, copy text from the terminal to
2053 * the buffer.
2054 * Return the time until the next timer will expire.
2055 */
2056 int
2057term_check_timers(int next_due_arg, proftime_T *now)
2058{
2059 term_T *term;
2060 int next_due = next_due_arg;
2061
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002062 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002063 {
2064 if (term->tl_timer_set && !term->tl_normal_mode)
2065 {
2066 long this_due = proftime_time_left(&term->tl_timer_due, now);
2067
2068 if (this_due <= 1)
2069 {
2070 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002071 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002072 }
2073 else if (next_due == -1 || next_due > this_due)
2074 next_due = this_due;
2075 }
2076 }
2077
2078 return next_due;
2079}
2080#endif
2081
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002082/*
2083 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
2084 * otherwise end it.
2085 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002086 static void
2087set_terminal_mode(term_T *term, int normal_mode)
2088{
2089 term->tl_normal_mode = normal_mode;
LemonBoy2bf52dd2022-04-09 18:17:34 +01002090 may_trigger_modechanged();
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002091 if (!normal_mode)
2092 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002093 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002094 if (term->tl_buffer == curbuf)
2095 maketitle();
2096}
2097
2098/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002099 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002100 * Move the vterm contents into the scrollback buffer and free the vterm.
2101 */
2102 static void
2103cleanup_vterm(term_T *term)
2104{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002105 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002106 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002107 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002108 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002109}
2110
2111/*
2112 * Switch from Terminal-Job mode to Terminal-Normal mode.
2113 * Suspends updating the terminal window.
2114 */
2115 static void
2116term_enter_normal_mode(void)
2117{
2118 term_T *term = curbuf->b_term;
2119
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002120 set_terminal_mode(term, TRUE);
2121
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002122 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002123 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002124
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002125 // Move the window cursor to the position of the cursor in the
2126 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002127 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2128 + term->tl_cursor_pos.row + 1;
2129 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002130 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2131 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002132 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002133
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002134 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002135 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2136}
2137
2138/*
2139 * Returns TRUE if the current window contains a terminal and we are in
2140 * Terminal-Normal mode.
2141 */
2142 int
2143term_in_normal_mode(void)
2144{
2145 term_T *term = curbuf->b_term;
2146
2147 return term != NULL && term->tl_normal_mode;
2148}
2149
2150/*
2151 * Switch from Terminal-Normal mode to Terminal-Job mode.
2152 * Restores updating the terminal window.
2153 */
2154 void
2155term_enter_job_mode()
2156{
2157 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002158
2159 set_terminal_mode(term, FALSE);
2160
2161 if (term->tl_channel_closed)
2162 cleanup_vterm(term);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002163 redraw_buf_and_status_later(curbuf, UPD_NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002164#ifdef FEAT_PROP_POPUP
2165 if (WIN_IS_POPUP(curwin))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002166 redraw_later(UPD_NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002167#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002168}
2169
2170/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002171 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002172 * Note: while waiting a terminal may be closed and freed if the channel is
Bram Moolenaar829c8e82021-12-14 08:41:38 +00002173 * closed and ++close was used. This may even happen before we get here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002174 */
2175 static int
2176term_vgetc()
2177{
2178 int c;
2179 int save_State = State;
Bram Moolenaar829c8e82021-12-14 08:41:38 +00002180 int modify_other_keys = curbuf->b_term->tl_vterm == NULL ? FALSE
2181 : vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002182
Bram Moolenaar24959102022-05-07 20:01:16 +01002183 State = MODE_TERMINAL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002184 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002185#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002186 ctrl_break_was_pressed = FALSE;
2187#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002188 if (modify_other_keys)
2189 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002190 c = vgetc();
2191 got_int = FALSE;
2192 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002193 if (modify_other_keys)
2194 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002195 return c;
2196}
2197
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002198static int mouse_was_outside = FALSE;
2199
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002200/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002201 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002202 * Return FAIL when the key needs to be handled in Normal mode.
2203 * Return OK when the key was dropped or sent to the terminal.
2204 */
2205 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002206send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002207{
2208 char msg[KEY_BUF_LEN];
2209 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002210 int dragging_outside = FALSE;
2211
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002212 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002213 switch (c)
2214 {
2215 case NUL:
2216 case K_ZERO:
2217 if (typed)
2218 stuffcharReadbuff(c);
2219 return FAIL;
2220
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002221 case K_TABLINE:
2222 stuffcharReadbuff(c);
2223 return FAIL;
2224
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002225 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002226 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002227 return FAIL;
2228
2229 case K_LEFTDRAG:
2230 case K_MIDDLEDRAG:
2231 case K_RIGHTDRAG:
2232 case K_X1DRAG:
2233 case K_X2DRAG:
2234 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002235 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002236 case K_LEFTMOUSE:
2237 case K_LEFTMOUSE_NM:
2238 case K_LEFTRELEASE:
2239 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002240 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002241 case K_MIDDLEMOUSE:
2242 case K_MIDDLERELEASE:
2243 case K_RIGHTMOUSE:
2244 case K_RIGHTRELEASE:
2245 case K_X1MOUSE:
2246 case K_X1RELEASE:
2247 case K_X2MOUSE:
2248 case K_X2RELEASE:
2249
2250 case K_MOUSEUP:
2251 case K_MOUSEDOWN:
2252 case K_MOUSELEFT:
2253 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002254 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002255 int row = mouse_row;
2256 int col = mouse_col;
2257
2258#ifdef FEAT_PROP_POPUP
2259 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002260 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002261 row -= popup_top_extra(curwin);
2262 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002263 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002264#endif
2265 if (row < W_WINROW(curwin)
2266 || row >= (W_WINROW(curwin) + curwin->w_height)
2267 || col < curwin->w_wincol
2268 || col >= W_ENDCOL(curwin)
2269 || dragging_outside)
2270 {
2271 // click or scroll outside the current window or on status
2272 // line or vertical separator
2273 if (typed)
2274 {
2275 stuffcharReadbuff(c);
2276 mouse_was_outside = TRUE;
2277 }
2278 return FAIL;
2279 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002280 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002281 break;
2282
2283 case K_COMMAND:
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002284 case K_SCRIPT_COMMAND:
2285 return do_cmdkey_command(c, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002286 }
2287 if (typed)
2288 mouse_was_outside = FALSE;
2289
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002290 // Convert the typed key to a sequence of bytes for the job.
2291 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002292 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002293 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002294 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2295 (char_u *)msg, (int)len, NULL);
2296
2297 return OK;
2298}
2299
2300 static void
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002301position_cursor(win_T *wp, VTermPos *pos)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002302{
2303 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2304 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002305#ifdef FEAT_PROP_POPUP
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002306 if (popup_is_popup(wp))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002307 {
Bram Moolenaarf5452692020-11-28 21:56:06 +01002308 wp->w_wrow += popup_top_extra(wp);
2309 wp->w_wcol += popup_left_extra(wp);
Bram Moolenaar6a076442020-11-15 20:32:58 +01002310 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002311 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01002312 else
2313 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002314#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002315 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2316}
2317
2318/*
2319 * Handle CTRL-W "": send register contents to the job.
2320 */
2321 static void
2322term_paste_register(int prev_c UNUSED)
2323{
2324 int c;
2325 list_T *l;
2326 listitem_T *item;
2327 long reglen = 0;
2328 int type;
2329
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002330 if (add_to_showcmd(prev_c))
2331 if (add_to_showcmd('"'))
2332 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +01002333
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002334 c = term_vgetc();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002335 clear_showcmd();
Martin Tournoijba43e762022-10-13 22:12:15 +01002336
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002337 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002338 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002339 return;
2340
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002341 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002342 if (c == '=' && get_expr_register() != '=')
2343 return;
2344 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002345 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002346 return;
2347
2348 l = (list_T *)get_reg_contents(c, GREG_LIST);
2349 if (l != NULL)
2350 {
2351 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002352 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002353 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002354 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002355#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002356 char_u *tmp = s;
2357
2358 if (!enc_utf8 && enc_codepage > 0)
2359 {
2360 WCHAR *ret = NULL;
2361 int length = 0;
2362
2363 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2364 (int)STRLEN(s), &ret, &length);
2365 if (ret != NULL)
2366 {
2367 WideCharToMultiByte_alloc(CP_UTF8, 0,
2368 ret, length, (char **)&s, &length, 0, 0);
2369 vim_free(ret);
2370 }
2371 }
2372#endif
2373 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2374 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002375#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002376 if (tmp != s)
2377 vim_free(s);
2378#endif
2379
2380 if (item->li_next != NULL || type == MLINE)
2381 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2382 (char_u *)"\r", 1, NULL);
2383 }
2384 list_free(l);
2385 }
2386}
2387
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002388/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002389 * Return TRUE when waiting for a character in the terminal, the cursor of the
2390 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002391 */
2392 int
2393terminal_is_active()
2394{
2395 return in_terminal_loop != NULL;
2396}
2397
Bram Moolenaar83d47902020-03-26 20:34:00 +01002398/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002399 * Return the highight group ID for the terminal and the window.
Bram Moolenaar83d47902020-03-26 20:34:00 +01002400 */
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002401 static int
2402term_get_highlight_id(term_T *term, win_T *wp)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002403{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002404 char_u *name;
2405
2406 if (wp != NULL && *wp->w_p_wcr != NUL)
2407 name = wp->w_p_wcr;
2408 else if (term->tl_highlight_name != NULL)
2409 name = term->tl_highlight_name;
2410 else
2411 name = (char_u*)"Terminal";
2412
2413 return syn_name2id(name);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002414}
2415
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002416#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002417 cursorentry_T *
2418term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2419{
2420 term_T *term = in_terminal_loop;
2421 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002422 int id;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002423 guicolor_T term_fg = INVALCOLOR;
2424 guicolor_T term_bg = INVALCOLOR;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002425
Bram Moolenaara80faa82020-04-12 19:37:17 +02002426 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002427 entry.shape = entry.mshape =
2428 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2429 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2430 SHAPE_BLOCK;
2431 entry.percentage = 20;
2432 if (term->tl_cursor_blink)
2433 {
2434 entry.blinkwait = 700;
2435 entry.blinkon = 400;
2436 entry.blinkoff = 250;
2437 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002438
Bram Moolenaar83d47902020-03-26 20:34:00 +01002439 // The highlight group overrules the defaults.
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002440 id = term_get_highlight_id(term, curwin);
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002441 if (id != 0)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002442 syn_id2colors(id, &term_fg, &term_bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002443 if (term_bg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002444 *fg = term_bg;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002445 else
2446 *fg = gui.back_pixel;
2447
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002448 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002449 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002450 if (term_fg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002451 *bg = term_fg;
2452 else
2453 *bg = gui.norm_pixel;
2454 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002455 else
2456 *bg = color_name2handle(term->tl_cursor_color);
2457 entry.name = "n";
2458 entry.used_for = SHAPE_CURSOR;
2459
2460 return &entry;
2461}
2462#endif
2463
Bram Moolenaard317b382018-02-08 22:33:31 +01002464 static void
2465may_output_cursor_props(void)
2466{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002467 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002468 || last_set_cursor_shape != desired_cursor_shape
2469 || last_set_cursor_blink != desired_cursor_blink)
2470 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002471 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002472 last_set_cursor_shape = desired_cursor_shape;
2473 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002474 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002475 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002476 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002477 ui_cursor_shape_forced(TRUE);
2478 else
2479 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2480 }
2481}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002482
Bram Moolenaard317b382018-02-08 22:33:31 +01002483/*
2484 * Set the cursor color and shape, if not last set to these.
2485 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002486 static void
2487may_set_cursor_props(term_T *term)
2488{
2489#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002490 // For the GUI the cursor properties are obtained with
2491 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002492 if (gui.in_use)
2493 return;
2494#endif
2495 if (in_terminal_loop == term)
2496 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002497 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002498 desired_cursor_shape = term->tl_cursor_shape;
2499 desired_cursor_blink = term->tl_cursor_blink;
2500 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002501 }
2502}
2503
Bram Moolenaard317b382018-02-08 22:33:31 +01002504/*
2505 * Reset the desired cursor properties and restore them when needed.
2506 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002507 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002508prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002509{
2510#ifdef FEAT_GUI
2511 if (gui.in_use)
2512 return;
2513#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002514 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002515 desired_cursor_shape = -1;
2516 desired_cursor_blink = -1;
2517 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002518}
2519
2520/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002521 * Returns TRUE if the current window contains a terminal and we are sending
2522 * keys to the job.
2523 * If "check_job_status" is TRUE update the job status.
2524 */
2525 static int
2526term_use_loop_check(int check_job_status)
2527{
2528 term_T *term = curbuf->b_term;
2529
2530 return term != NULL
2531 && !term->tl_normal_mode
2532 && term->tl_vterm != NULL
2533 && term_job_running_check(term, check_job_status);
2534}
2535
2536/*
2537 * Returns TRUE if the current window contains a terminal and we are sending
2538 * keys to the job.
2539 */
2540 int
2541term_use_loop(void)
2542{
2543 return term_use_loop_check(FALSE);
2544}
2545
2546/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002547 * Called when entering a window with the mouse. If this is a terminal window
2548 * we may want to change state.
2549 */
2550 void
2551term_win_entered()
2552{
2553 term_T *term = curbuf->b_term;
2554
2555 if (term != NULL)
2556 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002557 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002558 {
2559 reset_VIsual_and_resel();
Bram Moolenaar24959102022-05-07 20:01:16 +01002560 if (State & MODE_INSERT)
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002561 stop_insert_mode = TRUE;
2562 }
2563 mouse_was_outside = FALSE;
2564 enter_mouse_col = mouse_col;
2565 enter_mouse_row = mouse_row;
2566 }
2567}
2568
Bram Moolenaara48d4e42021-12-08 22:13:38 +00002569 void
2570term_focus_change(int in_focus)
2571{
2572 term_T *term = curbuf->b_term;
2573
2574 if (term != NULL && term->tl_vterm != NULL)
2575 {
2576 VTermState *state = vterm_obtain_state(term->tl_vterm);
2577
2578 if (in_focus)
2579 vterm_state_focus_in(state);
2580 else
2581 vterm_state_focus_out(state);
2582 term_forward_output(term);
2583 }
2584}
2585
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002586/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002587 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2588 * Return the Ctrl-key value in that case.
2589 */
2590 static int
2591raw_c_to_ctrl(int c)
2592{
2593 if ((mod_mask & MOD_MASK_CTRL)
2594 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2595 return c & 0x1f;
2596 return c;
2597}
2598
2599/*
2600 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2601 * May set "mod_mask".
2602 */
2603 static int
2604ctrl_to_raw_c(int c)
2605{
2606 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2607 {
2608 mod_mask |= MOD_MASK_CTRL;
2609 return c + '@';
2610 }
2611 return c;
2612}
2613
2614/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002615 * Wait for input and send it to the job.
2616 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2617 * when there is no more typahead.
2618 * Return when the start of a CTRL-W command is typed or anything else that
2619 * should be handled as a Normal mode command.
2620 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2621 * the terminal was closed.
2622 */
2623 int
2624terminal_loop(int blocking)
2625{
2626 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002627 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002628 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002629 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002630#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002631 int tty_fd = curbuf->b_term->tl_job->jv_channel
2632 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002633#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002634 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002635
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002636 // Remember the terminal we are sending keys to. However, the terminal
2637 // might be closed while waiting for a character, e.g. typing "exit" in a
2638 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2639 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002640 in_terminal_loop = curbuf->b_term;
2641
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002642 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002643 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002644 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002645 if (termwinkey == Ctrl_W)
2646 termwinkey = 0;
2647 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002648 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002649 may_set_cursor_props(curbuf->b_term);
2650
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002651 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002652 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002653#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002654 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002655#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002656 // TODO: skip screen update when handling a sequence of keys.
2657 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002658 while (must_redraw != 0)
2659 if (update_screen(0) == FAIL)
2660 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002661 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002662 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002663 break;
2664
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002665 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002666 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002667
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002668 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002669 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002670 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002671 // Job finished while waiting for a character. Push back the
2672 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002673 if (raw_c != K_IGNORE)
2674 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002675 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002676 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002677 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002678 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002679 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002680
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002681#ifdef UNIX
2682 /*
2683 * The shell or another program may change the tty settings. Getting
2684 * them for every typed character is a bit of overhead, but it's needed
2685 * for the first character typed, e.g. when Vim starts in a shell.
2686 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002687 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002688 {
2689 ttyinfo_T info;
2690
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002691 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002692 if (get_tty_info(tty_fd, &info) == OK)
2693 term_backspace_char = info.backspace;
2694 }
2695#endif
2696
Bram Moolenaar4f974752019-02-17 17:44:42 +01002697#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002698 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2699 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002700 if (ctrl_break_was_pressed)
2701 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2702#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002703 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2704 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002705 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002706#ifdef FEAT_GUI
2707 && !curbuf->b_term->tl_system
2708#endif
2709 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002710 {
2711 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002712 int prev_raw_c = raw_c;
2713 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002714
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002715 if (add_to_showcmd(c))
2716 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +01002717
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002718 raw_c = term_vgetc();
2719 c = raw_c_to_ctrl(raw_c);
2720
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002721 clear_showcmd();
Martin Tournoijba43e762022-10-13 22:12:15 +01002722
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002723 if (!term_use_loop_check(TRUE)
2724 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002725 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002726 break;
2727
2728 if (prev_c == Ctrl_BSL)
2729 {
2730 if (c == Ctrl_N)
2731 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002732 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002733 term_enter_normal_mode();
2734 ret = FAIL;
2735 goto theend;
2736 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002737 // Send both keys to the terminal, first one here, second one
2738 // below.
2739 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2740 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002741 }
2742 else if (c == Ctrl_C)
2743 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002744 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002745 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2746 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002747 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002748 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002749 // "CTRL-W .": send CTRL-W to the job
2750 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002751 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002752 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002753 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002754 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002755 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002756 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002757 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002758 else if (c == 'N')
2759 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002760 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002761 term_enter_normal_mode();
2762 ret = FAIL;
2763 goto theend;
2764 }
2765 else if (c == '"')
2766 {
2767 term_paste_register(prev_c);
2768 continue;
2769 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002770 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002771 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002772 // space for CTRL-W, modifier, multi-byte char and NUL
2773 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002774
2775 // Put the command into the typeahead buffer, when using the
2776 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2777 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002778 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002779 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002780 ret = OK;
2781 goto theend;
2782 }
2783 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002784# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002785 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002786 {
2787 WCHAR wc;
2788 char_u mb[3];
2789
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002790 mb[0] = (unsigned)raw_c >> 8;
2791 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002792 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002793 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002794 }
2795# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002796 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002797 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002798 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002799 // We are sure to come back here, don't reset the cursor color
2800 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002801 restore_cursor = FALSE;
2802
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002803 ret = OK;
2804 goto theend;
2805 }
2806 }
2807 ret = FAIL;
2808
2809theend:
2810 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002811 if (restore_cursor)
2812 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002813
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002814 // Move a snapshot of the screen contents to the buffer, so that completion
2815 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002816 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2817 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002818
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002819 return ret;
2820}
2821
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002822 static void
2823may_toggle_cursor(term_T *term)
2824{
2825 if (in_terminal_loop == term)
2826 {
2827 if (term->tl_cursor_visible)
2828 cursor_on();
2829 else
2830 cursor_off();
2831 }
2832}
2833
2834/*
2835 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002836 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002837 */
2838 static int
2839color2index(VTermColor *color, int fg, int *boldp)
2840{
2841 int red = color->red;
2842 int blue = color->blue;
2843 int green = color->green;
2844
LemonBoyb2b3acb2022-05-20 10:10:34 +01002845 *boldp = FALSE;
2846
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002847 if (VTERM_COLOR_IS_INVALID(color))
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002848 return 0;
LemonBoyb2b3acb2022-05-20 10:10:34 +01002849
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002850 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002851 {
LemonBoyb2b3acb2022-05-20 10:10:34 +01002852 // Use the color as-is if possible, give up otherwise.
2853 if (color->index < t_colors)
2854 return color->index + 1;
2855 // 8-color terminals can actually display twice as many colors by
2856 // setting the high-intensity/bold bit.
2857 else if (t_colors == 8 && fg && color->index < 16)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002858 {
LemonBoyb2b3acb2022-05-20 10:10:34 +01002859 *boldp = TRUE;
2860 return (color->index & 7) + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002861 }
LemonBoyb2b3acb2022-05-20 10:10:34 +01002862 return 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002863 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002864
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002865 if (t_colors >= 256)
2866 {
2867 if (red == blue && red == green)
2868 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002869 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002870 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002871 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2872 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2873 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002874 int i;
2875
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002876 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002877 return 17; // 00/00/00
2878 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002879 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002880 for (i = 0; i < 23; ++i)
2881 if (red < cutoff[i])
2882 return i + 233;
2883 return 256;
2884 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002885 {
2886 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2887 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002888
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002889 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002890 for (ri = 0; ri < 5; ++ri)
2891 if (red < cutoff[ri])
2892 break;
2893 for (gi = 0; gi < 5; ++gi)
2894 if (green < cutoff[gi])
2895 break;
2896 for (bi = 0; bi < 5; ++bi)
2897 if (blue < cutoff[bi])
2898 break;
2899 return 17 + ri * 36 + gi * 6 + bi;
2900 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002901 }
2902 return 0;
2903}
2904
2905/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002906 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002907 */
2908 static int
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002909vtermAttr2hl(VTermScreenCellAttrs *cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002910{
2911 int attr = 0;
2912
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002913 if (cellattrs->bold)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002914 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002915 if (cellattrs->underline)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002916 attr |= HL_UNDERLINE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002917 if (cellattrs->italic)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002918 attr |= HL_ITALIC;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002919 if (cellattrs->strike)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002920 attr |= HL_STRIKETHROUGH;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002921 if (cellattrs->reverse)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002922 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002923 return attr;
2924}
2925
2926/*
2927 * Store Vterm attributes in "cell" from highlight flags.
2928 */
2929 static void
2930hl2vtermAttr(int attr, cellattr_T *cell)
2931{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002932 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002933 if (attr & HL_BOLD)
2934 cell->attrs.bold = 1;
2935 if (attr & HL_UNDERLINE)
2936 cell->attrs.underline = 1;
2937 if (attr & HL_ITALIC)
2938 cell->attrs.italic = 1;
2939 if (attr & HL_STRIKETHROUGH)
2940 cell->attrs.strike = 1;
2941 if (attr & HL_INVERSE)
2942 cell->attrs.reverse = 1;
2943}
2944
2945/*
2946 * Convert the attributes of a vterm cell into an attribute index.
2947 */
2948 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002949cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002950 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002951 win_T *wp,
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002952 VTermScreenCellAttrs *cellattrs,
2953 VTermColor *cellfg,
2954 VTermColor *cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002955{
2956 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002957 VTermColor *fg = cellfg;
2958 VTermColor *bg = cellbg;
2959 int is_default_fg = VTERM_COLOR_IS_DEFAULT_FG(fg);
2960 int is_default_bg = VTERM_COLOR_IS_DEFAULT_BG(bg);
2961
2962 if (is_default_fg || is_default_bg)
2963 {
2964 if (wp != NULL && *wp->w_p_wcr != NUL)
2965 {
2966 if (is_default_fg)
2967 fg = &wp->w_term_wincolor.fg;
2968 if (is_default_bg)
2969 bg = &wp->w_term_wincolor.bg;
2970 }
2971 else
2972 {
2973 if (is_default_fg)
2974 fg = &term->tl_default_color.fg;
2975 if (is_default_bg)
2976 bg = &term->tl_default_color.bg;
2977 }
2978 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002979
2980#ifdef FEAT_GUI
2981 if (gui.in_use)
2982 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002983 guicolor_T guifg = gui_mch_get_rgb_color(fg->red, fg->green, fg->blue);
2984 guicolor_T guibg = gui_mch_get_rgb_color(bg->red, bg->green, bg->blue);
2985 return get_gui_attr_idx(attr, guifg, guibg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002986 }
2987 else
2988#endif
2989#ifdef FEAT_TERMGUICOLORS
2990 if (p_tgc)
2991 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002992 guicolor_T tgcfg = VTERM_COLOR_IS_INVALID(fg)
2993 ? INVALCOLOR
2994 : gui_get_rgb_color_cmn(fg->red, fg->green, fg->blue);
2995 guicolor_T tgcbg = VTERM_COLOR_IS_INVALID(bg)
2996 ? INVALCOLOR
2997 : gui_get_rgb_color_cmn(bg->red, bg->green, bg->blue);
2998 return get_tgc_attr_idx(attr, tgcfg, tgcbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002999 }
3000 else
3001#endif
3002 {
3003 int bold = MAYBE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003004 int ctermfg = color2index(fg, TRUE, &bold);
3005 int ctermbg = color2index(bg, FALSE, &bold);
Bram Moolenaar76bb7192017-11-30 22:07:07 +01003006
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003007 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003008 if (bold == TRUE)
3009 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003010
3011 return get_cterm_attr_idx(attr, ctermfg, ctermbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003012 }
3013 return 0;
3014}
3015
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003016 static void
3017set_dirty_snapshot(term_T *term)
3018{
3019 term->tl_dirty_snapshot = TRUE;
3020#ifdef FEAT_TIMERS
3021 if (!term->tl_normal_mode)
3022 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003023 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003024 profile_setlimit(100L, &term->tl_timer_due);
3025 term->tl_timer_set = TRUE;
3026 }
3027#endif
3028}
3029
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003030 static int
3031handle_damage(VTermRect rect, void *user)
3032{
3033 term_T *term = (term_T *)user;
3034
3035 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
3036 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003037 set_dirty_snapshot(term);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003038 redraw_buf_later(term->tl_buffer, UPD_SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003039 return 1;
3040}
3041
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003042 static void
3043term_scroll_up(term_T *term, int start_row, int count)
3044{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003045 win_T *wp = NULL;
3046 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003047 VTermColor fg, bg;
3048 VTermScreenCellAttrs attr;
3049 int clear_attr;
3050
Bram Moolenaara80faa82020-04-12 19:37:17 +02003051 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003052
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003053 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003054 {
3055 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003056 {
3057 // Set the color to clear lines with.
3058 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
3059 &fg, &bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003060 clear_attr = cell2attr(term, wp, &attr, &fg, &bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003061 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003062 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003063 }
3064}
3065
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003066 static int
3067handle_moverect(VTermRect dest, VTermRect src, void *user)
3068{
3069 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003070 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003071
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003072 // Scrolling up is done much more efficiently by deleting lines instead of
3073 // redrawing the text. But avoid doing this multiple times, postpone until
3074 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003075 if (dest.start_col == src.start_col
3076 && dest.end_col == src.end_col
3077 && dest.start_row < src.start_row)
3078 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003079 if (dest.start_row == 0)
3080 term->tl_postponed_scroll += count;
3081 else
3082 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003083 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003084
3085 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3086 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003087 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003088
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003089 // Note sure if the scrolling will work correctly, let's do a complete
3090 // redraw later.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003091 redraw_buf_later(term->tl_buffer, UPD_NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003092 return 1;
3093}
3094
3095 static int
3096handle_movecursor(
3097 VTermPos pos,
3098 VTermPos oldpos UNUSED,
3099 int visible,
3100 void *user)
3101{
3102 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003103 win_T *wp = NULL;
3104 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003105
3106 term->tl_cursor_pos = pos;
3107 term->tl_cursor_visible = visible;
3108
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003109 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003110 {
3111 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003112 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003113 }
3114 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003115 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003116
3117 return 1;
3118}
3119
3120 static int
3121handle_settermprop(
3122 VTermProp prop,
3123 VTermValue *value,
3124 void *user)
3125{
3126 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003127 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003128
3129 switch (prop)
3130 {
3131 case VTERM_PROP_TITLE:
ichizokae1bd872022-01-20 14:57:29 +00003132 if (disable_vterm_title_for_testing)
3133 break;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003134 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003135 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003136 if (strval == NULL)
3137 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003138 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003139 // a blank title isn't useful, make it empty, so that "running" is
3140 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003141 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003142 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003143 // Same as blank
3144 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003145 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003146 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3147 term->tl_title = NULL;
3148 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003149 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003150 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003151#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003152 else if (!enc_utf8 && enc_codepage > 0)
3153 {
3154 WCHAR *ret = NULL;
3155 int length = 0;
3156
3157 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003158 (char*)value->string.str,
3159 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003160 if (ret != NULL)
3161 {
3162 WideCharToMultiByte_alloc(enc_codepage, 0,
3163 ret, length, (char**)&term->tl_title,
3164 &length, 0, 0);
3165 vim_free(ret);
3166 }
3167 }
3168#endif
3169 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003170 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003171 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003172 strval = NULL;
3173 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003174 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003175 if (term == curbuf->b_term)
LemonBoy327e6dd2022-06-04 19:57:59 +01003176 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003177 maketitle();
LemonBoy327e6dd2022-06-04 19:57:59 +01003178 curwin->w_redr_status = TRUE;
3179 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003180 break;
3181
3182 case VTERM_PROP_CURSORVISIBLE:
3183 term->tl_cursor_visible = value->boolean;
3184 may_toggle_cursor(term);
3185 out_flush();
3186 break;
3187
3188 case VTERM_PROP_CURSORBLINK:
3189 term->tl_cursor_blink = value->boolean;
3190 may_set_cursor_props(term);
3191 break;
3192
3193 case VTERM_PROP_CURSORSHAPE:
3194 term->tl_cursor_shape = value->number;
3195 may_set_cursor_props(term);
3196 break;
3197
3198 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003199 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003200 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003201 if (strval == NULL)
3202 break;
3203 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003204 may_set_cursor_props(term);
3205 break;
3206
3207 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003208 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003209 term->tl_using_altscreen = value->boolean;
3210 break;
3211
3212 default:
3213 break;
3214 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003215 vim_free(strval);
3216
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003217 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003218 return 1;
3219}
3220
3221/*
3222 * The job running in the terminal resized the terminal.
3223 */
3224 static int
3225handle_resize(int rows, int cols, void *user)
3226{
3227 term_T *term = (term_T *)user;
3228 win_T *wp;
3229
3230 term->tl_rows = rows;
3231 term->tl_cols = cols;
3232 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003233 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003234 term->tl_vterm_size_changed = FALSE;
3235 else
3236 {
3237 FOR_ALL_WINDOWS(wp)
3238 {
3239 if (wp->w_buffer == term->tl_buffer)
3240 {
3241 win_setheight_win(rows, wp);
3242 win_setwidth_win(cols, wp);
3243 }
3244 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003245 redraw_buf_later(term->tl_buffer, UPD_NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003246 }
3247 return 1;
3248}
3249
3250/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003251 * If the number of lines that are stored goes over 'termscrollback' then
3252 * delete the first 10%.
3253 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3254 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003255 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003256 static void
3257limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003258{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003259 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003260 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003261 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003262 int i;
3263
3264 curbuf = term->tl_buffer;
3265 for (i = 0; i < todo; ++i)
3266 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003267 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3268 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003269 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003270 }
3271 curbuf = curwin->w_buffer;
3272
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003273 gap->ga_len -= todo;
3274 mch_memmove(gap->ga_data,
3275 (sb_line_T *)gap->ga_data + todo,
3276 sizeof(sb_line_T) * gap->ga_len);
3277 if (update_buffer)
3278 term->tl_scrollback_scrolled -= todo;
3279 }
3280}
3281
3282/*
3283 * Handle a line that is pushed off the top of the screen.
3284 */
3285 static int
3286handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3287{
3288 term_T *term = (term_T *)user;
3289 garray_T *gap;
3290 int update_buffer;
3291
3292 if (term->tl_normal_mode)
3293 {
3294 // In Terminal-Normal mode the user interacts with the buffer, thus we
3295 // must not change it. Postpone adding the scrollback lines.
3296 gap = &term->tl_scrollback_postponed;
3297 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003298 }
3299 else
3300 {
3301 // First remove the lines that were appended before, the pushed line
3302 // goes above it.
3303 cleanup_scrollback(term);
3304 gap = &term->tl_scrollback;
3305 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003306 }
3307
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003308 limit_scrollback(term, gap, update_buffer);
3309
3310 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003311 {
3312 cellattr_T *p = NULL;
3313 int len = 0;
3314 int i;
3315 int c;
3316 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003317 int text_len;
3318 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003319 sb_line_T *line;
3320 garray_T ga;
3321 cellattr_T fill_attr = term->tl_default_color;
3322
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003323 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003324 for (i = 0; i < cols; ++i)
3325 if (cells[i].chars[0] != 0)
3326 len = i + 1;
3327 else
3328 cell2cellattr(&cells[i], &fill_attr);
3329
3330 ga_init2(&ga, 1, 100);
3331 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003332 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003333 if (p != NULL)
3334 {
3335 for (col = 0; col < len; col += cells[col].width)
3336 {
3337 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3338 {
3339 ga.ga_len = 0;
3340 break;
3341 }
3342 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3343 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3344 (char_u *)ga.ga_data + ga.ga_len);
3345 cell2cellattr(&cells[col], &p[col]);
3346 }
3347 }
3348 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003349 {
3350 if (update_buffer)
3351 text = (char_u *)"";
3352 else
3353 text = vim_strsave((char_u *)"");
3354 text_len = 0;
3355 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003356 else
3357 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003358 text = ga.ga_data;
3359 text_len = ga.ga_len;
3360 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003361 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003362 if (update_buffer)
3363 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003364
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003365 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003366 line->sb_cols = len;
3367 line->sb_cells = p;
3368 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003369 if (update_buffer)
3370 {
3371 line->sb_text = NULL;
3372 ++term->tl_scrollback_scrolled;
3373 ga_clear(&ga); // free the text
3374 }
3375 else
3376 {
3377 line->sb_text = text;
3378 ga_init(&ga); // text is kept in tl_scrollback_postponed
3379 }
3380 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003381 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003382 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003383}
3384
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003385/*
3386 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3387 * received and stored in tl_scrollback_postponed.
3388 */
3389 static void
3390handle_postponed_scrollback(term_T *term)
3391{
3392 int i;
3393
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003394 if (term->tl_scrollback_postponed.ga_len == 0)
3395 return;
3396 ch_log(NULL, "Moving postponed scrollback to scrollback");
3397
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003398 // First remove the lines that were appended before, the pushed lines go
3399 // above it.
3400 cleanup_scrollback(term);
3401
3402 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3403 {
3404 char_u *text;
3405 sb_line_T *pp_line;
3406 sb_line_T *line;
3407
3408 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3409 break;
3410 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3411
3412 text = pp_line->sb_text;
3413 if (text == NULL)
3414 text = (char_u *)"";
3415 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3416 vim_free(pp_line->sb_text);
3417
3418 line = (sb_line_T *)term->tl_scrollback.ga_data
3419 + term->tl_scrollback.ga_len;
3420 line->sb_cols = pp_line->sb_cols;
3421 line->sb_cells = pp_line->sb_cells;
3422 line->sb_fill_attr = pp_line->sb_fill_attr;
3423 line->sb_text = NULL;
3424 ++term->tl_scrollback_scrolled;
3425 ++term->tl_scrollback.ga_len;
3426 }
3427
3428 ga_clear(&term->tl_scrollback_postponed);
3429 limit_scrollback(term, &term->tl_scrollback, TRUE);
3430}
3431
LemonBoy77771d32022-04-13 11:47:25 +01003432/*
3433 * Called when the terminal wants to ring the system bell.
3434 */
3435 static int
3436handle_bell(void *user UNUSED)
3437{
Bram Moolenaaraae97622022-04-13 14:28:07 +01003438 vim_beep(BO_TERM);
LemonBoy77771d32022-04-13 11:47:25 +01003439 return 0;
3440}
3441
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003442static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003443 handle_damage, // damage
3444 handle_moverect, // moverect
3445 handle_movecursor, // movecursor
3446 handle_settermprop, // settermprop
LemonBoy77771d32022-04-13 11:47:25 +01003447 handle_bell, // bell
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003448 handle_resize, // resize
3449 handle_pushline, // sb_pushline
3450 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003451};
3452
3453/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003454 * Do the work after the channel of a terminal was closed.
3455 * Must be called only when updating_screen is FALSE.
3456 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3457 */
3458 static int
3459term_after_channel_closed(term_T *term)
3460{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003461 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003462 if (!term->tl_normal_mode)
3463 {
3464 int fnum = term->tl_buffer->b_fnum;
3465
3466 cleanup_vterm(term);
3467
3468 if (term->tl_finish == TL_FINISH_CLOSE)
3469 {
3470 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003471 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003472#ifdef FEAT_PROP_POPUP
3473 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003474
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003475 // If this was a terminal in a popup window, go back to the
3476 // previous window.
3477 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3478 {
3479 pwin = curwin;
3480 if (win_valid(prevwin))
3481 win_enter(prevwin, FALSE);
3482 }
3483 else
3484#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003485 // If this is the last normal window: exit Vim.
3486 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3487 {
3488 exarg_T ea;
3489
Bram Moolenaara80faa82020-04-12 19:37:17 +02003490 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003491 ex_quit(&ea);
3492 return TRUE;
3493 }
3494
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003495 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003496 ch_log(NULL, "terminal job finished, closing window");
3497 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003498 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003499 if (curwin == aucmd_win)
3500 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003501 if (do_set_w_closing)
3502 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003503 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003504 if (do_set_w_closing)
3505 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003506 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003507#ifdef FEAT_PROP_POPUP
3508 if (pwin != NULL)
3509 popup_close_with_retval(pwin, 0);
3510#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003511 return TRUE;
3512 }
3513 if (term->tl_finish == TL_FINISH_OPEN
3514 && term->tl_buffer->b_nwindows == 0)
3515 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003516 char *cmd = term->tl_opencmd == NULL
3517 ? "botright sbuf %d"
3518 : (char *)term->tl_opencmd;
3519 size_t len = strlen(cmd) + 50;
3520 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003521
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003522 if (buf != NULL)
3523 {
3524 ch_log(NULL, "terminal job finished, opening window");
3525 vim_snprintf(buf, len, cmd, fnum);
3526 do_cmdline_cmd((char_u *)buf);
3527 vim_free(buf);
3528 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003529 }
3530 else
3531 ch_log(NULL, "terminal job finished");
3532 }
3533
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003534 redraw_buf_and_status_later(term->tl_buffer, UPD_NOT_VALID);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003535 return FALSE;
3536}
3537
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003538#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3539/*
3540 * If the current window is a terminal in a popup window and the job has
3541 * finished, close the popup window and to back to the previous window.
3542 * Otherwise return FAIL.
3543 */
3544 int
3545may_close_term_popup(void)
3546{
3547 if (popup_is_popup(curwin) && curbuf->b_term != NULL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003548 && !term_job_running_not_none(curbuf->b_term))
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003549 {
3550 win_T *pwin = curwin;
3551
3552 if (win_valid(prevwin))
3553 win_enter(prevwin, FALSE);
3554 popup_close_with_retval(pwin, 0);
3555 return OK;
3556 }
3557 return FAIL;
3558}
3559#endif
3560
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003561/*
Bram Moolenaareea32af2021-11-21 14:51:13 +00003562 * Called when a channel is going to be closed, before invoking the close
3563 * callback.
3564 */
3565 void
3566term_channel_closing(channel_T *ch)
3567{
3568 term_T *term;
3569
3570 for (term = first_term; term != NULL; term = term->tl_next)
3571 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
3572 term->tl_channel_closing = TRUE;
3573}
3574
3575/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003576 * Called when a channel has been closed.
3577 * If this was a channel for a terminal window then finish it up.
3578 */
3579 void
3580term_channel_closed(channel_T *ch)
3581{
3582 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003583 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003584 int did_one = FALSE;
3585
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003586 for (term = first_term; term != NULL; term = next_term)
3587 {
3588 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003589 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003590 {
3591 term->tl_channel_closed = TRUE;
3592 did_one = TRUE;
3593
Bram Moolenaard23a8232018-02-10 18:45:26 +01003594 VIM_CLEAR(term->tl_title);
3595 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003596#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003597 if (term->tl_out_fd != NULL)
3598 {
3599 fclose(term->tl_out_fd);
3600 term->tl_out_fd = NULL;
3601 }
3602#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003603
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003604 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003605 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003606 // Cannot open or close windows now. Can happen when
3607 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003608 term->tl_channel_recently_closed = TRUE;
3609 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003610 }
3611
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003612 if (term_after_channel_closed(term))
3613 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003614 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003615 }
3616
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003617 if (did_one)
3618 {
3619 redraw_statuslines();
3620
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003621 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003622 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003623 typebuf_was_filled = TRUE;
3624
3625 term = curbuf->b_term;
3626 if (term != NULL)
3627 {
3628 if (term->tl_job == ch->ch_job)
3629 maketitle();
3630 update_cursor(term, term->tl_cursor_visible);
3631 }
3632 }
3633}
3634
3635/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003636 * To be called after resetting updating_screen: handle any terminal where the
3637 * channel was closed.
3638 */
3639 void
3640term_check_channel_closed_recently()
3641{
3642 term_T *term;
3643 term_T *next_term;
3644
3645 for (term = first_term; term != NULL; term = next_term)
3646 {
3647 next_term = term->tl_next;
3648 if (term->tl_channel_recently_closed)
3649 {
3650 term->tl_channel_recently_closed = FALSE;
3651 if (term_after_channel_closed(term))
3652 // start over, the list may have changed
3653 next_term = first_term;
3654 }
3655 }
3656}
3657
3658/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003659 * Fill one screen line from a line of the terminal.
3660 * Advances "pos" to past the last column.
3661 */
3662 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003663term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003664 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003665 win_T *wp,
3666 VTermScreen *screen,
3667 VTermPos *pos,
3668 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003669{
3670 int off = screen_get_current_line_off();
3671
3672 for (pos->col = 0; pos->col < max_col; )
3673 {
3674 VTermScreenCell cell;
3675 int c;
3676
3677 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003678 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003679
3680 c = cell.chars[0];
3681 if (c == NUL)
3682 {
3683 ScreenLines[off] = ' ';
3684 if (enc_utf8)
3685 ScreenLinesUC[off] = NUL;
3686 }
3687 else
3688 {
3689 if (enc_utf8)
3690 {
3691 int i;
3692
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003693 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003694 for (i = 0; i < Screen_mco
3695 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3696 {
3697 ScreenLinesC[i][off] = cell.chars[i + 1];
3698 if (cell.chars[i + 1] == 0)
3699 break;
3700 }
3701 if (c >= 0x80 || (Screen_mco > 0
3702 && ScreenLinesC[0][off] != 0))
3703 {
3704 ScreenLines[off] = ' ';
3705 ScreenLinesUC[off] = c;
3706 }
3707 else
3708 {
3709 ScreenLines[off] = c;
3710 ScreenLinesUC[off] = NUL;
3711 }
3712 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003713#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003714 else if (has_mbyte && c >= 0x80)
3715 {
3716 char_u mb[MB_MAXBYTES+1];
3717 WCHAR wc = c;
3718
3719 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3720 (char*)mb, 2, 0, 0) > 1)
3721 {
3722 ScreenLines[off] = mb[0];
3723 ScreenLines[off + 1] = mb[1];
3724 cell.width = mb_ptr2cells(mb);
3725 }
3726 else
3727 ScreenLines[off] = c;
3728 }
3729#endif
3730 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003731 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003732 ScreenLines[off] = c;
3733 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003734 ScreenAttrs[off] = cell2attr(term, wp, &cell.attrs, &cell.fg,
3735 &cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003736
3737 ++pos->col;
3738 ++off;
3739 if (cell.width == 2)
3740 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003741 // don't set the second byte to NUL for a DBCS encoding, it
3742 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003743 if (enc_utf8)
3744 {
3745 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003746 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003747 }
3748 else if (!has_mbyte)
3749 {
3750 // Can't show a double-width character with a single-byte
3751 // 'encoding', just use a space.
3752 ScreenLines[off] = ' ';
3753 ScreenAttrs[off] = ScreenAttrs[off - 1];
3754 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003755
3756 ++pos->col;
3757 ++off;
3758 }
3759 }
3760}
3761
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003762#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003763 static void
3764update_system_term(term_T *term)
3765{
3766 VTermPos pos;
3767 VTermScreen *screen;
3768
3769 if (term->tl_vterm == NULL)
3770 return;
3771 screen = vterm_obtain_screen(term->tl_vterm);
3772
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003773 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003774 while (term->tl_toprow > 0
3775 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3776 {
3777 int save_p_more = p_more;
3778
3779 p_more = FALSE;
3780 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003781 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003782 p_more = save_p_more;
3783 --term->tl_toprow;
3784 }
3785
3786 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3787 && pos.row < Rows; ++pos.row)
3788 {
3789 if (pos.row < term->tl_rows)
3790 {
3791 int max_col = MIN(Columns, term->tl_cols);
3792
Bram Moolenaar83d47902020-03-26 20:34:00 +01003793 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003794 }
3795 else
3796 pos.col = 0;
3797
Bram Moolenaar510f0372022-07-04 17:46:22 +01003798 screen_line(curwin, term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003799 }
3800
3801 term->tl_dirty_row_start = MAX_ROW;
3802 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003803}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003804#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003805
3806/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003807 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3808 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003809 * Terminal-Normal mode.
3810 */
3811 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003812term_do_update_window(win_T *wp)
3813{
3814 term_T *term = wp->w_buffer->b_term;
3815
3816 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3817}
3818
3819/*
3820 * Called to update a window that contains an active terminal.
3821 */
3822 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003823term_update_window(win_T *wp)
3824{
3825 term_T *term = wp->w_buffer->b_term;
3826 VTerm *vterm;
3827 VTermScreen *screen;
3828 VTermState *state;
3829 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003830 int rows, cols;
3831 int newrows, newcols;
3832 int minsize;
3833 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003834
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003835 vterm = term->tl_vterm;
3836 screen = vterm_obtain_screen(vterm);
3837 state = vterm_obtain_state(vterm);
3838
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003839 // We use UPD_NOT_VALID on a resize or scroll, redraw everything then.
3840 // With UPD_SOME_VALID only redraw what was marked dirty.
3841 if (wp->w_redr_type > UPD_SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003842 {
3843 term->tl_dirty_row_start = 0;
3844 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003845
3846 if (term->tl_postponed_scroll > 0
3847 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003848 // Scrolling is usually faster than redrawing, when there are only
3849 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003850 term_scroll_up(term, 0, term->tl_postponed_scroll);
3851 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003852 }
3853
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003854 /*
3855 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003856 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003857 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003858 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003859
Bram Moolenaar498c2562018-04-15 23:45:15 +02003860 newrows = 99999;
3861 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003862 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003863 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003864 // Always use curwin, it may be a popup window.
3865 win_T *wwp = twp == NULL ? curwin : twp;
3866
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003867 // When more than one window shows the same terminal, use the
3868 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003869 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003870 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003871 newrows = MIN(newrows, wwp->w_height);
3872 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003873 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003874 if (twp == NULL)
3875 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003876 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003877 if (newrows == 99999 || newcols == 99999)
3878 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003879 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3880 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3881
Bram Moolenaareba13e42021-02-23 17:47:23 +01003882 // If no cell is visible there is no point in resizing. Also, vterm can't
3883 // handle a zero height.
3884 if (newrows == 0 || newcols == 0)
3885 return;
3886
Bram Moolenaar498c2562018-04-15 23:45:15 +02003887 if (term->tl_rows != newrows || term->tl_cols != newcols)
3888 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003889 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003890 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003891 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003892 newrows);
3893 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003894
3895 // Updating the terminal size will cause the snapshot to be cleared.
3896 // When not in terminal_loop() we need to restore it.
3897 if (term != in_terminal_loop)
3898 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003899 }
3900
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003901 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003902 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003903 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003904
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003905 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3906 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003907 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003908 if (pos.row < term->tl_rows)
3909 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003910 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003911
Bram Moolenaar83d47902020-03-26 20:34:00 +01003912 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003913 }
3914 else
3915 pos.col = 0;
3916
Bram Moolenaar510f0372022-07-04 17:46:22 +01003917 screen_line(wp, wp->w_winrow + pos.row
Bram Moolenaarf118d482018-03-13 13:14:00 +01003918#ifdef FEAT_MENU
3919 + winbar_height(wp)
3920#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003921 , wp->w_wincol, pos.col, wp->w_width,
3922#ifdef FEAT_PROP_POPUP
3923 popup_is_popup(wp) ? SLF_POPUP :
3924#endif
3925 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003926 }
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00003927}
3928
3929/*
3930 * Called after updating all windows: may reset dirty rows.
3931 */
3932 void
3933term_did_update_window(win_T *wp)
3934{
3935 term_T *term = wp->w_buffer->b_term;
3936
3937 if (term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode
3938 && wp->w_redr_type == 0)
3939 {
3940 term->tl_dirty_row_start = MAX_ROW;
3941 term->tl_dirty_row_end = 0;
3942 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003943}
3944
3945/*
3946 * Return TRUE if "wp" is a terminal window where the job has finished.
3947 */
3948 int
3949term_is_finished(buf_T *buf)
3950{
3951 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3952}
3953
3954/*
3955 * Return TRUE if "wp" is a terminal window where the job has finished or we
3956 * are in Terminal-Normal mode, thus we show the buffer contents.
3957 */
3958 int
3959term_show_buffer(buf_T *buf)
3960{
3961 term_T *term = buf->b_term;
3962
3963 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3964}
3965
3966/*
3967 * The current buffer is going to be changed. If there is terminal
3968 * highlighting remove it now.
3969 */
3970 void
3971term_change_in_curbuf(void)
3972{
3973 term_T *term = curbuf->b_term;
3974
3975 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3976 {
3977 free_scrollback(term);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003978 redraw_buf_later(term->tl_buffer, UPD_NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003979
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003980 // The buffer is now like a normal buffer, it cannot be easily
3981 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003982 set_string_option_direct((char_u *)"buftype", -1,
3983 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3984 }
3985}
3986
3987/*
3988 * Get the screen attribute for a position in the buffer.
3989 * Use a negative "col" to get the filler background color.
3990 */
3991 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003992term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003993{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003994 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003995 term_T *term = buf->b_term;
3996 sb_line_T *line;
3997 cellattr_T *cellattr;
3998
3999 if (lnum > term->tl_scrollback.ga_len)
4000 cellattr = &term->tl_default_color;
4001 else
4002 {
4003 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
4004 if (col < 0 || col >= line->sb_cols)
4005 cellattr = &line->sb_fill_attr;
4006 else
4007 cellattr = line->sb_cells + col;
4008 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004009 return cell2attr(term, wp, &cellattr->attrs, &cellattr->fg, &cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004010}
4011
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004012/*
4013 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02004014 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004015 */
4016 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004017cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004018{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004019 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
4020 if (rgb->index == 0)
4021 rgb->type = VTERM_COLOR_RGB;
4022 else
4023 {
4024 rgb->type = VTERM_COLOR_INDEXED;
4025 --rgb->index;
4026 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004027}
4028
4029/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004030 * Initialize vterm color from the synID.
4031 * Returns TRUE if color is set to "fg" and "bg".
4032 * Otherwise returns FALSE.
4033 */
4034 static int
4035get_vterm_color_from_synid(int id, VTermColor *fg, VTermColor *bg)
4036{
4037#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4038 // Use the actual color for the GUI and when 'termguicolors' is set.
4039 if (0
4040# ifdef FEAT_GUI
4041 || gui.in_use
4042# endif
4043# ifdef FEAT_TERMGUICOLORS
4044 || p_tgc
4045# ifdef FEAT_VTP
4046 // Finally get INVALCOLOR on this execution path
4047 || (!p_tgc && t_colors >= 256)
4048# endif
4049# endif
4050 )
4051 {
4052 guicolor_T fg_rgb = INVALCOLOR;
4053 guicolor_T bg_rgb = INVALCOLOR;
4054
4055 if (id > 0)
4056 syn_id2colors(id, &fg_rgb, &bg_rgb);
4057
4058 if (fg_rgb != INVALCOLOR)
4059 {
4060 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
4061 fg->red = (unsigned)(rgb >> 16);
4062 fg->green = (unsigned)(rgb >> 8) & 255;
4063 fg->blue = (unsigned)rgb & 255;
4064 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
4065 }
4066 else
4067 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4068
4069 if (bg_rgb != INVALCOLOR)
4070 {
4071 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
4072 bg->red = (unsigned)(rgb >> 16);
4073 bg->green = (unsigned)(rgb >> 8) & 255;
4074 bg->blue = (unsigned)rgb & 255;
4075 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
4076 }
4077 else
4078 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4079
4080 return TRUE;
4081 }
4082 else
4083#endif
4084 if (t_colors >= 16)
4085 {
4086 int cterm_fg = -1;
4087 int cterm_bg = -1;
4088
4089 if (id > 0)
4090 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
4091
4092 if (cterm_fg >= 0)
4093 {
4094 cterm_color2vterm(cterm_fg, fg);
4095 fg->type |= VTERM_COLOR_DEFAULT_FG;
4096 }
4097 else
4098 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4099
4100 if (cterm_bg >= 0)
4101 {
4102 cterm_color2vterm(cterm_bg, bg);
4103 bg->type |= VTERM_COLOR_DEFAULT_BG;
4104 }
4105 else
4106 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4107
4108 return TRUE;
4109 }
4110
4111 return FALSE;
4112}
4113
4114 void
4115term_reset_wincolor(win_T *wp)
4116{
4117 wp->w_term_wincolor.fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4118 wp->w_term_wincolor.bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4119}
4120
4121/*
4122 * Cache the color of 'wincolor'.
4123 */
4124 void
4125term_update_wincolor(win_T *wp)
4126{
4127 int id = 0;
4128
4129 if (*wp->w_p_wcr != NUL)
4130 id = syn_name2id(wp->w_p_wcr);
4131 if (id == 0 || !get_vterm_color_from_synid(id, &wp->w_term_wincolor.fg,
4132 &wp->w_term_wincolor.bg))
4133 term_reset_wincolor(wp);
4134}
4135
4136/*
4137 * Called when option 'termguicolors' was set,
4138 * or when any highlight is changed.
4139 */
4140 void
4141term_update_wincolor_all()
4142{
4143 win_T *wp = NULL;
4144 int did_curwin = FALSE;
4145
4146 while (for_all_windows_and_curwin(&wp, &did_curwin))
4147 term_update_wincolor(wp);
4148}
4149
4150/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004151 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004152 */
4153 static void
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004154init_default_colors(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004155{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004156 VTermColor *fg, *bg;
4157 int fgval, bgval;
4158 int id;
4159
Bram Moolenaara80faa82020-04-12 19:37:17 +02004160 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004161 term->tl_default_color.width = 1;
4162 fg = &term->tl_default_color.fg;
4163 bg = &term->tl_default_color.bg;
4164
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004165 // Vterm uses a default black background. Set it to white when
4166 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004167 if (*p_bg == 'l')
4168 {
4169 fgval = 0;
4170 bgval = 255;
4171 }
4172 else
4173 {
4174 fgval = 255;
4175 bgval = 0;
4176 }
4177 fg->red = fg->green = fg->blue = fgval;
4178 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004179 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
4180 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004181
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004182 // The highlight group overrules the defaults.
4183 id = term_get_highlight_id(term, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004184
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004185 if (!get_vterm_color_from_synid(id, fg, bg))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004186 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004187#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004188 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004189#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004190
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004191 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004192 if (cterm_normal_fg_color > 0)
4193 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004194 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004195# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4196# ifdef VIMDLL
4197 if (!gui.in_use)
4198# endif
4199 {
4200 tmp = fg->red;
4201 fg->red = fg->blue;
4202 fg->blue = tmp;
4203 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004204# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004205 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004206# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004207 else
4208 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004209# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004210
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004211 if (cterm_normal_bg_color > 0)
4212 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004213 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004214# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4215# ifdef VIMDLL
4216 if (!gui.in_use)
4217# endif
4218 {
4219 tmp = fg->red;
4220 fg->red = fg->blue;
4221 fg->blue = tmp;
4222 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004223# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004224 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004225# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004226 else
4227 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004228# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004229 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004230}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004231
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004232#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4233/*
Bram Moolenaar30b9a412022-05-26 14:06:37 +01004234 * Return TRUE if the user-defined palette (either g:terminal_ansi_colors or the
4235 * "ansi_colors" argument in term_start()) shall be applied.
4236 */
4237 static int
4238term_use_palette()
4239{
4240 if (0
4241#ifdef FEAT_GUI
4242 || gui.in_use
4243#endif
4244#ifdef FEAT_TERMGUICOLORS
4245 || p_tgc
4246#endif
4247 )
4248 return TRUE;
4249 return FALSE;
4250}
4251
4252/*
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004253 * Set the 16 ANSI colors from array of RGB values
4254 */
4255 static void
4256set_vterm_palette(VTerm *vterm, long_u *rgb)
4257{
4258 int index = 0;
4259 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004260
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004261 for (; index < 16; index++)
4262 {
4263 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004264
Millyb771b6b2021-11-23 12:07:25 +00004265 color.type = VTERM_COLOR_RGB;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004266 color.red = (unsigned)(rgb[index] >> 16);
4267 color.green = (unsigned)(rgb[index] >> 8) & 255;
4268 color.blue = (unsigned)rgb[index] & 255;
Bram Moolenaar68afde42022-02-25 20:48:26 +00004269 color.index = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004270 vterm_state_set_palette_color(state, index, &color);
4271 }
4272}
4273
4274/*
4275 * Set the ANSI color palette from a list of colors
4276 */
4277 static int
4278set_ansi_colors_list(VTerm *vterm, list_T *list)
4279{
4280 int n = 0;
4281 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004282 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004283
Bram Moolenaarb0992022020-01-30 14:55:42 +01004284 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004285 {
4286 char_u *color_name;
4287 guicolor_T guicolor;
4288
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004289 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004290 if (color_name == NULL)
4291 return FAIL;
4292
4293 guicolor = GUI_GET_COLOR(color_name);
4294 if (guicolor == INVALCOLOR)
4295 return FAIL;
4296
4297 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4298 }
4299
4300 if (n != 16 || li != NULL)
4301 return FAIL;
4302
4303 set_vterm_palette(vterm, rgb);
4304
4305 return OK;
4306}
4307
4308/*
4309 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4310 */
4311 static void
4312init_vterm_ansi_colors(VTerm *vterm)
4313{
4314 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4315
LemonBoyb2b3acb2022-05-20 10:10:34 +01004316 if (var == NULL)
4317 return;
4318
4319 if (var->di_tv.v_type != VAR_LIST
4320 || var->di_tv.vval.v_list == NULL
4321 || var->di_tv.vval.v_list->lv_first == &range_list_item
4322 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004323 semsg(_(e_invalid_argument_str), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004324}
4325#endif
4326
Bram Moolenaar52acb112018-03-18 19:20:22 +01004327/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004328 * Handles a "drop" command from the job in the terminal.
4329 * "item" is the file name, "item->li_next" may have options.
4330 */
4331 static void
4332handle_drop_command(listitem_T *item)
4333{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004334 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004335 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004336 int bufnr;
4337 win_T *wp;
4338 tabpage_T *tp;
4339 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004340 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004341
4342 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4343 FOR_ALL_TAB_WINDOWS(tp, wp)
4344 {
4345 if (wp->w_buffer->b_fnum == bufnr)
4346 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004347 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004348 goto_tabpage_win(tp, wp);
4349 return;
4350 }
4351 }
4352
Bram Moolenaara80faa82020-04-12 19:37:17 +02004353 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004354
4355 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4356 && opt_item->li_tv.vval.v_dict != NULL)
4357 {
4358 dict_T *dict = opt_item->li_tv.vval.v_dict;
4359 char_u *p;
4360
Bram Moolenaard61efa52022-07-23 09:52:04 +01004361 p = dict_get_string(dict, "ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004362 if (p == NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004363 p = dict_get_string(dict, "fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004364 if (p != NULL)
4365 {
4366 if (check_ff_value(p) == FAIL)
4367 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4368 else
4369 ea.force_ff = *p;
4370 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01004371 p = dict_get_string(dict, "enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004372 if (p == NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004373 p = dict_get_string(dict, "encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004374 if (p != NULL)
4375 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004376 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004377 if (ea.cmd != NULL)
4378 {
4379 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4380 ea.force_enc = 11;
4381 tofree = ea.cmd;
4382 }
4383 }
4384
Bram Moolenaard61efa52022-07-23 09:52:04 +01004385 p = dict_get_string(dict, "bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004386 if (p != NULL)
4387 get_bad_opt(p, &ea);
4388
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004389 if (dict_has_key(dict, "bin"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004390 ea.force_bin = FORCE_BIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004391 if (dict_has_key(dict, "binary"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004392 ea.force_bin = FORCE_BIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004393 if (dict_has_key(dict, "nobin"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004394 ea.force_bin = FORCE_NOBIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004395 if (dict_has_key(dict, "nobinary"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004396 ea.force_bin = FORCE_NOBIN;
4397 }
4398
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004399 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004400 if (ea.cmd == NULL)
4401 ea.cmd = (char_u *)"split";
4402 ea.arg = fname;
4403 ea.cmdidx = CMD_split;
4404 ex_splitview(&ea);
4405
4406 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004407}
4408
4409/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004410 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4411 */
4412 static int
4413is_permitted_term_api(char_u *func, char_u *pat)
4414{
4415 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4416}
4417
4418/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004419 * Handles a function call from the job running in a terminal.
4420 * "item" is the function name, "item->li_next" has the arguments.
4421 */
4422 static void
4423handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4424{
4425 char_u *func;
4426 typval_T argvars[2];
4427 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004428 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004429
4430 if (item->li_next == NULL)
4431 {
4432 ch_log(channel, "Missing function arguments for call");
4433 return;
4434 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004435 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004436
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004437 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004438 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004439 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004440 return;
4441 }
4442
4443 argvars[0].v_type = VAR_NUMBER;
4444 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4445 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004446 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004447 funcexe.fe_firstline = 1L;
4448 funcexe.fe_lastline = 1L;
4449 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004450 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004451 {
4452 clear_tv(&rettv);
4453 ch_log(channel, "Function %s called", func);
4454 }
4455 else
4456 ch_log(channel, "Calling function %s failed", func);
4457}
4458
4459/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004460 * URL decoding (also know as Percent-encoding).
4461 *
4462 * Note this function currently is only used for decoding shell's
4463 * OSC 7 escape sequence which we can assume all bytes are valid
4464 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4465 * encoding bytes like 0xfe, 0xff.
4466 */
4467 static size_t
4468url_decode(const char *src, const size_t len, char_u *dst)
4469{
4470 size_t i = 0, j = 0;
4471
4472 while (i < len)
4473 {
4474 if (src[i] == '%' && i + 2 < len)
4475 {
4476 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4477 j++;
4478 i += 3;
4479 }
4480 else
4481 {
4482 dst[j] = src[i];
4483 i++;
4484 j++;
4485 }
4486 }
4487 dst[j] = '\0';
4488 return j;
4489}
4490
4491/*
4492 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4493 *
4494 * The OSC 7 sequence has the format of
4495 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4496 * and what VTerm provides via VTermStringFragment is
4497 * "file://HOSTNAME/CURRENT/DIR"
4498 */
4499 static void
Bram Moolenaar474ad392022-08-21 11:37:17 +01004500sync_shell_dir(garray_T *gap)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004501{
Bram Moolenaar474ad392022-08-21 11:37:17 +01004502 int offset = 7; // len of "file://" is 7
4503 char *pos = (char *)gap->ga_data + offset;
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004504 char_u *new_dir;
4505
4506 // remove HOSTNAME to get PWD
Bram Moolenaar474ad392022-08-21 11:37:17 +01004507 while (offset < (int)gap->ga_len && *pos != '/' )
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004508 {
Bram Moolenaar474ad392022-08-21 11:37:17 +01004509 ++offset;
4510 ++pos;
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004511 }
4512
Bram Moolenaar474ad392022-08-21 11:37:17 +01004513 if (offset >= (int)gap->ga_len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004514 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004515 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
Bram Moolenaar474ad392022-08-21 11:37:17 +01004516 gap->ga_data);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004517 return;
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004518 }
4519
Bram Moolenaar474ad392022-08-21 11:37:17 +01004520 new_dir = alloc(gap->ga_len - offset + 1);
4521 url_decode(pos, gap->ga_len-offset, new_dir);
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004522 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4523 vim_free(new_dir);
4524}
4525
4526/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004527 * Called by libvterm when it cannot recognize an OSC sequence.
4528 * We recognize a terminal API command.
4529 */
4530 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004531parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004532{
4533 term_T *term = (term_T *)user;
4534 js_read_T reader;
4535 typval_T tv;
4536 channel_T *channel = term->tl_job == NULL ? NULL
4537 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004538 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004539
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004540 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
Bram Moolenaar474ad392022-08-21 11:37:17 +01004541 if (command != 51 && (command != 7 || !p_asd))
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004542 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004543
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004544 // Concatenate what was received until the final piece is found.
4545 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4546 {
4547 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004548 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004549 }
4550 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004551 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004552 if (!frag.final)
4553 return 1;
4554
4555 ((char *)gap->ga_data)[gap->ga_len] = 0;
Bram Moolenaar474ad392022-08-21 11:37:17 +01004556
4557 if (command == 7)
4558 {
4559 sync_shell_dir(gap);
4560 ga_clear(gap);
4561 return 1;
4562 }
4563
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004564 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004565 reader.js_fill = NULL;
4566 reader.js_used = 0;
4567 if (json_decode(&reader, &tv, 0) == OK
4568 && tv.v_type == VAR_LIST
4569 && tv.vval.v_list != NULL)
4570 {
4571 listitem_T *item = tv.vval.v_list->lv_first;
4572
4573 if (item == NULL)
4574 ch_log(channel, "Missing command");
4575 else
4576 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004577 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004578
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004579 // Make sure an invoked command doesn't delete the buffer (and the
4580 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004581 ++term->tl_buffer->b_locked;
4582
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004583 item = item->li_next;
4584 if (item == NULL)
4585 ch_log(channel, "Missing argument for %s", cmd);
4586 else if (STRCMP(cmd, "drop") == 0)
4587 handle_drop_command(item);
4588 else if (STRCMP(cmd, "call") == 0)
4589 handle_call_command(term, channel, item);
4590 else
4591 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004592 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004593 }
4594 }
4595 else
4596 ch_log(channel, "Invalid JSON received");
4597
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004598 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004599 clear_tv(&tv);
4600 return 1;
4601}
4602
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004603/*
4604 * Called by libvterm when it cannot recognize a CSI sequence.
4605 * We recognize the window position report.
4606 */
4607 static int
4608parse_csi(
4609 const char *leader UNUSED,
4610 const long args[],
4611 int argcount,
4612 const char *intermed UNUSED,
4613 char command,
4614 void *user)
4615{
4616 term_T *term = (term_T *)user;
4617 char buf[100];
4618 int len;
4619 int x = 0;
4620 int y = 0;
4621 win_T *wp;
4622
4623 // We recognize only CSI 13 t
4624 if (command != 't' || argcount != 1 || args[0] != 13)
4625 return 0; // not handled
4626
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004627 // When getting the window position is not possible or it fails it results
4628 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004629#if defined(FEAT_GUI) \
4630 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4631 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004632 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004633#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004634
4635 FOR_ALL_WINDOWS(wp)
4636 if (wp->w_buffer == term->tl_buffer)
4637 break;
4638 if (wp != NULL)
4639 {
4640#ifdef FEAT_GUI
4641 if (gui.in_use)
4642 {
4643 x += wp->w_wincol * gui.char_width;
4644 y += W_WINROW(wp) * gui.char_height;
4645 }
4646 else
4647#endif
4648 {
4649 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004650 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004651 x += wp->w_wincol * 7;
4652 y += W_WINROW(wp) * 10;
4653 }
4654 }
4655
4656 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4657 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4658 (char_u *)buf, len, NULL);
4659 return 1;
4660}
4661
Bram Moolenaard8637282020-05-20 18:41:41 +02004662static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004663 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004664 parse_csi, // csi
4665 parse_osc, // osc
Bram Moolenaar7da34152021-11-24 19:30:55 +00004666 NULL, // dcs
4667 NULL, // apc
4668 NULL, // pm
4669 NULL // sos
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004670};
4671
4672/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004673 * Use Vim's allocation functions for vterm so profiling works.
4674 */
4675 static void *
4676vterm_malloc(size_t size, void *data UNUSED)
4677{
Bram Moolenaar88137392021-11-12 16:01:15 +00004678 // make sure that the length is not zero
4679 return alloc_clear(size == 0 ? 1L : size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004680}
4681
4682 static void
4683vterm_memfree(void *ptr, void *data UNUSED)
4684{
4685 vim_free(ptr);
4686}
4687
4688static VTermAllocatorFunctions vterm_allocator = {
4689 &vterm_malloc,
4690 &vterm_memfree
4691};
4692
4693/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004694 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004695 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004696 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004697 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004698create_vterm(term_T *term, int rows, int cols)
4699{
4700 VTerm *vterm;
4701 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004702 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004703 VTermValue value;
4704
Bram Moolenaar756ef112018-04-10 12:04:27 +02004705 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004706 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004707 if (vterm == NULL)
4708 return FAIL;
4709
4710 // Allocate screen and state here, so we can bail out if that fails.
4711 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004712 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004713 if (state == NULL || screen == NULL)
4714 {
4715 vterm_free(vterm);
4716 return FAIL;
4717 }
4718
Bram Moolenaar52acb112018-03-18 19:20:22 +01004719 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004720 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004721 vterm_set_utf8(vterm, 1);
4722
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004723 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004724
4725 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004726 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004727 &term->tl_default_color.fg,
4728 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004729
Bram Moolenaar9e587872019-05-13 20:27:23 +02004730 if (t_colors < 16)
4731 // Less than 16 colors: assume that bold means using a bright color for
4732 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004733 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4734
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004735 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004736 vterm_screen_reset(screen, 1 /* hard */);
4737
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004738 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004739 vterm_screen_enable_altscreen(screen, 1);
4740
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004741 // For unix do not use a blinking cursor. In an xterm this causes the
4742 // cursor to blink if it's blinking in the xterm.
4743 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004744#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004745 if (GetCaretBlinkTime() == INFINITE)
4746 value.boolean = 0;
4747 else
4748 value.boolean = 1;
4749#else
4750 value.boolean = 0;
4751#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004752 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004753 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004754
4755 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004756}
4757
4758/*
LemonBoyb2b3acb2022-05-20 10:10:34 +01004759 * Reset the terminal palette to its default value.
4760 */
4761 static void
4762term_reset_palette(VTerm *vterm)
4763{
4764 VTermState *state = vterm_obtain_state(vterm);
4765 int index;
4766
4767 for (index = 0; index < 16; index++)
4768 {
4769 VTermColor color;
4770
4771 color.type = VTERM_COLOR_INDEXED;
4772 ansi_color2rgb(index, &color.red, &color.green, &color.blue,
4773 &color.index);
4774 // The first valid index starts at 1.
4775 color.index -= 1;
4776
4777 vterm_state_set_palette_color(state, index, &color);
4778 }
4779}
4780
4781 static void
4782term_update_palette(term_T *term)
4783{
Bram Moolenaar30b9a412022-05-26 14:06:37 +01004784#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
LemonBoyb2b3acb2022-05-20 10:10:34 +01004785 if (term_use_palette()
4786 && (term->tl_palette != NULL
4787 || find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE)
Bram Moolenaar30b9a412022-05-26 14:06:37 +01004788 != NULL))
LemonBoyb2b3acb2022-05-20 10:10:34 +01004789 {
4790 if (term->tl_palette != NULL)
4791 set_vterm_palette(term->tl_vterm, term->tl_palette);
4792 else
4793 init_vterm_ansi_colors(term->tl_vterm);
4794 }
4795 else
Bram Moolenaar30b9a412022-05-26 14:06:37 +01004796#endif
LemonBoyb2b3acb2022-05-20 10:10:34 +01004797 term_reset_palette(term->tl_vterm);
4798}
4799
4800/*
4801 * Called when option 'termguicolors' is changed.
4802 */
4803 void
4804term_update_palette_all()
4805{
4806 term_T *term;
4807
4808 FOR_ALL_TERMS(term)
4809 {
4810 if (term->tl_vterm == NULL)
4811 continue;
4812 term_update_palette(term);
4813 }
4814}
4815
4816/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004817 * Called when option 'background' or 'termguicolors' was set,
4818 * or when any highlight is changed.
Bram Moolenaarad431992021-05-03 20:40:38 +02004819 */
4820 void
4821term_update_colors_all(void)
4822{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004823 term_T *term;
Bram Moolenaarad431992021-05-03 20:40:38 +02004824
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004825 FOR_ALL_TERMS(term)
4826 {
4827 if (term->tl_vterm == NULL)
4828 continue;
4829 init_default_colors(term);
4830 vterm_state_set_default_colors(
4831 vterm_obtain_state(term->tl_vterm),
4832 &term->tl_default_color.fg,
4833 &term->tl_default_color.bg);
4834 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004835}
4836
4837/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004838 * Return the text to show for the buffer name and status.
4839 */
4840 char_u *
4841term_get_status_text(term_T *term)
4842{
4843 if (term->tl_status_text == NULL)
4844 {
4845 char_u *txt;
4846 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004847 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004848
4849 if (term->tl_normal_mode)
4850 {
4851 if (term_job_running(term))
4852 txt = (char_u *)_("Terminal");
4853 else
4854 txt = (char_u *)_("Terminal-finished");
4855 }
4856 else if (term->tl_title != NULL)
4857 txt = term->tl_title;
4858 else if (term_none_open(term))
4859 txt = (char_u *)_("active");
4860 else if (term_job_running(term))
4861 txt = (char_u *)_("running");
4862 else
4863 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004864 fname = buf_get_fname(term->tl_buffer);
4865 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004866 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004867 if (term->tl_status_text != NULL)
4868 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004869 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004870 }
4871 return term->tl_status_text;
4872}
4873
4874/*
Bram Moolenaar3ad69532021-11-19 17:01:08 +00004875 * Clear the cached value of the status text.
4876 */
4877 void
4878term_clear_status_text(term_T *term)
4879{
4880 VIM_CLEAR(term->tl_status_text);
4881}
4882
4883/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004884 * Mark references in jobs of terminals.
4885 */
4886 int
4887set_ref_in_term(int copyID)
4888{
4889 int abort = FALSE;
4890 term_T *term;
4891 typval_T tv;
4892
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004893 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004894 if (term->tl_job != NULL)
4895 {
4896 tv.v_type = VAR_JOB;
4897 tv.vval.v_job = term->tl_job;
4898 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4899 }
4900 return abort;
4901}
4902
4903/*
4904 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004905 * Returns NULL when the buffer is not for a terminal window and logs a message
4906 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004907 */
4908 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004909term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004910{
4911 buf_T *buf;
4912
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004913 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004914 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004915 --emsg_off;
4916 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004917 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004918 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004919 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004920 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004921 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004922 return buf;
4923}
4924
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004925 static void
4926clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004927{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004928 CLEAR_FIELD(*cell);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004929 cell->fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4930 cell->bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004931}
4932
4933 static void
4934dump_term_color(FILE *fd, VTermColor *color)
4935{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004936 int index;
4937
4938 if (VTERM_COLOR_IS_INDEXED(color))
4939 index = color->index + 1;
4940 else if (color->type == 0)
4941 // use RGB values
4942 index = 255;
4943 else
4944 // default color
4945 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004946 fprintf(fd, "%02x%02x%02x%d",
4947 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004948 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004949}
4950
4951/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004952 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004953 *
4954 * Each screen cell in full is:
4955 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4956 * {characters} is a space for an empty cell
4957 * For a double-width character "+" is changed to "*" and the next cell is
4958 * skipped.
4959 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4960 * when "&" use the same as the previous cell.
4961 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4962 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4963 * {color-idx} is a number from 0 to 255
4964 *
4965 * Screen cell with same width, attributes and color as the previous one:
4966 * |{characters}
4967 *
4968 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4969 *
4970 * Repeating the previous screen cell:
4971 * @{count}
4972 */
4973 void
4974f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4975{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004976 buf_T *buf;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004977 term_T *term;
4978 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004979 int max_height = 0;
4980 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004981 stat_T st;
4982 FILE *fd;
4983 VTermPos pos;
4984 VTermScreen *screen;
4985 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004986 VTermState *state;
4987 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004988
4989 if (check_restricted() || check_secure())
4990 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004991
4992 if (in_vim9script()
4993 && (check_for_buffer_arg(argvars, 0) == FAIL
4994 || check_for_string_arg(argvars, 1) == FAIL
4995 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4996 return;
4997
4998 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004999 if (buf == NULL)
5000 return;
5001 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02005002 if (term->tl_vterm == NULL)
5003 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005004 emsg(_(e_job_already_finished));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02005005 return;
5006 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005007
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005008 if (argvars[2].v_type != VAR_UNKNOWN)
5009 {
5010 dict_T *d;
5011
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01005012 if (check_for_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005013 return;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005014 d = argvars[2].vval.v_dict;
5015 if (d != NULL)
5016 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01005017 max_height = dict_get_number(d, "rows");
5018 max_width = dict_get_number(d, "columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005019 }
5020 }
5021
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005022 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005023 if (fname == NULL)
5024 return;
5025 if (mch_stat((char *)fname, &st) >= 0)
5026 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005027 semsg(_(e_file_exists_str), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005028 return;
5029 }
5030
Bram Moolenaard96ff162018-02-18 22:13:29 +01005031 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
5032 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005033 semsg(_(e_cant_create_file_str), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005034 return;
5035 }
5036
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005037 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005038
5039 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005040 state = vterm_obtain_state(term->tl_vterm);
5041 vterm_state_get_cursorpos(state, &cursor_pos);
5042
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005043 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
5044 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005045 {
5046 int repeat = 0;
5047
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005048 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
5049 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005050 {
5051 VTermScreenCell cell;
5052 int same_attr;
5053 int same_chars = TRUE;
5054 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005055 int is_cursor_pos = (pos.col == cursor_pos.col
5056 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005057
5058 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005059 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005060
5061 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
5062 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01005063 int c = cell.chars[i];
5064 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02005065 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01005066
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005067 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01005068 if (i == 0)
5069 {
5070 c = (c == NUL) ? ' ' : c;
5071 pc = (pc == NUL) ? ' ' : pc;
5072 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02005073 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005074 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02005075 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005076 break;
5077 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005078 same_attr = vtermAttr2hl(&cell.attrs)
5079 == vtermAttr2hl(&prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005080 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
5081 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005082 if (same_chars && cell.width == prev_cell.width && same_attr
5083 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005084 {
5085 ++repeat;
5086 }
5087 else
5088 {
5089 if (repeat > 0)
5090 {
5091 fprintf(fd, "@%d", repeat);
5092 repeat = 0;
5093 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005094 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005095
5096 if (cell.chars[0] == NUL)
5097 fputs(" ", fd);
5098 else
5099 {
5100 char_u charbuf[10];
5101 int len;
5102
5103 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
5104 && cell.chars[i] != NUL; ++i)
5105 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02005106 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005107 fwrite(charbuf, len, 1, fd);
5108 }
5109 }
5110
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005111 // When only the characters differ we don't write anything, the
5112 // following "|", "@" or NL will indicate using the same
5113 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005114 if (cell.width != prev_cell.width || !same_attr)
5115 {
5116 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005117 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005118 else
5119 fputs("+", fd);
5120
5121 if (same_attr)
5122 {
5123 fputs("&", fd);
5124 }
5125 else
5126 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005127 fprintf(fd, "%d", vtermAttr2hl(&cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005128 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005129 fputs("&", fd);
5130 else
5131 {
5132 fputs("#", fd);
5133 dump_term_color(fd, &cell.fg);
5134 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005135 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005136 fputs("&", fd);
5137 else
5138 {
5139 fputs("#", fd);
5140 dump_term_color(fd, &cell.bg);
5141 }
5142 }
5143 }
5144
5145 prev_cell = cell;
5146 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005147
5148 if (cell.width == 2)
5149 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005150 }
5151 if (repeat > 0)
5152 fprintf(fd, "@%d", repeat);
5153 fputs("\n", fd);
5154 }
5155
5156 fclose(fd);
5157}
5158
5159/*
5160 * Called when a dump is corrupted. Put a breakpoint here when debugging.
5161 */
5162 static void
5163dump_is_corrupt(garray_T *gap)
5164{
5165 ga_concat(gap, (char_u *)"CORRUPT");
5166}
5167
5168 static void
5169append_cell(garray_T *gap, cellattr_T *cell)
5170{
5171 if (ga_grow(gap, 1) == OK)
5172 {
5173 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
5174 ++gap->ga_len;
5175 }
5176}
5177
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005178 static void
5179clear_cellattr(cellattr_T *cell)
5180{
5181 CLEAR_FIELD(*cell);
5182 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
5183 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
5184}
5185
Bram Moolenaard96ff162018-02-18 22:13:29 +01005186/*
5187 * Read the dump file from "fd" and append lines to the current buffer.
5188 * Return the cell width of the longest line.
5189 */
5190 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01005191read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005192{
5193 int c;
5194 garray_T ga_text;
5195 garray_T ga_cell;
5196 char_u *prev_char = NULL;
5197 int attr = 0;
5198 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005199 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005200 term_T *term = curbuf->b_term;
5201 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005202 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005203
5204 ga_init2(&ga_text, 1, 90);
5205 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005206 clear_cellattr(&cell);
5207 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005208 cursor_pos->row = -1;
5209 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005210
5211 c = fgetc(fd);
5212 for (;;)
5213 {
5214 if (c == EOF)
5215 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02005216 if (c == '\r')
5217 {
5218 // DOS line endings? Ignore.
5219 c = fgetc(fd);
5220 }
5221 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005222 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005223 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005224 if (ga_text.ga_data == NULL)
5225 dump_is_corrupt(&ga_text);
5226 if (ga_grow(&term->tl_scrollback, 1) == OK)
5227 {
5228 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
5229 + term->tl_scrollback.ga_len;
5230
5231 if (max_cells < ga_cell.ga_len)
5232 max_cells = ga_cell.ga_len;
5233 line->sb_cols = ga_cell.ga_len;
5234 line->sb_cells = ga_cell.ga_data;
5235 line->sb_fill_attr = term->tl_default_color;
5236 ++term->tl_scrollback.ga_len;
5237 ga_init(&ga_cell);
5238
5239 ga_append(&ga_text, NUL);
5240 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5241 ga_text.ga_len, FALSE);
5242 }
5243 else
5244 ga_clear(&ga_cell);
5245 ga_text.ga_len = 0;
5246
5247 c = fgetc(fd);
5248 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005249 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005250 {
5251 int prev_len = ga_text.ga_len;
5252
Bram Moolenaar9271d052018-02-25 21:39:46 +01005253 if (c == '>')
5254 {
5255 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005256 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005257 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5258 cursor_pos->col = ga_cell.ga_len;
5259 }
5260
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005261 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005262 c = fgetc(fd);
5263 if (c != EOF)
5264 ga_append(&ga_text, c);
5265 for (;;)
5266 {
5267 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005268 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005269 || c == EOF || c == '\n')
5270 break;
5271 ga_append(&ga_text, c);
5272 }
5273
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005274 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005275 vim_free(prev_char);
5276 if (ga_text.ga_data != NULL)
5277 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5278 ga_text.ga_len - prev_len);
5279
Bram Moolenaar9271d052018-02-25 21:39:46 +01005280 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005281 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005282 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005283 }
5284 else if (c == '+' || c == '*')
5285 {
5286 int is_bg;
5287
5288 cell.width = c == '+' ? 1 : 2;
5289
5290 c = fgetc(fd);
5291 if (c == '&')
5292 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005293 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005294 c = fgetc(fd);
5295 }
5296 else if (isdigit(c))
5297 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005298 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005299 attr = 0;
5300 while (isdigit(c))
5301 {
5302 attr = attr * 10 + (c - '0');
5303 c = fgetc(fd);
5304 }
5305 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005306
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005307 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005308 for (is_bg = 0; is_bg <= 1; ++is_bg)
5309 {
5310 if (c == '&')
5311 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005312 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005313 c = fgetc(fd);
5314 }
5315 else if (c == '#')
5316 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005317 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005318
5319 c = fgetc(fd);
5320 red = hex2nr(c);
5321 c = fgetc(fd);
5322 red = (red << 4) + hex2nr(c);
5323 c = fgetc(fd);
5324 green = hex2nr(c);
5325 c = fgetc(fd);
5326 green = (green << 4) + hex2nr(c);
5327 c = fgetc(fd);
5328 blue = hex2nr(c);
5329 c = fgetc(fd);
5330 blue = (blue << 4) + hex2nr(c);
5331 c = fgetc(fd);
5332 if (!isdigit(c))
5333 dump_is_corrupt(&ga_text);
5334 while (isdigit(c))
5335 {
5336 index = index * 10 + (c - '0');
5337 c = fgetc(fd);
5338 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005339 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005340 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005341 type = VTERM_COLOR_RGB;
5342 if (index == 0)
5343 {
5344 if (is_bg)
5345 type |= VTERM_COLOR_DEFAULT_BG;
5346 else
5347 type |= VTERM_COLOR_DEFAULT_FG;
5348 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005349 }
5350 else
5351 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005352 type = VTERM_COLOR_INDEXED;
5353 index -= 1;
5354 }
5355 if (is_bg)
5356 {
5357 cell.bg.type = type;
5358 cell.bg.red = red;
5359 cell.bg.green = green;
5360 cell.bg.blue = blue;
5361 cell.bg.index = index;
5362 }
5363 else
5364 {
5365 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005366 cell.fg.red = red;
5367 cell.fg.green = green;
5368 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005369 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005370 }
5371 }
5372 else
5373 dump_is_corrupt(&ga_text);
5374 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005375 }
5376 else
5377 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005378 }
5379 else
5380 dump_is_corrupt(&ga_text);
5381
5382 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005383 if (cell.width == 2)
5384 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005385 }
5386 else if (c == '@')
5387 {
5388 if (prev_char == NULL)
5389 dump_is_corrupt(&ga_text);
5390 else
5391 {
5392 int count = 0;
5393
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005394 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005395 for (;;)
5396 {
5397 c = fgetc(fd);
5398 if (!isdigit(c))
5399 break;
5400 count = count * 10 + (c - '0');
5401 }
5402
5403 while (count-- > 0)
5404 {
5405 ga_concat(&ga_text, prev_char);
5406 append_cell(&ga_cell, &cell);
5407 }
5408 }
5409 }
5410 else
5411 {
5412 dump_is_corrupt(&ga_text);
5413 c = fgetc(fd);
5414 }
5415 }
5416
5417 if (ga_text.ga_len > 0)
5418 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005419 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005420 dump_is_corrupt(&ga_text);
5421 ga_append(&ga_text, NUL);
5422 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5423 ga_text.ga_len, FALSE);
5424 }
5425
5426 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005427 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005428 vim_free(prev_char);
5429
5430 return max_cells;
5431}
5432
5433/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005434 * Return an allocated string with at least "text_width" "=" characters and
5435 * "fname" inserted in the middle.
5436 */
5437 static char_u *
5438get_separator(int text_width, char_u *fname)
5439{
5440 int width = MAX(text_width, curwin->w_width);
5441 char_u *textline;
5442 int fname_size;
5443 char_u *p = fname;
5444 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005445 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005446
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005447 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005448 if (textline == NULL)
5449 return NULL;
5450
5451 fname_size = vim_strsize(fname);
5452 if (fname_size < width - 8)
5453 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005454 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005455 width = MAX(text_width, fname_size + 8);
5456 }
5457 else if (fname_size > width - 8)
5458 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005459 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005460 p = gettail(fname);
5461 fname_size = vim_strsize(p);
5462 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005463 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005464 while (fname_size > width - 8)
5465 {
5466 p += (*mb_ptr2len)(p);
5467 fname_size = vim_strsize(p);
5468 }
5469
5470 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5471 textline[i] = '=';
5472 textline[i++] = ' ';
5473
5474 STRCPY(textline + i, p);
5475 off = STRLEN(textline);
5476 textline[off] = ' ';
5477 for (i = 1; i < (width - fname_size) / 2; ++i)
5478 textline[off + i] = '=';
5479 textline[off + i] = NUL;
5480
5481 return textline;
5482}
5483
5484/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005485 * Common for "term_dumpdiff()" and "term_dumpload()".
5486 */
5487 static void
5488term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5489{
5490 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005491 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005492 char_u buf1[NUMBUFLEN];
5493 char_u buf2[NUMBUFLEN];
5494 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005495 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005496 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005497 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005498 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005499 char_u *textline = NULL;
5500
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005501 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005502 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005503 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005504 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005505 if (fname1 == NULL || (do_diff && fname2 == NULL))
5506 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005507 emsg(_(e_invalid_argument));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005508 return;
5509 }
5510 fd1 = mch_fopen((char *)fname1, READBIN);
5511 if (fd1 == NULL)
5512 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005513 semsg(_(e_cant_read_file_str), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005514 return;
5515 }
5516 if (do_diff)
5517 {
5518 fd2 = mch_fopen((char *)fname2, READBIN);
5519 if (fd2 == NULL)
5520 {
5521 fclose(fd1);
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005522 semsg(_(e_cant_read_file_str), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005523 return;
5524 }
5525 }
5526
5527 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005528 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5529 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5530 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5531 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5532 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005533
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005534 if (opt.jo_term_name == NULL)
5535 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005536 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005537
Bram Moolenaar51e14382019-05-25 20:21:28 +02005538 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005539 if (fname_tofree != NULL)
5540 {
5541 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5542 opt.jo_term_name = fname_tofree;
5543 }
5544 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005545
Bram Moolenaar87abab92019-06-03 21:14:59 +02005546 if (opt.jo_bufnr_buf != NULL)
5547 {
5548 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5549
5550 // With "bufnr" argument: enter the window with this buffer and make it
5551 // empty.
5552 if (wp == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005553 semsg(_(e_invalid_argument_str), "bufnr");
Bram Moolenaar87abab92019-06-03 21:14:59 +02005554 else
5555 {
5556 buf = curbuf;
5557 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005558 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005559 free_scrollback(curbuf->b_term);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005560 redraw_later(UPD_NOT_VALID);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005561 }
5562 }
5563 else
5564 // Create a new terminal window.
5565 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5566
Bram Moolenaard96ff162018-02-18 22:13:29 +01005567 if (buf != NULL && buf->b_term != NULL)
5568 {
5569 int i;
5570 linenr_T bot_lnum;
5571 linenr_T lnum;
5572 term_T *term = buf->b_term;
5573 int width;
5574 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005575 VTermPos cursor_pos1;
5576 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005577
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005578 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005579
Bram Moolenaard96ff162018-02-18 22:13:29 +01005580 rettv->vval.v_number = buf->b_fnum;
5581
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005582 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005583 width = read_dump_file(fd1, &cursor_pos1);
5584
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005585 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005586 if (cursor_pos1.row >= 0)
5587 {
5588 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5589 coladvance(cursor_pos1.col);
5590 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005591
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005592 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005593 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005594
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005595 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005596 if (!do_diff)
5597 goto theend;
5598
5599 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5600
Bram Moolenaar4a696342018-04-05 18:45:26 +02005601 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005602 if (textline == NULL)
5603 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005604 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5605 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5606 vim_free(textline);
5607
5608 textline = get_separator(width, fname2);
5609 if (textline == NULL)
5610 goto theend;
5611 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5612 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005613 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005614
5615 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005616 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005617 if (width2 > width)
5618 {
5619 vim_free(textline);
5620 textline = alloc(width2 + 1);
5621 if (textline == NULL)
5622 goto theend;
5623 width = width2;
5624 textline[width] = NUL;
5625 }
5626 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5627
5628 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5629 {
5630 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5631 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005632 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005633 for (i = 0; i < width; ++i)
5634 textline[i] = '-';
5635 }
5636 else
5637 {
5638 char_u *line1;
5639 char_u *line2;
5640 char_u *p1;
5641 char_u *p2;
5642 int col;
5643 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5644 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5645 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5646 ->sb_cells;
5647
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005648 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005649 line1 = vim_strsave(ml_get(lnum));
5650 if (line1 == NULL)
5651 break;
5652 p1 = line1;
5653
5654 line2 = ml_get(lnum + bot_lnum);
5655 p2 = line2;
5656 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5657 {
5658 int len1 = utfc_ptr2len(p1);
5659 int len2 = utfc_ptr2len(p2);
5660
5661 textline[col] = ' ';
5662 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005663 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005664 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005665 else if (lnum == cursor_pos1.row + 1
5666 && col == cursor_pos1.col
5667 && (cursor_pos1.row != cursor_pos2.row
5668 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005669 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005670 textline[col] = '>';
5671 else if (lnum == cursor_pos2.row + 1
5672 && col == cursor_pos2.col
5673 && (cursor_pos1.row != cursor_pos2.row
5674 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005675 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005676 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005677 else if (cellattr1 != NULL && cellattr2 != NULL)
5678 {
5679 if ((cellattr1 + col)->width
5680 != (cellattr2 + col)->width)
5681 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005682 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005683 &(cellattr2 + col)->fg))
5684 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005685 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005686 &(cellattr2 + col)->bg))
5687 textline[col] = 'b';
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005688 else if (vtermAttr2hl(&(cellattr1 + col)->attrs)
5689 != vtermAttr2hl(&((cellattr2 + col)->attrs)))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005690 textline[col] = 'a';
5691 }
5692 p1 += len1;
5693 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005694 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005695 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005696
5697 while (col < width)
5698 {
5699 if (*p1 == NUL && *p2 == NUL)
5700 textline[col] = '?';
5701 else if (*p1 == NUL)
5702 {
5703 textline[col] = '+';
5704 p2 += utfc_ptr2len(p2);
5705 }
5706 else
5707 {
5708 textline[col] = '-';
5709 p1 += utfc_ptr2len(p1);
5710 }
5711 ++col;
5712 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005713
5714 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005715 }
5716 if (add_empty_scrollback(term, &term->tl_default_color,
5717 term->tl_top_diff_rows) == OK)
5718 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5719 ++bot_lnum;
5720 }
5721
5722 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5723 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005724 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005725 for (i = 0; i < width; ++i)
5726 textline[i] = '+';
5727 if (add_empty_scrollback(term, &term->tl_default_color,
5728 term->tl_top_diff_rows) == OK)
5729 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5730 ++lnum;
5731 ++bot_lnum;
5732 }
5733
5734 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005735
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005736 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005737 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005738 }
5739
5740theend:
5741 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005742 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005743 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005744 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005745 fclose(fd2);
5746}
5747
5748/*
5749 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5750 * bottom files.
5751 * Return FAIL when this is not possible.
5752 */
5753 int
5754term_swap_diff()
5755{
5756 term_T *term = curbuf->b_term;
5757 linenr_T line_count;
5758 linenr_T top_rows;
5759 linenr_T bot_rows;
5760 linenr_T bot_start;
5761 linenr_T lnum;
5762 char_u *p;
5763 sb_line_T *sb_line;
5764
5765 if (term == NULL
5766 || !term_is_finished(curbuf)
5767 || term->tl_top_diff_rows == 0
5768 || term->tl_scrollback.ga_len == 0)
5769 return FAIL;
5770
5771 line_count = curbuf->b_ml.ml_line_count;
5772 top_rows = term->tl_top_diff_rows;
5773 bot_rows = term->tl_bot_diff_rows;
5774 bot_start = line_count - bot_rows;
5775 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5776
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005777 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005778 for (lnum = 1; lnum <= top_rows; ++lnum)
5779 {
5780 p = vim_strsave(ml_get(1));
5781 if (p == NULL)
5782 return OK;
5783 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005784 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005785 vim_free(p);
5786 }
5787
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005788 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005789 for (lnum = 1; lnum <= bot_rows; ++lnum)
5790 {
5791 p = vim_strsave(ml_get(bot_start + lnum));
5792 if (p == NULL)
5793 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005794 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005795 ml_append(lnum - 1, p, 0, FALSE);
5796 vim_free(p);
5797 }
5798
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005799 // move top title to bottom
5800 p = vim_strsave(ml_get(bot_rows + 1));
5801 if (p == NULL)
5802 return OK;
5803 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005804 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005805 vim_free(p);
5806
5807 // move bottom title to top
5808 p = vim_strsave(ml_get(line_count - top_rows));
5809 if (p == NULL)
5810 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005811 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005812 ml_append(bot_rows, p, 0, FALSE);
5813 vim_free(p);
5814
Bram Moolenaard96ff162018-02-18 22:13:29 +01005815 if (top_rows == bot_rows)
5816 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005817 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005818 for (lnum = 0; lnum < top_rows; ++lnum)
5819 {
5820 sb_line_T temp;
5821
5822 temp = *(sb_line + lnum);
5823 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5824 *(sb_line + bot_start + lnum) = temp;
5825 }
5826 }
5827 else
5828 {
5829 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005830 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005831
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005832 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005833 if (temp != NULL)
5834 {
5835 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5836 mch_memmove(term->tl_scrollback.ga_data,
5837 temp + bot_start,
5838 sizeof(sb_line_T) * bot_rows);
5839 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5840 temp + top_rows,
5841 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5842 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5843 + line_count - top_rows,
5844 temp,
5845 sizeof(sb_line_T) * top_rows);
5846 vim_free(temp);
5847 }
5848 }
5849
5850 term->tl_top_diff_rows = bot_rows;
5851 term->tl_bot_diff_rows = top_rows;
5852
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005853 update_screen(UPD_NOT_VALID);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005854 return OK;
5855}
5856
5857/*
5858 * "term_dumpdiff(filename, filename, options)" function
5859 */
5860 void
5861f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5862{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005863 if (in_vim9script()
5864 && (check_for_string_arg(argvars, 0) == FAIL
5865 || check_for_string_arg(argvars, 1) == FAIL
5866 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5867 return;
5868
Bram Moolenaard96ff162018-02-18 22:13:29 +01005869 term_load_dump(argvars, rettv, TRUE);
5870}
5871
5872/*
5873 * "term_dumpload(filename, options)" function
5874 */
5875 void
5876f_term_dumpload(typval_T *argvars, typval_T *rettv)
5877{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005878 if (in_vim9script()
5879 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005880 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005881 return;
5882
Bram Moolenaard96ff162018-02-18 22:13:29 +01005883 term_load_dump(argvars, rettv, FALSE);
5884}
5885
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005886/*
5887 * "term_getaltscreen(buf)" function
5888 */
5889 void
5890f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5891{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005892 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005893
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005894 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5895 return;
5896
5897 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005898 if (buf == NULL)
5899 return;
5900 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5901}
5902
5903/*
5904 * "term_getattr(attr, name)" function
5905 */
5906 void
5907f_term_getattr(typval_T *argvars, typval_T *rettv)
5908{
5909 int attr;
5910 size_t i;
5911 char_u *name;
5912
5913 static struct {
5914 char *name;
5915 int attr;
5916 } attrs[] = {
5917 {"bold", HL_BOLD},
5918 {"italic", HL_ITALIC},
5919 {"underline", HL_UNDERLINE},
5920 {"strike", HL_STRIKETHROUGH},
5921 {"reverse", HL_INVERSE},
5922 };
5923
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005924 if (in_vim9script()
5925 && (check_for_number_arg(argvars, 0) == FAIL
5926 || check_for_string_arg(argvars, 1) == FAIL))
5927 return;
5928
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005929 attr = tv_get_number(&argvars[0]);
5930 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005931 if (name == NULL)
5932 return;
5933
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005934 if (attr > HL_ALL)
5935 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02005936 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005937 if (STRCMP(name, attrs[i].name) == 0)
5938 {
5939 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5940 break;
5941 }
5942}
5943
5944/*
5945 * "term_getcursor(buf)" function
5946 */
5947 void
5948f_term_getcursor(typval_T *argvars, typval_T *rettv)
5949{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005950 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005951 term_T *term;
5952 list_T *l;
5953 dict_T *d;
5954
5955 if (rettv_list_alloc(rettv) == FAIL)
5956 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005957
5958 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5959 return;
5960
5961 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005962 if (buf == NULL)
5963 return;
5964 term = buf->b_term;
5965
5966 l = rettv->vval.v_list;
5967 list_append_number(l, term->tl_cursor_pos.row + 1);
5968 list_append_number(l, term->tl_cursor_pos.col + 1);
5969
5970 d = dict_alloc();
5971 if (d != NULL)
5972 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005973 dict_add_number(d, "visible", term->tl_cursor_visible);
5974 dict_add_number(d, "blink", blink_state_is_inverted()
5975 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5976 dict_add_number(d, "shape", term->tl_cursor_shape);
5977 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005978 list_append_dict(l, d);
5979 }
5980}
5981
5982/*
5983 * "term_getjob(buf)" function
5984 */
5985 void
5986f_term_getjob(typval_T *argvars, typval_T *rettv)
5987{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005988 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005989
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005990 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5991 return;
5992
5993 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005994 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005995 {
5996 rettv->v_type = VAR_SPECIAL;
5997 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005998 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005999 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006000
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01006001 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006002 rettv->vval.v_job = buf->b_term->tl_job;
6003 if (rettv->vval.v_job != NULL)
6004 ++rettv->vval.v_job->jv_refcount;
6005}
6006
6007 static int
6008get_row_number(typval_T *tv, term_T *term)
6009{
6010 if (tv->v_type == VAR_STRING
6011 && tv->vval.v_string != NULL
6012 && STRCMP(tv->vval.v_string, ".") == 0)
6013 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006014 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006015}
6016
6017/*
6018 * "term_getline(buf, row)" function
6019 */
6020 void
6021f_term_getline(typval_T *argvars, typval_T *rettv)
6022{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006023 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006024 term_T *term;
6025 int row;
6026
6027 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006028
6029 if (in_vim9script()
6030 && (check_for_buffer_arg(argvars, 0) == FAIL
6031 || check_for_lnum_arg(argvars, 1) == FAIL))
6032 return;
6033
6034 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006035 if (buf == NULL)
6036 return;
6037 term = buf->b_term;
6038 row = get_row_number(&argvars[1], term);
6039
6040 if (term->tl_vterm == NULL)
6041 {
6042 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
6043
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006044 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006045 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
6046 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
6047 }
6048 else
6049 {
6050 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
6051 VTermRect rect;
6052 int len;
6053 char_u *p;
6054
6055 if (row < 0 || row >= term->tl_rows)
6056 return;
6057 len = term->tl_cols * MB_MAXBYTES + 1;
6058 p = alloc(len);
6059 if (p == NULL)
6060 return;
6061 rettv->vval.v_string = p;
6062
6063 rect.start_col = 0;
6064 rect.end_col = term->tl_cols;
6065 rect.start_row = row;
6066 rect.end_row = row + 1;
6067 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
6068 }
6069}
6070
6071/*
6072 * "term_getscrolled(buf)" function
6073 */
6074 void
6075f_term_getscrolled(typval_T *argvars, typval_T *rettv)
6076{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006077 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006078
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006079 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6080 return;
6081
6082 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006083 if (buf == NULL)
6084 return;
6085 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
6086}
6087
6088/*
6089 * "term_getsize(buf)" function
6090 */
6091 void
6092f_term_getsize(typval_T *argvars, typval_T *rettv)
6093{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006094 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006095 list_T *l;
6096
6097 if (rettv_list_alloc(rettv) == FAIL)
6098 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006099
6100 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6101 return;
6102
6103 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006104 if (buf == NULL)
6105 return;
6106
6107 l = rettv->vval.v_list;
6108 list_append_number(l, buf->b_term->tl_rows);
6109 list_append_number(l, buf->b_term->tl_cols);
6110}
6111
6112/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02006113 * "term_setsize(buf, rows, cols)" function
6114 */
6115 void
6116f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6117{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006118 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02006119 term_T *term;
6120 varnumber_T rows, cols;
6121
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006122 if (in_vim9script()
6123 && (check_for_buffer_arg(argvars, 0) == FAIL
6124 || check_for_number_arg(argvars, 1) == FAIL
6125 || check_for_number_arg(argvars, 2) == FAIL))
6126 return;
6127
6128 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02006129 if (buf == NULL)
6130 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006131 emsg(_(e_not_terminal_buffer));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02006132 return;
6133 }
6134 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02006135 return;
6136 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006137 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02006138 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006139 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02006140 cols = cols <= 0 ? term->tl_cols : cols;
6141 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006142 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02006143
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006144 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02006145 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
6146 term_report_winsize(term, term->tl_rows, term->tl_cols);
6147}
6148
6149/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006150 * "term_getstatus(buf)" function
6151 */
6152 void
6153f_term_getstatus(typval_T *argvars, typval_T *rettv)
6154{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006155 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006156 term_T *term;
6157 char_u val[100];
6158
6159 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006160
6161 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6162 return;
6163
6164 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006165 if (buf == NULL)
6166 return;
6167 term = buf->b_term;
6168
6169 if (term_job_running(term))
6170 STRCPY(val, "running");
6171 else
6172 STRCPY(val, "finished");
6173 if (term->tl_normal_mode)
6174 STRCAT(val, ",normal");
6175 rettv->vval.v_string = vim_strsave(val);
6176}
6177
6178/*
6179 * "term_gettitle(buf)" function
6180 */
6181 void
6182f_term_gettitle(typval_T *argvars, typval_T *rettv)
6183{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006184 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006185
6186 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006187
6188 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6189 return;
6190
6191 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006192 if (buf == NULL)
6193 return;
6194
6195 if (buf->b_term->tl_title != NULL)
6196 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
6197}
6198
6199/*
6200 * "term_gettty(buf)" function
6201 */
6202 void
6203f_term_gettty(typval_T *argvars, typval_T *rettv)
6204{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006205 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02006206 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006207 int num = 0;
6208
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006209 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006210 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006211 || check_for_opt_bool_arg(argvars, 1) == FAIL))
6212 return;
6213
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006214 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006215 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006216 if (buf == NULL)
6217 return;
6218 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02006219 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006220
6221 switch (num)
6222 {
6223 case 0:
6224 if (buf->b_term->tl_job != NULL)
6225 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006226 break;
6227 case 1:
6228 if (buf->b_term->tl_job != NULL)
6229 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006230 break;
6231 default:
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006232 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006233 return;
6234 }
6235 if (p != NULL)
6236 rettv->vval.v_string = vim_strsave(p);
6237}
6238
6239/*
6240 * "term_list()" function
6241 */
6242 void
6243f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
6244{
6245 term_T *tp;
6246 list_T *l;
6247
6248 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
6249 return;
6250
6251 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006252 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02006253 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006254 if (list_append_number(l,
6255 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6256 return;
6257}
6258
6259/*
6260 * "term_scrape(buf, row)" function
6261 */
6262 void
6263f_term_scrape(typval_T *argvars, typval_T *rettv)
6264{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006265 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006266 VTermScreen *screen = NULL;
6267 VTermPos pos;
6268 list_T *l;
6269 term_T *term;
6270 char_u *p;
6271 sb_line_T *line;
6272
6273 if (rettv_list_alloc(rettv) == FAIL)
6274 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006275
6276 if (in_vim9script()
6277 && (check_for_buffer_arg(argvars, 0) == FAIL
6278 || check_for_lnum_arg(argvars, 1) == FAIL))
6279 return;
6280
6281 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006282 if (buf == NULL)
6283 return;
6284 term = buf->b_term;
6285
6286 l = rettv->vval.v_list;
6287 pos.row = get_row_number(&argvars[1], term);
6288
6289 if (term->tl_vterm != NULL)
6290 {
6291 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006292 if (screen == NULL) // can't really happen
6293 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006294 p = NULL;
6295 line = NULL;
6296 }
6297 else
6298 {
6299 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6300
6301 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6302 return;
6303 p = ml_get_buf(buf, lnum + 1, FALSE);
6304 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6305 }
6306
6307 for (pos.col = 0; pos.col < term->tl_cols; )
6308 {
6309 dict_T *dcell;
6310 int width;
6311 VTermScreenCellAttrs attrs;
6312 VTermColor fg, bg;
6313 char_u rgb[8];
6314 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6315 int off = 0;
6316 int i;
6317
6318 if (screen == NULL)
6319 {
6320 cellattr_T *cellattr;
6321 int len;
6322
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006323 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006324 if (pos.col >= line->sb_cols)
6325 break;
6326 cellattr = line->sb_cells + pos.col;
6327 width = cellattr->width;
6328 attrs = cellattr->attrs;
6329 fg = cellattr->fg;
6330 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006331 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006332 mch_memmove(mbs, p, len);
6333 mbs[len] = NUL;
6334 p += len;
6335 }
6336 else
6337 {
6338 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006339
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006340 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6341 break;
6342 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6343 {
6344 if (cell.chars[i] == 0)
6345 break;
6346 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6347 }
6348 mbs[off] = NUL;
6349 width = cell.width;
6350 attrs = cell.attrs;
6351 fg = cell.fg;
6352 bg = cell.bg;
6353 }
6354 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006355 if (dcell == NULL)
6356 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006357 list_append_dict(l, dcell);
6358
Bram Moolenaare0be1672018-07-08 16:50:37 +02006359 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006360
6361 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6362 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006363 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006364 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6365 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006366 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006367
Bram Moolenaar87fd0922021-11-20 13:47:45 +00006368 dict_add_number(dcell, "attr",
6369 cell2attr(term, NULL, &attrs, &fg, &bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006370 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006371
6372 ++pos.col;
6373 if (width == 2)
6374 ++pos.col;
6375 }
6376}
6377
6378/*
6379 * "term_sendkeys(buf, keys)" function
6380 */
6381 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006382f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006383{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006384 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006385 char_u *msg;
6386 term_T *term;
6387
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006388 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006389 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006390 || check_for_string_arg(argvars, 1) == FAIL))
6391 return;
6392
6393 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006394 if (buf == NULL)
6395 return;
6396
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006397 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006398 if (msg == NULL)
6399 return;
6400 term = buf->b_term;
6401 if (term->tl_vterm == NULL)
6402 return;
6403
6404 while (*msg != NUL)
6405 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006406 int c;
6407
6408 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6409 {
6410 c = TO_SPECIAL(msg[1], msg[2]);
6411 msg += 3;
6412 }
6413 else
6414 {
6415 c = PTR2CHAR(msg);
6416 msg += MB_CPTR2LEN(msg);
6417 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006418 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006419 }
6420}
6421
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006422#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6423/*
6424 * "term_getansicolors(buf)" function
6425 */
6426 void
6427f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6428{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006429 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006430 term_T *term;
6431 VTermState *state;
6432 VTermColor color;
6433 char_u hexbuf[10];
6434 int index;
6435 list_T *list;
6436
6437 if (rettv_list_alloc(rettv) == FAIL)
6438 return;
6439
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006440 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6441 return;
6442
6443 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006444 if (buf == NULL)
6445 return;
6446 term = buf->b_term;
6447 if (term->tl_vterm == NULL)
6448 return;
6449
6450 list = rettv->vval.v_list;
6451 state = vterm_obtain_state(term->tl_vterm);
6452 for (index = 0; index < 16; index++)
6453 {
6454 vterm_state_get_palette_color(state, index, &color);
6455 sprintf((char *)hexbuf, "#%02x%02x%02x",
6456 color.red, color.green, color.blue);
6457 if (list_append_string(list, hexbuf, 7) == FAIL)
6458 return;
6459 }
6460}
6461
6462/*
6463 * "term_setansicolors(buf, list)" function
6464 */
6465 void
6466f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6467{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006468 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006469 term_T *term;
LemonBoyb2b3acb2022-05-20 10:10:34 +01006470 listitem_T *li;
6471 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006472
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006473 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006474 && (check_for_buffer_arg(argvars, 0) == FAIL
6475 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006476 return;
6477
6478 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006479 if (buf == NULL)
6480 return;
6481 term = buf->b_term;
6482 if (term->tl_vterm == NULL)
6483 return;
6484
Bram Moolenaard83392a2022-09-01 12:22:46 +01006485 if (check_for_nonnull_list_arg(argvars, 1) == FAIL)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006486 return;
Bram Moolenaard83392a2022-09-01 12:22:46 +01006487
LemonBoyb2b3acb2022-05-20 10:10:34 +01006488 if (argvars[1].vval.v_list->lv_first == &range_list_item
6489 || argvars[1].vval.v_list->lv_len != 16)
6490 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006491 emsg(_(e_invalid_argument));
LemonBoyb2b3acb2022-05-20 10:10:34 +01006492 return;
6493 }
6494
6495 if (term->tl_palette == NULL)
6496 term->tl_palette = ALLOC_MULT(long_u, 16);
6497 if (term->tl_palette == NULL)
6498 return;
6499
6500 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
6501 {
6502 char_u *color_name;
6503 guicolor_T guicolor;
6504
6505 color_name = tv_get_string_chk(&li->li_tv);
6506 if (color_name == NULL)
6507 return;
6508
6509 guicolor = GUI_GET_COLOR(color_name);
6510 if (guicolor == INVALCOLOR)
6511 {
6512 semsg(_(e_cannot_allocate_color_str), color_name);
6513 return;
6514 }
6515
6516 term->tl_palette[n++] = GUI_MCH_GET_RGB(guicolor);
6517 }
6518
6519 term_update_palette(term);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006520}
6521#endif
6522
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006523/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006524 * "term_setapi(buf, api)" function
6525 */
6526 void
6527f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6528{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006529 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006530 term_T *term;
6531 char_u *api;
6532
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006533 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006534 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006535 || check_for_string_arg(argvars, 1) == FAIL))
6536 return;
6537
6538 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006539 if (buf == NULL)
6540 return;
6541 term = buf->b_term;
6542 vim_free(term->tl_api);
6543 api = tv_get_string_chk(&argvars[1]);
6544 if (api != NULL)
6545 term->tl_api = vim_strsave(api);
6546 else
6547 term->tl_api = NULL;
6548}
6549
6550/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006551 * "term_setrestore(buf, command)" function
6552 */
6553 void
6554f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6555{
6556#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006557 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006558 term_T *term;
6559 char_u *cmd;
6560
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006561 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006562 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006563 || check_for_string_arg(argvars, 1) == FAIL))
6564 return;
6565
6566 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006567 if (buf == NULL)
6568 return;
6569 term = buf->b_term;
6570 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006571 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006572 if (cmd != NULL)
6573 term->tl_command = vim_strsave(cmd);
6574 else
6575 term->tl_command = NULL;
6576#endif
6577}
6578
6579/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006580 * "term_setkill(buf, how)" function
6581 */
6582 void
6583f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6584{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006585 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006586 term_T *term;
6587 char_u *how;
6588
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006589 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006590 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006591 || check_for_string_arg(argvars, 1) == FAIL))
6592 return;
6593
6594 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006595 if (buf == NULL)
6596 return;
6597 term = buf->b_term;
6598 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006599 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006600 if (how != NULL)
6601 term->tl_kill = vim_strsave(how);
6602 else
6603 term->tl_kill = NULL;
6604}
6605
6606/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006607 * "term_start(command, options)" function
6608 */
6609 void
6610f_term_start(typval_T *argvars, typval_T *rettv)
6611{
6612 jobopt_T opt;
6613 buf_T *buf;
6614
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006615 if (in_vim9script()
6616 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6617 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6618 return;
6619
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006620 init_job_options(&opt);
6621 if (argvars[1].v_type != VAR_UNKNOWN
6622 && get_job_options(&argvars[1], &opt,
6623 JO_TIMEOUT_ALL + JO_STOPONEXIT
6624 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6625 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6626 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6627 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006628 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006629 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006630 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006631 return;
6632
Bram Moolenaar13568252018-03-16 20:46:58 +01006633 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006634
6635 if (buf != NULL && buf->b_term != NULL)
6636 rettv->vval.v_number = buf->b_fnum;
6637}
6638
6639/*
6640 * "term_wait" function
6641 */
6642 void
6643f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6644{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006645 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006646
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006647 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006648 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006649 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006650 return;
6651
6652 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006653 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006654 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006655 if (buf->b_term->tl_job == NULL)
6656 {
6657 ch_log(NULL, "term_wait(): no job to wait for");
6658 return;
6659 }
6660 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006661 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006662 return;
6663
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006664 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006665 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006666 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6667 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006668 // The job is dead, keep reading channel I/O until the channel is
6669 // closed. buf->b_term may become NULL if the terminal was closed while
6670 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006671 ch_log(NULL, "term_wait(): waiting for channel to close");
6672 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6673 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006674 term_flush_messages();
6675
Bram Moolenaard45aa552018-05-21 22:50:29 +02006676 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006677 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006678 // If the terminal is closed when the channel is closed the
6679 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006680 break;
Bram Moolenaareea32af2021-11-21 14:51:13 +00006681 if (buf->b_term == NULL || buf->b_term->tl_channel_closing)
6682 // came here from a close callback, only wait one time
6683 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006684 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006685
6686 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006687 }
6688 else
6689 {
6690 long wait = 10L;
6691
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006692 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006693
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006694 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006695 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006696 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006697 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006698
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006699 // Flushing messages on channels is hopefully sufficient.
6700 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006701 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006702 }
6703}
6704
6705/*
6706 * Called when a channel has sent all the lines to a terminal.
6707 * Send a CTRL-D to mark the end of the text.
6708 */
6709 void
6710term_send_eof(channel_T *ch)
6711{
6712 term_T *term;
6713
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006714 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006715 if (term->tl_job == ch->ch_job)
6716 {
6717 if (term->tl_eof_chars != NULL)
6718 {
6719 channel_send(ch, PART_IN, term->tl_eof_chars,
6720 (int)STRLEN(term->tl_eof_chars), NULL);
6721 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6722 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006723# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006724 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006725 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006726 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6727# endif
6728 }
6729}
6730
Bram Moolenaar113e1072019-01-20 15:30:40 +01006731#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006732 job_T *
6733term_getjob(term_T *term)
6734{
6735 return term != NULL ? term->tl_job : NULL;
6736}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006737#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006738
Bram Moolenaar4f974752019-02-17 17:44:42 +01006739# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006740
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006741///////////////////////////////////////
6742// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006743#ifdef PROTO
6744typedef int COORD;
6745typedef int DWORD;
6746typedef int HANDLE;
6747typedef int *DWORD_PTR;
6748typedef int HPCON;
6749typedef int HRESULT;
6750typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006751typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006752typedef int PSIZE_T;
6753typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006754typedef int BOOL;
6755# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006756#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006757
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006758HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6759HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6760HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006761BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6762BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6763void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006764
6765 static int
6766dyn_conpty_init(int verbose)
6767{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006768 static HMODULE hKerneldll = NULL;
6769 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006770 static struct
6771 {
6772 char *name;
6773 FARPROC *ptr;
6774 } conpty_entry[] =
6775 {
6776 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6777 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6778 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6779 {"InitializeProcThreadAttributeList",
6780 (FARPROC*)&pInitializeProcThreadAttributeList},
6781 {"UpdateProcThreadAttribute",
6782 (FARPROC*)&pUpdateProcThreadAttribute},
6783 {"DeleteProcThreadAttributeList",
6784 (FARPROC*)&pDeleteProcThreadAttributeList},
6785 {NULL, NULL}
6786 };
6787
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006788 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006789 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006790 if (verbose)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006791 emsg(_(e_conpty_is_not_available));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006792 return FAIL;
6793 }
6794
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006795 // No need to initialize twice.
6796 if (hKerneldll)
6797 return OK;
6798
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006799 hKerneldll = vimLoadLib("kernel32.dll");
6800 for (i = 0; conpty_entry[i].name != NULL
6801 && conpty_entry[i].ptr != NULL; ++i)
6802 {
6803 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6804 conpty_entry[i].name)) == NULL)
6805 {
6806 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006807 semsg(_(e_could_not_load_library_function_str), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006808 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006809 return FAIL;
6810 }
6811 }
6812
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006813 return OK;
6814}
6815
6816 static int
6817conpty_term_and_job_init(
6818 term_T *term,
6819 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006820 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006821 jobopt_T *opt,
6822 jobopt_T *orig_opt)
6823{
6824 WCHAR *cmd_wchar = NULL;
6825 WCHAR *cmd_wchar_copy = NULL;
6826 WCHAR *cwd_wchar = NULL;
6827 WCHAR *env_wchar = NULL;
6828 channel_T *channel = NULL;
6829 job_T *job = NULL;
6830 HANDLE jo = NULL;
6831 garray_T ga_cmd, ga_env;
6832 char_u *cmd = NULL;
6833 HRESULT hr;
6834 COORD consize;
6835 SIZE_T breq;
6836 PROCESS_INFORMATION proc_info;
6837 HANDLE i_theirs = NULL;
6838 HANDLE o_theirs = NULL;
6839 HANDLE i_ours = NULL;
6840 HANDLE o_ours = NULL;
6841
Bram Moolenaar04935fb2022-01-08 16:19:22 +00006842 ga_init2(&ga_cmd, sizeof(char*), 20);
6843 ga_init2(&ga_env, sizeof(char*), 20);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006844
6845 if (argvar->v_type == VAR_STRING)
6846 {
6847 cmd = argvar->vval.v_string;
6848 }
6849 else if (argvar->v_type == VAR_LIST)
6850 {
6851 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6852 goto failed;
6853 cmd = ga_cmd.ga_data;
6854 }
6855 if (cmd == NULL || *cmd == NUL)
6856 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006857 emsg(_(e_invalid_argument));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006858 goto failed;
6859 }
6860
6861 term->tl_arg0_cmd = vim_strsave(cmd);
6862
6863 cmd_wchar = enc_to_utf16(cmd, NULL);
6864
6865 if (cmd_wchar != NULL)
6866 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006867 // Request by CreateProcessW
6868 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006869 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006870 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6871 }
6872
6873 ga_clear(&ga_cmd);
6874 if (cmd_wchar == NULL)
6875 goto failed;
6876 if (opt->jo_cwd != NULL)
6877 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6878
6879 win32_build_env(opt->jo_env, &ga_env, TRUE);
6880 env_wchar = ga_env.ga_data;
6881
6882 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6883 goto failed;
6884 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6885 goto failed;
6886
6887 consize.X = term->tl_cols;
6888 consize.Y = term->tl_rows;
6889 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6890 &term->tl_conpty);
6891 if (FAILED(hr))
6892 goto failed;
6893
6894 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6895
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006896 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006897 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006898 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006899 if (!term->tl_siex.lpAttributeList)
6900 goto failed;
6901 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6902 0, &breq))
6903 goto failed;
6904 if (!pUpdateProcThreadAttribute(
6905 term->tl_siex.lpAttributeList, 0,
6906 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6907 sizeof(HPCON), NULL, NULL))
6908 goto failed;
6909
6910 channel = add_channel();
6911 if (channel == NULL)
6912 goto failed;
6913
6914 job = job_alloc();
6915 if (job == NULL)
6916 goto failed;
6917 if (argvar->v_type == VAR_STRING)
6918 {
6919 int argc;
6920
6921 build_argv_from_string(cmd, &job->jv_argv, &argc);
6922 }
6923 else
6924 {
6925 int argc;
6926
6927 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6928 }
6929
6930 if (opt->jo_set & JO_IN_BUF)
6931 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6932
6933 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6934 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006935 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006936 env_wchar, cwd_wchar,
6937 &term->tl_siex.StartupInfo, &proc_info))
6938 goto failed;
6939
6940 CloseHandle(i_theirs);
6941 CloseHandle(o_theirs);
6942
6943 channel_set_pipes(channel,
6944 (sock_T)i_ours,
6945 (sock_T)o_ours,
6946 (sock_T)o_ours);
6947
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006948 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006949 channel->ch_write_text_mode = TRUE;
6950
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006951 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006952 channel->ch_anonymous_pipe = TRUE;
6953
6954 jo = CreateJobObject(NULL, NULL);
6955 if (jo == NULL)
6956 goto failed;
6957
6958 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6959 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006960 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006961 CloseHandle(jo);
6962 jo = NULL;
6963 }
6964
6965 ResumeThread(proc_info.hThread);
6966 CloseHandle(proc_info.hThread);
6967
6968 vim_free(cmd_wchar);
6969 vim_free(cmd_wchar_copy);
6970 vim_free(cwd_wchar);
6971 vim_free(env_wchar);
6972
6973 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6974 goto failed;
6975
Bram Moolenaar30b9a412022-05-26 14:06:37 +01006976#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
LemonBoyb2b3acb2022-05-20 10:10:34 +01006977 if (term_use_palette())
6978 {
6979 if (term->tl_palette != NULL)
6980 set_vterm_palette(term->tl_vterm, term->tl_palette);
6981 else
6982 init_vterm_ansi_colors(term->tl_vterm);
6983 }
Bram Moolenaar30b9a412022-05-26 14:06:37 +01006984#endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006985
6986 channel_set_job(channel, job, opt);
6987 job_set_options(job, opt);
6988
6989 job->jv_channel = channel;
6990 job->jv_proc_info = proc_info;
6991 job->jv_job_object = jo;
6992 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006993 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006994 ++job->jv_refcount;
6995 term->tl_job = job;
6996
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006997 // Redirecting stdout and stderr doesn't work at the job level. Instead
6998 // open the file here and handle it in. opt->jo_io was changed in
6999 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007000 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7001 {
7002 char_u *fname = opt->jo_io_name[PART_OUT];
7003
7004 ch_log(channel, "Opening output file %s", fname);
7005 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7006 if (term->tl_out_fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007007 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007008 }
7009
7010 return OK;
7011
7012failed:
7013 ga_clear(&ga_cmd);
7014 ga_clear(&ga_env);
7015 vim_free(cmd_wchar);
7016 vim_free(cmd_wchar_copy);
7017 vim_free(cwd_wchar);
7018 if (channel != NULL)
7019 channel_clear(channel);
7020 if (job != NULL)
7021 {
7022 job->jv_channel = NULL;
7023 job_cleanup(job);
7024 }
7025 term->tl_job = NULL;
7026 if (jo != NULL)
7027 CloseHandle(jo);
7028
7029 if (term->tl_siex.lpAttributeList != NULL)
7030 {
7031 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
7032 vim_free(term->tl_siex.lpAttributeList);
7033 }
7034 term->tl_siex.lpAttributeList = NULL;
7035 if (o_theirs != NULL)
7036 CloseHandle(o_theirs);
7037 if (o_ours != NULL)
7038 CloseHandle(o_ours);
7039 if (i_ours != NULL)
7040 CloseHandle(i_ours);
7041 if (i_theirs != NULL)
7042 CloseHandle(i_theirs);
7043 if (term->tl_conpty != NULL)
7044 pClosePseudoConsole(term->tl_conpty);
7045 term->tl_conpty = NULL;
7046 return FAIL;
7047}
7048
7049 static void
7050conpty_term_report_winsize(term_T *term, int rows, int cols)
7051{
7052 COORD consize;
7053
7054 consize.X = cols;
7055 consize.Y = rows;
7056 pResizePseudoConsole(term->tl_conpty, consize);
7057}
7058
Bram Moolenaar840d16f2019-09-10 21:27:18 +02007059 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007060term_free_conpty(term_T *term)
7061{
7062 if (term->tl_siex.lpAttributeList != NULL)
7063 {
7064 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
7065 vim_free(term->tl_siex.lpAttributeList);
7066 }
7067 term->tl_siex.lpAttributeList = NULL;
7068 if (term->tl_conpty != NULL)
7069 pClosePseudoConsole(term->tl_conpty);
7070 term->tl_conpty = NULL;
7071}
7072
7073 int
7074use_conpty(void)
7075{
7076 return has_conpty;
7077}
7078
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007079# ifndef PROTO
7080
7081#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
7082#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01007083#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007084
7085void* (*winpty_config_new)(UINT64, void*);
7086void* (*winpty_open)(void*, void*);
7087void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
7088BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
7089void (*winpty_config_set_mouse_mode)(void*, int);
7090void (*winpty_config_set_initial_size)(void*, int, int);
7091LPCWSTR (*winpty_conin_name)(void*);
7092LPCWSTR (*winpty_conout_name)(void*);
7093LPCWSTR (*winpty_conerr_name)(void*);
7094void (*winpty_free)(void*);
7095void (*winpty_config_free)(void*);
7096void (*winpty_spawn_config_free)(void*);
7097void (*winpty_error_free)(void*);
7098LPCWSTR (*winpty_error_msg)(void*);
7099BOOL (*winpty_set_size)(void*, int, int, void*);
7100HANDLE (*winpty_agent_process)(void*);
7101
7102#define WINPTY_DLL "winpty.dll"
7103
7104static HINSTANCE hWinPtyDLL = NULL;
7105# endif
7106
7107 static int
7108dyn_winpty_init(int verbose)
7109{
7110 int i;
7111 static struct
7112 {
7113 char *name;
7114 FARPROC *ptr;
7115 } winpty_entry[] =
7116 {
7117 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
7118 {"winpty_config_free", (FARPROC*)&winpty_config_free},
7119 {"winpty_config_new", (FARPROC*)&winpty_config_new},
7120 {"winpty_config_set_mouse_mode",
7121 (FARPROC*)&winpty_config_set_mouse_mode},
7122 {"winpty_config_set_initial_size",
7123 (FARPROC*)&winpty_config_set_initial_size},
7124 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
7125 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
7126 {"winpty_error_free", (FARPROC*)&winpty_error_free},
7127 {"winpty_free", (FARPROC*)&winpty_free},
7128 {"winpty_open", (FARPROC*)&winpty_open},
7129 {"winpty_spawn", (FARPROC*)&winpty_spawn},
7130 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
7131 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
7132 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
7133 {"winpty_set_size", (FARPROC*)&winpty_set_size},
7134 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
7135 {NULL, NULL}
7136 };
7137
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007138 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007139 if (hWinPtyDLL)
7140 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007141 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
7142 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007143 if (*p_winptydll != NUL)
7144 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
7145 if (!hWinPtyDLL)
7146 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
7147 if (!hWinPtyDLL)
7148 {
7149 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007150 semsg(_(e_could_not_load_library_str_str),
Martin Tournoij1a3e5742021-07-24 13:57:29 +02007151 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
7152 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007153 return FAIL;
7154 }
7155 for (i = 0; winpty_entry[i].name != NULL
7156 && winpty_entry[i].ptr != NULL; ++i)
7157 {
7158 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
7159 winpty_entry[i].name)) == NULL)
7160 {
7161 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007162 semsg(_(e_could_not_load_library_function_str), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01007163 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007164 return FAIL;
7165 }
7166 }
7167
7168 return OK;
7169}
7170
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007171 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007172winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007173 term_T *term,
7174 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007175 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007176 jobopt_T *opt,
7177 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007178{
7179 WCHAR *cmd_wchar = NULL;
7180 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007181 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007182 channel_T *channel = NULL;
7183 job_T *job = NULL;
7184 DWORD error;
7185 HANDLE jo = NULL;
7186 HANDLE child_process_handle;
7187 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01007188 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007189 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007190 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007191 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007192
Bram Moolenaar04935fb2022-01-08 16:19:22 +00007193 ga_init2(&ga_cmd, sizeof(char*), 20);
7194 ga_init2(&ga_env, sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007195
7196 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007197 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007198 cmd = argvar->vval.v_string;
7199 }
7200 else if (argvar->v_type == VAR_LIST)
7201 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007202 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007203 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007204 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007205 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007206 if (cmd == NULL || *cmd == NUL)
7207 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007208 emsg(_(e_invalid_argument));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007209 goto failed;
7210 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007211
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007212 term->tl_arg0_cmd = vim_strsave(cmd);
7213
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007214 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007215 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007216 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007217 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007218 if (opt->jo_cwd != NULL)
7219 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007220
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007221 win32_build_env(opt->jo_env, &ga_env, TRUE);
7222 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007223
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007224 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
7225 if (term->tl_winpty_config == NULL)
7226 goto failed;
7227
7228 winpty_config_set_mouse_mode(term->tl_winpty_config,
7229 WINPTY_MOUSE_MODE_FORCE);
7230 winpty_config_set_initial_size(term->tl_winpty_config,
7231 term->tl_cols, term->tl_rows);
7232 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
7233 if (term->tl_winpty == NULL)
7234 goto failed;
7235
7236 spawn_config = winpty_spawn_config_new(
7237 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
7238 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
7239 NULL,
7240 cmd_wchar,
7241 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007242 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007243 &winpty_err);
7244 if (spawn_config == NULL)
7245 goto failed;
7246
7247 channel = add_channel();
7248 if (channel == NULL)
7249 goto failed;
7250
7251 job = job_alloc();
7252 if (job == NULL)
7253 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02007254 if (argvar->v_type == VAR_STRING)
7255 {
7256 int argc;
7257
7258 build_argv_from_string(cmd, &job->jv_argv, &argc);
7259 }
7260 else
7261 {
7262 int argc;
7263
7264 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
7265 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007266
7267 if (opt->jo_set & JO_IN_BUF)
7268 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
7269
7270 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
7271 &child_thread_handle, &error, &winpty_err))
7272 goto failed;
7273
7274 channel_set_pipes(channel,
7275 (sock_T)CreateFileW(
7276 winpty_conin_name(term->tl_winpty),
7277 GENERIC_WRITE, 0, NULL,
7278 OPEN_EXISTING, 0, NULL),
7279 (sock_T)CreateFileW(
7280 winpty_conout_name(term->tl_winpty),
7281 GENERIC_READ, 0, NULL,
7282 OPEN_EXISTING, 0, NULL),
7283 (sock_T)CreateFileW(
7284 winpty_conerr_name(term->tl_winpty),
7285 GENERIC_READ, 0, NULL,
7286 OPEN_EXISTING, 0, NULL));
7287
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007288 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007289 channel->ch_write_text_mode = TRUE;
7290
7291 jo = CreateJobObject(NULL, NULL);
7292 if (jo == NULL)
7293 goto failed;
7294
7295 if (!AssignProcessToJobObject(jo, child_process_handle))
7296 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007297 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007298 CloseHandle(jo);
7299 jo = NULL;
7300 }
7301
7302 winpty_spawn_config_free(spawn_config);
7303 vim_free(cmd_wchar);
7304 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007305 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007306
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007307 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7308 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007309
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007310#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
LemonBoyb2b3acb2022-05-20 10:10:34 +01007311 if (term_use_palette())
7312 {
7313 if (term->tl_palette != NULL)
7314 set_vterm_palette(term->tl_vterm, term->tl_palette);
7315 else
7316 init_vterm_ansi_colors(term->tl_vterm);
7317 }
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007318#endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007319
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007320 channel_set_job(channel, job, opt);
7321 job_set_options(job, opt);
7322
7323 job->jv_channel = channel;
7324 job->jv_proc_info.hProcess = child_process_handle;
7325 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7326 job->jv_job_object = jo;
7327 job->jv_status = JOB_STARTED;
7328 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007329 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007330 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007331 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007332 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007333 ++job->jv_refcount;
7334 term->tl_job = job;
7335
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007336 // Redirecting stdout and stderr doesn't work at the job level. Instead
7337 // open the file here and handle it in. opt->jo_io was changed in
7338 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007339 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7340 {
7341 char_u *fname = opt->jo_io_name[PART_OUT];
7342
7343 ch_log(channel, "Opening output file %s", fname);
7344 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7345 if (term->tl_out_fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007346 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007347 }
7348
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007349 return OK;
7350
7351failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007352 ga_clear(&ga_cmd);
7353 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007354 vim_free(cmd_wchar);
7355 vim_free(cwd_wchar);
7356 if (spawn_config != NULL)
7357 winpty_spawn_config_free(spawn_config);
7358 if (channel != NULL)
7359 channel_clear(channel);
7360 if (job != NULL)
7361 {
7362 job->jv_channel = NULL;
7363 job_cleanup(job);
7364 }
7365 term->tl_job = NULL;
7366 if (jo != NULL)
7367 CloseHandle(jo);
7368 if (term->tl_winpty != NULL)
7369 winpty_free(term->tl_winpty);
7370 term->tl_winpty = NULL;
7371 if (term->tl_winpty_config != NULL)
7372 winpty_config_free(term->tl_winpty_config);
7373 term->tl_winpty_config = NULL;
7374 if (winpty_err != NULL)
7375 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007376 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007377 (short_u *)winpty_error_msg(winpty_err), NULL);
7378
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007379 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007380 winpty_error_free(winpty_err);
7381 }
7382 return FAIL;
7383}
7384
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007385/*
7386 * Create a new terminal of "rows" by "cols" cells.
7387 * Store a reference in "term".
7388 * Return OK or FAIL.
7389 */
7390 static int
7391term_and_job_init(
7392 term_T *term,
7393 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007394 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007395 jobopt_T *opt,
7396 jobopt_T *orig_opt)
7397{
7398 int use_winpty = FALSE;
7399 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007400 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007401
7402 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7403 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7404
7405 if (!has_winpty && !has_conpty)
7406 // If neither is available give the errors for winpty, since when
7407 // conpty is not available it can't be installed either.
7408 return dyn_winpty_init(TRUE);
7409
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007410 if (opt->jo_tty_type != NUL)
7411 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007412
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007413 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007414 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007415 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007416 use_conpty = TRUE;
7417 else if (has_winpty)
7418 use_winpty = TRUE;
7419 // else: error
7420 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007421 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007422 {
7423 if (has_winpty)
7424 use_winpty = TRUE;
7425 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007426 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007427 {
7428 if (has_conpty)
7429 use_conpty = TRUE;
7430 else
7431 return dyn_conpty_init(TRUE);
7432 }
7433
7434 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007435 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007436
7437 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007438 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007439
7440 // error
7441 return dyn_winpty_init(TRUE);
7442}
7443
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007444 static int
7445create_pty_only(term_T *term, jobopt_T *options)
7446{
7447 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7448 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7449 char in_name[80], out_name[80];
7450 channel_T *channel = NULL;
7451
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007452 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7453 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007454
7455 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7456 GetCurrentProcessId(),
7457 curbuf->b_fnum);
7458 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7459 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7460 PIPE_UNLIMITED_INSTANCES,
7461 0, 0, NMPWAIT_NOWAIT, NULL);
7462 if (hPipeIn == INVALID_HANDLE_VALUE)
7463 goto failed;
7464
7465 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7466 GetCurrentProcessId(),
7467 curbuf->b_fnum);
7468 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7469 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7470 PIPE_UNLIMITED_INSTANCES,
7471 0, 0, 0, NULL);
7472 if (hPipeOut == INVALID_HANDLE_VALUE)
7473 goto failed;
7474
7475 ConnectNamedPipe(hPipeIn, NULL);
7476 ConnectNamedPipe(hPipeOut, NULL);
7477
7478 term->tl_job = job_alloc();
7479 if (term->tl_job == NULL)
7480 goto failed;
7481 ++term->tl_job->jv_refcount;
7482
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007483 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007484 term->tl_job->jv_status = JOB_FINISHED;
7485
7486 channel = add_channel();
7487 if (channel == NULL)
7488 goto failed;
7489 term->tl_job->jv_channel = channel;
7490 channel->ch_keep_open = TRUE;
7491 channel->ch_named_pipe = TRUE;
7492
7493 channel_set_pipes(channel,
7494 (sock_T)hPipeIn,
7495 (sock_T)hPipeOut,
7496 (sock_T)hPipeOut);
7497 channel_set_job(channel, term->tl_job, options);
7498 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7499 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7500
7501 return OK;
7502
7503failed:
7504 if (hPipeIn != NULL)
7505 CloseHandle(hPipeIn);
7506 if (hPipeOut != NULL)
7507 CloseHandle(hPipeOut);
7508 return FAIL;
7509}
7510
7511/*
7512 * Free the terminal emulator part of "term".
7513 */
7514 static void
7515term_free_vterm(term_T *term)
7516{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007517 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007518 if (term->tl_winpty != NULL)
7519 winpty_free(term->tl_winpty);
7520 term->tl_winpty = NULL;
7521 if (term->tl_winpty_config != NULL)
7522 winpty_config_free(term->tl_winpty_config);
7523 term->tl_winpty_config = NULL;
7524 if (term->tl_vterm != NULL)
7525 vterm_free(term->tl_vterm);
7526 term->tl_vterm = NULL;
7527}
7528
7529/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007530 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007531 */
7532 static void
7533term_report_winsize(term_T *term, int rows, int cols)
7534{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007535 if (term->tl_conpty)
7536 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007537 if (term->tl_winpty)
7538 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7539}
7540
7541 int
7542terminal_enabled(void)
7543{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007544 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007545}
7546
7547# else
7548
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007549///////////////////////////////////////
7550// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007551
7552/*
7553 * Create a new terminal of "rows" by "cols" cells.
7554 * Start job for "cmd".
7555 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007556 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007557 * Return OK or FAIL.
7558 */
7559 static int
7560term_and_job_init(
7561 term_T *term,
7562 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007563 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007564 jobopt_T *opt,
7565 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007566{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007567 term->tl_arg0_cmd = NULL;
7568
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007569 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7570 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007571
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007572#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
LemonBoyb2b3acb2022-05-20 10:10:34 +01007573 if (term_use_palette())
7574 {
7575 if (term->tl_palette != NULL)
7576 set_vterm_palette(term->tl_vterm, term->tl_palette);
7577 else
7578 init_vterm_ansi_colors(term->tl_vterm);
7579 }
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007580#endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007581
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007582 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007583 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007584 if (term->tl_job != NULL)
7585 ++term->tl_job->jv_refcount;
7586
7587 return term->tl_job != NULL
7588 && term->tl_job->jv_channel != NULL
7589 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7590}
7591
7592 static int
7593create_pty_only(term_T *term, jobopt_T *opt)
7594{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007595 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7596 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007597
7598 term->tl_job = job_alloc();
7599 if (term->tl_job == NULL)
7600 return FAIL;
7601 ++term->tl_job->jv_refcount;
7602
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007603 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007604 term->tl_job->jv_status = JOB_FINISHED;
7605
7606 return mch_create_pty_channel(term->tl_job, opt);
7607}
7608
7609/*
7610 * Free the terminal emulator part of "term".
7611 */
7612 static void
7613term_free_vterm(term_T *term)
7614{
7615 if (term->tl_vterm != NULL)
7616 vterm_free(term->tl_vterm);
7617 term->tl_vterm = NULL;
7618}
7619
7620/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007621 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007622 */
7623 static void
7624term_report_winsize(term_T *term, int rows, int cols)
7625{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007626 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007627 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7628 {
7629 int fd = -1;
7630 int part;
7631
7632 for (part = PART_OUT; part < PART_COUNT; ++part)
7633 {
7634 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007635 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007636 break;
7637 }
7638 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7639 mch_signal_job(term->tl_job, (char_u *)"winch");
7640 }
7641}
7642
7643# endif
7644
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007645#endif // FEAT_TERMINAL