blob: 997f83aef4e36c579c50ad6a3ffd997a30ce9697 [file] [log] [blame]
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Terminal window support, see ":help :terminal".
12 *
13 * There are three parts:
14 * 1. Generic code for all systems.
15 * Uses libvterm for the terminal emulator.
16 * 2. The MS-Windows implementation.
17 * Uses winpty.
18 * 3. The Unix-like implementation.
19 * Uses pseudo-tty's (pty's).
20 *
21 * For each terminal one VTerm is constructed. This uses libvterm. A copy of
22 * this library is in the libvterm directory.
23 *
24 * When a terminal window is opened, a job is started that will be connected to
25 * the terminal emulator.
26 *
27 * If the terminal window has keyboard focus, typed keys are converted to the
28 * terminal encoding and writing to the job over a channel.
29 *
30 * If the job produces output, it is written to the terminal emulator. The
31 * terminal emulator invokes callbacks when its screen content changes. The
32 * line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
33 * while, if the terminal window is visible, the screen contents is drawn.
34 *
35 * When the job ends the text is put in a buffer. Redrawing then happens from
36 * that buffer, attributes come from the scrollback buffer tl_scrollback.
37 * When the buffer is changed it is turned into a normal buffer, the attributes
38 * in tl_scrollback are no longer used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020039 */
40
41#include "vim.h"
42
43#if defined(FEAT_TERMINAL) || defined(PROTO)
44
45#ifndef MIN
46# define MIN(x,y) ((x) < (y) ? (x) : (y))
47#endif
48#ifndef MAX
49# define MAX(x,y) ((x) > (y) ? (x) : (y))
50#endif
51
52#include "libvterm/include/vterm.h"
53
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010054// This is VTermScreenCell without the characters, thus much smaller.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020055typedef struct {
56 VTermScreenCellAttrs attrs;
57 char width;
Bram Moolenaard96ff162018-02-18 22:13:29 +010058 VTermColor fg;
59 VTermColor bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020060} cellattr_T;
61
62typedef struct sb_line_S {
Bram Moolenaar29ae2232019-02-14 21:22:01 +010063 int sb_cols; // can differ per line
64 cellattr_T *sb_cells; // allocated
65 cellattr_T sb_fill_attr; // for short line
66 char_u *sb_text; // for tl_scrollback_postponed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020067} sb_line_T;
68
Bram Moolenaar4f974752019-02-17 17:44:42 +010069#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010070# ifndef HPCON
71# define HPCON VOID*
72# endif
73# ifndef EXTENDED_STARTUPINFO_PRESENT
74# define EXTENDED_STARTUPINFO_PRESENT 0x00080000
75# endif
76# ifndef PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE
77# define PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE 0x00020016
78# endif
79typedef struct _DYN_STARTUPINFOEXW
80{
81 STARTUPINFOW StartupInfo;
82 LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList;
83} DYN_STARTUPINFOEXW, *PDYN_STARTUPINFOEXW;
84#endif
85
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010086// typedef term_T in structs.h
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020087struct terminal_S {
88 term_T *tl_next;
89
90 VTerm *tl_vterm;
91 job_T *tl_job;
92 buf_T *tl_buffer;
Bram Moolenaar13568252018-03-16 20:46:58 +010093#if defined(FEAT_GUI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010094 int tl_system; // when non-zero used for :!cmd output
95 int tl_toprow; // row with first line of system terminal
Bram Moolenaar13568252018-03-16 20:46:58 +010096#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020097
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010098 // Set when setting the size of a vterm, reset after redrawing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020099 int tl_vterm_size_changed;
100
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100101 int tl_normal_mode; // TRUE: Terminal-Normal mode
Bram Moolenaareea32af2021-11-21 14:51:13 +0000102 int tl_channel_closing;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200103 int tl_channel_closed;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200104 int tl_channel_recently_closed; // still need to handle tl_finish
105
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100106 int tl_finish;
107#define TL_FINISH_UNSET NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100108#define TL_FINISH_CLOSE 'c' // ++close or :terminal without argument
109#define TL_FINISH_NOCLOSE 'n' // ++noclose
110#define TL_FINISH_OPEN 'o' // ++open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200111 char_u *tl_opencmd;
112 char_u *tl_eof_chars;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200113 char_u *tl_api; // prefix for terminal API function
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200114
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100115 char_u *tl_arg0_cmd; // To format the status bar
116
Bram Moolenaar4f974752019-02-17 17:44:42 +0100117#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200118 void *tl_winpty_config;
119 void *tl_winpty;
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200120
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100121 HPCON tl_conpty;
122 DYN_STARTUPINFOEXW tl_siex; // Structure that always needs to be hold
123
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200124 FILE *tl_out_fd;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200125#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100126#if defined(FEAT_SESSION)
127 char_u *tl_command;
128#endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100129 char_u *tl_kill;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200130
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100131 // last known vterm size
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200132 int tl_rows;
133 int tl_cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200134
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100135 char_u *tl_title; // NULL or allocated
136 char_u *tl_status_text; // NULL or allocated
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200137
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100138 // Range of screen rows to update. Zero based.
139 int tl_dirty_row_start; // MAX_ROW if nothing dirty
140 int tl_dirty_row_end; // row below last one to update
141 int tl_dirty_snapshot; // text updated after making snapshot
Bram Moolenaar56bc8e22018-05-10 18:05:56 +0200142#ifdef FEAT_TIMERS
143 int tl_timer_set;
144 proftime_T tl_timer_due;
145#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100146 int tl_postponed_scroll; // to be scrolled up
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200147
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200148 garray_T tl_scrollback;
149 int tl_scrollback_scrolled;
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100150 garray_T tl_scrollback_postponed;
151
Bram Moolenaar83d47902020-03-26 20:34:00 +0100152 char_u *tl_highlight_name; // replaces "Terminal"; allocated
153
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200154 cellattr_T tl_default_color;
155
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100156 linenr_T tl_top_diff_rows; // rows of top diff file or zero
157 linenr_T tl_bot_diff_rows; // rows of bottom diff file
Bram Moolenaard96ff162018-02-18 22:13:29 +0100158
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200159 VTermPos tl_cursor_pos;
160 int tl_cursor_visible;
161 int tl_cursor_blink;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100162 int tl_cursor_shape; // 1: block, 2: underline, 3: bar
163 char_u *tl_cursor_color; // NULL or allocated
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200164
LemonBoyb2b3acb2022-05-20 10:10:34 +0100165 long_u *tl_palette; // array of 16 colors specified by term_start, can
166 // be NULL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200167 int tl_using_altscreen;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200168 garray_T tl_osc_buf; // incomplete OSC string
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200169};
170
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100171#define TMODE_ONCE 1 // CTRL-\ CTRL-N used
172#define TMODE_LOOP 2 // CTRL-W N used
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200173
174/*
175 * List of all active terminals.
176 */
177static term_T *first_term = NULL;
178
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100179// Terminal active in terminal_loop().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200180static term_T *in_terminal_loop = NULL;
181
Bram Moolenaar4f974752019-02-17 17:44:42 +0100182#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100183static BOOL has_winpty = FALSE;
184static BOOL has_conpty = FALSE;
185#endif
186
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100187#define MAX_ROW 999999 // used for tl_dirty_row_end to update all rows
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200188#define KEY_BUF_LEN 200
189
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200190#define FOR_ALL_TERMS(term) \
191 for ((term) = first_term; (term) != NULL; (term) = (term)->tl_next)
192
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200193/*
194 * Functions with separate implementation for MS-Windows and Unix-like systems.
195 */
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200196static int term_and_job_init(term_T *term, typval_T *argvar, char **argv, jobopt_T *opt, jobopt_T *orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200197static int create_pty_only(term_T *term, jobopt_T *opt);
198static void term_report_winsize(term_T *term, int rows, int cols);
199static void term_free_vterm(term_T *term);
Bram Moolenaar13568252018-03-16 20:46:58 +0100200#ifdef FEAT_GUI
201static void update_system_term(term_T *term);
202#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200203
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100204static void handle_postponed_scrollback(term_T *term);
205
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100206// The character that we know (or assume) that the terminal expects for the
207// backspace key.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200208static int term_backspace_char = BS;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200209
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100210// Store the last set and the desired cursor properties, so that we only update
211// them when needed. Doing it unnecessary may result in flicker.
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200212static char_u *last_set_cursor_color = NULL;
213static char_u *desired_cursor_color = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +0100214static int last_set_cursor_shape = -1;
215static int desired_cursor_shape = -1;
216static int last_set_cursor_blink = -1;
217static int desired_cursor_blink = -1;
218
219
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100220///////////////////////////////////////
221// 1. Generic code for all systems.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200222
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200223 static int
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200224cursor_color_equal(char_u *lhs_color, char_u *rhs_color)
225{
226 if (lhs_color != NULL && rhs_color != NULL)
227 return STRCMP(lhs_color, rhs_color) == 0;
228 return lhs_color == NULL && rhs_color == NULL;
229}
230
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200231 static void
232cursor_color_copy(char_u **to_color, char_u *from_color)
233{
234 // Avoid a free & alloc if the value is already right.
235 if (cursor_color_equal(*to_color, from_color))
236 return;
237 vim_free(*to_color);
238 *to_color = (from_color == NULL) ? NULL : vim_strsave(from_color);
239}
240
241 static char_u *
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200242cursor_color_get(char_u *color)
243{
244 return (color == NULL) ? (char_u *)"" : color;
245}
246
247
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200248/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200249 * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
Bram Moolenaar498c2562018-04-15 23:45:15 +0200250 * current window.
251 * Sets "rows" and/or "cols" to zero when it should follow the window size.
252 * Return TRUE if the size is the minimum size: "24*80".
253 */
254 static int
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200255parse_termwinsize(win_T *wp, int *rows, int *cols)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200256{
257 int minsize = FALSE;
258
259 *rows = 0;
260 *cols = 0;
261
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000262 if (*wp->w_p_tws == NUL)
263 return FALSE;
Bram Moolenaar498c2562018-04-15 23:45:15 +0200264
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000265 char_u *p = vim_strchr(wp->w_p_tws, 'x');
266
267 // Syntax of value was already checked when it's set.
268 if (p == NULL)
269 {
270 minsize = TRUE;
271 p = vim_strchr(wp->w_p_tws, '*');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200272 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000273 *rows = atoi((char *)wp->w_p_tws);
274 *cols = atoi((char *)p + 1);
Bram Moolenaar498c2562018-04-15 23:45:15 +0200275 return minsize;
276}
277
278/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200279 * Determine the terminal size from 'termwinsize' and the current window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200280 */
281 static void
Bram Moolenaarb936b792020-09-04 18:34:09 +0200282set_term_and_win_size(term_T *term, jobopt_T *opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200283{
Bram Moolenaarb936b792020-09-04 18:34:09 +0200284 int rows, cols;
285 int minsize;
286
Bram Moolenaar13568252018-03-16 20:46:58 +0100287#ifdef FEAT_GUI
288 if (term->tl_system)
289 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100290 // Use the whole screen for the system command. However, it will start
291 // at the command line and scroll up as needed, using tl_toprow.
Bram Moolenaar13568252018-03-16 20:46:58 +0100292 term->tl_rows = Rows;
293 term->tl_cols = Columns;
Bram Moolenaar07b46af2018-04-10 14:56:18 +0200294 return;
Bram Moolenaar13568252018-03-16 20:46:58 +0100295 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100296#endif
Bram Moolenaarb936b792020-09-04 18:34:09 +0200297 term->tl_rows = curwin->w_height;
298 term->tl_cols = curwin->w_width;
299
300 minsize = parse_termwinsize(curwin, &rows, &cols);
301 if (minsize)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200302 {
Bram Moolenaarb936b792020-09-04 18:34:09 +0200303 if (term->tl_rows < rows)
304 term->tl_rows = rows;
305 if (term->tl_cols < cols)
306 term->tl_cols = cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200307 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200308 if ((opt->jo_set2 & JO2_TERM_ROWS))
309 term->tl_rows = opt->jo_term_rows;
310 else if (rows != 0)
311 term->tl_rows = rows;
312 if ((opt->jo_set2 & JO2_TERM_COLS))
313 term->tl_cols = opt->jo_term_cols;
314 else if (cols != 0)
315 term->tl_cols = cols;
316
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200317 if (!opt->jo_hidden)
Bram Moolenaarb936b792020-09-04 18:34:09 +0200318 {
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200319 if (term->tl_rows != curwin->w_height)
320 win_setheight_win(term->tl_rows, curwin);
321 if (term->tl_cols != curwin->w_width)
322 win_setwidth_win(term->tl_cols, curwin);
Bram Moolenaarb936b792020-09-04 18:34:09 +0200323
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200324 // Set 'winsize' now to avoid a resize at the next redraw.
325 if (!minsize && *curwin->w_p_tws != NUL)
326 {
327 char_u buf[100];
328
329 vim_snprintf((char *)buf, 100, "%dx%d",
330 term->tl_rows, term->tl_cols);
Bram Moolenaarac4174e2022-05-07 16:38:24 +0100331 set_option_value_give_err((char_u *)"termwinsize",
332 0L, buf, OPT_LOCAL);
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200333 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200334 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200335}
336
337/*
338 * Initialize job options for a terminal job.
339 * Caller may overrule some of them.
340 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100341 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200342init_job_options(jobopt_T *opt)
343{
344 clear_job_options(opt);
345
Bram Moolenaarac4174e2022-05-07 16:38:24 +0100346 opt->jo_mode = CH_MODE_RAW;
347 opt->jo_out_mode = CH_MODE_RAW;
348 opt->jo_err_mode = CH_MODE_RAW;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200349 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
350}
351
352/*
353 * Set job options mandatory for a terminal job.
354 */
355 static void
356setup_job_options(jobopt_T *opt, int rows, int cols)
357{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100358#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100359 // Win32: Redirecting the job output won't work, thus always connect stdout
360 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200361 if (!(opt->jo_set & JO_OUT_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200362#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200363 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100364 // Connect stdout to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200365 opt->jo_io[PART_OUT] = JIO_BUFFER;
366 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
367 opt->jo_modifiable[PART_OUT] = 0;
368 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
369 }
370
Bram Moolenaar4f974752019-02-17 17:44:42 +0100371#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100372 // Win32: Redirecting the job output won't work, thus always connect stderr
373 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200374 if (!(opt->jo_set & JO_ERR_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200375#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200376 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100377 // Connect stderr to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200378 opt->jo_io[PART_ERR] = JIO_BUFFER;
379 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
380 opt->jo_modifiable[PART_ERR] = 0;
381 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
382 }
383
384 opt->jo_pty = TRUE;
385 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
386 opt->jo_term_rows = rows;
387 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
388 opt->jo_term_cols = cols;
389}
390
391/*
Bram Moolenaar5c381eb2019-06-25 06:50:31 +0200392 * Flush messages on channels.
393 */
394 static void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000395term_flush_messages(void)
Bram Moolenaar5c381eb2019-06-25 06:50:31 +0200396{
397 mch_check_messages();
398 parse_queued_messages();
399}
400
401/*
Bram Moolenaard96ff162018-02-18 22:13:29 +0100402 * Close a terminal buffer (and its window). Used when creating the terminal
403 * fails.
404 */
405 static void
406term_close_buffer(buf_T *buf, buf_T *old_curbuf)
407{
408 free_terminal(buf);
409 if (old_curbuf != NULL)
410 {
411 --curbuf->b_nwindows;
412 curbuf = old_curbuf;
413 curwin->w_buffer = curbuf;
414 ++curbuf->b_nwindows;
415 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100416 CHECK_CURBUF;
Bram Moolenaard96ff162018-02-18 22:13:29 +0100417
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100418 // Wiping out the buffer will also close the window and call
419 // free_terminal().
Bram Moolenaard96ff162018-02-18 22:13:29 +0100420 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
421}
422
423/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200424 * Start a terminal window and return its buffer.
Bram Moolenaar13568252018-03-16 20:46:58 +0100425 * Use either "argvar" or "argv", the other must be NULL.
426 * When "flags" has TERM_START_NOJOB only create the buffer, b_term and open
427 * the window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200428 * Returns NULL when failed.
429 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100430 buf_T *
431term_start(
432 typval_T *argvar,
433 char **argv,
434 jobopt_T *opt,
435 int flags)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200436{
437 exarg_T split_ea;
438 win_T *old_curwin = curwin;
439 term_T *term;
440 buf_T *old_curbuf = NULL;
441 int res;
442 buf_T *newbuf;
Bram Moolenaare1004402020-10-24 20:49:43 +0200443 int vertical = opt->jo_vertical || (cmdmod.cmod_split & WSP_VERT);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200444 jobopt_T orig_opt; // only partly filled
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200445
446 if (check_restricted() || check_secure())
447 return NULL;
Bram Moolenaare5b44862021-05-30 13:54:03 +0200448 if (cmdwin_type != 0)
449 {
450 emsg(_(e_cannot_open_terminal_from_command_line_window));
451 return NULL;
452 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200453
454 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
455 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
456 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
Bram Moolenaarb0992022020-01-30 14:55:42 +0100457 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))
458 || (argvar != NULL
459 && argvar->v_type == VAR_LIST
460 && argvar->vval.v_list != NULL
461 && argvar->vval.v_list->lv_first == &range_list_item))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200462 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000463 emsg(_(e_invalid_argument));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200464 return NULL;
465 }
466
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200467 term = ALLOC_CLEAR_ONE(term_T);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200468 if (term == NULL)
469 return NULL;
470 term->tl_dirty_row_end = MAX_ROW;
471 term->tl_cursor_visible = TRUE;
472 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
473 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100474#ifdef FEAT_GUI
475 term->tl_system = (flags & TERM_START_SYSTEM);
476#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200477 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100478 ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200479 ga_init2(&term->tl_osc_buf, sizeof(char), 300);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200480
Bram Moolenaaraeed2a62021-04-29 20:18:45 +0200481 setpcmark();
Bram Moolenaara80faa82020-04-12 19:37:17 +0200482 CLEAR_FIELD(split_ea);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200483 if (opt->jo_curwin)
484 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100485 // Create a new buffer in the current window.
Bram Moolenaar13568252018-03-16 20:46:58 +0100486 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200487 {
488 no_write_message();
489 vim_free(term);
490 return NULL;
491 }
492 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaarb1009092020-05-31 16:04:42 +0200493 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
494 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
495 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200496 {
497 vim_free(term);
498 return NULL;
499 }
500 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100501 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200502 {
503 buf_T *buf;
504
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100505 // Create a new buffer without a window. Make it the current buffer for
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100506 // a moment to be able to do the initializations.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200507 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
508 BLN_NEW | BLN_LISTED);
509 if (buf == NULL || ml_open(buf) == FAIL)
510 {
511 vim_free(term);
512 return NULL;
513 }
514 old_curbuf = curbuf;
515 --curbuf->b_nwindows;
516 curbuf = buf;
517 curwin->w_buffer = buf;
518 ++curbuf->b_nwindows;
519 }
520 else
521 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100522 // Open a new window or tab.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200523 split_ea.cmdidx = CMD_new;
524 split_ea.cmd = (char_u *)"new";
525 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100526 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200527 {
528 split_ea.line2 = opt->jo_term_rows;
529 split_ea.addr_count = 1;
530 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100531 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200532 {
533 split_ea.line2 = opt->jo_term_cols;
534 split_ea.addr_count = 1;
535 }
536
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100537 if (vertical)
Bram Moolenaare1004402020-10-24 20:49:43 +0200538 cmdmod.cmod_split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200539 ex_splitview(&split_ea);
540 if (curwin == old_curwin)
541 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100542 // split failed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200543 vim_free(term);
544 return NULL;
545 }
546 }
547 term->tl_buffer = curbuf;
548 curbuf->b_term = term;
549
550 if (!opt->jo_hidden)
551 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100552 // Only one size was taken care of with :new, do the other one. With
553 // "curwin" both need to be done.
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100554 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200555 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100556 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200557 win_setwidth(opt->jo_term_cols);
558 }
559
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100560 // Link the new terminal in the list of active terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200561 term->tl_next = first_term;
562 first_term = term;
563
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100564 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
565
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200566 if (opt->jo_term_name != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100567 {
568 vim_free(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200569 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100570 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100571 else if (argv != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100572 {
573 vim_free(curbuf->b_ffname);
Bram Moolenaar13568252018-03-16 20:46:58 +0100574 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100575 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200576 else
577 {
578 int i;
579 size_t len;
580 char_u *cmd, *p;
581
582 if (argvar->v_type == VAR_STRING)
583 {
584 cmd = argvar->vval.v_string;
585 if (cmd == NULL)
586 cmd = (char_u *)"";
587 else if (STRCMP(cmd, "NONE") == 0)
588 cmd = (char_u *)"pty";
589 }
590 else if (argvar->v_type != VAR_LIST
591 || argvar->vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +0100592 || argvar->vval.v_list->lv_len == 0
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100593 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200594 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
595 cmd = (char_u*)"";
596
597 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200598 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200599
600 for (i = 0; p != NULL; ++i)
601 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100602 // Prepend a ! to the command name to avoid the buffer name equals
603 // the executable, otherwise ":w!" would overwrite it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200604 if (i == 0)
605 vim_snprintf((char *)p, len, "!%s", cmd);
606 else
607 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
608 if (buflist_findname(p) == NULL)
609 {
610 vim_free(curbuf->b_ffname);
611 curbuf->b_ffname = p;
612 break;
613 }
614 }
615 }
Bram Moolenaare010c722020-02-24 21:37:54 +0100616 vim_free(curbuf->b_sfname);
617 curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200618 curbuf->b_fname = curbuf->b_ffname;
619
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100620 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
621
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200622 if (opt->jo_term_opencmd != NULL)
623 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
624
625 if (opt->jo_eof_chars != NULL)
626 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
627
628 set_string_option_direct((char_u *)"buftype", -1,
629 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200630 // Avoid that 'buftype' is reset when this buffer is entered.
631 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200632
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100633 // Mark the buffer as not modifiable. It can only be made modifiable after
634 // the job finished.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200635 curbuf->b_p_ma = FALSE;
636
Bram Moolenaarb936b792020-09-04 18:34:09 +0200637 set_term_and_win_size(term, opt);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100638#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200639 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
640#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200641 setup_job_options(opt, term->tl_rows, term->tl_cols);
642
Bram Moolenaar13568252018-03-16 20:46:58 +0100643 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100644 return curbuf;
645
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100646#if defined(FEAT_SESSION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100647 // Remember the command for the session file.
Bram Moolenaar13568252018-03-16 20:46:58 +0100648 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100649 term->tl_command = vim_strsave((char_u *)"NONE");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100650 else if (argvar->v_type == VAR_STRING)
651 {
652 char_u *cmd = argvar->vval.v_string;
653
654 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
655 term->tl_command = vim_strsave(cmd);
656 }
657 else if (argvar->v_type == VAR_LIST
658 && argvar->vval.v_list != NULL
659 && argvar->vval.v_list->lv_len > 0)
660 {
661 garray_T ga;
662 listitem_T *item;
663
664 ga_init2(&ga, 1, 100);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200665 FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100666 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100667 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100668 char_u *p;
669
670 if (s == NULL)
671 break;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100672 p = vim_strsave_fnameescape(s, VSE_NONE);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100673 if (p == NULL)
674 break;
675 ga_concat(&ga, p);
676 vim_free(p);
677 ga_append(&ga, ' ');
678 }
679 if (item == NULL)
680 {
681 ga_append(&ga, NUL);
682 term->tl_command = ga.ga_data;
683 }
684 else
685 ga_clear(&ga);
686 }
687#endif
688
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100689 if (opt->jo_term_kill != NULL)
690 {
691 char_u *p = skiptowhite(opt->jo_term_kill);
692
693 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
694 }
695
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200696 if (opt->jo_term_api != NULL)
Bram Moolenaar21109272020-01-30 16:27:20 +0100697 {
698 char_u *p = skiptowhite(opt->jo_term_api);
699
700 term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
701 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200702 else
703 term->tl_api = vim_strsave((char_u *)"Tapi_");
704
Bram Moolenaar83d47902020-03-26 20:34:00 +0100705 if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
706 term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
707
Bram Moolenaar30b9a412022-05-26 14:06:37 +0100708#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
LemonBoyb2b3acb2022-05-20 10:10:34 +0100709 // Save the user-defined palette, it is only used in GUI (or 'tgc' is on).
710 if (opt->jo_set2 & JO2_ANSI_COLORS)
711 {
712 term->tl_palette = ALLOC_MULT(long_u, 16);
713 if (term->tl_palette == NULL)
714 {
715 vim_free(term);
716 return NULL;
717 }
718 memcpy(term->tl_palette, opt->jo_ansi_colors, sizeof(long_u) * 16);
719 }
Bram Moolenaar30b9a412022-05-26 14:06:37 +0100720#endif
LemonBoyb2b3acb2022-05-20 10:10:34 +0100721
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100722 // System dependent: setup the vterm and maybe start the job in it.
Bram Moolenaar13568252018-03-16 20:46:58 +0100723 if (argv == NULL
724 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200725 && argvar->vval.v_string != NULL
726 && STRCMP(argvar->vval.v_string, "NONE") == 0)
727 res = create_pty_only(term, opt);
728 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200729 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200730
731 newbuf = curbuf;
732 if (res == OK)
733 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100734 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200735 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
736 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100737#ifdef FEAT_GUI
738 if (term->tl_system)
739 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100740 // display first line below typed command
Bram Moolenaar13568252018-03-16 20:46:58 +0100741 term->tl_toprow = msg_row + 1;
742 term->tl_dirty_row_end = 0;
743 }
744#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200745
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100746 // Make sure we don't get stuck on sending keys to the job, it leads to
747 // a deadlock if the job is waiting for Vim to read.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200748 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
749
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200750 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200751 {
752 --curbuf->b_nwindows;
753 curbuf = old_curbuf;
754 curwin->w_buffer = curbuf;
755 ++curbuf->b_nwindows;
756 }
Bram Moolenaar81035272021-12-16 18:02:07 +0000757 else if (vgetc_busy
758#ifdef FEAT_TIMERS
759 || timer_busy
760#endif
761 || input_busy)
762 {
763 char_u ignore[4];
764
765 // When waiting for input need to return and possibly end up in
766 // terminal_loop() instead.
767 ignore[0] = K_SPECIAL;
768 ignore[1] = KS_EXTRA;
769 ignore[2] = KE_IGNORE;
770 ignore[3] = NUL;
771 ins_typebuf(ignore, REMAP_NONE, 0, TRUE, FALSE);
772 typebuf_was_filled = TRUE;
773 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200774 }
775 else
776 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100777 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200778 return NULL;
779 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100780
Bram Moolenaar13568252018-03-16 20:46:58 +0100781 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar28ed4df2019-10-26 16:21:40 +0200782 if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
783 apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200784 return newbuf;
785}
786
787/*
788 * ":terminal": open a terminal window and execute a job in it.
789 */
790 void
791ex_terminal(exarg_T *eap)
792{
793 typval_T argvar[2];
794 jobopt_T opt;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100795 int opt_shell = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200796 char_u *cmd;
797 char_u *tofree = NULL;
798
799 init_job_options(&opt);
800
801 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100802 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200803 {
804 char_u *p, *ep;
805
806 cmd += 2;
807 p = skiptowhite(cmd);
808 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200809 if (ep != NULL)
810 {
811 if (ep < p)
812 p = ep;
813 else
814 ep = NULL;
815 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200816
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200817# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
818 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
819 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200820 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200821 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100822 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200823 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200824 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200825 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200826 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200827 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200828 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200829 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100830 opt.jo_term_norestore = 1;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100831 else if (OPTARG_HAS("shell"))
832 opt_shell = TRUE;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200833 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100834 {
835 opt.jo_set2 |= JO2_TERM_KILL;
836 opt.jo_term_kill = ep + 1;
837 p = skiptowhite(cmd);
838 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200839 else if (OPTARG_HAS("api"))
840 {
841 opt.jo_set2 |= JO2_TERM_API;
842 if (ep != NULL)
843 {
844 opt.jo_term_api = ep + 1;
845 p = skiptowhite(cmd);
846 }
847 else
848 opt.jo_term_api = NULL;
849 }
850 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200851 {
852 opt.jo_set2 |= JO2_TERM_ROWS;
853 opt.jo_term_rows = atoi((char *)ep + 1);
854 p = skiptowhite(cmd);
855 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200856 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200857 {
858 opt.jo_set2 |= JO2_TERM_COLS;
859 opt.jo_term_cols = atoi((char *)ep + 1);
860 p = skiptowhite(cmd);
861 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200862 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200863 {
864 char_u *buf = NULL;
865 char_u *keys;
866
Bram Moolenaar21109272020-01-30 16:27:20 +0100867 vim_free(opt.jo_eof_chars);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200868 p = skiptowhite(cmd);
869 *p = NUL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200870 keys = replace_termcodes(ep + 1, &buf,
871 REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200872 opt.jo_set2 |= JO2_EOF_CHARS;
873 opt.jo_eof_chars = vim_strsave(keys);
874 vim_free(buf);
875 *p = ' ';
876 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100877#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100878 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
879 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100880 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100881 int tty_type = NUL;
882
883 p = skiptowhite(cmd);
884 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
885 tty_type = 'w';
886 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
887 tty_type = 'c';
888 else
889 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000890 semsg(e_invalid_value_for_argument_str, "type");
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100891 goto theend;
892 }
893 opt.jo_set2 |= JO2_TTY_TYPE;
894 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100895 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100896#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200897 else
898 {
899 if (*p)
900 *p = NUL;
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000901 semsg(_(e_invalid_attribute_str), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100902 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200903 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200904# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200905 cmd = skipwhite(p);
906 }
907 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100908 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100909 // Make a copy of 'shell', an autocommand may change the option.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200910 tofree = cmd = vim_strsave(p_sh);
911
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100912 // default to close when the shell exits
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100913 if (opt.jo_term_finish == NUL)
Bram Moolenaare2978022020-04-26 14:47:44 +0200914 opt.jo_term_finish = TL_FINISH_CLOSE;
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100915 }
916
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200917 if (eap->addr_count > 0)
918 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100919 // Write lines from current buffer to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200920 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
921 opt.jo_io[PART_IN] = JIO_BUFFER;
922 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
923 opt.jo_in_top = eap->line1;
924 opt.jo_in_bot = eap->line2;
925 }
926
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100927 if (opt_shell && tofree == NULL)
928 {
929#ifdef UNIX
930 char **argv = NULL;
931 char_u *tofree1 = NULL;
932 char_u *tofree2 = NULL;
933
934 // :term ++shell command
935 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
936 term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaaradf4aa22019-11-10 22:36:44 +0100937 vim_free(argv);
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100938 vim_free(tofree1);
939 vim_free(tofree2);
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100940 goto theend;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100941#else
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100942# ifdef MSWIN
943 long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
944 char_u *newcmd;
945
946 newcmd = alloc(cmdlen);
947 if (newcmd == NULL)
948 goto theend;
949 tofree = newcmd;
950 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
951 cmd = newcmd;
952# else
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000953 emsg(_(e_sorry_plusplusshell_not_supported_on_this_system));
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100954 goto theend;
955# endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100956#endif
957 }
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100958 argvar[0].v_type = VAR_STRING;
959 argvar[0].vval.v_string = cmd;
960 argvar[1].v_type = VAR_UNKNOWN;
961 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100962
963theend:
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100964 vim_free(tofree);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200965 vim_free(opt.jo_eof_chars);
966}
967
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100968#if defined(FEAT_SESSION) || defined(PROTO)
969/*
970 * Write a :terminal command to the session file to restore the terminal in
971 * window "wp".
972 * Return FAIL if writing fails.
973 */
974 int
Bram Moolenaar0e655112020-09-11 20:36:36 +0200975term_write_session(FILE *fd, win_T *wp, hashtab_T *terminal_bufs)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100976{
Bram Moolenaar0e655112020-09-11 20:36:36 +0200977 const int bufnr = wp->w_buffer->b_fnum;
978 term_T *term = wp->w_buffer->b_term;
979
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200980 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200981 {
982 // There are multiple views into this terminal buffer. We don't want to
983 // create the terminal multiple times. If it's the first time, create,
984 // otherwise link to the first buffer.
985 char id_as_str[NUMBUFLEN];
986 hashitem_T *entry;
987
988 vim_snprintf(id_as_str, sizeof(id_as_str), "%d", bufnr);
989
990 entry = hash_find(terminal_bufs, (char_u *)id_as_str);
991 if (!HASHITEM_EMPTY(entry))
992 {
993 // we've already opened this terminal buffer
994 if (fprintf(fd, "execute 'buffer ' . s:term_buf_%d", bufnr) < 0)
995 return FAIL;
996 return put_eol(fd);
997 }
998 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100999
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001000 // Create the terminal and run the command. This is not without
1001 // risk, but let's assume the user only creates a session when this
1002 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001003 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
1004 term->tl_cols, term->tl_rows) < 0)
1005 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001006#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01001007 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
1008 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001009#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001010 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
1011 return FAIL;
Bram Moolenaar0e655112020-09-11 20:36:36 +02001012 if (put_eol(fd) != OK)
1013 return FAIL;
1014
1015 if (fprintf(fd, "let s:term_buf_%d = bufnr()", bufnr) < 0)
1016 return FAIL;
1017
Bram Moolenaarc2c82052020-09-11 22:10:22 +02001018 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +02001019 {
1020 char *hash_key = alloc(NUMBUFLEN);
1021
1022 vim_snprintf(hash_key, NUMBUFLEN, "%d", bufnr);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001023 hash_add(terminal_bufs, (char_u *)hash_key, "terminal session");
Bram Moolenaar0e655112020-09-11 20:36:36 +02001024 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001025
1026 return put_eol(fd);
1027}
1028
1029/*
1030 * Return TRUE if "buf" has a terminal that should be restored.
1031 */
1032 int
1033term_should_restore(buf_T *buf)
1034{
1035 term_T *term = buf->b_term;
1036
1037 return term != NULL && (term->tl_command == NULL
1038 || STRCMP(term->tl_command, "NONE") != 0);
1039}
1040#endif
1041
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001042/*
1043 * Free the scrollback buffer for "term".
1044 */
1045 static void
1046free_scrollback(term_T *term)
1047{
1048 int i;
1049
1050 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
1051 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
1052 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001053 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
1054 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
1055 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001056}
1057
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001058
1059// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +02001060static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001061
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001062/*
1063 * Free a terminal and everything it refers to.
1064 * Kills the job if there is one.
1065 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001066 * The actual terminal structure is freed later in free_unused_terminals(),
1067 * because callbacks may wipe out a buffer while the terminal is still
1068 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001069 */
1070 void
1071free_terminal(buf_T *buf)
1072{
1073 term_T *term = buf->b_term;
1074 term_T *tp;
1075
1076 if (term == NULL)
1077 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001078
1079 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001080 if (first_term == term)
1081 first_term = term->tl_next;
1082 else
1083 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
1084 if (tp->tl_next == term)
1085 {
1086 tp->tl_next = term->tl_next;
1087 break;
1088 }
1089
1090 if (term->tl_job != NULL)
1091 {
1092 if (term->tl_job->jv_status != JOB_ENDED
1093 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +01001094 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001095 job_stop(term->tl_job, NULL, "kill");
1096 job_unref(term->tl_job);
1097 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001098 term->tl_next = terminals_to_free;
1099 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001100
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001101 buf->b_term = NULL;
1102 if (in_terminal_loop == term)
1103 in_terminal_loop = NULL;
1104}
1105
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001106 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00001107free_unused_terminals(void)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001108{
1109 while (terminals_to_free != NULL)
1110 {
1111 term_T *term = terminals_to_free;
1112
1113 terminals_to_free = term->tl_next;
1114
1115 free_scrollback(term);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02001116 ga_clear(&term->tl_osc_buf);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001117
1118 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001119 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001120 vim_free(term->tl_title);
1121#ifdef FEAT_SESSION
1122 vim_free(term->tl_command);
1123#endif
1124 vim_free(term->tl_kill);
1125 vim_free(term->tl_status_text);
1126 vim_free(term->tl_opencmd);
1127 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001128 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001129#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001130 if (term->tl_out_fd != NULL)
1131 fclose(term->tl_out_fd);
1132#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001133 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001134 vim_free(term->tl_cursor_color);
LemonBoyb2b3acb2022-05-20 10:10:34 +01001135 vim_free(term->tl_palette);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001136 vim_free(term);
1137 }
1138}
1139
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001140/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001141 * Get the part that is connected to the tty. Normally this is PART_IN, but
1142 * when writing buffer lines to the job it can be another. This makes it
1143 * possible to do "1,5term vim -".
1144 */
1145 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001146get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001147{
1148#ifdef UNIX
1149 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1150 int i;
1151
1152 for (i = 0; i < 3; ++i)
1153 {
1154 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1155
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001156 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001157 return parts[i];
1158 }
1159#endif
1160 return PART_IN;
1161}
1162
1163/*
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001164 * Read any vterm output and send it on the channel.
1165 */
1166 static void
1167term_forward_output(term_T *term)
1168{
1169 VTerm *vterm = term->tl_vterm;
1170 char buf[KEY_BUF_LEN];
1171 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1172
1173 if (curlen > 0)
1174 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1175 (char_u *)buf, (int)curlen, NULL);
1176}
1177
1178/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001179 * Write job output "msg[len]" to the vterm.
1180 */
1181 static void
Bram Moolenaar36968af2021-11-15 17:13:11 +00001182term_write_job_output(term_T *term, char_u *msg_arg, size_t len_arg)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001183{
Bram Moolenaar36968af2021-11-15 17:13:11 +00001184 char_u *msg = msg_arg;
1185 size_t len = len_arg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001186 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001187 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar36968af2021-11-15 17:13:11 +00001188 size_t limit = term->tl_buffer->b_p_twsl * term->tl_cols * 3;
1189
1190 // Limit the length to 'termwinscroll' * cols * 3 bytes. Keep the text at
1191 // the end.
1192 if (len > limit)
1193 {
1194 char_u *p = msg + len - limit;
1195
1196 p -= (*mb_head_off)(msg, p);
1197 len -= p - msg;
1198 msg = p;
1199 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001200
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001201 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001203 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001204 if (prevlen != vterm_output_get_buffer_current(vterm))
Bram Moolenaara48d4e42021-12-08 22:13:38 +00001205 term_forward_output(term);
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001206
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001207 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001208 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1209}
1210
1211 static void
1212update_cursor(term_T *term, int redraw)
1213{
1214 if (term->tl_normal_mode)
1215 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001216#ifdef FEAT_GUI
1217 if (term->tl_system)
1218 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1219 term->tl_cursor_pos.col);
1220 else
1221#endif
1222 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001223 if (redraw)
1224 {
Shougo Matsushita4ccaedf2022-10-15 11:48:00 +01001225 aco_save_T aco;
1226
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001227 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1228 cursor_on();
1229 out_flush();
1230#ifdef FEAT_GUI
1231 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001232 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001233 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001234 gui_mch_flush();
1235 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001236#endif
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001237 // Make sure an invoked autocmd doesn't delete the buffer (and the
1238 // terminal) under our fingers.
Shougo Matsushita4ccaedf2022-10-15 11:48:00 +01001239 ++term->tl_buffer->b_locked;
1240
1241 // save and restore curwin and curbuf, in case the autocmd changes them
1242 aucmd_prepbuf(&aco, curbuf);
1243 apply_autocmds(EVENT_TEXTCHANGEDT, NULL, NULL, FALSE, term->tl_buffer);
1244 aucmd_restbuf(&aco);
1245
1246 --term->tl_buffer->b_locked;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001247 }
1248}
1249
1250/*
1251 * Invoked when "msg" output from a job was received. Write it to the terminal
1252 * of "buffer".
1253 */
1254 void
1255write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1256{
1257 size_t len = STRLEN(msg);
1258 term_T *term = buffer->b_term;
1259
Bram Moolenaar4f974752019-02-17 17:44:42 +01001260#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001261 // Win32: Cannot redirect output of the job, intercept it here and write to
1262 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001263 if (term->tl_out_fd != NULL)
1264 {
1265 ch_log(channel, "Writing %d bytes to output file", (int)len);
1266 fwrite(msg, len, 1, term->tl_out_fd);
1267 return;
1268 }
1269#endif
1270
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001271 if (term->tl_vterm == NULL)
1272 {
1273 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1274 return;
1275 }
1276 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01001277 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001278 term_write_job_output(term, msg, len);
1279
Bram Moolenaar13568252018-03-16 20:46:58 +01001280#ifdef FEAT_GUI
1281 if (term->tl_system)
1282 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001283 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001284 update_system_term(term);
1285 update_cursor(term, TRUE);
1286 }
1287 else
1288#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001289 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1290 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001291 if (!term->tl_normal_mode)
1292 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001293 // Don't use update_screen() when editing the command line, it gets
1294 // cleared.
1295 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001296 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar24959102022-05-07 20:01:16 +01001297 if (buffer == curbuf && (State & MODE_CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001298 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001299 update_screen(UPD_VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001300 // update_screen() can be slow, check the terminal wasn't closed
1301 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001302 if (buffer == curbuf && curbuf->b_term != NULL)
1303 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001304 }
1305 else
Bram Moolenaare5050712021-12-09 10:51:05 +00001306 redraw_after_callback(TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001307 }
1308}
1309
1310/*
1311 * Send a mouse position and click to the vterm
1312 */
1313 static int
1314term_send_mouse(VTerm *vterm, int button, int pressed)
1315{
1316 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001317 int row = mouse_row - W_WINROW(curwin);
1318 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001319
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001320#ifdef FEAT_PROP_POPUP
1321 if (popup_is_popup(curwin))
1322 {
1323 row -= popup_top_extra(curwin);
1324 col -= popup_left_extra(curwin);
1325 }
1326#endif
1327 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001328 if (button != 0)
1329 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001330 return TRUE;
1331}
1332
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001333static int enter_mouse_col = -1;
1334static int enter_mouse_row = -1;
1335
1336/*
1337 * Handle a mouse click, drag or release.
1338 * Return TRUE when a mouse event is sent to the terminal.
1339 */
1340 static int
1341term_mouse_click(VTerm *vterm, int key)
1342{
1343#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001344 // For modeless selection mouse drag and release events are ignored, unless
1345 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001346 static int ignore_drag_release = TRUE;
1347 VTermMouseState mouse_state;
1348
1349 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1350 if (mouse_state.flags == 0)
1351 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001352 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001353 switch (key)
1354 {
1355 case K_LEFTDRAG:
1356 case K_LEFTRELEASE:
1357 case K_RIGHTDRAG:
1358 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001359 // Ignore drag and release events when the button-down wasn't
1360 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001361 if (ignore_drag_release)
1362 {
1363 int save_mouse_col, save_mouse_row;
1364
1365 if (enter_mouse_col < 0)
1366 break;
1367
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001368 // mouse click in the window gave us focus, handle that
1369 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001370 save_mouse_col = mouse_col;
1371 save_mouse_row = mouse_row;
1372 mouse_col = enter_mouse_col;
1373 mouse_row = enter_mouse_row;
1374 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1375 mouse_col = save_mouse_col;
1376 mouse_row = save_mouse_row;
1377 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001378 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001379 case K_LEFTMOUSE:
1380 case K_RIGHTMOUSE:
1381 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1382 ignore_drag_release = TRUE;
1383 else
1384 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001385 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001386 if (clip_star.available)
1387 {
1388 int button, is_click, is_drag;
1389
1390 button = get_mouse_button(KEY2TERMCAP1(key),
1391 &is_click, &is_drag);
1392 if (mouse_model_popup() && button == MOUSE_LEFT
1393 && (mod_mask & MOD_MASK_SHIFT))
1394 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001395 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001396 button = MOUSE_RIGHT;
1397 mod_mask &= ~MOD_MASK_SHIFT;
1398 }
1399 clip_modeless(button, is_click, is_drag);
1400 }
1401 break;
1402
1403 case K_MIDDLEMOUSE:
1404 if (clip_star.available)
1405 insert_reg('*', TRUE);
1406 break;
1407 }
1408 enter_mouse_col = -1;
1409 return FALSE;
1410 }
1411#endif
1412 enter_mouse_col = -1;
1413
1414 switch (key)
1415 {
1416 case K_LEFTMOUSE:
1417 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1418 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1419 case K_LEFTRELEASE:
1420 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1421 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1422 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1423 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1424 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1425 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1426 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1427 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1428 }
1429 return TRUE;
1430}
1431
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001432/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001433 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1434 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001435 * Return the number of bytes in "buf".
1436 */
1437 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001438term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001439{
1440 VTerm *vterm = term->tl_vterm;
1441 VTermKey key = VTERM_KEY_NONE;
1442 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001443 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001444
1445 switch (c)
1446 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001447 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001448
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001449 // don't use VTERM_KEY_BACKSPACE, it always
1450 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001451 case K_BS: c = term_backspace_char; break;
1452
1453 case ESC: key = VTERM_KEY_ESCAPE; break;
1454 case K_DEL: key = VTERM_KEY_DEL; break;
1455 case K_DOWN: key = VTERM_KEY_DOWN; break;
1456 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1457 key = VTERM_KEY_DOWN; break;
1458 case K_END: key = VTERM_KEY_END; break;
1459 case K_S_END: mod = VTERM_MOD_SHIFT;
1460 key = VTERM_KEY_END; break;
1461 case K_C_END: mod = VTERM_MOD_CTRL;
1462 key = VTERM_KEY_END; break;
1463 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1464 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1465 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1466 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1467 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1468 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1469 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1470 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1471 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1472 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1473 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1474 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1475 case K_HOME: key = VTERM_KEY_HOME; break;
1476 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1477 key = VTERM_KEY_HOME; break;
1478 case K_C_HOME: mod = VTERM_MOD_CTRL;
1479 key = VTERM_KEY_HOME; break;
1480 case K_INS: key = VTERM_KEY_INS; break;
1481 case K_K0: key = VTERM_KEY_KP_0; break;
1482 case K_K1: key = VTERM_KEY_KP_1; break;
1483 case K_K2: key = VTERM_KEY_KP_2; break;
1484 case K_K3: key = VTERM_KEY_KP_3; break;
1485 case K_K4: key = VTERM_KEY_KP_4; break;
1486 case K_K5: key = VTERM_KEY_KP_5; break;
1487 case K_K6: key = VTERM_KEY_KP_6; break;
1488 case K_K7: key = VTERM_KEY_KP_7; break;
1489 case K_K8: key = VTERM_KEY_KP_8; break;
1490 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001491 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001492 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001493 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001494 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001495 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1496 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001497 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1498 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001499 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1500 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001501 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1502 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1503 case K_LEFT: key = VTERM_KEY_LEFT; break;
1504 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1505 key = VTERM_KEY_LEFT; break;
1506 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1507 key = VTERM_KEY_LEFT; break;
1508 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1509 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1510 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1511 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1512 key = VTERM_KEY_RIGHT; break;
1513 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1514 key = VTERM_KEY_RIGHT; break;
1515 case K_UP: key = VTERM_KEY_UP; break;
1516 case K_S_UP: mod = VTERM_MOD_SHIFT;
1517 key = VTERM_KEY_UP; break;
1518 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001519 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1520 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001521
Bram Moolenaara42ad572017-11-16 13:08:04 +01001522 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1523 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001524 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1525 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001526
1527 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001528 case K_LEFTMOUSE_NM:
1529 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001530 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001531 case K_LEFTRELEASE_NM:
1532 case K_MOUSEMOVE:
1533 case K_MIDDLEMOUSE:
1534 case K_MIDDLEDRAG:
1535 case K_MIDDLERELEASE:
1536 case K_RIGHTMOUSE:
1537 case K_RIGHTDRAG:
1538 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1539 return 0;
1540 other = TRUE;
1541 break;
1542
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001543 case K_X1MOUSE: /* TODO */ return 0;
1544 case K_X1DRAG: /* TODO */ return 0;
1545 case K_X1RELEASE: /* TODO */ return 0;
1546 case K_X2MOUSE: /* TODO */ return 0;
1547 case K_X2DRAG: /* TODO */ return 0;
1548 case K_X2RELEASE: /* TODO */ return 0;
1549
1550 case K_IGNORE: return 0;
1551 case K_NOP: return 0;
1552 case K_UNDO: return 0;
1553 case K_HELP: return 0;
1554 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1555 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1556 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1557 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1558 case K_SELECT: return 0;
1559#ifdef FEAT_GUI
1560 case K_VER_SCROLLBAR: return 0;
1561 case K_HOR_SCROLLBAR: return 0;
1562#endif
1563#ifdef FEAT_GUI_TABLINE
1564 case K_TABLINE: return 0;
1565 case K_TABMENU: return 0;
1566#endif
1567#ifdef FEAT_NETBEANS_INTG
1568 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1569#endif
1570#ifdef FEAT_DND
1571 case K_DROP: return 0;
1572#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001573 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001574 case K_PS: vterm_keyboard_start_paste(vterm);
1575 other = TRUE;
1576 break;
1577 case K_PE: vterm_keyboard_end_paste(vterm);
1578 other = TRUE;
1579 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001580 }
1581
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001582 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001583 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001584 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001585 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001586 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001587 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001588 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001589
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001590 // Ctrl-Shift-i may have the key "I" instead of "i", but for the kitty
1591 // keyboard protocol should use "i". Applies to all ascii letters.
1592 if (ASCII_ISUPPER(c)
Bram Moolenaarebed1b02022-11-24 14:05:19 +00001593 && vterm_is_kitty_keyboard(vterm)
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001594 && mod == (VTERM_MOD_CTRL | VTERM_MOD_SHIFT))
1595 c = TOLOWER_ASC(c);
1596
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.
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001623 if (term == NULL
1624 || term->tl_job == NULL
1625 || !channel_is_open(term->tl_job->jv_channel))
1626 return FALSE;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001627
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001628 job_T *job = term->tl_job;
1629
1630 // Careful: Checking the job status may invoke callbacks, which close
1631 // the buffer and terminate "term". However, "job" will not be freed
1632 // yet.
1633 if (check_job_status)
1634 job_status(job);
1635 return (job->jv_status == JOB_STARTED
1636 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001637}
1638
1639/*
1640 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001641 */
1642 int
1643term_job_running(term_T *term)
1644{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001645 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001646}
1647
1648/*
Bram Moolenaar9e636b92022-05-29 22:37:05 +01001649 * Return TRUE if the job for "term" is still running, ignoring the job was
1650 * "NONE".
1651 */
1652 int
1653term_job_running_not_none(term_T *term)
1654{
1655 return term_job_running(term) && !term_none_open(term);
1656}
1657
1658/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001659 * Return TRUE if "term" has an active channel and used ":term NONE".
1660 */
1661 int
1662term_none_open(term_T *term)
1663{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001664 // Also consider the job finished when the channel is closed, to avoid a
1665 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001666 return term != NULL
1667 && term->tl_job != NULL
1668 && channel_is_open(term->tl_job->jv_channel)
1669 && term->tl_job->jv_channel->ch_keep_open;
1670}
1671
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001672//
1673// Used to confirm whether we would like to kill a terminal.
1674// Return OK when the user confirms to kill it.
1675// Return FAIL if the user selects otherwise.
1676//
1677 int
1678term_confirm_stop(buf_T *buf)
1679{
1680 char_u buff[DIALOG_MSG_SIZE];
1681 int ret;
1682
1683 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
1684 ret = vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1);
1685 if (ret == VIM_YES)
1686 return OK;
1687 else
1688 return FAIL;
1689}
1690
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001691/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001692 * Used when exiting: kill the job in "buf" if so desired.
1693 * Return OK when the job finished.
1694 * Return FAIL when the job is still running.
1695 */
1696 int
1697term_try_stop_job(buf_T *buf)
1698{
1699 int count;
1700 char *how = (char *)buf->b_term->tl_kill;
1701
1702#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001703 if ((how == NULL || *how == NUL)
1704 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001705 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001706 if (term_confirm_stop(buf) == OK)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001707 how = "kill";
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001708 else
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001709 return FAIL;
1710 }
1711#endif
1712 if (how == NULL || *how == NUL)
1713 return FAIL;
1714
1715 job_stop(buf->b_term->tl_job, NULL, how);
1716
Bram Moolenaar9172d232019-01-29 23:06:54 +01001717 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001718 for (count = 0; count < 100; ++count)
1719 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001720 job_T *job;
1721
1722 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001723 if (!buf_valid(buf)
1724 || buf->b_term == NULL
1725 || buf->b_term->tl_job == NULL)
1726 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001727 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001728
Bram Moolenaar9172d232019-01-29 23:06:54 +01001729 // Call job_status() to update jv_status. It may cause the job to be
1730 // cleaned up but it won't be freed.
1731 job_status(job);
1732 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001733 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001734
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001735 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001736 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001737 }
1738 return FAIL;
1739}
1740
1741/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001742 * Add the last line of the scrollback buffer to the buffer in the window.
1743 */
1744 static void
1745add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1746{
1747 buf_T *buf = term->tl_buffer;
1748 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1749 linenr_T lnum = buf->b_ml.ml_line_count;
1750
Bram Moolenaar4f974752019-02-17 17:44:42 +01001751#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001752 if (!enc_utf8 && enc_codepage > 0)
1753 {
1754 WCHAR *ret = NULL;
1755 int length = 0;
1756
1757 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1758 &ret, &length);
1759 if (ret != NULL)
1760 {
1761 WideCharToMultiByte_alloc(enc_codepage, 0,
1762 ret, length, (char **)&text, &len, 0, 0);
1763 vim_free(ret);
1764 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1765 vim_free(text);
1766 }
1767 }
1768 else
1769#endif
1770 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1771 if (empty)
1772 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001773 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001774 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001775 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001776 curbuf = curwin->w_buffer;
1777 }
1778}
1779
1780 static void
1781cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1782{
1783 attr->width = cell->width;
1784 attr->attrs = cell->attrs;
1785 attr->fg = cell->fg;
1786 attr->bg = cell->bg;
1787}
1788
1789 static int
1790equal_celattr(cellattr_T *a, cellattr_T *b)
1791{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001792 // We only compare the RGB colors, ignoring the ANSI index and type.
1793 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001794 return a->fg.red == b->fg.red
1795 && a->fg.green == b->fg.green
1796 && a->fg.blue == b->fg.blue
1797 && a->bg.red == b->bg.red
1798 && a->bg.green == b->bg.green
1799 && a->bg.blue == b->bg.blue;
1800}
1801
Bram Moolenaard96ff162018-02-18 22:13:29 +01001802/*
1803 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1804 * line at this position. Otherwise at the end.
1805 */
1806 static int
1807add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1808{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001809 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
1810 return FALSE;
1811
1812 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1813 + term->tl_scrollback.ga_len;
1814
1815 if (lnum > 0)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001816 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001817 int i;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001818
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001819 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001820 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001821 *line = *(line - 1);
1822 --line;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001823 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01001824 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001825 line->sb_cols = 0;
1826 line->sb_cells = NULL;
1827 line->sb_fill_attr = *fill_attr;
1828 ++term->tl_scrollback.ga_len;
1829 return OK;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001830}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001831
1832/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001833 * Remove the terminal contents from the scrollback and the buffer.
1834 * Used before adding a new scrollback line or updating the buffer for lines
1835 * displayed in the terminal.
1836 */
1837 static void
1838cleanup_scrollback(term_T *term)
1839{
1840 sb_line_T *line;
1841 garray_T *gap;
1842
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001843 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001844 gap = &term->tl_scrollback;
1845 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1846 && gap->ga_len > 0)
1847 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001848 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001849 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1850 vim_free(line->sb_cells);
1851 --gap->ga_len;
1852 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001853 curbuf = curwin->w_buffer;
1854 if (curbuf == term->tl_buffer)
1855 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001856}
1857
1858/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001859 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001860 */
1861 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001862update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001863{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001864 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001865 int len;
1866 int lines_skipped = 0;
1867 VTermPos pos;
1868 VTermScreenCell cell;
1869 cellattr_T fill_attr, new_fill_attr;
1870 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001871
1872 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1873 "Adding terminal window snapshot to buffer");
1874
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001875 // First remove the lines that were appended before, they might be
1876 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001877 cleanup_scrollback(term);
1878
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001879 screen = vterm_obtain_screen(term->tl_vterm);
1880 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001881 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1882 {
1883 len = 0;
1884 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1885 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1886 && cell.chars[0] != NUL)
1887 {
1888 len = pos.col + 1;
1889 new_fill_attr = term->tl_default_color;
1890 }
1891 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001892 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001893 cell2cellattr(&cell, &new_fill_attr);
1894
1895 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1896 ++lines_skipped;
1897 else
1898 {
1899 while (lines_skipped > 0)
1900 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001901 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001902 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001903 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001904 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001905 }
1906
1907 if (len == 0)
1908 p = NULL;
1909 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001910 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001911 if ((p != NULL || len == 0)
1912 && ga_grow(&term->tl_scrollback, 1) == OK)
1913 {
1914 garray_T ga;
1915 int width;
1916 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1917 + term->tl_scrollback.ga_len;
1918
1919 ga_init2(&ga, 1, 100);
1920 for (pos.col = 0; pos.col < len; pos.col += width)
1921 {
1922 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1923 {
1924 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001925 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001926 if (ga_grow(&ga, 1) == OK)
1927 ga.ga_len += utf_char2bytes(' ',
1928 (char_u *)ga.ga_data + ga.ga_len);
1929 }
1930 else
1931 {
1932 width = cell.width;
1933
1934 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001935 if (width == 2)
1936 // second cell of double-width character has the
1937 // same attributes.
1938 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001939
Bram Moolenaara79fd562018-12-20 20:47:32 +01001940 // Each character can be up to 6 bytes.
1941 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001942 {
1943 int i;
1944 int c;
1945
1946 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1947 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1948 (char_u *)ga.ga_data + ga.ga_len);
1949 }
1950 }
1951 }
1952 line->sb_cols = len;
1953 line->sb_cells = p;
1954 line->sb_fill_attr = new_fill_attr;
1955 fill_attr = new_fill_attr;
1956 ++term->tl_scrollback.ga_len;
1957
1958 if (ga_grow(&ga, 1) == FAIL)
1959 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1960 else
1961 {
1962 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1963 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1964 }
1965 ga_clear(&ga);
1966 }
1967 else
1968 vim_free(p);
1969 }
1970 }
1971
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001972 // Add trailing empty lines.
1973 for (pos.row = term->tl_scrollback.ga_len;
1974 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1975 ++pos.row)
1976 {
1977 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1978 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1979 }
1980
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001981 term->tl_dirty_snapshot = FALSE;
1982#ifdef FEAT_TIMERS
1983 term->tl_timer_set = FALSE;
1984#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001985}
1986
1987/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001988 * Loop over all windows in the current tab, and also curwin, which is not
1989 * encountered when using a terminal in a popup window.
1990 * Return TRUE if "*wp" was set to the next window.
1991 */
1992 static int
1993for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1994{
1995 if (*wp == NULL)
1996 *wp = firstwin;
1997 else if ((*wp)->w_next != NULL)
1998 *wp = (*wp)->w_next;
1999 else if (!*did_curwin)
2000 *wp = curwin;
2001 else
2002 return FALSE;
2003 if (*wp == curwin)
2004 *did_curwin = TRUE;
2005 return TRUE;
2006}
2007
2008/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002009 * If needed, add the current lines of the terminal to scrollback and to the
2010 * buffer. Called after the job has ended and when switching to
2011 * Terminal-Normal mode.
2012 * When "redraw" is TRUE redraw the windows that show the terminal.
2013 */
2014 static void
2015may_move_terminal_to_buffer(term_T *term, int redraw)
2016{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002017 if (term->tl_vterm == NULL)
2018 return;
2019
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002020 // Update the snapshot only if something changes or the buffer does not
2021 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002022 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
2023 <= term->tl_scrollback_scrolled)
2024 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002025
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002026 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002027 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2028 &term->tl_default_color.fg, &term->tl_default_color.bg);
2029
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002030 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002031 {
2032 win_T *wp = NULL;
2033 int did_curwin = FALSE;
2034
2035 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002036 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002037 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002038 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002039 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
2040 wp->w_cursor.col = 0;
2041 wp->w_valid = 0;
2042 if (wp->w_cursor.lnum >= wp->w_height)
2043 {
2044 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002045
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002046 if (wp->w_topline < min_topline)
2047 wp->w_topline = min_topline;
2048 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002049 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002050 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002051 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002052 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002053}
2054
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002055#if defined(FEAT_TIMERS) || defined(PROTO)
2056/*
2057 * Check if any terminal timer expired. If so, copy text from the terminal to
2058 * the buffer.
2059 * Return the time until the next timer will expire.
2060 */
2061 int
2062term_check_timers(int next_due_arg, proftime_T *now)
2063{
2064 term_T *term;
2065 int next_due = next_due_arg;
2066
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002067 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002068 {
2069 if (term->tl_timer_set && !term->tl_normal_mode)
2070 {
2071 long this_due = proftime_time_left(&term->tl_timer_due, now);
2072
2073 if (this_due <= 1)
2074 {
2075 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002076 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002077 }
2078 else if (next_due == -1 || next_due > this_due)
2079 next_due = this_due;
2080 }
2081 }
2082
2083 return next_due;
2084}
2085#endif
2086
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002087/*
2088 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
2089 * otherwise end it.
2090 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002091 static void
2092set_terminal_mode(term_T *term, int normal_mode)
2093{
2094 term->tl_normal_mode = normal_mode;
LemonBoy2bf52dd2022-04-09 18:17:34 +01002095 may_trigger_modechanged();
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002096 if (!normal_mode)
2097 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002098 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002099 if (term->tl_buffer == curbuf)
2100 maketitle();
2101}
2102
2103/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002104 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002105 * Move the vterm contents into the scrollback buffer and free the vterm.
2106 */
2107 static void
2108cleanup_vterm(term_T *term)
2109{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002110 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002111 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002112 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002113 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002114}
2115
2116/*
2117 * Switch from Terminal-Job mode to Terminal-Normal mode.
2118 * Suspends updating the terminal window.
2119 */
2120 static void
2121term_enter_normal_mode(void)
2122{
2123 term_T *term = curbuf->b_term;
2124
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002125 set_terminal_mode(term, TRUE);
2126
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002127 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002128 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002129
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002130 // Move the window cursor to the position of the cursor in the
2131 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002132 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2133 + term->tl_cursor_pos.row + 1;
2134 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002135 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2136 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002137 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002138
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002139 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002140 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2141}
2142
2143/*
2144 * Returns TRUE if the current window contains a terminal and we are in
2145 * Terminal-Normal mode.
2146 */
2147 int
2148term_in_normal_mode(void)
2149{
2150 term_T *term = curbuf->b_term;
2151
2152 return term != NULL && term->tl_normal_mode;
2153}
2154
2155/*
2156 * Switch from Terminal-Normal mode to Terminal-Job mode.
2157 * Restores updating the terminal window.
2158 */
2159 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002160term_enter_job_mode(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002161{
2162 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002163
2164 set_terminal_mode(term, FALSE);
2165
2166 if (term->tl_channel_closed)
2167 cleanup_vterm(term);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002168 redraw_buf_and_status_later(curbuf, UPD_NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002169#ifdef FEAT_PROP_POPUP
2170 if (WIN_IS_POPUP(curwin))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002171 redraw_later(UPD_NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002172#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002173}
2174
2175/*
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002176 * When "modify_other_keys" is set then vgetc() should not reduce a key with
2177 * modifiers into a basic key. However, we may only find out after calling
2178 * vgetc(). Therefore vgetorpeek() will call check_no_reduce_keys() to update
2179 * "no_reduce_keys" before using it.
2180 */
2181typedef enum {
2182 NRKS_NONE, // initial value
2183 NRKS_CHECK, // modify_other_keys was off before calling vgetc()
2184 NRKS_SET, // no_reduce_keys was incremented in term_vgetc() or
2185 // check_no_reduce_keys(), must be decremented.
2186} reduce_key_state_T;
2187
2188static reduce_key_state_T no_reduce_key_state = NRKS_NONE;
2189
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002190/*
2191 * Return TRUE if the term is using modifyOtherKeys level 2 or the kitty
2192 * keyboard protocol.
2193 */
2194 static int
2195vterm_using_key_protocol(void)
2196{
2197 return curbuf->b_term != NULL
2198 && curbuf->b_term->tl_vterm != NULL
2199 && (vterm_is_modify_other_keys(curbuf->b_term->tl_vterm)
2200 || vterm_is_kitty_keyboard(curbuf->b_term->tl_vterm));
2201}
2202
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002203 void
2204check_no_reduce_keys(void)
2205{
2206 if (no_reduce_key_state != NRKS_CHECK
2207 || no_reduce_keys >= 1
2208 || curbuf->b_term == NULL
2209 || curbuf->b_term->tl_vterm == NULL)
2210 return;
2211
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002212 if (vterm_using_key_protocol())
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002213 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002214 // "modify_other_keys" or kitty keyboard protocol was enabled while
2215 // waiting.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002216 no_reduce_key_state = NRKS_SET;
2217 ++no_reduce_keys;
2218 }
2219}
2220
2221/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002222 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002223 * Note: while waiting a terminal may be closed and freed if the channel is
Bram Moolenaar829c8e82021-12-14 08:41:38 +00002224 * closed and ++close was used. This may even happen before we get here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002225 */
2226 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002227term_vgetc(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002228{
2229 int c;
2230 int save_State = State;
2231
Bram Moolenaar24959102022-05-07 20:01:16 +01002232 State = MODE_TERMINAL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002233 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002234#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002235 ctrl_break_was_pressed = FALSE;
2236#endif
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002237
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002238 if (vterm_using_key_protocol())
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002239 {
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002240 ++no_reduce_keys;
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002241 no_reduce_key_state = NRKS_SET;
2242 }
2243 else
2244 {
2245 no_reduce_key_state = NRKS_CHECK;
2246 }
2247
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002248 c = vgetc();
2249 got_int = FALSE;
2250 State = save_State;
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002251
2252 if (no_reduce_key_state == NRKS_SET)
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002253 --no_reduce_keys;
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002254 no_reduce_key_state = NRKS_NONE;
2255
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002256 return c;
2257}
2258
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002259static int mouse_was_outside = FALSE;
2260
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002261/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002262 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002263 * Return FAIL when the key needs to be handled in Normal mode.
2264 * Return OK when the key was dropped or sent to the terminal.
2265 */
2266 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002267send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002268{
2269 char msg[KEY_BUF_LEN];
2270 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002271 int dragging_outside = FALSE;
2272
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002273 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002274 switch (c)
2275 {
2276 case NUL:
2277 case K_ZERO:
2278 if (typed)
2279 stuffcharReadbuff(c);
2280 return FAIL;
2281
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002282 case K_TABLINE:
2283 stuffcharReadbuff(c);
2284 return FAIL;
2285
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002286 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002287 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002288 return FAIL;
2289
2290 case K_LEFTDRAG:
2291 case K_MIDDLEDRAG:
2292 case K_RIGHTDRAG:
2293 case K_X1DRAG:
2294 case K_X2DRAG:
2295 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002296 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002297 case K_LEFTMOUSE:
2298 case K_LEFTMOUSE_NM:
2299 case K_LEFTRELEASE:
2300 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002301 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002302 case K_MIDDLEMOUSE:
2303 case K_MIDDLERELEASE:
2304 case K_RIGHTMOUSE:
2305 case K_RIGHTRELEASE:
2306 case K_X1MOUSE:
2307 case K_X1RELEASE:
2308 case K_X2MOUSE:
2309 case K_X2RELEASE:
2310
2311 case K_MOUSEUP:
2312 case K_MOUSEDOWN:
2313 case K_MOUSELEFT:
2314 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002315 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002316 int row = mouse_row;
2317 int col = mouse_col;
2318
2319#ifdef FEAT_PROP_POPUP
2320 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002321 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002322 row -= popup_top_extra(curwin);
2323 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002324 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002325#endif
2326 if (row < W_WINROW(curwin)
2327 || row >= (W_WINROW(curwin) + curwin->w_height)
2328 || col < curwin->w_wincol
2329 || col >= W_ENDCOL(curwin)
2330 || dragging_outside)
2331 {
2332 // click or scroll outside the current window or on status
2333 // line or vertical separator
2334 if (typed)
2335 {
2336 stuffcharReadbuff(c);
2337 mouse_was_outside = TRUE;
2338 }
2339 return FAIL;
2340 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002341 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002342 break;
2343
2344 case K_COMMAND:
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002345 case K_SCRIPT_COMMAND:
2346 return do_cmdkey_command(c, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002347 }
2348 if (typed)
2349 mouse_was_outside = FALSE;
2350
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002351 // Convert the typed key to a sequence of bytes for the job.
2352 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002353 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002354 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002355 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2356 (char_u *)msg, (int)len, NULL);
2357
2358 return OK;
2359}
2360
2361 static void
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002362position_cursor(win_T *wp, VTermPos *pos)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002363{
2364 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2365 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002366#ifdef FEAT_PROP_POPUP
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002367 if (popup_is_popup(wp))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002368 {
Bram Moolenaarf5452692020-11-28 21:56:06 +01002369 wp->w_wrow += popup_top_extra(wp);
2370 wp->w_wcol += popup_left_extra(wp);
Bram Moolenaar6a076442020-11-15 20:32:58 +01002371 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002372 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01002373 else
2374 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002375#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002376 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2377}
2378
2379/*
2380 * Handle CTRL-W "": send register contents to the job.
2381 */
2382 static void
2383term_paste_register(int prev_c UNUSED)
2384{
2385 int c;
2386 list_T *l;
2387 listitem_T *item;
2388 long reglen = 0;
2389 int type;
2390
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002391 if (add_to_showcmd(prev_c))
2392 if (add_to_showcmd('"'))
2393 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +01002394
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002395 c = term_vgetc();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002396 clear_showcmd();
Martin Tournoijba43e762022-10-13 22:12:15 +01002397
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002398 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002399 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002400 return;
2401
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002402 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002403 if (c == '=' && get_expr_register() != '=')
2404 return;
2405 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002406 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002407 return;
2408
2409 l = (list_T *)get_reg_contents(c, GREG_LIST);
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002410 if (l == NULL)
2411 return;
2412
2413 type = get_reg_type(c, &reglen);
2414 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002415 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002416 char_u *s = tv_get_string(&item->li_tv);
2417#ifdef MSWIN
2418 char_u *tmp = s;
2419
2420 if (!enc_utf8 && enc_codepage > 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002421 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002422 WCHAR *ret = NULL;
2423 int length = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002424
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002425 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2426 (int)STRLEN(s), &ret, &length);
2427 if (ret != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002428 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002429 WideCharToMultiByte_alloc(CP_UTF8, 0,
2430 ret, length, (char **)&s, &length, 0, 0);
2431 vim_free(ret);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002432 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002433 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002434#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002435 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2436 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002437#ifdef MSWIN
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002438 if (tmp != s)
2439 vim_free(s);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002440#endif
2441
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002442 if (item->li_next != NULL || type == MLINE)
2443 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2444 (char_u *)"\r", 1, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002445 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002446 list_free(l);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002447}
2448
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002449/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002450 * Return TRUE when waiting for a character in the terminal, the cursor of the
2451 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002452 */
2453 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002454terminal_is_active(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002455{
2456 return in_terminal_loop != NULL;
2457}
2458
Bram Moolenaar83d47902020-03-26 20:34:00 +01002459/*
dundargocc57b5bc2022-11-02 13:30:51 +00002460 * Return the highlight group ID for the terminal and the window.
Bram Moolenaar83d47902020-03-26 20:34:00 +01002461 */
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002462 static int
2463term_get_highlight_id(term_T *term, win_T *wp)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002464{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002465 char_u *name;
2466
2467 if (wp != NULL && *wp->w_p_wcr != NUL)
2468 name = wp->w_p_wcr;
2469 else if (term->tl_highlight_name != NULL)
2470 name = term->tl_highlight_name;
2471 else
2472 name = (char_u*)"Terminal";
2473
2474 return syn_name2id(name);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002475}
2476
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002477#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002478 cursorentry_T *
2479term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2480{
2481 term_T *term = in_terminal_loop;
2482 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002483 int id;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002484 guicolor_T term_fg = INVALCOLOR;
2485 guicolor_T term_bg = INVALCOLOR;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002486
Bram Moolenaara80faa82020-04-12 19:37:17 +02002487 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002488 entry.shape = entry.mshape =
2489 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2490 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2491 SHAPE_BLOCK;
2492 entry.percentage = 20;
2493 if (term->tl_cursor_blink)
2494 {
2495 entry.blinkwait = 700;
2496 entry.blinkon = 400;
2497 entry.blinkoff = 250;
2498 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002499
Bram Moolenaar83d47902020-03-26 20:34:00 +01002500 // The highlight group overrules the defaults.
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002501 id = term_get_highlight_id(term, curwin);
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002502 if (id != 0)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002503 syn_id2colors(id, &term_fg, &term_bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002504 if (term_bg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002505 *fg = term_bg;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002506 else
2507 *fg = gui.back_pixel;
2508
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002509 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002510 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002511 if (term_fg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002512 *bg = term_fg;
2513 else
2514 *bg = gui.norm_pixel;
2515 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002516 else
2517 *bg = color_name2handle(term->tl_cursor_color);
2518 entry.name = "n";
2519 entry.used_for = SHAPE_CURSOR;
2520
2521 return &entry;
2522}
2523#endif
2524
Bram Moolenaard317b382018-02-08 22:33:31 +01002525 static void
2526may_output_cursor_props(void)
2527{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002528 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002529 || last_set_cursor_shape != desired_cursor_shape
2530 || last_set_cursor_blink != desired_cursor_blink)
2531 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002532 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002533 last_set_cursor_shape = desired_cursor_shape;
2534 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002535 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002536 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002537 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002538 ui_cursor_shape_forced(TRUE);
2539 else
2540 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2541 }
2542}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002543
Bram Moolenaard317b382018-02-08 22:33:31 +01002544/*
2545 * Set the cursor color and shape, if not last set to these.
2546 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002547 static void
2548may_set_cursor_props(term_T *term)
2549{
2550#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002551 // For the GUI the cursor properties are obtained with
2552 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002553 if (gui.in_use)
2554 return;
2555#endif
2556 if (in_terminal_loop == term)
2557 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002558 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002559 desired_cursor_shape = term->tl_cursor_shape;
2560 desired_cursor_blink = term->tl_cursor_blink;
2561 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002562 }
2563}
2564
Bram Moolenaard317b382018-02-08 22:33:31 +01002565/*
2566 * Reset the desired cursor properties and restore them when needed.
2567 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002568 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002569prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002570{
2571#ifdef FEAT_GUI
2572 if (gui.in_use)
2573 return;
2574#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002575 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002576 desired_cursor_shape = -1;
2577 desired_cursor_blink = -1;
2578 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002579}
2580
2581/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002582 * Returns TRUE if the current window contains a terminal and we are sending
2583 * keys to the job.
2584 * If "check_job_status" is TRUE update the job status.
2585 */
2586 static int
2587term_use_loop_check(int check_job_status)
2588{
2589 term_T *term = curbuf->b_term;
2590
2591 return term != NULL
2592 && !term->tl_normal_mode
2593 && term->tl_vterm != NULL
2594 && term_job_running_check(term, check_job_status);
2595}
2596
2597/*
2598 * Returns TRUE if the current window contains a terminal and we are sending
2599 * keys to the job.
2600 */
2601 int
2602term_use_loop(void)
2603{
2604 return term_use_loop_check(FALSE);
2605}
2606
2607/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002608 * Called when entering a window with the mouse. If this is a terminal window
2609 * we may want to change state.
2610 */
2611 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002612term_win_entered(void)
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002613{
2614 term_T *term = curbuf->b_term;
2615
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002616 if (term == NULL)
2617 return;
2618
2619 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002620 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002621 reset_VIsual_and_resel();
2622 if (State & MODE_INSERT)
2623 stop_insert_mode = TRUE;
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002624 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002625 mouse_was_outside = FALSE;
2626 enter_mouse_col = mouse_col;
2627 enter_mouse_row = mouse_row;
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002628}
2629
Bram Moolenaara48d4e42021-12-08 22:13:38 +00002630 void
2631term_focus_change(int in_focus)
2632{
2633 term_T *term = curbuf->b_term;
2634
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002635 if (term == NULL || term->tl_vterm == NULL)
2636 return;
Bram Moolenaara48d4e42021-12-08 22:13:38 +00002637
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002638 VTermState *state = vterm_obtain_state(term->tl_vterm);
2639
2640 if (in_focus)
2641 vterm_state_focus_in(state);
2642 else
2643 vterm_state_focus_out(state);
2644 term_forward_output(term);
Bram Moolenaara48d4e42021-12-08 22:13:38 +00002645}
2646
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002647/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002648 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2649 * Return the Ctrl-key value in that case.
2650 */
2651 static int
2652raw_c_to_ctrl(int c)
2653{
2654 if ((mod_mask & MOD_MASK_CTRL)
2655 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2656 return c & 0x1f;
2657 return c;
2658}
2659
2660/*
2661 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002662 * Also when the Kitty keyboard protocol is used.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002663 * May set "mod_mask".
2664 */
2665 static int
2666ctrl_to_raw_c(int c)
2667{
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002668 if (c < 0x20 && vterm_using_key_protocol())
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002669 {
2670 mod_mask |= MOD_MASK_CTRL;
2671 return c + '@';
2672 }
2673 return c;
2674}
2675
2676/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002677 * Wait for input and send it to the job.
2678 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2679 * when there is no more typahead.
2680 * Return when the start of a CTRL-W command is typed or anything else that
2681 * should be handled as a Normal mode command.
2682 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2683 * the terminal was closed.
2684 */
2685 int
2686terminal_loop(int blocking)
2687{
2688 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002689 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002690 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002691 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002692#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002693 int tty_fd = curbuf->b_term->tl_job->jv_channel
2694 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002695#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002696 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002697
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002698 // Remember the terminal we are sending keys to. However, the terminal
2699 // might be closed while waiting for a character, e.g. typing "exit" in a
2700 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2701 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002702 in_terminal_loop = curbuf->b_term;
2703
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002704 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002705 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002706 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002707 if (termwinkey == Ctrl_W)
2708 termwinkey = 0;
2709 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002710 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002711 may_set_cursor_props(curbuf->b_term);
2712
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002713 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002714 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002715#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002716 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002717#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002718 // TODO: skip screen update when handling a sequence of keys.
2719 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002720 while (must_redraw != 0)
2721 if (update_screen(0) == FAIL)
2722 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002723 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002724 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002725 break;
2726
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002727 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002728 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002729
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002730 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002731 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002732 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002733 // Job finished while waiting for a character. Push back the
2734 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002735 if (raw_c != K_IGNORE)
2736 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002737 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002738 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002739 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002740 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002741 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002742
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002743#ifdef UNIX
2744 /*
2745 * The shell or another program may change the tty settings. Getting
2746 * them for every typed character is a bit of overhead, but it's needed
2747 * for the first character typed, e.g. when Vim starts in a shell.
2748 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002749 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002750 {
2751 ttyinfo_T info;
2752
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002753 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002754 if (get_tty_info(tty_fd, &info) == OK)
2755 term_backspace_char = info.backspace;
2756 }
2757#endif
2758
Bram Moolenaar4f974752019-02-17 17:44:42 +01002759#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002760 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2761 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002762 if (ctrl_break_was_pressed)
2763 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2764#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002765 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2766 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002767 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002768#ifdef FEAT_GUI
2769 && !curbuf->b_term->tl_system
2770#endif
2771 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002772 {
2773 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002774 int prev_raw_c = raw_c;
2775 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002776
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002777 if (add_to_showcmd(c))
2778 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +01002779
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002780 raw_c = term_vgetc();
2781 c = raw_c_to_ctrl(raw_c);
2782
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002783 clear_showcmd();
Martin Tournoijba43e762022-10-13 22:12:15 +01002784
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002785 if (!term_use_loop_check(TRUE)
2786 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002787 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002788 break;
2789
2790 if (prev_c == Ctrl_BSL)
2791 {
2792 if (c == Ctrl_N)
2793 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002794 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002795 term_enter_normal_mode();
2796 ret = FAIL;
2797 goto theend;
2798 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002799 // Send both keys to the terminal, first one here, second one
2800 // below.
2801 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2802 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002803 }
2804 else if (c == Ctrl_C)
2805 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002806 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002807 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2808 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002809 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002810 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002811 // "CTRL-W .": send CTRL-W to the job
2812 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002813 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002814 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002815 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002816 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002817 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002818 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002819 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002820 else if (c == 'N')
2821 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002822 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002823 term_enter_normal_mode();
2824 ret = FAIL;
2825 goto theend;
2826 }
2827 else if (c == '"')
2828 {
2829 term_paste_register(prev_c);
2830 continue;
2831 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002832 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002833 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002834 // space for CTRL-W, modifier, multi-byte char and NUL
2835 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002836
2837 // Put the command into the typeahead buffer, when using the
2838 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2839 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002840 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002841 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002842 ret = OK;
2843 goto theend;
2844 }
2845 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002846# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002847 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002848 {
2849 WCHAR wc;
2850 char_u mb[3];
2851
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002852 mb[0] = (unsigned)raw_c >> 8;
2853 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002854 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002855 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002856 }
2857# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002858 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002859 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002860 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002861 // We are sure to come back here, don't reset the cursor color
2862 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002863 restore_cursor = FALSE;
2864
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002865 ret = OK;
2866 goto theend;
2867 }
2868 }
2869 ret = FAIL;
2870
2871theend:
2872 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002873 if (restore_cursor)
2874 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002875
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002876 // Move a snapshot of the screen contents to the buffer, so that completion
2877 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002878 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2879 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002880
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002881 return ret;
2882}
2883
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002884 static void
2885may_toggle_cursor(term_T *term)
2886{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002887 if (in_terminal_loop != term)
2888 return;
2889
2890 if (term->tl_cursor_visible)
2891 cursor_on();
2892 else
2893 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002894}
2895
2896/*
2897 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002898 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002899 */
2900 static int
2901color2index(VTermColor *color, int fg, int *boldp)
2902{
2903 int red = color->red;
2904 int blue = color->blue;
2905 int green = color->green;
2906
LemonBoyb2b3acb2022-05-20 10:10:34 +01002907 *boldp = FALSE;
2908
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002909 if (VTERM_COLOR_IS_INVALID(color))
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002910 return 0;
LemonBoyb2b3acb2022-05-20 10:10:34 +01002911
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002912 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002913 {
LemonBoyb2b3acb2022-05-20 10:10:34 +01002914 // Use the color as-is if possible, give up otherwise.
2915 if (color->index < t_colors)
2916 return color->index + 1;
2917 // 8-color terminals can actually display twice as many colors by
2918 // setting the high-intensity/bold bit.
2919 else if (t_colors == 8 && fg && color->index < 16)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002920 {
LemonBoyb2b3acb2022-05-20 10:10:34 +01002921 *boldp = TRUE;
2922 return (color->index & 7) + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002923 }
LemonBoyb2b3acb2022-05-20 10:10:34 +01002924 return 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002925 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002926
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002927 if (t_colors >= 256)
2928 {
2929 if (red == blue && red == green)
2930 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002931 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002932 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002933 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2934 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2935 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002936 int i;
2937
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002938 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002939 return 17; // 00/00/00
2940 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002941 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002942 for (i = 0; i < 23; ++i)
2943 if (red < cutoff[i])
2944 return i + 233;
2945 return 256;
2946 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002947 {
2948 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2949 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002950
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002951 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002952 for (ri = 0; ri < 5; ++ri)
2953 if (red < cutoff[ri])
2954 break;
2955 for (gi = 0; gi < 5; ++gi)
2956 if (green < cutoff[gi])
2957 break;
2958 for (bi = 0; bi < 5; ++bi)
2959 if (blue < cutoff[bi])
2960 break;
2961 return 17 + ri * 36 + gi * 6 + bi;
2962 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002963 }
2964 return 0;
2965}
2966
2967/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002968 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002969 */
2970 static int
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002971vtermAttr2hl(VTermScreenCellAttrs *cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002972{
2973 int attr = 0;
2974
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002975 if (cellattrs->bold)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002976 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002977 if (cellattrs->underline)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002978 attr |= HL_UNDERLINE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002979 if (cellattrs->italic)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002980 attr |= HL_ITALIC;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002981 if (cellattrs->strike)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002982 attr |= HL_STRIKETHROUGH;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002983 if (cellattrs->reverse)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002984 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002985 return attr;
2986}
2987
2988/*
2989 * Store Vterm attributes in "cell" from highlight flags.
2990 */
2991 static void
2992hl2vtermAttr(int attr, cellattr_T *cell)
2993{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002994 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002995 if (attr & HL_BOLD)
2996 cell->attrs.bold = 1;
2997 if (attr & HL_UNDERLINE)
2998 cell->attrs.underline = 1;
2999 if (attr & HL_ITALIC)
3000 cell->attrs.italic = 1;
3001 if (attr & HL_STRIKETHROUGH)
3002 cell->attrs.strike = 1;
3003 if (attr & HL_INVERSE)
3004 cell->attrs.reverse = 1;
3005}
3006
3007/*
3008 * Convert the attributes of a vterm cell into an attribute index.
3009 */
3010 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003011cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003012 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003013 win_T *wp,
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003014 VTermScreenCellAttrs *cellattrs,
3015 VTermColor *cellfg,
3016 VTermColor *cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01003017{
3018 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003019 VTermColor *fg = cellfg;
3020 VTermColor *bg = cellbg;
3021 int is_default_fg = VTERM_COLOR_IS_DEFAULT_FG(fg);
3022 int is_default_bg = VTERM_COLOR_IS_DEFAULT_BG(bg);
3023
3024 if (is_default_fg || is_default_bg)
3025 {
3026 if (wp != NULL && *wp->w_p_wcr != NUL)
3027 {
3028 if (is_default_fg)
3029 fg = &wp->w_term_wincolor.fg;
3030 if (is_default_bg)
3031 bg = &wp->w_term_wincolor.bg;
3032 }
3033 else
3034 {
3035 if (is_default_fg)
3036 fg = &term->tl_default_color.fg;
3037 if (is_default_bg)
3038 bg = &term->tl_default_color.bg;
3039 }
3040 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003041
3042#ifdef FEAT_GUI
3043 if (gui.in_use)
3044 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003045 guicolor_T guifg = gui_mch_get_rgb_color(fg->red, fg->green, fg->blue);
3046 guicolor_T guibg = gui_mch_get_rgb_color(bg->red, bg->green, bg->blue);
3047 return get_gui_attr_idx(attr, guifg, guibg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003048 }
3049 else
3050#endif
3051#ifdef FEAT_TERMGUICOLORS
3052 if (p_tgc)
3053 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003054 guicolor_T tgcfg = VTERM_COLOR_IS_INVALID(fg)
3055 ? INVALCOLOR
3056 : gui_get_rgb_color_cmn(fg->red, fg->green, fg->blue);
3057 guicolor_T tgcbg = VTERM_COLOR_IS_INVALID(bg)
3058 ? INVALCOLOR
3059 : gui_get_rgb_color_cmn(bg->red, bg->green, bg->blue);
3060 return get_tgc_attr_idx(attr, tgcfg, tgcbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003061 }
3062 else
3063#endif
3064 {
3065 int bold = MAYBE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003066 int ctermfg = color2index(fg, TRUE, &bold);
3067 int ctermbg = color2index(bg, FALSE, &bold);
Bram Moolenaar76bb7192017-11-30 22:07:07 +01003068
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003069 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003070 if (bold == TRUE)
3071 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003072
3073 return get_cterm_attr_idx(attr, ctermfg, ctermbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003074 }
3075 return 0;
3076}
3077
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003078 static void
3079set_dirty_snapshot(term_T *term)
3080{
3081 term->tl_dirty_snapshot = TRUE;
3082#ifdef FEAT_TIMERS
3083 if (!term->tl_normal_mode)
3084 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003085 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003086 profile_setlimit(100L, &term->tl_timer_due);
3087 term->tl_timer_set = TRUE;
3088 }
3089#endif
3090}
3091
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003092 static int
3093handle_damage(VTermRect rect, void *user)
3094{
3095 term_T *term = (term_T *)user;
3096
3097 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
3098 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003099 set_dirty_snapshot(term);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003100 redraw_buf_later(term->tl_buffer, UPD_SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003101 return 1;
3102}
3103
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003104 static void
3105term_scroll_up(term_T *term, int start_row, int count)
3106{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003107 win_T *wp = NULL;
3108 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003109 VTermColor fg, bg;
3110 VTermScreenCellAttrs attr;
3111 int clear_attr;
3112
Bram Moolenaara80faa82020-04-12 19:37:17 +02003113 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003114
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003115 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003116 {
3117 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003118 {
3119 // Set the color to clear lines with.
3120 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
3121 &fg, &bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003122 clear_attr = cell2attr(term, wp, &attr, &fg, &bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003123 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003124 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003125 }
3126}
3127
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003128 static int
3129handle_moverect(VTermRect dest, VTermRect src, void *user)
3130{
3131 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003132 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003133
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003134 // Scrolling up is done much more efficiently by deleting lines instead of
3135 // redrawing the text. But avoid doing this multiple times, postpone until
3136 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003137 if (dest.start_col == src.start_col
3138 && dest.end_col == src.end_col
3139 && dest.start_row < src.start_row)
3140 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003141 if (dest.start_row == 0)
3142 term->tl_postponed_scroll += count;
3143 else
3144 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003145 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003146
3147 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3148 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003149 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003150
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003151 // Note sure if the scrolling will work correctly, let's do a complete
3152 // redraw later.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003153 redraw_buf_later(term->tl_buffer, UPD_NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003154 return 1;
3155}
3156
3157 static int
3158handle_movecursor(
3159 VTermPos pos,
3160 VTermPos oldpos UNUSED,
3161 int visible,
3162 void *user)
3163{
3164 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003165 win_T *wp = NULL;
3166 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003167
3168 term->tl_cursor_pos = pos;
3169 term->tl_cursor_visible = visible;
3170
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003171 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003172 {
3173 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003174 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003175 }
3176 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003177 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003178
3179 return 1;
3180}
3181
3182 static int
3183handle_settermprop(
3184 VTermProp prop,
3185 VTermValue *value,
3186 void *user)
3187{
3188 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003189 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003190
3191 switch (prop)
3192 {
3193 case VTERM_PROP_TITLE:
ichizokae1bd872022-01-20 14:57:29 +00003194 if (disable_vterm_title_for_testing)
3195 break;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003196 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003197 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003198 if (strval == NULL)
3199 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003200 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003201 // a blank title isn't useful, make it empty, so that "running" is
3202 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003203 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003204 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003205 // Same as blank
3206 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003207 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003208 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3209 term->tl_title = NULL;
3210 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003211 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003212 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003213#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003214 else if (!enc_utf8 && enc_codepage > 0)
3215 {
3216 WCHAR *ret = NULL;
3217 int length = 0;
3218
3219 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003220 (char*)value->string.str,
3221 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003222 if (ret != NULL)
3223 {
3224 WideCharToMultiByte_alloc(enc_codepage, 0,
3225 ret, length, (char**)&term->tl_title,
3226 &length, 0, 0);
3227 vim_free(ret);
3228 }
3229 }
3230#endif
3231 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003232 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003233 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003234 strval = NULL;
3235 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003236 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003237 if (term == curbuf->b_term)
LemonBoy327e6dd2022-06-04 19:57:59 +01003238 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003239 maketitle();
LemonBoy327e6dd2022-06-04 19:57:59 +01003240 curwin->w_redr_status = TRUE;
3241 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003242 break;
3243
3244 case VTERM_PROP_CURSORVISIBLE:
3245 term->tl_cursor_visible = value->boolean;
3246 may_toggle_cursor(term);
3247 out_flush();
3248 break;
3249
3250 case VTERM_PROP_CURSORBLINK:
3251 term->tl_cursor_blink = value->boolean;
3252 may_set_cursor_props(term);
3253 break;
3254
3255 case VTERM_PROP_CURSORSHAPE:
3256 term->tl_cursor_shape = value->number;
3257 may_set_cursor_props(term);
3258 break;
3259
3260 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003261 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003262 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003263 if (strval == NULL)
3264 break;
3265 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003266 may_set_cursor_props(term);
3267 break;
3268
3269 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003270 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003271 term->tl_using_altscreen = value->boolean;
3272 break;
3273
3274 default:
3275 break;
3276 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003277 vim_free(strval);
3278
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003279 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003280 return 1;
3281}
3282
3283/*
3284 * The job running in the terminal resized the terminal.
3285 */
3286 static int
3287handle_resize(int rows, int cols, void *user)
3288{
3289 term_T *term = (term_T *)user;
3290 win_T *wp;
3291
3292 term->tl_rows = rows;
3293 term->tl_cols = cols;
3294 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003295 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003296 term->tl_vterm_size_changed = FALSE;
3297 else
3298 {
3299 FOR_ALL_WINDOWS(wp)
3300 {
3301 if (wp->w_buffer == term->tl_buffer)
3302 {
3303 win_setheight_win(rows, wp);
3304 win_setwidth_win(cols, wp);
3305 }
3306 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003307 redraw_buf_later(term->tl_buffer, UPD_NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003308 }
3309 return 1;
3310}
3311
3312/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003313 * If the number of lines that are stored goes over 'termscrollback' then
3314 * delete the first 10%.
3315 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3316 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003317 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003318 static void
3319limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003320{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003321 if (gap->ga_len < term->tl_buffer->b_p_twsl)
3322 return;
3323
3324 int todo = term->tl_buffer->b_p_twsl / 10;
3325 int i;
3326
3327 curbuf = term->tl_buffer;
3328 for (i = 0; i < todo; ++i)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003329 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003330 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003331 if (update_buffer)
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003332 ml_delete(1);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003333 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003334 curbuf = curwin->w_buffer;
3335
3336 gap->ga_len -= todo;
3337 mch_memmove(gap->ga_data,
3338 (sb_line_T *)gap->ga_data + todo,
3339 sizeof(sb_line_T) * gap->ga_len);
3340 if (update_buffer)
3341 term->tl_scrollback_scrolled -= todo;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003342}
3343
3344/*
3345 * Handle a line that is pushed off the top of the screen.
3346 */
3347 static int
3348handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3349{
3350 term_T *term = (term_T *)user;
3351 garray_T *gap;
3352 int update_buffer;
3353
3354 if (term->tl_normal_mode)
3355 {
3356 // In Terminal-Normal mode the user interacts with the buffer, thus we
3357 // must not change it. Postpone adding the scrollback lines.
3358 gap = &term->tl_scrollback_postponed;
3359 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003360 }
3361 else
3362 {
3363 // First remove the lines that were appended before, the pushed line
3364 // goes above it.
3365 cleanup_scrollback(term);
3366 gap = &term->tl_scrollback;
3367 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003368 }
3369
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003370 limit_scrollback(term, gap, update_buffer);
3371
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003372 if (ga_grow(gap, 1) == FAIL)
3373 return 0;
3374
3375 cellattr_T *p = NULL;
3376 int len = 0;
3377 int i;
3378 int c;
3379 int col;
3380 int text_len;
3381 char_u *text;
3382 sb_line_T *line;
3383 garray_T ga;
3384 cellattr_T fill_attr = term->tl_default_color;
3385
3386 // do not store empty cells at the end
3387 for (i = 0; i < cols; ++i)
3388 if (cells[i].chars[0] != 0)
3389 len = i + 1;
3390 else
3391 cell2cellattr(&cells[i], &fill_attr);
3392
3393 ga_init2(&ga, 1, 100);
3394 if (len > 0)
3395 p = ALLOC_MULT(cellattr_T, len);
3396 if (p != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003397 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003398 for (col = 0; col < len; col += cells[col].width)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003399 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003400 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003401 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003402 ga.ga_len = 0;
3403 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003404 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003405 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3406 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3407 (char_u *)ga.ga_data + ga.ga_len);
3408 cell2cellattr(&cells[col], &p[col]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003409 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003410 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003411 if (ga_grow(&ga, 1) == FAIL)
3412 {
3413 if (update_buffer)
3414 text = (char_u *)"";
3415 else
3416 text = vim_strsave((char_u *)"");
3417 text_len = 0;
3418 }
3419 else
3420 {
3421 text = ga.ga_data;
3422 text_len = ga.ga_len;
3423 *(text + text_len) = NUL;
3424 }
3425 if (update_buffer)
3426 add_scrollback_line_to_buffer(term, text, text_len);
3427
3428 line = (sb_line_T *)gap->ga_data + gap->ga_len;
3429 line->sb_cols = len;
3430 line->sb_cells = p;
3431 line->sb_fill_attr = fill_attr;
3432 if (update_buffer)
3433 {
3434 line->sb_text = NULL;
3435 ++term->tl_scrollback_scrolled;
3436 ga_clear(&ga); // free the text
3437 }
3438 else
3439 {
3440 line->sb_text = text;
3441 ga_init(&ga); // text is kept in tl_scrollback_postponed
3442 }
3443 ++gap->ga_len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003444 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003445}
3446
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003447/*
3448 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3449 * received and stored in tl_scrollback_postponed.
3450 */
3451 static void
3452handle_postponed_scrollback(term_T *term)
3453{
3454 int i;
3455
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003456 if (term->tl_scrollback_postponed.ga_len == 0)
3457 return;
3458 ch_log(NULL, "Moving postponed scrollback to scrollback");
3459
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003460 // First remove the lines that were appended before, the pushed lines go
3461 // above it.
3462 cleanup_scrollback(term);
3463
3464 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3465 {
3466 char_u *text;
3467 sb_line_T *pp_line;
3468 sb_line_T *line;
3469
3470 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3471 break;
3472 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3473
3474 text = pp_line->sb_text;
3475 if (text == NULL)
3476 text = (char_u *)"";
3477 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3478 vim_free(pp_line->sb_text);
3479
3480 line = (sb_line_T *)term->tl_scrollback.ga_data
3481 + term->tl_scrollback.ga_len;
3482 line->sb_cols = pp_line->sb_cols;
3483 line->sb_cells = pp_line->sb_cells;
3484 line->sb_fill_attr = pp_line->sb_fill_attr;
3485 line->sb_text = NULL;
3486 ++term->tl_scrollback_scrolled;
3487 ++term->tl_scrollback.ga_len;
3488 }
3489
3490 ga_clear(&term->tl_scrollback_postponed);
3491 limit_scrollback(term, &term->tl_scrollback, TRUE);
3492}
3493
LemonBoy77771d32022-04-13 11:47:25 +01003494/*
3495 * Called when the terminal wants to ring the system bell.
3496 */
3497 static int
3498handle_bell(void *user UNUSED)
3499{
Bram Moolenaaraae97622022-04-13 14:28:07 +01003500 vim_beep(BO_TERM);
LemonBoy77771d32022-04-13 11:47:25 +01003501 return 0;
3502}
3503
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003504static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003505 handle_damage, // damage
3506 handle_moverect, // moverect
3507 handle_movecursor, // movecursor
3508 handle_settermprop, // settermprop
LemonBoy77771d32022-04-13 11:47:25 +01003509 handle_bell, // bell
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003510 handle_resize, // resize
3511 handle_pushline, // sb_pushline
Bram Moolenaar6a12d262022-10-16 19:26:52 +01003512 NULL, // sb_popline
3513 NULL // sb_clear
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003514};
3515
3516/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003517 * Do the work after the channel of a terminal was closed.
3518 * Must be called only when updating_screen is FALSE.
3519 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3520 */
3521 static int
3522term_after_channel_closed(term_T *term)
3523{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003524 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003525 if (!term->tl_normal_mode)
3526 {
3527 int fnum = term->tl_buffer->b_fnum;
3528
3529 cleanup_vterm(term);
3530
3531 if (term->tl_finish == TL_FINISH_CLOSE)
3532 {
3533 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003534 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003535#ifdef FEAT_PROP_POPUP
3536 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003537
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003538 // If this was a terminal in a popup window, go back to the
3539 // previous window.
3540 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3541 {
3542 pwin = curwin;
3543 if (win_valid(prevwin))
3544 win_enter(prevwin, FALSE);
3545 }
3546 else
3547#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003548 // If this is the last normal window: exit Vim.
3549 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3550 {
3551 exarg_T ea;
3552
Bram Moolenaara80faa82020-04-12 19:37:17 +02003553 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003554 ex_quit(&ea);
3555 return TRUE;
3556 }
3557
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003558 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003559 ch_log(NULL, "terminal job finished, closing window");
3560 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaare76062c2022-11-28 18:51:43 +00003561 if (curbuf == term->tl_buffer)
3562 {
3563 // Avoid closing the window if we temporarily use it.
3564 if (is_aucmd_win(curwin))
3565 do_set_w_closing = TRUE;
3566 if (do_set_w_closing)
3567 curwin->w_closing = TRUE;
3568 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
3569 if (do_set_w_closing)
3570 curwin->w_closing = FALSE;
3571 aucmd_restbuf(&aco);
3572 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003573#ifdef FEAT_PROP_POPUP
3574 if (pwin != NULL)
3575 popup_close_with_retval(pwin, 0);
3576#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003577 return TRUE;
3578 }
3579 if (term->tl_finish == TL_FINISH_OPEN
3580 && term->tl_buffer->b_nwindows == 0)
3581 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003582 char *cmd = term->tl_opencmd == NULL
3583 ? "botright sbuf %d"
3584 : (char *)term->tl_opencmd;
3585 size_t len = strlen(cmd) + 50;
3586 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003587
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003588 if (buf != NULL)
3589 {
3590 ch_log(NULL, "terminal job finished, opening window");
3591 vim_snprintf(buf, len, cmd, fnum);
3592 do_cmdline_cmd((char_u *)buf);
3593 vim_free(buf);
3594 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003595 }
3596 else
3597 ch_log(NULL, "terminal job finished");
3598 }
3599
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003600 redraw_buf_and_status_later(term->tl_buffer, UPD_NOT_VALID);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003601 return FALSE;
3602}
3603
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003604#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3605/*
3606 * If the current window is a terminal in a popup window and the job has
3607 * finished, close the popup window and to back to the previous window.
3608 * Otherwise return FAIL.
3609 */
3610 int
3611may_close_term_popup(void)
3612{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003613 if (!popup_is_popup(curwin) || curbuf->b_term == NULL
3614 || term_job_running_not_none(curbuf->b_term))
3615 return FAIL;
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003616
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003617 win_T *pwin = curwin;
3618
3619 if (win_valid(prevwin))
3620 win_enter(prevwin, FALSE);
3621 popup_close_with_retval(pwin, 0);
3622 return OK;
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003623}
3624#endif
3625
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003626/*
Bram Moolenaareea32af2021-11-21 14:51:13 +00003627 * Called when a channel is going to be closed, before invoking the close
3628 * callback.
3629 */
3630 void
3631term_channel_closing(channel_T *ch)
3632{
3633 term_T *term;
3634
3635 for (term = first_term; term != NULL; term = term->tl_next)
3636 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
3637 term->tl_channel_closing = TRUE;
3638}
3639
3640/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003641 * Called when a channel has been closed.
3642 * If this was a channel for a terminal window then finish it up.
3643 */
3644 void
3645term_channel_closed(channel_T *ch)
3646{
3647 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003648 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003649 int did_one = FALSE;
3650
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003651 for (term = first_term; term != NULL; term = next_term)
3652 {
3653 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003654 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003655 {
3656 term->tl_channel_closed = TRUE;
3657 did_one = TRUE;
3658
Bram Moolenaard23a8232018-02-10 18:45:26 +01003659 VIM_CLEAR(term->tl_title);
3660 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003661#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003662 if (term->tl_out_fd != NULL)
3663 {
3664 fclose(term->tl_out_fd);
3665 term->tl_out_fd = NULL;
3666 }
3667#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003668
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003669 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003670 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003671 // Cannot open or close windows now. Can happen when
3672 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003673 term->tl_channel_recently_closed = TRUE;
3674 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003675 }
3676
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003677 if (term_after_channel_closed(term))
3678 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003679 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003680 }
3681
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003682 if (did_one)
3683 {
3684 redraw_statuslines();
3685
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003686 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003687 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003688 typebuf_was_filled = TRUE;
3689
3690 term = curbuf->b_term;
3691 if (term != NULL)
3692 {
3693 if (term->tl_job == ch->ch_job)
3694 maketitle();
3695 update_cursor(term, term->tl_cursor_visible);
3696 }
3697 }
3698}
3699
3700/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003701 * To be called after resetting updating_screen: handle any terminal where the
3702 * channel was closed.
3703 */
3704 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003705term_check_channel_closed_recently(void)
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003706{
3707 term_T *term;
3708 term_T *next_term;
3709
3710 for (term = first_term; term != NULL; term = next_term)
3711 {
3712 next_term = term->tl_next;
3713 if (term->tl_channel_recently_closed)
3714 {
3715 term->tl_channel_recently_closed = FALSE;
3716 if (term_after_channel_closed(term))
3717 // start over, the list may have changed
3718 next_term = first_term;
3719 }
3720 }
3721}
3722
3723/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003724 * Fill one screen line from a line of the terminal.
3725 * Advances "pos" to past the last column.
3726 */
3727 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003728term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003729 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003730 win_T *wp,
3731 VTermScreen *screen,
3732 VTermPos *pos,
3733 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003734{
3735 int off = screen_get_current_line_off();
3736
3737 for (pos->col = 0; pos->col < max_col; )
3738 {
3739 VTermScreenCell cell;
3740 int c;
3741
3742 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003743 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003744
3745 c = cell.chars[0];
3746 if (c == NUL)
3747 {
3748 ScreenLines[off] = ' ';
3749 if (enc_utf8)
3750 ScreenLinesUC[off] = NUL;
3751 }
3752 else
3753 {
3754 if (enc_utf8)
3755 {
3756 int i;
3757
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003758 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003759 for (i = 0; i < Screen_mco
3760 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3761 {
3762 ScreenLinesC[i][off] = cell.chars[i + 1];
3763 if (cell.chars[i + 1] == 0)
3764 break;
3765 }
3766 if (c >= 0x80 || (Screen_mco > 0
3767 && ScreenLinesC[0][off] != 0))
3768 {
3769 ScreenLines[off] = ' ';
3770 ScreenLinesUC[off] = c;
3771 }
3772 else
3773 {
3774 ScreenLines[off] = c;
3775 ScreenLinesUC[off] = NUL;
3776 }
3777 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003778#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003779 else if (has_mbyte && c >= 0x80)
3780 {
3781 char_u mb[MB_MAXBYTES+1];
3782 WCHAR wc = c;
3783
3784 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3785 (char*)mb, 2, 0, 0) > 1)
3786 {
3787 ScreenLines[off] = mb[0];
3788 ScreenLines[off + 1] = mb[1];
3789 cell.width = mb_ptr2cells(mb);
3790 }
3791 else
3792 ScreenLines[off] = c;
3793 }
3794#endif
3795 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003796 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003797 ScreenLines[off] = c;
3798 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003799 ScreenAttrs[off] = cell2attr(term, wp, &cell.attrs, &cell.fg,
3800 &cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003801
3802 ++pos->col;
3803 ++off;
3804 if (cell.width == 2)
3805 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003806 // don't set the second byte to NUL for a DBCS encoding, it
3807 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003808 if (enc_utf8)
3809 {
3810 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003811 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003812 }
3813 else if (!has_mbyte)
3814 {
3815 // Can't show a double-width character with a single-byte
3816 // 'encoding', just use a space.
3817 ScreenLines[off] = ' ';
3818 ScreenAttrs[off] = ScreenAttrs[off - 1];
3819 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003820
3821 ++pos->col;
3822 ++off;
3823 }
3824 }
3825}
3826
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003827#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003828 static void
3829update_system_term(term_T *term)
3830{
3831 VTermPos pos;
3832 VTermScreen *screen;
3833
3834 if (term->tl_vterm == NULL)
3835 return;
3836 screen = vterm_obtain_screen(term->tl_vterm);
3837
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003838 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003839 while (term->tl_toprow > 0
3840 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3841 {
3842 int save_p_more = p_more;
3843
3844 p_more = FALSE;
3845 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003846 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003847 p_more = save_p_more;
3848 --term->tl_toprow;
3849 }
3850
3851 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3852 && pos.row < Rows; ++pos.row)
3853 {
3854 if (pos.row < term->tl_rows)
3855 {
3856 int max_col = MIN(Columns, term->tl_cols);
3857
Bram Moolenaar83d47902020-03-26 20:34:00 +01003858 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003859 }
3860 else
3861 pos.col = 0;
3862
Bram Moolenaar510f0372022-07-04 17:46:22 +01003863 screen_line(curwin, term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003864 }
3865
3866 term->tl_dirty_row_start = MAX_ROW;
3867 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003868}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003869#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003870
3871/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003872 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3873 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003874 * Terminal-Normal mode.
3875 */
3876 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003877term_do_update_window(win_T *wp)
3878{
3879 term_T *term = wp->w_buffer->b_term;
3880
3881 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3882}
3883
3884/*
3885 * Called to update a window that contains an active terminal.
3886 */
3887 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003888term_update_window(win_T *wp)
3889{
3890 term_T *term = wp->w_buffer->b_term;
3891 VTerm *vterm;
3892 VTermScreen *screen;
3893 VTermState *state;
3894 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003895 int rows, cols;
3896 int newrows, newcols;
3897 int minsize;
3898 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003899
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003900 vterm = term->tl_vterm;
3901 screen = vterm_obtain_screen(vterm);
3902 state = vterm_obtain_state(vterm);
3903
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003904 // We use UPD_NOT_VALID on a resize or scroll, redraw everything then.
3905 // With UPD_SOME_VALID only redraw what was marked dirty.
3906 if (wp->w_redr_type > UPD_SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003907 {
3908 term->tl_dirty_row_start = 0;
3909 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003910
3911 if (term->tl_postponed_scroll > 0
3912 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003913 // Scrolling is usually faster than redrawing, when there are only
3914 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003915 term_scroll_up(term, 0, term->tl_postponed_scroll);
3916 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003917 }
3918
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003919 /*
3920 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003921 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003922 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003923 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003924
Bram Moolenaar498c2562018-04-15 23:45:15 +02003925 newrows = 99999;
3926 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003927 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003928 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003929 // Always use curwin, it may be a popup window.
3930 win_T *wwp = twp == NULL ? curwin : twp;
3931
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003932 // When more than one window shows the same terminal, use the
3933 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003934 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003935 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003936 newrows = MIN(newrows, wwp->w_height);
3937 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003938 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003939 if (twp == NULL)
3940 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003941 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003942 if (newrows == 99999 || newcols == 99999)
3943 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003944 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3945 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3946
Bram Moolenaareba13e42021-02-23 17:47:23 +01003947 // If no cell is visible there is no point in resizing. Also, vterm can't
3948 // handle a zero height.
3949 if (newrows == 0 || newcols == 0)
3950 return;
3951
Bram Moolenaar498c2562018-04-15 23:45:15 +02003952 if (term->tl_rows != newrows || term->tl_cols != newcols)
3953 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003954 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003955 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003956 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003957 newrows);
3958 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003959
3960 // Updating the terminal size will cause the snapshot to be cleared.
3961 // When not in terminal_loop() we need to restore it.
3962 if (term != in_terminal_loop)
3963 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003964 }
3965
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003966 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003967 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003968 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003969
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003970 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3971 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003972 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003973 if (pos.row < term->tl_rows)
3974 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003975 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003976
Bram Moolenaar83d47902020-03-26 20:34:00 +01003977 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003978 }
3979 else
3980 pos.col = 0;
3981
Bram Moolenaar510f0372022-07-04 17:46:22 +01003982 screen_line(wp, wp->w_winrow + pos.row
Bram Moolenaarf118d482018-03-13 13:14:00 +01003983#ifdef FEAT_MENU
3984 + winbar_height(wp)
3985#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003986 , wp->w_wincol, pos.col, wp->w_width,
3987#ifdef FEAT_PROP_POPUP
3988 popup_is_popup(wp) ? SLF_POPUP :
3989#endif
3990 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003991 }
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00003992}
3993
3994/*
3995 * Called after updating all windows: may reset dirty rows.
3996 */
3997 void
3998term_did_update_window(win_T *wp)
3999{
4000 term_T *term = wp->w_buffer->b_term;
4001
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004002 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode
4003 || wp->w_redr_type != 0)
4004 return;
4005
4006 term->tl_dirty_row_start = MAX_ROW;
4007 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004008}
4009
4010/*
4011 * Return TRUE if "wp" is a terminal window where the job has finished.
4012 */
4013 int
4014term_is_finished(buf_T *buf)
4015{
4016 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
4017}
4018
4019/*
4020 * Return TRUE if "wp" is a terminal window where the job has finished or we
4021 * are in Terminal-Normal mode, thus we show the buffer contents.
4022 */
4023 int
4024term_show_buffer(buf_T *buf)
4025{
4026 term_T *term = buf->b_term;
4027
4028 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
4029}
4030
4031/*
4032 * The current buffer is going to be changed. If there is terminal
4033 * highlighting remove it now.
4034 */
4035 void
4036term_change_in_curbuf(void)
4037{
4038 term_T *term = curbuf->b_term;
4039
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004040 if (!term_is_finished(curbuf) || term->tl_scrollback.ga_len <= 0)
4041 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004042
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004043 free_scrollback(term);
4044 redraw_buf_later(term->tl_buffer, UPD_NOT_VALID);
4045
4046 // The buffer is now like a normal buffer, it cannot be easily
4047 // abandoned when changed.
4048 set_string_option_direct((char_u *)"buftype", -1,
4049 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004050}
4051
4052/*
4053 * Get the screen attribute for a position in the buffer.
4054 * Use a negative "col" to get the filler background color.
4055 */
4056 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004057term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004058{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004059 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004060 term_T *term = buf->b_term;
4061 sb_line_T *line;
4062 cellattr_T *cellattr;
4063
4064 if (lnum > term->tl_scrollback.ga_len)
4065 cellattr = &term->tl_default_color;
4066 else
4067 {
4068 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
4069 if (col < 0 || col >= line->sb_cols)
4070 cellattr = &line->sb_fill_attr;
4071 else
4072 cellattr = line->sb_cells + col;
4073 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004074 return cell2attr(term, wp, &cellattr->attrs, &cellattr->fg, &cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004075}
4076
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004077/*
4078 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02004079 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004080 */
4081 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004082cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004083{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004084 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
4085 if (rgb->index == 0)
4086 rgb->type = VTERM_COLOR_RGB;
4087 else
4088 {
4089 rgb->type = VTERM_COLOR_INDEXED;
4090 --rgb->index;
4091 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004092}
4093
4094/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004095 * Initialize vterm color from the synID.
4096 * Returns TRUE if color is set to "fg" and "bg".
4097 * Otherwise returns FALSE.
4098 */
4099 static int
4100get_vterm_color_from_synid(int id, VTermColor *fg, VTermColor *bg)
4101{
4102#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4103 // Use the actual color for the GUI and when 'termguicolors' is set.
4104 if (0
4105# ifdef FEAT_GUI
4106 || gui.in_use
4107# endif
4108# ifdef FEAT_TERMGUICOLORS
4109 || p_tgc
4110# ifdef FEAT_VTP
4111 // Finally get INVALCOLOR on this execution path
4112 || (!p_tgc && t_colors >= 256)
4113# endif
4114# endif
4115 )
4116 {
4117 guicolor_T fg_rgb = INVALCOLOR;
4118 guicolor_T bg_rgb = INVALCOLOR;
4119
4120 if (id > 0)
4121 syn_id2colors(id, &fg_rgb, &bg_rgb);
4122
4123 if (fg_rgb != INVALCOLOR)
4124 {
4125 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
4126 fg->red = (unsigned)(rgb >> 16);
4127 fg->green = (unsigned)(rgb >> 8) & 255;
4128 fg->blue = (unsigned)rgb & 255;
4129 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
4130 }
4131 else
4132 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4133
4134 if (bg_rgb != INVALCOLOR)
4135 {
4136 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
4137 bg->red = (unsigned)(rgb >> 16);
4138 bg->green = (unsigned)(rgb >> 8) & 255;
4139 bg->blue = (unsigned)rgb & 255;
4140 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
4141 }
4142 else
4143 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4144
4145 return TRUE;
4146 }
4147 else
4148#endif
4149 if (t_colors >= 16)
4150 {
4151 int cterm_fg = -1;
4152 int cterm_bg = -1;
4153
4154 if (id > 0)
4155 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
4156
4157 if (cterm_fg >= 0)
4158 {
4159 cterm_color2vterm(cterm_fg, fg);
4160 fg->type |= VTERM_COLOR_DEFAULT_FG;
4161 }
4162 else
4163 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4164
4165 if (cterm_bg >= 0)
4166 {
4167 cterm_color2vterm(cterm_bg, bg);
4168 bg->type |= VTERM_COLOR_DEFAULT_BG;
4169 }
4170 else
4171 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4172
4173 return TRUE;
4174 }
4175
4176 return FALSE;
4177}
4178
4179 void
4180term_reset_wincolor(win_T *wp)
4181{
4182 wp->w_term_wincolor.fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4183 wp->w_term_wincolor.bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4184}
4185
4186/*
4187 * Cache the color of 'wincolor'.
4188 */
4189 void
4190term_update_wincolor(win_T *wp)
4191{
4192 int id = 0;
4193
4194 if (*wp->w_p_wcr != NUL)
4195 id = syn_name2id(wp->w_p_wcr);
4196 if (id == 0 || !get_vterm_color_from_synid(id, &wp->w_term_wincolor.fg,
4197 &wp->w_term_wincolor.bg))
4198 term_reset_wincolor(wp);
4199}
4200
4201/*
4202 * Called when option 'termguicolors' was set,
4203 * or when any highlight is changed.
4204 */
4205 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004206term_update_wincolor_all(void)
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004207{
4208 win_T *wp = NULL;
4209 int did_curwin = FALSE;
4210
4211 while (for_all_windows_and_curwin(&wp, &did_curwin))
4212 term_update_wincolor(wp);
4213}
4214
4215/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004216 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004217 */
4218 static void
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004219init_default_colors(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004220{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004221 VTermColor *fg, *bg;
4222 int fgval, bgval;
4223 int id;
4224
Bram Moolenaara80faa82020-04-12 19:37:17 +02004225 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004226 term->tl_default_color.width = 1;
4227 fg = &term->tl_default_color.fg;
4228 bg = &term->tl_default_color.bg;
4229
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004230 // Vterm uses a default black background. Set it to white when
4231 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004232 if (*p_bg == 'l')
4233 {
4234 fgval = 0;
4235 bgval = 255;
4236 }
4237 else
4238 {
4239 fgval = 255;
4240 bgval = 0;
4241 }
4242 fg->red = fg->green = fg->blue = fgval;
4243 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004244 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
4245 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004246
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004247 // The highlight group overrules the defaults.
4248 id = term_get_highlight_id(term, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004249
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004250 if (!get_vterm_color_from_synid(id, fg, bg))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004251 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004252#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004253 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004254#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004255
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004256 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004257 if (cterm_normal_fg_color > 0)
4258 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004259 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004260# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4261# ifdef VIMDLL
4262 if (!gui.in_use)
4263# endif
4264 {
4265 tmp = fg->red;
4266 fg->red = fg->blue;
4267 fg->blue = tmp;
4268 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004269# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004270 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004271# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004272 else
4273 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004274# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004275
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004276 if (cterm_normal_bg_color > 0)
4277 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004278 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004279# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4280# ifdef VIMDLL
4281 if (!gui.in_use)
4282# endif
4283 {
4284 tmp = fg->red;
4285 fg->red = fg->blue;
4286 fg->blue = tmp;
4287 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004288# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004289 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004290# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004291 else
4292 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004293# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004294 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004295}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004296
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004297#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4298/*
Bram Moolenaar30b9a412022-05-26 14:06:37 +01004299 * Return TRUE if the user-defined palette (either g:terminal_ansi_colors or the
4300 * "ansi_colors" argument in term_start()) shall be applied.
4301 */
4302 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004303term_use_palette(void)
Bram Moolenaar30b9a412022-05-26 14:06:37 +01004304{
4305 if (0
4306#ifdef FEAT_GUI
4307 || gui.in_use
4308#endif
4309#ifdef FEAT_TERMGUICOLORS
4310 || p_tgc
4311#endif
4312 )
4313 return TRUE;
4314 return FALSE;
4315}
4316
4317/*
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004318 * Set the 16 ANSI colors from array of RGB values
4319 */
4320 static void
4321set_vterm_palette(VTerm *vterm, long_u *rgb)
4322{
4323 int index = 0;
4324 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004325
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004326 for (; index < 16; index++)
4327 {
4328 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004329
Millyb771b6b2021-11-23 12:07:25 +00004330 color.type = VTERM_COLOR_RGB;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004331 color.red = (unsigned)(rgb[index] >> 16);
4332 color.green = (unsigned)(rgb[index] >> 8) & 255;
4333 color.blue = (unsigned)rgb[index] & 255;
Bram Moolenaar68afde42022-02-25 20:48:26 +00004334 color.index = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004335 vterm_state_set_palette_color(state, index, &color);
4336 }
4337}
4338
4339/*
4340 * Set the ANSI color palette from a list of colors
4341 */
4342 static int
4343set_ansi_colors_list(VTerm *vterm, list_T *list)
4344{
4345 int n = 0;
4346 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004347 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004348
Bram Moolenaarb0992022020-01-30 14:55:42 +01004349 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004350 {
4351 char_u *color_name;
4352 guicolor_T guicolor;
4353
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004354 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004355 if (color_name == NULL)
4356 return FAIL;
4357
4358 guicolor = GUI_GET_COLOR(color_name);
4359 if (guicolor == INVALCOLOR)
4360 return FAIL;
4361
4362 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4363 }
4364
4365 if (n != 16 || li != NULL)
4366 return FAIL;
4367
4368 set_vterm_palette(vterm, rgb);
4369
4370 return OK;
4371}
4372
4373/*
4374 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4375 */
4376 static void
4377init_vterm_ansi_colors(VTerm *vterm)
4378{
4379 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4380
LemonBoyb2b3acb2022-05-20 10:10:34 +01004381 if (var == NULL)
4382 return;
4383
4384 if (var->di_tv.v_type != VAR_LIST
4385 || var->di_tv.vval.v_list == NULL
4386 || var->di_tv.vval.v_list->lv_first == &range_list_item
4387 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004388 semsg(_(e_invalid_argument_str), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004389}
4390#endif
4391
Bram Moolenaar52acb112018-03-18 19:20:22 +01004392/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004393 * Handles a "drop" command from the job in the terminal.
4394 * "item" is the file name, "item->li_next" may have options.
4395 */
4396 static void
4397handle_drop_command(listitem_T *item)
4398{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004399 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004400 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004401 int bufnr;
4402 win_T *wp;
4403 tabpage_T *tp;
4404 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004405 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004406
4407 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4408 FOR_ALL_TAB_WINDOWS(tp, wp)
4409 {
4410 if (wp->w_buffer->b_fnum == bufnr)
4411 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004412 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004413 goto_tabpage_win(tp, wp);
4414 return;
4415 }
4416 }
4417
Bram Moolenaara80faa82020-04-12 19:37:17 +02004418 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004419
4420 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4421 && opt_item->li_tv.vval.v_dict != NULL)
4422 {
4423 dict_T *dict = opt_item->li_tv.vval.v_dict;
4424 char_u *p;
4425
Bram Moolenaard61efa52022-07-23 09:52:04 +01004426 p = dict_get_string(dict, "ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004427 if (p == NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004428 p = dict_get_string(dict, "fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004429 if (p != NULL)
4430 {
4431 if (check_ff_value(p) == FAIL)
4432 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4433 else
4434 ea.force_ff = *p;
4435 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01004436 p = dict_get_string(dict, "enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004437 if (p == NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004438 p = dict_get_string(dict, "encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004439 if (p != NULL)
4440 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004441 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004442 if (ea.cmd != NULL)
4443 {
4444 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4445 ea.force_enc = 11;
4446 tofree = ea.cmd;
4447 }
4448 }
4449
Bram Moolenaard61efa52022-07-23 09:52:04 +01004450 p = dict_get_string(dict, "bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004451 if (p != NULL)
4452 get_bad_opt(p, &ea);
4453
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004454 if (dict_has_key(dict, "bin"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004455 ea.force_bin = FORCE_BIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004456 if (dict_has_key(dict, "binary"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004457 ea.force_bin = FORCE_BIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004458 if (dict_has_key(dict, "nobin"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004459 ea.force_bin = FORCE_NOBIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004460 if (dict_has_key(dict, "nobinary"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004461 ea.force_bin = FORCE_NOBIN;
4462 }
4463
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004464 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004465 if (ea.cmd == NULL)
4466 ea.cmd = (char_u *)"split";
4467 ea.arg = fname;
4468 ea.cmdidx = CMD_split;
4469 ex_splitview(&ea);
4470
4471 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004472}
4473
4474/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004475 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4476 */
4477 static int
4478is_permitted_term_api(char_u *func, char_u *pat)
4479{
4480 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4481}
4482
4483/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004484 * Handles a function call from the job running in a terminal.
4485 * "item" is the function name, "item->li_next" has the arguments.
4486 */
4487 static void
4488handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4489{
4490 char_u *func;
4491 typval_T argvars[2];
4492 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004493 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004494
4495 if (item->li_next == NULL)
4496 {
4497 ch_log(channel, "Missing function arguments for call");
4498 return;
4499 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004500 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004501
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004502 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004503 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004504 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004505 return;
4506 }
4507
4508 argvars[0].v_type = VAR_NUMBER;
4509 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4510 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004511 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004512 funcexe.fe_firstline = 1L;
4513 funcexe.fe_lastline = 1L;
4514 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004515 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004516 {
4517 clear_tv(&rettv);
4518 ch_log(channel, "Function %s called", func);
4519 }
4520 else
4521 ch_log(channel, "Calling function %s failed", func);
4522}
4523
4524/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004525 * URL decoding (also know as Percent-encoding).
4526 *
4527 * Note this function currently is only used for decoding shell's
4528 * OSC 7 escape sequence which we can assume all bytes are valid
4529 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4530 * encoding bytes like 0xfe, 0xff.
4531 */
4532 static size_t
4533url_decode(const char *src, const size_t len, char_u *dst)
4534{
4535 size_t i = 0, j = 0;
4536
4537 while (i < len)
4538 {
4539 if (src[i] == '%' && i + 2 < len)
4540 {
4541 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4542 j++;
4543 i += 3;
4544 }
4545 else
4546 {
4547 dst[j] = src[i];
4548 i++;
4549 j++;
4550 }
4551 }
4552 dst[j] = '\0';
4553 return j;
4554}
4555
4556/*
4557 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4558 *
4559 * The OSC 7 sequence has the format of
4560 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4561 * and what VTerm provides via VTermStringFragment is
4562 * "file://HOSTNAME/CURRENT/DIR"
4563 */
4564 static void
Bram Moolenaar474ad392022-08-21 11:37:17 +01004565sync_shell_dir(garray_T *gap)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004566{
Bram Moolenaar474ad392022-08-21 11:37:17 +01004567 int offset = 7; // len of "file://" is 7
4568 char *pos = (char *)gap->ga_data + offset;
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004569 char_u *new_dir;
4570
4571 // remove HOSTNAME to get PWD
Bram Moolenaar474ad392022-08-21 11:37:17 +01004572 while (offset < (int)gap->ga_len && *pos != '/' )
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004573 {
Bram Moolenaar474ad392022-08-21 11:37:17 +01004574 ++offset;
4575 ++pos;
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004576 }
4577
Bram Moolenaar474ad392022-08-21 11:37:17 +01004578 if (offset >= (int)gap->ga_len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004579 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004580 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
Bram Moolenaar474ad392022-08-21 11:37:17 +01004581 gap->ga_data);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004582 return;
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004583 }
4584
Bram Moolenaar474ad392022-08-21 11:37:17 +01004585 new_dir = alloc(gap->ga_len - offset + 1);
4586 url_decode(pos, gap->ga_len-offset, new_dir);
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004587 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4588 vim_free(new_dir);
4589}
4590
4591/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004592 * Called by libvterm when it cannot recognize an OSC sequence.
4593 * We recognize a terminal API command.
4594 */
4595 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004596parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004597{
4598 term_T *term = (term_T *)user;
4599 js_read_T reader;
4600 typval_T tv;
4601 channel_T *channel = term->tl_job == NULL ? NULL
4602 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004603 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004604
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004605 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
Bram Moolenaar474ad392022-08-21 11:37:17 +01004606 if (command != 51 && (command != 7 || !p_asd))
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004607 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004608
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004609 // Concatenate what was received until the final piece is found.
4610 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4611 {
4612 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004613 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004614 }
4615 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004616 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004617 if (!frag.final)
4618 return 1;
4619
4620 ((char *)gap->ga_data)[gap->ga_len] = 0;
Bram Moolenaar474ad392022-08-21 11:37:17 +01004621
4622 if (command == 7)
4623 {
4624 sync_shell_dir(gap);
4625 ga_clear(gap);
4626 return 1;
4627 }
4628
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004629 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004630 reader.js_fill = NULL;
4631 reader.js_used = 0;
4632 if (json_decode(&reader, &tv, 0) == OK
4633 && tv.v_type == VAR_LIST
4634 && tv.vval.v_list != NULL)
4635 {
4636 listitem_T *item = tv.vval.v_list->lv_first;
4637
4638 if (item == NULL)
4639 ch_log(channel, "Missing command");
4640 else
4641 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004642 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004643
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004644 // Make sure an invoked command doesn't delete the buffer (and the
4645 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004646 ++term->tl_buffer->b_locked;
4647
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004648 item = item->li_next;
4649 if (item == NULL)
4650 ch_log(channel, "Missing argument for %s", cmd);
4651 else if (STRCMP(cmd, "drop") == 0)
4652 handle_drop_command(item);
4653 else if (STRCMP(cmd, "call") == 0)
4654 handle_call_command(term, channel, item);
4655 else
4656 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004657 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004658 }
4659 }
4660 else
4661 ch_log(channel, "Invalid JSON received");
4662
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004663 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004664 clear_tv(&tv);
4665 return 1;
4666}
4667
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004668/*
4669 * Called by libvterm when it cannot recognize a CSI sequence.
4670 * We recognize the window position report.
4671 */
4672 static int
4673parse_csi(
4674 const char *leader UNUSED,
4675 const long args[],
4676 int argcount,
4677 const char *intermed UNUSED,
4678 char command,
4679 void *user)
4680{
4681 term_T *term = (term_T *)user;
4682 char buf[100];
4683 int len;
4684 int x = 0;
4685 int y = 0;
4686 win_T *wp;
4687
4688 // We recognize only CSI 13 t
4689 if (command != 't' || argcount != 1 || args[0] != 13)
4690 return 0; // not handled
4691
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004692 // When getting the window position is not possible or it fails it results
4693 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004694#if defined(FEAT_GUI) \
4695 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4696 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004697 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004698#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004699
4700 FOR_ALL_WINDOWS(wp)
4701 if (wp->w_buffer == term->tl_buffer)
4702 break;
4703 if (wp != NULL)
4704 {
4705#ifdef FEAT_GUI
4706 if (gui.in_use)
4707 {
4708 x += wp->w_wincol * gui.char_width;
4709 y += W_WINROW(wp) * gui.char_height;
4710 }
4711 else
4712#endif
4713 {
4714 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004715 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004716 x += wp->w_wincol * 7;
4717 y += W_WINROW(wp) * 10;
4718 }
4719 }
4720
4721 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4722 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4723 (char_u *)buf, len, NULL);
4724 return 1;
4725}
4726
Bram Moolenaard8637282020-05-20 18:41:41 +02004727static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004728 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004729 parse_csi, // csi
4730 parse_osc, // osc
Bram Moolenaar7da34152021-11-24 19:30:55 +00004731 NULL, // dcs
4732 NULL, // apc
4733 NULL, // pm
4734 NULL // sos
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004735};
4736
4737/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004738 * Use Vim's allocation functions for vterm so profiling works.
4739 */
4740 static void *
4741vterm_malloc(size_t size, void *data UNUSED)
4742{
Bram Moolenaar88137392021-11-12 16:01:15 +00004743 // make sure that the length is not zero
4744 return alloc_clear(size == 0 ? 1L : size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004745}
4746
4747 static void
4748vterm_memfree(void *ptr, void *data UNUSED)
4749{
4750 vim_free(ptr);
4751}
4752
4753static VTermAllocatorFunctions vterm_allocator = {
4754 &vterm_malloc,
4755 &vterm_memfree
4756};
4757
4758/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004759 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004760 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004761 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004762 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004763create_vterm(term_T *term, int rows, int cols)
4764{
4765 VTerm *vterm;
4766 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004767 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004768 VTermValue value;
4769
Bram Moolenaar756ef112018-04-10 12:04:27 +02004770 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004771 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004772 if (vterm == NULL)
4773 return FAIL;
4774
4775 // Allocate screen and state here, so we can bail out if that fails.
4776 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004777 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004778 if (state == NULL || screen == NULL)
4779 {
4780 vterm_free(vterm);
4781 return FAIL;
4782 }
4783
Bram Moolenaar52acb112018-03-18 19:20:22 +01004784 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004785 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004786 vterm_set_utf8(vterm, 1);
4787
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004788 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004789
4790 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004791 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004792 &term->tl_default_color.fg,
4793 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004794
Bram Moolenaar9e587872019-05-13 20:27:23 +02004795 if (t_colors < 16)
4796 // Less than 16 colors: assume that bold means using a bright color for
4797 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004798 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4799
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004800 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004801 vterm_screen_reset(screen, 1 /* hard */);
4802
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004803 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004804 vterm_screen_enable_altscreen(screen, 1);
4805
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004806 // For unix do not use a blinking cursor. In an xterm this causes the
4807 // cursor to blink if it's blinking in the xterm.
4808 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004809#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004810 if (GetCaretBlinkTime() == INFINITE)
4811 value.boolean = 0;
4812 else
4813 value.boolean = 1;
4814#else
4815 value.boolean = 0;
4816#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004817 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004818 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004819
4820 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004821}
4822
4823/*
LemonBoyb2b3acb2022-05-20 10:10:34 +01004824 * Reset the terminal palette to its default value.
4825 */
4826 static void
4827term_reset_palette(VTerm *vterm)
4828{
4829 VTermState *state = vterm_obtain_state(vterm);
4830 int index;
4831
4832 for (index = 0; index < 16; index++)
4833 {
4834 VTermColor color;
4835
4836 color.type = VTERM_COLOR_INDEXED;
4837 ansi_color2rgb(index, &color.red, &color.green, &color.blue,
4838 &color.index);
4839 // The first valid index starts at 1.
4840 color.index -= 1;
4841
4842 vterm_state_set_palette_color(state, index, &color);
4843 }
4844}
4845
4846 static void
4847term_update_palette(term_T *term)
4848{
Bram Moolenaar30b9a412022-05-26 14:06:37 +01004849#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
LemonBoyb2b3acb2022-05-20 10:10:34 +01004850 if (term_use_palette()
4851 && (term->tl_palette != NULL
4852 || find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE)
Bram Moolenaar30b9a412022-05-26 14:06:37 +01004853 != NULL))
LemonBoyb2b3acb2022-05-20 10:10:34 +01004854 {
4855 if (term->tl_palette != NULL)
4856 set_vterm_palette(term->tl_vterm, term->tl_palette);
4857 else
4858 init_vterm_ansi_colors(term->tl_vterm);
4859 }
4860 else
Bram Moolenaar30b9a412022-05-26 14:06:37 +01004861#endif
LemonBoyb2b3acb2022-05-20 10:10:34 +01004862 term_reset_palette(term->tl_vterm);
4863}
4864
4865/*
4866 * Called when option 'termguicolors' is changed.
4867 */
4868 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004869term_update_palette_all(void)
LemonBoyb2b3acb2022-05-20 10:10:34 +01004870{
4871 term_T *term;
4872
4873 FOR_ALL_TERMS(term)
4874 {
4875 if (term->tl_vterm == NULL)
4876 continue;
4877 term_update_palette(term);
4878 }
4879}
4880
4881/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004882 * Called when option 'background' or 'termguicolors' was set,
4883 * or when any highlight is changed.
Bram Moolenaarad431992021-05-03 20:40:38 +02004884 */
4885 void
4886term_update_colors_all(void)
4887{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004888 term_T *term;
Bram Moolenaarad431992021-05-03 20:40:38 +02004889
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004890 FOR_ALL_TERMS(term)
4891 {
4892 if (term->tl_vterm == NULL)
4893 continue;
4894 init_default_colors(term);
4895 vterm_state_set_default_colors(
4896 vterm_obtain_state(term->tl_vterm),
4897 &term->tl_default_color.fg,
4898 &term->tl_default_color.bg);
4899 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004900}
4901
4902/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004903 * Return the text to show for the buffer name and status.
4904 */
4905 char_u *
4906term_get_status_text(term_T *term)
4907{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004908 if (term->tl_status_text != NULL)
4909 return term->tl_status_text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004910
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004911 char_u *txt;
4912 size_t len;
4913 char_u *fname;
4914
4915 if (term->tl_normal_mode)
4916 {
4917 if (term_job_running(term))
4918 txt = (char_u *)_("Terminal");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004919 else
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004920 txt = (char_u *)_("Terminal-finished");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004921 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004922 else if (term->tl_title != NULL)
4923 txt = term->tl_title;
4924 else if (term_none_open(term))
4925 txt = (char_u *)_("active");
4926 else if (term_job_running(term))
4927 txt = (char_u *)_("running");
4928 else
4929 txt = (char_u *)_("finished");
4930 fname = buf_get_fname(term->tl_buffer);
4931 len = 9 + STRLEN(fname) + STRLEN(txt);
4932 term->tl_status_text = alloc(len);
4933 if (term->tl_status_text != NULL)
4934 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
4935 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004936 return term->tl_status_text;
4937}
4938
4939/*
Bram Moolenaar3ad69532021-11-19 17:01:08 +00004940 * Clear the cached value of the status text.
4941 */
4942 void
4943term_clear_status_text(term_T *term)
4944{
4945 VIM_CLEAR(term->tl_status_text);
4946}
4947
4948/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004949 * Mark references in jobs of terminals.
4950 */
4951 int
4952set_ref_in_term(int copyID)
4953{
4954 int abort = FALSE;
4955 term_T *term;
4956 typval_T tv;
4957
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004958 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004959 if (term->tl_job != NULL)
4960 {
4961 tv.v_type = VAR_JOB;
4962 tv.vval.v_job = term->tl_job;
4963 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4964 }
4965 return abort;
4966}
4967
4968/*
4969 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004970 * Returns NULL when the buffer is not for a terminal window and logs a message
4971 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004972 */
4973 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004974term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004975{
4976 buf_T *buf;
4977
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004978 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004979 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004980 --emsg_off;
4981 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004982 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004983 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004984 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004985 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004986 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004987 return buf;
4988}
4989
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004990 static void
4991clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004992{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004993 CLEAR_FIELD(*cell);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004994 cell->fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4995 cell->bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004996}
4997
4998 static void
4999dump_term_color(FILE *fd, VTermColor *color)
5000{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005001 int index;
5002
5003 if (VTERM_COLOR_IS_INDEXED(color))
5004 index = color->index + 1;
5005 else if (color->type == 0)
5006 // use RGB values
5007 index = 255;
5008 else
5009 // default color
5010 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005011 fprintf(fd, "%02x%02x%02x%d",
5012 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005013 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005014}
5015
5016/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005017 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01005018 *
5019 * Each screen cell in full is:
5020 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
5021 * {characters} is a space for an empty cell
5022 * For a double-width character "+" is changed to "*" and the next cell is
5023 * skipped.
5024 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
5025 * when "&" use the same as the previous cell.
5026 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
5027 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
5028 * {color-idx} is a number from 0 to 255
5029 *
5030 * Screen cell with same width, attributes and color as the previous one:
5031 * |{characters}
5032 *
5033 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
5034 *
5035 * Repeating the previous screen cell:
5036 * @{count}
5037 */
5038 void
5039f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
5040{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005041 buf_T *buf;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005042 term_T *term;
5043 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005044 int max_height = 0;
5045 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005046 stat_T st;
5047 FILE *fd;
5048 VTermPos pos;
5049 VTermScreen *screen;
5050 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005051 VTermState *state;
5052 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005053
5054 if (check_restricted() || check_secure())
5055 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005056
5057 if (in_vim9script()
5058 && (check_for_buffer_arg(argvars, 0) == FAIL
5059 || check_for_string_arg(argvars, 1) == FAIL
5060 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5061 return;
5062
5063 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01005064 if (buf == NULL)
5065 return;
5066 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02005067 if (term->tl_vterm == NULL)
5068 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005069 emsg(_(e_job_already_finished));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02005070 return;
5071 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005072
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005073 if (argvars[2].v_type != VAR_UNKNOWN)
5074 {
5075 dict_T *d;
5076
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01005077 if (check_for_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005078 return;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005079 d = argvars[2].vval.v_dict;
5080 if (d != NULL)
5081 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01005082 max_height = dict_get_number(d, "rows");
5083 max_width = dict_get_number(d, "columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005084 }
5085 }
5086
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005087 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005088 if (fname == NULL)
5089 return;
5090 if (mch_stat((char *)fname, &st) >= 0)
5091 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005092 semsg(_(e_file_exists_str), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005093 return;
5094 }
5095
Bram Moolenaard96ff162018-02-18 22:13:29 +01005096 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
5097 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005098 semsg(_(e_cant_create_file_str), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005099 return;
5100 }
5101
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005102 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005103
5104 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005105 state = vterm_obtain_state(term->tl_vterm);
5106 vterm_state_get_cursorpos(state, &cursor_pos);
5107
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005108 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
5109 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005110 {
5111 int repeat = 0;
5112
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005113 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
5114 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005115 {
5116 VTermScreenCell cell;
5117 int same_attr;
5118 int same_chars = TRUE;
5119 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005120 int is_cursor_pos = (pos.col == cursor_pos.col
5121 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005122
5123 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005124 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005125
5126 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
5127 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01005128 int c = cell.chars[i];
5129 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02005130 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01005131
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005132 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01005133 if (i == 0)
5134 {
5135 c = (c == NUL) ? ' ' : c;
5136 pc = (pc == NUL) ? ' ' : pc;
5137 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02005138 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005139 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02005140 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005141 break;
5142 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005143 same_attr = vtermAttr2hl(&cell.attrs)
5144 == vtermAttr2hl(&prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005145 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
5146 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005147 if (same_chars && cell.width == prev_cell.width && same_attr
5148 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005149 {
5150 ++repeat;
5151 }
5152 else
5153 {
5154 if (repeat > 0)
5155 {
5156 fprintf(fd, "@%d", repeat);
5157 repeat = 0;
5158 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005159 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005160
5161 if (cell.chars[0] == NUL)
5162 fputs(" ", fd);
5163 else
5164 {
5165 char_u charbuf[10];
5166 int len;
5167
5168 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
5169 && cell.chars[i] != NUL; ++i)
5170 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02005171 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005172 fwrite(charbuf, len, 1, fd);
5173 }
5174 }
5175
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005176 // When only the characters differ we don't write anything, the
5177 // following "|", "@" or NL will indicate using the same
5178 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005179 if (cell.width != prev_cell.width || !same_attr)
5180 {
5181 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005182 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005183 else
5184 fputs("+", fd);
5185
5186 if (same_attr)
5187 {
5188 fputs("&", fd);
5189 }
5190 else
5191 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005192 fprintf(fd, "%d", vtermAttr2hl(&cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005193 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005194 fputs("&", fd);
5195 else
5196 {
5197 fputs("#", fd);
5198 dump_term_color(fd, &cell.fg);
5199 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005200 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005201 fputs("&", fd);
5202 else
5203 {
5204 fputs("#", fd);
5205 dump_term_color(fd, &cell.bg);
5206 }
5207 }
5208 }
5209
5210 prev_cell = cell;
5211 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005212
5213 if (cell.width == 2)
5214 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005215 }
5216 if (repeat > 0)
5217 fprintf(fd, "@%d", repeat);
5218 fputs("\n", fd);
5219 }
5220
5221 fclose(fd);
5222}
5223
5224/*
5225 * Called when a dump is corrupted. Put a breakpoint here when debugging.
5226 */
5227 static void
5228dump_is_corrupt(garray_T *gap)
5229{
5230 ga_concat(gap, (char_u *)"CORRUPT");
5231}
5232
5233 static void
5234append_cell(garray_T *gap, cellattr_T *cell)
5235{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00005236 if (ga_grow(gap, 1) == FAIL)
5237 return;
5238
5239 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
5240 ++gap->ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005241}
5242
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005243 static void
5244clear_cellattr(cellattr_T *cell)
5245{
5246 CLEAR_FIELD(*cell);
5247 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
5248 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
5249}
5250
Bram Moolenaard96ff162018-02-18 22:13:29 +01005251/*
5252 * Read the dump file from "fd" and append lines to the current buffer.
5253 * Return the cell width of the longest line.
5254 */
5255 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01005256read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005257{
5258 int c;
5259 garray_T ga_text;
5260 garray_T ga_cell;
5261 char_u *prev_char = NULL;
5262 int attr = 0;
5263 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005264 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005265 term_T *term = curbuf->b_term;
5266 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005267 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005268
5269 ga_init2(&ga_text, 1, 90);
5270 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005271 clear_cellattr(&cell);
5272 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005273 cursor_pos->row = -1;
5274 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005275
5276 c = fgetc(fd);
5277 for (;;)
5278 {
5279 if (c == EOF)
5280 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02005281 if (c == '\r')
5282 {
5283 // DOS line endings? Ignore.
5284 c = fgetc(fd);
5285 }
5286 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005287 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005288 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005289 if (ga_text.ga_data == NULL)
5290 dump_is_corrupt(&ga_text);
5291 if (ga_grow(&term->tl_scrollback, 1) == OK)
5292 {
5293 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
5294 + term->tl_scrollback.ga_len;
5295
5296 if (max_cells < ga_cell.ga_len)
5297 max_cells = ga_cell.ga_len;
5298 line->sb_cols = ga_cell.ga_len;
5299 line->sb_cells = ga_cell.ga_data;
5300 line->sb_fill_attr = term->tl_default_color;
5301 ++term->tl_scrollback.ga_len;
5302 ga_init(&ga_cell);
5303
5304 ga_append(&ga_text, NUL);
5305 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5306 ga_text.ga_len, FALSE);
5307 }
5308 else
5309 ga_clear(&ga_cell);
5310 ga_text.ga_len = 0;
5311
5312 c = fgetc(fd);
5313 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005314 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005315 {
5316 int prev_len = ga_text.ga_len;
5317
Bram Moolenaar9271d052018-02-25 21:39:46 +01005318 if (c == '>')
5319 {
5320 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005321 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005322 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5323 cursor_pos->col = ga_cell.ga_len;
5324 }
5325
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005326 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005327 c = fgetc(fd);
5328 if (c != EOF)
5329 ga_append(&ga_text, c);
5330 for (;;)
5331 {
5332 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005333 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005334 || c == EOF || c == '\n')
5335 break;
5336 ga_append(&ga_text, c);
5337 }
5338
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005339 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005340 vim_free(prev_char);
5341 if (ga_text.ga_data != NULL)
5342 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5343 ga_text.ga_len - prev_len);
5344
Bram Moolenaar9271d052018-02-25 21:39:46 +01005345 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005346 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005347 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005348 }
5349 else if (c == '+' || c == '*')
5350 {
5351 int is_bg;
5352
5353 cell.width = c == '+' ? 1 : 2;
5354
5355 c = fgetc(fd);
5356 if (c == '&')
5357 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005358 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005359 c = fgetc(fd);
5360 }
5361 else if (isdigit(c))
5362 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005363 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005364 attr = 0;
5365 while (isdigit(c))
5366 {
5367 attr = attr * 10 + (c - '0');
5368 c = fgetc(fd);
5369 }
5370 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005371
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005372 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005373 for (is_bg = 0; is_bg <= 1; ++is_bg)
5374 {
5375 if (c == '&')
5376 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005377 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005378 c = fgetc(fd);
5379 }
5380 else if (c == '#')
5381 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005382 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005383
5384 c = fgetc(fd);
5385 red = hex2nr(c);
5386 c = fgetc(fd);
5387 red = (red << 4) + hex2nr(c);
5388 c = fgetc(fd);
5389 green = hex2nr(c);
5390 c = fgetc(fd);
5391 green = (green << 4) + hex2nr(c);
5392 c = fgetc(fd);
5393 blue = hex2nr(c);
5394 c = fgetc(fd);
5395 blue = (blue << 4) + hex2nr(c);
5396 c = fgetc(fd);
5397 if (!isdigit(c))
5398 dump_is_corrupt(&ga_text);
5399 while (isdigit(c))
5400 {
5401 index = index * 10 + (c - '0');
5402 c = fgetc(fd);
5403 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005404 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005405 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005406 type = VTERM_COLOR_RGB;
5407 if (index == 0)
5408 {
5409 if (is_bg)
5410 type |= VTERM_COLOR_DEFAULT_BG;
5411 else
5412 type |= VTERM_COLOR_DEFAULT_FG;
5413 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005414 }
5415 else
5416 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005417 type = VTERM_COLOR_INDEXED;
5418 index -= 1;
5419 }
5420 if (is_bg)
5421 {
5422 cell.bg.type = type;
5423 cell.bg.red = red;
5424 cell.bg.green = green;
5425 cell.bg.blue = blue;
5426 cell.bg.index = index;
5427 }
5428 else
5429 {
5430 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005431 cell.fg.red = red;
5432 cell.fg.green = green;
5433 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005434 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005435 }
5436 }
5437 else
5438 dump_is_corrupt(&ga_text);
5439 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005440 }
5441 else
5442 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005443 }
5444 else
5445 dump_is_corrupt(&ga_text);
5446
5447 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005448 if (cell.width == 2)
5449 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005450 }
5451 else if (c == '@')
5452 {
5453 if (prev_char == NULL)
5454 dump_is_corrupt(&ga_text);
5455 else
5456 {
5457 int count = 0;
5458
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005459 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005460 for (;;)
5461 {
5462 c = fgetc(fd);
5463 if (!isdigit(c))
5464 break;
5465 count = count * 10 + (c - '0');
5466 }
5467
5468 while (count-- > 0)
5469 {
5470 ga_concat(&ga_text, prev_char);
5471 append_cell(&ga_cell, &cell);
5472 }
5473 }
5474 }
5475 else
5476 {
5477 dump_is_corrupt(&ga_text);
5478 c = fgetc(fd);
5479 }
5480 }
5481
5482 if (ga_text.ga_len > 0)
5483 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005484 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005485 dump_is_corrupt(&ga_text);
5486 ga_append(&ga_text, NUL);
5487 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5488 ga_text.ga_len, FALSE);
5489 }
5490
5491 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005492 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005493 vim_free(prev_char);
5494
5495 return max_cells;
5496}
5497
5498/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005499 * Return an allocated string with at least "text_width" "=" characters and
5500 * "fname" inserted in the middle.
5501 */
5502 static char_u *
5503get_separator(int text_width, char_u *fname)
5504{
5505 int width = MAX(text_width, curwin->w_width);
5506 char_u *textline;
5507 int fname_size;
5508 char_u *p = fname;
5509 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005510 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005511
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005512 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005513 if (textline == NULL)
5514 return NULL;
5515
5516 fname_size = vim_strsize(fname);
5517 if (fname_size < width - 8)
5518 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005519 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005520 width = MAX(text_width, fname_size + 8);
5521 }
5522 else if (fname_size > width - 8)
5523 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005524 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005525 p = gettail(fname);
5526 fname_size = vim_strsize(p);
5527 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005528 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005529 while (fname_size > width - 8)
5530 {
5531 p += (*mb_ptr2len)(p);
5532 fname_size = vim_strsize(p);
5533 }
5534
5535 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5536 textline[i] = '=';
5537 textline[i++] = ' ';
5538
5539 STRCPY(textline + i, p);
5540 off = STRLEN(textline);
5541 textline[off] = ' ';
5542 for (i = 1; i < (width - fname_size) / 2; ++i)
5543 textline[off + i] = '=';
5544 textline[off + i] = NUL;
5545
5546 return textline;
5547}
5548
5549/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005550 * Common for "term_dumpdiff()" and "term_dumpload()".
5551 */
5552 static void
5553term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5554{
5555 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005556 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005557 char_u buf1[NUMBUFLEN];
5558 char_u buf2[NUMBUFLEN];
5559 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005560 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005561 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005562 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005563 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005564 char_u *textline = NULL;
5565
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005566 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005567 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005568 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005569 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005570 if (fname1 == NULL || (do_diff && fname2 == NULL))
5571 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005572 emsg(_(e_invalid_argument));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005573 return;
5574 }
5575 fd1 = mch_fopen((char *)fname1, READBIN);
5576 if (fd1 == NULL)
5577 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005578 semsg(_(e_cant_read_file_str), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005579 return;
5580 }
5581 if (do_diff)
5582 {
5583 fd2 = mch_fopen((char *)fname2, READBIN);
5584 if (fd2 == NULL)
5585 {
5586 fclose(fd1);
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005587 semsg(_(e_cant_read_file_str), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005588 return;
5589 }
5590 }
5591
5592 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005593 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5594 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5595 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5596 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5597 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005598
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005599 if (opt.jo_term_name == NULL)
5600 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005601 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005602
Bram Moolenaar51e14382019-05-25 20:21:28 +02005603 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005604 if (fname_tofree != NULL)
5605 {
5606 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5607 opt.jo_term_name = fname_tofree;
5608 }
5609 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005610
Bram Moolenaar87abab92019-06-03 21:14:59 +02005611 if (opt.jo_bufnr_buf != NULL)
5612 {
5613 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5614
5615 // With "bufnr" argument: enter the window with this buffer and make it
5616 // empty.
5617 if (wp == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005618 semsg(_(e_invalid_argument_str), "bufnr");
Bram Moolenaar87abab92019-06-03 21:14:59 +02005619 else
5620 {
5621 buf = curbuf;
5622 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005623 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005624 free_scrollback(curbuf->b_term);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005625 redraw_later(UPD_NOT_VALID);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005626 }
5627 }
5628 else
5629 // Create a new terminal window.
5630 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5631
Bram Moolenaard96ff162018-02-18 22:13:29 +01005632 if (buf != NULL && buf->b_term != NULL)
5633 {
5634 int i;
5635 linenr_T bot_lnum;
5636 linenr_T lnum;
5637 term_T *term = buf->b_term;
5638 int width;
5639 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005640 VTermPos cursor_pos1;
5641 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005642
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005643 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005644
Bram Moolenaard96ff162018-02-18 22:13:29 +01005645 rettv->vval.v_number = buf->b_fnum;
5646
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005647 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005648 width = read_dump_file(fd1, &cursor_pos1);
5649
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005650 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005651 if (cursor_pos1.row >= 0)
5652 {
5653 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5654 coladvance(cursor_pos1.col);
5655 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005656
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005657 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005658 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005659
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005660 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005661 if (!do_diff)
5662 goto theend;
5663
5664 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5665
Bram Moolenaar4a696342018-04-05 18:45:26 +02005666 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005667 if (textline == NULL)
5668 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005669 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5670 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5671 vim_free(textline);
5672
5673 textline = get_separator(width, fname2);
5674 if (textline == NULL)
5675 goto theend;
5676 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5677 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005678 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005679
5680 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005681 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005682 if (width2 > width)
5683 {
5684 vim_free(textline);
5685 textline = alloc(width2 + 1);
5686 if (textline == NULL)
5687 goto theend;
5688 width = width2;
5689 textline[width] = NUL;
5690 }
5691 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5692
5693 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5694 {
5695 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5696 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005697 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005698 for (i = 0; i < width; ++i)
5699 textline[i] = '-';
5700 }
5701 else
5702 {
5703 char_u *line1;
5704 char_u *line2;
5705 char_u *p1;
5706 char_u *p2;
5707 int col;
5708 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5709 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5710 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5711 ->sb_cells;
5712
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005713 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005714 line1 = vim_strsave(ml_get(lnum));
5715 if (line1 == NULL)
5716 break;
5717 p1 = line1;
5718
5719 line2 = ml_get(lnum + bot_lnum);
5720 p2 = line2;
5721 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5722 {
5723 int len1 = utfc_ptr2len(p1);
5724 int len2 = utfc_ptr2len(p2);
5725
5726 textline[col] = ' ';
5727 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005728 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005729 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005730 else if (lnum == cursor_pos1.row + 1
5731 && col == cursor_pos1.col
5732 && (cursor_pos1.row != cursor_pos2.row
5733 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005734 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005735 textline[col] = '>';
5736 else if (lnum == cursor_pos2.row + 1
5737 && col == cursor_pos2.col
5738 && (cursor_pos1.row != cursor_pos2.row
5739 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005740 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005741 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005742 else if (cellattr1 != NULL && cellattr2 != NULL)
5743 {
5744 if ((cellattr1 + col)->width
5745 != (cellattr2 + col)->width)
5746 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005747 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005748 &(cellattr2 + col)->fg))
5749 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005750 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005751 &(cellattr2 + col)->bg))
5752 textline[col] = 'b';
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005753 else if (vtermAttr2hl(&(cellattr1 + col)->attrs)
5754 != vtermAttr2hl(&((cellattr2 + col)->attrs)))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005755 textline[col] = 'a';
5756 }
5757 p1 += len1;
5758 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005759 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005760 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005761
5762 while (col < width)
5763 {
5764 if (*p1 == NUL && *p2 == NUL)
5765 textline[col] = '?';
5766 else if (*p1 == NUL)
5767 {
5768 textline[col] = '+';
5769 p2 += utfc_ptr2len(p2);
5770 }
5771 else
5772 {
5773 textline[col] = '-';
5774 p1 += utfc_ptr2len(p1);
5775 }
5776 ++col;
5777 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005778
5779 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005780 }
5781 if (add_empty_scrollback(term, &term->tl_default_color,
5782 term->tl_top_diff_rows) == OK)
5783 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5784 ++bot_lnum;
5785 }
5786
5787 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5788 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005789 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005790 for (i = 0; i < width; ++i)
5791 textline[i] = '+';
5792 if (add_empty_scrollback(term, &term->tl_default_color,
5793 term->tl_top_diff_rows) == OK)
5794 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5795 ++lnum;
5796 ++bot_lnum;
5797 }
5798
5799 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005800
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005801 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005802 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005803 }
5804
5805theend:
5806 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005807 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005808 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005809 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005810 fclose(fd2);
5811}
5812
5813/*
5814 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5815 * bottom files.
5816 * Return FAIL when this is not possible.
5817 */
5818 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00005819term_swap_diff(void)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005820{
5821 term_T *term = curbuf->b_term;
5822 linenr_T line_count;
5823 linenr_T top_rows;
5824 linenr_T bot_rows;
5825 linenr_T bot_start;
5826 linenr_T lnum;
5827 char_u *p;
5828 sb_line_T *sb_line;
5829
5830 if (term == NULL
5831 || !term_is_finished(curbuf)
5832 || term->tl_top_diff_rows == 0
5833 || term->tl_scrollback.ga_len == 0)
5834 return FAIL;
5835
5836 line_count = curbuf->b_ml.ml_line_count;
5837 top_rows = term->tl_top_diff_rows;
5838 bot_rows = term->tl_bot_diff_rows;
5839 bot_start = line_count - bot_rows;
5840 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5841
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005842 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005843 for (lnum = 1; lnum <= top_rows; ++lnum)
5844 {
5845 p = vim_strsave(ml_get(1));
5846 if (p == NULL)
5847 return OK;
5848 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005849 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005850 vim_free(p);
5851 }
5852
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005853 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005854 for (lnum = 1; lnum <= bot_rows; ++lnum)
5855 {
5856 p = vim_strsave(ml_get(bot_start + lnum));
5857 if (p == NULL)
5858 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005859 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005860 ml_append(lnum - 1, p, 0, FALSE);
5861 vim_free(p);
5862 }
5863
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005864 // move top title to bottom
5865 p = vim_strsave(ml_get(bot_rows + 1));
5866 if (p == NULL)
5867 return OK;
5868 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005869 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005870 vim_free(p);
5871
5872 // move bottom title to top
5873 p = vim_strsave(ml_get(line_count - top_rows));
5874 if (p == NULL)
5875 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005876 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005877 ml_append(bot_rows, p, 0, FALSE);
5878 vim_free(p);
5879
Bram Moolenaard96ff162018-02-18 22:13:29 +01005880 if (top_rows == bot_rows)
5881 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005882 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005883 for (lnum = 0; lnum < top_rows; ++lnum)
5884 {
5885 sb_line_T temp;
5886
5887 temp = *(sb_line + lnum);
5888 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5889 *(sb_line + bot_start + lnum) = temp;
5890 }
5891 }
5892 else
5893 {
5894 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005895 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005896
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005897 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005898 if (temp != NULL)
5899 {
5900 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5901 mch_memmove(term->tl_scrollback.ga_data,
5902 temp + bot_start,
5903 sizeof(sb_line_T) * bot_rows);
5904 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5905 temp + top_rows,
5906 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5907 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5908 + line_count - top_rows,
5909 temp,
5910 sizeof(sb_line_T) * top_rows);
5911 vim_free(temp);
5912 }
5913 }
5914
5915 term->tl_top_diff_rows = bot_rows;
5916 term->tl_bot_diff_rows = top_rows;
5917
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005918 update_screen(UPD_NOT_VALID);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005919 return OK;
5920}
5921
5922/*
5923 * "term_dumpdiff(filename, filename, options)" function
5924 */
5925 void
5926f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5927{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005928 if (in_vim9script()
5929 && (check_for_string_arg(argvars, 0) == FAIL
5930 || check_for_string_arg(argvars, 1) == FAIL
5931 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5932 return;
5933
Bram Moolenaard96ff162018-02-18 22:13:29 +01005934 term_load_dump(argvars, rettv, TRUE);
5935}
5936
5937/*
5938 * "term_dumpload(filename, options)" function
5939 */
5940 void
5941f_term_dumpload(typval_T *argvars, typval_T *rettv)
5942{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005943 if (in_vim9script()
5944 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005945 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005946 return;
5947
Bram Moolenaard96ff162018-02-18 22:13:29 +01005948 term_load_dump(argvars, rettv, FALSE);
5949}
5950
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005951/*
5952 * "term_getaltscreen(buf)" function
5953 */
5954 void
5955f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5956{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005957 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005958
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005959 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5960 return;
5961
5962 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005963 if (buf == NULL)
5964 return;
5965 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5966}
5967
5968/*
5969 * "term_getattr(attr, name)" function
5970 */
5971 void
5972f_term_getattr(typval_T *argvars, typval_T *rettv)
5973{
5974 int attr;
5975 size_t i;
5976 char_u *name;
5977
5978 static struct {
5979 char *name;
5980 int attr;
5981 } attrs[] = {
5982 {"bold", HL_BOLD},
5983 {"italic", HL_ITALIC},
5984 {"underline", HL_UNDERLINE},
5985 {"strike", HL_STRIKETHROUGH},
5986 {"reverse", HL_INVERSE},
5987 };
5988
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005989 if (in_vim9script()
5990 && (check_for_number_arg(argvars, 0) == FAIL
5991 || check_for_string_arg(argvars, 1) == FAIL))
5992 return;
5993
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005994 attr = tv_get_number(&argvars[0]);
5995 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005996 if (name == NULL)
5997 return;
5998
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005999 if (attr > HL_ALL)
6000 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02006001 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006002 if (STRCMP(name, attrs[i].name) == 0)
6003 {
6004 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
6005 break;
6006 }
6007}
6008
6009/*
6010 * "term_getcursor(buf)" function
6011 */
6012 void
6013f_term_getcursor(typval_T *argvars, typval_T *rettv)
6014{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006015 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006016 term_T *term;
6017 list_T *l;
6018 dict_T *d;
6019
6020 if (rettv_list_alloc(rettv) == FAIL)
6021 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006022
6023 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6024 return;
6025
6026 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006027 if (buf == NULL)
6028 return;
6029 term = buf->b_term;
6030
6031 l = rettv->vval.v_list;
6032 list_append_number(l, term->tl_cursor_pos.row + 1);
6033 list_append_number(l, term->tl_cursor_pos.col + 1);
6034
6035 d = dict_alloc();
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00006036 if (d == NULL)
6037 return;
6038
6039 dict_add_number(d, "visible", term->tl_cursor_visible);
6040 dict_add_number(d, "blink", blink_state_is_inverted()
6041 ? !term->tl_cursor_blink : term->tl_cursor_blink);
6042 dict_add_number(d, "shape", term->tl_cursor_shape);
6043 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
6044 list_append_dict(l, d);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006045}
6046
6047/*
6048 * "term_getjob(buf)" function
6049 */
6050 void
6051f_term_getjob(typval_T *argvars, typval_T *rettv)
6052{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006053 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006054
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006055 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6056 return;
6057
6058 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006059 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01006060 {
6061 rettv->v_type = VAR_SPECIAL;
6062 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006063 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01006064 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006065
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01006066 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006067 rettv->vval.v_job = buf->b_term->tl_job;
6068 if (rettv->vval.v_job != NULL)
6069 ++rettv->vval.v_job->jv_refcount;
6070}
6071
6072 static int
6073get_row_number(typval_T *tv, term_T *term)
6074{
6075 if (tv->v_type == VAR_STRING
6076 && tv->vval.v_string != NULL
6077 && STRCMP(tv->vval.v_string, ".") == 0)
6078 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006079 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006080}
6081
6082/*
6083 * "term_getline(buf, row)" function
6084 */
6085 void
6086f_term_getline(typval_T *argvars, typval_T *rettv)
6087{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006088 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006089 term_T *term;
6090 int row;
6091
6092 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006093
6094 if (in_vim9script()
6095 && (check_for_buffer_arg(argvars, 0) == FAIL
6096 || check_for_lnum_arg(argvars, 1) == FAIL))
6097 return;
6098
6099 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006100 if (buf == NULL)
6101 return;
6102 term = buf->b_term;
6103 row = get_row_number(&argvars[1], term);
6104
6105 if (term->tl_vterm == NULL)
6106 {
6107 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
6108
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006109 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006110 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
6111 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
6112 }
6113 else
6114 {
6115 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
6116 VTermRect rect;
6117 int len;
6118 char_u *p;
6119
6120 if (row < 0 || row >= term->tl_rows)
6121 return;
6122 len = term->tl_cols * MB_MAXBYTES + 1;
6123 p = alloc(len);
6124 if (p == NULL)
6125 return;
6126 rettv->vval.v_string = p;
6127
6128 rect.start_col = 0;
6129 rect.end_col = term->tl_cols;
6130 rect.start_row = row;
6131 rect.end_row = row + 1;
6132 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
6133 }
6134}
6135
6136/*
6137 * "term_getscrolled(buf)" function
6138 */
6139 void
6140f_term_getscrolled(typval_T *argvars, typval_T *rettv)
6141{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006142 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006143
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006144 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6145 return;
6146
6147 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006148 if (buf == NULL)
6149 return;
6150 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
6151}
6152
6153/*
6154 * "term_getsize(buf)" function
6155 */
6156 void
6157f_term_getsize(typval_T *argvars, typval_T *rettv)
6158{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006159 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006160 list_T *l;
6161
6162 if (rettv_list_alloc(rettv) == FAIL)
6163 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006164
6165 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6166 return;
6167
6168 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006169 if (buf == NULL)
6170 return;
6171
6172 l = rettv->vval.v_list;
6173 list_append_number(l, buf->b_term->tl_rows);
6174 list_append_number(l, buf->b_term->tl_cols);
6175}
6176
6177/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02006178 * "term_setsize(buf, rows, cols)" function
6179 */
6180 void
6181f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6182{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006183 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02006184 term_T *term;
6185 varnumber_T rows, cols;
6186
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006187 if (in_vim9script()
6188 && (check_for_buffer_arg(argvars, 0) == FAIL
6189 || check_for_number_arg(argvars, 1) == FAIL
6190 || check_for_number_arg(argvars, 2) == FAIL))
6191 return;
6192
6193 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02006194 if (buf == NULL)
6195 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006196 emsg(_(e_not_terminal_buffer));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02006197 return;
6198 }
6199 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02006200 return;
6201 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006202 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02006203 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006204 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02006205 cols = cols <= 0 ? term->tl_cols : cols;
6206 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006207 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02006208
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006209 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02006210 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
6211 term_report_winsize(term, term->tl_rows, term->tl_cols);
6212}
6213
6214/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006215 * "term_getstatus(buf)" function
6216 */
6217 void
6218f_term_getstatus(typval_T *argvars, typval_T *rettv)
6219{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006220 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006221 term_T *term;
6222 char_u val[100];
6223
6224 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006225
6226 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6227 return;
6228
6229 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006230 if (buf == NULL)
6231 return;
6232 term = buf->b_term;
6233
6234 if (term_job_running(term))
6235 STRCPY(val, "running");
6236 else
6237 STRCPY(val, "finished");
6238 if (term->tl_normal_mode)
6239 STRCAT(val, ",normal");
6240 rettv->vval.v_string = vim_strsave(val);
6241}
6242
6243/*
6244 * "term_gettitle(buf)" function
6245 */
6246 void
6247f_term_gettitle(typval_T *argvars, typval_T *rettv)
6248{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006249 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006250
6251 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006252
6253 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6254 return;
6255
6256 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006257 if (buf == NULL)
6258 return;
6259
6260 if (buf->b_term->tl_title != NULL)
6261 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
6262}
6263
6264/*
6265 * "term_gettty(buf)" function
6266 */
6267 void
6268f_term_gettty(typval_T *argvars, typval_T *rettv)
6269{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006270 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02006271 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006272 int num = 0;
6273
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006274 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006275 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006276 || check_for_opt_bool_arg(argvars, 1) == FAIL))
6277 return;
6278
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006279 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006280 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006281 if (buf == NULL)
6282 return;
6283 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02006284 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006285
6286 switch (num)
6287 {
6288 case 0:
6289 if (buf->b_term->tl_job != NULL)
6290 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006291 break;
6292 case 1:
6293 if (buf->b_term->tl_job != NULL)
6294 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006295 break;
6296 default:
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006297 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006298 return;
6299 }
6300 if (p != NULL)
6301 rettv->vval.v_string = vim_strsave(p);
6302}
6303
6304/*
6305 * "term_list()" function
6306 */
6307 void
6308f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
6309{
6310 term_T *tp;
6311 list_T *l;
6312
6313 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
6314 return;
6315
6316 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006317 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02006318 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006319 if (list_append_number(l,
6320 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6321 return;
6322}
6323
6324/*
6325 * "term_scrape(buf, row)" function
6326 */
6327 void
6328f_term_scrape(typval_T *argvars, typval_T *rettv)
6329{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006330 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006331 VTermScreen *screen = NULL;
6332 VTermPos pos;
6333 list_T *l;
6334 term_T *term;
6335 char_u *p;
6336 sb_line_T *line;
6337
6338 if (rettv_list_alloc(rettv) == FAIL)
6339 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006340
6341 if (in_vim9script()
6342 && (check_for_buffer_arg(argvars, 0) == FAIL
6343 || check_for_lnum_arg(argvars, 1) == FAIL))
6344 return;
6345
6346 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006347 if (buf == NULL)
6348 return;
6349 term = buf->b_term;
6350
6351 l = rettv->vval.v_list;
6352 pos.row = get_row_number(&argvars[1], term);
6353
6354 if (term->tl_vterm != NULL)
6355 {
6356 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006357 if (screen == NULL) // can't really happen
6358 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006359 p = NULL;
6360 line = NULL;
6361 }
6362 else
6363 {
6364 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6365
6366 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6367 return;
6368 p = ml_get_buf(buf, lnum + 1, FALSE);
6369 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6370 }
6371
6372 for (pos.col = 0; pos.col < term->tl_cols; )
6373 {
6374 dict_T *dcell;
6375 int width;
6376 VTermScreenCellAttrs attrs;
6377 VTermColor fg, bg;
6378 char_u rgb[8];
6379 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6380 int off = 0;
6381 int i;
6382
6383 if (screen == NULL)
6384 {
6385 cellattr_T *cellattr;
6386 int len;
6387
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006388 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006389 if (pos.col >= line->sb_cols)
6390 break;
6391 cellattr = line->sb_cells + pos.col;
6392 width = cellattr->width;
6393 attrs = cellattr->attrs;
6394 fg = cellattr->fg;
6395 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006396 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006397 mch_memmove(mbs, p, len);
6398 mbs[len] = NUL;
6399 p += len;
6400 }
6401 else
6402 {
6403 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006404
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006405 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6406 break;
6407 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6408 {
6409 if (cell.chars[i] == 0)
6410 break;
6411 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6412 }
6413 mbs[off] = NUL;
6414 width = cell.width;
6415 attrs = cell.attrs;
6416 fg = cell.fg;
6417 bg = cell.bg;
6418 }
6419 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006420 if (dcell == NULL)
6421 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006422 list_append_dict(l, dcell);
6423
Bram Moolenaare0be1672018-07-08 16:50:37 +02006424 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006425
6426 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6427 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006428 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006429 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6430 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006431 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006432
Bram Moolenaar87fd0922021-11-20 13:47:45 +00006433 dict_add_number(dcell, "attr",
6434 cell2attr(term, NULL, &attrs, &fg, &bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006435 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006436
6437 ++pos.col;
6438 if (width == 2)
6439 ++pos.col;
6440 }
6441}
6442
6443/*
6444 * "term_sendkeys(buf, keys)" function
6445 */
6446 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006447f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006448{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006449 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006450 char_u *msg;
6451 term_T *term;
6452
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006453 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006454 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006455 || check_for_string_arg(argvars, 1) == FAIL))
6456 return;
6457
6458 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006459 if (buf == NULL)
6460 return;
6461
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006462 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006463 if (msg == NULL)
6464 return;
6465 term = buf->b_term;
6466 if (term->tl_vterm == NULL)
6467 return;
6468
6469 while (*msg != NUL)
6470 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006471 int c;
6472
6473 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6474 {
6475 c = TO_SPECIAL(msg[1], msg[2]);
6476 msg += 3;
6477 }
6478 else
6479 {
6480 c = PTR2CHAR(msg);
6481 msg += MB_CPTR2LEN(msg);
6482 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006483 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006484 }
6485}
6486
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006487#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6488/*
6489 * "term_getansicolors(buf)" function
6490 */
6491 void
6492f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6493{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006494 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006495 term_T *term;
6496 VTermState *state;
6497 VTermColor color;
6498 char_u hexbuf[10];
6499 int index;
6500 list_T *list;
6501
6502 if (rettv_list_alloc(rettv) == FAIL)
6503 return;
6504
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006505 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6506 return;
6507
6508 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006509 if (buf == NULL)
6510 return;
6511 term = buf->b_term;
6512 if (term->tl_vterm == NULL)
6513 return;
6514
6515 list = rettv->vval.v_list;
6516 state = vterm_obtain_state(term->tl_vterm);
6517 for (index = 0; index < 16; index++)
6518 {
6519 vterm_state_get_palette_color(state, index, &color);
6520 sprintf((char *)hexbuf, "#%02x%02x%02x",
6521 color.red, color.green, color.blue);
6522 if (list_append_string(list, hexbuf, 7) == FAIL)
6523 return;
6524 }
6525}
6526
6527/*
6528 * "term_setansicolors(buf, list)" function
6529 */
6530 void
6531f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6532{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006533 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006534 term_T *term;
LemonBoyb2b3acb2022-05-20 10:10:34 +01006535 listitem_T *li;
6536 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006537
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006538 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006539 && (check_for_buffer_arg(argvars, 0) == FAIL
6540 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006541 return;
6542
6543 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006544 if (buf == NULL)
6545 return;
6546 term = buf->b_term;
6547 if (term->tl_vterm == NULL)
6548 return;
6549
Bram Moolenaard83392a2022-09-01 12:22:46 +01006550 if (check_for_nonnull_list_arg(argvars, 1) == FAIL)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006551 return;
Bram Moolenaard83392a2022-09-01 12:22:46 +01006552
LemonBoyb2b3acb2022-05-20 10:10:34 +01006553 if (argvars[1].vval.v_list->lv_first == &range_list_item
6554 || argvars[1].vval.v_list->lv_len != 16)
6555 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006556 emsg(_(e_invalid_argument));
LemonBoyb2b3acb2022-05-20 10:10:34 +01006557 return;
6558 }
6559
6560 if (term->tl_palette == NULL)
6561 term->tl_palette = ALLOC_MULT(long_u, 16);
6562 if (term->tl_palette == NULL)
6563 return;
6564
6565 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
6566 {
6567 char_u *color_name;
6568 guicolor_T guicolor;
6569
6570 color_name = tv_get_string_chk(&li->li_tv);
6571 if (color_name == NULL)
6572 return;
6573
6574 guicolor = GUI_GET_COLOR(color_name);
6575 if (guicolor == INVALCOLOR)
6576 {
6577 semsg(_(e_cannot_allocate_color_str), color_name);
6578 return;
6579 }
6580
6581 term->tl_palette[n++] = GUI_MCH_GET_RGB(guicolor);
6582 }
6583
6584 term_update_palette(term);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006585}
6586#endif
6587
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006588/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006589 * "term_setapi(buf, api)" function
6590 */
6591 void
6592f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6593{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006594 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006595 term_T *term;
6596 char_u *api;
6597
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006598 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006599 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006600 || check_for_string_arg(argvars, 1) == FAIL))
6601 return;
6602
6603 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006604 if (buf == NULL)
6605 return;
6606 term = buf->b_term;
6607 vim_free(term->tl_api);
6608 api = tv_get_string_chk(&argvars[1]);
6609 if (api != NULL)
6610 term->tl_api = vim_strsave(api);
6611 else
6612 term->tl_api = NULL;
6613}
6614
6615/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006616 * "term_setrestore(buf, command)" function
6617 */
6618 void
6619f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6620{
6621#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006622 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006623 term_T *term;
6624 char_u *cmd;
6625
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006626 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006627 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006628 || check_for_string_arg(argvars, 1) == FAIL))
6629 return;
6630
6631 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006632 if (buf == NULL)
6633 return;
6634 term = buf->b_term;
6635 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006636 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006637 if (cmd != NULL)
6638 term->tl_command = vim_strsave(cmd);
6639 else
6640 term->tl_command = NULL;
6641#endif
6642}
6643
6644/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006645 * "term_setkill(buf, how)" function
6646 */
6647 void
6648f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6649{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006650 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006651 term_T *term;
6652 char_u *how;
6653
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006654 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006655 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006656 || check_for_string_arg(argvars, 1) == FAIL))
6657 return;
6658
6659 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006660 if (buf == NULL)
6661 return;
6662 term = buf->b_term;
6663 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006664 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006665 if (how != NULL)
6666 term->tl_kill = vim_strsave(how);
6667 else
6668 term->tl_kill = NULL;
6669}
6670
6671/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006672 * "term_start(command, options)" function
6673 */
6674 void
6675f_term_start(typval_T *argvars, typval_T *rettv)
6676{
6677 jobopt_T opt;
6678 buf_T *buf;
6679
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006680 if (in_vim9script()
6681 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6682 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6683 return;
6684
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006685 init_job_options(&opt);
6686 if (argvars[1].v_type != VAR_UNKNOWN
6687 && get_job_options(&argvars[1], &opt,
6688 JO_TIMEOUT_ALL + JO_STOPONEXIT
6689 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6690 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6691 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6692 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006693 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006694 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006695 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006696 return;
6697
Bram Moolenaar13568252018-03-16 20:46:58 +01006698 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006699
6700 if (buf != NULL && buf->b_term != NULL)
6701 rettv->vval.v_number = buf->b_fnum;
6702}
6703
6704/*
6705 * "term_wait" function
6706 */
6707 void
6708f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6709{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006710 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006711
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006712 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006713 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006714 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006715 return;
6716
6717 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006718 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006719 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006720 if (buf->b_term->tl_job == NULL)
6721 {
6722 ch_log(NULL, "term_wait(): no job to wait for");
6723 return;
6724 }
6725 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006726 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006727 return;
6728
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006729 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006730 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006731 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6732 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006733 // The job is dead, keep reading channel I/O until the channel is
6734 // closed. buf->b_term may become NULL if the terminal was closed while
6735 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006736 ch_log(NULL, "term_wait(): waiting for channel to close");
6737 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6738 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006739 term_flush_messages();
6740
Bram Moolenaard45aa552018-05-21 22:50:29 +02006741 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006742 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006743 // If the terminal is closed when the channel is closed the
6744 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006745 break;
Bram Moolenaareea32af2021-11-21 14:51:13 +00006746 if (buf->b_term == NULL || buf->b_term->tl_channel_closing)
6747 // came here from a close callback, only wait one time
6748 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006749 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006750
6751 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006752 }
6753 else
6754 {
6755 long wait = 10L;
6756
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006757 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006758
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006759 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006760 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006761 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006762 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006763
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006764 // Flushing messages on channels is hopefully sufficient.
6765 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006766 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006767 }
6768}
6769
6770/*
6771 * Called when a channel has sent all the lines to a terminal.
6772 * Send a CTRL-D to mark the end of the text.
6773 */
6774 void
6775term_send_eof(channel_T *ch)
6776{
6777 term_T *term;
6778
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006779 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006780 if (term->tl_job == ch->ch_job)
6781 {
6782 if (term->tl_eof_chars != NULL)
6783 {
6784 channel_send(ch, PART_IN, term->tl_eof_chars,
6785 (int)STRLEN(term->tl_eof_chars), NULL);
6786 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6787 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006788# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006789 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006790 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006791 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6792# endif
6793 }
6794}
6795
Bram Moolenaar113e1072019-01-20 15:30:40 +01006796#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006797 job_T *
6798term_getjob(term_T *term)
6799{
6800 return term != NULL ? term->tl_job : NULL;
6801}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006802#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006803
Bram Moolenaar4f974752019-02-17 17:44:42 +01006804# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006805
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006806///////////////////////////////////////
6807// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006808#ifdef PROTO
6809typedef int COORD;
6810typedef int DWORD;
6811typedef int HANDLE;
6812typedef int *DWORD_PTR;
6813typedef int HPCON;
6814typedef int HRESULT;
6815typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006816typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006817typedef int PSIZE_T;
6818typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006819typedef int BOOL;
6820# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006821#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006822
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006823HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6824HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6825HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006826BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6827BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6828void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006829
6830 static int
6831dyn_conpty_init(int verbose)
6832{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006833 static HMODULE hKerneldll = NULL;
6834 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006835 static struct
6836 {
6837 char *name;
6838 FARPROC *ptr;
6839 } conpty_entry[] =
6840 {
6841 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6842 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6843 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6844 {"InitializeProcThreadAttributeList",
6845 (FARPROC*)&pInitializeProcThreadAttributeList},
6846 {"UpdateProcThreadAttribute",
6847 (FARPROC*)&pUpdateProcThreadAttribute},
6848 {"DeleteProcThreadAttributeList",
6849 (FARPROC*)&pDeleteProcThreadAttributeList},
6850 {NULL, NULL}
6851 };
6852
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006853 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006854 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006855 if (verbose)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006856 emsg(_(e_conpty_is_not_available));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006857 return FAIL;
6858 }
6859
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006860 // No need to initialize twice.
6861 if (hKerneldll)
6862 return OK;
6863
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006864 hKerneldll = vimLoadLib("kernel32.dll");
6865 for (i = 0; conpty_entry[i].name != NULL
6866 && conpty_entry[i].ptr != NULL; ++i)
6867 {
6868 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6869 conpty_entry[i].name)) == NULL)
6870 {
6871 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006872 semsg(_(e_could_not_load_library_function_str), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006873 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006874 return FAIL;
6875 }
6876 }
6877
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006878 return OK;
6879}
6880
6881 static int
6882conpty_term_and_job_init(
6883 term_T *term,
6884 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006885 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006886 jobopt_T *opt,
6887 jobopt_T *orig_opt)
6888{
6889 WCHAR *cmd_wchar = NULL;
6890 WCHAR *cmd_wchar_copy = NULL;
6891 WCHAR *cwd_wchar = NULL;
6892 WCHAR *env_wchar = NULL;
6893 channel_T *channel = NULL;
6894 job_T *job = NULL;
6895 HANDLE jo = NULL;
6896 garray_T ga_cmd, ga_env;
6897 char_u *cmd = NULL;
6898 HRESULT hr;
6899 COORD consize;
6900 SIZE_T breq;
6901 PROCESS_INFORMATION proc_info;
6902 HANDLE i_theirs = NULL;
6903 HANDLE o_theirs = NULL;
6904 HANDLE i_ours = NULL;
6905 HANDLE o_ours = NULL;
6906
Bram Moolenaar04935fb2022-01-08 16:19:22 +00006907 ga_init2(&ga_cmd, sizeof(char*), 20);
6908 ga_init2(&ga_env, sizeof(char*), 20);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006909
6910 if (argvar->v_type == VAR_STRING)
6911 {
6912 cmd = argvar->vval.v_string;
6913 }
6914 else if (argvar->v_type == VAR_LIST)
6915 {
6916 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6917 goto failed;
6918 cmd = ga_cmd.ga_data;
6919 }
6920 if (cmd == NULL || *cmd == NUL)
6921 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006922 emsg(_(e_invalid_argument));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006923 goto failed;
6924 }
6925
6926 term->tl_arg0_cmd = vim_strsave(cmd);
6927
6928 cmd_wchar = enc_to_utf16(cmd, NULL);
6929
6930 if (cmd_wchar != NULL)
6931 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006932 // Request by CreateProcessW
6933 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006934 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006935 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6936 }
6937
6938 ga_clear(&ga_cmd);
6939 if (cmd_wchar == NULL)
6940 goto failed;
6941 if (opt->jo_cwd != NULL)
6942 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6943
6944 win32_build_env(opt->jo_env, &ga_env, TRUE);
6945 env_wchar = ga_env.ga_data;
6946
6947 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6948 goto failed;
6949 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6950 goto failed;
6951
6952 consize.X = term->tl_cols;
6953 consize.Y = term->tl_rows;
6954 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6955 &term->tl_conpty);
6956 if (FAILED(hr))
6957 goto failed;
6958
6959 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6960
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006961 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006962 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006963 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006964 if (!term->tl_siex.lpAttributeList)
6965 goto failed;
6966 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6967 0, &breq))
6968 goto failed;
6969 if (!pUpdateProcThreadAttribute(
6970 term->tl_siex.lpAttributeList, 0,
6971 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6972 sizeof(HPCON), NULL, NULL))
6973 goto failed;
6974
6975 channel = add_channel();
6976 if (channel == NULL)
6977 goto failed;
6978
6979 job = job_alloc();
6980 if (job == NULL)
6981 goto failed;
6982 if (argvar->v_type == VAR_STRING)
6983 {
6984 int argc;
6985
6986 build_argv_from_string(cmd, &job->jv_argv, &argc);
6987 }
6988 else
6989 {
6990 int argc;
6991
6992 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6993 }
6994
6995 if (opt->jo_set & JO_IN_BUF)
6996 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6997
6998 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6999 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02007000 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007001 env_wchar, cwd_wchar,
7002 &term->tl_siex.StartupInfo, &proc_info))
7003 goto failed;
7004
7005 CloseHandle(i_theirs);
7006 CloseHandle(o_theirs);
7007
7008 channel_set_pipes(channel,
7009 (sock_T)i_ours,
7010 (sock_T)o_ours,
7011 (sock_T)o_ours);
7012
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007013 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007014 channel->ch_write_text_mode = TRUE;
7015
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007016 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007017 channel->ch_anonymous_pipe = TRUE;
7018
7019 jo = CreateJobObject(NULL, NULL);
7020 if (jo == NULL)
7021 goto failed;
7022
7023 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
7024 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007025 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007026 CloseHandle(jo);
7027 jo = NULL;
7028 }
7029
7030 ResumeThread(proc_info.hThread);
7031 CloseHandle(proc_info.hThread);
7032
7033 vim_free(cmd_wchar);
7034 vim_free(cmd_wchar_copy);
7035 vim_free(cwd_wchar);
7036 vim_free(env_wchar);
7037
7038 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7039 goto failed;
7040
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007041#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
LemonBoyb2b3acb2022-05-20 10:10:34 +01007042 if (term_use_palette())
7043 {
7044 if (term->tl_palette != NULL)
7045 set_vterm_palette(term->tl_vterm, term->tl_palette);
7046 else
7047 init_vterm_ansi_colors(term->tl_vterm);
7048 }
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007049#endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007050
7051 channel_set_job(channel, job, opt);
7052 job_set_options(job, opt);
7053
7054 job->jv_channel = channel;
7055 job->jv_proc_info = proc_info;
7056 job->jv_job_object = jo;
7057 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007058 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007059 ++job->jv_refcount;
7060 term->tl_job = job;
7061
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007062 // Redirecting stdout and stderr doesn't work at the job level. Instead
7063 // open the file here and handle it in. opt->jo_io was changed in
7064 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007065 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7066 {
7067 char_u *fname = opt->jo_io_name[PART_OUT];
7068
7069 ch_log(channel, "Opening output file %s", fname);
7070 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7071 if (term->tl_out_fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007072 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007073 }
7074
7075 return OK;
7076
7077failed:
7078 ga_clear(&ga_cmd);
7079 ga_clear(&ga_env);
7080 vim_free(cmd_wchar);
7081 vim_free(cmd_wchar_copy);
7082 vim_free(cwd_wchar);
7083 if (channel != NULL)
7084 channel_clear(channel);
7085 if (job != NULL)
7086 {
7087 job->jv_channel = NULL;
7088 job_cleanup(job);
7089 }
7090 term->tl_job = NULL;
7091 if (jo != NULL)
7092 CloseHandle(jo);
7093
7094 if (term->tl_siex.lpAttributeList != NULL)
7095 {
7096 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
7097 vim_free(term->tl_siex.lpAttributeList);
7098 }
7099 term->tl_siex.lpAttributeList = NULL;
7100 if (o_theirs != NULL)
7101 CloseHandle(o_theirs);
7102 if (o_ours != NULL)
7103 CloseHandle(o_ours);
7104 if (i_ours != NULL)
7105 CloseHandle(i_ours);
7106 if (i_theirs != NULL)
7107 CloseHandle(i_theirs);
7108 if (term->tl_conpty != NULL)
7109 pClosePseudoConsole(term->tl_conpty);
7110 term->tl_conpty = NULL;
7111 return FAIL;
7112}
7113
7114 static void
7115conpty_term_report_winsize(term_T *term, int rows, int cols)
7116{
7117 COORD consize;
7118
7119 consize.X = cols;
7120 consize.Y = rows;
7121 pResizePseudoConsole(term->tl_conpty, consize);
7122}
7123
Bram Moolenaar840d16f2019-09-10 21:27:18 +02007124 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007125term_free_conpty(term_T *term)
7126{
7127 if (term->tl_siex.lpAttributeList != NULL)
7128 {
7129 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
7130 vim_free(term->tl_siex.lpAttributeList);
7131 }
7132 term->tl_siex.lpAttributeList = NULL;
7133 if (term->tl_conpty != NULL)
7134 pClosePseudoConsole(term->tl_conpty);
7135 term->tl_conpty = NULL;
7136}
7137
7138 int
7139use_conpty(void)
7140{
7141 return has_conpty;
7142}
7143
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007144# ifndef PROTO
7145
7146#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
7147#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01007148#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007149
7150void* (*winpty_config_new)(UINT64, void*);
7151void* (*winpty_open)(void*, void*);
7152void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
7153BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
7154void (*winpty_config_set_mouse_mode)(void*, int);
7155void (*winpty_config_set_initial_size)(void*, int, int);
7156LPCWSTR (*winpty_conin_name)(void*);
7157LPCWSTR (*winpty_conout_name)(void*);
7158LPCWSTR (*winpty_conerr_name)(void*);
7159void (*winpty_free)(void*);
7160void (*winpty_config_free)(void*);
7161void (*winpty_spawn_config_free)(void*);
7162void (*winpty_error_free)(void*);
7163LPCWSTR (*winpty_error_msg)(void*);
7164BOOL (*winpty_set_size)(void*, int, int, void*);
7165HANDLE (*winpty_agent_process)(void*);
7166
7167#define WINPTY_DLL "winpty.dll"
7168
7169static HINSTANCE hWinPtyDLL = NULL;
7170# endif
7171
7172 static int
7173dyn_winpty_init(int verbose)
7174{
7175 int i;
7176 static struct
7177 {
7178 char *name;
7179 FARPROC *ptr;
7180 } winpty_entry[] =
7181 {
7182 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
7183 {"winpty_config_free", (FARPROC*)&winpty_config_free},
7184 {"winpty_config_new", (FARPROC*)&winpty_config_new},
7185 {"winpty_config_set_mouse_mode",
7186 (FARPROC*)&winpty_config_set_mouse_mode},
7187 {"winpty_config_set_initial_size",
7188 (FARPROC*)&winpty_config_set_initial_size},
7189 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
7190 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
7191 {"winpty_error_free", (FARPROC*)&winpty_error_free},
7192 {"winpty_free", (FARPROC*)&winpty_free},
7193 {"winpty_open", (FARPROC*)&winpty_open},
7194 {"winpty_spawn", (FARPROC*)&winpty_spawn},
7195 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
7196 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
7197 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
7198 {"winpty_set_size", (FARPROC*)&winpty_set_size},
7199 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
7200 {NULL, NULL}
7201 };
7202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007203 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007204 if (hWinPtyDLL)
7205 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007206 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
7207 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007208 if (*p_winptydll != NUL)
7209 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
7210 if (!hWinPtyDLL)
7211 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
7212 if (!hWinPtyDLL)
7213 {
7214 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007215 semsg(_(e_could_not_load_library_str_str),
Martin Tournoij1a3e5742021-07-24 13:57:29 +02007216 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
7217 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007218 return FAIL;
7219 }
7220 for (i = 0; winpty_entry[i].name != NULL
7221 && winpty_entry[i].ptr != NULL; ++i)
7222 {
7223 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
7224 winpty_entry[i].name)) == NULL)
7225 {
7226 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007227 semsg(_(e_could_not_load_library_function_str), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01007228 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007229 return FAIL;
7230 }
7231 }
7232
7233 return OK;
7234}
7235
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007236 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007237winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007238 term_T *term,
7239 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007240 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007241 jobopt_T *opt,
7242 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007243{
7244 WCHAR *cmd_wchar = NULL;
7245 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007246 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007247 channel_T *channel = NULL;
7248 job_T *job = NULL;
7249 DWORD error;
7250 HANDLE jo = NULL;
7251 HANDLE child_process_handle;
7252 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01007253 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007254 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007255 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007256 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007257
Bram Moolenaar04935fb2022-01-08 16:19:22 +00007258 ga_init2(&ga_cmd, sizeof(char*), 20);
7259 ga_init2(&ga_env, sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007260
7261 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007262 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007263 cmd = argvar->vval.v_string;
7264 }
7265 else if (argvar->v_type == VAR_LIST)
7266 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007267 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007268 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007269 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007270 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007271 if (cmd == NULL || *cmd == NUL)
7272 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007273 emsg(_(e_invalid_argument));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007274 goto failed;
7275 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007276
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007277 term->tl_arg0_cmd = vim_strsave(cmd);
7278
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007279 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007280 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007281 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007282 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007283 if (opt->jo_cwd != NULL)
7284 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007285
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007286 win32_build_env(opt->jo_env, &ga_env, TRUE);
7287 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007288
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007289 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
7290 if (term->tl_winpty_config == NULL)
7291 goto failed;
7292
7293 winpty_config_set_mouse_mode(term->tl_winpty_config,
7294 WINPTY_MOUSE_MODE_FORCE);
7295 winpty_config_set_initial_size(term->tl_winpty_config,
7296 term->tl_cols, term->tl_rows);
7297 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
7298 if (term->tl_winpty == NULL)
7299 goto failed;
7300
7301 spawn_config = winpty_spawn_config_new(
7302 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
7303 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
7304 NULL,
7305 cmd_wchar,
7306 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007307 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007308 &winpty_err);
7309 if (spawn_config == NULL)
7310 goto failed;
7311
7312 channel = add_channel();
7313 if (channel == NULL)
7314 goto failed;
7315
7316 job = job_alloc();
7317 if (job == NULL)
7318 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02007319 if (argvar->v_type == VAR_STRING)
7320 {
7321 int argc;
7322
7323 build_argv_from_string(cmd, &job->jv_argv, &argc);
7324 }
7325 else
7326 {
7327 int argc;
7328
7329 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
7330 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007331
7332 if (opt->jo_set & JO_IN_BUF)
7333 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
7334
7335 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
7336 &child_thread_handle, &error, &winpty_err))
7337 goto failed;
7338
7339 channel_set_pipes(channel,
7340 (sock_T)CreateFileW(
7341 winpty_conin_name(term->tl_winpty),
7342 GENERIC_WRITE, 0, NULL,
7343 OPEN_EXISTING, 0, NULL),
7344 (sock_T)CreateFileW(
7345 winpty_conout_name(term->tl_winpty),
7346 GENERIC_READ, 0, NULL,
7347 OPEN_EXISTING, 0, NULL),
7348 (sock_T)CreateFileW(
7349 winpty_conerr_name(term->tl_winpty),
7350 GENERIC_READ, 0, NULL,
7351 OPEN_EXISTING, 0, NULL));
7352
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007353 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007354 channel->ch_write_text_mode = TRUE;
7355
7356 jo = CreateJobObject(NULL, NULL);
7357 if (jo == NULL)
7358 goto failed;
7359
7360 if (!AssignProcessToJobObject(jo, child_process_handle))
7361 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007362 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007363 CloseHandle(jo);
7364 jo = NULL;
7365 }
7366
7367 winpty_spawn_config_free(spawn_config);
7368 vim_free(cmd_wchar);
7369 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007370 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007371
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007372 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7373 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007374
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007375#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
LemonBoyb2b3acb2022-05-20 10:10:34 +01007376 if (term_use_palette())
7377 {
7378 if (term->tl_palette != NULL)
7379 set_vterm_palette(term->tl_vterm, term->tl_palette);
7380 else
7381 init_vterm_ansi_colors(term->tl_vterm);
7382 }
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007383#endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007384
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007385 channel_set_job(channel, job, opt);
7386 job_set_options(job, opt);
7387
7388 job->jv_channel = channel;
7389 job->jv_proc_info.hProcess = child_process_handle;
7390 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7391 job->jv_job_object = jo;
7392 job->jv_status = JOB_STARTED;
7393 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007394 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007395 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007396 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007397 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007398 ++job->jv_refcount;
7399 term->tl_job = job;
7400
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007401 // Redirecting stdout and stderr doesn't work at the job level. Instead
7402 // open the file here and handle it in. opt->jo_io was changed in
7403 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007404 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7405 {
7406 char_u *fname = opt->jo_io_name[PART_OUT];
7407
7408 ch_log(channel, "Opening output file %s", fname);
7409 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7410 if (term->tl_out_fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007411 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007412 }
7413
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007414 return OK;
7415
7416failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007417 ga_clear(&ga_cmd);
7418 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007419 vim_free(cmd_wchar);
7420 vim_free(cwd_wchar);
7421 if (spawn_config != NULL)
7422 winpty_spawn_config_free(spawn_config);
7423 if (channel != NULL)
7424 channel_clear(channel);
7425 if (job != NULL)
7426 {
7427 job->jv_channel = NULL;
7428 job_cleanup(job);
7429 }
7430 term->tl_job = NULL;
7431 if (jo != NULL)
7432 CloseHandle(jo);
7433 if (term->tl_winpty != NULL)
7434 winpty_free(term->tl_winpty);
7435 term->tl_winpty = NULL;
7436 if (term->tl_winpty_config != NULL)
7437 winpty_config_free(term->tl_winpty_config);
7438 term->tl_winpty_config = NULL;
7439 if (winpty_err != NULL)
7440 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007441 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007442 (short_u *)winpty_error_msg(winpty_err), NULL);
7443
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007444 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007445 winpty_error_free(winpty_err);
7446 }
7447 return FAIL;
7448}
7449
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007450/*
7451 * Create a new terminal of "rows" by "cols" cells.
7452 * Store a reference in "term".
7453 * Return OK or FAIL.
7454 */
7455 static int
7456term_and_job_init(
7457 term_T *term,
7458 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007459 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007460 jobopt_T *opt,
7461 jobopt_T *orig_opt)
7462{
7463 int use_winpty = FALSE;
7464 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007465 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007466
7467 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7468 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7469
7470 if (!has_winpty && !has_conpty)
7471 // If neither is available give the errors for winpty, since when
7472 // conpty is not available it can't be installed either.
7473 return dyn_winpty_init(TRUE);
7474
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007475 if (opt->jo_tty_type != NUL)
7476 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007477
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007478 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007479 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007480 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007481 use_conpty = TRUE;
7482 else if (has_winpty)
7483 use_winpty = TRUE;
7484 // else: error
7485 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007486 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007487 {
7488 if (has_winpty)
7489 use_winpty = TRUE;
7490 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007491 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007492 {
7493 if (has_conpty)
7494 use_conpty = TRUE;
7495 else
7496 return dyn_conpty_init(TRUE);
7497 }
7498
7499 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007500 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007501
7502 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007503 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007504
7505 // error
7506 return dyn_winpty_init(TRUE);
7507}
7508
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007509 static int
7510create_pty_only(term_T *term, jobopt_T *options)
7511{
7512 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7513 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7514 char in_name[80], out_name[80];
7515 channel_T *channel = NULL;
7516
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007517 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7518 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007519
7520 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7521 GetCurrentProcessId(),
7522 curbuf->b_fnum);
7523 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7524 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7525 PIPE_UNLIMITED_INSTANCES,
7526 0, 0, NMPWAIT_NOWAIT, NULL);
7527 if (hPipeIn == INVALID_HANDLE_VALUE)
7528 goto failed;
7529
7530 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7531 GetCurrentProcessId(),
7532 curbuf->b_fnum);
7533 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7534 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7535 PIPE_UNLIMITED_INSTANCES,
7536 0, 0, 0, NULL);
7537 if (hPipeOut == INVALID_HANDLE_VALUE)
7538 goto failed;
7539
7540 ConnectNamedPipe(hPipeIn, NULL);
7541 ConnectNamedPipe(hPipeOut, NULL);
7542
7543 term->tl_job = job_alloc();
7544 if (term->tl_job == NULL)
7545 goto failed;
7546 ++term->tl_job->jv_refcount;
7547
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007548 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007549 term->tl_job->jv_status = JOB_FINISHED;
7550
7551 channel = add_channel();
7552 if (channel == NULL)
7553 goto failed;
7554 term->tl_job->jv_channel = channel;
7555 channel->ch_keep_open = TRUE;
7556 channel->ch_named_pipe = TRUE;
7557
7558 channel_set_pipes(channel,
7559 (sock_T)hPipeIn,
7560 (sock_T)hPipeOut,
7561 (sock_T)hPipeOut);
7562 channel_set_job(channel, term->tl_job, options);
7563 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7564 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7565
7566 return OK;
7567
7568failed:
7569 if (hPipeIn != NULL)
7570 CloseHandle(hPipeIn);
7571 if (hPipeOut != NULL)
7572 CloseHandle(hPipeOut);
7573 return FAIL;
7574}
7575
7576/*
7577 * Free the terminal emulator part of "term".
7578 */
7579 static void
7580term_free_vterm(term_T *term)
7581{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007582 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007583 if (term->tl_winpty != NULL)
7584 winpty_free(term->tl_winpty);
7585 term->tl_winpty = NULL;
7586 if (term->tl_winpty_config != NULL)
7587 winpty_config_free(term->tl_winpty_config);
7588 term->tl_winpty_config = NULL;
7589 if (term->tl_vterm != NULL)
7590 vterm_free(term->tl_vterm);
7591 term->tl_vterm = NULL;
7592}
7593
7594/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007595 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007596 */
7597 static void
7598term_report_winsize(term_T *term, int rows, int cols)
7599{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007600 if (term->tl_conpty)
7601 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007602 if (term->tl_winpty)
7603 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7604}
7605
7606 int
7607terminal_enabled(void)
7608{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007609 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007610}
7611
7612# else
7613
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007614///////////////////////////////////////
7615// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007616
7617/*
7618 * Create a new terminal of "rows" by "cols" cells.
7619 * Start job for "cmd".
7620 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007621 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007622 * Return OK or FAIL.
7623 */
7624 static int
7625term_and_job_init(
7626 term_T *term,
7627 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007628 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007629 jobopt_T *opt,
7630 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007631{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007632 term->tl_arg0_cmd = NULL;
7633
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007634 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7635 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007636
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007637#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
LemonBoyb2b3acb2022-05-20 10:10:34 +01007638 if (term_use_palette())
7639 {
7640 if (term->tl_palette != NULL)
7641 set_vterm_palette(term->tl_vterm, term->tl_palette);
7642 else
7643 init_vterm_ansi_colors(term->tl_vterm);
7644 }
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007645#endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007646
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007647 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007648 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007649 if (term->tl_job != NULL)
7650 ++term->tl_job->jv_refcount;
7651
7652 return term->tl_job != NULL
7653 && term->tl_job->jv_channel != NULL
7654 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7655}
7656
7657 static int
7658create_pty_only(term_T *term, jobopt_T *opt)
7659{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007660 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7661 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007662
7663 term->tl_job = job_alloc();
7664 if (term->tl_job == NULL)
7665 return FAIL;
7666 ++term->tl_job->jv_refcount;
7667
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007668 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007669 term->tl_job->jv_status = JOB_FINISHED;
7670
7671 return mch_create_pty_channel(term->tl_job, opt);
7672}
7673
7674/*
7675 * Free the terminal emulator part of "term".
7676 */
7677 static void
7678term_free_vterm(term_T *term)
7679{
7680 if (term->tl_vterm != NULL)
7681 vterm_free(term->tl_vterm);
7682 term->tl_vterm = NULL;
7683}
7684
7685/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007686 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007687 */
7688 static void
7689term_report_winsize(term_T *term, int rows, int cols)
7690{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007691 // Use an ioctl() to report the new window size to the job.
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007692 if (term->tl_job == NULL || term->tl_job->jv_channel == NULL)
7693 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007694
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007695 int fd = -1;
7696 int part;
7697
7698 for (part = PART_OUT; part < PART_COUNT; ++part)
7699 {
7700 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
7701 if (mch_isatty(fd))
7702 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007703 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007704 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7705 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007706}
7707
7708# endif
7709
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007710#endif // FEAT_TERMINAL