blob: fde77219dc709efa1252f6b9eb8efe160fb42cc3 [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
Bram Moolenaar58806c12023-04-29 14:26:02 +01001212position_cursor(win_T *wp, VTermPos *pos)
1213{
1214 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1215 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1216#ifdef FEAT_PROP_POPUP
1217 if (popup_is_popup(wp))
1218 {
1219 wp->w_wrow += popup_top_extra(wp);
1220 wp->w_wcol += popup_left_extra(wp);
1221 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
1222 }
1223 else
1224 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
1225#endif
1226 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1227}
1228
1229 static void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001230update_cursor(term_T *term, int redraw)
1231{
1232 if (term->tl_normal_mode)
1233 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001234#ifdef FEAT_GUI
1235 if (term->tl_system)
1236 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1237 term->tl_cursor_pos.col);
1238 else
1239#endif
Bram Moolenaar58806c12023-04-29 14:26:02 +01001240 if (!term_job_running(term))
1241 // avoid the cursor positioned below the last used line
Bram Moolenaar13568252018-03-16 20:46:58 +01001242 setcursor();
Bram Moolenaar58806c12023-04-29 14:26:02 +01001243 else
1244 {
1245 // do not use the window cursor position
1246 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
1247 windgoto(W_WINROW(curwin) + curwin->w_wrow,
1248 curwin->w_wincol + curwin->w_wcol);
1249 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001250 if (redraw)
1251 {
Shougo Matsushita4ccaedf2022-10-15 11:48:00 +01001252 aco_save_T aco;
1253
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001254 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1255 cursor_on();
1256 out_flush();
1257#ifdef FEAT_GUI
1258 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001259 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001260 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001261 gui_mch_flush();
1262 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001263#endif
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001264 // Make sure an invoked autocmd doesn't delete the buffer (and the
1265 // terminal) under our fingers.
Shougo Matsushita4ccaedf2022-10-15 11:48:00 +01001266 ++term->tl_buffer->b_locked;
1267
1268 // save and restore curwin and curbuf, in case the autocmd changes them
1269 aucmd_prepbuf(&aco, curbuf);
1270 apply_autocmds(EVENT_TEXTCHANGEDT, NULL, NULL, FALSE, term->tl_buffer);
1271 aucmd_restbuf(&aco);
1272
1273 --term->tl_buffer->b_locked;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001274 }
1275}
1276
1277/*
1278 * Invoked when "msg" output from a job was received. Write it to the terminal
1279 * of "buffer".
1280 */
1281 void
1282write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1283{
1284 size_t len = STRLEN(msg);
1285 term_T *term = buffer->b_term;
1286
Bram Moolenaar4f974752019-02-17 17:44:42 +01001287#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001288 // Win32: Cannot redirect output of the job, intercept it here and write to
1289 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001290 if (term->tl_out_fd != NULL)
1291 {
1292 ch_log(channel, "Writing %d bytes to output file", (int)len);
1293 fwrite(msg, len, 1, term->tl_out_fd);
1294 return;
1295 }
1296#endif
1297
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001298 if (term->tl_vterm == NULL)
1299 {
1300 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1301 return;
1302 }
1303 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01001304 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001305 term_write_job_output(term, msg, len);
1306
Bram Moolenaar13568252018-03-16 20:46:58 +01001307#ifdef FEAT_GUI
1308 if (term->tl_system)
1309 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001310 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001311 update_system_term(term);
1312 update_cursor(term, TRUE);
1313 }
1314 else
1315#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001316 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1317 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001318 if (!term->tl_normal_mode)
1319 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001320 // Don't use update_screen() when editing the command line, it gets
1321 // cleared.
1322 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001323 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar24959102022-05-07 20:01:16 +01001324 if (buffer == curbuf && (State & MODE_CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001325 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001326 update_screen(UPD_VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001327 // update_screen() can be slow, check the terminal wasn't closed
1328 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001329 if (buffer == curbuf && curbuf->b_term != NULL)
1330 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001331 }
1332 else
Bram Moolenaare5050712021-12-09 10:51:05 +00001333 redraw_after_callback(TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001334 }
1335}
1336
1337/*
1338 * Send a mouse position and click to the vterm
1339 */
1340 static int
1341term_send_mouse(VTerm *vterm, int button, int pressed)
1342{
1343 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001344 int row = mouse_row - W_WINROW(curwin);
1345 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001346
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001347#ifdef FEAT_PROP_POPUP
1348 if (popup_is_popup(curwin))
1349 {
1350 row -= popup_top_extra(curwin);
1351 col -= popup_left_extra(curwin);
1352 }
1353#endif
1354 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001355 if (button != 0)
1356 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001357 return TRUE;
1358}
1359
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001360static int enter_mouse_col = -1;
1361static int enter_mouse_row = -1;
1362
1363/*
1364 * Handle a mouse click, drag or release.
1365 * Return TRUE when a mouse event is sent to the terminal.
1366 */
1367 static int
1368term_mouse_click(VTerm *vterm, int key)
1369{
1370#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001371 // For modeless selection mouse drag and release events are ignored, unless
1372 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001373 static int ignore_drag_release = TRUE;
1374 VTermMouseState mouse_state;
1375
1376 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1377 if (mouse_state.flags == 0)
1378 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001379 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001380 switch (key)
1381 {
1382 case K_LEFTDRAG:
1383 case K_LEFTRELEASE:
1384 case K_RIGHTDRAG:
1385 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001386 // Ignore drag and release events when the button-down wasn't
1387 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001388 if (ignore_drag_release)
1389 {
1390 int save_mouse_col, save_mouse_row;
1391
1392 if (enter_mouse_col < 0)
1393 break;
1394
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001395 // mouse click in the window gave us focus, handle that
1396 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001397 save_mouse_col = mouse_col;
1398 save_mouse_row = mouse_row;
1399 mouse_col = enter_mouse_col;
1400 mouse_row = enter_mouse_row;
1401 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1402 mouse_col = save_mouse_col;
1403 mouse_row = save_mouse_row;
1404 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001405 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001406 case K_LEFTMOUSE:
1407 case K_RIGHTMOUSE:
1408 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1409 ignore_drag_release = TRUE;
1410 else
1411 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001412 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001413 if (clip_star.available)
1414 {
1415 int button, is_click, is_drag;
1416
1417 button = get_mouse_button(KEY2TERMCAP1(key),
1418 &is_click, &is_drag);
1419 if (mouse_model_popup() && button == MOUSE_LEFT
1420 && (mod_mask & MOD_MASK_SHIFT))
1421 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001422 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001423 button = MOUSE_RIGHT;
1424 mod_mask &= ~MOD_MASK_SHIFT;
1425 }
1426 clip_modeless(button, is_click, is_drag);
1427 }
1428 break;
1429
1430 case K_MIDDLEMOUSE:
1431 if (clip_star.available)
1432 insert_reg('*', TRUE);
1433 break;
1434 }
1435 enter_mouse_col = -1;
1436 return FALSE;
1437 }
1438#endif
1439 enter_mouse_col = -1;
1440
1441 switch (key)
1442 {
1443 case K_LEFTMOUSE:
1444 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1445 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1446 case K_LEFTRELEASE:
1447 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1448 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1449 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1450 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1451 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1452 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1453 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1454 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1455 }
1456 return TRUE;
1457}
1458
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001459/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001460 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1461 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001462 * Return the number of bytes in "buf".
1463 */
1464 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001465term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001466{
1467 VTerm *vterm = term->tl_vterm;
1468 VTermKey key = VTERM_KEY_NONE;
1469 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001470 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001471
1472 switch (c)
1473 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001474 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001475
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001476 // don't use VTERM_KEY_BACKSPACE, it always
1477 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001478 case K_BS: c = term_backspace_char; break;
1479
1480 case ESC: key = VTERM_KEY_ESCAPE; break;
1481 case K_DEL: key = VTERM_KEY_DEL; break;
1482 case K_DOWN: key = VTERM_KEY_DOWN; break;
1483 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1484 key = VTERM_KEY_DOWN; break;
1485 case K_END: key = VTERM_KEY_END; break;
1486 case K_S_END: mod = VTERM_MOD_SHIFT;
1487 key = VTERM_KEY_END; break;
1488 case K_C_END: mod = VTERM_MOD_CTRL;
1489 key = VTERM_KEY_END; break;
1490 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1491 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1492 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1493 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1494 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1495 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1496 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1497 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1498 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1499 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1500 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1501 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1502 case K_HOME: key = VTERM_KEY_HOME; break;
1503 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1504 key = VTERM_KEY_HOME; break;
1505 case K_C_HOME: mod = VTERM_MOD_CTRL;
1506 key = VTERM_KEY_HOME; break;
1507 case K_INS: key = VTERM_KEY_INS; break;
1508 case K_K0: key = VTERM_KEY_KP_0; break;
1509 case K_K1: key = VTERM_KEY_KP_1; break;
1510 case K_K2: key = VTERM_KEY_KP_2; break;
1511 case K_K3: key = VTERM_KEY_KP_3; break;
1512 case K_K4: key = VTERM_KEY_KP_4; break;
1513 case K_K5: key = VTERM_KEY_KP_5; break;
1514 case K_K6: key = VTERM_KEY_KP_6; break;
1515 case K_K7: key = VTERM_KEY_KP_7; break;
1516 case K_K8: key = VTERM_KEY_KP_8; break;
1517 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001518 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001519 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001520 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001521 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001522 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1523 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001524 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1525 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001526 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1527 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001528 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1529 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1530 case K_LEFT: key = VTERM_KEY_LEFT; break;
1531 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1532 key = VTERM_KEY_LEFT; break;
1533 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1534 key = VTERM_KEY_LEFT; break;
1535 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1536 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1537 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1538 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1539 key = VTERM_KEY_RIGHT; break;
1540 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1541 key = VTERM_KEY_RIGHT; break;
1542 case K_UP: key = VTERM_KEY_UP; break;
1543 case K_S_UP: mod = VTERM_MOD_SHIFT;
1544 key = VTERM_KEY_UP; break;
1545 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001546 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1547 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001548
Bram Moolenaara42ad572017-11-16 13:08:04 +01001549 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1550 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001551 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1552 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001553
1554 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001555 case K_LEFTMOUSE_NM:
1556 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001557 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001558 case K_LEFTRELEASE_NM:
1559 case K_MOUSEMOVE:
1560 case K_MIDDLEMOUSE:
1561 case K_MIDDLEDRAG:
1562 case K_MIDDLERELEASE:
1563 case K_RIGHTMOUSE:
1564 case K_RIGHTDRAG:
1565 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1566 return 0;
1567 other = TRUE;
1568 break;
1569
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001570 case K_X1MOUSE: /* TODO */ return 0;
1571 case K_X1DRAG: /* TODO */ return 0;
1572 case K_X1RELEASE: /* TODO */ return 0;
1573 case K_X2MOUSE: /* TODO */ return 0;
1574 case K_X2DRAG: /* TODO */ return 0;
1575 case K_X2RELEASE: /* TODO */ return 0;
1576
1577 case K_IGNORE: return 0;
1578 case K_NOP: return 0;
1579 case K_UNDO: return 0;
1580 case K_HELP: return 0;
1581 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1582 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1583 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1584 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1585 case K_SELECT: return 0;
1586#ifdef FEAT_GUI
1587 case K_VER_SCROLLBAR: return 0;
1588 case K_HOR_SCROLLBAR: return 0;
1589#endif
1590#ifdef FEAT_GUI_TABLINE
1591 case K_TABLINE: return 0;
1592 case K_TABMENU: return 0;
1593#endif
1594#ifdef FEAT_NETBEANS_INTG
1595 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1596#endif
1597#ifdef FEAT_DND
1598 case K_DROP: return 0;
1599#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001600 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001601 case K_PS: vterm_keyboard_start_paste(vterm);
1602 other = TRUE;
1603 break;
1604 case K_PE: vterm_keyboard_end_paste(vterm);
1605 other = TRUE;
1606 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001607 }
1608
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001609 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001610 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001611 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001612 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001613 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001614 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001615 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001616
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001617 // Ctrl-Shift-i may have the key "I" instead of "i", but for the kitty
1618 // keyboard protocol should use "i". Applies to all ascii letters.
1619 if (ASCII_ISUPPER(c)
Bram Moolenaarebed1b02022-11-24 14:05:19 +00001620 && vterm_is_kitty_keyboard(vterm)
Bram Moolenaar63a2e362022-11-23 20:20:18 +00001621 && mod == (VTERM_MOD_CTRL | VTERM_MOD_SHIFT))
1622 c = TOLOWER_ASC(c);
1623
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001624 /*
1625 * Convert special keys to vterm keys:
1626 * - Write keys to vterm: vterm_keyboard_key()
1627 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001628 */
1629 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001630 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001631 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001632 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001633 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001634 vterm_keyboard_unichar(vterm, c, mod);
1635
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001636 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001637 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1638}
1639
1640/*
1641 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001642 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001643 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001644 */
1645 static int
1646term_job_running_check(term_T *term, int check_job_status)
1647{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001648 // Also consider the job finished when the channel is closed, to avoid a
1649 // race condition when updating the title.
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001650 if (term == NULL
1651 || term->tl_job == NULL
1652 || !channel_is_open(term->tl_job->jv_channel))
1653 return FALSE;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001654
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001655 job_T *job = term->tl_job;
1656
1657 // Careful: Checking the job status may invoke callbacks, which close
1658 // the buffer and terminate "term". However, "job" will not be freed
1659 // yet.
1660 if (check_job_status)
1661 job_status(job);
1662 return (job->jv_status == JOB_STARTED
1663 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001664}
1665
1666/*
1667 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001668 */
1669 int
1670term_job_running(term_T *term)
1671{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001672 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001673}
1674
1675/*
Bram Moolenaar9e636b92022-05-29 22:37:05 +01001676 * Return TRUE if the job for "term" is still running, ignoring the job was
1677 * "NONE".
1678 */
1679 int
1680term_job_running_not_none(term_T *term)
1681{
1682 return term_job_running(term) && !term_none_open(term);
1683}
1684
1685/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001686 * Return TRUE if "term" has an active channel and used ":term NONE".
1687 */
1688 int
1689term_none_open(term_T *term)
1690{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001691 // Also consider the job finished when the channel is closed, to avoid a
1692 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001693 return term != NULL
1694 && term->tl_job != NULL
1695 && channel_is_open(term->tl_job->jv_channel)
1696 && term->tl_job->jv_channel->ch_keep_open;
1697}
1698
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001699//
1700// Used to confirm whether we would like to kill a terminal.
1701// Return OK when the user confirms to kill it.
1702// Return FAIL if the user selects otherwise.
1703//
1704 int
1705term_confirm_stop(buf_T *buf)
1706{
1707 char_u buff[DIALOG_MSG_SIZE];
1708 int ret;
1709
1710 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
1711 ret = vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1);
1712 if (ret == VIM_YES)
1713 return OK;
1714 else
1715 return FAIL;
1716}
1717
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001718/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001719 * Used when exiting: kill the job in "buf" if so desired.
1720 * Return OK when the job finished.
1721 * Return FAIL when the job is still running.
1722 */
1723 int
1724term_try_stop_job(buf_T *buf)
1725{
1726 int count;
1727 char *how = (char *)buf->b_term->tl_kill;
1728
1729#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001730 if ((how == NULL || *how == NUL)
1731 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001732 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001733 if (term_confirm_stop(buf) == OK)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001734 how = "kill";
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001735 else
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001736 return FAIL;
1737 }
1738#endif
1739 if (how == NULL || *how == NUL)
1740 return FAIL;
1741
1742 job_stop(buf->b_term->tl_job, NULL, how);
1743
Bram Moolenaar9172d232019-01-29 23:06:54 +01001744 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001745 for (count = 0; count < 100; ++count)
1746 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001747 job_T *job;
1748
1749 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001750 if (!buf_valid(buf)
1751 || buf->b_term == NULL
1752 || buf->b_term->tl_job == NULL)
1753 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001754 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001755
Bram Moolenaar9172d232019-01-29 23:06:54 +01001756 // Call job_status() to update jv_status. It may cause the job to be
1757 // cleaned up but it won't be freed.
1758 job_status(job);
1759 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001760 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001761
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001762 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001763 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001764 }
1765 return FAIL;
1766}
1767
1768/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001769 * Add the last line of the scrollback buffer to the buffer in the window.
1770 */
1771 static void
1772add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1773{
1774 buf_T *buf = term->tl_buffer;
1775 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1776 linenr_T lnum = buf->b_ml.ml_line_count;
1777
Bram Moolenaar4f974752019-02-17 17:44:42 +01001778#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001779 if (!enc_utf8 && enc_codepage > 0)
1780 {
1781 WCHAR *ret = NULL;
1782 int length = 0;
1783
1784 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1785 &ret, &length);
1786 if (ret != NULL)
1787 {
1788 WideCharToMultiByte_alloc(enc_codepage, 0,
1789 ret, length, (char **)&text, &len, 0, 0);
1790 vim_free(ret);
1791 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1792 vim_free(text);
1793 }
1794 }
1795 else
1796#endif
1797 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1798 if (empty)
1799 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001800 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001801 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001802 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001803 curbuf = curwin->w_buffer;
1804 }
1805}
1806
1807 static void
1808cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1809{
1810 attr->width = cell->width;
1811 attr->attrs = cell->attrs;
1812 attr->fg = cell->fg;
1813 attr->bg = cell->bg;
1814}
1815
1816 static int
1817equal_celattr(cellattr_T *a, cellattr_T *b)
1818{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001819 // We only compare the RGB colors, ignoring the ANSI index and type.
1820 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001821 return a->fg.red == b->fg.red
1822 && a->fg.green == b->fg.green
1823 && a->fg.blue == b->fg.blue
1824 && a->bg.red == b->bg.red
1825 && a->bg.green == b->bg.green
1826 && a->bg.blue == b->bg.blue;
1827}
1828
Bram Moolenaard96ff162018-02-18 22:13:29 +01001829/*
1830 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1831 * line at this position. Otherwise at the end.
1832 */
1833 static int
1834add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1835{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001836 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
1837 return FALSE;
1838
1839 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1840 + term->tl_scrollback.ga_len;
1841
1842 if (lnum > 0)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001843 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001844 int i;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001845
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001846 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001847 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001848 *line = *(line - 1);
1849 --line;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001850 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01001851 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00001852 line->sb_cols = 0;
1853 line->sb_cells = NULL;
1854 line->sb_fill_attr = *fill_attr;
1855 ++term->tl_scrollback.ga_len;
1856 return OK;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001857}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001858
1859/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001860 * Remove the terminal contents from the scrollback and the buffer.
1861 * Used before adding a new scrollback line or updating the buffer for lines
1862 * displayed in the terminal.
1863 */
1864 static void
1865cleanup_scrollback(term_T *term)
1866{
1867 sb_line_T *line;
1868 garray_T *gap;
1869
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001870 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001871 gap = &term->tl_scrollback;
1872 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1873 && gap->ga_len > 0)
1874 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001875 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001876 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1877 vim_free(line->sb_cells);
1878 --gap->ga_len;
1879 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001880 curbuf = curwin->w_buffer;
1881 if (curbuf == term->tl_buffer)
1882 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001883}
1884
1885/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001886 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001887 */
1888 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001889update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001890{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001891 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001892 int len;
1893 int lines_skipped = 0;
1894 VTermPos pos;
1895 VTermScreenCell cell;
1896 cellattr_T fill_attr, new_fill_attr;
1897 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001898
1899 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1900 "Adding terminal window snapshot to buffer");
1901
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001902 // First remove the lines that were appended before, they might be
1903 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001904 cleanup_scrollback(term);
1905
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001906 screen = vterm_obtain_screen(term->tl_vterm);
1907 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001908 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1909 {
1910 len = 0;
1911 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1912 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1913 && cell.chars[0] != NUL)
1914 {
1915 len = pos.col + 1;
1916 new_fill_attr = term->tl_default_color;
1917 }
1918 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001919 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001920 cell2cellattr(&cell, &new_fill_attr);
1921
1922 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1923 ++lines_skipped;
1924 else
1925 {
1926 while (lines_skipped > 0)
1927 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001928 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001929 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001930 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001931 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001932 }
1933
1934 if (len == 0)
1935 p = NULL;
1936 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001937 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001938 if ((p != NULL || len == 0)
1939 && ga_grow(&term->tl_scrollback, 1) == OK)
1940 {
1941 garray_T ga;
1942 int width;
1943 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1944 + term->tl_scrollback.ga_len;
1945
1946 ga_init2(&ga, 1, 100);
1947 for (pos.col = 0; pos.col < len; pos.col += width)
1948 {
1949 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1950 {
1951 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001952 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001953 if (ga_grow(&ga, 1) == OK)
1954 ga.ga_len += utf_char2bytes(' ',
1955 (char_u *)ga.ga_data + ga.ga_len);
1956 }
1957 else
1958 {
1959 width = cell.width;
1960
1961 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001962 if (width == 2)
1963 // second cell of double-width character has the
1964 // same attributes.
1965 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001966
Bram Moolenaara79fd562018-12-20 20:47:32 +01001967 // Each character can be up to 6 bytes.
1968 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001969 {
1970 int i;
1971 int c;
1972
1973 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1974 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1975 (char_u *)ga.ga_data + ga.ga_len);
1976 }
1977 }
1978 }
1979 line->sb_cols = len;
1980 line->sb_cells = p;
1981 line->sb_fill_attr = new_fill_attr;
1982 fill_attr = new_fill_attr;
1983 ++term->tl_scrollback.ga_len;
1984
1985 if (ga_grow(&ga, 1) == FAIL)
1986 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1987 else
1988 {
1989 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1990 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1991 }
1992 ga_clear(&ga);
1993 }
1994 else
1995 vim_free(p);
1996 }
1997 }
1998
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001999 // Add trailing empty lines.
2000 for (pos.row = term->tl_scrollback.ga_len;
2001 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
2002 ++pos.row)
2003 {
2004 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
2005 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
2006 }
2007
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002008 term->tl_dirty_snapshot = FALSE;
2009#ifdef FEAT_TIMERS
2010 term->tl_timer_set = FALSE;
2011#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002012}
2013
2014/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002015 * Loop over all windows in the current tab, and also curwin, which is not
2016 * encountered when using a terminal in a popup window.
2017 * Return TRUE if "*wp" was set to the next window.
2018 */
2019 static int
2020for_all_windows_and_curwin(win_T **wp, int *did_curwin)
2021{
2022 if (*wp == NULL)
2023 *wp = firstwin;
2024 else if ((*wp)->w_next != NULL)
2025 *wp = (*wp)->w_next;
2026 else if (!*did_curwin)
2027 *wp = curwin;
2028 else
2029 return FALSE;
2030 if (*wp == curwin)
2031 *did_curwin = TRUE;
2032 return TRUE;
2033}
2034
2035/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002036 * If needed, add the current lines of the terminal to scrollback and to the
2037 * buffer. Called after the job has ended and when switching to
2038 * Terminal-Normal mode.
2039 * When "redraw" is TRUE redraw the windows that show the terminal.
2040 */
2041 static void
2042may_move_terminal_to_buffer(term_T *term, int redraw)
2043{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002044 if (term->tl_vterm == NULL)
2045 return;
2046
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002047 // Update the snapshot only if something changes or the buffer does not
2048 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002049 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
2050 <= term->tl_scrollback_scrolled)
2051 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002052
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002053 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002054 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2055 &term->tl_default_color.fg, &term->tl_default_color.bg);
2056
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002057 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002058 {
2059 win_T *wp = NULL;
2060 int did_curwin = FALSE;
2061
2062 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002063 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002064 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002065 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002066 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
2067 wp->w_cursor.col = 0;
2068 wp->w_valid = 0;
2069 if (wp->w_cursor.lnum >= wp->w_height)
2070 {
2071 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002072
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002073 if (wp->w_topline < min_topline)
2074 wp->w_topline = min_topline;
2075 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002076 redraw_win_later(wp, UPD_NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002077 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002078 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002079 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002080}
2081
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002082#if defined(FEAT_TIMERS) || defined(PROTO)
2083/*
2084 * Check if any terminal timer expired. If so, copy text from the terminal to
2085 * the buffer.
2086 * Return the time until the next timer will expire.
2087 */
2088 int
2089term_check_timers(int next_due_arg, proftime_T *now)
2090{
2091 term_T *term;
2092 int next_due = next_due_arg;
2093
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002094 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002095 {
2096 if (term->tl_timer_set && !term->tl_normal_mode)
2097 {
2098 long this_due = proftime_time_left(&term->tl_timer_due, now);
2099
2100 if (this_due <= 1)
2101 {
2102 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002103 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002104 }
2105 else if (next_due == -1 || next_due > this_due)
2106 next_due = this_due;
2107 }
2108 }
2109
2110 return next_due;
2111}
2112#endif
2113
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002114/*
2115 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
2116 * otherwise end it.
2117 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002118 static void
2119set_terminal_mode(term_T *term, int normal_mode)
2120{
2121 term->tl_normal_mode = normal_mode;
LemonBoy2bf52dd2022-04-09 18:17:34 +01002122 may_trigger_modechanged();
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002123 if (!normal_mode)
2124 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002125 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002126 if (term->tl_buffer == curbuf)
2127 maketitle();
2128}
2129
2130/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002131 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002132 * Move the vterm contents into the scrollback buffer and free the vterm.
2133 */
2134 static void
2135cleanup_vterm(term_T *term)
2136{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002137 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002138 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002139 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002140 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002141}
2142
2143/*
2144 * Switch from Terminal-Job mode to Terminal-Normal mode.
2145 * Suspends updating the terminal window.
2146 */
2147 static void
2148term_enter_normal_mode(void)
2149{
2150 term_T *term = curbuf->b_term;
2151
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002152 set_terminal_mode(term, TRUE);
2153
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002154 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002155 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002156
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002157 // Move the window cursor to the position of the cursor in the
2158 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002159 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2160 + term->tl_cursor_pos.row + 1;
2161 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002162 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2163 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002164 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002165
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002166 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002167 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2168}
2169
2170/*
2171 * Returns TRUE if the current window contains a terminal and we are in
2172 * Terminal-Normal mode.
2173 */
2174 int
2175term_in_normal_mode(void)
2176{
2177 term_T *term = curbuf->b_term;
2178
2179 return term != NULL && term->tl_normal_mode;
2180}
2181
2182/*
2183 * Switch from Terminal-Normal mode to Terminal-Job mode.
2184 * Restores updating the terminal window.
2185 */
2186 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002187term_enter_job_mode(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002188{
2189 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002190
2191 set_terminal_mode(term, FALSE);
2192
2193 if (term->tl_channel_closed)
2194 cleanup_vterm(term);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002195 redraw_buf_and_status_later(curbuf, UPD_NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002196#ifdef FEAT_PROP_POPUP
2197 if (WIN_IS_POPUP(curwin))
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002198 redraw_later(UPD_NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002199#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002200}
2201
2202/*
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002203 * When "modify_other_keys" is set then vgetc() should not reduce a key with
2204 * modifiers into a basic key. However, we may only find out after calling
2205 * vgetc(). Therefore vgetorpeek() will call check_no_reduce_keys() to update
2206 * "no_reduce_keys" before using it.
2207 */
2208typedef enum {
2209 NRKS_NONE, // initial value
2210 NRKS_CHECK, // modify_other_keys was off before calling vgetc()
2211 NRKS_SET, // no_reduce_keys was incremented in term_vgetc() or
2212 // check_no_reduce_keys(), must be decremented.
2213} reduce_key_state_T;
2214
2215static reduce_key_state_T no_reduce_key_state = NRKS_NONE;
2216
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002217/*
2218 * Return TRUE if the term is using modifyOtherKeys level 2 or the kitty
2219 * keyboard protocol.
2220 */
2221 static int
2222vterm_using_key_protocol(void)
2223{
2224 return curbuf->b_term != NULL
2225 && curbuf->b_term->tl_vterm != NULL
2226 && (vterm_is_modify_other_keys(curbuf->b_term->tl_vterm)
2227 || vterm_is_kitty_keyboard(curbuf->b_term->tl_vterm));
2228}
2229
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002230 void
2231check_no_reduce_keys(void)
2232{
2233 if (no_reduce_key_state != NRKS_CHECK
2234 || no_reduce_keys >= 1
2235 || curbuf->b_term == NULL
2236 || curbuf->b_term->tl_vterm == NULL)
2237 return;
2238
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002239 if (vterm_using_key_protocol())
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002240 {
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002241 // "modify_other_keys" or kitty keyboard protocol was enabled while
2242 // waiting.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002243 no_reduce_key_state = NRKS_SET;
2244 ++no_reduce_keys;
2245 }
2246}
2247
2248/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002249 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002250 * Note: while waiting a terminal may be closed and freed if the channel is
Bram Moolenaar829c8e82021-12-14 08:41:38 +00002251 * closed and ++close was used. This may even happen before we get here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002252 */
2253 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002254term_vgetc(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002255{
2256 int c;
2257 int save_State = State;
2258
Bram Moolenaar24959102022-05-07 20:01:16 +01002259 State = MODE_TERMINAL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002260 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002261#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002262 ctrl_break_was_pressed = FALSE;
2263#endif
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002264
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002265 if (vterm_using_key_protocol())
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002266 {
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002267 ++no_reduce_keys;
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002268 no_reduce_key_state = NRKS_SET;
2269 }
2270 else
2271 {
2272 no_reduce_key_state = NRKS_CHECK;
2273 }
2274
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002275 c = vgetc();
2276 got_int = FALSE;
2277 State = save_State;
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002278
2279 if (no_reduce_key_state == NRKS_SET)
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002280 --no_reduce_keys;
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002281 no_reduce_key_state = NRKS_NONE;
2282
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002283 return c;
2284}
2285
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002286static int mouse_was_outside = FALSE;
2287
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002288/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002289 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002290 * Return FAIL when the key needs to be handled in Normal mode.
2291 * Return OK when the key was dropped or sent to the terminal.
2292 */
2293 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002294send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002295{
2296 char msg[KEY_BUF_LEN];
2297 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002298 int dragging_outside = FALSE;
2299
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002300 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002301 switch (c)
2302 {
2303 case NUL:
2304 case K_ZERO:
2305 if (typed)
2306 stuffcharReadbuff(c);
2307 return FAIL;
2308
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002309 case K_TABLINE:
2310 stuffcharReadbuff(c);
2311 return FAIL;
2312
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002313 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002314 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002315 return FAIL;
2316
2317 case K_LEFTDRAG:
2318 case K_MIDDLEDRAG:
2319 case K_RIGHTDRAG:
2320 case K_X1DRAG:
2321 case K_X2DRAG:
2322 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002323 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002324 case K_LEFTMOUSE:
2325 case K_LEFTMOUSE_NM:
2326 case K_LEFTRELEASE:
2327 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002328 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002329 case K_MIDDLEMOUSE:
2330 case K_MIDDLERELEASE:
2331 case K_RIGHTMOUSE:
2332 case K_RIGHTRELEASE:
2333 case K_X1MOUSE:
2334 case K_X1RELEASE:
2335 case K_X2MOUSE:
2336 case K_X2RELEASE:
2337
2338 case K_MOUSEUP:
2339 case K_MOUSEDOWN:
2340 case K_MOUSELEFT:
2341 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002342 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002343 int row = mouse_row;
2344 int col = mouse_col;
2345
2346#ifdef FEAT_PROP_POPUP
2347 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002348 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002349 row -= popup_top_extra(curwin);
2350 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002351 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002352#endif
2353 if (row < W_WINROW(curwin)
2354 || row >= (W_WINROW(curwin) + curwin->w_height)
2355 || col < curwin->w_wincol
2356 || col >= W_ENDCOL(curwin)
2357 || dragging_outside)
2358 {
2359 // click or scroll outside the current window or on status
2360 // line or vertical separator
2361 if (typed)
2362 {
2363 stuffcharReadbuff(c);
2364 mouse_was_outside = TRUE;
2365 }
2366 return FAIL;
2367 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002368 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002369 break;
2370
2371 case K_COMMAND:
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002372 case K_SCRIPT_COMMAND:
2373 return do_cmdkey_command(c, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002374 }
2375 if (typed)
2376 mouse_was_outside = FALSE;
2377
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002378 // Convert the typed key to a sequence of bytes for the job.
2379 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002380 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002381 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002382 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2383 (char_u *)msg, (int)len, NULL);
2384
2385 return OK;
2386}
2387
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002388/*
2389 * Handle CTRL-W "": send register contents to the job.
2390 */
2391 static void
2392term_paste_register(int prev_c UNUSED)
2393{
2394 int c;
2395 list_T *l;
2396 listitem_T *item;
2397 long reglen = 0;
2398 int type;
2399
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002400 if (add_to_showcmd(prev_c))
2401 if (add_to_showcmd('"'))
2402 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +01002403
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002404 c = term_vgetc();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002405 clear_showcmd();
Martin Tournoijba43e762022-10-13 22:12:15 +01002406
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002407 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002408 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002409 return;
2410
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002411 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002412 if (c == '=' && get_expr_register() != '=')
2413 return;
2414 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002415 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002416 return;
2417
2418 l = (list_T *)get_reg_contents(c, GREG_LIST);
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002419 if (l == NULL)
2420 return;
2421
2422 type = get_reg_type(c, &reglen);
2423 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002424 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002425 char_u *s = tv_get_string(&item->li_tv);
2426#ifdef MSWIN
2427 char_u *tmp = s;
2428
2429 if (!enc_utf8 && enc_codepage > 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002430 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002431 WCHAR *ret = NULL;
2432 int length = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002433
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002434 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2435 (int)STRLEN(s), &ret, &length);
2436 if (ret != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002437 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002438 WideCharToMultiByte_alloc(CP_UTF8, 0,
2439 ret, length, (char **)&s, &length, 0, 0);
2440 vim_free(ret);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002441 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002442 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002443#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002444 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2445 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002446#ifdef MSWIN
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002447 if (tmp != s)
2448 vim_free(s);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002449#endif
2450
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002451 if (item->li_next != NULL || type == MLINE)
2452 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2453 (char_u *)"\r", 1, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002454 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002455 list_free(l);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002456}
2457
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002458/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002459 * Return TRUE when waiting for a character in the terminal, the cursor of the
2460 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002461 */
2462 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002463terminal_is_active(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002464{
2465 return in_terminal_loop != NULL;
2466}
2467
Bram Moolenaar83d47902020-03-26 20:34:00 +01002468/*
dundargocc57b5bc2022-11-02 13:30:51 +00002469 * Return the highlight group ID for the terminal and the window.
Bram Moolenaar83d47902020-03-26 20:34:00 +01002470 */
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002471 static int
2472term_get_highlight_id(term_T *term, win_T *wp)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002473{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002474 char_u *name;
2475
2476 if (wp != NULL && *wp->w_p_wcr != NUL)
2477 name = wp->w_p_wcr;
2478 else if (term->tl_highlight_name != NULL)
2479 name = term->tl_highlight_name;
2480 else
2481 name = (char_u*)"Terminal";
2482
2483 return syn_name2id(name);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002484}
2485
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002486#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002487 cursorentry_T *
2488term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2489{
2490 term_T *term = in_terminal_loop;
2491 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002492 int id;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002493 guicolor_T term_fg = INVALCOLOR;
2494 guicolor_T term_bg = INVALCOLOR;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002495
Bram Moolenaara80faa82020-04-12 19:37:17 +02002496 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002497 entry.shape = entry.mshape =
2498 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2499 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2500 SHAPE_BLOCK;
2501 entry.percentage = 20;
2502 if (term->tl_cursor_blink)
2503 {
2504 entry.blinkwait = 700;
2505 entry.blinkon = 400;
2506 entry.blinkoff = 250;
2507 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002508
Bram Moolenaar83d47902020-03-26 20:34:00 +01002509 // The highlight group overrules the defaults.
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002510 id = term_get_highlight_id(term, curwin);
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002511 if (id != 0)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002512 syn_id2colors(id, &term_fg, &term_bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002513 if (term_bg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002514 *fg = term_bg;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002515 else
2516 *fg = gui.back_pixel;
2517
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002518 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002519 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002520 if (term_fg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002521 *bg = term_fg;
2522 else
2523 *bg = gui.norm_pixel;
2524 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002525 else
2526 *bg = color_name2handle(term->tl_cursor_color);
2527 entry.name = "n";
2528 entry.used_for = SHAPE_CURSOR;
2529
2530 return &entry;
2531}
2532#endif
2533
Bram Moolenaard317b382018-02-08 22:33:31 +01002534 static void
2535may_output_cursor_props(void)
2536{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002537 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002538 || last_set_cursor_shape != desired_cursor_shape
2539 || last_set_cursor_blink != desired_cursor_blink)
2540 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002541 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002542 last_set_cursor_shape = desired_cursor_shape;
2543 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002544 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002545 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002546 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002547 ui_cursor_shape_forced(TRUE);
2548 else
2549 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2550 }
2551}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002552
Bram Moolenaard317b382018-02-08 22:33:31 +01002553/*
2554 * Set the cursor color and shape, if not last set to these.
2555 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002556 static void
2557may_set_cursor_props(term_T *term)
2558{
2559#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002560 // For the GUI the cursor properties are obtained with
2561 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002562 if (gui.in_use)
2563 return;
2564#endif
2565 if (in_terminal_loop == term)
2566 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002567 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002568 desired_cursor_shape = term->tl_cursor_shape;
2569 desired_cursor_blink = term->tl_cursor_blink;
2570 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002571 }
2572}
2573
Bram Moolenaard317b382018-02-08 22:33:31 +01002574/*
2575 * Reset the desired cursor properties and restore them when needed.
2576 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002577 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002578prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002579{
2580#ifdef FEAT_GUI
2581 if (gui.in_use)
2582 return;
2583#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002584 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002585 desired_cursor_shape = -1;
2586 desired_cursor_blink = -1;
2587 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002588}
2589
2590/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002591 * Returns TRUE if the current window contains a terminal and we are sending
2592 * keys to the job.
2593 * If "check_job_status" is TRUE update the job status.
2594 */
2595 static int
2596term_use_loop_check(int check_job_status)
2597{
2598 term_T *term = curbuf->b_term;
2599
2600 return term != NULL
2601 && !term->tl_normal_mode
2602 && term->tl_vterm != NULL
2603 && term_job_running_check(term, check_job_status);
2604}
2605
2606/*
2607 * Returns TRUE if the current window contains a terminal and we are sending
2608 * keys to the job.
2609 */
2610 int
2611term_use_loop(void)
2612{
2613 return term_use_loop_check(FALSE);
2614}
2615
2616/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002617 * Called when entering a window with the mouse. If this is a terminal window
2618 * we may want to change state.
2619 */
2620 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002621term_win_entered(void)
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002622{
2623 term_T *term = curbuf->b_term;
2624
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002625 if (term == NULL)
2626 return;
2627
2628 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002629 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002630 reset_VIsual_and_resel();
2631 if (State & MODE_INSERT)
2632 stop_insert_mode = TRUE;
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002633 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002634 mouse_was_outside = FALSE;
2635 enter_mouse_col = mouse_col;
2636 enter_mouse_row = mouse_row;
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002637}
2638
Bram Moolenaara48d4e42021-12-08 22:13:38 +00002639 void
2640term_focus_change(int in_focus)
2641{
2642 term_T *term = curbuf->b_term;
2643
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002644 if (term == NULL || term->tl_vterm == NULL)
2645 return;
Bram Moolenaara48d4e42021-12-08 22:13:38 +00002646
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002647 VTermState *state = vterm_obtain_state(term->tl_vterm);
2648
2649 if (in_focus)
2650 vterm_state_focus_in(state);
2651 else
2652 vterm_state_focus_out(state);
2653 term_forward_output(term);
Bram Moolenaara48d4e42021-12-08 22:13:38 +00002654}
2655
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002656/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002657 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2658 * Return the Ctrl-key value in that case.
2659 */
2660 static int
2661raw_c_to_ctrl(int c)
2662{
2663 if ((mod_mask & MOD_MASK_CTRL)
2664 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2665 return c & 0x1f;
2666 return c;
2667}
2668
2669/*
2670 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002671 * Also when the Kitty keyboard protocol is used.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002672 * May set "mod_mask".
2673 */
2674 static int
2675ctrl_to_raw_c(int c)
2676{
Bram Moolenaar63a2e362022-11-23 20:20:18 +00002677 if (c < 0x20 && vterm_using_key_protocol())
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002678 {
2679 mod_mask |= MOD_MASK_CTRL;
2680 return c + '@';
2681 }
2682 return c;
2683}
2684
2685/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002686 * Wait for input and send it to the job.
2687 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2688 * when there is no more typahead.
2689 * Return when the start of a CTRL-W command is typed or anything else that
2690 * should be handled as a Normal mode command.
2691 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2692 * the terminal was closed.
2693 */
2694 int
2695terminal_loop(int blocking)
2696{
2697 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002698 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002699 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002700 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002701#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002702 int tty_fd = curbuf->b_term->tl_job->jv_channel
2703 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002704#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002705 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002706
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002707 // Remember the terminal we are sending keys to. However, the terminal
2708 // might be closed while waiting for a character, e.g. typing "exit" in a
2709 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2710 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002711 in_terminal_loop = curbuf->b_term;
2712
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002713 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002714 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002715 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002716 if (termwinkey == Ctrl_W)
2717 termwinkey = 0;
2718 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002719 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002720 may_set_cursor_props(curbuf->b_term);
2721
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002722 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002723 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002724#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002725 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002726#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002727 // TODO: skip screen update when handling a sequence of keys.
2728 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002729 while (must_redraw != 0)
2730 if (update_screen(0) == FAIL)
2731 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002732 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002733 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002734 break;
2735
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002736 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002737 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002738
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002739 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002740 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002741 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002742 // Job finished while waiting for a character. Push back the
2743 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002744 if (raw_c != K_IGNORE)
2745 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002746 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002747 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002748 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002749 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002750 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002751
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002752#ifdef UNIX
2753 /*
2754 * The shell or another program may change the tty settings. Getting
2755 * them for every typed character is a bit of overhead, but it's needed
2756 * for the first character typed, e.g. when Vim starts in a shell.
2757 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002758 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002759 {
2760 ttyinfo_T info;
2761
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002762 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002763 if (get_tty_info(tty_fd, &info) == OK)
2764 term_backspace_char = info.backspace;
2765 }
2766#endif
2767
Bram Moolenaar4f974752019-02-17 17:44:42 +01002768#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002769 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2770 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002771 if (ctrl_break_was_pressed)
2772 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2773#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002774 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2775 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002776 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002777#ifdef FEAT_GUI
2778 && !curbuf->b_term->tl_system
2779#endif
2780 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002781 {
2782 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002783 int prev_raw_c = raw_c;
2784 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002785
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002786 if (add_to_showcmd(c))
2787 out_flush();
Martin Tournoijba43e762022-10-13 22:12:15 +01002788
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002789 raw_c = term_vgetc();
2790 c = raw_c_to_ctrl(raw_c);
2791
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002792 clear_showcmd();
Martin Tournoijba43e762022-10-13 22:12:15 +01002793
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002794 if (!term_use_loop_check(TRUE)
2795 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002796 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002797 break;
2798
2799 if (prev_c == Ctrl_BSL)
2800 {
2801 if (c == Ctrl_N)
2802 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002803 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002804 term_enter_normal_mode();
2805 ret = FAIL;
2806 goto theend;
2807 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002808 // Send both keys to the terminal, first one here, second one
2809 // below.
2810 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2811 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002812 }
2813 else if (c == Ctrl_C)
2814 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002815 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002816 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2817 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002818 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002819 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002820 // "CTRL-W .": send CTRL-W to the job
2821 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002822 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002823 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002824 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002825 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002826 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002827 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002828 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002829 else if (c == 'N')
2830 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002831 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002832 term_enter_normal_mode();
2833 ret = FAIL;
2834 goto theend;
2835 }
2836 else if (c == '"')
2837 {
2838 term_paste_register(prev_c);
2839 continue;
2840 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002841 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002842 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002843 // space for CTRL-W, modifier, multi-byte char and NUL
2844 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002845
2846 // Put the command into the typeahead buffer, when using the
2847 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2848 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002849 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002850 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002851 ret = OK;
2852 goto theend;
2853 }
2854 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002855# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002856 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002857 {
2858 WCHAR wc;
2859 char_u mb[3];
2860
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002861 mb[0] = (unsigned)raw_c >> 8;
2862 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002863 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002864 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002865 }
2866# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002867 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002868 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002869 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002870 // We are sure to come back here, don't reset the cursor color
2871 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002872 restore_cursor = FALSE;
2873
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002874 ret = OK;
2875 goto theend;
2876 }
2877 }
2878 ret = FAIL;
2879
2880theend:
2881 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002882 if (restore_cursor)
2883 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002884
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002885 // Move a snapshot of the screen contents to the buffer, so that completion
2886 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002887 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2888 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002889
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002890 return ret;
2891}
2892
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002893 static void
2894may_toggle_cursor(term_T *term)
2895{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00002896 if (in_terminal_loop != term)
2897 return;
2898
2899 if (term->tl_cursor_visible)
2900 cursor_on();
2901 else
2902 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002903}
2904
2905/*
2906 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002907 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002908 */
2909 static int
2910color2index(VTermColor *color, int fg, int *boldp)
2911{
2912 int red = color->red;
2913 int blue = color->blue;
2914 int green = color->green;
2915
LemonBoyb2b3acb2022-05-20 10:10:34 +01002916 *boldp = FALSE;
2917
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002918 if (VTERM_COLOR_IS_INVALID(color))
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002919 return 0;
LemonBoyb2b3acb2022-05-20 10:10:34 +01002920
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002921 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002922 {
LemonBoyb2b3acb2022-05-20 10:10:34 +01002923 // Use the color as-is if possible, give up otherwise.
2924 if (color->index < t_colors)
2925 return color->index + 1;
2926 // 8-color terminals can actually display twice as many colors by
2927 // setting the high-intensity/bold bit.
2928 else if (t_colors == 8 && fg && color->index < 16)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002929 {
LemonBoyb2b3acb2022-05-20 10:10:34 +01002930 *boldp = TRUE;
2931 return (color->index & 7) + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002932 }
LemonBoyb2b3acb2022-05-20 10:10:34 +01002933 return 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002934 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002935
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002936 if (t_colors >= 256)
2937 {
2938 if (red == blue && red == green)
2939 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002940 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002941 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002942 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2943 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2944 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002945 int i;
2946
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002947 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002948 return 17; // 00/00/00
2949 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002950 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002951 for (i = 0; i < 23; ++i)
2952 if (red < cutoff[i])
2953 return i + 233;
2954 return 256;
2955 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002956 {
2957 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2958 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002959
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002960 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002961 for (ri = 0; ri < 5; ++ri)
2962 if (red < cutoff[ri])
2963 break;
2964 for (gi = 0; gi < 5; ++gi)
2965 if (green < cutoff[gi])
2966 break;
2967 for (bi = 0; bi < 5; ++bi)
2968 if (blue < cutoff[bi])
2969 break;
2970 return 17 + ri * 36 + gi * 6 + bi;
2971 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002972 }
2973 return 0;
2974}
2975
2976/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002977 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002978 */
2979 static int
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002980vtermAttr2hl(VTermScreenCellAttrs *cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002981{
2982 int attr = 0;
2983
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002984 if (cellattrs->bold)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002985 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002986 if (cellattrs->underline)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002987 attr |= HL_UNDERLINE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002988 if (cellattrs->italic)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002989 attr |= HL_ITALIC;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002990 if (cellattrs->strike)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002991 attr |= HL_STRIKETHROUGH;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002992 if (cellattrs->reverse)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002993 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002994 return attr;
2995}
2996
2997/*
2998 * Store Vterm attributes in "cell" from highlight flags.
2999 */
3000 static void
3001hl2vtermAttr(int attr, cellattr_T *cell)
3002{
Bram Moolenaara80faa82020-04-12 19:37:17 +02003003 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01003004 if (attr & HL_BOLD)
3005 cell->attrs.bold = 1;
3006 if (attr & HL_UNDERLINE)
3007 cell->attrs.underline = 1;
3008 if (attr & HL_ITALIC)
3009 cell->attrs.italic = 1;
3010 if (attr & HL_STRIKETHROUGH)
3011 cell->attrs.strike = 1;
3012 if (attr & HL_INVERSE)
3013 cell->attrs.reverse = 1;
3014}
3015
3016/*
3017 * Convert the attributes of a vterm cell into an attribute index.
3018 */
3019 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003020cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003021 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003022 win_T *wp,
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003023 VTermScreenCellAttrs *cellattrs,
3024 VTermColor *cellfg,
3025 VTermColor *cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01003026{
3027 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003028 VTermColor *fg = cellfg;
3029 VTermColor *bg = cellbg;
3030 int is_default_fg = VTERM_COLOR_IS_DEFAULT_FG(fg);
3031 int is_default_bg = VTERM_COLOR_IS_DEFAULT_BG(bg);
3032
3033 if (is_default_fg || is_default_bg)
3034 {
3035 if (wp != NULL && *wp->w_p_wcr != NUL)
3036 {
3037 if (is_default_fg)
3038 fg = &wp->w_term_wincolor.fg;
3039 if (is_default_bg)
3040 bg = &wp->w_term_wincolor.bg;
3041 }
3042 else
3043 {
3044 if (is_default_fg)
3045 fg = &term->tl_default_color.fg;
3046 if (is_default_bg)
3047 bg = &term->tl_default_color.bg;
3048 }
3049 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003050
3051#ifdef FEAT_GUI
3052 if (gui.in_use)
3053 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003054 guicolor_T guifg = gui_mch_get_rgb_color(fg->red, fg->green, fg->blue);
3055 guicolor_T guibg = gui_mch_get_rgb_color(bg->red, bg->green, bg->blue);
3056 return get_gui_attr_idx(attr, guifg, guibg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003057 }
3058 else
3059#endif
3060#ifdef FEAT_TERMGUICOLORS
3061 if (p_tgc)
3062 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003063 guicolor_T tgcfg = VTERM_COLOR_IS_INVALID(fg)
3064 ? INVALCOLOR
3065 : gui_get_rgb_color_cmn(fg->red, fg->green, fg->blue);
3066 guicolor_T tgcbg = VTERM_COLOR_IS_INVALID(bg)
3067 ? INVALCOLOR
3068 : gui_get_rgb_color_cmn(bg->red, bg->green, bg->blue);
3069 return get_tgc_attr_idx(attr, tgcfg, tgcbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003070 }
3071 else
3072#endif
3073 {
3074 int bold = MAYBE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003075 int ctermfg = color2index(fg, TRUE, &bold);
3076 int ctermbg = color2index(bg, FALSE, &bold);
Bram Moolenaar76bb7192017-11-30 22:07:07 +01003077
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003078 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003079 if (bold == TRUE)
3080 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003081
3082 return get_cterm_attr_idx(attr, ctermfg, ctermbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003083 }
3084 return 0;
3085}
3086
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003087 static void
3088set_dirty_snapshot(term_T *term)
3089{
3090 term->tl_dirty_snapshot = TRUE;
3091#ifdef FEAT_TIMERS
3092 if (!term->tl_normal_mode)
3093 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003094 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003095 profile_setlimit(100L, &term->tl_timer_due);
3096 term->tl_timer_set = TRUE;
3097 }
3098#endif
3099}
3100
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003101 static int
3102handle_damage(VTermRect rect, void *user)
3103{
3104 term_T *term = (term_T *)user;
3105
3106 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
3107 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003108 set_dirty_snapshot(term);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003109 redraw_buf_later(term->tl_buffer, UPD_SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003110 return 1;
3111}
3112
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003113 static void
3114term_scroll_up(term_T *term, int start_row, int count)
3115{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003116 win_T *wp = NULL;
3117 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003118 VTermColor fg, bg;
3119 VTermScreenCellAttrs attr;
3120 int clear_attr;
3121
Bram Moolenaara80faa82020-04-12 19:37:17 +02003122 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003123
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003124 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003125 {
3126 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003127 {
3128 // Set the color to clear lines with.
3129 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
3130 &fg, &bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003131 clear_attr = cell2attr(term, wp, &attr, &fg, &bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003132 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003133 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003134 }
3135}
3136
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003137 static int
3138handle_moverect(VTermRect dest, VTermRect src, void *user)
3139{
3140 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003141 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003142
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003143 // Scrolling up is done much more efficiently by deleting lines instead of
3144 // redrawing the text. But avoid doing this multiple times, postpone until
3145 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003146 if (dest.start_col == src.start_col
3147 && dest.end_col == src.end_col
3148 && dest.start_row < src.start_row)
3149 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003150 if (dest.start_row == 0)
3151 term->tl_postponed_scroll += count;
3152 else
3153 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003154 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003155
3156 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3157 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003158 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003159
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003160 // Note sure if the scrolling will work correctly, let's do a complete
3161 // redraw later.
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003162 redraw_buf_later(term->tl_buffer, UPD_NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003163 return 1;
3164}
3165
3166 static int
3167handle_movecursor(
3168 VTermPos pos,
3169 VTermPos oldpos UNUSED,
3170 int visible,
3171 void *user)
3172{
3173 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003174 win_T *wp = NULL;
3175 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003176
3177 term->tl_cursor_pos = pos;
3178 term->tl_cursor_visible = visible;
3179
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003180 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003181 {
3182 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003183 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003184 }
3185 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003186 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003187
3188 return 1;
3189}
3190
3191 static int
3192handle_settermprop(
3193 VTermProp prop,
3194 VTermValue *value,
3195 void *user)
3196{
3197 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003198 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003199
3200 switch (prop)
3201 {
3202 case VTERM_PROP_TITLE:
ichizokae1bd872022-01-20 14:57:29 +00003203 if (disable_vterm_title_for_testing)
3204 break;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003205 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003206 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003207 if (strval == NULL)
3208 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003209 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003210 // a blank title isn't useful, make it empty, so that "running" is
3211 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003212 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003213 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003214 // Same as blank
3215 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003216 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003217 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3218 term->tl_title = NULL;
3219 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003220 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003221 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003222#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003223 else if (!enc_utf8 && enc_codepage > 0)
3224 {
3225 WCHAR *ret = NULL;
3226 int length = 0;
3227
3228 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003229 (char*)value->string.str,
3230 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003231 if (ret != NULL)
3232 {
3233 WideCharToMultiByte_alloc(enc_codepage, 0,
3234 ret, length, (char**)&term->tl_title,
3235 &length, 0, 0);
3236 vim_free(ret);
3237 }
3238 }
3239#endif
3240 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003241 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003242 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003243 strval = NULL;
3244 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003245 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003246 if (term == curbuf->b_term)
LemonBoy327e6dd2022-06-04 19:57:59 +01003247 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003248 maketitle();
LemonBoy327e6dd2022-06-04 19:57:59 +01003249 curwin->w_redr_status = TRUE;
3250 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003251 break;
3252
3253 case VTERM_PROP_CURSORVISIBLE:
3254 term->tl_cursor_visible = value->boolean;
3255 may_toggle_cursor(term);
3256 out_flush();
3257 break;
3258
3259 case VTERM_PROP_CURSORBLINK:
3260 term->tl_cursor_blink = value->boolean;
3261 may_set_cursor_props(term);
3262 break;
3263
3264 case VTERM_PROP_CURSORSHAPE:
3265 term->tl_cursor_shape = value->number;
3266 may_set_cursor_props(term);
3267 break;
3268
3269 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003270 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003271 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003272 if (strval == NULL)
3273 break;
3274 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003275 may_set_cursor_props(term);
3276 break;
3277
3278 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003279 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003280 term->tl_using_altscreen = value->boolean;
3281 break;
3282
3283 default:
3284 break;
3285 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003286 vim_free(strval);
3287
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003288 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003289 return 1;
3290}
3291
3292/*
3293 * The job running in the terminal resized the terminal.
3294 */
3295 static int
3296handle_resize(int rows, int cols, void *user)
3297{
3298 term_T *term = (term_T *)user;
3299 win_T *wp;
3300
3301 term->tl_rows = rows;
3302 term->tl_cols = cols;
3303 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003304 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003305 term->tl_vterm_size_changed = FALSE;
3306 else
3307 {
3308 FOR_ALL_WINDOWS(wp)
3309 {
3310 if (wp->w_buffer == term->tl_buffer)
3311 {
3312 win_setheight_win(rows, wp);
3313 win_setwidth_win(cols, wp);
3314 }
3315 }
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003316 redraw_buf_later(term->tl_buffer, UPD_NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003317 }
3318 return 1;
3319}
3320
3321/*
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01003322 * If the number of lines that are stored goes over 'termwinscroll' then
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003323 * delete the first 10%.
3324 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3325 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003326 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003327 static void
3328limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003329{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003330 if (gap->ga_len < term->tl_buffer->b_p_twsl)
3331 return;
3332
3333 int todo = term->tl_buffer->b_p_twsl / 10;
3334 int i;
3335
3336 curbuf = term->tl_buffer;
3337 for (i = 0; i < todo; ++i)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003338 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003339 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003340 if (update_buffer)
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003341 ml_delete(1);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003342 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003343 curbuf = curwin->w_buffer;
3344
3345 gap->ga_len -= todo;
3346 mch_memmove(gap->ga_data,
3347 (sb_line_T *)gap->ga_data + todo,
3348 sizeof(sb_line_T) * gap->ga_len);
3349 if (update_buffer)
3350 term->tl_scrollback_scrolled -= todo;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003351}
3352
3353/*
3354 * Handle a line that is pushed off the top of the screen.
3355 */
3356 static int
3357handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3358{
3359 term_T *term = (term_T *)user;
3360 garray_T *gap;
3361 int update_buffer;
3362
3363 if (term->tl_normal_mode)
3364 {
3365 // In Terminal-Normal mode the user interacts with the buffer, thus we
3366 // must not change it. Postpone adding the scrollback lines.
3367 gap = &term->tl_scrollback_postponed;
3368 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003369 }
3370 else
3371 {
3372 // First remove the lines that were appended before, the pushed line
3373 // goes above it.
3374 cleanup_scrollback(term);
3375 gap = &term->tl_scrollback;
3376 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003377 }
3378
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003379 limit_scrollback(term, gap, update_buffer);
3380
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003381 if (ga_grow(gap, 1) == FAIL)
3382 return 0;
3383
3384 cellattr_T *p = NULL;
3385 int len = 0;
3386 int i;
3387 int c;
3388 int col;
3389 int text_len;
3390 char_u *text;
3391 sb_line_T *line;
3392 garray_T ga;
3393 cellattr_T fill_attr = term->tl_default_color;
3394
3395 // do not store empty cells at the end
3396 for (i = 0; i < cols; ++i)
3397 if (cells[i].chars[0] != 0)
3398 len = i + 1;
3399 else
3400 cell2cellattr(&cells[i], &fill_attr);
3401
3402 ga_init2(&ga, 1, 100);
3403 if (len > 0)
3404 p = ALLOC_MULT(cellattr_T, len);
3405 if (p != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003406 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003407 for (col = 0; col < len; col += cells[col].width)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003408 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003409 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003410 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003411 ga.ga_len = 0;
3412 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003413 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003414 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3415 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3416 (char_u *)ga.ga_data + ga.ga_len);
3417 cell2cellattr(&cells[col], &p[col]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003418 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003419 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003420 if (ga_grow(&ga, 1) == FAIL)
3421 {
3422 if (update_buffer)
3423 text = (char_u *)"";
3424 else
3425 text = vim_strsave((char_u *)"");
3426 text_len = 0;
3427 }
3428 else
3429 {
3430 text = ga.ga_data;
3431 text_len = ga.ga_len;
3432 *(text + text_len) = NUL;
3433 }
3434 if (update_buffer)
3435 add_scrollback_line_to_buffer(term, text, text_len);
3436
3437 line = (sb_line_T *)gap->ga_data + gap->ga_len;
3438 line->sb_cols = len;
3439 line->sb_cells = p;
3440 line->sb_fill_attr = fill_attr;
3441 if (update_buffer)
3442 {
3443 line->sb_text = NULL;
3444 ++term->tl_scrollback_scrolled;
3445 ga_clear(&ga); // free the text
3446 }
3447 else
3448 {
3449 line->sb_text = text;
3450 ga_init(&ga); // text is kept in tl_scrollback_postponed
3451 }
3452 ++gap->ga_len;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003453 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003454}
3455
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003456/*
3457 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3458 * received and stored in tl_scrollback_postponed.
3459 */
3460 static void
3461handle_postponed_scrollback(term_T *term)
3462{
3463 int i;
3464
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003465 if (term->tl_scrollback_postponed.ga_len == 0)
3466 return;
3467 ch_log(NULL, "Moving postponed scrollback to scrollback");
3468
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003469 // First remove the lines that were appended before, the pushed lines go
3470 // above it.
3471 cleanup_scrollback(term);
3472
3473 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3474 {
3475 char_u *text;
3476 sb_line_T *pp_line;
3477 sb_line_T *line;
3478
3479 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3480 break;
3481 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3482
3483 text = pp_line->sb_text;
3484 if (text == NULL)
3485 text = (char_u *)"";
3486 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3487 vim_free(pp_line->sb_text);
3488
3489 line = (sb_line_T *)term->tl_scrollback.ga_data
3490 + term->tl_scrollback.ga_len;
3491 line->sb_cols = pp_line->sb_cols;
3492 line->sb_cells = pp_line->sb_cells;
3493 line->sb_fill_attr = pp_line->sb_fill_attr;
3494 line->sb_text = NULL;
3495 ++term->tl_scrollback_scrolled;
3496 ++term->tl_scrollback.ga_len;
3497 }
3498
3499 ga_clear(&term->tl_scrollback_postponed);
3500 limit_scrollback(term, &term->tl_scrollback, TRUE);
3501}
3502
LemonBoy77771d32022-04-13 11:47:25 +01003503/*
3504 * Called when the terminal wants to ring the system bell.
3505 */
3506 static int
3507handle_bell(void *user UNUSED)
3508{
Bram Moolenaaraae97622022-04-13 14:28:07 +01003509 vim_beep(BO_TERM);
LemonBoy77771d32022-04-13 11:47:25 +01003510 return 0;
3511}
3512
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003513static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003514 handle_damage, // damage
3515 handle_moverect, // moverect
3516 handle_movecursor, // movecursor
3517 handle_settermprop, // settermprop
LemonBoy77771d32022-04-13 11:47:25 +01003518 handle_bell, // bell
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003519 handle_resize, // resize
3520 handle_pushline, // sb_pushline
Bram Moolenaar6a12d262022-10-16 19:26:52 +01003521 NULL, // sb_popline
3522 NULL // sb_clear
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003523};
3524
3525/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003526 * Do the work after the channel of a terminal was closed.
3527 * Must be called only when updating_screen is FALSE.
3528 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3529 */
3530 static int
3531term_after_channel_closed(term_T *term)
3532{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003533 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003534 if (!term->tl_normal_mode)
3535 {
3536 int fnum = term->tl_buffer->b_fnum;
3537
3538 cleanup_vterm(term);
3539
3540 if (term->tl_finish == TL_FINISH_CLOSE)
3541 {
3542 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003543 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003544#ifdef FEAT_PROP_POPUP
3545 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003546
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003547 // If this was a terminal in a popup window, go back to the
3548 // previous window.
3549 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3550 {
3551 pwin = curwin;
3552 if (win_valid(prevwin))
3553 win_enter(prevwin, FALSE);
3554 }
3555 else
3556#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003557 // If this is the last normal window: exit Vim.
3558 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3559 {
3560 exarg_T ea;
3561
Bram Moolenaara80faa82020-04-12 19:37:17 +02003562 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003563 ex_quit(&ea);
3564 return TRUE;
3565 }
3566
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003567 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003568 ch_log(NULL, "terminal job finished, closing window");
3569 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaare76062c2022-11-28 18:51:43 +00003570 if (curbuf == term->tl_buffer)
3571 {
3572 // Avoid closing the window if we temporarily use it.
3573 if (is_aucmd_win(curwin))
3574 do_set_w_closing = TRUE;
3575 if (do_set_w_closing)
3576 curwin->w_closing = TRUE;
3577 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
3578 if (do_set_w_closing)
3579 curwin->w_closing = FALSE;
3580 aucmd_restbuf(&aco);
3581 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003582#ifdef FEAT_PROP_POPUP
3583 if (pwin != NULL)
3584 popup_close_with_retval(pwin, 0);
3585#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003586 return TRUE;
3587 }
3588 if (term->tl_finish == TL_FINISH_OPEN
3589 && term->tl_buffer->b_nwindows == 0)
3590 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003591 char *cmd = term->tl_opencmd == NULL
3592 ? "botright sbuf %d"
3593 : (char *)term->tl_opencmd;
3594 size_t len = strlen(cmd) + 50;
3595 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003596
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003597 if (buf != NULL)
3598 {
3599 ch_log(NULL, "terminal job finished, opening window");
3600 vim_snprintf(buf, len, cmd, fnum);
3601 do_cmdline_cmd((char_u *)buf);
3602 vim_free(buf);
3603 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003604 }
3605 else
3606 ch_log(NULL, "terminal job finished");
3607 }
3608
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003609 redraw_buf_and_status_later(term->tl_buffer, UPD_NOT_VALID);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003610 return FALSE;
3611}
3612
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003613#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3614/*
3615 * If the current window is a terminal in a popup window and the job has
3616 * finished, close the popup window and to back to the previous window.
3617 * Otherwise return FAIL.
3618 */
3619 int
3620may_close_term_popup(void)
3621{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003622 if (!popup_is_popup(curwin) || curbuf->b_term == NULL
3623 || term_job_running_not_none(curbuf->b_term))
3624 return FAIL;
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003625
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00003626 win_T *pwin = curwin;
3627
3628 if (win_valid(prevwin))
3629 win_enter(prevwin, FALSE);
3630 popup_close_with_retval(pwin, 0);
3631 return OK;
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003632}
3633#endif
3634
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003635/*
Bram Moolenaareea32af2021-11-21 14:51:13 +00003636 * Called when a channel is going to be closed, before invoking the close
3637 * callback.
3638 */
3639 void
3640term_channel_closing(channel_T *ch)
3641{
3642 term_T *term;
3643
3644 for (term = first_term; term != NULL; term = term->tl_next)
3645 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
3646 term->tl_channel_closing = TRUE;
3647}
3648
3649/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003650 * Called when a channel has been closed.
3651 * If this was a channel for a terminal window then finish it up.
3652 */
3653 void
3654term_channel_closed(channel_T *ch)
3655{
3656 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003657 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003658 int did_one = FALSE;
3659
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003660 for (term = first_term; term != NULL; term = next_term)
3661 {
3662 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003663 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003664 {
3665 term->tl_channel_closed = TRUE;
3666 did_one = TRUE;
3667
Bram Moolenaard23a8232018-02-10 18:45:26 +01003668 VIM_CLEAR(term->tl_title);
3669 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003670#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003671 if (term->tl_out_fd != NULL)
3672 {
3673 fclose(term->tl_out_fd);
3674 term->tl_out_fd = NULL;
3675 }
3676#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003677
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003678 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003679 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003680 // Cannot open or close windows now. Can happen when
3681 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003682 term->tl_channel_recently_closed = TRUE;
3683 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003684 }
3685
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003686 if (term_after_channel_closed(term))
3687 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003688 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003689 }
3690
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003691 if (did_one)
3692 {
3693 redraw_statuslines();
3694
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003695 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003696 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003697 typebuf_was_filled = TRUE;
3698
3699 term = curbuf->b_term;
3700 if (term != NULL)
3701 {
3702 if (term->tl_job == ch->ch_job)
3703 maketitle();
3704 update_cursor(term, term->tl_cursor_visible);
3705 }
3706 }
3707}
3708
3709/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003710 * To be called after resetting updating_screen: handle any terminal where the
3711 * channel was closed.
3712 */
3713 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00003714term_check_channel_closed_recently(void)
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003715{
3716 term_T *term;
3717 term_T *next_term;
3718
3719 for (term = first_term; term != NULL; term = next_term)
3720 {
3721 next_term = term->tl_next;
3722 if (term->tl_channel_recently_closed)
3723 {
3724 term->tl_channel_recently_closed = FALSE;
3725 if (term_after_channel_closed(term))
3726 // start over, the list may have changed
3727 next_term = first_term;
3728 }
3729 }
3730}
3731
3732/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003733 * Fill one screen line from a line of the terminal.
3734 * Advances "pos" to past the last column.
3735 */
3736 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003737term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003738 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003739 win_T *wp,
3740 VTermScreen *screen,
3741 VTermPos *pos,
3742 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003743{
3744 int off = screen_get_current_line_off();
3745
3746 for (pos->col = 0; pos->col < max_col; )
3747 {
3748 VTermScreenCell cell;
3749 int c;
3750
3751 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003752 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003753
3754 c = cell.chars[0];
3755 if (c == NUL)
3756 {
3757 ScreenLines[off] = ' ';
3758 if (enc_utf8)
3759 ScreenLinesUC[off] = NUL;
3760 }
3761 else
3762 {
3763 if (enc_utf8)
3764 {
3765 int i;
3766
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003767 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003768 for (i = 0; i < Screen_mco
3769 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3770 {
3771 ScreenLinesC[i][off] = cell.chars[i + 1];
3772 if (cell.chars[i + 1] == 0)
3773 break;
3774 }
3775 if (c >= 0x80 || (Screen_mco > 0
3776 && ScreenLinesC[0][off] != 0))
3777 {
3778 ScreenLines[off] = ' ';
3779 ScreenLinesUC[off] = c;
3780 }
3781 else
3782 {
3783 ScreenLines[off] = c;
3784 ScreenLinesUC[off] = NUL;
3785 }
3786 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003787#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003788 else if (has_mbyte && c >= 0x80)
3789 {
3790 char_u mb[MB_MAXBYTES+1];
3791 WCHAR wc = c;
3792
3793 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3794 (char*)mb, 2, 0, 0) > 1)
3795 {
3796 ScreenLines[off] = mb[0];
3797 ScreenLines[off + 1] = mb[1];
3798 cell.width = mb_ptr2cells(mb);
3799 }
3800 else
3801 ScreenLines[off] = c;
3802 }
3803#endif
3804 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003805 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003806 ScreenLines[off] = c;
3807 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003808 ScreenAttrs[off] = cell2attr(term, wp, &cell.attrs, &cell.fg,
3809 &cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003810
3811 ++pos->col;
3812 ++off;
3813 if (cell.width == 2)
3814 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003815 // don't set the second byte to NUL for a DBCS encoding, it
3816 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003817 if (enc_utf8)
3818 {
3819 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003820 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003821 }
3822 else if (!has_mbyte)
3823 {
3824 // Can't show a double-width character with a single-byte
3825 // 'encoding', just use a space.
3826 ScreenLines[off] = ' ';
3827 ScreenAttrs[off] = ScreenAttrs[off - 1];
3828 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003829
3830 ++pos->col;
3831 ++off;
3832 }
3833 }
3834}
3835
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003836#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003837 static void
3838update_system_term(term_T *term)
3839{
3840 VTermPos pos;
3841 VTermScreen *screen;
3842
3843 if (term->tl_vterm == NULL)
3844 return;
3845 screen = vterm_obtain_screen(term->tl_vterm);
3846
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003847 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003848 while (term->tl_toprow > 0
3849 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3850 {
3851 int save_p_more = p_more;
3852
3853 p_more = FALSE;
3854 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003855 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003856 p_more = save_p_more;
3857 --term->tl_toprow;
3858 }
3859
3860 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3861 && pos.row < Rows; ++pos.row)
3862 {
3863 if (pos.row < term->tl_rows)
3864 {
3865 int max_col = MIN(Columns, term->tl_cols);
3866
Bram Moolenaar83d47902020-03-26 20:34:00 +01003867 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003868 }
3869 else
3870 pos.col = 0;
3871
Bram Moolenaar510f0372022-07-04 17:46:22 +01003872 screen_line(curwin, term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003873 }
3874
3875 term->tl_dirty_row_start = MAX_ROW;
3876 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003877}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003878#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003879
3880/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003881 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3882 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003883 * Terminal-Normal mode.
3884 */
3885 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003886term_do_update_window(win_T *wp)
3887{
3888 term_T *term = wp->w_buffer->b_term;
3889
3890 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3891}
3892
3893/*
3894 * Called to update a window that contains an active terminal.
3895 */
3896 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003897term_update_window(win_T *wp)
3898{
3899 term_T *term = wp->w_buffer->b_term;
3900 VTerm *vterm;
3901 VTermScreen *screen;
3902 VTermState *state;
3903 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003904 int rows, cols;
3905 int newrows, newcols;
3906 int minsize;
3907 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003908
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003909 vterm = term->tl_vterm;
3910 screen = vterm_obtain_screen(vterm);
3911 state = vterm_obtain_state(vterm);
3912
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003913 // We use UPD_NOT_VALID on a resize or scroll, redraw everything then.
3914 // With UPD_SOME_VALID only redraw what was marked dirty.
3915 if (wp->w_redr_type > UPD_SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003916 {
3917 term->tl_dirty_row_start = 0;
3918 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003919
3920 if (term->tl_postponed_scroll > 0
3921 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003922 // Scrolling is usually faster than redrawing, when there are only
3923 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003924 term_scroll_up(term, 0, term->tl_postponed_scroll);
3925 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003926 }
3927
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003928 /*
3929 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003930 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003931 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003932 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003933
Bram Moolenaar498c2562018-04-15 23:45:15 +02003934 newrows = 99999;
3935 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003936 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003937 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003938 // Always use curwin, it may be a popup window.
3939 win_T *wwp = twp == NULL ? curwin : twp;
3940
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003941 // When more than one window shows the same terminal, use the
3942 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003943 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003944 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003945 newrows = MIN(newrows, wwp->w_height);
3946 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003947 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003948 if (twp == NULL)
3949 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003950 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003951 if (newrows == 99999 || newcols == 99999)
3952 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003953 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3954 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3955
Bram Moolenaareba13e42021-02-23 17:47:23 +01003956 // If no cell is visible there is no point in resizing. Also, vterm can't
3957 // handle a zero height.
3958 if (newrows == 0 || newcols == 0)
3959 return;
3960
Bram Moolenaar498c2562018-04-15 23:45:15 +02003961 if (term->tl_rows != newrows || term->tl_cols != newcols)
3962 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003963 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003964 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003965 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003966 newrows);
3967 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003968
3969 // Updating the terminal size will cause the snapshot to be cleared.
3970 // When not in terminal_loop() we need to restore it.
3971 if (term != in_terminal_loop)
3972 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003973 }
3974
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003975 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003976 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003977 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003978
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003979 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3980 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003981 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003982 if (pos.row < term->tl_rows)
3983 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003984 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003985
Bram Moolenaar83d47902020-03-26 20:34:00 +01003986 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003987 }
3988 else
3989 pos.col = 0;
3990
Bram Moolenaar510f0372022-07-04 17:46:22 +01003991 screen_line(wp, wp->w_winrow + pos.row
Bram Moolenaarf118d482018-03-13 13:14:00 +01003992#ifdef FEAT_MENU
3993 + winbar_height(wp)
3994#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003995 , wp->w_wincol, pos.col, wp->w_width,
3996#ifdef FEAT_PROP_POPUP
3997 popup_is_popup(wp) ? SLF_POPUP :
3998#endif
3999 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004000 }
Bram Moolenaar3194e5b2021-12-13 21:59:09 +00004001}
4002
4003/*
4004 * Called after updating all windows: may reset dirty rows.
4005 */
4006 void
4007term_did_update_window(win_T *wp)
4008{
4009 term_T *term = wp->w_buffer->b_term;
4010
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004011 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode
4012 || wp->w_redr_type != 0)
4013 return;
4014
4015 term->tl_dirty_row_start = MAX_ROW;
4016 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004017}
4018
4019/*
4020 * Return TRUE if "wp" is a terminal window where the job has finished.
4021 */
4022 int
4023term_is_finished(buf_T *buf)
4024{
4025 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
4026}
4027
4028/*
4029 * Return TRUE if "wp" is a terminal window where the job has finished or we
4030 * are in Terminal-Normal mode, thus we show the buffer contents.
4031 */
4032 int
4033term_show_buffer(buf_T *buf)
4034{
4035 term_T *term = buf->b_term;
4036
4037 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
4038}
4039
4040/*
4041 * The current buffer is going to be changed. If there is terminal
4042 * highlighting remove it now.
4043 */
4044 void
4045term_change_in_curbuf(void)
4046{
4047 term_T *term = curbuf->b_term;
4048
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004049 if (!term_is_finished(curbuf) || term->tl_scrollback.ga_len <= 0)
4050 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004051
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004052 free_scrollback(term);
4053 redraw_buf_later(term->tl_buffer, UPD_NOT_VALID);
4054
4055 // The buffer is now like a normal buffer, it cannot be easily
4056 // abandoned when changed.
4057 set_string_option_direct((char_u *)"buftype", -1,
4058 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004059}
4060
4061/*
4062 * Get the screen attribute for a position in the buffer.
4063 * Use a negative "col" to get the filler background color.
4064 */
4065 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004066term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004067{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004068 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004069 term_T *term = buf->b_term;
4070 sb_line_T *line;
4071 cellattr_T *cellattr;
4072
4073 if (lnum > term->tl_scrollback.ga_len)
4074 cellattr = &term->tl_default_color;
4075 else
4076 {
4077 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
4078 if (col < 0 || col >= line->sb_cols)
4079 cellattr = &line->sb_fill_attr;
4080 else
4081 cellattr = line->sb_cells + col;
4082 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004083 return cell2attr(term, wp, &cellattr->attrs, &cellattr->fg, &cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004084}
4085
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004086/*
4087 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02004088 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004089 */
4090 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004091cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004092{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004093 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
4094 if (rgb->index == 0)
4095 rgb->type = VTERM_COLOR_RGB;
4096 else
4097 {
4098 rgb->type = VTERM_COLOR_INDEXED;
4099 --rgb->index;
4100 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004101}
4102
4103/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004104 * Initialize vterm color from the synID.
4105 * Returns TRUE if color is set to "fg" and "bg".
4106 * Otherwise returns FALSE.
4107 */
4108 static int
4109get_vterm_color_from_synid(int id, VTermColor *fg, VTermColor *bg)
4110{
4111#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4112 // Use the actual color for the GUI and when 'termguicolors' is set.
4113 if (0
4114# ifdef FEAT_GUI
4115 || gui.in_use
4116# endif
4117# ifdef FEAT_TERMGUICOLORS
4118 || p_tgc
4119# ifdef FEAT_VTP
4120 // Finally get INVALCOLOR on this execution path
4121 || (!p_tgc && t_colors >= 256)
4122# endif
4123# endif
4124 )
4125 {
4126 guicolor_T fg_rgb = INVALCOLOR;
4127 guicolor_T bg_rgb = INVALCOLOR;
4128
4129 if (id > 0)
4130 syn_id2colors(id, &fg_rgb, &bg_rgb);
4131
4132 if (fg_rgb != INVALCOLOR)
4133 {
4134 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
4135 fg->red = (unsigned)(rgb >> 16);
4136 fg->green = (unsigned)(rgb >> 8) & 255;
4137 fg->blue = (unsigned)rgb & 255;
4138 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
4139 }
4140 else
4141 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4142
4143 if (bg_rgb != INVALCOLOR)
4144 {
4145 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
4146 bg->red = (unsigned)(rgb >> 16);
4147 bg->green = (unsigned)(rgb >> 8) & 255;
4148 bg->blue = (unsigned)rgb & 255;
4149 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
4150 }
4151 else
4152 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4153
4154 return TRUE;
4155 }
4156 else
4157#endif
4158 if (t_colors >= 16)
4159 {
4160 int cterm_fg = -1;
4161 int cterm_bg = -1;
4162
4163 if (id > 0)
4164 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
4165
4166 if (cterm_fg >= 0)
4167 {
4168 cterm_color2vterm(cterm_fg, fg);
4169 fg->type |= VTERM_COLOR_DEFAULT_FG;
4170 }
4171 else
4172 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4173
4174 if (cterm_bg >= 0)
4175 {
4176 cterm_color2vterm(cterm_bg, bg);
4177 bg->type |= VTERM_COLOR_DEFAULT_BG;
4178 }
4179 else
4180 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4181
4182 return TRUE;
4183 }
4184
4185 return FALSE;
4186}
4187
4188 void
4189term_reset_wincolor(win_T *wp)
4190{
4191 wp->w_term_wincolor.fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4192 wp->w_term_wincolor.bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
4193}
4194
4195/*
4196 * Cache the color of 'wincolor'.
4197 */
4198 void
4199term_update_wincolor(win_T *wp)
4200{
4201 int id = 0;
4202
4203 if (*wp->w_p_wcr != NUL)
4204 id = syn_name2id(wp->w_p_wcr);
4205 if (id == 0 || !get_vterm_color_from_synid(id, &wp->w_term_wincolor.fg,
4206 &wp->w_term_wincolor.bg))
4207 term_reset_wincolor(wp);
4208}
4209
4210/*
4211 * Called when option 'termguicolors' was set,
4212 * or when any highlight is changed.
4213 */
4214 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004215term_update_wincolor_all(void)
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004216{
4217 win_T *wp = NULL;
4218 int did_curwin = FALSE;
4219
4220 while (for_all_windows_and_curwin(&wp, &did_curwin))
4221 term_update_wincolor(wp);
4222}
4223
4224/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004225 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004226 */
4227 static void
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004228init_default_colors(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004229{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004230 VTermColor *fg, *bg;
4231 int fgval, bgval;
4232 int id;
4233
Bram Moolenaara80faa82020-04-12 19:37:17 +02004234 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004235 term->tl_default_color.width = 1;
4236 fg = &term->tl_default_color.fg;
4237 bg = &term->tl_default_color.bg;
4238
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004239 // Vterm uses a default black background. Set it to white when
4240 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004241 if (*p_bg == 'l')
4242 {
4243 fgval = 0;
4244 bgval = 255;
4245 }
4246 else
4247 {
4248 fgval = 255;
4249 bgval = 0;
4250 }
4251 fg->red = fg->green = fg->blue = fgval;
4252 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004253 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
4254 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004255
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004256 // The highlight group overrules the defaults.
4257 id = term_get_highlight_id(term, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004258
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004259 if (!get_vterm_color_from_synid(id, fg, bg))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004260 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004261#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004262 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004263#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004264
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004265 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004266 if (cterm_normal_fg_color > 0)
4267 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004268 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004269# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4270# ifdef VIMDLL
4271 if (!gui.in_use)
4272# endif
4273 {
4274 tmp = fg->red;
4275 fg->red = fg->blue;
4276 fg->blue = tmp;
4277 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004278# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004279 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004280# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004281 else
4282 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004283# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004284
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004285 if (cterm_normal_bg_color > 0)
4286 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004287 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004288# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4289# ifdef VIMDLL
4290 if (!gui.in_use)
4291# endif
4292 {
4293 tmp = fg->red;
4294 fg->red = fg->blue;
4295 fg->blue = tmp;
4296 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004297# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004298 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004299# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004300 else
4301 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004302# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004303 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004304}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004305
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004306#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4307/*
Bram Moolenaar30b9a412022-05-26 14:06:37 +01004308 * Return TRUE if the user-defined palette (either g:terminal_ansi_colors or the
4309 * "ansi_colors" argument in term_start()) shall be applied.
4310 */
4311 static int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004312term_use_palette(void)
Bram Moolenaar30b9a412022-05-26 14:06:37 +01004313{
4314 if (0
4315#ifdef FEAT_GUI
4316 || gui.in_use
4317#endif
4318#ifdef FEAT_TERMGUICOLORS
4319 || p_tgc
4320#endif
4321 )
4322 return TRUE;
4323 return FALSE;
4324}
4325
4326/*
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004327 * Set the 16 ANSI colors from array of RGB values
4328 */
4329 static void
4330set_vterm_palette(VTerm *vterm, long_u *rgb)
4331{
4332 int index = 0;
4333 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004334
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004335 for (; index < 16; index++)
4336 {
4337 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004338
Millyb771b6b2021-11-23 12:07:25 +00004339 color.type = VTERM_COLOR_RGB;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004340 color.red = (unsigned)(rgb[index] >> 16);
4341 color.green = (unsigned)(rgb[index] >> 8) & 255;
4342 color.blue = (unsigned)rgb[index] & 255;
Bram Moolenaar68afde42022-02-25 20:48:26 +00004343 color.index = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004344 vterm_state_set_palette_color(state, index, &color);
4345 }
4346}
4347
4348/*
4349 * Set the ANSI color palette from a list of colors
4350 */
4351 static int
4352set_ansi_colors_list(VTerm *vterm, list_T *list)
4353{
4354 int n = 0;
4355 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004356 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004357
Bram Moolenaarb0992022020-01-30 14:55:42 +01004358 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004359 {
4360 char_u *color_name;
4361 guicolor_T guicolor;
4362
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004363 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004364 if (color_name == NULL)
4365 return FAIL;
4366
4367 guicolor = GUI_GET_COLOR(color_name);
4368 if (guicolor == INVALCOLOR)
4369 return FAIL;
4370
4371 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4372 }
4373
4374 if (n != 16 || li != NULL)
4375 return FAIL;
4376
4377 set_vterm_palette(vterm, rgb);
4378
4379 return OK;
4380}
4381
4382/*
4383 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4384 */
4385 static void
4386init_vterm_ansi_colors(VTerm *vterm)
4387{
4388 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4389
LemonBoyb2b3acb2022-05-20 10:10:34 +01004390 if (var == NULL)
4391 return;
4392
4393 if (var->di_tv.v_type != VAR_LIST
4394 || var->di_tv.vval.v_list == NULL
4395 || var->di_tv.vval.v_list->lv_first == &range_list_item
4396 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004397 semsg(_(e_invalid_argument_str), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004398}
4399#endif
4400
Bram Moolenaar52acb112018-03-18 19:20:22 +01004401/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004402 * Handles a "drop" command from the job in the terminal.
4403 * "item" is the file name, "item->li_next" may have options.
4404 */
4405 static void
4406handle_drop_command(listitem_T *item)
4407{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004408 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004409 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004410 int bufnr;
4411 win_T *wp;
4412 tabpage_T *tp;
4413 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004414 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004415
4416 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4417 FOR_ALL_TAB_WINDOWS(tp, wp)
4418 {
4419 if (wp->w_buffer->b_fnum == bufnr)
4420 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004421 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004422 goto_tabpage_win(tp, wp);
4423 return;
4424 }
4425 }
4426
Bram Moolenaara80faa82020-04-12 19:37:17 +02004427 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004428
4429 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4430 && opt_item->li_tv.vval.v_dict != NULL)
4431 {
4432 dict_T *dict = opt_item->li_tv.vval.v_dict;
4433 char_u *p;
4434
Bram Moolenaard61efa52022-07-23 09:52:04 +01004435 p = dict_get_string(dict, "ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004436 if (p == NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004437 p = dict_get_string(dict, "fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004438 if (p != NULL)
4439 {
4440 if (check_ff_value(p) == FAIL)
4441 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4442 else
4443 ea.force_ff = *p;
4444 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01004445 p = dict_get_string(dict, "enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004446 if (p == NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004447 p = dict_get_string(dict, "encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004448 if (p != NULL)
4449 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004450 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004451 if (ea.cmd != NULL)
4452 {
4453 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4454 ea.force_enc = 11;
4455 tofree = ea.cmd;
4456 }
4457 }
4458
Bram Moolenaard61efa52022-07-23 09:52:04 +01004459 p = dict_get_string(dict, "bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004460 if (p != NULL)
4461 get_bad_opt(p, &ea);
4462
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004463 if (dict_has_key(dict, "bin"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004464 ea.force_bin = FORCE_BIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004465 if (dict_has_key(dict, "binary"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004466 ea.force_bin = FORCE_BIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004467 if (dict_has_key(dict, "nobin"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004468 ea.force_bin = FORCE_NOBIN;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004469 if (dict_has_key(dict, "nobinary"))
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004470 ea.force_bin = FORCE_NOBIN;
4471 }
4472
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004473 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004474 if (ea.cmd == NULL)
4475 ea.cmd = (char_u *)"split";
4476 ea.arg = fname;
4477 ea.cmdidx = CMD_split;
4478 ex_splitview(&ea);
4479
4480 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004481}
4482
4483/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004484 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4485 */
4486 static int
4487is_permitted_term_api(char_u *func, char_u *pat)
4488{
4489 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4490}
4491
4492/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004493 * Handles a function call from the job running in a terminal.
4494 * "item" is the function name, "item->li_next" has the arguments.
4495 */
4496 static void
4497handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4498{
4499 char_u *func;
4500 typval_T argvars[2];
4501 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004502 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004503
4504 if (item->li_next == NULL)
4505 {
4506 ch_log(channel, "Missing function arguments for call");
4507 return;
4508 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004509 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004510
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004511 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004512 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004513 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004514 return;
4515 }
4516
4517 argvars[0].v_type = VAR_NUMBER;
4518 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4519 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004520 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004521 funcexe.fe_firstline = 1L;
4522 funcexe.fe_lastline = 1L;
4523 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004524 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004525 {
4526 clear_tv(&rettv);
4527 ch_log(channel, "Function %s called", func);
4528 }
4529 else
4530 ch_log(channel, "Calling function %s failed", func);
4531}
4532
4533/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004534 * URL decoding (also know as Percent-encoding).
4535 *
4536 * Note this function currently is only used for decoding shell's
4537 * OSC 7 escape sequence which we can assume all bytes are valid
4538 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4539 * encoding bytes like 0xfe, 0xff.
4540 */
4541 static size_t
4542url_decode(const char *src, const size_t len, char_u *dst)
4543{
4544 size_t i = 0, j = 0;
4545
4546 while (i < len)
4547 {
4548 if (src[i] == '%' && i + 2 < len)
4549 {
4550 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4551 j++;
4552 i += 3;
4553 }
4554 else
4555 {
4556 dst[j] = src[i];
4557 i++;
4558 j++;
4559 }
4560 }
4561 dst[j] = '\0';
4562 return j;
4563}
4564
4565/*
4566 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4567 *
4568 * The OSC 7 sequence has the format of
4569 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4570 * and what VTerm provides via VTermStringFragment is
4571 * "file://HOSTNAME/CURRENT/DIR"
4572 */
4573 static void
Bram Moolenaar474ad392022-08-21 11:37:17 +01004574sync_shell_dir(garray_T *gap)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004575{
Bram Moolenaar474ad392022-08-21 11:37:17 +01004576 int offset = 7; // len of "file://" is 7
4577 char *pos = (char *)gap->ga_data + offset;
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004578 char_u *new_dir;
4579
4580 // remove HOSTNAME to get PWD
Bram Moolenaar474ad392022-08-21 11:37:17 +01004581 while (offset < (int)gap->ga_len && *pos != '/' )
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004582 {
Bram Moolenaar474ad392022-08-21 11:37:17 +01004583 ++offset;
4584 ++pos;
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004585 }
4586
Bram Moolenaar474ad392022-08-21 11:37:17 +01004587 if (offset >= (int)gap->ga_len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004588 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004589 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
Bram Moolenaar474ad392022-08-21 11:37:17 +01004590 gap->ga_data);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004591 return;
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004592 }
4593
Bram Moolenaar474ad392022-08-21 11:37:17 +01004594 new_dir = alloc(gap->ga_len - offset + 1);
4595 url_decode(pos, gap->ga_len-offset, new_dir);
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004596 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4597 vim_free(new_dir);
4598}
4599
4600/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004601 * Called by libvterm when it cannot recognize an OSC sequence.
4602 * We recognize a terminal API command.
4603 */
4604 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004605parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004606{
4607 term_T *term = (term_T *)user;
4608 js_read_T reader;
4609 typval_T tv;
4610 channel_T *channel = term->tl_job == NULL ? NULL
4611 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004612 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004613
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004614 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
Bram Moolenaar474ad392022-08-21 11:37:17 +01004615 if (command != 51 && (command != 7 || !p_asd))
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004616 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004617
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004618 // Concatenate what was received until the final piece is found.
4619 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4620 {
4621 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004622 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004623 }
4624 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004625 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004626 if (!frag.final)
4627 return 1;
4628
4629 ((char *)gap->ga_data)[gap->ga_len] = 0;
Bram Moolenaar474ad392022-08-21 11:37:17 +01004630
4631 if (command == 7)
4632 {
4633 sync_shell_dir(gap);
4634 ga_clear(gap);
4635 return 1;
4636 }
4637
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004638 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004639 reader.js_fill = NULL;
4640 reader.js_used = 0;
4641 if (json_decode(&reader, &tv, 0) == OK
4642 && tv.v_type == VAR_LIST
4643 && tv.vval.v_list != NULL)
4644 {
4645 listitem_T *item = tv.vval.v_list->lv_first;
4646
4647 if (item == NULL)
4648 ch_log(channel, "Missing command");
4649 else
4650 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004651 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004652
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004653 // Make sure an invoked command doesn't delete the buffer (and the
4654 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004655 ++term->tl_buffer->b_locked;
4656
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004657 item = item->li_next;
4658 if (item == NULL)
4659 ch_log(channel, "Missing argument for %s", cmd);
4660 else if (STRCMP(cmd, "drop") == 0)
4661 handle_drop_command(item);
4662 else if (STRCMP(cmd, "call") == 0)
4663 handle_call_command(term, channel, item);
4664 else
4665 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004666 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004667 }
4668 }
4669 else
4670 ch_log(channel, "Invalid JSON received");
4671
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004672 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004673 clear_tv(&tv);
4674 return 1;
4675}
4676
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004677/*
4678 * Called by libvterm when it cannot recognize a CSI sequence.
4679 * We recognize the window position report.
4680 */
4681 static int
4682parse_csi(
4683 const char *leader UNUSED,
4684 const long args[],
4685 int argcount,
4686 const char *intermed UNUSED,
4687 char command,
4688 void *user)
4689{
4690 term_T *term = (term_T *)user;
4691 char buf[100];
4692 int len;
4693 int x = 0;
4694 int y = 0;
4695 win_T *wp;
4696
4697 // We recognize only CSI 13 t
4698 if (command != 't' || argcount != 1 || args[0] != 13)
4699 return 0; // not handled
4700
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004701 // When getting the window position is not possible or it fails it results
4702 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004703#if defined(FEAT_GUI) \
4704 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4705 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004706 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004707#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004708
4709 FOR_ALL_WINDOWS(wp)
4710 if (wp->w_buffer == term->tl_buffer)
4711 break;
4712 if (wp != NULL)
4713 {
4714#ifdef FEAT_GUI
4715 if (gui.in_use)
4716 {
4717 x += wp->w_wincol * gui.char_width;
4718 y += W_WINROW(wp) * gui.char_height;
4719 }
4720 else
4721#endif
4722 {
4723 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004724 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004725 x += wp->w_wincol * 7;
4726 y += W_WINROW(wp) * 10;
4727 }
4728 }
4729
4730 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4731 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4732 (char_u *)buf, len, NULL);
4733 return 1;
4734}
4735
Bram Moolenaard8637282020-05-20 18:41:41 +02004736static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004737 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004738 parse_csi, // csi
4739 parse_osc, // osc
Bram Moolenaar7da34152021-11-24 19:30:55 +00004740 NULL, // dcs
4741 NULL, // apc
4742 NULL, // pm
4743 NULL // sos
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004744};
4745
4746/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004747 * Use Vim's allocation functions for vterm so profiling works.
4748 */
4749 static void *
4750vterm_malloc(size_t size, void *data UNUSED)
4751{
Bram Moolenaar88137392021-11-12 16:01:15 +00004752 // make sure that the length is not zero
4753 return alloc_clear(size == 0 ? 1L : size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004754}
4755
4756 static void
4757vterm_memfree(void *ptr, void *data UNUSED)
4758{
4759 vim_free(ptr);
4760}
4761
4762static VTermAllocatorFunctions vterm_allocator = {
4763 &vterm_malloc,
4764 &vterm_memfree
4765};
4766
4767/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004768 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004769 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004770 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004771 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004772create_vterm(term_T *term, int rows, int cols)
4773{
4774 VTerm *vterm;
4775 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004776 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004777 VTermValue value;
4778
Bram Moolenaar756ef112018-04-10 12:04:27 +02004779 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004780 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004781 if (vterm == NULL)
4782 return FAIL;
4783
4784 // Allocate screen and state here, so we can bail out if that fails.
4785 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004786 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004787 if (state == NULL || screen == NULL)
4788 {
4789 vterm_free(vterm);
4790 return FAIL;
4791 }
4792
Bram Moolenaar52acb112018-03-18 19:20:22 +01004793 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004794 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004795 vterm_set_utf8(vterm, 1);
4796
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004797 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004798
4799 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004800 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004801 &term->tl_default_color.fg,
4802 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004803
Bram Moolenaar9e587872019-05-13 20:27:23 +02004804 if (t_colors < 16)
4805 // Less than 16 colors: assume that bold means using a bright color for
4806 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004807 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4808
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004809 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004810 vterm_screen_reset(screen, 1 /* hard */);
4811
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004812 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004813 vterm_screen_enable_altscreen(screen, 1);
4814
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004815 // For unix do not use a blinking cursor. In an xterm this causes the
4816 // cursor to blink if it's blinking in the xterm.
4817 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004818#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004819 if (GetCaretBlinkTime() == INFINITE)
4820 value.boolean = 0;
4821 else
4822 value.boolean = 1;
4823#else
4824 value.boolean = 0;
4825#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004826 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004827 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004828
4829 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004830}
4831
4832/*
LemonBoyb2b3acb2022-05-20 10:10:34 +01004833 * Reset the terminal palette to its default value.
4834 */
4835 static void
4836term_reset_palette(VTerm *vterm)
4837{
4838 VTermState *state = vterm_obtain_state(vterm);
4839 int index;
4840
4841 for (index = 0; index < 16; index++)
4842 {
4843 VTermColor color;
4844
4845 color.type = VTERM_COLOR_INDEXED;
4846 ansi_color2rgb(index, &color.red, &color.green, &color.blue,
4847 &color.index);
4848 // The first valid index starts at 1.
4849 color.index -= 1;
4850
4851 vterm_state_set_palette_color(state, index, &color);
4852 }
4853}
4854
4855 static void
4856term_update_palette(term_T *term)
4857{
Bram Moolenaar30b9a412022-05-26 14:06:37 +01004858#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
LemonBoyb2b3acb2022-05-20 10:10:34 +01004859 if (term_use_palette()
4860 && (term->tl_palette != NULL
4861 || find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE)
Bram Moolenaar30b9a412022-05-26 14:06:37 +01004862 != NULL))
LemonBoyb2b3acb2022-05-20 10:10:34 +01004863 {
4864 if (term->tl_palette != NULL)
4865 set_vterm_palette(term->tl_vterm, term->tl_palette);
4866 else
4867 init_vterm_ansi_colors(term->tl_vterm);
4868 }
4869 else
Bram Moolenaar30b9a412022-05-26 14:06:37 +01004870#endif
LemonBoyb2b3acb2022-05-20 10:10:34 +01004871 term_reset_palette(term->tl_vterm);
4872}
4873
4874/*
4875 * Called when option 'termguicolors' is changed.
4876 */
4877 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00004878term_update_palette_all(void)
LemonBoyb2b3acb2022-05-20 10:10:34 +01004879{
4880 term_T *term;
4881
4882 FOR_ALL_TERMS(term)
4883 {
4884 if (term->tl_vterm == NULL)
4885 continue;
4886 term_update_palette(term);
4887 }
4888}
4889
4890/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004891 * Called when option 'background' or 'termguicolors' was set,
4892 * or when any highlight is changed.
Bram Moolenaarad431992021-05-03 20:40:38 +02004893 */
4894 void
4895term_update_colors_all(void)
4896{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004897 term_T *term;
Bram Moolenaarad431992021-05-03 20:40:38 +02004898
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004899 FOR_ALL_TERMS(term)
4900 {
4901 if (term->tl_vterm == NULL)
4902 continue;
4903 init_default_colors(term);
4904 vterm_state_set_default_colors(
4905 vterm_obtain_state(term->tl_vterm),
4906 &term->tl_default_color.fg,
4907 &term->tl_default_color.bg);
4908 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004909}
4910
4911/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004912 * Return the text to show for the buffer name and status.
4913 */
4914 char_u *
4915term_get_status_text(term_T *term)
4916{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004917 if (term->tl_status_text != NULL)
4918 return term->tl_status_text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004919
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004920 char_u *txt;
4921 size_t len;
4922 char_u *fname;
4923
4924 if (term->tl_normal_mode)
4925 {
4926 if (term_job_running(term))
4927 txt = (char_u *)_("Terminal");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004928 else
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004929 txt = (char_u *)_("Terminal-finished");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004930 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00004931 else if (term->tl_title != NULL)
4932 txt = term->tl_title;
4933 else if (term_none_open(term))
4934 txt = (char_u *)_("active");
4935 else if (term_job_running(term))
4936 txt = (char_u *)_("running");
4937 else
4938 txt = (char_u *)_("finished");
4939 fname = buf_get_fname(term->tl_buffer);
4940 len = 9 + STRLEN(fname) + STRLEN(txt);
4941 term->tl_status_text = alloc(len);
4942 if (term->tl_status_text != NULL)
4943 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
4944 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004945 return term->tl_status_text;
4946}
4947
4948/*
Bram Moolenaar3ad69532021-11-19 17:01:08 +00004949 * Clear the cached value of the status text.
4950 */
4951 void
4952term_clear_status_text(term_T *term)
4953{
4954 VIM_CLEAR(term->tl_status_text);
4955}
4956
4957/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004958 * Mark references in jobs of terminals.
4959 */
4960 int
4961set_ref_in_term(int copyID)
4962{
4963 int abort = FALSE;
4964 term_T *term;
4965 typval_T tv;
4966
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004967 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004968 if (term->tl_job != NULL)
4969 {
4970 tv.v_type = VAR_JOB;
4971 tv.vval.v_job = term->tl_job;
4972 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4973 }
4974 return abort;
4975}
4976
4977/*
4978 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004979 * Returns NULL when the buffer is not for a terminal window and logs a message
4980 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004981 */
4982 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004983term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004984{
4985 buf_T *buf;
4986
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004987 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004988 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004989 --emsg_off;
4990 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004991 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004992 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004993 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004994 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004995 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004996 return buf;
4997}
4998
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004999 static void
5000clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005001{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005002 CLEAR_FIELD(*cell);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005003 cell->fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
5004 cell->bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005005}
5006
5007 static void
5008dump_term_color(FILE *fd, VTermColor *color)
5009{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005010 int index;
5011
5012 if (VTERM_COLOR_IS_INDEXED(color))
5013 index = color->index + 1;
5014 else if (color->type == 0)
5015 // use RGB values
5016 index = 255;
5017 else
5018 // default color
5019 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005020 fprintf(fd, "%02x%02x%02x%d",
5021 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005022 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005023}
5024
5025/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005026 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01005027 *
5028 * Each screen cell in full is:
5029 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
5030 * {characters} is a space for an empty cell
5031 * For a double-width character "+" is changed to "*" and the next cell is
5032 * skipped.
5033 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
5034 * when "&" use the same as the previous cell.
5035 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
5036 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
5037 * {color-idx} is a number from 0 to 255
5038 *
5039 * Screen cell with same width, attributes and color as the previous one:
5040 * |{characters}
5041 *
5042 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
5043 *
5044 * Repeating the previous screen cell:
5045 * @{count}
5046 */
5047 void
5048f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
5049{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005050 buf_T *buf;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005051 term_T *term;
5052 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005053 int max_height = 0;
5054 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005055 stat_T st;
5056 FILE *fd;
5057 VTermPos pos;
5058 VTermScreen *screen;
5059 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005060 VTermState *state;
5061 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005062
5063 if (check_restricted() || check_secure())
5064 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005065
5066 if (in_vim9script()
5067 && (check_for_buffer_arg(argvars, 0) == FAIL
5068 || check_for_string_arg(argvars, 1) == FAIL
5069 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5070 return;
5071
5072 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01005073 if (buf == NULL)
5074 return;
5075 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02005076 if (term->tl_vterm == NULL)
5077 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005078 emsg(_(e_job_already_finished));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02005079 return;
5080 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005081
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005082 if (argvars[2].v_type != VAR_UNKNOWN)
5083 {
5084 dict_T *d;
5085
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01005086 if (check_for_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005087 return;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005088 d = argvars[2].vval.v_dict;
5089 if (d != NULL)
5090 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01005091 max_height = dict_get_number(d, "rows");
5092 max_width = dict_get_number(d, "columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005093 }
5094 }
5095
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005096 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005097 if (fname == NULL)
5098 return;
5099 if (mch_stat((char *)fname, &st) >= 0)
5100 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005101 semsg(_(e_file_exists_str), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005102 return;
5103 }
5104
Bram Moolenaard96ff162018-02-18 22:13:29 +01005105 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
5106 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005107 semsg(_(e_cant_create_file_str), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005108 return;
5109 }
5110
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005111 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005112
5113 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005114 state = vterm_obtain_state(term->tl_vterm);
5115 vterm_state_get_cursorpos(state, &cursor_pos);
5116
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005117 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
5118 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005119 {
5120 int repeat = 0;
5121
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01005122 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
5123 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005124 {
5125 VTermScreenCell cell;
5126 int same_attr;
5127 int same_chars = TRUE;
5128 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005129 int is_cursor_pos = (pos.col == cursor_pos.col
5130 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005131
5132 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005133 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005134
5135 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
5136 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01005137 int c = cell.chars[i];
5138 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02005139 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01005140
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005141 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01005142 if (i == 0)
5143 {
5144 c = (c == NUL) ? ' ' : c;
5145 pc = (pc == NUL) ? ' ' : pc;
5146 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02005147 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005148 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02005149 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005150 break;
5151 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005152 same_attr = vtermAttr2hl(&cell.attrs)
5153 == vtermAttr2hl(&prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005154 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
5155 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005156 if (same_chars && cell.width == prev_cell.width && same_attr
5157 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005158 {
5159 ++repeat;
5160 }
5161 else
5162 {
5163 if (repeat > 0)
5164 {
5165 fprintf(fd, "@%d", repeat);
5166 repeat = 0;
5167 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005168 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005169
5170 if (cell.chars[0] == NUL)
5171 fputs(" ", fd);
5172 else
5173 {
5174 char_u charbuf[10];
5175 int len;
5176
5177 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
5178 && cell.chars[i] != NUL; ++i)
5179 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02005180 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005181 fwrite(charbuf, len, 1, fd);
5182 }
5183 }
5184
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005185 // When only the characters differ we don't write anything, the
5186 // following "|", "@" or NL will indicate using the same
5187 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005188 if (cell.width != prev_cell.width || !same_attr)
5189 {
5190 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005191 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005192 else
5193 fputs("+", fd);
5194
5195 if (same_attr)
5196 {
5197 fputs("&", fd);
5198 }
5199 else
5200 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005201 fprintf(fd, "%d", vtermAttr2hl(&cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005202 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005203 fputs("&", fd);
5204 else
5205 {
5206 fputs("#", fd);
5207 dump_term_color(fd, &cell.fg);
5208 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005209 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005210 fputs("&", fd);
5211 else
5212 {
5213 fputs("#", fd);
5214 dump_term_color(fd, &cell.bg);
5215 }
5216 }
5217 }
5218
5219 prev_cell = cell;
5220 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005221
5222 if (cell.width == 2)
5223 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005224 }
5225 if (repeat > 0)
5226 fprintf(fd, "@%d", repeat);
5227 fputs("\n", fd);
5228 }
5229
5230 fclose(fd);
5231}
5232
5233/*
5234 * Called when a dump is corrupted. Put a breakpoint here when debugging.
5235 */
5236 static void
5237dump_is_corrupt(garray_T *gap)
5238{
5239 ga_concat(gap, (char_u *)"CORRUPT");
5240}
5241
5242 static void
5243append_cell(garray_T *gap, cellattr_T *cell)
5244{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00005245 if (ga_grow(gap, 1) == FAIL)
5246 return;
5247
5248 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
5249 ++gap->ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005250}
5251
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005252 static void
5253clear_cellattr(cellattr_T *cell)
5254{
5255 CLEAR_FIELD(*cell);
5256 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
5257 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
5258}
5259
Bram Moolenaard96ff162018-02-18 22:13:29 +01005260/*
5261 * Read the dump file from "fd" and append lines to the current buffer.
5262 * Return the cell width of the longest line.
5263 */
5264 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01005265read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005266{
5267 int c;
5268 garray_T ga_text;
5269 garray_T ga_cell;
5270 char_u *prev_char = NULL;
5271 int attr = 0;
5272 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005273 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005274 term_T *term = curbuf->b_term;
5275 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005276 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005277
5278 ga_init2(&ga_text, 1, 90);
5279 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005280 clear_cellattr(&cell);
5281 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005282 cursor_pos->row = -1;
5283 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005284
5285 c = fgetc(fd);
5286 for (;;)
5287 {
5288 if (c == EOF)
5289 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02005290 if (c == '\r')
5291 {
5292 // DOS line endings? Ignore.
5293 c = fgetc(fd);
5294 }
5295 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005296 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005297 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005298 if (ga_text.ga_data == NULL)
5299 dump_is_corrupt(&ga_text);
5300 if (ga_grow(&term->tl_scrollback, 1) == OK)
5301 {
5302 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
5303 + term->tl_scrollback.ga_len;
5304
5305 if (max_cells < ga_cell.ga_len)
5306 max_cells = ga_cell.ga_len;
5307 line->sb_cols = ga_cell.ga_len;
5308 line->sb_cells = ga_cell.ga_data;
5309 line->sb_fill_attr = term->tl_default_color;
5310 ++term->tl_scrollback.ga_len;
5311 ga_init(&ga_cell);
5312
5313 ga_append(&ga_text, NUL);
5314 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5315 ga_text.ga_len, FALSE);
5316 }
5317 else
5318 ga_clear(&ga_cell);
5319 ga_text.ga_len = 0;
5320
5321 c = fgetc(fd);
5322 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005323 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005324 {
5325 int prev_len = ga_text.ga_len;
5326
Bram Moolenaar9271d052018-02-25 21:39:46 +01005327 if (c == '>')
5328 {
5329 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005330 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005331 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5332 cursor_pos->col = ga_cell.ga_len;
5333 }
5334
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005335 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005336 c = fgetc(fd);
5337 if (c != EOF)
5338 ga_append(&ga_text, c);
5339 for (;;)
5340 {
5341 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005342 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005343 || c == EOF || c == '\n')
5344 break;
5345 ga_append(&ga_text, c);
5346 }
5347
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005348 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005349 vim_free(prev_char);
5350 if (ga_text.ga_data != NULL)
5351 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5352 ga_text.ga_len - prev_len);
5353
Bram Moolenaar9271d052018-02-25 21:39:46 +01005354 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005355 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005356 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005357 }
5358 else if (c == '+' || c == '*')
5359 {
5360 int is_bg;
5361
5362 cell.width = c == '+' ? 1 : 2;
5363
5364 c = fgetc(fd);
5365 if (c == '&')
5366 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005367 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005368 c = fgetc(fd);
5369 }
5370 else if (isdigit(c))
5371 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005372 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005373 attr = 0;
5374 while (isdigit(c))
5375 {
5376 attr = attr * 10 + (c - '0');
5377 c = fgetc(fd);
5378 }
5379 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005380
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005381 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005382 for (is_bg = 0; is_bg <= 1; ++is_bg)
5383 {
5384 if (c == '&')
5385 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005386 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005387 c = fgetc(fd);
5388 }
5389 else if (c == '#')
5390 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005391 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005392
5393 c = fgetc(fd);
5394 red = hex2nr(c);
5395 c = fgetc(fd);
5396 red = (red << 4) + hex2nr(c);
5397 c = fgetc(fd);
5398 green = hex2nr(c);
5399 c = fgetc(fd);
5400 green = (green << 4) + hex2nr(c);
5401 c = fgetc(fd);
5402 blue = hex2nr(c);
5403 c = fgetc(fd);
5404 blue = (blue << 4) + hex2nr(c);
5405 c = fgetc(fd);
5406 if (!isdigit(c))
5407 dump_is_corrupt(&ga_text);
5408 while (isdigit(c))
5409 {
5410 index = index * 10 + (c - '0');
5411 c = fgetc(fd);
5412 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005413 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005414 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005415 type = VTERM_COLOR_RGB;
5416 if (index == 0)
5417 {
5418 if (is_bg)
5419 type |= VTERM_COLOR_DEFAULT_BG;
5420 else
5421 type |= VTERM_COLOR_DEFAULT_FG;
5422 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005423 }
5424 else
5425 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005426 type = VTERM_COLOR_INDEXED;
5427 index -= 1;
5428 }
5429 if (is_bg)
5430 {
5431 cell.bg.type = type;
5432 cell.bg.red = red;
5433 cell.bg.green = green;
5434 cell.bg.blue = blue;
5435 cell.bg.index = index;
5436 }
5437 else
5438 {
5439 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005440 cell.fg.red = red;
5441 cell.fg.green = green;
5442 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005443 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005444 }
5445 }
5446 else
5447 dump_is_corrupt(&ga_text);
5448 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005449 }
5450 else
5451 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005452 }
5453 else
5454 dump_is_corrupt(&ga_text);
5455
5456 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005457 if (cell.width == 2)
5458 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005459 }
5460 else if (c == '@')
5461 {
5462 if (prev_char == NULL)
5463 dump_is_corrupt(&ga_text);
5464 else
5465 {
5466 int count = 0;
5467
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005468 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005469 for (;;)
5470 {
5471 c = fgetc(fd);
5472 if (!isdigit(c))
5473 break;
5474 count = count * 10 + (c - '0');
5475 }
5476
5477 while (count-- > 0)
5478 {
5479 ga_concat(&ga_text, prev_char);
5480 append_cell(&ga_cell, &cell);
5481 }
5482 }
5483 }
5484 else
5485 {
5486 dump_is_corrupt(&ga_text);
5487 c = fgetc(fd);
5488 }
5489 }
5490
5491 if (ga_text.ga_len > 0)
5492 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005493 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005494 dump_is_corrupt(&ga_text);
5495 ga_append(&ga_text, NUL);
5496 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5497 ga_text.ga_len, FALSE);
5498 }
5499
5500 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005501 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005502 vim_free(prev_char);
5503
5504 return max_cells;
5505}
5506
5507/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005508 * Return an allocated string with at least "text_width" "=" characters and
5509 * "fname" inserted in the middle.
5510 */
5511 static char_u *
5512get_separator(int text_width, char_u *fname)
5513{
5514 int width = MAX(text_width, curwin->w_width);
5515 char_u *textline;
5516 int fname_size;
5517 char_u *p = fname;
5518 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005519 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005520
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005521 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005522 if (textline == NULL)
5523 return NULL;
5524
5525 fname_size = vim_strsize(fname);
5526 if (fname_size < width - 8)
5527 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005528 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005529 width = MAX(text_width, fname_size + 8);
5530 }
5531 else if (fname_size > width - 8)
5532 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005533 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005534 p = gettail(fname);
5535 fname_size = vim_strsize(p);
5536 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005537 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005538 while (fname_size > width - 8)
5539 {
5540 p += (*mb_ptr2len)(p);
5541 fname_size = vim_strsize(p);
5542 }
5543
5544 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5545 textline[i] = '=';
5546 textline[i++] = ' ';
5547
5548 STRCPY(textline + i, p);
5549 off = STRLEN(textline);
5550 textline[off] = ' ';
5551 for (i = 1; i < (width - fname_size) / 2; ++i)
5552 textline[off + i] = '=';
5553 textline[off + i] = NUL;
5554
5555 return textline;
5556}
5557
5558/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005559 * Common for "term_dumpdiff()" and "term_dumpload()".
5560 */
5561 static void
5562term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5563{
5564 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005565 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005566 char_u buf1[NUMBUFLEN];
5567 char_u buf2[NUMBUFLEN];
5568 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005569 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005570 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005571 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005572 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005573 char_u *textline = NULL;
5574
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005575 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005576 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005577 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005578 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005579 if (fname1 == NULL || (do_diff && fname2 == NULL))
5580 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005581 emsg(_(e_invalid_argument));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005582 return;
5583 }
5584 fd1 = mch_fopen((char *)fname1, READBIN);
5585 if (fd1 == NULL)
5586 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005587 semsg(_(e_cant_read_file_str), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005588 return;
5589 }
5590 if (do_diff)
5591 {
5592 fd2 = mch_fopen((char *)fname2, READBIN);
5593 if (fd2 == NULL)
5594 {
5595 fclose(fd1);
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005596 semsg(_(e_cant_read_file_str), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005597 return;
5598 }
5599 }
5600
5601 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005602 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5603 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5604 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5605 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5606 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005607
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005608 if (opt.jo_term_name == NULL)
5609 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005610 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005611
Bram Moolenaar51e14382019-05-25 20:21:28 +02005612 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005613 if (fname_tofree != NULL)
5614 {
5615 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5616 opt.jo_term_name = fname_tofree;
5617 }
5618 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005619
Bram Moolenaar87abab92019-06-03 21:14:59 +02005620 if (opt.jo_bufnr_buf != NULL)
5621 {
5622 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5623
5624 // With "bufnr" argument: enter the window with this buffer and make it
5625 // empty.
5626 if (wp == NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005627 semsg(_(e_invalid_argument_str), "bufnr");
Bram Moolenaar87abab92019-06-03 21:14:59 +02005628 else
5629 {
5630 buf = curbuf;
5631 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005632 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005633 free_scrollback(curbuf->b_term);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005634 redraw_later(UPD_NOT_VALID);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005635 }
5636 }
5637 else
5638 // Create a new terminal window.
5639 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5640
Bram Moolenaard96ff162018-02-18 22:13:29 +01005641 if (buf != NULL && buf->b_term != NULL)
5642 {
5643 int i;
5644 linenr_T bot_lnum;
5645 linenr_T lnum;
5646 term_T *term = buf->b_term;
5647 int width;
5648 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005649 VTermPos cursor_pos1;
5650 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005651
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005652 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005653
Bram Moolenaard96ff162018-02-18 22:13:29 +01005654 rettv->vval.v_number = buf->b_fnum;
5655
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005656 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005657 width = read_dump_file(fd1, &cursor_pos1);
5658
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005659 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005660 if (cursor_pos1.row >= 0)
5661 {
5662 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5663 coladvance(cursor_pos1.col);
5664 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005665
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005666 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005667 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005668
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005669 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005670 if (!do_diff)
5671 goto theend;
5672
5673 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5674
Bram Moolenaar4a696342018-04-05 18:45:26 +02005675 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005676 if (textline == NULL)
5677 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005678 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5679 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5680 vim_free(textline);
5681
5682 textline = get_separator(width, fname2);
5683 if (textline == NULL)
5684 goto theend;
5685 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5686 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005687 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005688
5689 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005690 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005691 if (width2 > width)
5692 {
5693 vim_free(textline);
5694 textline = alloc(width2 + 1);
5695 if (textline == NULL)
5696 goto theend;
5697 width = width2;
5698 textline[width] = NUL;
5699 }
5700 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5701
5702 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5703 {
5704 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5705 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005706 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005707 for (i = 0; i < width; ++i)
5708 textline[i] = '-';
5709 }
5710 else
5711 {
5712 char_u *line1;
5713 char_u *line2;
5714 char_u *p1;
5715 char_u *p2;
5716 int col;
5717 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5718 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5719 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5720 ->sb_cells;
5721
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005722 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005723 line1 = vim_strsave(ml_get(lnum));
5724 if (line1 == NULL)
5725 break;
5726 p1 = line1;
5727
5728 line2 = ml_get(lnum + bot_lnum);
5729 p2 = line2;
5730 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5731 {
5732 int len1 = utfc_ptr2len(p1);
5733 int len2 = utfc_ptr2len(p2);
5734
5735 textline[col] = ' ';
5736 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005737 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005738 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005739 else if (lnum == cursor_pos1.row + 1
5740 && col == cursor_pos1.col
5741 && (cursor_pos1.row != cursor_pos2.row
5742 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005743 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005744 textline[col] = '>';
5745 else if (lnum == cursor_pos2.row + 1
5746 && col == cursor_pos2.col
5747 && (cursor_pos1.row != cursor_pos2.row
5748 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005749 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005750 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005751 else if (cellattr1 != NULL && cellattr2 != NULL)
5752 {
5753 if ((cellattr1 + col)->width
5754 != (cellattr2 + col)->width)
5755 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005756 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005757 &(cellattr2 + col)->fg))
5758 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005759 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005760 &(cellattr2 + col)->bg))
5761 textline[col] = 'b';
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005762 else if (vtermAttr2hl(&(cellattr1 + col)->attrs)
5763 != vtermAttr2hl(&((cellattr2 + col)->attrs)))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005764 textline[col] = 'a';
5765 }
5766 p1 += len1;
5767 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005768 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005769 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005770
5771 while (col < width)
5772 {
5773 if (*p1 == NUL && *p2 == NUL)
5774 textline[col] = '?';
5775 else if (*p1 == NUL)
5776 {
5777 textline[col] = '+';
5778 p2 += utfc_ptr2len(p2);
5779 }
5780 else
5781 {
5782 textline[col] = '-';
5783 p1 += utfc_ptr2len(p1);
5784 }
5785 ++col;
5786 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005787
5788 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005789 }
5790 if (add_empty_scrollback(term, &term->tl_default_color,
5791 term->tl_top_diff_rows) == OK)
5792 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5793 ++bot_lnum;
5794 }
5795
5796 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5797 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005798 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005799 for (i = 0; i < width; ++i)
5800 textline[i] = '+';
5801 if (add_empty_scrollback(term, &term->tl_default_color,
5802 term->tl_top_diff_rows) == OK)
5803 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5804 ++lnum;
5805 ++bot_lnum;
5806 }
5807
5808 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005809
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005810 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005811 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005812 }
5813
5814theend:
5815 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005816 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005817 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005818 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005819 fclose(fd2);
5820}
5821
5822/*
5823 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5824 * bottom files.
5825 * Return FAIL when this is not possible.
5826 */
5827 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00005828term_swap_diff(void)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005829{
5830 term_T *term = curbuf->b_term;
5831 linenr_T line_count;
5832 linenr_T top_rows;
5833 linenr_T bot_rows;
5834 linenr_T bot_start;
5835 linenr_T lnum;
5836 char_u *p;
5837 sb_line_T *sb_line;
5838
5839 if (term == NULL
5840 || !term_is_finished(curbuf)
5841 || term->tl_top_diff_rows == 0
5842 || term->tl_scrollback.ga_len == 0)
5843 return FAIL;
5844
5845 line_count = curbuf->b_ml.ml_line_count;
5846 top_rows = term->tl_top_diff_rows;
5847 bot_rows = term->tl_bot_diff_rows;
5848 bot_start = line_count - bot_rows;
5849 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5850
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005851 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005852 for (lnum = 1; lnum <= top_rows; ++lnum)
5853 {
5854 p = vim_strsave(ml_get(1));
5855 if (p == NULL)
5856 return OK;
5857 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005858 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005859 vim_free(p);
5860 }
5861
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005862 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005863 for (lnum = 1; lnum <= bot_rows; ++lnum)
5864 {
5865 p = vim_strsave(ml_get(bot_start + lnum));
5866 if (p == NULL)
5867 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005868 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005869 ml_append(lnum - 1, p, 0, FALSE);
5870 vim_free(p);
5871 }
5872
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005873 // move top title to bottom
5874 p = vim_strsave(ml_get(bot_rows + 1));
5875 if (p == NULL)
5876 return OK;
5877 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005878 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005879 vim_free(p);
5880
5881 // move bottom title to top
5882 p = vim_strsave(ml_get(line_count - top_rows));
5883 if (p == NULL)
5884 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005885 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005886 ml_append(bot_rows, p, 0, FALSE);
5887 vim_free(p);
5888
Bram Moolenaard96ff162018-02-18 22:13:29 +01005889 if (top_rows == bot_rows)
5890 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005891 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005892 for (lnum = 0; lnum < top_rows; ++lnum)
5893 {
5894 sb_line_T temp;
5895
5896 temp = *(sb_line + lnum);
5897 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5898 *(sb_line + bot_start + lnum) = temp;
5899 }
5900 }
5901 else
5902 {
5903 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005904 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005905
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005906 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005907 if (temp != NULL)
5908 {
5909 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5910 mch_memmove(term->tl_scrollback.ga_data,
5911 temp + bot_start,
5912 sizeof(sb_line_T) * bot_rows);
5913 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5914 temp + top_rows,
5915 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5916 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5917 + line_count - top_rows,
5918 temp,
5919 sizeof(sb_line_T) * top_rows);
5920 vim_free(temp);
5921 }
5922 }
5923
5924 term->tl_top_diff_rows = bot_rows;
5925 term->tl_bot_diff_rows = top_rows;
5926
Bram Moolenaara4d158b2022-08-14 14:17:45 +01005927 update_screen(UPD_NOT_VALID);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005928 return OK;
5929}
5930
5931/*
5932 * "term_dumpdiff(filename, filename, options)" function
5933 */
5934 void
5935f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5936{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005937 if (in_vim9script()
5938 && (check_for_string_arg(argvars, 0) == FAIL
5939 || check_for_string_arg(argvars, 1) == FAIL
5940 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5941 return;
5942
Bram Moolenaard96ff162018-02-18 22:13:29 +01005943 term_load_dump(argvars, rettv, TRUE);
5944}
5945
5946/*
5947 * "term_dumpload(filename, options)" function
5948 */
5949 void
5950f_term_dumpload(typval_T *argvars, typval_T *rettv)
5951{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005952 if (in_vim9script()
5953 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005954 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005955 return;
5956
Bram Moolenaard96ff162018-02-18 22:13:29 +01005957 term_load_dump(argvars, rettv, FALSE);
5958}
5959
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005960/*
5961 * "term_getaltscreen(buf)" function
5962 */
5963 void
5964f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5965{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005966 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005967
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005968 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5969 return;
5970
5971 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005972 if (buf == NULL)
5973 return;
5974 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5975}
5976
5977/*
5978 * "term_getattr(attr, name)" function
5979 */
5980 void
5981f_term_getattr(typval_T *argvars, typval_T *rettv)
5982{
5983 int attr;
5984 size_t i;
5985 char_u *name;
5986
5987 static struct {
5988 char *name;
5989 int attr;
5990 } attrs[] = {
5991 {"bold", HL_BOLD},
5992 {"italic", HL_ITALIC},
5993 {"underline", HL_UNDERLINE},
5994 {"strike", HL_STRIKETHROUGH},
5995 {"reverse", HL_INVERSE},
5996 };
5997
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005998 if (in_vim9script()
5999 && (check_for_number_arg(argvars, 0) == FAIL
6000 || check_for_string_arg(argvars, 1) == FAIL))
6001 return;
6002
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006003 attr = tv_get_number(&argvars[0]);
6004 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006005 if (name == NULL)
6006 return;
6007
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02006008 if (attr > HL_ALL)
6009 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02006010 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006011 if (STRCMP(name, attrs[i].name) == 0)
6012 {
6013 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
6014 break;
6015 }
6016}
6017
6018/*
6019 * "term_getcursor(buf)" function
6020 */
6021 void
6022f_term_getcursor(typval_T *argvars, typval_T *rettv)
6023{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006024 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006025 term_T *term;
6026 list_T *l;
6027 dict_T *d;
6028
6029 if (rettv_list_alloc(rettv) == FAIL)
6030 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006031
6032 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6033 return;
6034
6035 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006036 if (buf == NULL)
6037 return;
6038 term = buf->b_term;
6039
6040 l = rettv->vval.v_list;
6041 list_append_number(l, term->tl_cursor_pos.row + 1);
6042 list_append_number(l, term->tl_cursor_pos.col + 1);
6043
6044 d = dict_alloc();
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00006045 if (d == NULL)
6046 return;
6047
6048 dict_add_number(d, "visible", term->tl_cursor_visible);
6049 dict_add_number(d, "blink", blink_state_is_inverted()
6050 ? !term->tl_cursor_blink : term->tl_cursor_blink);
6051 dict_add_number(d, "shape", term->tl_cursor_shape);
6052 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
6053 list_append_dict(l, d);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006054}
6055
6056/*
6057 * "term_getjob(buf)" function
6058 */
6059 void
6060f_term_getjob(typval_T *argvars, typval_T *rettv)
6061{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006062 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006063
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006064 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6065 return;
6066
6067 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006068 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01006069 {
6070 rettv->v_type = VAR_SPECIAL;
6071 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006072 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01006073 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006074
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01006075 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006076 rettv->vval.v_job = buf->b_term->tl_job;
6077 if (rettv->vval.v_job != NULL)
6078 ++rettv->vval.v_job->jv_refcount;
6079}
6080
6081 static int
6082get_row_number(typval_T *tv, term_T *term)
6083{
6084 if (tv->v_type == VAR_STRING
6085 && tv->vval.v_string != NULL
6086 && STRCMP(tv->vval.v_string, ".") == 0)
6087 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006088 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006089}
6090
6091/*
6092 * "term_getline(buf, row)" function
6093 */
6094 void
6095f_term_getline(typval_T *argvars, typval_T *rettv)
6096{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006097 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006098 term_T *term;
6099 int row;
6100
6101 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006102
6103 if (in_vim9script()
6104 && (check_for_buffer_arg(argvars, 0) == FAIL
6105 || check_for_lnum_arg(argvars, 1) == FAIL))
6106 return;
6107
6108 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006109 if (buf == NULL)
6110 return;
6111 term = buf->b_term;
6112 row = get_row_number(&argvars[1], term);
6113
6114 if (term->tl_vterm == NULL)
6115 {
6116 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
6117
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006118 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006119 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
6120 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
6121 }
6122 else
6123 {
6124 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
6125 VTermRect rect;
6126 int len;
6127 char_u *p;
6128
6129 if (row < 0 || row >= term->tl_rows)
6130 return;
6131 len = term->tl_cols * MB_MAXBYTES + 1;
6132 p = alloc(len);
6133 if (p == NULL)
6134 return;
6135 rettv->vval.v_string = p;
6136
6137 rect.start_col = 0;
6138 rect.end_col = term->tl_cols;
6139 rect.start_row = row;
6140 rect.end_row = row + 1;
6141 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
6142 }
6143}
6144
6145/*
6146 * "term_getscrolled(buf)" function
6147 */
6148 void
6149f_term_getscrolled(typval_T *argvars, typval_T *rettv)
6150{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006151 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006152
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006153 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6154 return;
6155
6156 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006157 if (buf == NULL)
6158 return;
6159 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
6160}
6161
6162/*
6163 * "term_getsize(buf)" function
6164 */
6165 void
6166f_term_getsize(typval_T *argvars, typval_T *rettv)
6167{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006168 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006169 list_T *l;
6170
6171 if (rettv_list_alloc(rettv) == FAIL)
6172 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006173
6174 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6175 return;
6176
6177 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006178 if (buf == NULL)
6179 return;
6180
6181 l = rettv->vval.v_list;
6182 list_append_number(l, buf->b_term->tl_rows);
6183 list_append_number(l, buf->b_term->tl_cols);
6184}
6185
6186/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02006187 * "term_setsize(buf, rows, cols)" function
6188 */
6189 void
6190f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6191{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006192 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02006193 term_T *term;
6194 varnumber_T rows, cols;
6195
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006196 if (in_vim9script()
6197 && (check_for_buffer_arg(argvars, 0) == FAIL
6198 || check_for_number_arg(argvars, 1) == FAIL
6199 || check_for_number_arg(argvars, 2) == FAIL))
6200 return;
6201
6202 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02006203 if (buf == NULL)
6204 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006205 emsg(_(e_not_terminal_buffer));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02006206 return;
6207 }
6208 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02006209 return;
6210 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006211 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02006212 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006213 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02006214 cols = cols <= 0 ? term->tl_cols : cols;
6215 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006216 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02006217
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006218 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02006219 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
6220 term_report_winsize(term, term->tl_rows, term->tl_cols);
6221}
6222
6223/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006224 * "term_getstatus(buf)" function
6225 */
6226 void
6227f_term_getstatus(typval_T *argvars, typval_T *rettv)
6228{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006229 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006230 term_T *term;
6231 char_u val[100];
6232
6233 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006234
6235 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6236 return;
6237
6238 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006239 if (buf == NULL)
6240 return;
6241 term = buf->b_term;
6242
6243 if (term_job_running(term))
6244 STRCPY(val, "running");
6245 else
6246 STRCPY(val, "finished");
6247 if (term->tl_normal_mode)
6248 STRCAT(val, ",normal");
6249 rettv->vval.v_string = vim_strsave(val);
6250}
6251
6252/*
6253 * "term_gettitle(buf)" function
6254 */
6255 void
6256f_term_gettitle(typval_T *argvars, typval_T *rettv)
6257{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006258 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006259
6260 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006261
6262 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6263 return;
6264
6265 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006266 if (buf == NULL)
6267 return;
6268
6269 if (buf->b_term->tl_title != NULL)
6270 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
6271}
6272
6273/*
6274 * "term_gettty(buf)" function
6275 */
6276 void
6277f_term_gettty(typval_T *argvars, typval_T *rettv)
6278{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006279 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02006280 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006281 int num = 0;
6282
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006283 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006284 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006285 || check_for_opt_bool_arg(argvars, 1) == FAIL))
6286 return;
6287
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006288 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006289 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006290 if (buf == NULL)
6291 return;
6292 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02006293 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006294
6295 switch (num)
6296 {
6297 case 0:
6298 if (buf->b_term->tl_job != NULL)
6299 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006300 break;
6301 case 1:
6302 if (buf->b_term->tl_job != NULL)
6303 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006304 break;
6305 default:
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006306 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006307 return;
6308 }
6309 if (p != NULL)
6310 rettv->vval.v_string = vim_strsave(p);
6311}
6312
6313/*
6314 * "term_list()" function
6315 */
6316 void
6317f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
6318{
6319 term_T *tp;
6320 list_T *l;
6321
6322 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
6323 return;
6324
6325 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006326 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02006327 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006328 if (list_append_number(l,
6329 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6330 return;
6331}
6332
6333/*
6334 * "term_scrape(buf, row)" function
6335 */
6336 void
6337f_term_scrape(typval_T *argvars, typval_T *rettv)
6338{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006339 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006340 VTermScreen *screen = NULL;
6341 VTermPos pos;
6342 list_T *l;
6343 term_T *term;
6344 char_u *p;
6345 sb_line_T *line;
6346
6347 if (rettv_list_alloc(rettv) == FAIL)
6348 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006349
6350 if (in_vim9script()
6351 && (check_for_buffer_arg(argvars, 0) == FAIL
6352 || check_for_lnum_arg(argvars, 1) == FAIL))
6353 return;
6354
6355 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006356 if (buf == NULL)
6357 return;
6358 term = buf->b_term;
6359
6360 l = rettv->vval.v_list;
6361 pos.row = get_row_number(&argvars[1], term);
6362
6363 if (term->tl_vterm != NULL)
6364 {
6365 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006366 if (screen == NULL) // can't really happen
6367 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006368 p = NULL;
6369 line = NULL;
6370 }
6371 else
6372 {
6373 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6374
6375 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6376 return;
6377 p = ml_get_buf(buf, lnum + 1, FALSE);
6378 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6379 }
6380
6381 for (pos.col = 0; pos.col < term->tl_cols; )
6382 {
6383 dict_T *dcell;
6384 int width;
6385 VTermScreenCellAttrs attrs;
6386 VTermColor fg, bg;
6387 char_u rgb[8];
6388 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6389 int off = 0;
6390 int i;
6391
6392 if (screen == NULL)
6393 {
6394 cellattr_T *cellattr;
6395 int len;
6396
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006397 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006398 if (pos.col >= line->sb_cols)
6399 break;
6400 cellattr = line->sb_cells + pos.col;
6401 width = cellattr->width;
6402 attrs = cellattr->attrs;
6403 fg = cellattr->fg;
6404 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006405 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006406 mch_memmove(mbs, p, len);
6407 mbs[len] = NUL;
6408 p += len;
6409 }
6410 else
6411 {
6412 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006413
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006414 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6415 break;
6416 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6417 {
6418 if (cell.chars[i] == 0)
6419 break;
6420 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6421 }
6422 mbs[off] = NUL;
6423 width = cell.width;
6424 attrs = cell.attrs;
6425 fg = cell.fg;
6426 bg = cell.bg;
6427 }
6428 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006429 if (dcell == NULL)
6430 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006431 list_append_dict(l, dcell);
6432
Bram Moolenaare0be1672018-07-08 16:50:37 +02006433 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006434
6435 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6436 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006437 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006438 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6439 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006440 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006441
Bram Moolenaar87fd0922021-11-20 13:47:45 +00006442 dict_add_number(dcell, "attr",
6443 cell2attr(term, NULL, &attrs, &fg, &bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006444 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006445
6446 ++pos.col;
6447 if (width == 2)
6448 ++pos.col;
6449 }
6450}
6451
6452/*
6453 * "term_sendkeys(buf, keys)" function
6454 */
6455 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006456f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006457{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006458 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006459 char_u *msg;
6460 term_T *term;
6461
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006462 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006463 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006464 || check_for_string_arg(argvars, 1) == FAIL))
6465 return;
6466
6467 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006468 if (buf == NULL)
6469 return;
6470
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006471 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006472 if (msg == NULL)
6473 return;
6474 term = buf->b_term;
6475 if (term->tl_vterm == NULL)
6476 return;
6477
6478 while (*msg != NUL)
6479 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006480 int c;
6481
6482 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6483 {
6484 c = TO_SPECIAL(msg[1], msg[2]);
6485 msg += 3;
6486 }
6487 else
6488 {
6489 c = PTR2CHAR(msg);
6490 msg += MB_CPTR2LEN(msg);
6491 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006492 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006493 }
6494}
6495
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006496#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6497/*
6498 * "term_getansicolors(buf)" function
6499 */
6500 void
6501f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6502{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006503 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006504 term_T *term;
6505 VTermState *state;
6506 VTermColor color;
6507 char_u hexbuf[10];
6508 int index;
6509 list_T *list;
6510
6511 if (rettv_list_alloc(rettv) == FAIL)
6512 return;
6513
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006514 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6515 return;
6516
6517 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006518 if (buf == NULL)
6519 return;
6520 term = buf->b_term;
6521 if (term->tl_vterm == NULL)
6522 return;
6523
6524 list = rettv->vval.v_list;
6525 state = vterm_obtain_state(term->tl_vterm);
6526 for (index = 0; index < 16; index++)
6527 {
6528 vterm_state_get_palette_color(state, index, &color);
6529 sprintf((char *)hexbuf, "#%02x%02x%02x",
6530 color.red, color.green, color.blue);
6531 if (list_append_string(list, hexbuf, 7) == FAIL)
6532 return;
6533 }
6534}
6535
6536/*
6537 * "term_setansicolors(buf, list)" function
6538 */
6539 void
6540f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6541{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006542 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006543 term_T *term;
LemonBoyb2b3acb2022-05-20 10:10:34 +01006544 listitem_T *li;
6545 int n = 0;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006546
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006547 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006548 && (check_for_buffer_arg(argvars, 0) == FAIL
6549 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006550 return;
6551
6552 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006553 if (buf == NULL)
6554 return;
6555 term = buf->b_term;
6556 if (term->tl_vterm == NULL)
6557 return;
6558
Bram Moolenaard83392a2022-09-01 12:22:46 +01006559 if (check_for_nonnull_list_arg(argvars, 1) == FAIL)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006560 return;
Bram Moolenaard83392a2022-09-01 12:22:46 +01006561
LemonBoyb2b3acb2022-05-20 10:10:34 +01006562 if (argvars[1].vval.v_list->lv_first == &range_list_item
6563 || argvars[1].vval.v_list->lv_len != 16)
6564 {
Bram Moolenaar23919542023-05-05 22:12:22 +01006565 semsg(_(e_invalid_value_for_argument_str), "\"colors\"");
LemonBoyb2b3acb2022-05-20 10:10:34 +01006566 return;
6567 }
6568
6569 if (term->tl_palette == NULL)
6570 term->tl_palette = ALLOC_MULT(long_u, 16);
6571 if (term->tl_palette == NULL)
6572 return;
6573
6574 FOR_ALL_LIST_ITEMS(argvars[1].vval.v_list, li)
6575 {
6576 char_u *color_name;
6577 guicolor_T guicolor;
6578
6579 color_name = tv_get_string_chk(&li->li_tv);
6580 if (color_name == NULL)
6581 return;
6582
6583 guicolor = GUI_GET_COLOR(color_name);
6584 if (guicolor == INVALCOLOR)
6585 {
6586 semsg(_(e_cannot_allocate_color_str), color_name);
6587 return;
6588 }
6589
6590 term->tl_palette[n++] = GUI_MCH_GET_RGB(guicolor);
6591 }
6592
6593 term_update_palette(term);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006594}
6595#endif
6596
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006597/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006598 * "term_setapi(buf, api)" function
6599 */
6600 void
6601f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6602{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006603 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006604 term_T *term;
6605 char_u *api;
6606
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006607 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006608 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006609 || check_for_string_arg(argvars, 1) == FAIL))
6610 return;
6611
6612 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006613 if (buf == NULL)
6614 return;
6615 term = buf->b_term;
6616 vim_free(term->tl_api);
6617 api = tv_get_string_chk(&argvars[1]);
6618 if (api != NULL)
6619 term->tl_api = vim_strsave(api);
6620 else
6621 term->tl_api = NULL;
6622}
6623
6624/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006625 * "term_setrestore(buf, command)" function
6626 */
6627 void
6628f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6629{
6630#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006631 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006632 term_T *term;
6633 char_u *cmd;
6634
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006635 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006636 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006637 || check_for_string_arg(argvars, 1) == FAIL))
6638 return;
6639
6640 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006641 if (buf == NULL)
6642 return;
6643 term = buf->b_term;
6644 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006645 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006646 if (cmd != NULL)
6647 term->tl_command = vim_strsave(cmd);
6648 else
6649 term->tl_command = NULL;
6650#endif
6651}
6652
6653/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006654 * "term_setkill(buf, how)" function
6655 */
6656 void
6657f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6658{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006659 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006660 term_T *term;
6661 char_u *how;
6662
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006663 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006664 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006665 || check_for_string_arg(argvars, 1) == FAIL))
6666 return;
6667
6668 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006669 if (buf == NULL)
6670 return;
6671 term = buf->b_term;
6672 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006673 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006674 if (how != NULL)
6675 term->tl_kill = vim_strsave(how);
6676 else
6677 term->tl_kill = NULL;
6678}
6679
6680/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006681 * "term_start(command, options)" function
6682 */
6683 void
6684f_term_start(typval_T *argvars, typval_T *rettv)
6685{
6686 jobopt_T opt;
6687 buf_T *buf;
6688
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006689 if (in_vim9script()
6690 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6691 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6692 return;
6693
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006694 init_job_options(&opt);
6695 if (argvars[1].v_type != VAR_UNKNOWN
6696 && get_job_options(&argvars[1], &opt,
6697 JO_TIMEOUT_ALL + JO_STOPONEXIT
6698 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6699 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6700 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6701 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006702 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006703 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006704 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006705 return;
6706
Bram Moolenaar13568252018-03-16 20:46:58 +01006707 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006708
6709 if (buf != NULL && buf->b_term != NULL)
6710 rettv->vval.v_number = buf->b_fnum;
6711}
6712
6713/*
6714 * "term_wait" function
6715 */
6716 void
6717f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6718{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006719 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006720
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006721 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006722 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006723 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006724 return;
6725
6726 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006727 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006728 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006729 if (buf->b_term->tl_job == NULL)
6730 {
6731 ch_log(NULL, "term_wait(): no job to wait for");
6732 return;
6733 }
6734 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006735 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006736 return;
6737
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006738 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006739 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006740 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6741 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006742 // The job is dead, keep reading channel I/O until the channel is
6743 // closed. buf->b_term may become NULL if the terminal was closed while
6744 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006745 ch_log(NULL, "term_wait(): waiting for channel to close");
6746 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6747 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006748 term_flush_messages();
6749
Bram Moolenaard45aa552018-05-21 22:50:29 +02006750 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006751 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006752 // If the terminal is closed when the channel is closed the
6753 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006754 break;
Bram Moolenaareea32af2021-11-21 14:51:13 +00006755 if (buf->b_term == NULL || buf->b_term->tl_channel_closing)
6756 // came here from a close callback, only wait one time
6757 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006758 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006759
6760 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006761 }
6762 else
6763 {
6764 long wait = 10L;
6765
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006766 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006767
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006768 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006769 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006770 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006771 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006772
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006773 // Flushing messages on channels is hopefully sufficient.
6774 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006775 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006776 }
6777}
6778
6779/*
6780 * Called when a channel has sent all the lines to a terminal.
6781 * Send a CTRL-D to mark the end of the text.
6782 */
6783 void
6784term_send_eof(channel_T *ch)
6785{
6786 term_T *term;
6787
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006788 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006789 if (term->tl_job == ch->ch_job)
6790 {
6791 if (term->tl_eof_chars != NULL)
6792 {
6793 channel_send(ch, PART_IN, term->tl_eof_chars,
6794 (int)STRLEN(term->tl_eof_chars), NULL);
6795 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6796 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006797# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006798 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006799 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006800 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6801# endif
6802 }
6803}
6804
Bram Moolenaar113e1072019-01-20 15:30:40 +01006805#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006806 job_T *
6807term_getjob(term_T *term)
6808{
6809 return term != NULL ? term->tl_job : NULL;
6810}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006811#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006812
Bram Moolenaar4f974752019-02-17 17:44:42 +01006813# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006814
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006815///////////////////////////////////////
6816// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006817#ifdef PROTO
6818typedef int COORD;
6819typedef int DWORD;
6820typedef int HANDLE;
6821typedef int *DWORD_PTR;
6822typedef int HPCON;
6823typedef int HRESULT;
6824typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006825typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006826typedef int PSIZE_T;
6827typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006828typedef int BOOL;
6829# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006830#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006831
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006832HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6833HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6834HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006835BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6836BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6837void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006838
6839 static int
6840dyn_conpty_init(int verbose)
6841{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006842 static HMODULE hKerneldll = NULL;
6843 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006844 static struct
6845 {
6846 char *name;
6847 FARPROC *ptr;
6848 } conpty_entry[] =
6849 {
6850 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6851 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6852 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6853 {"InitializeProcThreadAttributeList",
6854 (FARPROC*)&pInitializeProcThreadAttributeList},
6855 {"UpdateProcThreadAttribute",
6856 (FARPROC*)&pUpdateProcThreadAttribute},
6857 {"DeleteProcThreadAttributeList",
6858 (FARPROC*)&pDeleteProcThreadAttributeList},
6859 {NULL, NULL}
6860 };
6861
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006862 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006863 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006864 if (verbose)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00006865 emsg(_(e_conpty_is_not_available));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006866 return FAIL;
6867 }
6868
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006869 // No need to initialize twice.
6870 if (hKerneldll)
6871 return OK;
6872
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006873 hKerneldll = vimLoadLib("kernel32.dll");
6874 for (i = 0; conpty_entry[i].name != NULL
6875 && conpty_entry[i].ptr != NULL; ++i)
6876 {
6877 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6878 conpty_entry[i].name)) == NULL)
6879 {
6880 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00006881 semsg(_(e_could_not_load_library_function_str), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006882 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006883 return FAIL;
6884 }
6885 }
6886
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006887 return OK;
6888}
6889
6890 static int
6891conpty_term_and_job_init(
6892 term_T *term,
6893 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006894 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006895 jobopt_T *opt,
6896 jobopt_T *orig_opt)
6897{
6898 WCHAR *cmd_wchar = NULL;
6899 WCHAR *cmd_wchar_copy = NULL;
6900 WCHAR *cwd_wchar = NULL;
6901 WCHAR *env_wchar = NULL;
6902 channel_T *channel = NULL;
6903 job_T *job = NULL;
6904 HANDLE jo = NULL;
6905 garray_T ga_cmd, ga_env;
6906 char_u *cmd = NULL;
6907 HRESULT hr;
6908 COORD consize;
6909 SIZE_T breq;
6910 PROCESS_INFORMATION proc_info;
6911 HANDLE i_theirs = NULL;
6912 HANDLE o_theirs = NULL;
6913 HANDLE i_ours = NULL;
6914 HANDLE o_ours = NULL;
6915
Bram Moolenaar04935fb2022-01-08 16:19:22 +00006916 ga_init2(&ga_cmd, sizeof(char*), 20);
6917 ga_init2(&ga_env, sizeof(char*), 20);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006918
6919 if (argvar->v_type == VAR_STRING)
6920 {
6921 cmd = argvar->vval.v_string;
6922 }
6923 else if (argvar->v_type == VAR_LIST)
6924 {
6925 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6926 goto failed;
6927 cmd = ga_cmd.ga_data;
6928 }
6929 if (cmd == NULL || *cmd == NUL)
6930 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006931 emsg(_(e_invalid_argument));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006932 goto failed;
6933 }
6934
6935 term->tl_arg0_cmd = vim_strsave(cmd);
6936
6937 cmd_wchar = enc_to_utf16(cmd, NULL);
6938
6939 if (cmd_wchar != NULL)
6940 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006941 // Request by CreateProcessW
6942 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006943 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006944 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6945 }
6946
6947 ga_clear(&ga_cmd);
6948 if (cmd_wchar == NULL)
6949 goto failed;
6950 if (opt->jo_cwd != NULL)
6951 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6952
6953 win32_build_env(opt->jo_env, &ga_env, TRUE);
6954 env_wchar = ga_env.ga_data;
6955
6956 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6957 goto failed;
6958 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6959 goto failed;
6960
6961 consize.X = term->tl_cols;
6962 consize.Y = term->tl_rows;
6963 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6964 &term->tl_conpty);
6965 if (FAILED(hr))
6966 goto failed;
6967
6968 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6969
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006970 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006971 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006972 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006973 if (!term->tl_siex.lpAttributeList)
6974 goto failed;
6975 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6976 0, &breq))
6977 goto failed;
6978 if (!pUpdateProcThreadAttribute(
6979 term->tl_siex.lpAttributeList, 0,
6980 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6981 sizeof(HPCON), NULL, NULL))
6982 goto failed;
6983
6984 channel = add_channel();
6985 if (channel == NULL)
6986 goto failed;
6987
6988 job = job_alloc();
6989 if (job == NULL)
6990 goto failed;
6991 if (argvar->v_type == VAR_STRING)
6992 {
6993 int argc;
6994
6995 build_argv_from_string(cmd, &job->jv_argv, &argc);
6996 }
6997 else
6998 {
6999 int argc;
7000
7001 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
7002 }
7003
7004 if (opt->jo_set & JO_IN_BUF)
7005 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
7006
7007 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
7008 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02007009 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007010 env_wchar, cwd_wchar,
7011 &term->tl_siex.StartupInfo, &proc_info))
7012 goto failed;
7013
7014 CloseHandle(i_theirs);
7015 CloseHandle(o_theirs);
7016
7017 channel_set_pipes(channel,
7018 (sock_T)i_ours,
7019 (sock_T)o_ours,
7020 (sock_T)o_ours);
7021
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007022 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007023 channel->ch_write_text_mode = TRUE;
7024
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007025 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007026 channel->ch_anonymous_pipe = TRUE;
7027
7028 jo = CreateJobObject(NULL, NULL);
7029 if (jo == NULL)
7030 goto failed;
7031
7032 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
7033 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007034 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007035 CloseHandle(jo);
7036 jo = NULL;
7037 }
7038
7039 ResumeThread(proc_info.hThread);
7040 CloseHandle(proc_info.hThread);
7041
7042 vim_free(cmd_wchar);
7043 vim_free(cmd_wchar_copy);
7044 vim_free(cwd_wchar);
7045 vim_free(env_wchar);
7046
7047 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7048 goto failed;
7049
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007050#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
LemonBoyb2b3acb2022-05-20 10:10:34 +01007051 if (term_use_palette())
7052 {
7053 if (term->tl_palette != NULL)
7054 set_vterm_palette(term->tl_vterm, term->tl_palette);
7055 else
7056 init_vterm_ansi_colors(term->tl_vterm);
7057 }
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007058#endif
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007059
7060 channel_set_job(channel, job, opt);
7061 job_set_options(job, opt);
7062
7063 job->jv_channel = channel;
7064 job->jv_proc_info = proc_info;
7065 job->jv_job_object = jo;
7066 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007067 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007068 ++job->jv_refcount;
7069 term->tl_job = job;
7070
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007071 // Redirecting stdout and stderr doesn't work at the job level. Instead
7072 // open the file here and handle it in. opt->jo_io was changed in
7073 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007074 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7075 {
7076 char_u *fname = opt->jo_io_name[PART_OUT];
7077
7078 ch_log(channel, "Opening output file %s", fname);
7079 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7080 if (term->tl_out_fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007081 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007082 }
7083
7084 return OK;
7085
7086failed:
7087 ga_clear(&ga_cmd);
7088 ga_clear(&ga_env);
7089 vim_free(cmd_wchar);
7090 vim_free(cmd_wchar_copy);
7091 vim_free(cwd_wchar);
7092 if (channel != NULL)
7093 channel_clear(channel);
7094 if (job != NULL)
7095 {
7096 job->jv_channel = NULL;
7097 job_cleanup(job);
7098 }
7099 term->tl_job = NULL;
7100 if (jo != NULL)
7101 CloseHandle(jo);
7102
7103 if (term->tl_siex.lpAttributeList != NULL)
7104 {
7105 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
7106 vim_free(term->tl_siex.lpAttributeList);
7107 }
7108 term->tl_siex.lpAttributeList = NULL;
7109 if (o_theirs != NULL)
7110 CloseHandle(o_theirs);
7111 if (o_ours != NULL)
7112 CloseHandle(o_ours);
7113 if (i_ours != NULL)
7114 CloseHandle(i_ours);
7115 if (i_theirs != NULL)
7116 CloseHandle(i_theirs);
7117 if (term->tl_conpty != NULL)
7118 pClosePseudoConsole(term->tl_conpty);
7119 term->tl_conpty = NULL;
7120 return FAIL;
7121}
7122
7123 static void
7124conpty_term_report_winsize(term_T *term, int rows, int cols)
7125{
7126 COORD consize;
7127
7128 consize.X = cols;
7129 consize.Y = rows;
7130 pResizePseudoConsole(term->tl_conpty, consize);
7131}
7132
Bram Moolenaar840d16f2019-09-10 21:27:18 +02007133 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007134term_free_conpty(term_T *term)
7135{
7136 if (term->tl_siex.lpAttributeList != NULL)
7137 {
7138 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
7139 vim_free(term->tl_siex.lpAttributeList);
7140 }
7141 term->tl_siex.lpAttributeList = NULL;
7142 if (term->tl_conpty != NULL)
7143 pClosePseudoConsole(term->tl_conpty);
7144 term->tl_conpty = NULL;
7145}
7146
7147 int
7148use_conpty(void)
7149{
7150 return has_conpty;
7151}
7152
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007153# ifndef PROTO
7154
7155#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
7156#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01007157#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007158
7159void* (*winpty_config_new)(UINT64, void*);
7160void* (*winpty_open)(void*, void*);
7161void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
7162BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
7163void (*winpty_config_set_mouse_mode)(void*, int);
7164void (*winpty_config_set_initial_size)(void*, int, int);
7165LPCWSTR (*winpty_conin_name)(void*);
7166LPCWSTR (*winpty_conout_name)(void*);
7167LPCWSTR (*winpty_conerr_name)(void*);
7168void (*winpty_free)(void*);
7169void (*winpty_config_free)(void*);
7170void (*winpty_spawn_config_free)(void*);
7171void (*winpty_error_free)(void*);
7172LPCWSTR (*winpty_error_msg)(void*);
7173BOOL (*winpty_set_size)(void*, int, int, void*);
7174HANDLE (*winpty_agent_process)(void*);
7175
7176#define WINPTY_DLL "winpty.dll"
7177
7178static HINSTANCE hWinPtyDLL = NULL;
7179# endif
7180
7181 static int
7182dyn_winpty_init(int verbose)
7183{
7184 int i;
7185 static struct
7186 {
7187 char *name;
7188 FARPROC *ptr;
7189 } winpty_entry[] =
7190 {
7191 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
7192 {"winpty_config_free", (FARPROC*)&winpty_config_free},
7193 {"winpty_config_new", (FARPROC*)&winpty_config_new},
7194 {"winpty_config_set_mouse_mode",
7195 (FARPROC*)&winpty_config_set_mouse_mode},
7196 {"winpty_config_set_initial_size",
7197 (FARPROC*)&winpty_config_set_initial_size},
7198 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
7199 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
7200 {"winpty_error_free", (FARPROC*)&winpty_error_free},
7201 {"winpty_free", (FARPROC*)&winpty_free},
7202 {"winpty_open", (FARPROC*)&winpty_open},
7203 {"winpty_spawn", (FARPROC*)&winpty_spawn},
7204 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
7205 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
7206 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
7207 {"winpty_set_size", (FARPROC*)&winpty_set_size},
7208 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
7209 {NULL, NULL}
7210 };
7211
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007212 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007213 if (hWinPtyDLL)
7214 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007215 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
7216 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007217 if (*p_winptydll != NUL)
7218 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
7219 if (!hWinPtyDLL)
7220 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
7221 if (!hWinPtyDLL)
7222 {
7223 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007224 semsg(_(e_could_not_load_library_str_str),
Martin Tournoij1a3e5742021-07-24 13:57:29 +02007225 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
7226 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007227 return FAIL;
7228 }
7229 for (i = 0; winpty_entry[i].name != NULL
7230 && winpty_entry[i].ptr != NULL; ++i)
7231 {
7232 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
7233 winpty_entry[i].name)) == NULL)
7234 {
7235 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007236 semsg(_(e_could_not_load_library_function_str), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01007237 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007238 return FAIL;
7239 }
7240 }
7241
7242 return OK;
7243}
7244
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007245 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007246winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007247 term_T *term,
7248 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007249 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007250 jobopt_T *opt,
7251 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007252{
7253 WCHAR *cmd_wchar = NULL;
7254 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007255 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007256 channel_T *channel = NULL;
7257 job_T *job = NULL;
7258 DWORD error;
7259 HANDLE jo = NULL;
7260 HANDLE child_process_handle;
7261 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01007262 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007263 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007264 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007265 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007266
Bram Moolenaar04935fb2022-01-08 16:19:22 +00007267 ga_init2(&ga_cmd, sizeof(char*), 20);
7268 ga_init2(&ga_env, sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007269
7270 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007271 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007272 cmd = argvar->vval.v_string;
7273 }
7274 else if (argvar->v_type == VAR_LIST)
7275 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007276 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007277 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007278 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007279 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007280 if (cmd == NULL || *cmd == NUL)
7281 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00007282 emsg(_(e_invalid_argument));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007283 goto failed;
7284 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007285
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007286 term->tl_arg0_cmd = vim_strsave(cmd);
7287
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007288 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007289 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007290 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007291 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007292 if (opt->jo_cwd != NULL)
7293 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007294
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01007295 win32_build_env(opt->jo_env, &ga_env, TRUE);
7296 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007297
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007298 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
7299 if (term->tl_winpty_config == NULL)
7300 goto failed;
7301
7302 winpty_config_set_mouse_mode(term->tl_winpty_config,
7303 WINPTY_MOUSE_MODE_FORCE);
7304 winpty_config_set_initial_size(term->tl_winpty_config,
7305 term->tl_cols, term->tl_rows);
7306 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
7307 if (term->tl_winpty == NULL)
7308 goto failed;
7309
7310 spawn_config = winpty_spawn_config_new(
7311 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
7312 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
7313 NULL,
7314 cmd_wchar,
7315 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01007316 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007317 &winpty_err);
7318 if (spawn_config == NULL)
7319 goto failed;
7320
7321 channel = add_channel();
7322 if (channel == NULL)
7323 goto failed;
7324
7325 job = job_alloc();
7326 if (job == NULL)
7327 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02007328 if (argvar->v_type == VAR_STRING)
7329 {
7330 int argc;
7331
7332 build_argv_from_string(cmd, &job->jv_argv, &argc);
7333 }
7334 else
7335 {
7336 int argc;
7337
7338 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
7339 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007340
7341 if (opt->jo_set & JO_IN_BUF)
7342 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
7343
7344 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
7345 &child_thread_handle, &error, &winpty_err))
7346 goto failed;
7347
7348 channel_set_pipes(channel,
7349 (sock_T)CreateFileW(
7350 winpty_conin_name(term->tl_winpty),
7351 GENERIC_WRITE, 0, NULL,
7352 OPEN_EXISTING, 0, NULL),
7353 (sock_T)CreateFileW(
7354 winpty_conout_name(term->tl_winpty),
7355 GENERIC_READ, 0, NULL,
7356 OPEN_EXISTING, 0, NULL),
7357 (sock_T)CreateFileW(
7358 winpty_conerr_name(term->tl_winpty),
7359 GENERIC_READ, 0, NULL,
7360 OPEN_EXISTING, 0, NULL));
7361
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007362 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007363 channel->ch_write_text_mode = TRUE;
7364
7365 jo = CreateJobObject(NULL, NULL);
7366 if (jo == NULL)
7367 goto failed;
7368
7369 if (!AssignProcessToJobObject(jo, child_process_handle))
7370 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007371 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007372 CloseHandle(jo);
7373 jo = NULL;
7374 }
7375
7376 winpty_spawn_config_free(spawn_config);
7377 vim_free(cmd_wchar);
7378 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007379 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007380
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007381 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7382 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007383
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007384#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
LemonBoyb2b3acb2022-05-20 10:10:34 +01007385 if (term_use_palette())
7386 {
7387 if (term->tl_palette != NULL)
7388 set_vterm_palette(term->tl_vterm, term->tl_palette);
7389 else
7390 init_vterm_ansi_colors(term->tl_vterm);
7391 }
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007392#endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007393
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007394 channel_set_job(channel, job, opt);
7395 job_set_options(job, opt);
7396
7397 job->jv_channel = channel;
7398 job->jv_proc_info.hProcess = child_process_handle;
7399 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7400 job->jv_job_object = jo;
7401 job->jv_status = JOB_STARTED;
7402 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007403 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007404 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007405 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007406 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007407 ++job->jv_refcount;
7408 term->tl_job = job;
7409
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007410 // Redirecting stdout and stderr doesn't work at the job level. Instead
7411 // open the file here and handle it in. opt->jo_io was changed in
7412 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007413 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7414 {
7415 char_u *fname = opt->jo_io_name[PART_OUT];
7416
7417 ch_log(channel, "Opening output file %s", fname);
7418 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7419 if (term->tl_out_fd == NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00007420 semsg(_(e_cant_open_file_str), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007421 }
7422
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007423 return OK;
7424
7425failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007426 ga_clear(&ga_cmd);
7427 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007428 vim_free(cmd_wchar);
7429 vim_free(cwd_wchar);
7430 if (spawn_config != NULL)
7431 winpty_spawn_config_free(spawn_config);
7432 if (channel != NULL)
7433 channel_clear(channel);
7434 if (job != NULL)
7435 {
7436 job->jv_channel = NULL;
7437 job_cleanup(job);
7438 }
7439 term->tl_job = NULL;
7440 if (jo != NULL)
7441 CloseHandle(jo);
7442 if (term->tl_winpty != NULL)
7443 winpty_free(term->tl_winpty);
7444 term->tl_winpty = NULL;
7445 if (term->tl_winpty_config != NULL)
7446 winpty_config_free(term->tl_winpty_config);
7447 term->tl_winpty_config = NULL;
7448 if (winpty_err != NULL)
7449 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007450 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007451 (short_u *)winpty_error_msg(winpty_err), NULL);
7452
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007453 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007454 winpty_error_free(winpty_err);
7455 }
7456 return FAIL;
7457}
7458
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007459/*
7460 * Create a new terminal of "rows" by "cols" cells.
7461 * Store a reference in "term".
7462 * Return OK or FAIL.
7463 */
7464 static int
7465term_and_job_init(
7466 term_T *term,
7467 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007468 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007469 jobopt_T *opt,
7470 jobopt_T *orig_opt)
7471{
7472 int use_winpty = FALSE;
7473 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007474 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007475
7476 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7477 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7478
7479 if (!has_winpty && !has_conpty)
7480 // If neither is available give the errors for winpty, since when
7481 // conpty is not available it can't be installed either.
7482 return dyn_winpty_init(TRUE);
7483
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007484 if (opt->jo_tty_type != NUL)
7485 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007486
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007487 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007488 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007489 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007490 use_conpty = TRUE;
7491 else if (has_winpty)
7492 use_winpty = TRUE;
7493 // else: error
7494 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007495 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007496 {
7497 if (has_winpty)
7498 use_winpty = TRUE;
7499 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007500 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007501 {
7502 if (has_conpty)
7503 use_conpty = TRUE;
7504 else
7505 return dyn_conpty_init(TRUE);
7506 }
7507
7508 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007509 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007510
7511 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007512 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007513
7514 // error
7515 return dyn_winpty_init(TRUE);
7516}
7517
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007518 static int
7519create_pty_only(term_T *term, jobopt_T *options)
7520{
7521 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7522 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7523 char in_name[80], out_name[80];
7524 channel_T *channel = NULL;
7525
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007526 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7527 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007528
7529 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7530 GetCurrentProcessId(),
7531 curbuf->b_fnum);
7532 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7533 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7534 PIPE_UNLIMITED_INSTANCES,
7535 0, 0, NMPWAIT_NOWAIT, NULL);
7536 if (hPipeIn == INVALID_HANDLE_VALUE)
7537 goto failed;
7538
7539 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7540 GetCurrentProcessId(),
7541 curbuf->b_fnum);
7542 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7543 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7544 PIPE_UNLIMITED_INSTANCES,
7545 0, 0, 0, NULL);
7546 if (hPipeOut == INVALID_HANDLE_VALUE)
7547 goto failed;
7548
7549 ConnectNamedPipe(hPipeIn, NULL);
7550 ConnectNamedPipe(hPipeOut, NULL);
7551
7552 term->tl_job = job_alloc();
7553 if (term->tl_job == NULL)
7554 goto failed;
7555 ++term->tl_job->jv_refcount;
7556
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007557 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007558 term->tl_job->jv_status = JOB_FINISHED;
7559
7560 channel = add_channel();
7561 if (channel == NULL)
7562 goto failed;
7563 term->tl_job->jv_channel = channel;
7564 channel->ch_keep_open = TRUE;
7565 channel->ch_named_pipe = TRUE;
7566
7567 channel_set_pipes(channel,
7568 (sock_T)hPipeIn,
7569 (sock_T)hPipeOut,
7570 (sock_T)hPipeOut);
7571 channel_set_job(channel, term->tl_job, options);
7572 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7573 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7574
7575 return OK;
7576
7577failed:
7578 if (hPipeIn != NULL)
7579 CloseHandle(hPipeIn);
7580 if (hPipeOut != NULL)
7581 CloseHandle(hPipeOut);
7582 return FAIL;
7583}
7584
7585/*
7586 * Free the terminal emulator part of "term".
7587 */
7588 static void
7589term_free_vterm(term_T *term)
7590{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007591 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007592 if (term->tl_winpty != NULL)
7593 winpty_free(term->tl_winpty);
7594 term->tl_winpty = NULL;
7595 if (term->tl_winpty_config != NULL)
7596 winpty_config_free(term->tl_winpty_config);
7597 term->tl_winpty_config = NULL;
7598 if (term->tl_vterm != NULL)
7599 vterm_free(term->tl_vterm);
7600 term->tl_vterm = NULL;
7601}
7602
7603/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007604 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007605 */
7606 static void
7607term_report_winsize(term_T *term, int rows, int cols)
7608{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007609 if (term->tl_conpty)
7610 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007611 if (term->tl_winpty)
7612 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7613}
7614
7615 int
7616terminal_enabled(void)
7617{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007618 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007619}
7620
7621# else
7622
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007623///////////////////////////////////////
7624// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007625
7626/*
7627 * Create a new terminal of "rows" by "cols" cells.
7628 * Start job for "cmd".
7629 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007630 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007631 * Return OK or FAIL.
7632 */
7633 static int
7634term_and_job_init(
7635 term_T *term,
7636 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007637 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007638 jobopt_T *opt,
7639 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007640{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007641 term->tl_arg0_cmd = NULL;
7642
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007643 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7644 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007645
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007646#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
LemonBoyb2b3acb2022-05-20 10:10:34 +01007647 if (term_use_palette())
7648 {
7649 if (term->tl_palette != NULL)
7650 set_vterm_palette(term->tl_vterm, term->tl_palette);
7651 else
7652 init_vterm_ansi_colors(term->tl_vterm);
7653 }
Bram Moolenaar30b9a412022-05-26 14:06:37 +01007654#endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007655
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007656 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007657 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007658 if (term->tl_job != NULL)
7659 ++term->tl_job->jv_refcount;
7660
7661 return term->tl_job != NULL
7662 && term->tl_job->jv_channel != NULL
7663 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7664}
7665
7666 static int
7667create_pty_only(term_T *term, jobopt_T *opt)
7668{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007669 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7670 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007671
7672 term->tl_job = job_alloc();
7673 if (term->tl_job == NULL)
7674 return FAIL;
7675 ++term->tl_job->jv_refcount;
7676
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007677 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007678 term->tl_job->jv_status = JOB_FINISHED;
7679
7680 return mch_create_pty_channel(term->tl_job, opt);
7681}
7682
7683/*
7684 * Free the terminal emulator part of "term".
7685 */
7686 static void
7687term_free_vterm(term_T *term)
7688{
7689 if (term->tl_vterm != NULL)
7690 vterm_free(term->tl_vterm);
7691 term->tl_vterm = NULL;
7692}
7693
7694/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007695 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007696 */
7697 static void
7698term_report_winsize(term_T *term, int rows, int cols)
7699{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007700 // Use an ioctl() to report the new window size to the job.
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007701 if (term->tl_job == NULL || term->tl_job->jv_channel == NULL)
7702 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007703
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007704 int fd = -1;
7705 int part;
7706
7707 for (part = PART_OUT; part < PART_COUNT; ++part)
7708 {
7709 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
7710 if (mch_isatty(fd))
7711 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007712 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +00007713 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7714 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007715}
7716
7717# endif
7718
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007719#endif // FEAT_TERMINAL