blob: 4c8ee7feabce908f53dc34d2eb9a98d5c9058e7e [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
LemonBoyb2b3acb2022-05-20 10:10:34 +0100247/*
248 * Return TRUE if the user-defined palette (either g:terminal_ansi_colors or the
249 * "ansi_colors" argument in term_start()) shall be applied.
250 */
251 static int
252term_use_palette()
253{
254 if (0
255#ifdef FEAT_GUI
256 || gui.in_use
257#endif
258#ifdef FEAT_TERMGUICOLORS
259 || p_tgc
260#endif
261 )
262 return TRUE;
263 return FALSE;
264}
265
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200266
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200267/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200268 * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
Bram Moolenaar498c2562018-04-15 23:45:15 +0200269 * current window.
270 * Sets "rows" and/or "cols" to zero when it should follow the window size.
271 * Return TRUE if the size is the minimum size: "24*80".
272 */
273 static int
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200274parse_termwinsize(win_T *wp, int *rows, int *cols)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200275{
276 int minsize = FALSE;
277
278 *rows = 0;
279 *cols = 0;
280
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200281 if (*wp->w_p_tws != NUL)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200282 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200283 char_u *p = vim_strchr(wp->w_p_tws, 'x');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200284
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100285 // Syntax of value was already checked when it's set.
Bram Moolenaar498c2562018-04-15 23:45:15 +0200286 if (p == NULL)
287 {
288 minsize = TRUE;
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200289 p = vim_strchr(wp->w_p_tws, '*');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200290 }
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200291 *rows = atoi((char *)wp->w_p_tws);
Bram Moolenaar498c2562018-04-15 23:45:15 +0200292 *cols = atoi((char *)p + 1);
293 }
294 return minsize;
295}
296
297/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200298 * Determine the terminal size from 'termwinsize' and the current window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200299 */
300 static void
Bram Moolenaarb936b792020-09-04 18:34:09 +0200301set_term_and_win_size(term_T *term, jobopt_T *opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200302{
Bram Moolenaarb936b792020-09-04 18:34:09 +0200303 int rows, cols;
304 int minsize;
305
Bram Moolenaar13568252018-03-16 20:46:58 +0100306#ifdef FEAT_GUI
307 if (term->tl_system)
308 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100309 // Use the whole screen for the system command. However, it will start
310 // at the command line and scroll up as needed, using tl_toprow.
Bram Moolenaar13568252018-03-16 20:46:58 +0100311 term->tl_rows = Rows;
312 term->tl_cols = Columns;
Bram Moolenaar07b46af2018-04-10 14:56:18 +0200313 return;
Bram Moolenaar13568252018-03-16 20:46:58 +0100314 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100315#endif
Bram Moolenaarb936b792020-09-04 18:34:09 +0200316 term->tl_rows = curwin->w_height;
317 term->tl_cols = curwin->w_width;
318
319 minsize = parse_termwinsize(curwin, &rows, &cols);
320 if (minsize)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200321 {
Bram Moolenaarb936b792020-09-04 18:34:09 +0200322 if (term->tl_rows < rows)
323 term->tl_rows = rows;
324 if (term->tl_cols < cols)
325 term->tl_cols = cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200326 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200327 if ((opt->jo_set2 & JO2_TERM_ROWS))
328 term->tl_rows = opt->jo_term_rows;
329 else if (rows != 0)
330 term->tl_rows = rows;
331 if ((opt->jo_set2 & JO2_TERM_COLS))
332 term->tl_cols = opt->jo_term_cols;
333 else if (cols != 0)
334 term->tl_cols = cols;
335
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200336 if (!opt->jo_hidden)
Bram Moolenaarb936b792020-09-04 18:34:09 +0200337 {
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200338 if (term->tl_rows != curwin->w_height)
339 win_setheight_win(term->tl_rows, curwin);
340 if (term->tl_cols != curwin->w_width)
341 win_setwidth_win(term->tl_cols, curwin);
Bram Moolenaarb936b792020-09-04 18:34:09 +0200342
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200343 // Set 'winsize' now to avoid a resize at the next redraw.
344 if (!minsize && *curwin->w_p_tws != NUL)
345 {
346 char_u buf[100];
347
348 vim_snprintf((char *)buf, 100, "%dx%d",
349 term->tl_rows, term->tl_cols);
Bram Moolenaarac4174e2022-05-07 16:38:24 +0100350 set_option_value_give_err((char_u *)"termwinsize",
351 0L, buf, OPT_LOCAL);
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200352 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200353 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200354}
355
356/*
357 * Initialize job options for a terminal job.
358 * Caller may overrule some of them.
359 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100360 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200361init_job_options(jobopt_T *opt)
362{
363 clear_job_options(opt);
364
Bram Moolenaarac4174e2022-05-07 16:38:24 +0100365 opt->jo_mode = CH_MODE_RAW;
366 opt->jo_out_mode = CH_MODE_RAW;
367 opt->jo_err_mode = CH_MODE_RAW;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200368 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
369}
370
371/*
372 * Set job options mandatory for a terminal job.
373 */
374 static void
375setup_job_options(jobopt_T *opt, int rows, int cols)
376{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100377#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100378 // Win32: Redirecting the job output won't work, thus always connect stdout
379 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200380 if (!(opt->jo_set & JO_OUT_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200381#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200382 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100383 // Connect stdout to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200384 opt->jo_io[PART_OUT] = JIO_BUFFER;
385 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
386 opt->jo_modifiable[PART_OUT] = 0;
387 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
388 }
389
Bram Moolenaar4f974752019-02-17 17:44:42 +0100390#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100391 // Win32: Redirecting the job output won't work, thus always connect stderr
392 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200393 if (!(opt->jo_set & JO_ERR_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200394#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200395 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100396 // Connect stderr to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200397 opt->jo_io[PART_ERR] = JIO_BUFFER;
398 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
399 opt->jo_modifiable[PART_ERR] = 0;
400 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
401 }
402
403 opt->jo_pty = TRUE;
404 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
405 opt->jo_term_rows = rows;
406 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
407 opt->jo_term_cols = cols;
408}
409
410/*
Bram Moolenaar5c381eb2019-06-25 06:50:31 +0200411 * Flush messages on channels.
412 */
413 static void
414term_flush_messages()
415{
416 mch_check_messages();
417 parse_queued_messages();
418}
419
420/*
Bram Moolenaard96ff162018-02-18 22:13:29 +0100421 * Close a terminal buffer (and its window). Used when creating the terminal
422 * fails.
423 */
424 static void
425term_close_buffer(buf_T *buf, buf_T *old_curbuf)
426{
427 free_terminal(buf);
428 if (old_curbuf != NULL)
429 {
430 --curbuf->b_nwindows;
431 curbuf = old_curbuf;
432 curwin->w_buffer = curbuf;
433 ++curbuf->b_nwindows;
434 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100435 CHECK_CURBUF;
Bram Moolenaard96ff162018-02-18 22:13:29 +0100436
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100437 // Wiping out the buffer will also close the window and call
438 // free_terminal().
Bram Moolenaard96ff162018-02-18 22:13:29 +0100439 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
440}
441
442/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200443 * Start a terminal window and return its buffer.
Bram Moolenaar13568252018-03-16 20:46:58 +0100444 * Use either "argvar" or "argv", the other must be NULL.
445 * When "flags" has TERM_START_NOJOB only create the buffer, b_term and open
446 * the window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200447 * Returns NULL when failed.
448 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100449 buf_T *
450term_start(
451 typval_T *argvar,
452 char **argv,
453 jobopt_T *opt,
454 int flags)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200455{
456 exarg_T split_ea;
457 win_T *old_curwin = curwin;
458 term_T *term;
459 buf_T *old_curbuf = NULL;
460 int res;
461 buf_T *newbuf;
Bram Moolenaare1004402020-10-24 20:49:43 +0200462 int vertical = opt->jo_vertical || (cmdmod.cmod_split & WSP_VERT);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200463 jobopt_T orig_opt; // only partly filled
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200464
465 if (check_restricted() || check_secure())
466 return NULL;
Bram Moolenaare5b44862021-05-30 13:54:03 +0200467#ifdef FEAT_CMDWIN
468 if (cmdwin_type != 0)
469 {
470 emsg(_(e_cannot_open_terminal_from_command_line_window));
471 return NULL;
472 }
473#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200474
475 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
476 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
477 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
Bram Moolenaarb0992022020-01-30 14:55:42 +0100478 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))
479 || (argvar != NULL
480 && argvar->v_type == VAR_LIST
481 && argvar->vval.v_list != NULL
482 && argvar->vval.v_list->lv_first == &range_list_item))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200483 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000484 emsg(_(e_invalid_argument));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200485 return NULL;
486 }
487
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200488 term = ALLOC_CLEAR_ONE(term_T);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200489 if (term == NULL)
490 return NULL;
491 term->tl_dirty_row_end = MAX_ROW;
492 term->tl_cursor_visible = TRUE;
493 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
494 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100495#ifdef FEAT_GUI
496 term->tl_system = (flags & TERM_START_SYSTEM);
497#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200498 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100499 ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200500 ga_init2(&term->tl_osc_buf, sizeof(char), 300);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200501
Bram Moolenaaraeed2a62021-04-29 20:18:45 +0200502 setpcmark();
Bram Moolenaara80faa82020-04-12 19:37:17 +0200503 CLEAR_FIELD(split_ea);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200504 if (opt->jo_curwin)
505 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100506 // Create a new buffer in the current window.
Bram Moolenaar13568252018-03-16 20:46:58 +0100507 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200508 {
509 no_write_message();
510 vim_free(term);
511 return NULL;
512 }
513 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaarb1009092020-05-31 16:04:42 +0200514 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
515 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
516 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200517 {
518 vim_free(term);
519 return NULL;
520 }
521 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100522 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200523 {
524 buf_T *buf;
525
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100526 // Create a new buffer without a window. Make it the current buffer for
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100527 // a moment to be able to do the initializations.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200528 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
529 BLN_NEW | BLN_LISTED);
530 if (buf == NULL || ml_open(buf) == FAIL)
531 {
532 vim_free(term);
533 return NULL;
534 }
535 old_curbuf = curbuf;
536 --curbuf->b_nwindows;
537 curbuf = buf;
538 curwin->w_buffer = buf;
539 ++curbuf->b_nwindows;
540 }
541 else
542 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100543 // Open a new window or tab.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200544 split_ea.cmdidx = CMD_new;
545 split_ea.cmd = (char_u *)"new";
546 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100547 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200548 {
549 split_ea.line2 = opt->jo_term_rows;
550 split_ea.addr_count = 1;
551 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100552 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200553 {
554 split_ea.line2 = opt->jo_term_cols;
555 split_ea.addr_count = 1;
556 }
557
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100558 if (vertical)
Bram Moolenaare1004402020-10-24 20:49:43 +0200559 cmdmod.cmod_split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200560 ex_splitview(&split_ea);
561 if (curwin == old_curwin)
562 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100563 // split failed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200564 vim_free(term);
565 return NULL;
566 }
567 }
568 term->tl_buffer = curbuf;
569 curbuf->b_term = term;
570
571 if (!opt->jo_hidden)
572 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100573 // Only one size was taken care of with :new, do the other one. With
574 // "curwin" both need to be done.
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100575 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200576 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100577 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200578 win_setwidth(opt->jo_term_cols);
579 }
580
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100581 // Link the new terminal in the list of active terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200582 term->tl_next = first_term;
583 first_term = term;
584
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100585 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
586
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200587 if (opt->jo_term_name != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100588 {
589 vim_free(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200590 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100591 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100592 else if (argv != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100593 {
594 vim_free(curbuf->b_ffname);
Bram Moolenaar13568252018-03-16 20:46:58 +0100595 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100596 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200597 else
598 {
599 int i;
600 size_t len;
601 char_u *cmd, *p;
602
603 if (argvar->v_type == VAR_STRING)
604 {
605 cmd = argvar->vval.v_string;
606 if (cmd == NULL)
607 cmd = (char_u *)"";
608 else if (STRCMP(cmd, "NONE") == 0)
609 cmd = (char_u *)"pty";
610 }
611 else if (argvar->v_type != VAR_LIST
612 || argvar->vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +0100613 || argvar->vval.v_list->lv_len == 0
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100614 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200615 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
616 cmd = (char_u*)"";
617
618 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200619 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200620
621 for (i = 0; p != NULL; ++i)
622 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100623 // Prepend a ! to the command name to avoid the buffer name equals
624 // the executable, otherwise ":w!" would overwrite it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200625 if (i == 0)
626 vim_snprintf((char *)p, len, "!%s", cmd);
627 else
628 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
629 if (buflist_findname(p) == NULL)
630 {
631 vim_free(curbuf->b_ffname);
632 curbuf->b_ffname = p;
633 break;
634 }
635 }
636 }
Bram Moolenaare010c722020-02-24 21:37:54 +0100637 vim_free(curbuf->b_sfname);
638 curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200639 curbuf->b_fname = curbuf->b_ffname;
640
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100641 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
642
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200643 if (opt->jo_term_opencmd != NULL)
644 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
645
646 if (opt->jo_eof_chars != NULL)
647 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
648
649 set_string_option_direct((char_u *)"buftype", -1,
650 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200651 // Avoid that 'buftype' is reset when this buffer is entered.
652 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200653
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100654 // Mark the buffer as not modifiable. It can only be made modifiable after
655 // the job finished.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200656 curbuf->b_p_ma = FALSE;
657
Bram Moolenaarb936b792020-09-04 18:34:09 +0200658 set_term_and_win_size(term, opt);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100659#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200660 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
661#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200662 setup_job_options(opt, term->tl_rows, term->tl_cols);
663
Bram Moolenaar13568252018-03-16 20:46:58 +0100664 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100665 return curbuf;
666
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100667#if defined(FEAT_SESSION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100668 // Remember the command for the session file.
Bram Moolenaar13568252018-03-16 20:46:58 +0100669 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100670 term->tl_command = vim_strsave((char_u *)"NONE");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100671 else if (argvar->v_type == VAR_STRING)
672 {
673 char_u *cmd = argvar->vval.v_string;
674
675 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
676 term->tl_command = vim_strsave(cmd);
677 }
678 else if (argvar->v_type == VAR_LIST
679 && argvar->vval.v_list != NULL
680 && argvar->vval.v_list->lv_len > 0)
681 {
682 garray_T ga;
683 listitem_T *item;
684
685 ga_init2(&ga, 1, 100);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200686 FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100687 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100688 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100689 char_u *p;
690
691 if (s == NULL)
692 break;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100693 p = vim_strsave_fnameescape(s, VSE_NONE);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100694 if (p == NULL)
695 break;
696 ga_concat(&ga, p);
697 vim_free(p);
698 ga_append(&ga, ' ');
699 }
700 if (item == NULL)
701 {
702 ga_append(&ga, NUL);
703 term->tl_command = ga.ga_data;
704 }
705 else
706 ga_clear(&ga);
707 }
708#endif
709
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100710 if (opt->jo_term_kill != NULL)
711 {
712 char_u *p = skiptowhite(opt->jo_term_kill);
713
714 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
715 }
716
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200717 if (opt->jo_term_api != NULL)
Bram Moolenaar21109272020-01-30 16:27:20 +0100718 {
719 char_u *p = skiptowhite(opt->jo_term_api);
720
721 term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
722 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200723 else
724 term->tl_api = vim_strsave((char_u *)"Tapi_");
725
Bram Moolenaar83d47902020-03-26 20:34:00 +0100726 if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
727 term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
728
LemonBoyb2b3acb2022-05-20 10:10:34 +0100729 // Save the user-defined palette, it is only used in GUI (or 'tgc' is on).
730 if (opt->jo_set2 & JO2_ANSI_COLORS)
731 {
732 term->tl_palette = ALLOC_MULT(long_u, 16);
733 if (term->tl_palette == NULL)
734 {
735 vim_free(term);
736 return NULL;
737 }
738 memcpy(term->tl_palette, opt->jo_ansi_colors, sizeof(long_u) * 16);
739 }
740
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100741 // System dependent: setup the vterm and maybe start the job in it.
Bram Moolenaar13568252018-03-16 20:46:58 +0100742 if (argv == NULL
743 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200744 && argvar->vval.v_string != NULL
745 && STRCMP(argvar->vval.v_string, "NONE") == 0)
746 res = create_pty_only(term, opt);
747 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200748 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200749
750 newbuf = curbuf;
751 if (res == OK)
752 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100753 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200754 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
755 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100756#ifdef FEAT_GUI
757 if (term->tl_system)
758 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100759 // display first line below typed command
Bram Moolenaar13568252018-03-16 20:46:58 +0100760 term->tl_toprow = msg_row + 1;
761 term->tl_dirty_row_end = 0;
762 }
763#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200764
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100765 // Make sure we don't get stuck on sending keys to the job, it leads to
766 // a deadlock if the job is waiting for Vim to read.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200767 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
768
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200769 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200770 {
771 --curbuf->b_nwindows;
772 curbuf = old_curbuf;
773 curwin->w_buffer = curbuf;
774 ++curbuf->b_nwindows;
775 }
Bram Moolenaar81035272021-12-16 18:02:07 +0000776 else if (vgetc_busy
777#ifdef FEAT_TIMERS
778 || timer_busy
779#endif
780 || input_busy)
781 {
782 char_u ignore[4];
783
784 // When waiting for input need to return and possibly end up in
785 // terminal_loop() instead.
786 ignore[0] = K_SPECIAL;
787 ignore[1] = KS_EXTRA;
788 ignore[2] = KE_IGNORE;
789 ignore[3] = NUL;
790 ins_typebuf(ignore, REMAP_NONE, 0, TRUE, FALSE);
791 typebuf_was_filled = TRUE;
792 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200793 }
794 else
795 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100796 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200797 return NULL;
798 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100799
Bram Moolenaar13568252018-03-16 20:46:58 +0100800 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar28ed4df2019-10-26 16:21:40 +0200801 if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
802 apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200803 return newbuf;
804}
805
806/*
807 * ":terminal": open a terminal window and execute a job in it.
808 */
809 void
810ex_terminal(exarg_T *eap)
811{
812 typval_T argvar[2];
813 jobopt_T opt;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100814 int opt_shell = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200815 char_u *cmd;
816 char_u *tofree = NULL;
817
818 init_job_options(&opt);
819
820 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100821 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200822 {
823 char_u *p, *ep;
824
825 cmd += 2;
826 p = skiptowhite(cmd);
827 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200828 if (ep != NULL)
829 {
830 if (ep < p)
831 p = ep;
832 else
833 ep = NULL;
834 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200835
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200836# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
837 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
838 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200839 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200840 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100841 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200842 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200843 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200844 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200845 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200846 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200847 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200848 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100849 opt.jo_term_norestore = 1;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100850 else if (OPTARG_HAS("shell"))
851 opt_shell = TRUE;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200852 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100853 {
854 opt.jo_set2 |= JO2_TERM_KILL;
855 opt.jo_term_kill = ep + 1;
856 p = skiptowhite(cmd);
857 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200858 else if (OPTARG_HAS("api"))
859 {
860 opt.jo_set2 |= JO2_TERM_API;
861 if (ep != NULL)
862 {
863 opt.jo_term_api = ep + 1;
864 p = skiptowhite(cmd);
865 }
866 else
867 opt.jo_term_api = NULL;
868 }
869 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200870 {
871 opt.jo_set2 |= JO2_TERM_ROWS;
872 opt.jo_term_rows = atoi((char *)ep + 1);
873 p = skiptowhite(cmd);
874 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200875 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200876 {
877 opt.jo_set2 |= JO2_TERM_COLS;
878 opt.jo_term_cols = atoi((char *)ep + 1);
879 p = skiptowhite(cmd);
880 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200881 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200882 {
883 char_u *buf = NULL;
884 char_u *keys;
885
Bram Moolenaar21109272020-01-30 16:27:20 +0100886 vim_free(opt.jo_eof_chars);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200887 p = skiptowhite(cmd);
888 *p = NUL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200889 keys = replace_termcodes(ep + 1, &buf,
890 REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200891 opt.jo_set2 |= JO2_EOF_CHARS;
892 opt.jo_eof_chars = vim_strsave(keys);
893 vim_free(buf);
894 *p = ' ';
895 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100896#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100897 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
898 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100899 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100900 int tty_type = NUL;
901
902 p = skiptowhite(cmd);
903 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
904 tty_type = 'w';
905 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
906 tty_type = 'c';
907 else
908 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000909 semsg(e_invalid_value_for_argument_str, "type");
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100910 goto theend;
911 }
912 opt.jo_set2 |= JO2_TTY_TYPE;
913 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100914 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100915#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200916 else
917 {
918 if (*p)
919 *p = NUL;
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000920 semsg(_(e_invalid_attribute_str), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100921 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200922 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200923# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200924 cmd = skipwhite(p);
925 }
926 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100927 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100928 // Make a copy of 'shell', an autocommand may change the option.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200929 tofree = cmd = vim_strsave(p_sh);
930
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100931 // default to close when the shell exits
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100932 if (opt.jo_term_finish == NUL)
Bram Moolenaare2978022020-04-26 14:47:44 +0200933 opt.jo_term_finish = TL_FINISH_CLOSE;
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100934 }
935
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200936 if (eap->addr_count > 0)
937 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100938 // Write lines from current buffer to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200939 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
940 opt.jo_io[PART_IN] = JIO_BUFFER;
941 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
942 opt.jo_in_top = eap->line1;
943 opt.jo_in_bot = eap->line2;
944 }
945
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100946 if (opt_shell && tofree == NULL)
947 {
948#ifdef UNIX
949 char **argv = NULL;
950 char_u *tofree1 = NULL;
951 char_u *tofree2 = NULL;
952
953 // :term ++shell command
954 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
955 term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaaradf4aa22019-11-10 22:36:44 +0100956 vim_free(argv);
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100957 vim_free(tofree1);
958 vim_free(tofree2);
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100959 goto theend;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100960#else
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100961# ifdef MSWIN
962 long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
963 char_u *newcmd;
964
965 newcmd = alloc(cmdlen);
966 if (newcmd == NULL)
967 goto theend;
968 tofree = newcmd;
969 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
970 cmd = newcmd;
971# else
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000972 emsg(_(e_sorry_plusplusshell_not_supported_on_this_system));
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100973 goto theend;
974# endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100975#endif
976 }
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100977 argvar[0].v_type = VAR_STRING;
978 argvar[0].vval.v_string = cmd;
979 argvar[1].v_type = VAR_UNKNOWN;
980 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100981
982theend:
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100983 vim_free(tofree);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200984 vim_free(opt.jo_eof_chars);
985}
986
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100987#if defined(FEAT_SESSION) || defined(PROTO)
988/*
989 * Write a :terminal command to the session file to restore the terminal in
990 * window "wp".
991 * Return FAIL if writing fails.
992 */
993 int
Bram Moolenaar0e655112020-09-11 20:36:36 +0200994term_write_session(FILE *fd, win_T *wp, hashtab_T *terminal_bufs)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100995{
Bram Moolenaar0e655112020-09-11 20:36:36 +0200996 const int bufnr = wp->w_buffer->b_fnum;
997 term_T *term = wp->w_buffer->b_term;
998
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200999 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +02001000 {
1001 // There are multiple views into this terminal buffer. We don't want to
1002 // create the terminal multiple times. If it's the first time, create,
1003 // otherwise link to the first buffer.
1004 char id_as_str[NUMBUFLEN];
1005 hashitem_T *entry;
1006
1007 vim_snprintf(id_as_str, sizeof(id_as_str), "%d", bufnr);
1008
1009 entry = hash_find(terminal_bufs, (char_u *)id_as_str);
1010 if (!HASHITEM_EMPTY(entry))
1011 {
1012 // we've already opened this terminal buffer
1013 if (fprintf(fd, "execute 'buffer ' . s:term_buf_%d", bufnr) < 0)
1014 return FAIL;
1015 return put_eol(fd);
1016 }
1017 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001018
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001019 // Create the terminal and run the command. This is not without
1020 // risk, but let's assume the user only creates a session when this
1021 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001022 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
1023 term->tl_cols, term->tl_rows) < 0)
1024 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001025#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01001026 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
1027 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001028#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001029 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
1030 return FAIL;
Bram Moolenaar0e655112020-09-11 20:36:36 +02001031 if (put_eol(fd) != OK)
1032 return FAIL;
1033
1034 if (fprintf(fd, "let s:term_buf_%d = bufnr()", bufnr) < 0)
1035 return FAIL;
1036
Bram Moolenaarc2c82052020-09-11 22:10:22 +02001037 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +02001038 {
1039 char *hash_key = alloc(NUMBUFLEN);
1040
1041 vim_snprintf(hash_key, NUMBUFLEN, "%d", bufnr);
1042 hash_add(terminal_bufs, (char_u *)hash_key);
1043 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001044
1045 return put_eol(fd);
1046}
1047
1048/*
1049 * Return TRUE if "buf" has a terminal that should be restored.
1050 */
1051 int
1052term_should_restore(buf_T *buf)
1053{
1054 term_T *term = buf->b_term;
1055
1056 return term != NULL && (term->tl_command == NULL
1057 || STRCMP(term->tl_command, "NONE") != 0);
1058}
1059#endif
1060
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001061/*
1062 * Free the scrollback buffer for "term".
1063 */
1064 static void
1065free_scrollback(term_T *term)
1066{
1067 int i;
1068
1069 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
1070 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
1071 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001072 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
1073 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
1074 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001075}
1076
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001077
1078// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +02001079static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001080
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001081/*
1082 * Free a terminal and everything it refers to.
1083 * Kills the job if there is one.
1084 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001085 * The actual terminal structure is freed later in free_unused_terminals(),
1086 * because callbacks may wipe out a buffer while the terminal is still
1087 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001088 */
1089 void
1090free_terminal(buf_T *buf)
1091{
1092 term_T *term = buf->b_term;
1093 term_T *tp;
1094
1095 if (term == NULL)
1096 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001097
1098 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001099 if (first_term == term)
1100 first_term = term->tl_next;
1101 else
1102 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
1103 if (tp->tl_next == term)
1104 {
1105 tp->tl_next = term->tl_next;
1106 break;
1107 }
1108
1109 if (term->tl_job != NULL)
1110 {
1111 if (term->tl_job->jv_status != JOB_ENDED
1112 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +01001113 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001114 job_stop(term->tl_job, NULL, "kill");
1115 job_unref(term->tl_job);
1116 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001117 term->tl_next = terminals_to_free;
1118 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001119
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001120 buf->b_term = NULL;
1121 if (in_terminal_loop == term)
1122 in_terminal_loop = NULL;
1123}
1124
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001125 void
1126free_unused_terminals()
1127{
1128 while (terminals_to_free != NULL)
1129 {
1130 term_T *term = terminals_to_free;
1131
1132 terminals_to_free = term->tl_next;
1133
1134 free_scrollback(term);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02001135 ga_clear(&term->tl_osc_buf);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001136
1137 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001138 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001139 vim_free(term->tl_title);
1140#ifdef FEAT_SESSION
1141 vim_free(term->tl_command);
1142#endif
1143 vim_free(term->tl_kill);
1144 vim_free(term->tl_status_text);
1145 vim_free(term->tl_opencmd);
1146 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001147 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001148#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001149 if (term->tl_out_fd != NULL)
1150 fclose(term->tl_out_fd);
1151#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001152 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001153 vim_free(term->tl_cursor_color);
LemonBoyb2b3acb2022-05-20 10:10:34 +01001154 vim_free(term->tl_palette);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001155 vim_free(term);
1156 }
1157}
1158
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001159/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001160 * Get the part that is connected to the tty. Normally this is PART_IN, but
1161 * when writing buffer lines to the job it can be another. This makes it
1162 * possible to do "1,5term vim -".
1163 */
1164 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001165get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001166{
1167#ifdef UNIX
1168 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1169 int i;
1170
1171 for (i = 0; i < 3; ++i)
1172 {
1173 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1174
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001175 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001176 return parts[i];
1177 }
1178#endif
1179 return PART_IN;
1180}
1181
1182/*
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001183 * Read any vterm output and send it on the channel.
1184 */
1185 static void
1186term_forward_output(term_T *term)
1187{
1188 VTerm *vterm = term->tl_vterm;
1189 char buf[KEY_BUF_LEN];
1190 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1191
1192 if (curlen > 0)
1193 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1194 (char_u *)buf, (int)curlen, NULL);
1195}
1196
1197/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001198 * Write job output "msg[len]" to the vterm.
1199 */
1200 static void
Bram Moolenaar36968af2021-11-15 17:13:11 +00001201term_write_job_output(term_T *term, char_u *msg_arg, size_t len_arg)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001202{
Bram Moolenaar36968af2021-11-15 17:13:11 +00001203 char_u *msg = msg_arg;
1204 size_t len = len_arg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001205 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001206 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar36968af2021-11-15 17:13:11 +00001207 size_t limit = term->tl_buffer->b_p_twsl * term->tl_cols * 3;
1208
1209 // Limit the length to 'termwinscroll' * cols * 3 bytes. Keep the text at
1210 // the end.
1211 if (len > limit)
1212 {
1213 char_u *p = msg + len - limit;
1214
1215 p -= (*mb_head_off)(msg, p);
1216 len -= p - msg;
1217 msg = p;
1218 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001219
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001220 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001221
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001222 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001223 if (prevlen != vterm_output_get_buffer_current(vterm))
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001224 term_forward_output(term);
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001225
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001226 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001227 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1228}
1229
1230 static void
1231update_cursor(term_T *term, int redraw)
1232{
1233 if (term->tl_normal_mode)
1234 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001235#ifdef FEAT_GUI
1236 if (term->tl_system)
1237 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1238 term->tl_cursor_pos.col);
1239 else
1240#endif
1241 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001242 if (redraw)
1243 {
1244 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1245 cursor_on();
1246 out_flush();
1247#ifdef FEAT_GUI
1248 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001249 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001250 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001251 gui_mch_flush();
1252 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001253#endif
1254 }
1255}
1256
1257/*
1258 * Invoked when "msg" output from a job was received. Write it to the terminal
1259 * of "buffer".
1260 */
1261 void
1262write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1263{
1264 size_t len = STRLEN(msg);
1265 term_T *term = buffer->b_term;
1266
Bram Moolenaar4f974752019-02-17 17:44:42 +01001267#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001268 // Win32: Cannot redirect output of the job, intercept it here and write to
1269 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001270 if (term->tl_out_fd != NULL)
1271 {
1272 ch_log(channel, "Writing %d bytes to output file", (int)len);
1273 fwrite(msg, len, 1, term->tl_out_fd);
1274 return;
1275 }
1276#endif
1277
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001278 if (term->tl_vterm == NULL)
1279 {
1280 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1281 return;
1282 }
1283 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01001284 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001285 term_write_job_output(term, msg, len);
1286
Bram Moolenaar13568252018-03-16 20:46:58 +01001287#ifdef FEAT_GUI
1288 if (term->tl_system)
1289 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001290 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001291 update_system_term(term);
1292 update_cursor(term, TRUE);
1293 }
1294 else
1295#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001296 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1297 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001298 if (!term->tl_normal_mode)
1299 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001300 // Don't use update_screen() when editing the command line, it gets
1301 // cleared.
1302 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001303 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar24959102022-05-07 20:01:16 +01001304 if (buffer == curbuf && (State & MODE_CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001305 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001306 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001307 // update_screen() can be slow, check the terminal wasn't closed
1308 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001309 if (buffer == curbuf && curbuf->b_term != NULL)
1310 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001311 }
1312 else
Bram Moolenaare5050712021-12-09 10:51:05 +00001313 redraw_after_callback(TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001314 }
1315}
1316
1317/*
1318 * Send a mouse position and click to the vterm
1319 */
1320 static int
1321term_send_mouse(VTerm *vterm, int button, int pressed)
1322{
1323 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001324 int row = mouse_row - W_WINROW(curwin);
1325 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001326
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001327#ifdef FEAT_PROP_POPUP
1328 if (popup_is_popup(curwin))
1329 {
1330 row -= popup_top_extra(curwin);
1331 col -= popup_left_extra(curwin);
1332 }
1333#endif
1334 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001335 if (button != 0)
1336 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001337 return TRUE;
1338}
1339
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001340static int enter_mouse_col = -1;
1341static int enter_mouse_row = -1;
1342
1343/*
1344 * Handle a mouse click, drag or release.
1345 * Return TRUE when a mouse event is sent to the terminal.
1346 */
1347 static int
1348term_mouse_click(VTerm *vterm, int key)
1349{
1350#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001351 // For modeless selection mouse drag and release events are ignored, unless
1352 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001353 static int ignore_drag_release = TRUE;
1354 VTermMouseState mouse_state;
1355
1356 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1357 if (mouse_state.flags == 0)
1358 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001359 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001360 switch (key)
1361 {
1362 case K_LEFTDRAG:
1363 case K_LEFTRELEASE:
1364 case K_RIGHTDRAG:
1365 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001366 // Ignore drag and release events when the button-down wasn't
1367 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001368 if (ignore_drag_release)
1369 {
1370 int save_mouse_col, save_mouse_row;
1371
1372 if (enter_mouse_col < 0)
1373 break;
1374
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001375 // mouse click in the window gave us focus, handle that
1376 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001377 save_mouse_col = mouse_col;
1378 save_mouse_row = mouse_row;
1379 mouse_col = enter_mouse_col;
1380 mouse_row = enter_mouse_row;
1381 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1382 mouse_col = save_mouse_col;
1383 mouse_row = save_mouse_row;
1384 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001385 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001386 case K_LEFTMOUSE:
1387 case K_RIGHTMOUSE:
1388 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1389 ignore_drag_release = TRUE;
1390 else
1391 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001392 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001393 if (clip_star.available)
1394 {
1395 int button, is_click, is_drag;
1396
1397 button = get_mouse_button(KEY2TERMCAP1(key),
1398 &is_click, &is_drag);
1399 if (mouse_model_popup() && button == MOUSE_LEFT
1400 && (mod_mask & MOD_MASK_SHIFT))
1401 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001402 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001403 button = MOUSE_RIGHT;
1404 mod_mask &= ~MOD_MASK_SHIFT;
1405 }
1406 clip_modeless(button, is_click, is_drag);
1407 }
1408 break;
1409
1410 case K_MIDDLEMOUSE:
1411 if (clip_star.available)
1412 insert_reg('*', TRUE);
1413 break;
1414 }
1415 enter_mouse_col = -1;
1416 return FALSE;
1417 }
1418#endif
1419 enter_mouse_col = -1;
1420
1421 switch (key)
1422 {
1423 case K_LEFTMOUSE:
1424 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1425 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1426 case K_LEFTRELEASE:
1427 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1428 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1429 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1430 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1431 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1432 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1433 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1434 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1435 }
1436 return TRUE;
1437}
1438
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001439/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001440 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1441 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001442 * Return the number of bytes in "buf".
1443 */
1444 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001445term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001446{
1447 VTerm *vterm = term->tl_vterm;
1448 VTermKey key = VTERM_KEY_NONE;
1449 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001450 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001451
1452 switch (c)
1453 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001454 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001455
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001456 // don't use VTERM_KEY_BACKSPACE, it always
1457 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001458 case K_BS: c = term_backspace_char; break;
1459
1460 case ESC: key = VTERM_KEY_ESCAPE; break;
1461 case K_DEL: key = VTERM_KEY_DEL; break;
1462 case K_DOWN: key = VTERM_KEY_DOWN; break;
1463 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1464 key = VTERM_KEY_DOWN; break;
1465 case K_END: key = VTERM_KEY_END; break;
1466 case K_S_END: mod = VTERM_MOD_SHIFT;
1467 key = VTERM_KEY_END; break;
1468 case K_C_END: mod = VTERM_MOD_CTRL;
1469 key = VTERM_KEY_END; break;
1470 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1471 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1472 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1473 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1474 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1475 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1476 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1477 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1478 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1479 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1480 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1481 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1482 case K_HOME: key = VTERM_KEY_HOME; break;
1483 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1484 key = VTERM_KEY_HOME; break;
1485 case K_C_HOME: mod = VTERM_MOD_CTRL;
1486 key = VTERM_KEY_HOME; break;
1487 case K_INS: key = VTERM_KEY_INS; break;
1488 case K_K0: key = VTERM_KEY_KP_0; break;
1489 case K_K1: key = VTERM_KEY_KP_1; break;
1490 case K_K2: key = VTERM_KEY_KP_2; break;
1491 case K_K3: key = VTERM_KEY_KP_3; break;
1492 case K_K4: key = VTERM_KEY_KP_4; break;
1493 case K_K5: key = VTERM_KEY_KP_5; break;
1494 case K_K6: key = VTERM_KEY_KP_6; break;
1495 case K_K7: key = VTERM_KEY_KP_7; break;
1496 case K_K8: key = VTERM_KEY_KP_8; break;
1497 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001498 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001499 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001500 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001501 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001502 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1503 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001504 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1505 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001506 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1507 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001508 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1509 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1510 case K_LEFT: key = VTERM_KEY_LEFT; break;
1511 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1512 key = VTERM_KEY_LEFT; break;
1513 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1514 key = VTERM_KEY_LEFT; break;
1515 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1516 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1517 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1518 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1519 key = VTERM_KEY_RIGHT; break;
1520 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1521 key = VTERM_KEY_RIGHT; break;
1522 case K_UP: key = VTERM_KEY_UP; break;
1523 case K_S_UP: mod = VTERM_MOD_SHIFT;
1524 key = VTERM_KEY_UP; break;
1525 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001526 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1527 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001528
Bram Moolenaara42ad572017-11-16 13:08:04 +01001529 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1530 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001531 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1532 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001533
1534 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001535 case K_LEFTMOUSE_NM:
1536 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001537 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001538 case K_LEFTRELEASE_NM:
1539 case K_MOUSEMOVE:
1540 case K_MIDDLEMOUSE:
1541 case K_MIDDLEDRAG:
1542 case K_MIDDLERELEASE:
1543 case K_RIGHTMOUSE:
1544 case K_RIGHTDRAG:
1545 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1546 return 0;
1547 other = TRUE;
1548 break;
1549
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001550 case K_X1MOUSE: /* TODO */ return 0;
1551 case K_X1DRAG: /* TODO */ return 0;
1552 case K_X1RELEASE: /* TODO */ return 0;
1553 case K_X2MOUSE: /* TODO */ return 0;
1554 case K_X2DRAG: /* TODO */ return 0;
1555 case K_X2RELEASE: /* TODO */ return 0;
1556
1557 case K_IGNORE: return 0;
1558 case K_NOP: return 0;
1559 case K_UNDO: return 0;
1560 case K_HELP: return 0;
1561 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1562 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1563 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1564 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1565 case K_SELECT: return 0;
1566#ifdef FEAT_GUI
1567 case K_VER_SCROLLBAR: return 0;
1568 case K_HOR_SCROLLBAR: return 0;
1569#endif
1570#ifdef FEAT_GUI_TABLINE
1571 case K_TABLINE: return 0;
1572 case K_TABMENU: return 0;
1573#endif
1574#ifdef FEAT_NETBEANS_INTG
1575 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1576#endif
1577#ifdef FEAT_DND
1578 case K_DROP: return 0;
1579#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001580 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001581 case K_PS: vterm_keyboard_start_paste(vterm);
1582 other = TRUE;
1583 break;
1584 case K_PE: vterm_keyboard_end_paste(vterm);
1585 other = TRUE;
1586 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001587 }
1588
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001589 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001590 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001591 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001592 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001593 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001594 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001595 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001596
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001597 /*
1598 * Convert special keys to vterm keys:
1599 * - Write keys to vterm: vterm_keyboard_key()
1600 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001601 */
1602 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001603 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001604 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001605 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001606 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001607 vterm_keyboard_unichar(vterm, c, mod);
1608
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001609 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001610 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1611}
1612
1613/*
1614 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001615 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001616 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001617 */
1618 static int
1619term_job_running_check(term_T *term, int check_job_status)
1620{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001621 // Also consider the job finished when the channel is closed, to avoid a
1622 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001623 if (term != NULL
1624 && term->tl_job != NULL
1625 && channel_is_open(term->tl_job->jv_channel))
1626 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001627 job_T *job = term->tl_job;
1628
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001629 // Careful: Checking the job status may invoke callbacks, which close
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001630 // the buffer and terminate "term". However, "job" will not be freed
1631 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001632 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001633 job_status(job);
1634 return (job->jv_status == JOB_STARTED
1635 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001636 }
1637 return FALSE;
1638}
1639
1640/*
1641 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001642 */
1643 int
1644term_job_running(term_T *term)
1645{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001646 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001647}
1648
1649/*
1650 * Return TRUE if "term" has an active channel and used ":term NONE".
1651 */
1652 int
1653term_none_open(term_T *term)
1654{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001655 // Also consider the job finished when the channel is closed, to avoid a
1656 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001657 return term != NULL
1658 && term->tl_job != NULL
1659 && channel_is_open(term->tl_job->jv_channel)
1660 && term->tl_job->jv_channel->ch_keep_open;
1661}
1662
1663/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001664 * Used when exiting: kill the job in "buf" if so desired.
1665 * Return OK when the job finished.
1666 * Return FAIL when the job is still running.
1667 */
1668 int
1669term_try_stop_job(buf_T *buf)
1670{
1671 int count;
1672 char *how = (char *)buf->b_term->tl_kill;
1673
1674#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001675 if ((how == NULL || *how == NUL)
1676 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001677 {
1678 char_u buff[DIALOG_MSG_SIZE];
1679 int ret;
1680
Bram Moolenaar00806bc2020-11-05 19:36:38 +01001681 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001682 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1683 if (ret == VIM_YES)
1684 how = "kill";
1685 else if (ret == VIM_CANCEL)
1686 return FAIL;
1687 }
1688#endif
1689 if (how == NULL || *how == NUL)
1690 return FAIL;
1691
1692 job_stop(buf->b_term->tl_job, NULL, how);
1693
Bram Moolenaar9172d232019-01-29 23:06:54 +01001694 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001695 for (count = 0; count < 100; ++count)
1696 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001697 job_T *job;
1698
1699 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001700 if (!buf_valid(buf)
1701 || buf->b_term == NULL
1702 || buf->b_term->tl_job == NULL)
1703 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001704 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001705
Bram Moolenaar9172d232019-01-29 23:06:54 +01001706 // Call job_status() to update jv_status. It may cause the job to be
1707 // cleaned up but it won't be freed.
1708 job_status(job);
1709 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001710 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001711
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001712 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001713 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001714 }
1715 return FAIL;
1716}
1717
1718/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001719 * Add the last line of the scrollback buffer to the buffer in the window.
1720 */
1721 static void
1722add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1723{
1724 buf_T *buf = term->tl_buffer;
1725 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1726 linenr_T lnum = buf->b_ml.ml_line_count;
1727
Bram Moolenaar4f974752019-02-17 17:44:42 +01001728#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001729 if (!enc_utf8 && enc_codepage > 0)
1730 {
1731 WCHAR *ret = NULL;
1732 int length = 0;
1733
1734 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1735 &ret, &length);
1736 if (ret != NULL)
1737 {
1738 WideCharToMultiByte_alloc(enc_codepage, 0,
1739 ret, length, (char **)&text, &len, 0, 0);
1740 vim_free(ret);
1741 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1742 vim_free(text);
1743 }
1744 }
1745 else
1746#endif
1747 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1748 if (empty)
1749 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001750 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001751 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001752 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001753 curbuf = curwin->w_buffer;
1754 }
1755}
1756
1757 static void
1758cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1759{
1760 attr->width = cell->width;
1761 attr->attrs = cell->attrs;
1762 attr->fg = cell->fg;
1763 attr->bg = cell->bg;
1764}
1765
1766 static int
1767equal_celattr(cellattr_T *a, cellattr_T *b)
1768{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001769 // We only compare the RGB colors, ignoring the ANSI index and type.
1770 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001771 return a->fg.red == b->fg.red
1772 && a->fg.green == b->fg.green
1773 && a->fg.blue == b->fg.blue
1774 && a->bg.red == b->bg.red
1775 && a->bg.green == b->bg.green
1776 && a->bg.blue == b->bg.blue;
1777}
1778
Bram Moolenaard96ff162018-02-18 22:13:29 +01001779/*
1780 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1781 * line at this position. Otherwise at the end.
1782 */
1783 static int
1784add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1785{
1786 if (ga_grow(&term->tl_scrollback, 1) == OK)
1787 {
1788 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1789 + term->tl_scrollback.ga_len;
1790
1791 if (lnum > 0)
1792 {
1793 int i;
1794
1795 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1796 {
1797 *line = *(line - 1);
1798 --line;
1799 }
1800 }
1801 line->sb_cols = 0;
1802 line->sb_cells = NULL;
1803 line->sb_fill_attr = *fill_attr;
1804 ++term->tl_scrollback.ga_len;
1805 return OK;
1806 }
1807 return FALSE;
1808}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001809
1810/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001811 * Remove the terminal contents from the scrollback and the buffer.
1812 * Used before adding a new scrollback line or updating the buffer for lines
1813 * displayed in the terminal.
1814 */
1815 static void
1816cleanup_scrollback(term_T *term)
1817{
1818 sb_line_T *line;
1819 garray_T *gap;
1820
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001821 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001822 gap = &term->tl_scrollback;
1823 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1824 && gap->ga_len > 0)
1825 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001826 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001827 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1828 vim_free(line->sb_cells);
1829 --gap->ga_len;
1830 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001831 curbuf = curwin->w_buffer;
1832 if (curbuf == term->tl_buffer)
1833 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001834}
1835
1836/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001837 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001838 */
1839 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001840update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001841{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001842 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001843 int len;
1844 int lines_skipped = 0;
1845 VTermPos pos;
1846 VTermScreenCell cell;
1847 cellattr_T fill_attr, new_fill_attr;
1848 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001849
1850 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1851 "Adding terminal window snapshot to buffer");
1852
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001853 // First remove the lines that were appended before, they might be
1854 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001855 cleanup_scrollback(term);
1856
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001857 screen = vterm_obtain_screen(term->tl_vterm);
1858 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001859 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1860 {
1861 len = 0;
1862 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1863 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1864 && cell.chars[0] != NUL)
1865 {
1866 len = pos.col + 1;
1867 new_fill_attr = term->tl_default_color;
1868 }
1869 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001870 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001871 cell2cellattr(&cell, &new_fill_attr);
1872
1873 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1874 ++lines_skipped;
1875 else
1876 {
1877 while (lines_skipped > 0)
1878 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001879 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001880 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001881 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001882 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001883 }
1884
1885 if (len == 0)
1886 p = NULL;
1887 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001888 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001889 if ((p != NULL || len == 0)
1890 && ga_grow(&term->tl_scrollback, 1) == OK)
1891 {
1892 garray_T ga;
1893 int width;
1894 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1895 + term->tl_scrollback.ga_len;
1896
1897 ga_init2(&ga, 1, 100);
1898 for (pos.col = 0; pos.col < len; pos.col += width)
1899 {
1900 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1901 {
1902 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001903 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001904 if (ga_grow(&ga, 1) == OK)
1905 ga.ga_len += utf_char2bytes(' ',
1906 (char_u *)ga.ga_data + ga.ga_len);
1907 }
1908 else
1909 {
1910 width = cell.width;
1911
1912 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001913 if (width == 2)
1914 // second cell of double-width character has the
1915 // same attributes.
1916 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001917
Bram Moolenaara79fd562018-12-20 20:47:32 +01001918 // Each character can be up to 6 bytes.
1919 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001920 {
1921 int i;
1922 int c;
1923
1924 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1925 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1926 (char_u *)ga.ga_data + ga.ga_len);
1927 }
1928 }
1929 }
1930 line->sb_cols = len;
1931 line->sb_cells = p;
1932 line->sb_fill_attr = new_fill_attr;
1933 fill_attr = new_fill_attr;
1934 ++term->tl_scrollback.ga_len;
1935
1936 if (ga_grow(&ga, 1) == FAIL)
1937 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1938 else
1939 {
1940 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1941 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1942 }
1943 ga_clear(&ga);
1944 }
1945 else
1946 vim_free(p);
1947 }
1948 }
1949
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001950 // Add trailing empty lines.
1951 for (pos.row = term->tl_scrollback.ga_len;
1952 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1953 ++pos.row)
1954 {
1955 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1956 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1957 }
1958
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001959 term->tl_dirty_snapshot = FALSE;
1960#ifdef FEAT_TIMERS
1961 term->tl_timer_set = FALSE;
1962#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001963}
1964
1965/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001966 * Loop over all windows in the current tab, and also curwin, which is not
1967 * encountered when using a terminal in a popup window.
1968 * Return TRUE if "*wp" was set to the next window.
1969 */
1970 static int
1971for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1972{
1973 if (*wp == NULL)
1974 *wp = firstwin;
1975 else if ((*wp)->w_next != NULL)
1976 *wp = (*wp)->w_next;
1977 else if (!*did_curwin)
1978 *wp = curwin;
1979 else
1980 return FALSE;
1981 if (*wp == curwin)
1982 *did_curwin = TRUE;
1983 return TRUE;
1984}
1985
1986/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001987 * If needed, add the current lines of the terminal to scrollback and to the
1988 * buffer. Called after the job has ended and when switching to
1989 * Terminal-Normal mode.
1990 * When "redraw" is TRUE redraw the windows that show the terminal.
1991 */
1992 static void
1993may_move_terminal_to_buffer(term_T *term, int redraw)
1994{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001995 if (term->tl_vterm == NULL)
1996 return;
1997
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001998 // Update the snapshot only if something changes or the buffer does not
1999 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002000 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
2001 <= term->tl_scrollback_scrolled)
2002 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002003
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002004 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002005 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2006 &term->tl_default_color.fg, &term->tl_default_color.bg);
2007
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002008 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002009 {
2010 win_T *wp = NULL;
2011 int did_curwin = FALSE;
2012
2013 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002014 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002015 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002016 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002017 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
2018 wp->w_cursor.col = 0;
2019 wp->w_valid = 0;
2020 if (wp->w_cursor.lnum >= wp->w_height)
2021 {
2022 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002023
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002024 if (wp->w_topline < min_topline)
2025 wp->w_topline = min_topline;
2026 }
2027 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002028 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002029 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002030 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002031}
2032
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002033#if defined(FEAT_TIMERS) || defined(PROTO)
2034/*
2035 * Check if any terminal timer expired. If so, copy text from the terminal to
2036 * the buffer.
2037 * Return the time until the next timer will expire.
2038 */
2039 int
2040term_check_timers(int next_due_arg, proftime_T *now)
2041{
2042 term_T *term;
2043 int next_due = next_due_arg;
2044
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002045 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002046 {
2047 if (term->tl_timer_set && !term->tl_normal_mode)
2048 {
2049 long this_due = proftime_time_left(&term->tl_timer_due, now);
2050
2051 if (this_due <= 1)
2052 {
2053 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002054 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002055 }
2056 else if (next_due == -1 || next_due > this_due)
2057 next_due = this_due;
2058 }
2059 }
2060
2061 return next_due;
2062}
2063#endif
2064
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002065/*
2066 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
2067 * otherwise end it.
2068 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002069 static void
2070set_terminal_mode(term_T *term, int normal_mode)
2071{
2072 term->tl_normal_mode = normal_mode;
LemonBoy2bf52dd2022-04-09 18:17:34 +01002073 may_trigger_modechanged();
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002074 if (!normal_mode)
2075 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002076 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002077 if (term->tl_buffer == curbuf)
2078 maketitle();
2079}
2080
2081/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002082 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002083 * Move the vterm contents into the scrollback buffer and free the vterm.
2084 */
2085 static void
2086cleanup_vterm(term_T *term)
2087{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002088 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002089 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002090 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002091 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002092}
2093
2094/*
2095 * Switch from Terminal-Job mode to Terminal-Normal mode.
2096 * Suspends updating the terminal window.
2097 */
2098 static void
2099term_enter_normal_mode(void)
2100{
2101 term_T *term = curbuf->b_term;
2102
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002103 set_terminal_mode(term, TRUE);
2104
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002105 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002106 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002107
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002108 // Move the window cursor to the position of the cursor in the
2109 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002110 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2111 + term->tl_cursor_pos.row + 1;
2112 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002113 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2114 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002115 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002116
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002117 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002118 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2119}
2120
2121/*
2122 * Returns TRUE if the current window contains a terminal and we are in
2123 * Terminal-Normal mode.
2124 */
2125 int
2126term_in_normal_mode(void)
2127{
2128 term_T *term = curbuf->b_term;
2129
2130 return term != NULL && term->tl_normal_mode;
2131}
2132
2133/*
2134 * Switch from Terminal-Normal mode to Terminal-Job mode.
2135 * Restores updating the terminal window.
2136 */
2137 void
2138term_enter_job_mode()
2139{
2140 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002141
2142 set_terminal_mode(term, FALSE);
2143
2144 if (term->tl_channel_closed)
2145 cleanup_vterm(term);
2146 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002147#ifdef FEAT_PROP_POPUP
2148 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002149 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002150#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002151}
2152
2153/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002154 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002155 * Note: while waiting a terminal may be closed and freed if the channel is
Bram Moolenaar829c8e82021-12-14 08:41:38 +00002156 * closed and ++close was used. This may even happen before we get here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002157 */
2158 static int
2159term_vgetc()
2160{
2161 int c;
2162 int save_State = State;
Bram Moolenaar829c8e82021-12-14 08:41:38 +00002163 int modify_other_keys = curbuf->b_term->tl_vterm == NULL ? FALSE
2164 : vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002165
Bram Moolenaar24959102022-05-07 20:01:16 +01002166 State = MODE_TERMINAL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002167 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002168#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002169 ctrl_break_was_pressed = FALSE;
2170#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002171 if (modify_other_keys)
2172 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002173 c = vgetc();
2174 got_int = FALSE;
2175 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002176 if (modify_other_keys)
2177 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002178 return c;
2179}
2180
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002181static int mouse_was_outside = FALSE;
2182
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002183/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002184 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002185 * Return FAIL when the key needs to be handled in Normal mode.
2186 * Return OK when the key was dropped or sent to the terminal.
2187 */
2188 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002189send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002190{
2191 char msg[KEY_BUF_LEN];
2192 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002193 int dragging_outside = FALSE;
2194
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002195 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002196 switch (c)
2197 {
2198 case NUL:
2199 case K_ZERO:
2200 if (typed)
2201 stuffcharReadbuff(c);
2202 return FAIL;
2203
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002204 case K_TABLINE:
2205 stuffcharReadbuff(c);
2206 return FAIL;
2207
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002208 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002209 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002210 return FAIL;
2211
2212 case K_LEFTDRAG:
2213 case K_MIDDLEDRAG:
2214 case K_RIGHTDRAG:
2215 case K_X1DRAG:
2216 case K_X2DRAG:
2217 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002218 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002219 case K_LEFTMOUSE:
2220 case K_LEFTMOUSE_NM:
2221 case K_LEFTRELEASE:
2222 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002223 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002224 case K_MIDDLEMOUSE:
2225 case K_MIDDLERELEASE:
2226 case K_RIGHTMOUSE:
2227 case K_RIGHTRELEASE:
2228 case K_X1MOUSE:
2229 case K_X1RELEASE:
2230 case K_X2MOUSE:
2231 case K_X2RELEASE:
2232
2233 case K_MOUSEUP:
2234 case K_MOUSEDOWN:
2235 case K_MOUSELEFT:
2236 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002237 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002238 int row = mouse_row;
2239 int col = mouse_col;
2240
2241#ifdef FEAT_PROP_POPUP
2242 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002243 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002244 row -= popup_top_extra(curwin);
2245 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002246 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002247#endif
2248 if (row < W_WINROW(curwin)
2249 || row >= (W_WINROW(curwin) + curwin->w_height)
2250 || col < curwin->w_wincol
2251 || col >= W_ENDCOL(curwin)
2252 || dragging_outside)
2253 {
2254 // click or scroll outside the current window or on status
2255 // line or vertical separator
2256 if (typed)
2257 {
2258 stuffcharReadbuff(c);
2259 mouse_was_outside = TRUE;
2260 }
2261 return FAIL;
2262 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002263 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002264 break;
2265
2266 case K_COMMAND:
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002267 case K_SCRIPT_COMMAND:
2268 return do_cmdkey_command(c, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002269 }
2270 if (typed)
2271 mouse_was_outside = FALSE;
2272
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002273 // Convert the typed key to a sequence of bytes for the job.
2274 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002275 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002276 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002277 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2278 (char_u *)msg, (int)len, NULL);
2279
2280 return OK;
2281}
2282
2283 static void
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002284position_cursor(win_T *wp, VTermPos *pos)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002285{
2286 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2287 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002288#ifdef FEAT_PROP_POPUP
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002289 if (popup_is_popup(wp))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002290 {
Bram Moolenaarf5452692020-11-28 21:56:06 +01002291 wp->w_wrow += popup_top_extra(wp);
2292 wp->w_wcol += popup_left_extra(wp);
Bram Moolenaar6a076442020-11-15 20:32:58 +01002293 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002294 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01002295 else
2296 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002297#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002298 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2299}
2300
2301/*
2302 * Handle CTRL-W "": send register contents to the job.
2303 */
2304 static void
2305term_paste_register(int prev_c UNUSED)
2306{
2307 int c;
2308 list_T *l;
2309 listitem_T *item;
2310 long reglen = 0;
2311 int type;
2312
2313#ifdef FEAT_CMDL_INFO
2314 if (add_to_showcmd(prev_c))
2315 if (add_to_showcmd('"'))
2316 out_flush();
2317#endif
2318 c = term_vgetc();
2319#ifdef FEAT_CMDL_INFO
2320 clear_showcmd();
2321#endif
2322 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002323 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002324 return;
2325
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002326 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002327 if (c == '=' && get_expr_register() != '=')
2328 return;
2329 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002330 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002331 return;
2332
2333 l = (list_T *)get_reg_contents(c, GREG_LIST);
2334 if (l != NULL)
2335 {
2336 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002337 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002338 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002339 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002340#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002341 char_u *tmp = s;
2342
2343 if (!enc_utf8 && enc_codepage > 0)
2344 {
2345 WCHAR *ret = NULL;
2346 int length = 0;
2347
2348 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2349 (int)STRLEN(s), &ret, &length);
2350 if (ret != NULL)
2351 {
2352 WideCharToMultiByte_alloc(CP_UTF8, 0,
2353 ret, length, (char **)&s, &length, 0, 0);
2354 vim_free(ret);
2355 }
2356 }
2357#endif
2358 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2359 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002360#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002361 if (tmp != s)
2362 vim_free(s);
2363#endif
2364
2365 if (item->li_next != NULL || type == MLINE)
2366 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2367 (char_u *)"\r", 1, NULL);
2368 }
2369 list_free(l);
2370 }
2371}
2372
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002373/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002374 * Return TRUE when waiting for a character in the terminal, the cursor of the
2375 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002376 */
2377 int
2378terminal_is_active()
2379{
2380 return in_terminal_loop != NULL;
2381}
2382
Bram Moolenaar83d47902020-03-26 20:34:00 +01002383/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002384 * Return the highight group ID for the terminal and the window.
Bram Moolenaar83d47902020-03-26 20:34:00 +01002385 */
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002386 static int
2387term_get_highlight_id(term_T *term, win_T *wp)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002388{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002389 char_u *name;
2390
2391 if (wp != NULL && *wp->w_p_wcr != NUL)
2392 name = wp->w_p_wcr;
2393 else if (term->tl_highlight_name != NULL)
2394 name = term->tl_highlight_name;
2395 else
2396 name = (char_u*)"Terminal";
2397
2398 return syn_name2id(name);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002399}
2400
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002401#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002402 cursorentry_T *
2403term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2404{
2405 term_T *term = in_terminal_loop;
2406 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002407 int id;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002408 guicolor_T term_fg = INVALCOLOR;
2409 guicolor_T term_bg = INVALCOLOR;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002410
Bram Moolenaara80faa82020-04-12 19:37:17 +02002411 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002412 entry.shape = entry.mshape =
2413 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2414 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2415 SHAPE_BLOCK;
2416 entry.percentage = 20;
2417 if (term->tl_cursor_blink)
2418 {
2419 entry.blinkwait = 700;
2420 entry.blinkon = 400;
2421 entry.blinkoff = 250;
2422 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002423
Bram Moolenaar83d47902020-03-26 20:34:00 +01002424 // The highlight group overrules the defaults.
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002425 id = term_get_highlight_id(term, curwin);
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002426 if (id != 0)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002427 syn_id2colors(id, &term_fg, &term_bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002428 if (term_bg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002429 *fg = term_bg;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002430 else
2431 *fg = gui.back_pixel;
2432
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002433 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002434 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002435 if (term_fg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002436 *bg = term_fg;
2437 else
2438 *bg = gui.norm_pixel;
2439 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002440 else
2441 *bg = color_name2handle(term->tl_cursor_color);
2442 entry.name = "n";
2443 entry.used_for = SHAPE_CURSOR;
2444
2445 return &entry;
2446}
2447#endif
2448
Bram Moolenaard317b382018-02-08 22:33:31 +01002449 static void
2450may_output_cursor_props(void)
2451{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002452 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002453 || last_set_cursor_shape != desired_cursor_shape
2454 || last_set_cursor_blink != desired_cursor_blink)
2455 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002456 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002457 last_set_cursor_shape = desired_cursor_shape;
2458 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002459 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002460 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002461 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002462 ui_cursor_shape_forced(TRUE);
2463 else
2464 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2465 }
2466}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002467
Bram Moolenaard317b382018-02-08 22:33:31 +01002468/*
2469 * Set the cursor color and shape, if not last set to these.
2470 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002471 static void
2472may_set_cursor_props(term_T *term)
2473{
2474#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002475 // For the GUI the cursor properties are obtained with
2476 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002477 if (gui.in_use)
2478 return;
2479#endif
2480 if (in_terminal_loop == term)
2481 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002482 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002483 desired_cursor_shape = term->tl_cursor_shape;
2484 desired_cursor_blink = term->tl_cursor_blink;
2485 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002486 }
2487}
2488
Bram Moolenaard317b382018-02-08 22:33:31 +01002489/*
2490 * Reset the desired cursor properties and restore them when needed.
2491 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002492 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002493prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002494{
2495#ifdef FEAT_GUI
2496 if (gui.in_use)
2497 return;
2498#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002499 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002500 desired_cursor_shape = -1;
2501 desired_cursor_blink = -1;
2502 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002503}
2504
2505/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002506 * Returns TRUE if the current window contains a terminal and we are sending
2507 * keys to the job.
2508 * If "check_job_status" is TRUE update the job status.
2509 */
2510 static int
2511term_use_loop_check(int check_job_status)
2512{
2513 term_T *term = curbuf->b_term;
2514
2515 return term != NULL
2516 && !term->tl_normal_mode
2517 && term->tl_vterm != NULL
2518 && term_job_running_check(term, check_job_status);
2519}
2520
2521/*
2522 * Returns TRUE if the current window contains a terminal and we are sending
2523 * keys to the job.
2524 */
2525 int
2526term_use_loop(void)
2527{
2528 return term_use_loop_check(FALSE);
2529}
2530
2531/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002532 * Called when entering a window with the mouse. If this is a terminal window
2533 * we may want to change state.
2534 */
2535 void
2536term_win_entered()
2537{
2538 term_T *term = curbuf->b_term;
2539
2540 if (term != NULL)
2541 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002542 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002543 {
2544 reset_VIsual_and_resel();
Bram Moolenaar24959102022-05-07 20:01:16 +01002545 if (State & MODE_INSERT)
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002546 stop_insert_mode = TRUE;
2547 }
2548 mouse_was_outside = FALSE;
2549 enter_mouse_col = mouse_col;
2550 enter_mouse_row = mouse_row;
2551 }
2552}
2553
Bram Moolenaara48d4e42021-12-08 22:13:38 +00002554 void
2555term_focus_change(int in_focus)
2556{
2557 term_T *term = curbuf->b_term;
2558
2559 if (term != NULL && term->tl_vterm != NULL)
2560 {
2561 VTermState *state = vterm_obtain_state(term->tl_vterm);
2562
2563 if (in_focus)
2564 vterm_state_focus_in(state);
2565 else
2566 vterm_state_focus_out(state);
2567 term_forward_output(term);
2568 }
2569}
2570
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002571/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002572 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2573 * Return the Ctrl-key value in that case.
2574 */
2575 static int
2576raw_c_to_ctrl(int c)
2577{
2578 if ((mod_mask & MOD_MASK_CTRL)
2579 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2580 return c & 0x1f;
2581 return c;
2582}
2583
2584/*
2585 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2586 * May set "mod_mask".
2587 */
2588 static int
2589ctrl_to_raw_c(int c)
2590{
2591 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2592 {
2593 mod_mask |= MOD_MASK_CTRL;
2594 return c + '@';
2595 }
2596 return c;
2597}
2598
2599/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002600 * Wait for input and send it to the job.
2601 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2602 * when there is no more typahead.
2603 * Return when the start of a CTRL-W command is typed or anything else that
2604 * should be handled as a Normal mode command.
2605 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2606 * the terminal was closed.
2607 */
2608 int
2609terminal_loop(int blocking)
2610{
2611 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002612 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002613 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002614 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002615#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002616 int tty_fd = curbuf->b_term->tl_job->jv_channel
2617 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002618#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002619 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002620
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002621 // Remember the terminal we are sending keys to. However, the terminal
2622 // might be closed while waiting for a character, e.g. typing "exit" in a
2623 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2624 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002625 in_terminal_loop = curbuf->b_term;
2626
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002627 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002628 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002629 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002630 if (termwinkey == Ctrl_W)
2631 termwinkey = 0;
2632 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002633 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002634 may_set_cursor_props(curbuf->b_term);
2635
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002636 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002637 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002638#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002639 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002640#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002641 // TODO: skip screen update when handling a sequence of keys.
2642 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002643 while (must_redraw != 0)
2644 if (update_screen(0) == FAIL)
2645 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002646 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002647 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002648 break;
2649
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002650 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002651 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002652
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002653 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002654 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002655 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002656 // Job finished while waiting for a character. Push back the
2657 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002658 if (raw_c != K_IGNORE)
2659 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002660 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002661 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002662 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002663 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002664 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002665
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002666#ifdef UNIX
2667 /*
2668 * The shell or another program may change the tty settings. Getting
2669 * them for every typed character is a bit of overhead, but it's needed
2670 * for the first character typed, e.g. when Vim starts in a shell.
2671 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002672 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002673 {
2674 ttyinfo_T info;
2675
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002676 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002677 if (get_tty_info(tty_fd, &info) == OK)
2678 term_backspace_char = info.backspace;
2679 }
2680#endif
2681
Bram Moolenaar4f974752019-02-17 17:44:42 +01002682#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002683 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2684 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002685 if (ctrl_break_was_pressed)
2686 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2687#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002688 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2689 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002690 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002691#ifdef FEAT_GUI
2692 && !curbuf->b_term->tl_system
2693#endif
2694 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002695 {
2696 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002697 int prev_raw_c = raw_c;
2698 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002699
2700#ifdef FEAT_CMDL_INFO
2701 if (add_to_showcmd(c))
2702 out_flush();
2703#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002704 raw_c = term_vgetc();
2705 c = raw_c_to_ctrl(raw_c);
2706
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002707#ifdef FEAT_CMDL_INFO
2708 clear_showcmd();
2709#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002710 if (!term_use_loop_check(TRUE)
2711 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002712 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002713 break;
2714
2715 if (prev_c == Ctrl_BSL)
2716 {
2717 if (c == Ctrl_N)
2718 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002719 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002720 term_enter_normal_mode();
2721 ret = FAIL;
2722 goto theend;
2723 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002724 // Send both keys to the terminal, first one here, second one
2725 // below.
2726 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2727 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002728 }
2729 else if (c == Ctrl_C)
2730 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002731 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002732 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2733 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002734 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002735 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002736 // "CTRL-W .": send CTRL-W to the job
2737 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002738 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002739 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002740 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002741 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002742 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002743 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002744 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002745 else if (c == 'N')
2746 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002747 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002748 term_enter_normal_mode();
2749 ret = FAIL;
2750 goto theend;
2751 }
2752 else if (c == '"')
2753 {
2754 term_paste_register(prev_c);
2755 continue;
2756 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002757 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002758 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002759 // space for CTRL-W, modifier, multi-byte char and NUL
2760 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002761
2762 // Put the command into the typeahead buffer, when using the
2763 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2764 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002765 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002766 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002767 ret = OK;
2768 goto theend;
2769 }
2770 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002771# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002772 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002773 {
2774 WCHAR wc;
2775 char_u mb[3];
2776
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002777 mb[0] = (unsigned)raw_c >> 8;
2778 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002779 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002780 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002781 }
2782# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002783 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002784 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002785 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002786 // We are sure to come back here, don't reset the cursor color
2787 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002788 restore_cursor = FALSE;
2789
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002790 ret = OK;
2791 goto theend;
2792 }
2793 }
2794 ret = FAIL;
2795
2796theend:
2797 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002798 if (restore_cursor)
2799 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002800
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002801 // Move a snapshot of the screen contents to the buffer, so that completion
2802 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002803 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2804 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002805
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002806 return ret;
2807}
2808
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002809 static void
2810may_toggle_cursor(term_T *term)
2811{
2812 if (in_terminal_loop == term)
2813 {
2814 if (term->tl_cursor_visible)
2815 cursor_on();
2816 else
2817 cursor_off();
2818 }
2819}
2820
2821/*
2822 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002823 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002824 */
2825 static int
2826color2index(VTermColor *color, int fg, int *boldp)
2827{
2828 int red = color->red;
2829 int blue = color->blue;
2830 int green = color->green;
2831
LemonBoyb2b3acb2022-05-20 10:10:34 +01002832 *boldp = FALSE;
2833
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002834 if (VTERM_COLOR_IS_INVALID(color))
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002835 return 0;
LemonBoyb2b3acb2022-05-20 10:10:34 +01002836
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002837 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002838 {
LemonBoyb2b3acb2022-05-20 10:10:34 +01002839 // Use the color as-is if possible, give up otherwise.
2840 if (color->index < t_colors)
2841 return color->index + 1;
2842 // 8-color terminals can actually display twice as many colors by
2843 // setting the high-intensity/bold bit.
2844 else if (t_colors == 8 && fg && color->index < 16)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002845 {
LemonBoyb2b3acb2022-05-20 10:10:34 +01002846 *boldp = TRUE;
2847 return (color->index & 7) + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002848 }
LemonBoyb2b3acb2022-05-20 10:10:34 +01002849 return 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002850 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002851
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002852 if (t_colors >= 256)
2853 {
2854 if (red == blue && red == green)
2855 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002856 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002857 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002858 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2859 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2860 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002861 int i;
2862
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002863 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002864 return 17; // 00/00/00
2865 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002866 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002867 for (i = 0; i < 23; ++i)
2868 if (red < cutoff[i])
2869 return i + 233;
2870 return 256;
2871 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002872 {
2873 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2874 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002875
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002876 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002877 for (ri = 0; ri < 5; ++ri)
2878 if (red < cutoff[ri])
2879 break;
2880 for (gi = 0; gi < 5; ++gi)
2881 if (green < cutoff[gi])
2882 break;
2883 for (bi = 0; bi < 5; ++bi)
2884 if (blue < cutoff[bi])
2885 break;
2886 return 17 + ri * 36 + gi * 6 + bi;
2887 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002888 }
2889 return 0;
2890}
2891
2892/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002893 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002894 */
2895 static int
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002896vtermAttr2hl(VTermScreenCellAttrs *cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002897{
2898 int attr = 0;
2899
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002900 if (cellattrs->bold)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002901 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002902 if (cellattrs->underline)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002903 attr |= HL_UNDERLINE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002904 if (cellattrs->italic)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002905 attr |= HL_ITALIC;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002906 if (cellattrs->strike)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002907 attr |= HL_STRIKETHROUGH;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002908 if (cellattrs->reverse)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002909 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002910 return attr;
2911}
2912
2913/*
2914 * Store Vterm attributes in "cell" from highlight flags.
2915 */
2916 static void
2917hl2vtermAttr(int attr, cellattr_T *cell)
2918{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002919 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002920 if (attr & HL_BOLD)
2921 cell->attrs.bold = 1;
2922 if (attr & HL_UNDERLINE)
2923 cell->attrs.underline = 1;
2924 if (attr & HL_ITALIC)
2925 cell->attrs.italic = 1;
2926 if (attr & HL_STRIKETHROUGH)
2927 cell->attrs.strike = 1;
2928 if (attr & HL_INVERSE)
2929 cell->attrs.reverse = 1;
2930}
2931
2932/*
2933 * Convert the attributes of a vterm cell into an attribute index.
2934 */
2935 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002936cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002937 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002938 win_T *wp,
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002939 VTermScreenCellAttrs *cellattrs,
2940 VTermColor *cellfg,
2941 VTermColor *cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002942{
2943 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002944 VTermColor *fg = cellfg;
2945 VTermColor *bg = cellbg;
2946 int is_default_fg = VTERM_COLOR_IS_DEFAULT_FG(fg);
2947 int is_default_bg = VTERM_COLOR_IS_DEFAULT_BG(bg);
2948
2949 if (is_default_fg || is_default_bg)
2950 {
2951 if (wp != NULL && *wp->w_p_wcr != NUL)
2952 {
2953 if (is_default_fg)
2954 fg = &wp->w_term_wincolor.fg;
2955 if (is_default_bg)
2956 bg = &wp->w_term_wincolor.bg;
2957 }
2958 else
2959 {
2960 if (is_default_fg)
2961 fg = &term->tl_default_color.fg;
2962 if (is_default_bg)
2963 bg = &term->tl_default_color.bg;
2964 }
2965 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002966
2967#ifdef FEAT_GUI
2968 if (gui.in_use)
2969 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002970 guicolor_T guifg = gui_mch_get_rgb_color(fg->red, fg->green, fg->blue);
2971 guicolor_T guibg = gui_mch_get_rgb_color(bg->red, bg->green, bg->blue);
2972 return get_gui_attr_idx(attr, guifg, guibg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002973 }
2974 else
2975#endif
2976#ifdef FEAT_TERMGUICOLORS
2977 if (p_tgc)
2978 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002979 guicolor_T tgcfg = VTERM_COLOR_IS_INVALID(fg)
2980 ? INVALCOLOR
2981 : gui_get_rgb_color_cmn(fg->red, fg->green, fg->blue);
2982 guicolor_T tgcbg = VTERM_COLOR_IS_INVALID(bg)
2983 ? INVALCOLOR
2984 : gui_get_rgb_color_cmn(bg->red, bg->green, bg->blue);
2985 return get_tgc_attr_idx(attr, tgcfg, tgcbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002986 }
2987 else
2988#endif
2989 {
2990 int bold = MAYBE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002991 int ctermfg = color2index(fg, TRUE, &bold);
2992 int ctermbg = color2index(bg, FALSE, &bold);
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002993
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002994 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002995 if (bold == TRUE)
2996 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002997
2998 return get_cterm_attr_idx(attr, ctermfg, ctermbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002999 }
3000 return 0;
3001}
3002
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003003 static void
3004set_dirty_snapshot(term_T *term)
3005{
3006 term->tl_dirty_snapshot = TRUE;
3007#ifdef FEAT_TIMERS
3008 if (!term->tl_normal_mode)
3009 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003010 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003011 profile_setlimit(100L, &term->tl_timer_due);
3012 term->tl_timer_set = TRUE;
3013 }
3014#endif
3015}
3016
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003017 static int
3018handle_damage(VTermRect rect, void *user)
3019{
3020 term_T *term = (term_T *)user;
3021
3022 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
3023 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003024 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003025 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003026 return 1;
3027}
3028
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003029 static void
3030term_scroll_up(term_T *term, int start_row, int count)
3031{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003032 win_T *wp = NULL;
3033 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003034 VTermColor fg, bg;
3035 VTermScreenCellAttrs attr;
3036 int clear_attr;
3037
Bram Moolenaara80faa82020-04-12 19:37:17 +02003038 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003039
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003040 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003041 {
3042 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003043 {
3044 // Set the color to clear lines with.
3045 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
3046 &fg, &bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003047 clear_attr = cell2attr(term, wp, &attr, &fg, &bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003048 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003049 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003050 }
3051}
3052
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003053 static int
3054handle_moverect(VTermRect dest, VTermRect src, void *user)
3055{
3056 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003057 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003058
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003059 // Scrolling up is done much more efficiently by deleting lines instead of
3060 // redrawing the text. But avoid doing this multiple times, postpone until
3061 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003062 if (dest.start_col == src.start_col
3063 && dest.end_col == src.end_col
3064 && dest.start_row < src.start_row)
3065 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003066 if (dest.start_row == 0)
3067 term->tl_postponed_scroll += count;
3068 else
3069 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003070 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003071
3072 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3073 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003074 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003075
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003076 // Note sure if the scrolling will work correctly, let's do a complete
3077 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003078 redraw_buf_later(term->tl_buffer, NOT_VALID);
3079 return 1;
3080}
3081
3082 static int
3083handle_movecursor(
3084 VTermPos pos,
3085 VTermPos oldpos UNUSED,
3086 int visible,
3087 void *user)
3088{
3089 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003090 win_T *wp = NULL;
3091 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003092
3093 term->tl_cursor_pos = pos;
3094 term->tl_cursor_visible = visible;
3095
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003096 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003097 {
3098 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003099 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003100 }
3101 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003102 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003103
3104 return 1;
3105}
3106
3107 static int
3108handle_settermprop(
3109 VTermProp prop,
3110 VTermValue *value,
3111 void *user)
3112{
3113 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003114 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003115
3116 switch (prop)
3117 {
3118 case VTERM_PROP_TITLE:
ichizokae1bd872022-01-20 14:57:29 +00003119 if (disable_vterm_title_for_testing)
3120 break;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003121 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003122 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003123 if (strval == NULL)
3124 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003125 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003126 // a blank title isn't useful, make it empty, so that "running" is
3127 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003128 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003129 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003130 // Same as blank
3131 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003132 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003133 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3134 term->tl_title = NULL;
3135 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003136 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003137 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003138#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003139 else if (!enc_utf8 && enc_codepage > 0)
3140 {
3141 WCHAR *ret = NULL;
3142 int length = 0;
3143
3144 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003145 (char*)value->string.str,
3146 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003147 if (ret != NULL)
3148 {
3149 WideCharToMultiByte_alloc(enc_codepage, 0,
3150 ret, length, (char**)&term->tl_title,
3151 &length, 0, 0);
3152 vim_free(ret);
3153 }
3154 }
3155#endif
3156 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003157 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003158 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003159 strval = NULL;
3160 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003161 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003162 if (term == curbuf->b_term)
3163 maketitle();
3164 break;
3165
3166 case VTERM_PROP_CURSORVISIBLE:
3167 term->tl_cursor_visible = value->boolean;
3168 may_toggle_cursor(term);
3169 out_flush();
3170 break;
3171
3172 case VTERM_PROP_CURSORBLINK:
3173 term->tl_cursor_blink = value->boolean;
3174 may_set_cursor_props(term);
3175 break;
3176
3177 case VTERM_PROP_CURSORSHAPE:
3178 term->tl_cursor_shape = value->number;
3179 may_set_cursor_props(term);
3180 break;
3181
3182 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003183 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003184 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003185 if (strval == NULL)
3186 break;
3187 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003188 may_set_cursor_props(term);
3189 break;
3190
3191 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003192 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003193 term->tl_using_altscreen = value->boolean;
3194 break;
3195
3196 default:
3197 break;
3198 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003199 vim_free(strval);
3200
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003201 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003202 return 1;
3203}
3204
3205/*
3206 * The job running in the terminal resized the terminal.
3207 */
3208 static int
3209handle_resize(int rows, int cols, void *user)
3210{
3211 term_T *term = (term_T *)user;
3212 win_T *wp;
3213
3214 term->tl_rows = rows;
3215 term->tl_cols = cols;
3216 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003217 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003218 term->tl_vterm_size_changed = FALSE;
3219 else
3220 {
3221 FOR_ALL_WINDOWS(wp)
3222 {
3223 if (wp->w_buffer == term->tl_buffer)
3224 {
3225 win_setheight_win(rows, wp);
3226 win_setwidth_win(cols, wp);
3227 }
3228 }
3229 redraw_buf_later(term->tl_buffer, NOT_VALID);
3230 }
3231 return 1;
3232}
3233
3234/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003235 * If the number of lines that are stored goes over 'termscrollback' then
3236 * delete the first 10%.
3237 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3238 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003239 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003240 static void
3241limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003242{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003243 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003244 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003245 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003246 int i;
3247
3248 curbuf = term->tl_buffer;
3249 for (i = 0; i < todo; ++i)
3250 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003251 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3252 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003253 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003254 }
3255 curbuf = curwin->w_buffer;
3256
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003257 gap->ga_len -= todo;
3258 mch_memmove(gap->ga_data,
3259 (sb_line_T *)gap->ga_data + todo,
3260 sizeof(sb_line_T) * gap->ga_len);
3261 if (update_buffer)
3262 term->tl_scrollback_scrolled -= todo;
3263 }
3264}
3265
3266/*
3267 * Handle a line that is pushed off the top of the screen.
3268 */
3269 static int
3270handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3271{
3272 term_T *term = (term_T *)user;
3273 garray_T *gap;
3274 int update_buffer;
3275
3276 if (term->tl_normal_mode)
3277 {
3278 // In Terminal-Normal mode the user interacts with the buffer, thus we
3279 // must not change it. Postpone adding the scrollback lines.
3280 gap = &term->tl_scrollback_postponed;
3281 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003282 }
3283 else
3284 {
3285 // First remove the lines that were appended before, the pushed line
3286 // goes above it.
3287 cleanup_scrollback(term);
3288 gap = &term->tl_scrollback;
3289 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003290 }
3291
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003292 limit_scrollback(term, gap, update_buffer);
3293
3294 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003295 {
3296 cellattr_T *p = NULL;
3297 int len = 0;
3298 int i;
3299 int c;
3300 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003301 int text_len;
3302 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003303 sb_line_T *line;
3304 garray_T ga;
3305 cellattr_T fill_attr = term->tl_default_color;
3306
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003307 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003308 for (i = 0; i < cols; ++i)
3309 if (cells[i].chars[0] != 0)
3310 len = i + 1;
3311 else
3312 cell2cellattr(&cells[i], &fill_attr);
3313
3314 ga_init2(&ga, 1, 100);
3315 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003316 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003317 if (p != NULL)
3318 {
3319 for (col = 0; col < len; col += cells[col].width)
3320 {
3321 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3322 {
3323 ga.ga_len = 0;
3324 break;
3325 }
3326 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3327 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3328 (char_u *)ga.ga_data + ga.ga_len);
3329 cell2cellattr(&cells[col], &p[col]);
3330 }
3331 }
3332 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003333 {
3334 if (update_buffer)
3335 text = (char_u *)"";
3336 else
3337 text = vim_strsave((char_u *)"");
3338 text_len = 0;
3339 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003340 else
3341 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003342 text = ga.ga_data;
3343 text_len = ga.ga_len;
3344 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003345 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003346 if (update_buffer)
3347 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003348
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003349 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003350 line->sb_cols = len;
3351 line->sb_cells = p;
3352 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003353 if (update_buffer)
3354 {
3355 line->sb_text = NULL;
3356 ++term->tl_scrollback_scrolled;
3357 ga_clear(&ga); // free the text
3358 }
3359 else
3360 {
3361 line->sb_text = text;
3362 ga_init(&ga); // text is kept in tl_scrollback_postponed
3363 }
3364 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003365 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003366 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003367}
3368
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003369/*
3370 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3371 * received and stored in tl_scrollback_postponed.
3372 */
3373 static void
3374handle_postponed_scrollback(term_T *term)
3375{
3376 int i;
3377
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003378 if (term->tl_scrollback_postponed.ga_len == 0)
3379 return;
3380 ch_log(NULL, "Moving postponed scrollback to scrollback");
3381
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003382 // First remove the lines that were appended before, the pushed lines go
3383 // above it.
3384 cleanup_scrollback(term);
3385
3386 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3387 {
3388 char_u *text;
3389 sb_line_T *pp_line;
3390 sb_line_T *line;
3391
3392 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3393 break;
3394 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3395
3396 text = pp_line->sb_text;
3397 if (text == NULL)
3398 text = (char_u *)"";
3399 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3400 vim_free(pp_line->sb_text);
3401
3402 line = (sb_line_T *)term->tl_scrollback.ga_data
3403 + term->tl_scrollback.ga_len;
3404 line->sb_cols = pp_line->sb_cols;
3405 line->sb_cells = pp_line->sb_cells;
3406 line->sb_fill_attr = pp_line->sb_fill_attr;
3407 line->sb_text = NULL;
3408 ++term->tl_scrollback_scrolled;
3409 ++term->tl_scrollback.ga_len;
3410 }
3411
3412 ga_clear(&term->tl_scrollback_postponed);
3413 limit_scrollback(term, &term->tl_scrollback, TRUE);
3414}
3415
LemonBoy77771d32022-04-13 11:47:25 +01003416/*
3417 * Called when the terminal wants to ring the system bell.
3418 */
3419 static int
3420handle_bell(void *user UNUSED)
3421{
Bram Moolenaaraae97622022-04-13 14:28:07 +01003422 vim_beep(BO_TERM);
LemonBoy77771d32022-04-13 11:47:25 +01003423 return 0;
3424}
3425
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003426static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003427 handle_damage, // damage
3428 handle_moverect, // moverect
3429 handle_movecursor, // movecursor
3430 handle_settermprop, // settermprop
LemonBoy77771d32022-04-13 11:47:25 +01003431 handle_bell, // bell
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003432 handle_resize, // resize
3433 handle_pushline, // sb_pushline
3434 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003435};
3436
3437/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003438 * Do the work after the channel of a terminal was closed.
3439 * Must be called only when updating_screen is FALSE.
3440 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3441 */
3442 static int
3443term_after_channel_closed(term_T *term)
3444{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003445 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003446 if (!term->tl_normal_mode)
3447 {
3448 int fnum = term->tl_buffer->b_fnum;
3449
3450 cleanup_vterm(term);
3451
3452 if (term->tl_finish == TL_FINISH_CLOSE)
3453 {
3454 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003455 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003456#ifdef FEAT_PROP_POPUP
3457 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003458
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003459 // If this was a terminal in a popup window, go back to the
3460 // previous window.
3461 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3462 {
3463 pwin = curwin;
3464 if (win_valid(prevwin))
3465 win_enter(prevwin, FALSE);
3466 }
3467 else
3468#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003469 // If this is the last normal window: exit Vim.
3470 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3471 {
3472 exarg_T ea;
3473
Bram Moolenaara80faa82020-04-12 19:37:17 +02003474 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003475 ex_quit(&ea);
3476 return TRUE;
3477 }
3478
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003479 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003480 ch_log(NULL, "terminal job finished, closing window");
3481 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003482 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003483 if (curwin == aucmd_win)
3484 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003485 if (do_set_w_closing)
3486 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003487 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003488 if (do_set_w_closing)
3489 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003490 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003491#ifdef FEAT_PROP_POPUP
3492 if (pwin != NULL)
3493 popup_close_with_retval(pwin, 0);
3494#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003495 return TRUE;
3496 }
3497 if (term->tl_finish == TL_FINISH_OPEN
3498 && term->tl_buffer->b_nwindows == 0)
3499 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003500 char *cmd = term->tl_opencmd == NULL
3501 ? "botright sbuf %d"
3502 : (char *)term->tl_opencmd;
3503 size_t len = strlen(cmd) + 50;
3504 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003505
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003506 if (buf != NULL)
3507 {
3508 ch_log(NULL, "terminal job finished, opening window");
3509 vim_snprintf(buf, len, cmd, fnum);
3510 do_cmdline_cmd((char_u *)buf);
3511 vim_free(buf);
3512 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003513 }
3514 else
3515 ch_log(NULL, "terminal job finished");
3516 }
3517
3518 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3519 return FALSE;
3520}
3521
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003522#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3523/*
3524 * If the current window is a terminal in a popup window and the job has
3525 * finished, close the popup window and to back to the previous window.
3526 * Otherwise return FAIL.
3527 */
3528 int
3529may_close_term_popup(void)
3530{
3531 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3532 && !term_job_running(curbuf->b_term))
3533 {
3534 win_T *pwin = curwin;
3535
3536 if (win_valid(prevwin))
3537 win_enter(prevwin, FALSE);
3538 popup_close_with_retval(pwin, 0);
3539 return OK;
3540 }
3541 return FAIL;
3542}
3543#endif
3544
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003545/*
Bram Moolenaareea32af2021-11-21 14:51:13 +00003546 * Called when a channel is going to be closed, before invoking the close
3547 * callback.
3548 */
3549 void
3550term_channel_closing(channel_T *ch)
3551{
3552 term_T *term;
3553
3554 for (term = first_term; term != NULL; term = term->tl_next)
3555 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
3556 term->tl_channel_closing = TRUE;
3557}
3558
3559/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003560 * Called when a channel has been closed.
3561 * If this was a channel for a terminal window then finish it up.
3562 */
3563 void
3564term_channel_closed(channel_T *ch)
3565{
3566 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003567 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003568 int did_one = FALSE;
3569
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003570 for (term = first_term; term != NULL; term = next_term)
3571 {
3572 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003573 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003574 {
3575 term->tl_channel_closed = TRUE;
3576 did_one = TRUE;
3577
Bram Moolenaard23a8232018-02-10 18:45:26 +01003578 VIM_CLEAR(term->tl_title);
3579 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003580#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003581 if (term->tl_out_fd != NULL)
3582 {
3583 fclose(term->tl_out_fd);
3584 term->tl_out_fd = NULL;
3585 }
3586#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003587
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003588 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003589 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003590 // Cannot open or close windows now. Can happen when
3591 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003592 term->tl_channel_recently_closed = TRUE;
3593 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003594 }
3595
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003596 if (term_after_channel_closed(term))
3597 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003598 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003599 }
3600
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003601 if (did_one)
3602 {
3603 redraw_statuslines();
3604
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003605 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003606 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003607 typebuf_was_filled = TRUE;
3608
3609 term = curbuf->b_term;
3610 if (term != NULL)
3611 {
3612 if (term->tl_job == ch->ch_job)
3613 maketitle();
3614 update_cursor(term, term->tl_cursor_visible);
3615 }
3616 }
3617}
3618
3619/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003620 * To be called after resetting updating_screen: handle any terminal where the
3621 * channel was closed.
3622 */
3623 void
3624term_check_channel_closed_recently()
3625{
3626 term_T *term;
3627 term_T *next_term;
3628
3629 for (term = first_term; term != NULL; term = next_term)
3630 {
3631 next_term = term->tl_next;
3632 if (term->tl_channel_recently_closed)
3633 {
3634 term->tl_channel_recently_closed = FALSE;
3635 if (term_after_channel_closed(term))
3636 // start over, the list may have changed
3637 next_term = first_term;
3638 }
3639 }
3640}
3641
3642/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003643 * Fill one screen line from a line of the terminal.
3644 * Advances "pos" to past the last column.
3645 */
3646 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003647term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003648 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003649 win_T *wp,
3650 VTermScreen *screen,
3651 VTermPos *pos,
3652 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003653{
3654 int off = screen_get_current_line_off();
3655
3656 for (pos->col = 0; pos->col < max_col; )
3657 {
3658 VTermScreenCell cell;
3659 int c;
3660
3661 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003662 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003663
3664 c = cell.chars[0];
3665 if (c == NUL)
3666 {
3667 ScreenLines[off] = ' ';
3668 if (enc_utf8)
3669 ScreenLinesUC[off] = NUL;
3670 }
3671 else
3672 {
3673 if (enc_utf8)
3674 {
3675 int i;
3676
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003677 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003678 for (i = 0; i < Screen_mco
3679 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3680 {
3681 ScreenLinesC[i][off] = cell.chars[i + 1];
3682 if (cell.chars[i + 1] == 0)
3683 break;
3684 }
3685 if (c >= 0x80 || (Screen_mco > 0
3686 && ScreenLinesC[0][off] != 0))
3687 {
3688 ScreenLines[off] = ' ';
3689 ScreenLinesUC[off] = c;
3690 }
3691 else
3692 {
3693 ScreenLines[off] = c;
3694 ScreenLinesUC[off] = NUL;
3695 }
3696 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003697#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003698 else if (has_mbyte && c >= 0x80)
3699 {
3700 char_u mb[MB_MAXBYTES+1];
3701 WCHAR wc = c;
3702
3703 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3704 (char*)mb, 2, 0, 0) > 1)
3705 {
3706 ScreenLines[off] = mb[0];
3707 ScreenLines[off + 1] = mb[1];
3708 cell.width = mb_ptr2cells(mb);
3709 }
3710 else
3711 ScreenLines[off] = c;
3712 }
3713#endif
3714 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003715 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003716 ScreenLines[off] = c;
3717 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003718 ScreenAttrs[off] = cell2attr(term, wp, &cell.attrs, &cell.fg,
3719 &cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003720
3721 ++pos->col;
3722 ++off;
3723 if (cell.width == 2)
3724 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003725 // don't set the second byte to NUL for a DBCS encoding, it
3726 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003727 if (enc_utf8)
3728 {
3729 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003730 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003731 }
3732 else if (!has_mbyte)
3733 {
3734 // Can't show a double-width character with a single-byte
3735 // 'encoding', just use a space.
3736 ScreenLines[off] = ' ';
3737 ScreenAttrs[off] = ScreenAttrs[off - 1];
3738 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003739
3740 ++pos->col;
3741 ++off;
3742 }
3743 }
3744}
3745
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003746#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003747 static void
3748update_system_term(term_T *term)
3749{
3750 VTermPos pos;
3751 VTermScreen *screen;
3752
3753 if (term->tl_vterm == NULL)
3754 return;
3755 screen = vterm_obtain_screen(term->tl_vterm);
3756
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003757 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003758 while (term->tl_toprow > 0
3759 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3760 {
3761 int save_p_more = p_more;
3762
3763 p_more = FALSE;
3764 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003765 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003766 p_more = save_p_more;
3767 --term->tl_toprow;
3768 }
3769
3770 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3771 && pos.row < Rows; ++pos.row)
3772 {
3773 if (pos.row < term->tl_rows)
3774 {
3775 int max_col = MIN(Columns, term->tl_cols);
3776
Bram Moolenaar83d47902020-03-26 20:34:00 +01003777 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003778 }
3779 else
3780 pos.col = 0;
3781
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003782 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003783 }
3784
3785 term->tl_dirty_row_start = MAX_ROW;
3786 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003787}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003788#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003789
3790/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003791 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3792 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003793 * Terminal-Normal mode.
3794 */
3795 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003796term_do_update_window(win_T *wp)
3797{
3798 term_T *term = wp->w_buffer->b_term;
3799
3800 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3801}
3802
3803/*
3804 * Called to update a window that contains an active terminal.
3805 */
3806 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003807term_update_window(win_T *wp)
3808{
3809 term_T *term = wp->w_buffer->b_term;
3810 VTerm *vterm;
3811 VTermScreen *screen;
3812 VTermState *state;
3813 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003814 int rows, cols;
3815 int newrows, newcols;
3816 int minsize;
3817 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003818
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003819 vterm = term->tl_vterm;
3820 screen = vterm_obtain_screen(vterm);
3821 state = vterm_obtain_state(vterm);
3822
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003823 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3824 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003825 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003826 {
3827 term->tl_dirty_row_start = 0;
3828 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003829
3830 if (term->tl_postponed_scroll > 0
3831 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003832 // Scrolling is usually faster than redrawing, when there are only
3833 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003834 term_scroll_up(term, 0, term->tl_postponed_scroll);
3835 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003836 }
3837
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003838 /*
3839 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003840 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003841 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003842 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003843
Bram Moolenaar498c2562018-04-15 23:45:15 +02003844 newrows = 99999;
3845 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003846 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003847 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003848 // Always use curwin, it may be a popup window.
3849 win_T *wwp = twp == NULL ? curwin : twp;
3850
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003851 // When more than one window shows the same terminal, use the
3852 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003853 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003854 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003855 newrows = MIN(newrows, wwp->w_height);
3856 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003857 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003858 if (twp == NULL)
3859 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003860 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003861 if (newrows == 99999 || newcols == 99999)
3862 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003863 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3864 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3865
Bram Moolenaareba13e42021-02-23 17:47:23 +01003866 // If no cell is visible there is no point in resizing. Also, vterm can't
3867 // handle a zero height.
3868 if (newrows == 0 || newcols == 0)
3869 return;
3870
Bram Moolenaar498c2562018-04-15 23:45:15 +02003871 if (term->tl_rows != newrows || term->tl_cols != newcols)
3872 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003873 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003874 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003875 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003876 newrows);
3877 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003878
3879 // Updating the terminal size will cause the snapshot to be cleared.
3880 // When not in terminal_loop() we need to restore it.
3881 if (term != in_terminal_loop)
3882 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003883 }
3884
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003885 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003886 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003887 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003888
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003889 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3890 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003891 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003892 if (pos.row < term->tl_rows)
3893 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003894 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003895
Bram Moolenaar83d47902020-03-26 20:34:00 +01003896 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003897 }
3898 else
3899 pos.col = 0;
3900
Bram Moolenaarf118d482018-03-13 13:14:00 +01003901 screen_line(wp->w_winrow + pos.row
3902#ifdef FEAT_MENU
3903 + winbar_height(wp)
3904#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003905 , wp->w_wincol, pos.col, wp->w_width,
3906#ifdef FEAT_PROP_POPUP
3907 popup_is_popup(wp) ? SLF_POPUP :
3908#endif
3909 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003910 }
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00003911}
3912
3913/*
3914 * Called after updating all windows: may reset dirty rows.
3915 */
3916 void
3917term_did_update_window(win_T *wp)
3918{
3919 term_T *term = wp->w_buffer->b_term;
3920
3921 if (term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode
3922 && wp->w_redr_type == 0)
3923 {
3924 term->tl_dirty_row_start = MAX_ROW;
3925 term->tl_dirty_row_end = 0;
3926 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003927}
3928
3929/*
3930 * Return TRUE if "wp" is a terminal window where the job has finished.
3931 */
3932 int
3933term_is_finished(buf_T *buf)
3934{
3935 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3936}
3937
3938/*
3939 * Return TRUE if "wp" is a terminal window where the job has finished or we
3940 * are in Terminal-Normal mode, thus we show the buffer contents.
3941 */
3942 int
3943term_show_buffer(buf_T *buf)
3944{
3945 term_T *term = buf->b_term;
3946
3947 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3948}
3949
3950/*
3951 * The current buffer is going to be changed. If there is terminal
3952 * highlighting remove it now.
3953 */
3954 void
3955term_change_in_curbuf(void)
3956{
3957 term_T *term = curbuf->b_term;
3958
3959 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3960 {
3961 free_scrollback(term);
3962 redraw_buf_later(term->tl_buffer, NOT_VALID);
3963
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003964 // The buffer is now like a normal buffer, it cannot be easily
3965 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003966 set_string_option_direct((char_u *)"buftype", -1,
3967 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3968 }
3969}
3970
3971/*
3972 * Get the screen attribute for a position in the buffer.
3973 * Use a negative "col" to get the filler background color.
3974 */
3975 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003976term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003977{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003978 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003979 term_T *term = buf->b_term;
3980 sb_line_T *line;
3981 cellattr_T *cellattr;
3982
3983 if (lnum > term->tl_scrollback.ga_len)
3984 cellattr = &term->tl_default_color;
3985 else
3986 {
3987 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3988 if (col < 0 || col >= line->sb_cols)
3989 cellattr = &line->sb_fill_attr;
3990 else
3991 cellattr = line->sb_cells + col;
3992 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003993 return cell2attr(term, wp, &cellattr->attrs, &cellattr->fg, &cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003994}
3995
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003996/*
3997 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003998 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003999 */
4000 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004001cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004002{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004003 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
4004 if (rgb->index == 0)
4005 rgb->type = VTERM_COLOR_RGB;
4006 else
4007 {
4008 rgb->type = VTERM_COLOR_INDEXED;
4009 --rgb->index;
4010 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004011}
4012
4013/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004014 * Initialize vterm color from the synID.
4015 * Returns TRUE if color is set to "fg" and "bg".
4016 * Otherwise returns FALSE.
4017 */
4018 static int
4019get_vterm_color_from_synid(int id, VTermColor *fg, VTermColor *bg)
4020{
4021#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4022 // Use the actual color for the GUI and when 'termguicolors' is set.
4023 if (0
4024# ifdef FEAT_GUI
4025 || gui.in_use
4026# endif
4027# ifdef FEAT_TERMGUICOLORS
4028 || p_tgc
4029# ifdef FEAT_VTP
4030 // Finally get INVALCOLOR on this execution path
4031 || (!p_tgc && t_colors >= 256)
4032# endif
4033# endif
4034 )
4035 {
4036 guicolor_T fg_rgb = INVALCOLOR;
4037 guicolor_T bg_rgb = INVALCOLOR;
4038
4039 if (id > 0)
4040 syn_id2colors(id, &fg_rgb, &bg_rgb);
4041
4042 if (fg_rgb != INVALCOLOR)
4043 {
4044 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
4045 fg->red = (unsigned)(rgb >> 16);
4046 fg->green = (unsigned)(rgb >> 8) & 255;
4047 fg->blue = (unsigned)rgb & 255;
4048 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
4049 }
4050 else
4051 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4052
4053 if (bg_rgb != INVALCOLOR)
4054 {
4055 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
4056 bg->red = (unsigned)(rgb >> 16);
4057 bg->green = (unsigned)(rgb >> 8) & 255;
4058 bg->blue = (unsigned)rgb & 255;
4059 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
4060 }
4061 else
4062 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4063
4064 return TRUE;
4065 }
4066 else
4067#endif
4068 if (t_colors >= 16)
4069 {
4070 int cterm_fg = -1;
4071 int cterm_bg = -1;
4072
4073 if (id > 0)
4074 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
4075
4076 if (cterm_fg >= 0)
4077 {
4078 cterm_color2vterm(cterm_fg, fg);
4079 fg->type |= VTERM_COLOR_DEFAULT_FG;
4080 }
4081 else
4082 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4083
4084 if (cterm_bg >= 0)
4085 {
4086 cterm_color2vterm(cterm_bg, bg);
4087 bg->type |= VTERM_COLOR_DEFAULT_BG;
4088 }
4089 else
4090 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4091
4092 return TRUE;
4093 }
4094
4095 return FALSE;
4096}
4097
4098 void
4099term_reset_wincolor(win_T *wp)
4100{
4101 wp->w_term_wincolor.fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4102 wp->w_term_wincolor.bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4103}
4104
4105/*
4106 * Cache the color of 'wincolor'.
4107 */
4108 void
4109term_update_wincolor(win_T *wp)
4110{
4111 int id = 0;
4112
4113 if (*wp->w_p_wcr != NUL)
4114 id = syn_name2id(wp->w_p_wcr);
4115 if (id == 0 || !get_vterm_color_from_synid(id, &wp->w_term_wincolor.fg,
4116 &wp->w_term_wincolor.bg))
4117 term_reset_wincolor(wp);
4118}
4119
4120/*
4121 * Called when option 'termguicolors' was set,
4122 * or when any highlight is changed.
4123 */
4124 void
4125term_update_wincolor_all()
4126{
4127 win_T *wp = NULL;
4128 int did_curwin = FALSE;
4129
4130 while (for_all_windows_and_curwin(&wp, &did_curwin))
4131 term_update_wincolor(wp);
4132}
4133
4134/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004135 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004136 */
4137 static void
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004138init_default_colors(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004139{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004140 VTermColor *fg, *bg;
4141 int fgval, bgval;
4142 int id;
4143
Bram Moolenaara80faa82020-04-12 19:37:17 +02004144 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004145 term->tl_default_color.width = 1;
4146 fg = &term->tl_default_color.fg;
4147 bg = &term->tl_default_color.bg;
4148
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004149 // Vterm uses a default black background. Set it to white when
4150 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004151 if (*p_bg == 'l')
4152 {
4153 fgval = 0;
4154 bgval = 255;
4155 }
4156 else
4157 {
4158 fgval = 255;
4159 bgval = 0;
4160 }
4161 fg->red = fg->green = fg->blue = fgval;
4162 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004163 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
4164 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004165
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004166 // The highlight group overrules the defaults.
4167 id = term_get_highlight_id(term, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004168
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004169 if (!get_vterm_color_from_synid(id, fg, bg))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004170 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004171#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004172 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004173#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004174
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004175 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004176 if (cterm_normal_fg_color > 0)
4177 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004178 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004179# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4180# ifdef VIMDLL
4181 if (!gui.in_use)
4182# endif
4183 {
4184 tmp = fg->red;
4185 fg->red = fg->blue;
4186 fg->blue = tmp;
4187 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004188# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004189 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004190# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004191 else
4192 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004193# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004194
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004195 if (cterm_normal_bg_color > 0)
4196 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004197 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004198# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4199# ifdef VIMDLL
4200 if (!gui.in_use)
4201# endif
4202 {
4203 tmp = fg->red;
4204 fg->red = fg->blue;
4205 fg->blue = tmp;
4206 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004207# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004208 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004209# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004210 else
4211 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004212# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004213 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004214}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004215
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004216#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4217/*
4218 * Set the 16 ANSI colors from array of RGB values
4219 */
4220 static void
4221set_vterm_palette(VTerm *vterm, long_u *rgb)
4222{
4223 int index = 0;
4224 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004225
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004226 for (; index < 16; index++)
4227 {
4228 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004229
Millyb771b6b2021-11-23 12:07:25 +00004230 color.type = VTERM_COLOR_RGB;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004231 color.red = (unsigned)(rgb[index] >> 16);
4232 color.green = (unsigned)(rgb[index] >> 8) & 255;
4233 color.blue = (unsigned)rgb[index] & 255;
Bram Moolenaar68afde42022-02-25 20:48:26 +00004234 color.index = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004235 vterm_state_set_palette_color(state, index, &color);
4236 }
4237}
4238
4239/*
4240 * Set the ANSI color palette from a list of colors
4241 */
4242 static int
4243set_ansi_colors_list(VTerm *vterm, list_T *list)
4244{
4245 int n = 0;
4246 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004247 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004248
Bram Moolenaarb0992022020-01-30 14:55:42 +01004249 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004250 {
4251 char_u *color_name;
4252 guicolor_T guicolor;
4253
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004254 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004255 if (color_name == NULL)
4256 return FAIL;
4257
4258 guicolor = GUI_GET_COLOR(color_name);
4259 if (guicolor == INVALCOLOR)
4260 return FAIL;
4261
4262 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4263 }
4264
4265 if (n != 16 || li != NULL)
4266 return FAIL;
4267
4268 set_vterm_palette(vterm, rgb);
4269
4270 return OK;
4271}
4272
4273/*
4274 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4275 */
4276 static void
4277init_vterm_ansi_colors(VTerm *vterm)
4278{
4279 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4280
LemonBoyb2b3acb2022-05-20 10:10:34 +01004281 if (var == NULL)
4282 return;
4283
4284 if (var->di_tv.v_type != VAR_LIST
4285 || var->di_tv.vval.v_list == NULL
4286 || var->di_tv.vval.v_list->lv_first == &range_list_item
4287 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004288 semsg(_(e_invalid_argument_str), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004289}
4290#endif
4291
Bram Moolenaar52acb112018-03-18 19:20:22 +01004292/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004293 * Handles a "drop" command from the job in the terminal.
4294 * "item" is the file name, "item->li_next" may have options.
4295 */
4296 static void
4297handle_drop_command(listitem_T *item)
4298{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004299 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004300 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004301 int bufnr;
4302 win_T *wp;
4303 tabpage_T *tp;
4304 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004305 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004306
4307 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4308 FOR_ALL_TAB_WINDOWS(tp, wp)
4309 {
4310 if (wp->w_buffer->b_fnum == bufnr)
4311 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004312 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004313 goto_tabpage_win(tp, wp);
4314 return;
4315 }
4316 }
4317
Bram Moolenaara80faa82020-04-12 19:37:17 +02004318 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004319
4320 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4321 && opt_item->li_tv.vval.v_dict != NULL)
4322 {
4323 dict_T *dict = opt_item->li_tv.vval.v_dict;
4324 char_u *p;
4325
Bram Moolenaar8f667172018-12-14 15:38:31 +01004326 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004327 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004328 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004329 if (p != NULL)
4330 {
4331 if (check_ff_value(p) == FAIL)
4332 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4333 else
4334 ea.force_ff = *p;
4335 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004336 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004337 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004338 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004339 if (p != NULL)
4340 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004341 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004342 if (ea.cmd != NULL)
4343 {
4344 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4345 ea.force_enc = 11;
4346 tofree = ea.cmd;
4347 }
4348 }
4349
Bram Moolenaar8f667172018-12-14 15:38:31 +01004350 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004351 if (p != NULL)
4352 get_bad_opt(p, &ea);
4353
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004354 if (dict_has_key(dict, "bin"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004355 ea.force_bin = FORCE_BIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004356 if (dict_has_key(dict, "binary"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004357 ea.force_bin = FORCE_BIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004358 if (dict_has_key(dict, "nobin"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004359 ea.force_bin = FORCE_NOBIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004360 if (dict_has_key(dict, "nobinary"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004361 ea.force_bin = FORCE_NOBIN;
4362 }
4363
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004364 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004365 if (ea.cmd == NULL)
4366 ea.cmd = (char_u *)"split";
4367 ea.arg = fname;
4368 ea.cmdidx = CMD_split;
4369 ex_splitview(&ea);
4370
4371 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004372}
4373
4374/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004375 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4376 */
4377 static int
4378is_permitted_term_api(char_u *func, char_u *pat)
4379{
4380 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4381}
4382
4383/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004384 * Handles a function call from the job running in a terminal.
4385 * "item" is the function name, "item->li_next" has the arguments.
4386 */
4387 static void
4388handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4389{
4390 char_u *func;
4391 typval_T argvars[2];
4392 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004393 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004394
4395 if (item->li_next == NULL)
4396 {
4397 ch_log(channel, "Missing function arguments for call");
4398 return;
4399 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004400 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004401
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004402 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004403 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004404 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004405 return;
4406 }
4407
4408 argvars[0].v_type = VAR_NUMBER;
4409 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4410 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004411 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004412 funcexe.fe_firstline = 1L;
4413 funcexe.fe_lastline = 1L;
4414 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004415 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004416 {
4417 clear_tv(&rettv);
4418 ch_log(channel, "Function %s called", func);
4419 }
4420 else
4421 ch_log(channel, "Calling function %s failed", func);
4422}
4423
4424/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004425 * URL decoding (also know as Percent-encoding).
4426 *
4427 * Note this function currently is only used for decoding shell's
4428 * OSC 7 escape sequence which we can assume all bytes are valid
4429 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4430 * encoding bytes like 0xfe, 0xff.
4431 */
4432 static size_t
4433url_decode(const char *src, const size_t len, char_u *dst)
4434{
4435 size_t i = 0, j = 0;
4436
4437 while (i < len)
4438 {
4439 if (src[i] == '%' && i + 2 < len)
4440 {
4441 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4442 j++;
4443 i += 3;
4444 }
4445 else
4446 {
4447 dst[j] = src[i];
4448 i++;
4449 j++;
4450 }
4451 }
4452 dst[j] = '\0';
4453 return j;
4454}
4455
4456/*
4457 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4458 *
4459 * The OSC 7 sequence has the format of
4460 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4461 * and what VTerm provides via VTermStringFragment is
4462 * "file://HOSTNAME/CURRENT/DIR"
4463 */
4464 static void
4465sync_shell_dir(VTermStringFragment *frag)
4466{
4467 int offset = 7; // len of "file://" is 7
4468 char *pos = (char *)frag->str + offset;
4469 char_u *new_dir;
4470
4471 // remove HOSTNAME to get PWD
Bram Moolenaar918b0892021-05-08 20:09:24 +02004472 while (*pos != '/' && offset < (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004473 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004474 offset += 1;
4475 pos += 1;
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004476 }
4477
Bram Moolenaar918b0892021-05-08 20:09:24 +02004478 if (offset >= (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004479 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004480 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004481 frag->str);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004482 return;
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004483 }
4484
4485 new_dir = alloc(frag->len - offset + 1);
4486 url_decode(pos, frag->len-offset, new_dir);
4487 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4488 vim_free(new_dir);
4489}
4490
4491/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004492 * Called by libvterm when it cannot recognize an OSC sequence.
4493 * We recognize a terminal API command.
4494 */
4495 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004496parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004497{
4498 term_T *term = (term_T *)user;
4499 js_read_T reader;
4500 typval_T tv;
4501 channel_T *channel = term->tl_job == NULL ? NULL
4502 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004503 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004504
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004505 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
4506 if (p_asd && command == 7)
4507 {
4508 sync_shell_dir(&frag);
4509 return 1;
4510 }
4511
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004512 if (command != 51)
4513 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004514
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004515 // Concatenate what was received until the final piece is found.
4516 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4517 {
4518 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004519 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004520 }
4521 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004522 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004523 if (!frag.final)
4524 return 1;
4525
4526 ((char *)gap->ga_data)[gap->ga_len] = 0;
4527 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004528 reader.js_fill = NULL;
4529 reader.js_used = 0;
4530 if (json_decode(&reader, &tv, 0) == OK
4531 && tv.v_type == VAR_LIST
4532 && tv.vval.v_list != NULL)
4533 {
4534 listitem_T *item = tv.vval.v_list->lv_first;
4535
4536 if (item == NULL)
4537 ch_log(channel, "Missing command");
4538 else
4539 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004540 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004541
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004542 // Make sure an invoked command doesn't delete the buffer (and the
4543 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004544 ++term->tl_buffer->b_locked;
4545
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004546 item = item->li_next;
4547 if (item == NULL)
4548 ch_log(channel, "Missing argument for %s", cmd);
4549 else if (STRCMP(cmd, "drop") == 0)
4550 handle_drop_command(item);
4551 else if (STRCMP(cmd, "call") == 0)
4552 handle_call_command(term, channel, item);
4553 else
4554 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004555 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004556 }
4557 }
4558 else
4559 ch_log(channel, "Invalid JSON received");
4560
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004561 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004562 clear_tv(&tv);
4563 return 1;
4564}
4565
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004566/*
4567 * Called by libvterm when it cannot recognize a CSI sequence.
4568 * We recognize the window position report.
4569 */
4570 static int
4571parse_csi(
4572 const char *leader UNUSED,
4573 const long args[],
4574 int argcount,
4575 const char *intermed UNUSED,
4576 char command,
4577 void *user)
4578{
4579 term_T *term = (term_T *)user;
4580 char buf[100];
4581 int len;
4582 int x = 0;
4583 int y = 0;
4584 win_T *wp;
4585
4586 // We recognize only CSI 13 t
4587 if (command != 't' || argcount != 1 || args[0] != 13)
4588 return 0; // not handled
4589
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004590 // When getting the window position is not possible or it fails it results
4591 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004592#if defined(FEAT_GUI) \
4593 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4594 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004595 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004596#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004597
4598 FOR_ALL_WINDOWS(wp)
4599 if (wp->w_buffer == term->tl_buffer)
4600 break;
4601 if (wp != NULL)
4602 {
4603#ifdef FEAT_GUI
4604 if (gui.in_use)
4605 {
4606 x += wp->w_wincol * gui.char_width;
4607 y += W_WINROW(wp) * gui.char_height;
4608 }
4609 else
4610#endif
4611 {
4612 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004613 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004614 x += wp->w_wincol * 7;
4615 y += W_WINROW(wp) * 10;
4616 }
4617 }
4618
4619 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4620 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4621 (char_u *)buf, len, NULL);
4622 return 1;
4623}
4624
Bram Moolenaard8637282020-05-20 18:41:41 +02004625static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004626 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004627 parse_csi, // csi
4628 parse_osc, // osc
Bram Moolenaar7da34152021-11-24 19:30:55 +00004629 NULL, // dcs
4630 NULL, // apc
4631 NULL, // pm
4632 NULL // sos
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004633};
4634
4635/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004636 * Use Vim's allocation functions for vterm so profiling works.
4637 */
4638 static void *
4639vterm_malloc(size_t size, void *data UNUSED)
4640{
Bram Moolenaar88137392021-11-12 16:01:15 +00004641 // make sure that the length is not zero
4642 return alloc_clear(size == 0 ? 1L : size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004643}
4644
4645 static void
4646vterm_memfree(void *ptr, void *data UNUSED)
4647{
4648 vim_free(ptr);
4649}
4650
4651static VTermAllocatorFunctions vterm_allocator = {
4652 &vterm_malloc,
4653 &vterm_memfree
4654};
4655
4656/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004657 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004658 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004659 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004660 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004661create_vterm(term_T *term, int rows, int cols)
4662{
4663 VTerm *vterm;
4664 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004665 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004666 VTermValue value;
4667
Bram Moolenaar756ef112018-04-10 12:04:27 +02004668 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004669 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004670 if (vterm == NULL)
4671 return FAIL;
4672
4673 // Allocate screen and state here, so we can bail out if that fails.
4674 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004675 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004676 if (state == NULL || screen == NULL)
4677 {
4678 vterm_free(vterm);
4679 return FAIL;
4680 }
4681
Bram Moolenaar52acb112018-03-18 19:20:22 +01004682 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004683 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004684 vterm_set_utf8(vterm, 1);
4685
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004686 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004687
4688 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004689 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004690 &term->tl_default_color.fg,
4691 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004692
Bram Moolenaar9e587872019-05-13 20:27:23 +02004693 if (t_colors < 16)
4694 // Less than 16 colors: assume that bold means using a bright color for
4695 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004696 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4697
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004698 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004699 vterm_screen_reset(screen, 1 /* hard */);
4700
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004701 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004702 vterm_screen_enable_altscreen(screen, 1);
4703
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004704 // For unix do not use a blinking cursor. In an xterm this causes the
4705 // cursor to blink if it's blinking in the xterm.
4706 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004707#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004708 if (GetCaretBlinkTime() == INFINITE)
4709 value.boolean = 0;
4710 else
4711 value.boolean = 1;
4712#else
4713 value.boolean = 0;
4714#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004715 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004716 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004717
4718 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004719}
4720
4721/*
LemonBoyb2b3acb2022-05-20 10:10:34 +01004722 * Reset the terminal palette to its default value.
4723 */
4724 static void
4725term_reset_palette(VTerm *vterm)
4726{
4727 VTermState *state = vterm_obtain_state(vterm);
4728 int index;
4729
4730 for (index = 0; index < 16; index++)
4731 {
4732 VTermColor color;
4733
4734 color.type = VTERM_COLOR_INDEXED;
4735 ansi_color2rgb(index, &color.red, &color.green, &color.blue,
4736 &color.index);
4737 // The first valid index starts at 1.
4738 color.index -= 1;
4739
4740 vterm_state_set_palette_color(state, index, &color);
4741 }
4742}
4743
4744 static void
4745term_update_palette(term_T *term)
4746{
4747 if (term_use_palette()
4748 && (term->tl_palette != NULL
4749 || find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE)
4750 != NULL))
4751 {
4752 if (term->tl_palette != NULL)
4753 set_vterm_palette(term->tl_vterm, term->tl_palette);
4754 else
4755 init_vterm_ansi_colors(term->tl_vterm);
4756 }
4757 else
4758 term_reset_palette(term->tl_vterm);
4759}
4760
4761/*
4762 * Called when option 'termguicolors' is changed.
4763 */
4764 void
4765term_update_palette_all()
4766{
4767 term_T *term;
4768
4769 FOR_ALL_TERMS(term)
4770 {
4771 if (term->tl_vterm == NULL)
4772 continue;
4773 term_update_palette(term);
4774 }
4775}
4776
4777/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004778 * Called when option 'background' or 'termguicolors' was set,
4779 * or when any highlight is changed.
Bram Moolenaarad431992021-05-03 20:40:38 +02004780 */
4781 void
4782term_update_colors_all(void)
4783{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004784 term_T *term;
Bram Moolenaarad431992021-05-03 20:40:38 +02004785
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004786 FOR_ALL_TERMS(term)
4787 {
4788 if (term->tl_vterm == NULL)
4789 continue;
4790 init_default_colors(term);
4791 vterm_state_set_default_colors(
4792 vterm_obtain_state(term->tl_vterm),
4793 &term->tl_default_color.fg,
4794 &term->tl_default_color.bg);
4795 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004796}
4797
4798/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004799 * Return the text to show for the buffer name and status.
4800 */
4801 char_u *
4802term_get_status_text(term_T *term)
4803{
4804 if (term->tl_status_text == NULL)
4805 {
4806 char_u *txt;
4807 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004808 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004809
4810 if (term->tl_normal_mode)
4811 {
4812 if (term_job_running(term))
4813 txt = (char_u *)_("Terminal");
4814 else
4815 txt = (char_u *)_("Terminal-finished");
4816 }
4817 else if (term->tl_title != NULL)
4818 txt = term->tl_title;
4819 else if (term_none_open(term))
4820 txt = (char_u *)_("active");
4821 else if (term_job_running(term))
4822 txt = (char_u *)_("running");
4823 else
4824 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004825 fname = buf_get_fname(term->tl_buffer);
4826 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004827 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004828 if (term->tl_status_text != NULL)
4829 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004830 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004831 }
4832 return term->tl_status_text;
4833}
4834
4835/*
Bram Moolenaar3ad69532021-11-19 17:01:08 +00004836 * Clear the cached value of the status text.
4837 */
4838 void
4839term_clear_status_text(term_T *term)
4840{
4841 VIM_CLEAR(term->tl_status_text);
4842}
4843
4844/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004845 * Mark references in jobs of terminals.
4846 */
4847 int
4848set_ref_in_term(int copyID)
4849{
4850 int abort = FALSE;
4851 term_T *term;
4852 typval_T tv;
4853
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004854 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004855 if (term->tl_job != NULL)
4856 {
4857 tv.v_type = VAR_JOB;
4858 tv.vval.v_job = term->tl_job;
4859 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4860 }
4861 return abort;
4862}
4863
4864/*
4865 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004866 * Returns NULL when the buffer is not for a terminal window and logs a message
4867 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004868 */
4869 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004870term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004871{
4872 buf_T *buf;
4873
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004874 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004875 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004876 --emsg_off;
4877 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004878 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004879 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004880 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004881 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004882 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004883 return buf;
4884}
4885
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004886 static void
4887clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004888{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004889 CLEAR_FIELD(*cell);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004890 cell->fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4891 cell->bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004892}
4893
4894 static void
4895dump_term_color(FILE *fd, VTermColor *color)
4896{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004897 int index;
4898
4899 if (VTERM_COLOR_IS_INDEXED(color))
4900 index = color->index + 1;
4901 else if (color->type == 0)
4902 // use RGB values
4903 index = 255;
4904 else
4905 // default color
4906 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004907 fprintf(fd, "%02x%02x%02x%d",
4908 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004909 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004910}
4911
4912/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004913 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004914 *
4915 * Each screen cell in full is:
4916 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4917 * {characters} is a space for an empty cell
4918 * For a double-width character "+" is changed to "*" and the next cell is
4919 * skipped.
4920 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4921 * when "&" use the same as the previous cell.
4922 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4923 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4924 * {color-idx} is a number from 0 to 255
4925 *
4926 * Screen cell with same width, attributes and color as the previous one:
4927 * |{characters}
4928 *
4929 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4930 *
4931 * Repeating the previous screen cell:
4932 * @{count}
4933 */
4934 void
4935f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4936{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004937 buf_T *buf;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004938 term_T *term;
4939 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004940 int max_height = 0;
4941 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004942 stat_T st;
4943 FILE *fd;
4944 VTermPos pos;
4945 VTermScreen *screen;
4946 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004947 VTermState *state;
4948 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004949
4950 if (check_restricted() || check_secure())
4951 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004952
4953 if (in_vim9script()
4954 && (check_for_buffer_arg(argvars, 0) == FAIL
4955 || check_for_string_arg(argvars, 1) == FAIL
4956 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4957 return;
4958
4959 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004960 if (buf == NULL)
4961 return;
4962 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004963 if (term->tl_vterm == NULL)
4964 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004965 emsg(_(e_job_already_finished));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004966 return;
4967 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004968
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004969 if (argvars[2].v_type != VAR_UNKNOWN)
4970 {
4971 dict_T *d;
4972
4973 if (argvars[2].v_type != VAR_DICT)
4974 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004975 emsg(_(e_dictionary_required));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004976 return;
4977 }
4978 d = argvars[2].vval.v_dict;
4979 if (d != NULL)
4980 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004981 max_height = dict_get_number(d, (char_u *)"rows");
4982 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004983 }
4984 }
4985
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004986 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004987 if (fname == NULL)
4988 return;
4989 if (mch_stat((char *)fname, &st) >= 0)
4990 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004991 semsg(_(e_file_exists_str), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004992 return;
4993 }
4994
Bram Moolenaard96ff162018-02-18 22:13:29 +01004995 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4996 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004997 semsg(_(e_cant_create_file_str), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004998 return;
4999 }
5000
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005001 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005002
5003 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005004 state = vterm_obtain_state(term->tl_vterm);
5005 vterm_state_get_cursorpos(state, &cursor_pos);
5006
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005007 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
5008 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005009 {
5010 int repeat = 0;
5011
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005012 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
5013 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005014 {
5015 VTermScreenCell cell;
5016 int same_attr;
5017 int same_chars = TRUE;
5018 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005019 int is_cursor_pos = (pos.col == cursor_pos.col
5020 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005021
5022 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005023 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005024
5025 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
5026 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01005027 int c = cell.chars[i];
5028 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02005029 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01005030
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005031 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01005032 if (i == 0)
5033 {
5034 c = (c == NUL) ? ' ' : c;
5035 pc = (pc == NUL) ? ' ' : pc;
5036 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02005037 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005038 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02005039 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005040 break;
5041 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005042 same_attr = vtermAttr2hl(&cell.attrs)
5043 == vtermAttr2hl(&prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005044 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
5045 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005046 if (same_chars && cell.width == prev_cell.width && same_attr
5047 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005048 {
5049 ++repeat;
5050 }
5051 else
5052 {
5053 if (repeat > 0)
5054 {
5055 fprintf(fd, "@%d", repeat);
5056 repeat = 0;
5057 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005058 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005059
5060 if (cell.chars[0] == NUL)
5061 fputs(" ", fd);
5062 else
5063 {
5064 char_u charbuf[10];
5065 int len;
5066
5067 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
5068 && cell.chars[i] != NUL; ++i)
5069 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02005070 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005071 fwrite(charbuf, len, 1, fd);
5072 }
5073 }
5074
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005075 // When only the characters differ we don't write anything, the
5076 // following "|", "@" or NL will indicate using the same
5077 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005078 if (cell.width != prev_cell.width || !same_attr)
5079 {
5080 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005081 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005082 else
5083 fputs("+", fd);
5084
5085 if (same_attr)
5086 {
5087 fputs("&", fd);
5088 }
5089 else
5090 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005091 fprintf(fd, "%d", vtermAttr2hl(&cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005092 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005093 fputs("&", fd);
5094 else
5095 {
5096 fputs("#", fd);
5097 dump_term_color(fd, &cell.fg);
5098 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005099 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005100 fputs("&", fd);
5101 else
5102 {
5103 fputs("#", fd);
5104 dump_term_color(fd, &cell.bg);
5105 }
5106 }
5107 }
5108
5109 prev_cell = cell;
5110 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005111
5112 if (cell.width == 2)
5113 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005114 }
5115 if (repeat > 0)
5116 fprintf(fd, "@%d", repeat);
5117 fputs("\n", fd);
5118 }
5119
5120 fclose(fd);
5121}
5122
5123/*
5124 * Called when a dump is corrupted. Put a breakpoint here when debugging.
5125 */
5126 static void
5127dump_is_corrupt(garray_T *gap)
5128{
5129 ga_concat(gap, (char_u *)"CORRUPT");
5130}
5131
5132 static void
5133append_cell(garray_T *gap, cellattr_T *cell)
5134{
5135 if (ga_grow(gap, 1) == OK)
5136 {
5137 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
5138 ++gap->ga_len;
5139 }
5140}
5141
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005142 static void
5143clear_cellattr(cellattr_T *cell)
5144{
5145 CLEAR_FIELD(*cell);
5146 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
5147 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
5148}
5149
Bram Moolenaard96ff162018-02-18 22:13:29 +01005150/*
5151 * Read the dump file from "fd" and append lines to the current buffer.
5152 * Return the cell width of the longest line.
5153 */
5154 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01005155read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005156{
5157 int c;
5158 garray_T ga_text;
5159 garray_T ga_cell;
5160 char_u *prev_char = NULL;
5161 int attr = 0;
5162 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005163 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005164 term_T *term = curbuf->b_term;
5165 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005166 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005167
5168 ga_init2(&ga_text, 1, 90);
5169 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005170 clear_cellattr(&cell);
5171 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005172 cursor_pos->row = -1;
5173 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005174
5175 c = fgetc(fd);
5176 for (;;)
5177 {
5178 if (c == EOF)
5179 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02005180 if (c == '\r')
5181 {
5182 // DOS line endings? Ignore.
5183 c = fgetc(fd);
5184 }
5185 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005186 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005187 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005188 if (ga_text.ga_data == NULL)
5189 dump_is_corrupt(&ga_text);
5190 if (ga_grow(&term->tl_scrollback, 1) == OK)
5191 {
5192 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
5193 + term->tl_scrollback.ga_len;
5194
5195 if (max_cells < ga_cell.ga_len)
5196 max_cells = ga_cell.ga_len;
5197 line->sb_cols = ga_cell.ga_len;
5198 line->sb_cells = ga_cell.ga_data;
5199 line->sb_fill_attr = term->tl_default_color;
5200 ++term->tl_scrollback.ga_len;
5201 ga_init(&ga_cell);
5202
5203 ga_append(&ga_text, NUL);
5204 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5205 ga_text.ga_len, FALSE);
5206 }
5207 else
5208 ga_clear(&ga_cell);
5209 ga_text.ga_len = 0;
5210
5211 c = fgetc(fd);
5212 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005213 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005214 {
5215 int prev_len = ga_text.ga_len;
5216
Bram Moolenaar9271d052018-02-25 21:39:46 +01005217 if (c == '>')
5218 {
5219 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005220 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005221 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5222 cursor_pos->col = ga_cell.ga_len;
5223 }
5224
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005225 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005226 c = fgetc(fd);
5227 if (c != EOF)
5228 ga_append(&ga_text, c);
5229 for (;;)
5230 {
5231 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005232 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005233 || c == EOF || c == '\n')
5234 break;
5235 ga_append(&ga_text, c);
5236 }
5237
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005238 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005239 vim_free(prev_char);
5240 if (ga_text.ga_data != NULL)
5241 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5242 ga_text.ga_len - prev_len);
5243
Bram Moolenaar9271d052018-02-25 21:39:46 +01005244 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005245 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005246 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005247 }
5248 else if (c == '+' || c == '*')
5249 {
5250 int is_bg;
5251
5252 cell.width = c == '+' ? 1 : 2;
5253
5254 c = fgetc(fd);
5255 if (c == '&')
5256 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005257 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005258 c = fgetc(fd);
5259 }
5260 else if (isdigit(c))
5261 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005262 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005263 attr = 0;
5264 while (isdigit(c))
5265 {
5266 attr = attr * 10 + (c - '0');
5267 c = fgetc(fd);
5268 }
5269 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005270
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005271 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005272 for (is_bg = 0; is_bg <= 1; ++is_bg)
5273 {
5274 if (c == '&')
5275 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005276 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005277 c = fgetc(fd);
5278 }
5279 else if (c == '#')
5280 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005281 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005282
5283 c = fgetc(fd);
5284 red = hex2nr(c);
5285 c = fgetc(fd);
5286 red = (red << 4) + hex2nr(c);
5287 c = fgetc(fd);
5288 green = hex2nr(c);
5289 c = fgetc(fd);
5290 green = (green << 4) + hex2nr(c);
5291 c = fgetc(fd);
5292 blue = hex2nr(c);
5293 c = fgetc(fd);
5294 blue = (blue << 4) + hex2nr(c);
5295 c = fgetc(fd);
5296 if (!isdigit(c))
5297 dump_is_corrupt(&ga_text);
5298 while (isdigit(c))
5299 {
5300 index = index * 10 + (c - '0');
5301 c = fgetc(fd);
5302 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005303 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005304 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005305 type = VTERM_COLOR_RGB;
5306 if (index == 0)
5307 {
5308 if (is_bg)
5309 type |= VTERM_COLOR_DEFAULT_BG;
5310 else
5311 type |= VTERM_COLOR_DEFAULT_FG;
5312 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005313 }
5314 else
5315 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005316 type = VTERM_COLOR_INDEXED;
5317 index -= 1;
5318 }
5319 if (is_bg)
5320 {
5321 cell.bg.type = type;
5322 cell.bg.red = red;
5323 cell.bg.green = green;
5324 cell.bg.blue = blue;
5325 cell.bg.index = index;
5326 }
5327 else
5328 {
5329 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005330 cell.fg.red = red;
5331 cell.fg.green = green;
5332 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005333 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005334 }
5335 }
5336 else
5337 dump_is_corrupt(&ga_text);
5338 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005339 }
5340 else
5341 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005342 }
5343 else
5344 dump_is_corrupt(&ga_text);
5345
5346 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005347 if (cell.width == 2)
5348 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005349 }
5350 else if (c == '@')
5351 {
5352 if (prev_char == NULL)
5353 dump_is_corrupt(&ga_text);
5354 else
5355 {
5356 int count = 0;
5357
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005358 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005359 for (;;)
5360 {
5361 c = fgetc(fd);
5362 if (!isdigit(c))
5363 break;
5364 count = count * 10 + (c - '0');
5365 }
5366
5367 while (count-- > 0)
5368 {
5369 ga_concat(&ga_text, prev_char);
5370 append_cell(&ga_cell, &cell);
5371 }
5372 }
5373 }
5374 else
5375 {
5376 dump_is_corrupt(&ga_text);
5377 c = fgetc(fd);
5378 }
5379 }
5380
5381 if (ga_text.ga_len > 0)
5382 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005383 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005384 dump_is_corrupt(&ga_text);
5385 ga_append(&ga_text, NUL);
5386 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5387 ga_text.ga_len, FALSE);
5388 }
5389
5390 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005391 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005392 vim_free(prev_char);
5393
5394 return max_cells;
5395}
5396
5397/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005398 * Return an allocated string with at least "text_width" "=" characters and
5399 * "fname" inserted in the middle.
5400 */
5401 static char_u *
5402get_separator(int text_width, char_u *fname)
5403{
5404 int width = MAX(text_width, curwin->w_width);
5405 char_u *textline;
5406 int fname_size;
5407 char_u *p = fname;
5408 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005409 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005410
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005411 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005412 if (textline == NULL)
5413 return NULL;
5414
5415 fname_size = vim_strsize(fname);
5416 if (fname_size < width - 8)
5417 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005418 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005419 width = MAX(text_width, fname_size + 8);
5420 }
5421 else if (fname_size > width - 8)
5422 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005423 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005424 p = gettail(fname);
5425 fname_size = vim_strsize(p);
5426 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005427 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005428 while (fname_size > width - 8)
5429 {
5430 p += (*mb_ptr2len)(p);
5431 fname_size = vim_strsize(p);
5432 }
5433
5434 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5435 textline[i] = '=';
5436 textline[i++] = ' ';
5437
5438 STRCPY(textline + i, p);
5439 off = STRLEN(textline);
5440 textline[off] = ' ';
5441 for (i = 1; i < (width - fname_size) / 2; ++i)
5442 textline[off + i] = '=';
5443 textline[off + i] = NUL;
5444
5445 return textline;
5446}
5447
5448/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005449 * Common for "term_dumpdiff()" and "term_dumpload()".
5450 */
5451 static void
5452term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5453{
5454 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005455 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005456 char_u buf1[NUMBUFLEN];
5457 char_u buf2[NUMBUFLEN];
5458 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005459 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005460 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005461 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005462 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005463 char_u *textline = NULL;
5464
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005465 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005466 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005467 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005468 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005469 if (fname1 == NULL || (do_diff && fname2 == NULL))
5470 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005471 emsg(_(e_invalid_argument));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005472 return;
5473 }
5474 fd1 = mch_fopen((char *)fname1, READBIN);
5475 if (fd1 == NULL)
5476 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005477 semsg(_(e_cant_read_file_str), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005478 return;
5479 }
5480 if (do_diff)
5481 {
5482 fd2 = mch_fopen((char *)fname2, READBIN);
5483 if (fd2 == NULL)
5484 {
5485 fclose(fd1);
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005486 semsg(_(e_cant_read_file_str), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005487 return;
5488 }
5489 }
5490
5491 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005492 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5493 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5494 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5495 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5496 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005497
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005498 if (opt.jo_term_name == NULL)
5499 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005500 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005501
Bram Moolenaar51e14382019-05-25 20:21:28 +02005502 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005503 if (fname_tofree != NULL)
5504 {
5505 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5506 opt.jo_term_name = fname_tofree;
5507 }
5508 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005509
Bram Moolenaar87abab92019-06-03 21:14:59 +02005510 if (opt.jo_bufnr_buf != NULL)
5511 {
5512 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5513
5514 // With "bufnr" argument: enter the window with this buffer and make it
5515 // empty.
5516 if (wp == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005517 semsg(_(e_invalid_argument_str), "bufnr");
Bram Moolenaar87abab92019-06-03 21:14:59 +02005518 else
5519 {
5520 buf = curbuf;
5521 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005522 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005523 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005524 redraw_later(NOT_VALID);
5525 }
5526 }
5527 else
5528 // Create a new terminal window.
5529 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5530
Bram Moolenaard96ff162018-02-18 22:13:29 +01005531 if (buf != NULL && buf->b_term != NULL)
5532 {
5533 int i;
5534 linenr_T bot_lnum;
5535 linenr_T lnum;
5536 term_T *term = buf->b_term;
5537 int width;
5538 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005539 VTermPos cursor_pos1;
5540 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005541
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005542 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005543
Bram Moolenaard96ff162018-02-18 22:13:29 +01005544 rettv->vval.v_number = buf->b_fnum;
5545
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005546 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005547 width = read_dump_file(fd1, &cursor_pos1);
5548
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005549 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005550 if (cursor_pos1.row >= 0)
5551 {
5552 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5553 coladvance(cursor_pos1.col);
5554 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005555
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005556 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005557 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005558
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005559 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005560 if (!do_diff)
5561 goto theend;
5562
5563 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5564
Bram Moolenaar4a696342018-04-05 18:45:26 +02005565 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005566 if (textline == NULL)
5567 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005568 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5569 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5570 vim_free(textline);
5571
5572 textline = get_separator(width, fname2);
5573 if (textline == NULL)
5574 goto theend;
5575 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5576 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005577 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005578
5579 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005580 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005581 if (width2 > width)
5582 {
5583 vim_free(textline);
5584 textline = alloc(width2 + 1);
5585 if (textline == NULL)
5586 goto theend;
5587 width = width2;
5588 textline[width] = NUL;
5589 }
5590 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5591
5592 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5593 {
5594 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5595 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005596 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005597 for (i = 0; i < width; ++i)
5598 textline[i] = '-';
5599 }
5600 else
5601 {
5602 char_u *line1;
5603 char_u *line2;
5604 char_u *p1;
5605 char_u *p2;
5606 int col;
5607 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5608 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5609 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5610 ->sb_cells;
5611
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005612 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005613 line1 = vim_strsave(ml_get(lnum));
5614 if (line1 == NULL)
5615 break;
5616 p1 = line1;
5617
5618 line2 = ml_get(lnum + bot_lnum);
5619 p2 = line2;
5620 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5621 {
5622 int len1 = utfc_ptr2len(p1);
5623 int len2 = utfc_ptr2len(p2);
5624
5625 textline[col] = ' ';
5626 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005627 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005628 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005629 else if (lnum == cursor_pos1.row + 1
5630 && col == cursor_pos1.col
5631 && (cursor_pos1.row != cursor_pos2.row
5632 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005633 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005634 textline[col] = '>';
5635 else if (lnum == cursor_pos2.row + 1
5636 && col == cursor_pos2.col
5637 && (cursor_pos1.row != cursor_pos2.row
5638 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005639 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005640 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005641 else if (cellattr1 != NULL && cellattr2 != NULL)
5642 {
5643 if ((cellattr1 + col)->width
5644 != (cellattr2 + col)->width)
5645 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005646 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005647 &(cellattr2 + col)->fg))
5648 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005649 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005650 &(cellattr2 + col)->bg))
5651 textline[col] = 'b';
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005652 else if (vtermAttr2hl(&(cellattr1 + col)->attrs)
5653 != vtermAttr2hl(&((cellattr2 + col)->attrs)))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005654 textline[col] = 'a';
5655 }
5656 p1 += len1;
5657 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005658 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005659 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005660
5661 while (col < width)
5662 {
5663 if (*p1 == NUL && *p2 == NUL)
5664 textline[col] = '?';
5665 else if (*p1 == NUL)
5666 {
5667 textline[col] = '+';
5668 p2 += utfc_ptr2len(p2);
5669 }
5670 else
5671 {
5672 textline[col] = '-';
5673 p1 += utfc_ptr2len(p1);
5674 }
5675 ++col;
5676 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005677
5678 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005679 }
5680 if (add_empty_scrollback(term, &term->tl_default_color,
5681 term->tl_top_diff_rows) == OK)
5682 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5683 ++bot_lnum;
5684 }
5685
5686 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5687 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005688 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005689 for (i = 0; i < width; ++i)
5690 textline[i] = '+';
5691 if (add_empty_scrollback(term, &term->tl_default_color,
5692 term->tl_top_diff_rows) == OK)
5693 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5694 ++lnum;
5695 ++bot_lnum;
5696 }
5697
5698 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005699
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005700 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005701 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005702 }
5703
5704theend:
5705 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005706 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005707 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005708 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005709 fclose(fd2);
5710}
5711
5712/*
5713 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5714 * bottom files.
5715 * Return FAIL when this is not possible.
5716 */
5717 int
5718term_swap_diff()
5719{
5720 term_T *term = curbuf->b_term;
5721 linenr_T line_count;
5722 linenr_T top_rows;
5723 linenr_T bot_rows;
5724 linenr_T bot_start;
5725 linenr_T lnum;
5726 char_u *p;
5727 sb_line_T *sb_line;
5728
5729 if (term == NULL
5730 || !term_is_finished(curbuf)
5731 || term->tl_top_diff_rows == 0
5732 || term->tl_scrollback.ga_len == 0)
5733 return FAIL;
5734
5735 line_count = curbuf->b_ml.ml_line_count;
5736 top_rows = term->tl_top_diff_rows;
5737 bot_rows = term->tl_bot_diff_rows;
5738 bot_start = line_count - bot_rows;
5739 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5740
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005741 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005742 for (lnum = 1; lnum <= top_rows; ++lnum)
5743 {
5744 p = vim_strsave(ml_get(1));
5745 if (p == NULL)
5746 return OK;
5747 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005748 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005749 vim_free(p);
5750 }
5751
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005752 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005753 for (lnum = 1; lnum <= bot_rows; ++lnum)
5754 {
5755 p = vim_strsave(ml_get(bot_start + lnum));
5756 if (p == NULL)
5757 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005758 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005759 ml_append(lnum - 1, p, 0, FALSE);
5760 vim_free(p);
5761 }
5762
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005763 // move top title to bottom
5764 p = vim_strsave(ml_get(bot_rows + 1));
5765 if (p == NULL)
5766 return OK;
5767 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005768 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005769 vim_free(p);
5770
5771 // move bottom title to top
5772 p = vim_strsave(ml_get(line_count - top_rows));
5773 if (p == NULL)
5774 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005775 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005776 ml_append(bot_rows, p, 0, FALSE);
5777 vim_free(p);
5778
Bram Moolenaard96ff162018-02-18 22:13:29 +01005779 if (top_rows == bot_rows)
5780 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005781 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005782 for (lnum = 0; lnum < top_rows; ++lnum)
5783 {
5784 sb_line_T temp;
5785
5786 temp = *(sb_line + lnum);
5787 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5788 *(sb_line + bot_start + lnum) = temp;
5789 }
5790 }
5791 else
5792 {
5793 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005794 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005795
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005796 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005797 if (temp != NULL)
5798 {
5799 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5800 mch_memmove(term->tl_scrollback.ga_data,
5801 temp + bot_start,
5802 sizeof(sb_line_T) * bot_rows);
5803 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5804 temp + top_rows,
5805 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5806 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5807 + line_count - top_rows,
5808 temp,
5809 sizeof(sb_line_T) * top_rows);
5810 vim_free(temp);
5811 }
5812 }
5813
5814 term->tl_top_diff_rows = bot_rows;
5815 term->tl_bot_diff_rows = top_rows;
5816
5817 update_screen(NOT_VALID);
5818 return OK;
5819}
5820
5821/*
5822 * "term_dumpdiff(filename, filename, options)" function
5823 */
5824 void
5825f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5826{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005827 if (in_vim9script()
5828 && (check_for_string_arg(argvars, 0) == FAIL
5829 || check_for_string_arg(argvars, 1) == FAIL
5830 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5831 return;
5832
Bram Moolenaard96ff162018-02-18 22:13:29 +01005833 term_load_dump(argvars, rettv, TRUE);
5834}
5835
5836/*
5837 * "term_dumpload(filename, options)" function
5838 */
5839 void
5840f_term_dumpload(typval_T *argvars, typval_T *rettv)
5841{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005842 if (in_vim9script()
5843 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005844 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005845 return;
5846
Bram Moolenaard96ff162018-02-18 22:13:29 +01005847 term_load_dump(argvars, rettv, FALSE);
5848}
5849
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005850/*
5851 * "term_getaltscreen(buf)" function
5852 */
5853 void
5854f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5855{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005856 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005857
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005858 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5859 return;
5860
5861 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005862 if (buf == NULL)
5863 return;
5864 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5865}
5866
5867/*
5868 * "term_getattr(attr, name)" function
5869 */
5870 void
5871f_term_getattr(typval_T *argvars, typval_T *rettv)
5872{
5873 int attr;
5874 size_t i;
5875 char_u *name;
5876
5877 static struct {
5878 char *name;
5879 int attr;
5880 } attrs[] = {
5881 {"bold", HL_BOLD},
5882 {"italic", HL_ITALIC},
5883 {"underline", HL_UNDERLINE},
5884 {"strike", HL_STRIKETHROUGH},
5885 {"reverse", HL_INVERSE},
5886 };
5887
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005888 if (in_vim9script()
5889 && (check_for_number_arg(argvars, 0) == FAIL
5890 || check_for_string_arg(argvars, 1) == FAIL))
5891 return;
5892
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005893 attr = tv_get_number(&argvars[0]);
5894 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005895 if (name == NULL)
5896 return;
5897
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005898 if (attr > HL_ALL)
5899 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02005900 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005901 if (STRCMP(name, attrs[i].name) == 0)
5902 {
5903 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5904 break;
5905 }
5906}
5907
5908/*
5909 * "term_getcursor(buf)" function
5910 */
5911 void
5912f_term_getcursor(typval_T *argvars, typval_T *rettv)
5913{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005914 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005915 term_T *term;
5916 list_T *l;
5917 dict_T *d;
5918
5919 if (rettv_list_alloc(rettv) == FAIL)
5920 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005921
5922 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5923 return;
5924
5925 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005926 if (buf == NULL)
5927 return;
5928 term = buf->b_term;
5929
5930 l = rettv->vval.v_list;
5931 list_append_number(l, term->tl_cursor_pos.row + 1);
5932 list_append_number(l, term->tl_cursor_pos.col + 1);
5933
5934 d = dict_alloc();
5935 if (d != NULL)
5936 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005937 dict_add_number(d, "visible", term->tl_cursor_visible);
5938 dict_add_number(d, "blink", blink_state_is_inverted()
5939 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5940 dict_add_number(d, "shape", term->tl_cursor_shape);
5941 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005942 list_append_dict(l, d);
5943 }
5944}
5945
5946/*
5947 * "term_getjob(buf)" function
5948 */
5949 void
5950f_term_getjob(typval_T *argvars, typval_T *rettv)
5951{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005952 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005953
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005954 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5955 return;
5956
5957 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005958 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005959 {
5960 rettv->v_type = VAR_SPECIAL;
5961 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005962 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005963 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005964
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005965 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005966 rettv->vval.v_job = buf->b_term->tl_job;
5967 if (rettv->vval.v_job != NULL)
5968 ++rettv->vval.v_job->jv_refcount;
5969}
5970
5971 static int
5972get_row_number(typval_T *tv, term_T *term)
5973{
5974 if (tv->v_type == VAR_STRING
5975 && tv->vval.v_string != NULL
5976 && STRCMP(tv->vval.v_string, ".") == 0)
5977 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005978 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005979}
5980
5981/*
5982 * "term_getline(buf, row)" function
5983 */
5984 void
5985f_term_getline(typval_T *argvars, typval_T *rettv)
5986{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005987 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005988 term_T *term;
5989 int row;
5990
5991 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005992
5993 if (in_vim9script()
5994 && (check_for_buffer_arg(argvars, 0) == FAIL
5995 || check_for_lnum_arg(argvars, 1) == FAIL))
5996 return;
5997
5998 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005999 if (buf == NULL)
6000 return;
6001 term = buf->b_term;
6002 row = get_row_number(&argvars[1], term);
6003
6004 if (term->tl_vterm == NULL)
6005 {
6006 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
6007
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006008 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006009 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
6010 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
6011 }
6012 else
6013 {
6014 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
6015 VTermRect rect;
6016 int len;
6017 char_u *p;
6018
6019 if (row < 0 || row >= term->tl_rows)
6020 return;
6021 len = term->tl_cols * MB_MAXBYTES + 1;
6022 p = alloc(len);
6023 if (p == NULL)
6024 return;
6025 rettv->vval.v_string = p;
6026
6027 rect.start_col = 0;
6028 rect.end_col = term->tl_cols;
6029 rect.start_row = row;
6030 rect.end_row = row + 1;
6031 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
6032 }
6033}
6034
6035/*
6036 * "term_getscrolled(buf)" function
6037 */
6038 void
6039f_term_getscrolled(typval_T *argvars, typval_T *rettv)
6040{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006041 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006042
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006043 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6044 return;
6045
6046 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006047 if (buf == NULL)
6048 return;
6049 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
6050}
6051
6052/*
6053 * "term_getsize(buf)" function
6054 */
6055 void
6056f_term_getsize(typval_T *argvars, typval_T *rettv)
6057{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006058 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006059 list_T *l;
6060
6061 if (rettv_list_alloc(rettv) == FAIL)
6062 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006063
6064 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6065 return;
6066
6067 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006068 if (buf == NULL)
6069 return;
6070
6071 l = rettv->vval.v_list;
6072 list_append_number(l, buf->b_term->tl_rows);
6073 list_append_number(l, buf->b_term->tl_cols);
6074}
6075
6076/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02006077 * "term_setsize(buf, rows, cols)" function
6078 */
6079 void
6080f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6081{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006082 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02006083 term_T *term;
6084 varnumber_T rows, cols;
6085
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006086 if (in_vim9script()
6087 && (check_for_buffer_arg(argvars, 0) == FAIL
6088 || check_for_number_arg(argvars, 1) == FAIL
6089 || check_for_number_arg(argvars, 2) == FAIL))
6090 return;
6091
6092 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02006093 if (buf == NULL)
6094 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006095 emsg(_(e_not_terminal_buffer));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02006096 return;
6097 }
6098 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02006099 return;
6100 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006101 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02006102 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006103 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02006104 cols = cols <= 0 ? term->tl_cols : cols;
6105 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006106 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02006107
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006108 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02006109 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
6110 term_report_winsize(term, term->tl_rows, term->tl_cols);
6111}
6112
6113/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006114 * "term_getstatus(buf)" function
6115 */
6116 void
6117f_term_getstatus(typval_T *argvars, typval_T *rettv)
6118{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006119 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006120 term_T *term;
6121 char_u val[100];
6122
6123 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006124
6125 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6126 return;
6127
6128 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006129 if (buf == NULL)
6130 return;
6131 term = buf->b_term;
6132
6133 if (term_job_running(term))
6134 STRCPY(val, "running");
6135 else
6136 STRCPY(val, "finished");
6137 if (term->tl_normal_mode)
6138 STRCAT(val, ",normal");
6139 rettv->vval.v_string = vim_strsave(val);
6140}
6141
6142/*
6143 * "term_gettitle(buf)" function
6144 */
6145 void
6146f_term_gettitle(typval_T *argvars, typval_T *rettv)
6147{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006148 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006149
6150 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006151
6152 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6153 return;
6154
6155 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006156 if (buf == NULL)
6157 return;
6158
6159 if (buf->b_term->tl_title != NULL)
6160 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
6161}
6162
6163/*
6164 * "term_gettty(buf)" function
6165 */
6166 void
6167f_term_gettty(typval_T *argvars, typval_T *rettv)
6168{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006169 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02006170 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006171 int num = 0;
6172
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006173 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006174 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006175 || check_for_opt_bool_arg(argvars, 1) == FAIL))
6176 return;
6177
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006178 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006179 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006180 if (buf == NULL)
6181 return;
6182 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02006183 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006184
6185 switch (num)
6186 {
6187 case 0:
6188 if (buf->b_term->tl_job != NULL)
6189 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006190 break;
6191 case 1:
6192 if (buf->b_term->tl_job != NULL)
6193 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006194 break;
6195 default:
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006196 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006197 return;
6198 }
6199 if (p != NULL)
6200 rettv->vval.v_string = vim_strsave(p);
6201}
6202
6203/*
6204 * "term_list()" function
6205 */
6206 void
6207f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
6208{
6209 term_T *tp;
6210 list_T *l;
6211
6212 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
6213 return;
6214
6215 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006216 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02006217 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006218 if (list_append_number(l,
6219 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6220 return;
6221}
6222
6223/*
6224 * "term_scrape(buf, row)" function
6225 */
6226 void
6227f_term_scrape(typval_T *argvars, typval_T *rettv)
6228{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006229 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006230 VTermScreen *screen = NULL;
6231 VTermPos pos;
6232 list_T *l;
6233 term_T *term;
6234 char_u *p;
6235 sb_line_T *line;
6236
6237 if (rettv_list_alloc(rettv) == FAIL)
6238 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006239
6240 if (in_vim9script()
6241 && (check_for_buffer_arg(argvars, 0) == FAIL
6242 || check_for_lnum_arg(argvars, 1) == FAIL))
6243 return;
6244
6245 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006246 if (buf == NULL)
6247 return;
6248 term = buf->b_term;
6249
6250 l = rettv->vval.v_list;
6251 pos.row = get_row_number(&argvars[1], term);
6252
6253 if (term->tl_vterm != NULL)
6254 {
6255 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006256 if (screen == NULL) // can't really happen
6257 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006258 p = NULL;
6259 line = NULL;
6260 }
6261 else
6262 {
6263 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6264
6265 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6266 return;
6267 p = ml_get_buf(buf, lnum + 1, FALSE);
6268 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6269 }
6270
6271 for (pos.col = 0; pos.col < term->tl_cols; )
6272 {
6273 dict_T *dcell;
6274 int width;
6275 VTermScreenCellAttrs attrs;
6276 VTermColor fg, bg;
6277 char_u rgb[8];
6278 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6279 int off = 0;
6280 int i;
6281
6282 if (screen == NULL)
6283 {
6284 cellattr_T *cellattr;
6285 int len;
6286
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006287 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006288 if (pos.col >= line->sb_cols)
6289 break;
6290 cellattr = line->sb_cells + pos.col;
6291 width = cellattr->width;
6292 attrs = cellattr->attrs;
6293 fg = cellattr->fg;
6294 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006295 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006296 mch_memmove(mbs, p, len);
6297 mbs[len] = NUL;
6298 p += len;
6299 }
6300 else
6301 {
6302 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006303
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006304 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6305 break;
6306 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6307 {
6308 if (cell.chars[i] == 0)
6309 break;
6310 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6311 }
6312 mbs[off] = NUL;
6313 width = cell.width;
6314 attrs = cell.attrs;
6315 fg = cell.fg;
6316 bg = cell.bg;
6317 }
6318 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006319 if (dcell == NULL)
6320 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006321 list_append_dict(l, dcell);
6322
Bram Moolenaare0be1672018-07-08 16:50:37 +02006323 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006324
6325 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6326 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006327 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006328 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6329 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006330 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006331
Bram Moolenaar87fd0922021-11-20 13:47:45 +00006332 dict_add_number(dcell, "attr",
6333 cell2attr(term, NULL, &attrs, &fg, &bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006334 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006335
6336 ++pos.col;
6337 if (width == 2)
6338 ++pos.col;
6339 }
6340}
6341
6342/*
6343 * "term_sendkeys(buf, keys)" function
6344 */
6345 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006346f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006347{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006348 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006349 char_u *msg;
6350 term_T *term;
6351
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006352 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006353 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006354 || check_for_string_arg(argvars, 1) == FAIL))
6355 return;
6356
6357 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006358 if (buf == NULL)
6359 return;
6360
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006361 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006362 if (msg == NULL)
6363 return;
6364 term = buf->b_term;
6365 if (term->tl_vterm == NULL)
6366 return;
6367
6368 while (*msg != NUL)
6369 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006370 int c;
6371
6372 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6373 {
6374 c = TO_SPECIAL(msg[1], msg[2]);
6375 msg += 3;
6376 }
6377 else
6378 {
6379 c = PTR2CHAR(msg);
6380 msg += MB_CPTR2LEN(msg);
6381 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006382 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006383 }
6384}
6385
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006386#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6387/*
6388 * "term_getansicolors(buf)" function
6389 */
6390 void
6391f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6392{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006393 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006394 term_T *term;
6395 VTermState *state;
6396 VTermColor color;
6397 char_u hexbuf[10];
6398 int index;
6399 list_T *list;
6400
6401 if (rettv_list_alloc(rettv) == FAIL)
6402 return;
6403
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006404 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6405 return;
6406
6407 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006408 if (buf == NULL)
6409 return;
6410 term = buf->b_term;
6411 if (term->tl_vterm == NULL)
6412 return;
6413
6414 list = rettv->vval.v_list;
6415 state = vterm_obtain_state(term->tl_vterm);
6416 for (index = 0; index < 16; index++)
6417 {
6418 vterm_state_get_palette_color(state, index, &color);
6419 sprintf((char *)hexbuf, "#%02x%02x%02x",
6420 color.red, color.green, color.blue);
6421 if (list_append_string(list, hexbuf, 7) == FAIL)
6422 return;
6423 }
6424}
6425
6426/*
6427 * "term_setansicolors(buf, list)" function
6428 */
6429 void
6430f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6431{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006432 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006433 term_T *term;
LemonBoyb2b3acb2022-05-20 10:10:34 +01006434 listitem_T *li;
6435 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006436
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006437 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006438 && (check_for_buffer_arg(argvars, 0) == FAIL
6439 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006440 return;
6441
6442 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006443 if (buf == NULL)
6444 return;
6445 term = buf->b_term;
6446 if (term->tl_vterm == NULL)
6447 return;
6448
6449 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6450 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006451 emsg(_(e_list_required));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006452 return;
6453 }
LemonBoyb2b3acb2022-05-20 10:10:34 +01006454 if (argvars[1].vval.v_list->lv_first == &range_list_item
6455 || argvars[1].vval.v_list->lv_len != 16)
6456 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006457 emsg(_(e_invalid_argument));
LemonBoyb2b3acb2022-05-20 10:10:34 +01006458 return;
6459 }
6460
6461 if (term->tl_palette == NULL)
6462 term->tl_palette = ALLOC_MULT(long_u, 16);
6463 if (term->tl_palette == NULL)
6464 return;
6465
6466 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
6467 {
6468 char_u *color_name;
6469 guicolor_T guicolor;
6470
6471 color_name = tv_get_string_chk(&li->li_tv);
6472 if (color_name == NULL)
6473 return;
6474
6475 guicolor = GUI_GET_COLOR(color_name);
6476 if (guicolor == INVALCOLOR)
6477 {
6478 semsg(_(e_cannot_allocate_color_str), color_name);
6479 return;
6480 }
6481
6482 term->tl_palette[n++] = GUI_MCH_GET_RGB(guicolor);
6483 }
6484
6485 term_update_palette(term);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006486}
6487#endif
6488
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006489/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006490 * "term_setapi(buf, api)" function
6491 */
6492 void
6493f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6494{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006495 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006496 term_T *term;
6497 char_u *api;
6498
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006499 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006500 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006501 || check_for_string_arg(argvars, 1) == FAIL))
6502 return;
6503
6504 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006505 if (buf == NULL)
6506 return;
6507 term = buf->b_term;
6508 vim_free(term->tl_api);
6509 api = tv_get_string_chk(&argvars[1]);
6510 if (api != NULL)
6511 term->tl_api = vim_strsave(api);
6512 else
6513 term->tl_api = NULL;
6514}
6515
6516/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006517 * "term_setrestore(buf, command)" function
6518 */
6519 void
6520f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6521{
6522#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006523 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006524 term_T *term;
6525 char_u *cmd;
6526
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006527 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006528 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006529 || check_for_string_arg(argvars, 1) == FAIL))
6530 return;
6531
6532 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006533 if (buf == NULL)
6534 return;
6535 term = buf->b_term;
6536 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006537 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006538 if (cmd != NULL)
6539 term->tl_command = vim_strsave(cmd);
6540 else
6541 term->tl_command = NULL;
6542#endif
6543}
6544
6545/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006546 * "term_setkill(buf, how)" function
6547 */
6548 void
6549f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6550{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006551 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006552 term_T *term;
6553 char_u *how;
6554
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006555 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006556 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006557 || check_for_string_arg(argvars, 1) == FAIL))
6558 return;
6559
6560 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006561 if (buf == NULL)
6562 return;
6563 term = buf->b_term;
6564 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006565 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006566 if (how != NULL)
6567 term->tl_kill = vim_strsave(how);
6568 else
6569 term->tl_kill = NULL;
6570}
6571
6572/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006573 * "term_start(command, options)" function
6574 */
6575 void
6576f_term_start(typval_T *argvars, typval_T *rettv)
6577{
6578 jobopt_T opt;
6579 buf_T *buf;
6580
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006581 if (in_vim9script()
6582 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6583 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6584 return;
6585
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006586 init_job_options(&opt);
6587 if (argvars[1].v_type != VAR_UNKNOWN
6588 && get_job_options(&argvars[1], &opt,
6589 JO_TIMEOUT_ALL + JO_STOPONEXIT
6590 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6591 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6592 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6593 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006594 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006595 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006596 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006597 return;
6598
Bram Moolenaar13568252018-03-16 20:46:58 +01006599 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006600
6601 if (buf != NULL && buf->b_term != NULL)
6602 rettv->vval.v_number = buf->b_fnum;
6603}
6604
6605/*
6606 * "term_wait" function
6607 */
6608 void
6609f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6610{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006611 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006612
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006613 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006614 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006615 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006616 return;
6617
6618 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006619 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006620 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006621 if (buf->b_term->tl_job == NULL)
6622 {
6623 ch_log(NULL, "term_wait(): no job to wait for");
6624 return;
6625 }
6626 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006627 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006628 return;
6629
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006630 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006631 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006632 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6633 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006634 // The job is dead, keep reading channel I/O until the channel is
6635 // closed. buf->b_term may become NULL if the terminal was closed while
6636 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006637 ch_log(NULL, "term_wait(): waiting for channel to close");
6638 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6639 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006640 term_flush_messages();
6641
Bram Moolenaard45aa552018-05-21 22:50:29 +02006642 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006643 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006644 // If the terminal is closed when the channel is closed the
6645 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006646 break;
Bram Moolenaareea32af2021-11-21 14:51:13 +00006647 if (buf->b_term == NULL || buf->b_term->tl_channel_closing)
6648 // came here from a close callback, only wait one time
6649 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006650 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006651
6652 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006653 }
6654 else
6655 {
6656 long wait = 10L;
6657
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006658 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006659
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006660 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006661 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006662 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006663 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006664
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006665 // Flushing messages on channels is hopefully sufficient.
6666 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006667 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006668 }
6669}
6670
6671/*
6672 * Called when a channel has sent all the lines to a terminal.
6673 * Send a CTRL-D to mark the end of the text.
6674 */
6675 void
6676term_send_eof(channel_T *ch)
6677{
6678 term_T *term;
6679
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006680 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006681 if (term->tl_job == ch->ch_job)
6682 {
6683 if (term->tl_eof_chars != NULL)
6684 {
6685 channel_send(ch, PART_IN, term->tl_eof_chars,
6686 (int)STRLEN(term->tl_eof_chars), NULL);
6687 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6688 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006689# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006690 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006691 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006692 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6693# endif
6694 }
6695}
6696
Bram Moolenaar113e1072019-01-20 15:30:40 +01006697#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006698 job_T *
6699term_getjob(term_T *term)
6700{
6701 return term != NULL ? term->tl_job : NULL;
6702}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006703#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006704
Bram Moolenaar4f974752019-02-17 17:44:42 +01006705# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006706
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006707///////////////////////////////////////
6708// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006709#ifdef PROTO
6710typedef int COORD;
6711typedef int DWORD;
6712typedef int HANDLE;
6713typedef int *DWORD_PTR;
6714typedef int HPCON;
6715typedef int HRESULT;
6716typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006717typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006718typedef int PSIZE_T;
6719typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006720typedef int BOOL;
6721# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006722#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006723
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006724HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6725HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6726HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006727BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6728BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6729void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006730
6731 static int
6732dyn_conpty_init(int verbose)
6733{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006734 static HMODULE hKerneldll = NULL;
6735 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006736 static struct
6737 {
6738 char *name;
6739 FARPROC *ptr;
6740 } conpty_entry[] =
6741 {
6742 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6743 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6744 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6745 {"InitializeProcThreadAttributeList",
6746 (FARPROC*)&pInitializeProcThreadAttributeList},
6747 {"UpdateProcThreadAttribute",
6748 (FARPROC*)&pUpdateProcThreadAttribute},
6749 {"DeleteProcThreadAttributeList",
6750 (FARPROC*)&pDeleteProcThreadAttributeList},
6751 {NULL, NULL}
6752 };
6753
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006754 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006755 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006756 if (verbose)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006757 emsg(_(e_conpty_is_not_available));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006758 return FAIL;
6759 }
6760
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006761 // No need to initialize twice.
6762 if (hKerneldll)
6763 return OK;
6764
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006765 hKerneldll = vimLoadLib("kernel32.dll");
6766 for (i = 0; conpty_entry[i].name != NULL
6767 && conpty_entry[i].ptr != NULL; ++i)
6768 {
6769 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6770 conpty_entry[i].name)) == NULL)
6771 {
6772 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006773 semsg(_(e_could_not_load_library_function_str), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006774 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006775 return FAIL;
6776 }
6777 }
6778
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006779 return OK;
6780}
6781
6782 static int
6783conpty_term_and_job_init(
6784 term_T *term,
6785 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006786 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006787 jobopt_T *opt,
6788 jobopt_T *orig_opt)
6789{
6790 WCHAR *cmd_wchar = NULL;
6791 WCHAR *cmd_wchar_copy = NULL;
6792 WCHAR *cwd_wchar = NULL;
6793 WCHAR *env_wchar = NULL;
6794 channel_T *channel = NULL;
6795 job_T *job = NULL;
6796 HANDLE jo = NULL;
6797 garray_T ga_cmd, ga_env;
6798 char_u *cmd = NULL;
6799 HRESULT hr;
6800 COORD consize;
6801 SIZE_T breq;
6802 PROCESS_INFORMATION proc_info;
6803 HANDLE i_theirs = NULL;
6804 HANDLE o_theirs = NULL;
6805 HANDLE i_ours = NULL;
6806 HANDLE o_ours = NULL;
6807
Bram Moolenaar04935fb2022-01-08 16:19:22 +00006808 ga_init2(&ga_cmd, sizeof(char*), 20);
6809 ga_init2(&ga_env, sizeof(char*), 20);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006810
6811 if (argvar->v_type == VAR_STRING)
6812 {
6813 cmd = argvar->vval.v_string;
6814 }
6815 else if (argvar->v_type == VAR_LIST)
6816 {
6817 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6818 goto failed;
6819 cmd = ga_cmd.ga_data;
6820 }
6821 if (cmd == NULL || *cmd == NUL)
6822 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006823 emsg(_(e_invalid_argument));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006824 goto failed;
6825 }
6826
6827 term->tl_arg0_cmd = vim_strsave(cmd);
6828
6829 cmd_wchar = enc_to_utf16(cmd, NULL);
6830
6831 if (cmd_wchar != NULL)
6832 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006833 // Request by CreateProcessW
6834 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006835 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006836 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6837 }
6838
6839 ga_clear(&ga_cmd);
6840 if (cmd_wchar == NULL)
6841 goto failed;
6842 if (opt->jo_cwd != NULL)
6843 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6844
6845 win32_build_env(opt->jo_env, &ga_env, TRUE);
6846 env_wchar = ga_env.ga_data;
6847
6848 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6849 goto failed;
6850 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6851 goto failed;
6852
6853 consize.X = term->tl_cols;
6854 consize.Y = term->tl_rows;
6855 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6856 &term->tl_conpty);
6857 if (FAILED(hr))
6858 goto failed;
6859
6860 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6861
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006862 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006863 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006864 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006865 if (!term->tl_siex.lpAttributeList)
6866 goto failed;
6867 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6868 0, &breq))
6869 goto failed;
6870 if (!pUpdateProcThreadAttribute(
6871 term->tl_siex.lpAttributeList, 0,
6872 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6873 sizeof(HPCON), NULL, NULL))
6874 goto failed;
6875
6876 channel = add_channel();
6877 if (channel == NULL)
6878 goto failed;
6879
6880 job = job_alloc();
6881 if (job == NULL)
6882 goto failed;
6883 if (argvar->v_type == VAR_STRING)
6884 {
6885 int argc;
6886
6887 build_argv_from_string(cmd, &job->jv_argv, &argc);
6888 }
6889 else
6890 {
6891 int argc;
6892
6893 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6894 }
6895
6896 if (opt->jo_set & JO_IN_BUF)
6897 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6898
6899 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6900 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006901 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006902 env_wchar, cwd_wchar,
6903 &term->tl_siex.StartupInfo, &proc_info))
6904 goto failed;
6905
6906 CloseHandle(i_theirs);
6907 CloseHandle(o_theirs);
6908
6909 channel_set_pipes(channel,
6910 (sock_T)i_ours,
6911 (sock_T)o_ours,
6912 (sock_T)o_ours);
6913
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006914 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006915 channel->ch_write_text_mode = TRUE;
6916
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006917 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006918 channel->ch_anonymous_pipe = TRUE;
6919
6920 jo = CreateJobObject(NULL, NULL);
6921 if (jo == NULL)
6922 goto failed;
6923
6924 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6925 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006926 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006927 CloseHandle(jo);
6928 jo = NULL;
6929 }
6930
6931 ResumeThread(proc_info.hThread);
6932 CloseHandle(proc_info.hThread);
6933
6934 vim_free(cmd_wchar);
6935 vim_free(cmd_wchar_copy);
6936 vim_free(cwd_wchar);
6937 vim_free(env_wchar);
6938
6939 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6940 goto failed;
6941
LemonBoyb2b3acb2022-05-20 10:10:34 +01006942 if (term_use_palette())
6943 {
6944 if (term->tl_palette != NULL)
6945 set_vterm_palette(term->tl_vterm, term->tl_palette);
6946 else
6947 init_vterm_ansi_colors(term->tl_vterm);
6948 }
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006949
6950 channel_set_job(channel, job, opt);
6951 job_set_options(job, opt);
6952
6953 job->jv_channel = channel;
6954 job->jv_proc_info = proc_info;
6955 job->jv_job_object = jo;
6956 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006957 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006958 ++job->jv_refcount;
6959 term->tl_job = job;
6960
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006961 // Redirecting stdout and stderr doesn't work at the job level. Instead
6962 // open the file here and handle it in. opt->jo_io was changed in
6963 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006964 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6965 {
6966 char_u *fname = opt->jo_io_name[PART_OUT];
6967
6968 ch_log(channel, "Opening output file %s", fname);
6969 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6970 if (term->tl_out_fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006971 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006972 }
6973
6974 return OK;
6975
6976failed:
6977 ga_clear(&ga_cmd);
6978 ga_clear(&ga_env);
6979 vim_free(cmd_wchar);
6980 vim_free(cmd_wchar_copy);
6981 vim_free(cwd_wchar);
6982 if (channel != NULL)
6983 channel_clear(channel);
6984 if (job != NULL)
6985 {
6986 job->jv_channel = NULL;
6987 job_cleanup(job);
6988 }
6989 term->tl_job = NULL;
6990 if (jo != NULL)
6991 CloseHandle(jo);
6992
6993 if (term->tl_siex.lpAttributeList != NULL)
6994 {
6995 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6996 vim_free(term->tl_siex.lpAttributeList);
6997 }
6998 term->tl_siex.lpAttributeList = NULL;
6999 if (o_theirs != NULL)
7000 CloseHandle(o_theirs);
7001 if (o_ours != NULL)
7002 CloseHandle(o_ours);
7003 if (i_ours != NULL)
7004 CloseHandle(i_ours);
7005 if (i_theirs != NULL)
7006 CloseHandle(i_theirs);
7007 if (term->tl_conpty != NULL)
7008 pClosePseudoConsole(term->tl_conpty);
7009 term->tl_conpty = NULL;
7010 return FAIL;
7011}
7012
7013 static void
7014conpty_term_report_winsize(term_T *term, int rows, int cols)
7015{
7016 COORD consize;
7017
7018 consize.X = cols;
7019 consize.Y = rows;
7020 pResizePseudoConsole(term->tl_conpty, consize);
7021}
7022
Bram Moolenaar840d16f2019-09-10 21:27:18 +02007023 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007024term_free_conpty(term_T *term)
7025{
7026 if (term->tl_siex.lpAttributeList != NULL)
7027 {
7028 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
7029 vim_free(term->tl_siex.lpAttributeList);
7030 }
7031 term->tl_siex.lpAttributeList = NULL;
7032 if (term->tl_conpty != NULL)
7033 pClosePseudoConsole(term->tl_conpty);
7034 term->tl_conpty = NULL;
7035}
7036
7037 int
7038use_conpty(void)
7039{
7040 return has_conpty;
7041}
7042
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007043# ifndef PROTO
7044
7045#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
7046#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01007047#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007048
7049void* (*winpty_config_new)(UINT64, void*);
7050void* (*winpty_open)(void*, void*);
7051void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
7052BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
7053void (*winpty_config_set_mouse_mode)(void*, int);
7054void (*winpty_config_set_initial_size)(void*, int, int);
7055LPCWSTR (*winpty_conin_name)(void*);
7056LPCWSTR (*winpty_conout_name)(void*);
7057LPCWSTR (*winpty_conerr_name)(void*);
7058void (*winpty_free)(void*);
7059void (*winpty_config_free)(void*);
7060void (*winpty_spawn_config_free)(void*);
7061void (*winpty_error_free)(void*);
7062LPCWSTR (*winpty_error_msg)(void*);
7063BOOL (*winpty_set_size)(void*, int, int, void*);
7064HANDLE (*winpty_agent_process)(void*);
7065
7066#define WINPTY_DLL "winpty.dll"
7067
7068static HINSTANCE hWinPtyDLL = NULL;
7069# endif
7070
7071 static int
7072dyn_winpty_init(int verbose)
7073{
7074 int i;
7075 static struct
7076 {
7077 char *name;
7078 FARPROC *ptr;
7079 } winpty_entry[] =
7080 {
7081 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
7082 {"winpty_config_free", (FARPROC*)&winpty_config_free},
7083 {"winpty_config_new", (FARPROC*)&winpty_config_new},
7084 {"winpty_config_set_mouse_mode",
7085 (FARPROC*)&winpty_config_set_mouse_mode},
7086 {"winpty_config_set_initial_size",
7087 (FARPROC*)&winpty_config_set_initial_size},
7088 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
7089 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
7090 {"winpty_error_free", (FARPROC*)&winpty_error_free},
7091 {"winpty_free", (FARPROC*)&winpty_free},
7092 {"winpty_open", (FARPROC*)&winpty_open},
7093 {"winpty_spawn", (FARPROC*)&winpty_spawn},
7094 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
7095 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
7096 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
7097 {"winpty_set_size", (FARPROC*)&winpty_set_size},
7098 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
7099 {NULL, NULL}
7100 };
7101
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007102 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007103 if (hWinPtyDLL)
7104 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007105 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
7106 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007107 if (*p_winptydll != NUL)
7108 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
7109 if (!hWinPtyDLL)
7110 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
7111 if (!hWinPtyDLL)
7112 {
7113 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007114 semsg(_(e_could_not_load_library_str_str),
Martin Tournoij1a3e5742021-07-24 13:57:29 +02007115 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
7116 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007117 return FAIL;
7118 }
7119 for (i = 0; winpty_entry[i].name != NULL
7120 && winpty_entry[i].ptr != NULL; ++i)
7121 {
7122 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
7123 winpty_entry[i].name)) == NULL)
7124 {
7125 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007126 semsg(_(e_could_not_load_library_function_str), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01007127 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007128 return FAIL;
7129 }
7130 }
7131
7132 return OK;
7133}
7134
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007135 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007136winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007137 term_T *term,
7138 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007139 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007140 jobopt_T *opt,
7141 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007142{
7143 WCHAR *cmd_wchar = NULL;
7144 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007145 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007146 channel_T *channel = NULL;
7147 job_T *job = NULL;
7148 DWORD error;
7149 HANDLE jo = NULL;
7150 HANDLE child_process_handle;
7151 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01007152 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007153 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007154 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007155 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007156
Bram Moolenaar04935fb2022-01-08 16:19:22 +00007157 ga_init2(&ga_cmd, sizeof(char*), 20);
7158 ga_init2(&ga_env, sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007159
7160 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007161 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007162 cmd = argvar->vval.v_string;
7163 }
7164 else if (argvar->v_type == VAR_LIST)
7165 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007166 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007167 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007168 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007169 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007170 if (cmd == NULL || *cmd == NUL)
7171 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007172 emsg(_(e_invalid_argument));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007173 goto failed;
7174 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007175
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007176 term->tl_arg0_cmd = vim_strsave(cmd);
7177
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007178 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007179 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007180 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007181 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007182 if (opt->jo_cwd != NULL)
7183 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007184
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007185 win32_build_env(opt->jo_env, &ga_env, TRUE);
7186 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007187
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007188 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
7189 if (term->tl_winpty_config == NULL)
7190 goto failed;
7191
7192 winpty_config_set_mouse_mode(term->tl_winpty_config,
7193 WINPTY_MOUSE_MODE_FORCE);
7194 winpty_config_set_initial_size(term->tl_winpty_config,
7195 term->tl_cols, term->tl_rows);
7196 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
7197 if (term->tl_winpty == NULL)
7198 goto failed;
7199
7200 spawn_config = winpty_spawn_config_new(
7201 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
7202 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
7203 NULL,
7204 cmd_wchar,
7205 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007206 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007207 &winpty_err);
7208 if (spawn_config == NULL)
7209 goto failed;
7210
7211 channel = add_channel();
7212 if (channel == NULL)
7213 goto failed;
7214
7215 job = job_alloc();
7216 if (job == NULL)
7217 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02007218 if (argvar->v_type == VAR_STRING)
7219 {
7220 int argc;
7221
7222 build_argv_from_string(cmd, &job->jv_argv, &argc);
7223 }
7224 else
7225 {
7226 int argc;
7227
7228 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
7229 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007230
7231 if (opt->jo_set & JO_IN_BUF)
7232 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
7233
7234 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
7235 &child_thread_handle, &error, &winpty_err))
7236 goto failed;
7237
7238 channel_set_pipes(channel,
7239 (sock_T)CreateFileW(
7240 winpty_conin_name(term->tl_winpty),
7241 GENERIC_WRITE, 0, NULL,
7242 OPEN_EXISTING, 0, NULL),
7243 (sock_T)CreateFileW(
7244 winpty_conout_name(term->tl_winpty),
7245 GENERIC_READ, 0, NULL,
7246 OPEN_EXISTING, 0, NULL),
7247 (sock_T)CreateFileW(
7248 winpty_conerr_name(term->tl_winpty),
7249 GENERIC_READ, 0, NULL,
7250 OPEN_EXISTING, 0, NULL));
7251
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007252 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007253 channel->ch_write_text_mode = TRUE;
7254
7255 jo = CreateJobObject(NULL, NULL);
7256 if (jo == NULL)
7257 goto failed;
7258
7259 if (!AssignProcessToJobObject(jo, child_process_handle))
7260 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007261 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007262 CloseHandle(jo);
7263 jo = NULL;
7264 }
7265
7266 winpty_spawn_config_free(spawn_config);
7267 vim_free(cmd_wchar);
7268 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007269 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007270
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007271 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7272 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007273
LemonBoyb2b3acb2022-05-20 10:10:34 +01007274 if (term_use_palette())
7275 {
7276 if (term->tl_palette != NULL)
7277 set_vterm_palette(term->tl_vterm, term->tl_palette);
7278 else
7279 init_vterm_ansi_colors(term->tl_vterm);
7280 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007281
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007282 channel_set_job(channel, job, opt);
7283 job_set_options(job, opt);
7284
7285 job->jv_channel = channel;
7286 job->jv_proc_info.hProcess = child_process_handle;
7287 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7288 job->jv_job_object = jo;
7289 job->jv_status = JOB_STARTED;
7290 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007291 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007292 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007293 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007294 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007295 ++job->jv_refcount;
7296 term->tl_job = job;
7297
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007298 // Redirecting stdout and stderr doesn't work at the job level. Instead
7299 // open the file here and handle it in. opt->jo_io was changed in
7300 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007301 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7302 {
7303 char_u *fname = opt->jo_io_name[PART_OUT];
7304
7305 ch_log(channel, "Opening output file %s", fname);
7306 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7307 if (term->tl_out_fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007308 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007309 }
7310
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007311 return OK;
7312
7313failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007314 ga_clear(&ga_cmd);
7315 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007316 vim_free(cmd_wchar);
7317 vim_free(cwd_wchar);
7318 if (spawn_config != NULL)
7319 winpty_spawn_config_free(spawn_config);
7320 if (channel != NULL)
7321 channel_clear(channel);
7322 if (job != NULL)
7323 {
7324 job->jv_channel = NULL;
7325 job_cleanup(job);
7326 }
7327 term->tl_job = NULL;
7328 if (jo != NULL)
7329 CloseHandle(jo);
7330 if (term->tl_winpty != NULL)
7331 winpty_free(term->tl_winpty);
7332 term->tl_winpty = NULL;
7333 if (term->tl_winpty_config != NULL)
7334 winpty_config_free(term->tl_winpty_config);
7335 term->tl_winpty_config = NULL;
7336 if (winpty_err != NULL)
7337 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007338 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007339 (short_u *)winpty_error_msg(winpty_err), NULL);
7340
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007341 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007342 winpty_error_free(winpty_err);
7343 }
7344 return FAIL;
7345}
7346
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007347/*
7348 * Create a new terminal of "rows" by "cols" cells.
7349 * Store a reference in "term".
7350 * Return OK or FAIL.
7351 */
7352 static int
7353term_and_job_init(
7354 term_T *term,
7355 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007356 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007357 jobopt_T *opt,
7358 jobopt_T *orig_opt)
7359{
7360 int use_winpty = FALSE;
7361 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007362 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007363
7364 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7365 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7366
7367 if (!has_winpty && !has_conpty)
7368 // If neither is available give the errors for winpty, since when
7369 // conpty is not available it can't be installed either.
7370 return dyn_winpty_init(TRUE);
7371
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007372 if (opt->jo_tty_type != NUL)
7373 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007374
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007375 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007376 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007377 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007378 use_conpty = TRUE;
7379 else if (has_winpty)
7380 use_winpty = TRUE;
7381 // else: error
7382 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007383 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007384 {
7385 if (has_winpty)
7386 use_winpty = TRUE;
7387 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007388 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007389 {
7390 if (has_conpty)
7391 use_conpty = TRUE;
7392 else
7393 return dyn_conpty_init(TRUE);
7394 }
7395
7396 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007397 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007398
7399 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007400 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007401
7402 // error
7403 return dyn_winpty_init(TRUE);
7404}
7405
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007406 static int
7407create_pty_only(term_T *term, jobopt_T *options)
7408{
7409 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7410 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7411 char in_name[80], out_name[80];
7412 channel_T *channel = NULL;
7413
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007414 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7415 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007416
7417 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7418 GetCurrentProcessId(),
7419 curbuf->b_fnum);
7420 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7421 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7422 PIPE_UNLIMITED_INSTANCES,
7423 0, 0, NMPWAIT_NOWAIT, NULL);
7424 if (hPipeIn == INVALID_HANDLE_VALUE)
7425 goto failed;
7426
7427 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7428 GetCurrentProcessId(),
7429 curbuf->b_fnum);
7430 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7431 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7432 PIPE_UNLIMITED_INSTANCES,
7433 0, 0, 0, NULL);
7434 if (hPipeOut == INVALID_HANDLE_VALUE)
7435 goto failed;
7436
7437 ConnectNamedPipe(hPipeIn, NULL);
7438 ConnectNamedPipe(hPipeOut, NULL);
7439
7440 term->tl_job = job_alloc();
7441 if (term->tl_job == NULL)
7442 goto failed;
7443 ++term->tl_job->jv_refcount;
7444
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007445 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007446 term->tl_job->jv_status = JOB_FINISHED;
7447
7448 channel = add_channel();
7449 if (channel == NULL)
7450 goto failed;
7451 term->tl_job->jv_channel = channel;
7452 channel->ch_keep_open = TRUE;
7453 channel->ch_named_pipe = TRUE;
7454
7455 channel_set_pipes(channel,
7456 (sock_T)hPipeIn,
7457 (sock_T)hPipeOut,
7458 (sock_T)hPipeOut);
7459 channel_set_job(channel, term->tl_job, options);
7460 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7461 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7462
7463 return OK;
7464
7465failed:
7466 if (hPipeIn != NULL)
7467 CloseHandle(hPipeIn);
7468 if (hPipeOut != NULL)
7469 CloseHandle(hPipeOut);
7470 return FAIL;
7471}
7472
7473/*
7474 * Free the terminal emulator part of "term".
7475 */
7476 static void
7477term_free_vterm(term_T *term)
7478{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007479 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007480 if (term->tl_winpty != NULL)
7481 winpty_free(term->tl_winpty);
7482 term->tl_winpty = NULL;
7483 if (term->tl_winpty_config != NULL)
7484 winpty_config_free(term->tl_winpty_config);
7485 term->tl_winpty_config = NULL;
7486 if (term->tl_vterm != NULL)
7487 vterm_free(term->tl_vterm);
7488 term->tl_vterm = NULL;
7489}
7490
7491/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007492 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007493 */
7494 static void
7495term_report_winsize(term_T *term, int rows, int cols)
7496{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007497 if (term->tl_conpty)
7498 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007499 if (term->tl_winpty)
7500 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7501}
7502
7503 int
7504terminal_enabled(void)
7505{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007506 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007507}
7508
7509# else
7510
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007511///////////////////////////////////////
7512// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007513
7514/*
7515 * Create a new terminal of "rows" by "cols" cells.
7516 * Start job for "cmd".
7517 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007518 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007519 * Return OK or FAIL.
7520 */
7521 static int
7522term_and_job_init(
7523 term_T *term,
7524 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007525 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007526 jobopt_T *opt,
7527 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007528{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007529 term->tl_arg0_cmd = NULL;
7530
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007531 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7532 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007533
LemonBoyb2b3acb2022-05-20 10:10:34 +01007534 if (term_use_palette())
7535 {
7536 if (term->tl_palette != NULL)
7537 set_vterm_palette(term->tl_vterm, term->tl_palette);
7538 else
7539 init_vterm_ansi_colors(term->tl_vterm);
7540 }
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007541
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007542 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007543 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007544 if (term->tl_job != NULL)
7545 ++term->tl_job->jv_refcount;
7546
7547 return term->tl_job != NULL
7548 && term->tl_job->jv_channel != NULL
7549 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7550}
7551
7552 static int
7553create_pty_only(term_T *term, jobopt_T *opt)
7554{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007555 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7556 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007557
7558 term->tl_job = job_alloc();
7559 if (term->tl_job == NULL)
7560 return FAIL;
7561 ++term->tl_job->jv_refcount;
7562
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007563 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007564 term->tl_job->jv_status = JOB_FINISHED;
7565
7566 return mch_create_pty_channel(term->tl_job, opt);
7567}
7568
7569/*
7570 * Free the terminal emulator part of "term".
7571 */
7572 static void
7573term_free_vterm(term_T *term)
7574{
7575 if (term->tl_vterm != NULL)
7576 vterm_free(term->tl_vterm);
7577 term->tl_vterm = NULL;
7578}
7579
7580/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007581 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007582 */
7583 static void
7584term_report_winsize(term_T *term, int rows, int cols)
7585{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007586 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007587 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7588 {
7589 int fd = -1;
7590 int part;
7591
7592 for (part = PART_OUT; part < PART_COUNT; ++part)
7593 {
7594 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007595 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007596 break;
7597 }
7598 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7599 mch_signal_job(term->tl_job, (char_u *)"winch");
7600 }
7601}
7602
7603# endif
7604
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007605#endif // FEAT_TERMINAL