blob: 7fbff46171cb553e5f97914c0d89122571bed012 [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 Moolenaar2e6ab182017-09-20 10:03:07 +0200102 int tl_channel_closed;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200103 int tl_channel_recently_closed; // still need to handle tl_finish
104
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100105 int tl_finish;
106#define TL_FINISH_UNSET NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100107#define TL_FINISH_CLOSE 'c' // ++close or :terminal without argument
108#define TL_FINISH_NOCLOSE 'n' // ++noclose
109#define TL_FINISH_OPEN 'o' // ++open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200110 char_u *tl_opencmd;
111 char_u *tl_eof_chars;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200112 char_u *tl_api; // prefix for terminal API function
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200113
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100114 char_u *tl_arg0_cmd; // To format the status bar
115
Bram Moolenaar4f974752019-02-17 17:44:42 +0100116#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200117 void *tl_winpty_config;
118 void *tl_winpty;
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200119
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100120 HPCON tl_conpty;
121 DYN_STARTUPINFOEXW tl_siex; // Structure that always needs to be hold
122
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200123 FILE *tl_out_fd;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200124#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100125#if defined(FEAT_SESSION)
126 char_u *tl_command;
127#endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100128 char_u *tl_kill;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200129
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100130 // last known vterm size
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200131 int tl_rows;
132 int tl_cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200133
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100134 char_u *tl_title; // NULL or allocated
135 char_u *tl_status_text; // NULL or allocated
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200136
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100137 // Range of screen rows to update. Zero based.
138 int tl_dirty_row_start; // MAX_ROW if nothing dirty
139 int tl_dirty_row_end; // row below last one to update
140 int tl_dirty_snapshot; // text updated after making snapshot
Bram Moolenaar56bc8e22018-05-10 18:05:56 +0200141#ifdef FEAT_TIMERS
142 int tl_timer_set;
143 proftime_T tl_timer_due;
144#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100145 int tl_postponed_scroll; // to be scrolled up
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200146
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200147 garray_T tl_scrollback;
148 int tl_scrollback_scrolled;
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100149 garray_T tl_scrollback_postponed;
150
Bram Moolenaar83d47902020-03-26 20:34:00 +0100151 char_u *tl_highlight_name; // replaces "Terminal"; allocated
152
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200153 cellattr_T tl_default_color;
154
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100155 linenr_T tl_top_diff_rows; // rows of top diff file or zero
156 linenr_T tl_bot_diff_rows; // rows of bottom diff file
Bram Moolenaard96ff162018-02-18 22:13:29 +0100157
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200158 VTermPos tl_cursor_pos;
159 int tl_cursor_visible;
160 int tl_cursor_blink;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100161 int tl_cursor_shape; // 1: block, 2: underline, 3: bar
162 char_u *tl_cursor_color; // NULL or allocated
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200163
164 int tl_using_altscreen;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200165 garray_T tl_osc_buf; // incomplete OSC string
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200166};
167
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100168#define TMODE_ONCE 1 // CTRL-\ CTRL-N used
169#define TMODE_LOOP 2 // CTRL-W N used
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200170
171/*
172 * List of all active terminals.
173 */
174static term_T *first_term = NULL;
175
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100176// Terminal active in terminal_loop().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200177static term_T *in_terminal_loop = NULL;
178
Bram Moolenaar4f974752019-02-17 17:44:42 +0100179#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100180static BOOL has_winpty = FALSE;
181static BOOL has_conpty = FALSE;
182#endif
183
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100184#define MAX_ROW 999999 // used for tl_dirty_row_end to update all rows
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200185#define KEY_BUF_LEN 200
186
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200187#define FOR_ALL_TERMS(term) \
188 for ((term) = first_term; (term) != NULL; (term) = (term)->tl_next)
189
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200190/*
191 * Functions with separate implementation for MS-Windows and Unix-like systems.
192 */
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200193static 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 +0200194static int create_pty_only(term_T *term, jobopt_T *opt);
195static void term_report_winsize(term_T *term, int rows, int cols);
196static void term_free_vterm(term_T *term);
Bram Moolenaar13568252018-03-16 20:46:58 +0100197#ifdef FEAT_GUI
198static void update_system_term(term_T *term);
199#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200200
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100201static void handle_postponed_scrollback(term_T *term);
202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100203// The character that we know (or assume) that the terminal expects for the
204// backspace key.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200205static int term_backspace_char = BS;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200206
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100207// "Terminal" highlight group colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +0100208static int term_default_cterm_fg = -1;
209static int term_default_cterm_bg = -1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200210
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100211// Store the last set and the desired cursor properties, so that we only update
212// them when needed. Doing it unnecessary may result in flicker.
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200213static char_u *last_set_cursor_color = NULL;
214static char_u *desired_cursor_color = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +0100215static int last_set_cursor_shape = -1;
216static int desired_cursor_shape = -1;
217static int last_set_cursor_blink = -1;
218static int desired_cursor_blink = -1;
219
220
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100221///////////////////////////////////////
222// 1. Generic code for all systems.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200223
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200224 static int
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200225cursor_color_equal(char_u *lhs_color, char_u *rhs_color)
226{
227 if (lhs_color != NULL && rhs_color != NULL)
228 return STRCMP(lhs_color, rhs_color) == 0;
229 return lhs_color == NULL && rhs_color == NULL;
230}
231
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200232 static void
233cursor_color_copy(char_u **to_color, char_u *from_color)
234{
235 // Avoid a free & alloc if the value is already right.
236 if (cursor_color_equal(*to_color, from_color))
237 return;
238 vim_free(*to_color);
239 *to_color = (from_color == NULL) ? NULL : vim_strsave(from_color);
240}
241
242 static char_u *
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200243cursor_color_get(char_u *color)
244{
245 return (color == NULL) ? (char_u *)"" : color;
246}
247
248
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200249/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200250 * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
Bram Moolenaar498c2562018-04-15 23:45:15 +0200251 * current window.
252 * Sets "rows" and/or "cols" to zero when it should follow the window size.
253 * Return TRUE if the size is the minimum size: "24*80".
254 */
255 static int
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200256parse_termwinsize(win_T *wp, int *rows, int *cols)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200257{
258 int minsize = FALSE;
259
260 *rows = 0;
261 *cols = 0;
262
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200263 if (*wp->w_p_tws != NUL)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200264 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200265 char_u *p = vim_strchr(wp->w_p_tws, 'x');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200266
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100267 // Syntax of value was already checked when it's set.
Bram Moolenaar498c2562018-04-15 23:45:15 +0200268 if (p == NULL)
269 {
270 minsize = TRUE;
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200271 p = vim_strchr(wp->w_p_tws, '*');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200272 }
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200273 *rows = atoi((char *)wp->w_p_tws);
Bram Moolenaar498c2562018-04-15 23:45:15 +0200274 *cols = atoi((char *)p + 1);
275 }
276 return minsize;
277}
278
279/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200280 * Determine the terminal size from 'termwinsize' and the current window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200281 */
282 static void
Bram Moolenaarb936b792020-09-04 18:34:09 +0200283set_term_and_win_size(term_T *term, jobopt_T *opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200284{
Bram Moolenaarb936b792020-09-04 18:34:09 +0200285 int rows, cols;
286 int minsize;
287
Bram Moolenaar13568252018-03-16 20:46:58 +0100288#ifdef FEAT_GUI
289 if (term->tl_system)
290 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100291 // Use the whole screen for the system command. However, it will start
292 // at the command line and scroll up as needed, using tl_toprow.
Bram Moolenaar13568252018-03-16 20:46:58 +0100293 term->tl_rows = Rows;
294 term->tl_cols = Columns;
Bram Moolenaar07b46af2018-04-10 14:56:18 +0200295 return;
Bram Moolenaar13568252018-03-16 20:46:58 +0100296 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100297#endif
Bram Moolenaarb936b792020-09-04 18:34:09 +0200298 term->tl_rows = curwin->w_height;
299 term->tl_cols = curwin->w_width;
300
301 minsize = parse_termwinsize(curwin, &rows, &cols);
302 if (minsize)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200303 {
Bram Moolenaarb936b792020-09-04 18:34:09 +0200304 if (term->tl_rows < rows)
305 term->tl_rows = rows;
306 if (term->tl_cols < cols)
307 term->tl_cols = cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200308 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200309 if ((opt->jo_set2 & JO2_TERM_ROWS))
310 term->tl_rows = opt->jo_term_rows;
311 else if (rows != 0)
312 term->tl_rows = rows;
313 if ((opt->jo_set2 & JO2_TERM_COLS))
314 term->tl_cols = opt->jo_term_cols;
315 else if (cols != 0)
316 term->tl_cols = cols;
317
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200318 if (!opt->jo_hidden)
Bram Moolenaarb936b792020-09-04 18:34:09 +0200319 {
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200320 if (term->tl_rows != curwin->w_height)
321 win_setheight_win(term->tl_rows, curwin);
322 if (term->tl_cols != curwin->w_width)
323 win_setwidth_win(term->tl_cols, curwin);
Bram Moolenaarb936b792020-09-04 18:34:09 +0200324
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200325 // Set 'winsize' now to avoid a resize at the next redraw.
326 if (!minsize && *curwin->w_p_tws != NUL)
327 {
328 char_u buf[100];
329
330 vim_snprintf((char *)buf, 100, "%dx%d",
331 term->tl_rows, term->tl_cols);
332 set_option_value((char_u *)"termwinsize", 0L, buf, OPT_LOCAL);
333 }
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
346 opt->jo_mode = MODE_RAW;
347 opt->jo_out_mode = MODE_RAW;
348 opt->jo_err_mode = MODE_RAW;
349 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
350}
351
352/*
353 * Set job options mandatory for a terminal job.
354 */
355 static void
356setup_job_options(jobopt_T *opt, int rows, int cols)
357{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100358#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100359 // Win32: Redirecting the job output won't work, thus always connect stdout
360 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200361 if (!(opt->jo_set & JO_OUT_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200362#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200363 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100364 // Connect stdout to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200365 opt->jo_io[PART_OUT] = JIO_BUFFER;
366 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
367 opt->jo_modifiable[PART_OUT] = 0;
368 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
369 }
370
Bram Moolenaar4f974752019-02-17 17:44:42 +0100371#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100372 // Win32: Redirecting the job output won't work, thus always connect stderr
373 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200374 if (!(opt->jo_set & JO_ERR_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200375#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200376 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100377 // Connect stderr to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200378 opt->jo_io[PART_ERR] = JIO_BUFFER;
379 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
380 opt->jo_modifiable[PART_ERR] = 0;
381 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
382 }
383
384 opt->jo_pty = TRUE;
385 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
386 opt->jo_term_rows = rows;
387 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
388 opt->jo_term_cols = cols;
389}
390
391/*
Bram Moolenaar5c381eb2019-06-25 06:50:31 +0200392 * Flush messages on channels.
393 */
394 static void
395term_flush_messages()
396{
397 mch_check_messages();
398 parse_queued_messages();
399}
400
401/*
Bram Moolenaard96ff162018-02-18 22:13:29 +0100402 * Close a terminal buffer (and its window). Used when creating the terminal
403 * fails.
404 */
405 static void
406term_close_buffer(buf_T *buf, buf_T *old_curbuf)
407{
408 free_terminal(buf);
409 if (old_curbuf != NULL)
410 {
411 --curbuf->b_nwindows;
412 curbuf = old_curbuf;
413 curwin->w_buffer = curbuf;
414 ++curbuf->b_nwindows;
415 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100416 CHECK_CURBUF;
Bram Moolenaard96ff162018-02-18 22:13:29 +0100417
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100418 // Wiping out the buffer will also close the window and call
419 // free_terminal().
Bram Moolenaard96ff162018-02-18 22:13:29 +0100420 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
421}
422
423/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200424 * Start a terminal window and return its buffer.
Bram Moolenaar13568252018-03-16 20:46:58 +0100425 * Use either "argvar" or "argv", the other must be NULL.
426 * When "flags" has TERM_START_NOJOB only create the buffer, b_term and open
427 * the window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200428 * Returns NULL when failed.
429 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100430 buf_T *
431term_start(
432 typval_T *argvar,
433 char **argv,
434 jobopt_T *opt,
435 int flags)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200436{
437 exarg_T split_ea;
438 win_T *old_curwin = curwin;
439 term_T *term;
440 buf_T *old_curbuf = NULL;
441 int res;
442 buf_T *newbuf;
Bram Moolenaare1004402020-10-24 20:49:43 +0200443 int vertical = opt->jo_vertical || (cmdmod.cmod_split & WSP_VERT);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200444 jobopt_T orig_opt; // only partly filled
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200445
446 if (check_restricted() || check_secure())
447 return NULL;
448
449 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
450 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
451 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
Bram Moolenaarb0992022020-01-30 14:55:42 +0100452 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))
453 || (argvar != NULL
454 && argvar->v_type == VAR_LIST
455 && argvar->vval.v_list != NULL
456 && argvar->vval.v_list->lv_first == &range_list_item))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200457 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100458 emsg(_(e_invarg));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200459 return NULL;
460 }
461
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200462 term = ALLOC_CLEAR_ONE(term_T);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200463 if (term == NULL)
464 return NULL;
465 term->tl_dirty_row_end = MAX_ROW;
466 term->tl_cursor_visible = TRUE;
467 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
468 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100469#ifdef FEAT_GUI
470 term->tl_system = (flags & TERM_START_SYSTEM);
471#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200472 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100473 ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200474 ga_init2(&term->tl_osc_buf, sizeof(char), 300);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200475
Bram Moolenaaraeed2a62021-04-29 20:18:45 +0200476 setpcmark();
Bram Moolenaara80faa82020-04-12 19:37:17 +0200477 CLEAR_FIELD(split_ea);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200478 if (opt->jo_curwin)
479 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100480 // Create a new buffer in the current window.
Bram Moolenaar13568252018-03-16 20:46:58 +0100481 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200482 {
483 no_write_message();
484 vim_free(term);
485 return NULL;
486 }
487 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaarb1009092020-05-31 16:04:42 +0200488 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
489 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
490 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200491 {
492 vim_free(term);
493 return NULL;
494 }
495 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100496 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200497 {
498 buf_T *buf;
499
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100500 // Create a new buffer without a window. Make it the current buffer for
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100501 // a moment to be able to do the initializations.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200502 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
503 BLN_NEW | BLN_LISTED);
504 if (buf == NULL || ml_open(buf) == FAIL)
505 {
506 vim_free(term);
507 return NULL;
508 }
509 old_curbuf = curbuf;
510 --curbuf->b_nwindows;
511 curbuf = buf;
512 curwin->w_buffer = buf;
513 ++curbuf->b_nwindows;
514 }
515 else
516 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100517 // Open a new window or tab.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200518 split_ea.cmdidx = CMD_new;
519 split_ea.cmd = (char_u *)"new";
520 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100521 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200522 {
523 split_ea.line2 = opt->jo_term_rows;
524 split_ea.addr_count = 1;
525 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100526 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200527 {
528 split_ea.line2 = opt->jo_term_cols;
529 split_ea.addr_count = 1;
530 }
531
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100532 if (vertical)
Bram Moolenaare1004402020-10-24 20:49:43 +0200533 cmdmod.cmod_split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200534 ex_splitview(&split_ea);
535 if (curwin == old_curwin)
536 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100537 // split failed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200538 vim_free(term);
539 return NULL;
540 }
541 }
542 term->tl_buffer = curbuf;
543 curbuf->b_term = term;
544
545 if (!opt->jo_hidden)
546 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100547 // Only one size was taken care of with :new, do the other one. With
548 // "curwin" both need to be done.
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100549 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200550 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100551 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200552 win_setwidth(opt->jo_term_cols);
553 }
554
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100555 // Link the new terminal in the list of active terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200556 term->tl_next = first_term;
557 first_term = term;
558
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100559 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
560
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200561 if (opt->jo_term_name != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100562 {
563 vim_free(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200564 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100565 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100566 else if (argv != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100567 {
568 vim_free(curbuf->b_ffname);
Bram Moolenaar13568252018-03-16 20:46:58 +0100569 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100570 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200571 else
572 {
573 int i;
574 size_t len;
575 char_u *cmd, *p;
576
577 if (argvar->v_type == VAR_STRING)
578 {
579 cmd = argvar->vval.v_string;
580 if (cmd == NULL)
581 cmd = (char_u *)"";
582 else if (STRCMP(cmd, "NONE") == 0)
583 cmd = (char_u *)"pty";
584 }
585 else if (argvar->v_type != VAR_LIST
586 || argvar->vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +0100587 || argvar->vval.v_list->lv_len == 0
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100588 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200589 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
590 cmd = (char_u*)"";
591
592 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200593 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200594
595 for (i = 0; p != NULL; ++i)
596 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100597 // Prepend a ! to the command name to avoid the buffer name equals
598 // the executable, otherwise ":w!" would overwrite it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200599 if (i == 0)
600 vim_snprintf((char *)p, len, "!%s", cmd);
601 else
602 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
603 if (buflist_findname(p) == NULL)
604 {
605 vim_free(curbuf->b_ffname);
606 curbuf->b_ffname = p;
607 break;
608 }
609 }
610 }
Bram Moolenaare010c722020-02-24 21:37:54 +0100611 vim_free(curbuf->b_sfname);
612 curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200613 curbuf->b_fname = curbuf->b_ffname;
614
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100615 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
616
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200617 if (opt->jo_term_opencmd != NULL)
618 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
619
620 if (opt->jo_eof_chars != NULL)
621 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
622
623 set_string_option_direct((char_u *)"buftype", -1,
624 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200625 // Avoid that 'buftype' is reset when this buffer is entered.
626 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200627
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100628 // Mark the buffer as not modifiable. It can only be made modifiable after
629 // the job finished.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200630 curbuf->b_p_ma = FALSE;
631
Bram Moolenaarb936b792020-09-04 18:34:09 +0200632 set_term_and_win_size(term, opt);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100633#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200634 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
635#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200636 setup_job_options(opt, term->tl_rows, term->tl_cols);
637
Bram Moolenaar13568252018-03-16 20:46:58 +0100638 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100639 return curbuf;
640
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100641#if defined(FEAT_SESSION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100642 // Remember the command for the session file.
Bram Moolenaar13568252018-03-16 20:46:58 +0100643 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100644 term->tl_command = vim_strsave((char_u *)"NONE");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100645 else if (argvar->v_type == VAR_STRING)
646 {
647 char_u *cmd = argvar->vval.v_string;
648
649 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
650 term->tl_command = vim_strsave(cmd);
651 }
652 else if (argvar->v_type == VAR_LIST
653 && argvar->vval.v_list != NULL
654 && argvar->vval.v_list->lv_len > 0)
655 {
656 garray_T ga;
657 listitem_T *item;
658
659 ga_init2(&ga, 1, 100);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200660 FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100661 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100662 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100663 char_u *p;
664
665 if (s == NULL)
666 break;
667 p = vim_strsave_fnameescape(s, FALSE);
668 if (p == NULL)
669 break;
670 ga_concat(&ga, p);
671 vim_free(p);
672 ga_append(&ga, ' ');
673 }
674 if (item == NULL)
675 {
676 ga_append(&ga, NUL);
677 term->tl_command = ga.ga_data;
678 }
679 else
680 ga_clear(&ga);
681 }
682#endif
683
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100684 if (opt->jo_term_kill != NULL)
685 {
686 char_u *p = skiptowhite(opt->jo_term_kill);
687
688 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
689 }
690
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200691 if (opt->jo_term_api != NULL)
Bram Moolenaar21109272020-01-30 16:27:20 +0100692 {
693 char_u *p = skiptowhite(opt->jo_term_api);
694
695 term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
696 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200697 else
698 term->tl_api = vim_strsave((char_u *)"Tapi_");
699
Bram Moolenaar83d47902020-03-26 20:34:00 +0100700 if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
701 term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
702
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100703 // System dependent: setup the vterm and maybe start the job in it.
Bram Moolenaar13568252018-03-16 20:46:58 +0100704 if (argv == NULL
705 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200706 && argvar->vval.v_string != NULL
707 && STRCMP(argvar->vval.v_string, "NONE") == 0)
708 res = create_pty_only(term, opt);
709 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200710 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200711
712 newbuf = curbuf;
713 if (res == OK)
714 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100715 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200716 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
717 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100718#ifdef FEAT_GUI
719 if (term->tl_system)
720 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100721 // display first line below typed command
Bram Moolenaar13568252018-03-16 20:46:58 +0100722 term->tl_toprow = msg_row + 1;
723 term->tl_dirty_row_end = 0;
724 }
725#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200726
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100727 // Make sure we don't get stuck on sending keys to the job, it leads to
728 // a deadlock if the job is waiting for Vim to read.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200729 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
730
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200731 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200732 {
733 --curbuf->b_nwindows;
734 curbuf = old_curbuf;
735 curwin->w_buffer = curbuf;
736 ++curbuf->b_nwindows;
737 }
738 }
739 else
740 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100741 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200742 return NULL;
743 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100744
Bram Moolenaar13568252018-03-16 20:46:58 +0100745 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar28ed4df2019-10-26 16:21:40 +0200746 if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
747 apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200748 return newbuf;
749}
750
751/*
752 * ":terminal": open a terminal window and execute a job in it.
753 */
754 void
755ex_terminal(exarg_T *eap)
756{
757 typval_T argvar[2];
758 jobopt_T opt;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100759 int opt_shell = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200760 char_u *cmd;
761 char_u *tofree = NULL;
762
763 init_job_options(&opt);
764
765 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100766 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200767 {
768 char_u *p, *ep;
769
770 cmd += 2;
771 p = skiptowhite(cmd);
772 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200773 if (ep != NULL)
774 {
775 if (ep < p)
776 p = ep;
777 else
778 ep = NULL;
779 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200780
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200781# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
782 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
783 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200784 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200785 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100786 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200787 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200788 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200789 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200790 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200791 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200792 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200793 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100794 opt.jo_term_norestore = 1;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100795 else if (OPTARG_HAS("shell"))
796 opt_shell = TRUE;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200797 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100798 {
799 opt.jo_set2 |= JO2_TERM_KILL;
800 opt.jo_term_kill = ep + 1;
801 p = skiptowhite(cmd);
802 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200803 else if (OPTARG_HAS("api"))
804 {
805 opt.jo_set2 |= JO2_TERM_API;
806 if (ep != NULL)
807 {
808 opt.jo_term_api = ep + 1;
809 p = skiptowhite(cmd);
810 }
811 else
812 opt.jo_term_api = NULL;
813 }
814 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200815 {
816 opt.jo_set2 |= JO2_TERM_ROWS;
817 opt.jo_term_rows = atoi((char *)ep + 1);
818 p = skiptowhite(cmd);
819 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200820 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200821 {
822 opt.jo_set2 |= JO2_TERM_COLS;
823 opt.jo_term_cols = atoi((char *)ep + 1);
824 p = skiptowhite(cmd);
825 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200826 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200827 {
828 char_u *buf = NULL;
829 char_u *keys;
830
Bram Moolenaar21109272020-01-30 16:27:20 +0100831 vim_free(opt.jo_eof_chars);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200832 p = skiptowhite(cmd);
833 *p = NUL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200834 keys = replace_termcodes(ep + 1, &buf,
835 REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200836 opt.jo_set2 |= JO2_EOF_CHARS;
837 opt.jo_eof_chars = vim_strsave(keys);
838 vim_free(buf);
839 *p = ' ';
840 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100841#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100842 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
843 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100844 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100845 int tty_type = NUL;
846
847 p = skiptowhite(cmd);
848 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
849 tty_type = 'w';
850 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
851 tty_type = 'c';
852 else
853 {
854 semsg(e_invargval, "type");
855 goto theend;
856 }
857 opt.jo_set2 |= JO2_TTY_TYPE;
858 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100859 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100860#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200861 else
862 {
863 if (*p)
864 *p = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100865 semsg(_("E181: Invalid attribute: %s"), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100866 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200867 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200868# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200869 cmd = skipwhite(p);
870 }
871 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100872 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100873 // Make a copy of 'shell', an autocommand may change the option.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200874 tofree = cmd = vim_strsave(p_sh);
875
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100876 // default to close when the shell exits
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100877 if (opt.jo_term_finish == NUL)
Bram Moolenaare2978022020-04-26 14:47:44 +0200878 opt.jo_term_finish = TL_FINISH_CLOSE;
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100879 }
880
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200881 if (eap->addr_count > 0)
882 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100883 // Write lines from current buffer to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200884 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
885 opt.jo_io[PART_IN] = JIO_BUFFER;
886 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
887 opt.jo_in_top = eap->line1;
888 opt.jo_in_bot = eap->line2;
889 }
890
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100891 if (opt_shell && tofree == NULL)
892 {
893#ifdef UNIX
894 char **argv = NULL;
895 char_u *tofree1 = NULL;
896 char_u *tofree2 = NULL;
897
898 // :term ++shell command
899 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
900 term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaaradf4aa22019-11-10 22:36:44 +0100901 vim_free(argv);
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100902 vim_free(tofree1);
903 vim_free(tofree2);
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100904 goto theend;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100905#else
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100906# ifdef MSWIN
907 long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
908 char_u *newcmd;
909
910 newcmd = alloc(cmdlen);
911 if (newcmd == NULL)
912 goto theend;
913 tofree = newcmd;
914 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
915 cmd = newcmd;
916# else
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100917 emsg(_("E279: Sorry, ++shell is not supported on this system"));
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100918 goto theend;
919# endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100920#endif
921 }
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100922 argvar[0].v_type = VAR_STRING;
923 argvar[0].vval.v_string = cmd;
924 argvar[1].v_type = VAR_UNKNOWN;
925 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100926
927theend:
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100928 vim_free(tofree);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200929 vim_free(opt.jo_eof_chars);
930}
931
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100932#if defined(FEAT_SESSION) || defined(PROTO)
933/*
934 * Write a :terminal command to the session file to restore the terminal in
935 * window "wp".
936 * Return FAIL if writing fails.
937 */
938 int
Bram Moolenaar0e655112020-09-11 20:36:36 +0200939term_write_session(FILE *fd, win_T *wp, hashtab_T *terminal_bufs)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100940{
Bram Moolenaar0e655112020-09-11 20:36:36 +0200941 const int bufnr = wp->w_buffer->b_fnum;
942 term_T *term = wp->w_buffer->b_term;
943
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200944 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200945 {
946 // There are multiple views into this terminal buffer. We don't want to
947 // create the terminal multiple times. If it's the first time, create,
948 // otherwise link to the first buffer.
949 char id_as_str[NUMBUFLEN];
950 hashitem_T *entry;
951
952 vim_snprintf(id_as_str, sizeof(id_as_str), "%d", bufnr);
953
954 entry = hash_find(terminal_bufs, (char_u *)id_as_str);
955 if (!HASHITEM_EMPTY(entry))
956 {
957 // we've already opened this terminal buffer
958 if (fprintf(fd, "execute 'buffer ' . s:term_buf_%d", bufnr) < 0)
959 return FAIL;
960 return put_eol(fd);
961 }
962 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100963
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100964 // Create the terminal and run the command. This is not without
965 // risk, but let's assume the user only creates a session when this
966 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100967 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
968 term->tl_cols, term->tl_rows) < 0)
969 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100970#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100971 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
972 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100973#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100974 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
975 return FAIL;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200976 if (put_eol(fd) != OK)
977 return FAIL;
978
979 if (fprintf(fd, "let s:term_buf_%d = bufnr()", bufnr) < 0)
980 return FAIL;
981
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200982 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200983 {
984 char *hash_key = alloc(NUMBUFLEN);
985
986 vim_snprintf(hash_key, NUMBUFLEN, "%d", bufnr);
987 hash_add(terminal_bufs, (char_u *)hash_key);
988 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100989
990 return put_eol(fd);
991}
992
993/*
994 * Return TRUE if "buf" has a terminal that should be restored.
995 */
996 int
997term_should_restore(buf_T *buf)
998{
999 term_T *term = buf->b_term;
1000
1001 return term != NULL && (term->tl_command == NULL
1002 || STRCMP(term->tl_command, "NONE") != 0);
1003}
1004#endif
1005
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001006/*
1007 * Free the scrollback buffer for "term".
1008 */
1009 static void
1010free_scrollback(term_T *term)
1011{
1012 int i;
1013
1014 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
1015 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
1016 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001017 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
1018 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
1019 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001020}
1021
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001022
1023// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +02001024static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001025
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001026/*
1027 * Free a terminal and everything it refers to.
1028 * Kills the job if there is one.
1029 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001030 * The actual terminal structure is freed later in free_unused_terminals(),
1031 * because callbacks may wipe out a buffer while the terminal is still
1032 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001033 */
1034 void
1035free_terminal(buf_T *buf)
1036{
1037 term_T *term = buf->b_term;
1038 term_T *tp;
1039
1040 if (term == NULL)
1041 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001042
1043 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001044 if (first_term == term)
1045 first_term = term->tl_next;
1046 else
1047 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
1048 if (tp->tl_next == term)
1049 {
1050 tp->tl_next = term->tl_next;
1051 break;
1052 }
1053
1054 if (term->tl_job != NULL)
1055 {
1056 if (term->tl_job->jv_status != JOB_ENDED
1057 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +01001058 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001059 job_stop(term->tl_job, NULL, "kill");
1060 job_unref(term->tl_job);
1061 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001062 term->tl_next = terminals_to_free;
1063 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001064
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001065 buf->b_term = NULL;
1066 if (in_terminal_loop == term)
1067 in_terminal_loop = NULL;
1068}
1069
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001070 void
1071free_unused_terminals()
1072{
1073 while (terminals_to_free != NULL)
1074 {
1075 term_T *term = terminals_to_free;
1076
1077 terminals_to_free = term->tl_next;
1078
1079 free_scrollback(term);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02001080 ga_clear(&term->tl_osc_buf);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001081
1082 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001083 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001084 vim_free(term->tl_title);
1085#ifdef FEAT_SESSION
1086 vim_free(term->tl_command);
1087#endif
1088 vim_free(term->tl_kill);
1089 vim_free(term->tl_status_text);
1090 vim_free(term->tl_opencmd);
1091 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001092 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001093#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001094 if (term->tl_out_fd != NULL)
1095 fclose(term->tl_out_fd);
1096#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001097 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001098 vim_free(term->tl_cursor_color);
1099 vim_free(term);
1100 }
1101}
1102
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001103/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001104 * Get the part that is connected to the tty. Normally this is PART_IN, but
1105 * when writing buffer lines to the job it can be another. This makes it
1106 * possible to do "1,5term vim -".
1107 */
1108 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001109get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001110{
1111#ifdef UNIX
1112 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1113 int i;
1114
1115 for (i = 0; i < 3; ++i)
1116 {
1117 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1118
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001119 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001120 return parts[i];
1121 }
1122#endif
1123 return PART_IN;
1124}
1125
1126/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001127 * Write job output "msg[len]" to the vterm.
1128 */
1129 static void
1130term_write_job_output(term_T *term, char_u *msg, size_t len)
1131{
1132 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001133 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001134
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001135 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001136
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001137 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001138 if (prevlen != vterm_output_get_buffer_current(vterm))
1139 {
1140 char buf[KEY_BUF_LEN];
1141 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1142
1143 if (curlen > 0)
1144 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1145 (char_u *)buf, (int)curlen, NULL);
1146 }
1147
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001148 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001149 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1150}
1151
1152 static void
1153update_cursor(term_T *term, int redraw)
1154{
1155 if (term->tl_normal_mode)
1156 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001157#ifdef FEAT_GUI
1158 if (term->tl_system)
1159 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1160 term->tl_cursor_pos.col);
1161 else
1162#endif
1163 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001164 if (redraw)
1165 {
1166 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1167 cursor_on();
1168 out_flush();
1169#ifdef FEAT_GUI
1170 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001171 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001172 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001173 gui_mch_flush();
1174 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001175#endif
1176 }
1177}
1178
1179/*
1180 * Invoked when "msg" output from a job was received. Write it to the terminal
1181 * of "buffer".
1182 */
1183 void
1184write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1185{
1186 size_t len = STRLEN(msg);
1187 term_T *term = buffer->b_term;
1188
Bram Moolenaar4f974752019-02-17 17:44:42 +01001189#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001190 // Win32: Cannot redirect output of the job, intercept it here and write to
1191 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001192 if (term->tl_out_fd != NULL)
1193 {
1194 ch_log(channel, "Writing %d bytes to output file", (int)len);
1195 fwrite(msg, len, 1, term->tl_out_fd);
1196 return;
1197 }
1198#endif
1199
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001200 if (term->tl_vterm == NULL)
1201 {
1202 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1203 return;
1204 }
1205 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01001206 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001207 term_write_job_output(term, msg, len);
1208
Bram Moolenaar13568252018-03-16 20:46:58 +01001209#ifdef FEAT_GUI
1210 if (term->tl_system)
1211 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001212 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001213 update_system_term(term);
1214 update_cursor(term, TRUE);
1215 }
1216 else
1217#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001218 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1219 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001220 if (!term->tl_normal_mode)
1221 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001222 // Don't use update_screen() when editing the command line, it gets
1223 // cleared.
1224 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001225 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001226 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001227 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001228 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001229 // update_screen() can be slow, check the terminal wasn't closed
1230 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001231 if (buffer == curbuf && curbuf->b_term != NULL)
1232 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001233 }
1234 else
1235 redraw_after_callback(TRUE);
1236 }
1237}
1238
1239/*
1240 * Send a mouse position and click to the vterm
1241 */
1242 static int
1243term_send_mouse(VTerm *vterm, int button, int pressed)
1244{
1245 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001246 int row = mouse_row - W_WINROW(curwin);
1247 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001248
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001249#ifdef FEAT_PROP_POPUP
1250 if (popup_is_popup(curwin))
1251 {
1252 row -= popup_top_extra(curwin);
1253 col -= popup_left_extra(curwin);
1254 }
1255#endif
1256 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001257 if (button != 0)
1258 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001259 return TRUE;
1260}
1261
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001262static int enter_mouse_col = -1;
1263static int enter_mouse_row = -1;
1264
1265/*
1266 * Handle a mouse click, drag or release.
1267 * Return TRUE when a mouse event is sent to the terminal.
1268 */
1269 static int
1270term_mouse_click(VTerm *vterm, int key)
1271{
1272#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001273 // For modeless selection mouse drag and release events are ignored, unless
1274 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001275 static int ignore_drag_release = TRUE;
1276 VTermMouseState mouse_state;
1277
1278 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1279 if (mouse_state.flags == 0)
1280 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001281 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001282 switch (key)
1283 {
1284 case K_LEFTDRAG:
1285 case K_LEFTRELEASE:
1286 case K_RIGHTDRAG:
1287 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001288 // Ignore drag and release events when the button-down wasn't
1289 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001290 if (ignore_drag_release)
1291 {
1292 int save_mouse_col, save_mouse_row;
1293
1294 if (enter_mouse_col < 0)
1295 break;
1296
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001297 // mouse click in the window gave us focus, handle that
1298 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001299 save_mouse_col = mouse_col;
1300 save_mouse_row = mouse_row;
1301 mouse_col = enter_mouse_col;
1302 mouse_row = enter_mouse_row;
1303 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1304 mouse_col = save_mouse_col;
1305 mouse_row = save_mouse_row;
1306 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001307 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001308 case K_LEFTMOUSE:
1309 case K_RIGHTMOUSE:
1310 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1311 ignore_drag_release = TRUE;
1312 else
1313 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001314 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001315 if (clip_star.available)
1316 {
1317 int button, is_click, is_drag;
1318
1319 button = get_mouse_button(KEY2TERMCAP1(key),
1320 &is_click, &is_drag);
1321 if (mouse_model_popup() && button == MOUSE_LEFT
1322 && (mod_mask & MOD_MASK_SHIFT))
1323 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001324 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001325 button = MOUSE_RIGHT;
1326 mod_mask &= ~MOD_MASK_SHIFT;
1327 }
1328 clip_modeless(button, is_click, is_drag);
1329 }
1330 break;
1331
1332 case K_MIDDLEMOUSE:
1333 if (clip_star.available)
1334 insert_reg('*', TRUE);
1335 break;
1336 }
1337 enter_mouse_col = -1;
1338 return FALSE;
1339 }
1340#endif
1341 enter_mouse_col = -1;
1342
1343 switch (key)
1344 {
1345 case K_LEFTMOUSE:
1346 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1347 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1348 case K_LEFTRELEASE:
1349 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1350 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1351 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1352 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1353 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1354 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1355 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1356 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1357 }
1358 return TRUE;
1359}
1360
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001361/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001362 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1363 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001364 * Return the number of bytes in "buf".
1365 */
1366 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001367term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001368{
1369 VTerm *vterm = term->tl_vterm;
1370 VTermKey key = VTERM_KEY_NONE;
1371 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001372 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001373
1374 switch (c)
1375 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001376 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001377
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001378 // don't use VTERM_KEY_BACKSPACE, it always
1379 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001380 case K_BS: c = term_backspace_char; break;
1381
1382 case ESC: key = VTERM_KEY_ESCAPE; break;
1383 case K_DEL: key = VTERM_KEY_DEL; break;
1384 case K_DOWN: key = VTERM_KEY_DOWN; break;
1385 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1386 key = VTERM_KEY_DOWN; break;
1387 case K_END: key = VTERM_KEY_END; break;
1388 case K_S_END: mod = VTERM_MOD_SHIFT;
1389 key = VTERM_KEY_END; break;
1390 case K_C_END: mod = VTERM_MOD_CTRL;
1391 key = VTERM_KEY_END; break;
1392 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1393 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1394 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1395 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1396 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1397 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1398 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1399 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1400 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1401 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1402 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1403 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1404 case K_HOME: key = VTERM_KEY_HOME; break;
1405 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1406 key = VTERM_KEY_HOME; break;
1407 case K_C_HOME: mod = VTERM_MOD_CTRL;
1408 key = VTERM_KEY_HOME; break;
1409 case K_INS: key = VTERM_KEY_INS; break;
1410 case K_K0: key = VTERM_KEY_KP_0; break;
1411 case K_K1: key = VTERM_KEY_KP_1; break;
1412 case K_K2: key = VTERM_KEY_KP_2; break;
1413 case K_K3: key = VTERM_KEY_KP_3; break;
1414 case K_K4: key = VTERM_KEY_KP_4; break;
1415 case K_K5: key = VTERM_KEY_KP_5; break;
1416 case K_K6: key = VTERM_KEY_KP_6; break;
1417 case K_K7: key = VTERM_KEY_KP_7; break;
1418 case K_K8: key = VTERM_KEY_KP_8; break;
1419 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001420 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001421 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001422 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001423 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001424 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1425 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001426 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1427 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001428 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1429 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001430 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1431 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1432 case K_LEFT: key = VTERM_KEY_LEFT; break;
1433 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1434 key = VTERM_KEY_LEFT; break;
1435 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1436 key = VTERM_KEY_LEFT; break;
1437 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1438 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1439 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1440 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1441 key = VTERM_KEY_RIGHT; break;
1442 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1443 key = VTERM_KEY_RIGHT; break;
1444 case K_UP: key = VTERM_KEY_UP; break;
1445 case K_S_UP: mod = VTERM_MOD_SHIFT;
1446 key = VTERM_KEY_UP; break;
1447 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001448 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1449 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001450
Bram Moolenaara42ad572017-11-16 13:08:04 +01001451 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1452 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001453 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1454 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001455
1456 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001457 case K_LEFTMOUSE_NM:
1458 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001459 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001460 case K_LEFTRELEASE_NM:
1461 case K_MOUSEMOVE:
1462 case K_MIDDLEMOUSE:
1463 case K_MIDDLEDRAG:
1464 case K_MIDDLERELEASE:
1465 case K_RIGHTMOUSE:
1466 case K_RIGHTDRAG:
1467 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1468 return 0;
1469 other = TRUE;
1470 break;
1471
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001472 case K_X1MOUSE: /* TODO */ return 0;
1473 case K_X1DRAG: /* TODO */ return 0;
1474 case K_X1RELEASE: /* TODO */ return 0;
1475 case K_X2MOUSE: /* TODO */ return 0;
1476 case K_X2DRAG: /* TODO */ return 0;
1477 case K_X2RELEASE: /* TODO */ return 0;
1478
1479 case K_IGNORE: return 0;
1480 case K_NOP: return 0;
1481 case K_UNDO: return 0;
1482 case K_HELP: return 0;
1483 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1484 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1485 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1486 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1487 case K_SELECT: return 0;
1488#ifdef FEAT_GUI
1489 case K_VER_SCROLLBAR: return 0;
1490 case K_HOR_SCROLLBAR: return 0;
1491#endif
1492#ifdef FEAT_GUI_TABLINE
1493 case K_TABLINE: return 0;
1494 case K_TABMENU: return 0;
1495#endif
1496#ifdef FEAT_NETBEANS_INTG
1497 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1498#endif
1499#ifdef FEAT_DND
1500 case K_DROP: return 0;
1501#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001502 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001503 case K_PS: vterm_keyboard_start_paste(vterm);
1504 other = TRUE;
1505 break;
1506 case K_PE: vterm_keyboard_end_paste(vterm);
1507 other = TRUE;
1508 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001509 }
1510
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001511 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001512 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001513 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001514 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001515 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001516 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001517 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001518
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001519 /*
1520 * Convert special keys to vterm keys:
1521 * - Write keys to vterm: vterm_keyboard_key()
1522 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001523 */
1524 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001525 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001526 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001527 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001528 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001529 vterm_keyboard_unichar(vterm, c, mod);
1530
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001531 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001532 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1533}
1534
1535/*
1536 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001537 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001538 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001539 */
1540 static int
1541term_job_running_check(term_T *term, int check_job_status)
1542{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001543 // Also consider the job finished when the channel is closed, to avoid a
1544 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001545 if (term != NULL
1546 && term->tl_job != NULL
1547 && channel_is_open(term->tl_job->jv_channel))
1548 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001549 job_T *job = term->tl_job;
1550
1551 // Careful: Checking the job status may invoked callbacks, which close
1552 // the buffer and terminate "term". However, "job" will not be freed
1553 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001554 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001555 job_status(job);
1556 return (job->jv_status == JOB_STARTED
1557 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001558 }
1559 return FALSE;
1560}
1561
1562/*
1563 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001564 */
1565 int
1566term_job_running(term_T *term)
1567{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001568 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001569}
1570
1571/*
1572 * Return TRUE if "term" has an active channel and used ":term NONE".
1573 */
1574 int
1575term_none_open(term_T *term)
1576{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001577 // Also consider the job finished when the channel is closed, to avoid a
1578 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001579 return term != NULL
1580 && term->tl_job != NULL
1581 && channel_is_open(term->tl_job->jv_channel)
1582 && term->tl_job->jv_channel->ch_keep_open;
1583}
1584
1585/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001586 * Used when exiting: kill the job in "buf" if so desired.
1587 * Return OK when the job finished.
1588 * Return FAIL when the job is still running.
1589 */
1590 int
1591term_try_stop_job(buf_T *buf)
1592{
1593 int count;
1594 char *how = (char *)buf->b_term->tl_kill;
1595
1596#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001597 if ((how == NULL || *how == NUL)
1598 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001599 {
1600 char_u buff[DIALOG_MSG_SIZE];
1601 int ret;
1602
Bram Moolenaar00806bc2020-11-05 19:36:38 +01001603 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001604 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1605 if (ret == VIM_YES)
1606 how = "kill";
1607 else if (ret == VIM_CANCEL)
1608 return FAIL;
1609 }
1610#endif
1611 if (how == NULL || *how == NUL)
1612 return FAIL;
1613
1614 job_stop(buf->b_term->tl_job, NULL, how);
1615
Bram Moolenaar9172d232019-01-29 23:06:54 +01001616 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001617 for (count = 0; count < 100; ++count)
1618 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001619 job_T *job;
1620
1621 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001622 if (!buf_valid(buf)
1623 || buf->b_term == NULL
1624 || buf->b_term->tl_job == NULL)
1625 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001626 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001627
Bram Moolenaar9172d232019-01-29 23:06:54 +01001628 // Call job_status() to update jv_status. It may cause the job to be
1629 // cleaned up but it won't be freed.
1630 job_status(job);
1631 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001632 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001633
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001634 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001635 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001636 }
1637 return FAIL;
1638}
1639
1640/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001641 * Add the last line of the scrollback buffer to the buffer in the window.
1642 */
1643 static void
1644add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1645{
1646 buf_T *buf = term->tl_buffer;
1647 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1648 linenr_T lnum = buf->b_ml.ml_line_count;
1649
Bram Moolenaar4f974752019-02-17 17:44:42 +01001650#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001651 if (!enc_utf8 && enc_codepage > 0)
1652 {
1653 WCHAR *ret = NULL;
1654 int length = 0;
1655
1656 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1657 &ret, &length);
1658 if (ret != NULL)
1659 {
1660 WideCharToMultiByte_alloc(enc_codepage, 0,
1661 ret, length, (char **)&text, &len, 0, 0);
1662 vim_free(ret);
1663 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1664 vim_free(text);
1665 }
1666 }
1667 else
1668#endif
1669 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1670 if (empty)
1671 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001672 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001673 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001674 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001675 curbuf = curwin->w_buffer;
1676 }
1677}
1678
1679 static void
1680cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1681{
1682 attr->width = cell->width;
1683 attr->attrs = cell->attrs;
1684 attr->fg = cell->fg;
1685 attr->bg = cell->bg;
1686}
1687
1688 static int
1689equal_celattr(cellattr_T *a, cellattr_T *b)
1690{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001691 // We only compare the RGB colors, ignoring the ANSI index and type.
1692 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001693 return a->fg.red == b->fg.red
1694 && a->fg.green == b->fg.green
1695 && a->fg.blue == b->fg.blue
1696 && a->bg.red == b->bg.red
1697 && a->bg.green == b->bg.green
1698 && a->bg.blue == b->bg.blue;
1699}
1700
Bram Moolenaard96ff162018-02-18 22:13:29 +01001701/*
1702 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1703 * line at this position. Otherwise at the end.
1704 */
1705 static int
1706add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1707{
1708 if (ga_grow(&term->tl_scrollback, 1) == OK)
1709 {
1710 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1711 + term->tl_scrollback.ga_len;
1712
1713 if (lnum > 0)
1714 {
1715 int i;
1716
1717 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1718 {
1719 *line = *(line - 1);
1720 --line;
1721 }
1722 }
1723 line->sb_cols = 0;
1724 line->sb_cells = NULL;
1725 line->sb_fill_attr = *fill_attr;
1726 ++term->tl_scrollback.ga_len;
1727 return OK;
1728 }
1729 return FALSE;
1730}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001731
1732/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001733 * Remove the terminal contents from the scrollback and the buffer.
1734 * Used before adding a new scrollback line or updating the buffer for lines
1735 * displayed in the terminal.
1736 */
1737 static void
1738cleanup_scrollback(term_T *term)
1739{
1740 sb_line_T *line;
1741 garray_T *gap;
1742
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001743 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001744 gap = &term->tl_scrollback;
1745 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1746 && gap->ga_len > 0)
1747 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001748 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001749 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1750 vim_free(line->sb_cells);
1751 --gap->ga_len;
1752 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001753 curbuf = curwin->w_buffer;
1754 if (curbuf == term->tl_buffer)
1755 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001756}
1757
1758/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001759 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001760 */
1761 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001762update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001763{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001764 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001765 int len;
1766 int lines_skipped = 0;
1767 VTermPos pos;
1768 VTermScreenCell cell;
1769 cellattr_T fill_attr, new_fill_attr;
1770 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001771
1772 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1773 "Adding terminal window snapshot to buffer");
1774
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001775 // First remove the lines that were appended before, they might be
1776 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001777 cleanup_scrollback(term);
1778
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001779 screen = vterm_obtain_screen(term->tl_vterm);
1780 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001781 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1782 {
1783 len = 0;
1784 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1785 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1786 && cell.chars[0] != NUL)
1787 {
1788 len = pos.col + 1;
1789 new_fill_attr = term->tl_default_color;
1790 }
1791 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001792 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001793 cell2cellattr(&cell, &new_fill_attr);
1794
1795 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1796 ++lines_skipped;
1797 else
1798 {
1799 while (lines_skipped > 0)
1800 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001801 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001802 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001803 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001804 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001805 }
1806
1807 if (len == 0)
1808 p = NULL;
1809 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001810 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001811 if ((p != NULL || len == 0)
1812 && ga_grow(&term->tl_scrollback, 1) == OK)
1813 {
1814 garray_T ga;
1815 int width;
1816 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1817 + term->tl_scrollback.ga_len;
1818
1819 ga_init2(&ga, 1, 100);
1820 for (pos.col = 0; pos.col < len; pos.col += width)
1821 {
1822 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1823 {
1824 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001825 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001826 if (ga_grow(&ga, 1) == OK)
1827 ga.ga_len += utf_char2bytes(' ',
1828 (char_u *)ga.ga_data + ga.ga_len);
1829 }
1830 else
1831 {
1832 width = cell.width;
1833
1834 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001835 if (width == 2)
1836 // second cell of double-width character has the
1837 // same attributes.
1838 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001839
Bram Moolenaara79fd562018-12-20 20:47:32 +01001840 // Each character can be up to 6 bytes.
1841 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001842 {
1843 int i;
1844 int c;
1845
1846 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1847 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1848 (char_u *)ga.ga_data + ga.ga_len);
1849 }
1850 }
1851 }
1852 line->sb_cols = len;
1853 line->sb_cells = p;
1854 line->sb_fill_attr = new_fill_attr;
1855 fill_attr = new_fill_attr;
1856 ++term->tl_scrollback.ga_len;
1857
1858 if (ga_grow(&ga, 1) == FAIL)
1859 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1860 else
1861 {
1862 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1863 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1864 }
1865 ga_clear(&ga);
1866 }
1867 else
1868 vim_free(p);
1869 }
1870 }
1871
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001872 // Add trailing empty lines.
1873 for (pos.row = term->tl_scrollback.ga_len;
1874 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1875 ++pos.row)
1876 {
1877 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1878 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1879 }
1880
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001881 term->tl_dirty_snapshot = FALSE;
1882#ifdef FEAT_TIMERS
1883 term->tl_timer_set = FALSE;
1884#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001885}
1886
1887/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001888 * Loop over all windows in the current tab, and also curwin, which is not
1889 * encountered when using a terminal in a popup window.
1890 * Return TRUE if "*wp" was set to the next window.
1891 */
1892 static int
1893for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1894{
1895 if (*wp == NULL)
1896 *wp = firstwin;
1897 else if ((*wp)->w_next != NULL)
1898 *wp = (*wp)->w_next;
1899 else if (!*did_curwin)
1900 *wp = curwin;
1901 else
1902 return FALSE;
1903 if (*wp == curwin)
1904 *did_curwin = TRUE;
1905 return TRUE;
1906}
1907
1908/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001909 * If needed, add the current lines of the terminal to scrollback and to the
1910 * buffer. Called after the job has ended and when switching to
1911 * Terminal-Normal mode.
1912 * When "redraw" is TRUE redraw the windows that show the terminal.
1913 */
1914 static void
1915may_move_terminal_to_buffer(term_T *term, int redraw)
1916{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001917 if (term->tl_vterm == NULL)
1918 return;
1919
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001920 // Update the snapshot only if something changes or the buffer does not
1921 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001922 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1923 <= term->tl_scrollback_scrolled)
1924 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001925
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001926 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001927 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1928 &term->tl_default_color.fg, &term->tl_default_color.bg);
1929
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001930 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001931 {
1932 win_T *wp = NULL;
1933 int did_curwin = FALSE;
1934
1935 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001936 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001937 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001938 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001939 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1940 wp->w_cursor.col = 0;
1941 wp->w_valid = 0;
1942 if (wp->w_cursor.lnum >= wp->w_height)
1943 {
1944 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001945
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001946 if (wp->w_topline < min_topline)
1947 wp->w_topline = min_topline;
1948 }
1949 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001950 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001951 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001952 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001953}
1954
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001955#if defined(FEAT_TIMERS) || defined(PROTO)
1956/*
1957 * Check if any terminal timer expired. If so, copy text from the terminal to
1958 * the buffer.
1959 * Return the time until the next timer will expire.
1960 */
1961 int
1962term_check_timers(int next_due_arg, proftime_T *now)
1963{
1964 term_T *term;
1965 int next_due = next_due_arg;
1966
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001967 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001968 {
1969 if (term->tl_timer_set && !term->tl_normal_mode)
1970 {
1971 long this_due = proftime_time_left(&term->tl_timer_due, now);
1972
1973 if (this_due <= 1)
1974 {
1975 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001976 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001977 }
1978 else if (next_due == -1 || next_due > this_due)
1979 next_due = this_due;
1980 }
1981 }
1982
1983 return next_due;
1984}
1985#endif
1986
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001987/*
1988 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
1989 * otherwise end it.
1990 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001991 static void
1992set_terminal_mode(term_T *term, int normal_mode)
1993{
1994 term->tl_normal_mode = normal_mode;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001995 if (!normal_mode)
1996 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001997 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001998 if (term->tl_buffer == curbuf)
1999 maketitle();
2000}
2001
2002/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002003 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002004 * Move the vterm contents into the scrollback buffer and free the vterm.
2005 */
2006 static void
2007cleanup_vterm(term_T *term)
2008{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002009 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002010 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002011 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002012 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002013}
2014
2015/*
2016 * Switch from Terminal-Job mode to Terminal-Normal mode.
2017 * Suspends updating the terminal window.
2018 */
2019 static void
2020term_enter_normal_mode(void)
2021{
2022 term_T *term = curbuf->b_term;
2023
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002024 set_terminal_mode(term, TRUE);
2025
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002026 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002027 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002028
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002029 // Move the window cursor to the position of the cursor in the
2030 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002031 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2032 + term->tl_cursor_pos.row + 1;
2033 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002034 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2035 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002036 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002037
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002038 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002039 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2040}
2041
2042/*
2043 * Returns TRUE if the current window contains a terminal and we are in
2044 * Terminal-Normal mode.
2045 */
2046 int
2047term_in_normal_mode(void)
2048{
2049 term_T *term = curbuf->b_term;
2050
2051 return term != NULL && term->tl_normal_mode;
2052}
2053
2054/*
2055 * Switch from Terminal-Normal mode to Terminal-Job mode.
2056 * Restores updating the terminal window.
2057 */
2058 void
2059term_enter_job_mode()
2060{
2061 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002062
2063 set_terminal_mode(term, FALSE);
2064
2065 if (term->tl_channel_closed)
2066 cleanup_vterm(term);
2067 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002068#ifdef FEAT_PROP_POPUP
2069 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002070 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002071#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002072}
2073
2074/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002075 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002076 * Note: while waiting a terminal may be closed and freed if the channel is
2077 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002078 */
2079 static int
2080term_vgetc()
2081{
2082 int c;
2083 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002084 int modify_other_keys =
2085 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002086
2087 State = TERMINAL;
2088 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002089#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002090 ctrl_break_was_pressed = FALSE;
2091#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002092 if (modify_other_keys)
2093 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002094 c = vgetc();
2095 got_int = FALSE;
2096 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002097 if (modify_other_keys)
2098 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002099 return c;
2100}
2101
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002102static int mouse_was_outside = FALSE;
2103
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002104/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002105 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002106 * Return FAIL when the key needs to be handled in Normal mode.
2107 * Return OK when the key was dropped or sent to the terminal.
2108 */
2109 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002110send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002111{
2112 char msg[KEY_BUF_LEN];
2113 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002114 int dragging_outside = FALSE;
2115
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002116 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002117 switch (c)
2118 {
2119 case NUL:
2120 case K_ZERO:
2121 if (typed)
2122 stuffcharReadbuff(c);
2123 return FAIL;
2124
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002125 case K_TABLINE:
2126 stuffcharReadbuff(c);
2127 return FAIL;
2128
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002129 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002130 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002131 return FAIL;
2132
2133 case K_LEFTDRAG:
2134 case K_MIDDLEDRAG:
2135 case K_RIGHTDRAG:
2136 case K_X1DRAG:
2137 case K_X2DRAG:
2138 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002139 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002140 case K_LEFTMOUSE:
2141 case K_LEFTMOUSE_NM:
2142 case K_LEFTRELEASE:
2143 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002144 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002145 case K_MIDDLEMOUSE:
2146 case K_MIDDLERELEASE:
2147 case K_RIGHTMOUSE:
2148 case K_RIGHTRELEASE:
2149 case K_X1MOUSE:
2150 case K_X1RELEASE:
2151 case K_X2MOUSE:
2152 case K_X2RELEASE:
2153
2154 case K_MOUSEUP:
2155 case K_MOUSEDOWN:
2156 case K_MOUSELEFT:
2157 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002158 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002159 int row = mouse_row;
2160 int col = mouse_col;
2161
2162#ifdef FEAT_PROP_POPUP
2163 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002164 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002165 row -= popup_top_extra(curwin);
2166 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002167 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002168#endif
2169 if (row < W_WINROW(curwin)
2170 || row >= (W_WINROW(curwin) + curwin->w_height)
2171 || col < curwin->w_wincol
2172 || col >= W_ENDCOL(curwin)
2173 || dragging_outside)
2174 {
2175 // click or scroll outside the current window or on status
2176 // line or vertical separator
2177 if (typed)
2178 {
2179 stuffcharReadbuff(c);
2180 mouse_was_outside = TRUE;
2181 }
2182 return FAIL;
2183 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002184 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002185 break;
2186
2187 case K_COMMAND:
2188 return do_cmdline(NULL, getcmdkeycmd, NULL, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002189 }
2190 if (typed)
2191 mouse_was_outside = FALSE;
2192
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002193 // Convert the typed key to a sequence of bytes for the job.
2194 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002195 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002196 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002197 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2198 (char_u *)msg, (int)len, NULL);
2199
2200 return OK;
2201}
2202
2203 static void
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002204position_cursor(win_T *wp, VTermPos *pos)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002205{
2206 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2207 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002208#ifdef FEAT_PROP_POPUP
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002209 if (popup_is_popup(wp))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002210 {
Bram Moolenaarf5452692020-11-28 21:56:06 +01002211 wp->w_wrow += popup_top_extra(wp);
2212 wp->w_wcol += popup_left_extra(wp);
Bram Moolenaar6a076442020-11-15 20:32:58 +01002213 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002214 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01002215 else
2216 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002217#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002218 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2219}
2220
2221/*
2222 * Handle CTRL-W "": send register contents to the job.
2223 */
2224 static void
2225term_paste_register(int prev_c UNUSED)
2226{
2227 int c;
2228 list_T *l;
2229 listitem_T *item;
2230 long reglen = 0;
2231 int type;
2232
2233#ifdef FEAT_CMDL_INFO
2234 if (add_to_showcmd(prev_c))
2235 if (add_to_showcmd('"'))
2236 out_flush();
2237#endif
2238 c = term_vgetc();
2239#ifdef FEAT_CMDL_INFO
2240 clear_showcmd();
2241#endif
2242 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002243 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002244 return;
2245
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002246 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002247 if (c == '=' && get_expr_register() != '=')
2248 return;
2249 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002250 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002251 return;
2252
2253 l = (list_T *)get_reg_contents(c, GREG_LIST);
2254 if (l != NULL)
2255 {
2256 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002257 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002258 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002259 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002260#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002261 char_u *tmp = s;
2262
2263 if (!enc_utf8 && enc_codepage > 0)
2264 {
2265 WCHAR *ret = NULL;
2266 int length = 0;
2267
2268 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2269 (int)STRLEN(s), &ret, &length);
2270 if (ret != NULL)
2271 {
2272 WideCharToMultiByte_alloc(CP_UTF8, 0,
2273 ret, length, (char **)&s, &length, 0, 0);
2274 vim_free(ret);
2275 }
2276 }
2277#endif
2278 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2279 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002280#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002281 if (tmp != s)
2282 vim_free(s);
2283#endif
2284
2285 if (item->li_next != NULL || type == MLINE)
2286 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2287 (char_u *)"\r", 1, NULL);
2288 }
2289 list_free(l);
2290 }
2291}
2292
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002293/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002294 * Return TRUE when waiting for a character in the terminal, the cursor of the
2295 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002296 */
2297 int
2298terminal_is_active()
2299{
2300 return in_terminal_loop != NULL;
2301}
2302
Bram Moolenaar83d47902020-03-26 20:34:00 +01002303/*
2304 * Return the highight group name for the terminal; "Terminal" if not set.
2305 */
2306 static char_u *
2307term_get_highlight_name(term_T *term)
2308{
2309 if (term->tl_highlight_name == NULL)
2310 return (char_u *)"Terminal";
2311 return term->tl_highlight_name;
2312}
2313
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002314#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002315 cursorentry_T *
2316term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2317{
2318 term_T *term = in_terminal_loop;
2319 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002320 int id;
2321 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002322
Bram Moolenaara80faa82020-04-12 19:37:17 +02002323 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002324 entry.shape = entry.mshape =
2325 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2326 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2327 SHAPE_BLOCK;
2328 entry.percentage = 20;
2329 if (term->tl_cursor_blink)
2330 {
2331 entry.blinkwait = 700;
2332 entry.blinkon = 400;
2333 entry.blinkoff = 250;
2334 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002335
Bram Moolenaar83d47902020-03-26 20:34:00 +01002336 // The highlight group overrules the defaults.
2337 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002338 if (id != 0)
2339 {
2340 syn_id2colors(id, &term_fg, &term_bg);
2341 *fg = term_bg;
2342 }
2343 else
2344 *fg = gui.back_pixel;
2345
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002346 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002347 {
2348 if (id != 0)
2349 *bg = term_fg;
2350 else
2351 *bg = gui.norm_pixel;
2352 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002353 else
2354 *bg = color_name2handle(term->tl_cursor_color);
2355 entry.name = "n";
2356 entry.used_for = SHAPE_CURSOR;
2357
2358 return &entry;
2359}
2360#endif
2361
Bram Moolenaard317b382018-02-08 22:33:31 +01002362 static void
2363may_output_cursor_props(void)
2364{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002365 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002366 || last_set_cursor_shape != desired_cursor_shape
2367 || last_set_cursor_blink != desired_cursor_blink)
2368 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002369 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002370 last_set_cursor_shape = desired_cursor_shape;
2371 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002372 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002373 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002374 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002375 ui_cursor_shape_forced(TRUE);
2376 else
2377 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2378 }
2379}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002380
Bram Moolenaard317b382018-02-08 22:33:31 +01002381/*
2382 * Set the cursor color and shape, if not last set to these.
2383 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002384 static void
2385may_set_cursor_props(term_T *term)
2386{
2387#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002388 // For the GUI the cursor properties are obtained with
2389 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002390 if (gui.in_use)
2391 return;
2392#endif
2393 if (in_terminal_loop == term)
2394 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002395 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002396 desired_cursor_shape = term->tl_cursor_shape;
2397 desired_cursor_blink = term->tl_cursor_blink;
2398 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002399 }
2400}
2401
Bram Moolenaard317b382018-02-08 22:33:31 +01002402/*
2403 * Reset the desired cursor properties and restore them when needed.
2404 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002405 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002406prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002407{
2408#ifdef FEAT_GUI
2409 if (gui.in_use)
2410 return;
2411#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002412 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002413 desired_cursor_shape = -1;
2414 desired_cursor_blink = -1;
2415 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002416}
2417
2418/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002419 * Returns TRUE if the current window contains a terminal and we are sending
2420 * keys to the job.
2421 * If "check_job_status" is TRUE update the job status.
2422 */
2423 static int
2424term_use_loop_check(int check_job_status)
2425{
2426 term_T *term = curbuf->b_term;
2427
2428 return term != NULL
2429 && !term->tl_normal_mode
2430 && term->tl_vterm != NULL
2431 && term_job_running_check(term, check_job_status);
2432}
2433
2434/*
2435 * Returns TRUE if the current window contains a terminal and we are sending
2436 * keys to the job.
2437 */
2438 int
2439term_use_loop(void)
2440{
2441 return term_use_loop_check(FALSE);
2442}
2443
2444/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002445 * Called when entering a window with the mouse. If this is a terminal window
2446 * we may want to change state.
2447 */
2448 void
2449term_win_entered()
2450{
2451 term_T *term = curbuf->b_term;
2452
2453 if (term != NULL)
2454 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002455 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002456 {
2457 reset_VIsual_and_resel();
2458 if (State & INSERT)
2459 stop_insert_mode = TRUE;
2460 }
2461 mouse_was_outside = FALSE;
2462 enter_mouse_col = mouse_col;
2463 enter_mouse_row = mouse_row;
2464 }
2465}
2466
2467/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002468 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2469 * Return the Ctrl-key value in that case.
2470 */
2471 static int
2472raw_c_to_ctrl(int c)
2473{
2474 if ((mod_mask & MOD_MASK_CTRL)
2475 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2476 return c & 0x1f;
2477 return c;
2478}
2479
2480/*
2481 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2482 * May set "mod_mask".
2483 */
2484 static int
2485ctrl_to_raw_c(int c)
2486{
2487 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2488 {
2489 mod_mask |= MOD_MASK_CTRL;
2490 return c + '@';
2491 }
2492 return c;
2493}
2494
2495/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002496 * Wait for input and send it to the job.
2497 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2498 * when there is no more typahead.
2499 * Return when the start of a CTRL-W command is typed or anything else that
2500 * should be handled as a Normal mode command.
2501 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2502 * the terminal was closed.
2503 */
2504 int
2505terminal_loop(int blocking)
2506{
2507 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002508 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002509 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002510 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002511#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002512 int tty_fd = curbuf->b_term->tl_job->jv_channel
2513 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002514#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002515 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002516
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002517 // Remember the terminal we are sending keys to. However, the terminal
2518 // might be closed while waiting for a character, e.g. typing "exit" in a
2519 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2520 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002521 in_terminal_loop = curbuf->b_term;
2522
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002523 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002524 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002525 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002526 if (termwinkey == Ctrl_W)
2527 termwinkey = 0;
2528 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002529 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002530 may_set_cursor_props(curbuf->b_term);
2531
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002532 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002533 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002534#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002535 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002536#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002537 // TODO: skip screen update when handling a sequence of keys.
2538 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002539 while (must_redraw != 0)
2540 if (update_screen(0) == FAIL)
2541 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002542 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002543 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002544 break;
2545
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002546 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002547 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002548
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002549 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002550 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002551 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002552 // Job finished while waiting for a character. Push back the
2553 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002554 if (raw_c != K_IGNORE)
2555 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002556 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002557 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002558 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002559 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002560 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002561
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002562#ifdef UNIX
2563 /*
2564 * The shell or another program may change the tty settings. Getting
2565 * them for every typed character is a bit of overhead, but it's needed
2566 * for the first character typed, e.g. when Vim starts in a shell.
2567 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002568 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002569 {
2570 ttyinfo_T info;
2571
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002572 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002573 if (get_tty_info(tty_fd, &info) == OK)
2574 term_backspace_char = info.backspace;
2575 }
2576#endif
2577
Bram Moolenaar4f974752019-02-17 17:44:42 +01002578#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002579 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2580 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002581 if (ctrl_break_was_pressed)
2582 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2583#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002584 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2585 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002586 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002587#ifdef FEAT_GUI
2588 && !curbuf->b_term->tl_system
2589#endif
2590 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002591 {
2592 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002593 int prev_raw_c = raw_c;
2594 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002595
2596#ifdef FEAT_CMDL_INFO
2597 if (add_to_showcmd(c))
2598 out_flush();
2599#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002600 raw_c = term_vgetc();
2601 c = raw_c_to_ctrl(raw_c);
2602
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002603#ifdef FEAT_CMDL_INFO
2604 clear_showcmd();
2605#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002606 if (!term_use_loop_check(TRUE)
2607 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002608 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002609 break;
2610
2611 if (prev_c == Ctrl_BSL)
2612 {
2613 if (c == Ctrl_N)
2614 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002615 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002616 term_enter_normal_mode();
2617 ret = FAIL;
2618 goto theend;
2619 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002620 // Send both keys to the terminal, first one here, second one
2621 // below.
2622 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2623 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002624 }
2625 else if (c == Ctrl_C)
2626 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002627 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002628 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2629 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002630 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002631 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002632 // "CTRL-W .": send CTRL-W to the job
2633 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002634 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002635 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002636 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002637 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002638 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002639 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002640 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002641 else if (c == 'N')
2642 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002643 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002644 term_enter_normal_mode();
2645 ret = FAIL;
2646 goto theend;
2647 }
2648 else if (c == '"')
2649 {
2650 term_paste_register(prev_c);
2651 continue;
2652 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002653 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002654 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002655 // space for CTRL-W, modifier, multi-byte char and NUL
2656 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002657
2658 // Put the command into the typeahead buffer, when using the
2659 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2660 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002661 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002662 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002663 ret = OK;
2664 goto theend;
2665 }
2666 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002667# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002668 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002669 {
2670 WCHAR wc;
2671 char_u mb[3];
2672
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002673 mb[0] = (unsigned)raw_c >> 8;
2674 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002675 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002676 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002677 }
2678# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002679 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002680 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002681 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002682 // We are sure to come back here, don't reset the cursor color
2683 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002684 restore_cursor = FALSE;
2685
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002686 ret = OK;
2687 goto theend;
2688 }
2689 }
2690 ret = FAIL;
2691
2692theend:
2693 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002694 if (restore_cursor)
2695 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002696
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002697 // Move a snapshot of the screen contents to the buffer, so that completion
2698 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002699 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2700 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002701
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002702 return ret;
2703}
2704
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002705 static void
2706may_toggle_cursor(term_T *term)
2707{
2708 if (in_terminal_loop == term)
2709 {
2710 if (term->tl_cursor_visible)
2711 cursor_on();
2712 else
2713 cursor_off();
2714 }
2715}
2716
2717/*
Bram Moolenaar83d47902020-03-26 20:34:00 +01002718 * Cache "Terminal" highlight group colors.
2719 */
2720 void
2721set_terminal_default_colors(int cterm_fg, int cterm_bg)
2722{
2723 term_default_cterm_fg = cterm_fg - 1;
2724 term_default_cterm_bg = cterm_bg - 1;
2725}
2726
2727 static int
2728get_default_cterm_fg(term_T *term)
2729{
2730 if (term->tl_highlight_name != NULL)
2731 {
2732 int id = syn_name2id(term->tl_highlight_name);
2733 int fg = -1;
2734 int bg = -1;
2735
2736 if (id > 0)
2737 syn_id2cterm_bg(id, &fg, &bg);
2738 return fg;
2739 }
2740 return term_default_cterm_fg;
2741}
2742
2743 static int
2744get_default_cterm_bg(term_T *term)
2745{
2746 if (term->tl_highlight_name != NULL)
2747 {
2748 int id = syn_name2id(term->tl_highlight_name);
2749 int fg = -1;
2750 int bg = -1;
2751
2752 if (id > 0)
2753 syn_id2cterm_bg(id, &fg, &bg);
2754 return bg;
2755 }
2756 return term_default_cterm_bg;
2757}
2758
2759/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002760 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002761 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002762 */
2763 static int
2764color2index(VTermColor *color, int fg, int *boldp)
2765{
2766 int red = color->red;
2767 int blue = color->blue;
2768 int green = color->green;
2769
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002770 if (VTERM_COLOR_IS_DEFAULT_FG(color)
2771 || VTERM_COLOR_IS_DEFAULT_BG(color))
2772 return 0;
2773 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002774 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002775 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002776 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002777 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002778 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002779 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2780 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2781 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002782 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002783 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2784 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2785 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2786 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2787 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2788 case 10: return lookup_color(20, fg, boldp) + 1; // red
2789 case 11: return lookup_color(16, fg, boldp) + 1; // green
2790 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2791 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2792 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2793 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2794 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002795 }
2796 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002797
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002798 if (t_colors >= 256)
2799 {
2800 if (red == blue && red == green)
2801 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002802 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002803 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002804 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2805 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2806 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002807 int i;
2808
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002809 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002810 return 17; // 00/00/00
2811 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002812 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002813 for (i = 0; i < 23; ++i)
2814 if (red < cutoff[i])
2815 return i + 233;
2816 return 256;
2817 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002818 {
2819 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2820 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002821
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002822 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002823 for (ri = 0; ri < 5; ++ri)
2824 if (red < cutoff[ri])
2825 break;
2826 for (gi = 0; gi < 5; ++gi)
2827 if (green < cutoff[gi])
2828 break;
2829 for (bi = 0; bi < 5; ++bi)
2830 if (blue < cutoff[bi])
2831 break;
2832 return 17 + ri * 36 + gi * 6 + bi;
2833 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002834 }
2835 return 0;
2836}
2837
2838/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002839 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002840 */
2841 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002842vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002843{
2844 int attr = 0;
2845
2846 if (cellattrs.bold)
2847 attr |= HL_BOLD;
2848 if (cellattrs.underline)
2849 attr |= HL_UNDERLINE;
2850 if (cellattrs.italic)
2851 attr |= HL_ITALIC;
2852 if (cellattrs.strike)
2853 attr |= HL_STRIKETHROUGH;
2854 if (cellattrs.reverse)
2855 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002856 return attr;
2857}
2858
2859/*
2860 * Store Vterm attributes in "cell" from highlight flags.
2861 */
2862 static void
2863hl2vtermAttr(int attr, cellattr_T *cell)
2864{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002865 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002866 if (attr & HL_BOLD)
2867 cell->attrs.bold = 1;
2868 if (attr & HL_UNDERLINE)
2869 cell->attrs.underline = 1;
2870 if (attr & HL_ITALIC)
2871 cell->attrs.italic = 1;
2872 if (attr & HL_STRIKETHROUGH)
2873 cell->attrs.strike = 1;
2874 if (attr & HL_INVERSE)
2875 cell->attrs.reverse = 1;
2876}
2877
2878/*
2879 * Convert the attributes of a vterm cell into an attribute index.
2880 */
2881 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002882cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002883 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002884 win_T *wp,
2885 VTermScreenCellAttrs cellattrs,
2886 VTermColor cellfg,
2887 VTermColor cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002888{
2889 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002890
2891#ifdef FEAT_GUI
2892 if (gui.in_use)
2893 {
2894 guicolor_T fg, bg;
2895
2896 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2897 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2898 return get_gui_attr_idx(attr, fg, bg);
2899 }
2900 else
2901#endif
2902#ifdef FEAT_TERMGUICOLORS
2903 if (p_tgc)
2904 {
2905 guicolor_T fg, bg;
2906
2907 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2908 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2909
2910 return get_tgc_attr_idx(attr, fg, bg);
2911 }
2912 else
2913#endif
2914 {
2915 int bold = MAYBE;
2916 int fg = color2index(&cellfg, TRUE, &bold);
2917 int bg = color2index(&cellbg, FALSE, &bold);
2918
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002919 // Use the 'wincolor' or "Terminal" highlighting for the default
2920 // colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002921 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002922 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002923 int wincolor_fg = -1;
2924 int wincolor_bg = -1;
2925
2926 if (wp != NULL && *wp->w_p_wcr != NUL)
2927 {
2928 int id = syn_name2id(curwin->w_p_wcr);
2929
2930 // Get the 'wincolor' group colors.
2931 if (id > 0)
2932 syn_id2cterm_bg(id, &wincolor_fg, &wincolor_bg);
2933 }
2934 if (fg == 0)
2935 {
2936 if (wincolor_fg >= 0)
2937 fg = wincolor_fg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002938 else
2939 {
2940 int cterm_fg = get_default_cterm_fg(term);
2941
2942 if (cterm_fg >= 0)
2943 fg = cterm_fg + 1;
2944 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002945 }
2946 if (bg == 0)
2947 {
2948 if (wincolor_bg >= 0)
2949 bg = wincolor_bg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002950 else
2951 {
2952 int cterm_bg = get_default_cterm_bg(term);
2953
2954 if (cterm_bg >= 0)
2955 bg = cterm_bg + 1;
2956 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002957 }
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002958 }
2959
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002960 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002961 if (bold == TRUE)
2962 attr |= HL_BOLD;
2963 return get_cterm_attr_idx(attr, fg, bg);
2964 }
2965 return 0;
2966}
2967
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002968 static void
2969set_dirty_snapshot(term_T *term)
2970{
2971 term->tl_dirty_snapshot = TRUE;
2972#ifdef FEAT_TIMERS
2973 if (!term->tl_normal_mode)
2974 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002975 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002976 profile_setlimit(100L, &term->tl_timer_due);
2977 term->tl_timer_set = TRUE;
2978 }
2979#endif
2980}
2981
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002982 static int
2983handle_damage(VTermRect rect, void *user)
2984{
2985 term_T *term = (term_T *)user;
2986
2987 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2988 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002989 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002990 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002991 return 1;
2992}
2993
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002994 static void
2995term_scroll_up(term_T *term, int start_row, int count)
2996{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002997 win_T *wp = NULL;
2998 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002999 VTermColor fg, bg;
3000 VTermScreenCellAttrs attr;
3001 int clear_attr;
3002
Bram Moolenaara80faa82020-04-12 19:37:17 +02003003 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003004
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003005 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003006 {
3007 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003008 {
3009 // Set the color to clear lines with.
3010 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
3011 &fg, &bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01003012 clear_attr = cell2attr(term, wp, attr, fg, bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003013 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003014 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003015 }
3016}
3017
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003018 static int
3019handle_moverect(VTermRect dest, VTermRect src, void *user)
3020{
3021 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003022 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003023
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003024 // Scrolling up is done much more efficiently by deleting lines instead of
3025 // redrawing the text. But avoid doing this multiple times, postpone until
3026 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003027 if (dest.start_col == src.start_col
3028 && dest.end_col == src.end_col
3029 && dest.start_row < src.start_row)
3030 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003031 if (dest.start_row == 0)
3032 term->tl_postponed_scroll += count;
3033 else
3034 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003035 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003036
3037 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3038 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003039 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003040
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003041 // Note sure if the scrolling will work correctly, let's do a complete
3042 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003043 redraw_buf_later(term->tl_buffer, NOT_VALID);
3044 return 1;
3045}
3046
3047 static int
3048handle_movecursor(
3049 VTermPos pos,
3050 VTermPos oldpos UNUSED,
3051 int visible,
3052 void *user)
3053{
3054 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003055 win_T *wp = NULL;
3056 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003057
3058 term->tl_cursor_pos = pos;
3059 term->tl_cursor_visible = visible;
3060
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003061 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003062 {
3063 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003064 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003065 }
3066 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003067 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003068
3069 return 1;
3070}
3071
3072 static int
3073handle_settermprop(
3074 VTermProp prop,
3075 VTermValue *value,
3076 void *user)
3077{
3078 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003079 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003080
3081 switch (prop)
3082 {
3083 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003084 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003085 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003086 if (strval == NULL)
3087 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003088 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003089 // a blank title isn't useful, make it empty, so that "running" is
3090 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003091 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003092 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003093 // Same as blank
3094 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003095 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003096 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3097 term->tl_title = NULL;
3098 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003099 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003100 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003101#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003102 else if (!enc_utf8 && enc_codepage > 0)
3103 {
3104 WCHAR *ret = NULL;
3105 int length = 0;
3106
3107 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003108 (char*)value->string.str,
3109 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003110 if (ret != NULL)
3111 {
3112 WideCharToMultiByte_alloc(enc_codepage, 0,
3113 ret, length, (char**)&term->tl_title,
3114 &length, 0, 0);
3115 vim_free(ret);
3116 }
3117 }
3118#endif
3119 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003120 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003121 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003122 strval = NULL;
3123 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003124 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003125 if (term == curbuf->b_term)
3126 maketitle();
3127 break;
3128
3129 case VTERM_PROP_CURSORVISIBLE:
3130 term->tl_cursor_visible = value->boolean;
3131 may_toggle_cursor(term);
3132 out_flush();
3133 break;
3134
3135 case VTERM_PROP_CURSORBLINK:
3136 term->tl_cursor_blink = value->boolean;
3137 may_set_cursor_props(term);
3138 break;
3139
3140 case VTERM_PROP_CURSORSHAPE:
3141 term->tl_cursor_shape = value->number;
3142 may_set_cursor_props(term);
3143 break;
3144
3145 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003146 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003147 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003148 if (strval == NULL)
3149 break;
3150 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003151 may_set_cursor_props(term);
3152 break;
3153
3154 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003155 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003156 term->tl_using_altscreen = value->boolean;
3157 break;
3158
3159 default:
3160 break;
3161 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003162 vim_free(strval);
3163
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003164 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003165 return 1;
3166}
3167
3168/*
3169 * The job running in the terminal resized the terminal.
3170 */
3171 static int
3172handle_resize(int rows, int cols, void *user)
3173{
3174 term_T *term = (term_T *)user;
3175 win_T *wp;
3176
3177 term->tl_rows = rows;
3178 term->tl_cols = cols;
3179 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003180 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003181 term->tl_vterm_size_changed = FALSE;
3182 else
3183 {
3184 FOR_ALL_WINDOWS(wp)
3185 {
3186 if (wp->w_buffer == term->tl_buffer)
3187 {
3188 win_setheight_win(rows, wp);
3189 win_setwidth_win(cols, wp);
3190 }
3191 }
3192 redraw_buf_later(term->tl_buffer, NOT_VALID);
3193 }
3194 return 1;
3195}
3196
3197/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003198 * If the number of lines that are stored goes over 'termscrollback' then
3199 * delete the first 10%.
3200 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3201 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003202 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003203 static void
3204limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003205{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003206 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003207 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003208 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003209 int i;
3210
3211 curbuf = term->tl_buffer;
3212 for (i = 0; i < todo; ++i)
3213 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003214 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3215 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003216 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003217 }
3218 curbuf = curwin->w_buffer;
3219
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003220 gap->ga_len -= todo;
3221 mch_memmove(gap->ga_data,
3222 (sb_line_T *)gap->ga_data + todo,
3223 sizeof(sb_line_T) * gap->ga_len);
3224 if (update_buffer)
3225 term->tl_scrollback_scrolled -= todo;
3226 }
3227}
3228
3229/*
3230 * Handle a line that is pushed off the top of the screen.
3231 */
3232 static int
3233handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3234{
3235 term_T *term = (term_T *)user;
3236 garray_T *gap;
3237 int update_buffer;
3238
3239 if (term->tl_normal_mode)
3240 {
3241 // In Terminal-Normal mode the user interacts with the buffer, thus we
3242 // must not change it. Postpone adding the scrollback lines.
3243 gap = &term->tl_scrollback_postponed;
3244 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003245 }
3246 else
3247 {
3248 // First remove the lines that were appended before, the pushed line
3249 // goes above it.
3250 cleanup_scrollback(term);
3251 gap = &term->tl_scrollback;
3252 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003253 }
3254
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003255 limit_scrollback(term, gap, update_buffer);
3256
3257 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003258 {
3259 cellattr_T *p = NULL;
3260 int len = 0;
3261 int i;
3262 int c;
3263 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003264 int text_len;
3265 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003266 sb_line_T *line;
3267 garray_T ga;
3268 cellattr_T fill_attr = term->tl_default_color;
3269
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003270 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003271 for (i = 0; i < cols; ++i)
3272 if (cells[i].chars[0] != 0)
3273 len = i + 1;
3274 else
3275 cell2cellattr(&cells[i], &fill_attr);
3276
3277 ga_init2(&ga, 1, 100);
3278 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003279 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003280 if (p != NULL)
3281 {
3282 for (col = 0; col < len; col += cells[col].width)
3283 {
3284 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3285 {
3286 ga.ga_len = 0;
3287 break;
3288 }
3289 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3290 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3291 (char_u *)ga.ga_data + ga.ga_len);
3292 cell2cellattr(&cells[col], &p[col]);
3293 }
3294 }
3295 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003296 {
3297 if (update_buffer)
3298 text = (char_u *)"";
3299 else
3300 text = vim_strsave((char_u *)"");
3301 text_len = 0;
3302 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003303 else
3304 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003305 text = ga.ga_data;
3306 text_len = ga.ga_len;
3307 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003308 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003309 if (update_buffer)
3310 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003311
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003312 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003313 line->sb_cols = len;
3314 line->sb_cells = p;
3315 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003316 if (update_buffer)
3317 {
3318 line->sb_text = NULL;
3319 ++term->tl_scrollback_scrolled;
3320 ga_clear(&ga); // free the text
3321 }
3322 else
3323 {
3324 line->sb_text = text;
3325 ga_init(&ga); // text is kept in tl_scrollback_postponed
3326 }
3327 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003328 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003329 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003330}
3331
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003332/*
3333 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3334 * received and stored in tl_scrollback_postponed.
3335 */
3336 static void
3337handle_postponed_scrollback(term_T *term)
3338{
3339 int i;
3340
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003341 if (term->tl_scrollback_postponed.ga_len == 0)
3342 return;
3343 ch_log(NULL, "Moving postponed scrollback to scrollback");
3344
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003345 // First remove the lines that were appended before, the pushed lines go
3346 // above it.
3347 cleanup_scrollback(term);
3348
3349 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3350 {
3351 char_u *text;
3352 sb_line_T *pp_line;
3353 sb_line_T *line;
3354
3355 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3356 break;
3357 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3358
3359 text = pp_line->sb_text;
3360 if (text == NULL)
3361 text = (char_u *)"";
3362 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3363 vim_free(pp_line->sb_text);
3364
3365 line = (sb_line_T *)term->tl_scrollback.ga_data
3366 + term->tl_scrollback.ga_len;
3367 line->sb_cols = pp_line->sb_cols;
3368 line->sb_cells = pp_line->sb_cells;
3369 line->sb_fill_attr = pp_line->sb_fill_attr;
3370 line->sb_text = NULL;
3371 ++term->tl_scrollback_scrolled;
3372 ++term->tl_scrollback.ga_len;
3373 }
3374
3375 ga_clear(&term->tl_scrollback_postponed);
3376 limit_scrollback(term, &term->tl_scrollback, TRUE);
3377}
3378
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003379static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003380 handle_damage, // damage
3381 handle_moverect, // moverect
3382 handle_movecursor, // movecursor
3383 handle_settermprop, // settermprop
3384 NULL, // bell
3385 handle_resize, // resize
3386 handle_pushline, // sb_pushline
3387 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003388};
3389
3390/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003391 * Do the work after the channel of a terminal was closed.
3392 * Must be called only when updating_screen is FALSE.
3393 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3394 */
3395 static int
3396term_after_channel_closed(term_T *term)
3397{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003398 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003399 if (!term->tl_normal_mode)
3400 {
3401 int fnum = term->tl_buffer->b_fnum;
3402
3403 cleanup_vterm(term);
3404
3405 if (term->tl_finish == TL_FINISH_CLOSE)
3406 {
3407 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003408 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003409#ifdef FEAT_PROP_POPUP
3410 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003411
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003412 // If this was a terminal in a popup window, go back to the
3413 // previous window.
3414 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3415 {
3416 pwin = curwin;
3417 if (win_valid(prevwin))
3418 win_enter(prevwin, FALSE);
3419 }
3420 else
3421#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003422 // If this is the last normal window: exit Vim.
3423 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3424 {
3425 exarg_T ea;
3426
Bram Moolenaara80faa82020-04-12 19:37:17 +02003427 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003428 ex_quit(&ea);
3429 return TRUE;
3430 }
3431
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003432 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003433 ch_log(NULL, "terminal job finished, closing window");
3434 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003435 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003436 if (curwin == aucmd_win)
3437 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003438 if (do_set_w_closing)
3439 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003440 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003441 if (do_set_w_closing)
3442 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003443 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003444#ifdef FEAT_PROP_POPUP
3445 if (pwin != NULL)
3446 popup_close_with_retval(pwin, 0);
3447#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003448 return TRUE;
3449 }
3450 if (term->tl_finish == TL_FINISH_OPEN
3451 && term->tl_buffer->b_nwindows == 0)
3452 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003453 char *cmd = term->tl_opencmd == NULL
3454 ? "botright sbuf %d"
3455 : (char *)term->tl_opencmd;
3456 size_t len = strlen(cmd) + 50;
3457 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003458
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003459 if (buf != NULL)
3460 {
3461 ch_log(NULL, "terminal job finished, opening window");
3462 vim_snprintf(buf, len, cmd, fnum);
3463 do_cmdline_cmd((char_u *)buf);
3464 vim_free(buf);
3465 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003466 }
3467 else
3468 ch_log(NULL, "terminal job finished");
3469 }
3470
3471 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3472 return FALSE;
3473}
3474
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003475#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3476/*
3477 * If the current window is a terminal in a popup window and the job has
3478 * finished, close the popup window and to back to the previous window.
3479 * Otherwise return FAIL.
3480 */
3481 int
3482may_close_term_popup(void)
3483{
3484 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3485 && !term_job_running(curbuf->b_term))
3486 {
3487 win_T *pwin = curwin;
3488
3489 if (win_valid(prevwin))
3490 win_enter(prevwin, FALSE);
3491 popup_close_with_retval(pwin, 0);
3492 return OK;
3493 }
3494 return FAIL;
3495}
3496#endif
3497
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003498/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003499 * Called when a channel has been closed.
3500 * If this was a channel for a terminal window then finish it up.
3501 */
3502 void
3503term_channel_closed(channel_T *ch)
3504{
3505 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003506 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003507 int did_one = FALSE;
3508
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003509 for (term = first_term; term != NULL; term = next_term)
3510 {
3511 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003512 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003513 {
3514 term->tl_channel_closed = TRUE;
3515 did_one = TRUE;
3516
Bram Moolenaard23a8232018-02-10 18:45:26 +01003517 VIM_CLEAR(term->tl_title);
3518 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003519#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003520 if (term->tl_out_fd != NULL)
3521 {
3522 fclose(term->tl_out_fd);
3523 term->tl_out_fd = NULL;
3524 }
3525#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003526
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003527 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003528 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003529 // Cannot open or close windows now. Can happen when
3530 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003531 term->tl_channel_recently_closed = TRUE;
3532 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003533 }
3534
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003535 if (term_after_channel_closed(term))
3536 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003537 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003538 }
3539
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003540 if (did_one)
3541 {
3542 redraw_statuslines();
3543
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003544 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003545 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003546 typebuf_was_filled = TRUE;
3547
3548 term = curbuf->b_term;
3549 if (term != NULL)
3550 {
3551 if (term->tl_job == ch->ch_job)
3552 maketitle();
3553 update_cursor(term, term->tl_cursor_visible);
3554 }
3555 }
3556}
3557
3558/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003559 * To be called after resetting updating_screen: handle any terminal where the
3560 * channel was closed.
3561 */
3562 void
3563term_check_channel_closed_recently()
3564{
3565 term_T *term;
3566 term_T *next_term;
3567
3568 for (term = first_term; term != NULL; term = next_term)
3569 {
3570 next_term = term->tl_next;
3571 if (term->tl_channel_recently_closed)
3572 {
3573 term->tl_channel_recently_closed = FALSE;
3574 if (term_after_channel_closed(term))
3575 // start over, the list may have changed
3576 next_term = first_term;
3577 }
3578 }
3579}
3580
3581/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003582 * Fill one screen line from a line of the terminal.
3583 * Advances "pos" to past the last column.
3584 */
3585 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003586term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003587 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003588 win_T *wp,
3589 VTermScreen *screen,
3590 VTermPos *pos,
3591 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003592{
3593 int off = screen_get_current_line_off();
3594
3595 for (pos->col = 0; pos->col < max_col; )
3596 {
3597 VTermScreenCell cell;
3598 int c;
3599
3600 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003601 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003602
3603 c = cell.chars[0];
3604 if (c == NUL)
3605 {
3606 ScreenLines[off] = ' ';
3607 if (enc_utf8)
3608 ScreenLinesUC[off] = NUL;
3609 }
3610 else
3611 {
3612 if (enc_utf8)
3613 {
3614 int i;
3615
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003616 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003617 for (i = 0; i < Screen_mco
3618 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3619 {
3620 ScreenLinesC[i][off] = cell.chars[i + 1];
3621 if (cell.chars[i + 1] == 0)
3622 break;
3623 }
3624 if (c >= 0x80 || (Screen_mco > 0
3625 && ScreenLinesC[0][off] != 0))
3626 {
3627 ScreenLines[off] = ' ';
3628 ScreenLinesUC[off] = c;
3629 }
3630 else
3631 {
3632 ScreenLines[off] = c;
3633 ScreenLinesUC[off] = NUL;
3634 }
3635 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003636#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003637 else if (has_mbyte && c >= 0x80)
3638 {
3639 char_u mb[MB_MAXBYTES+1];
3640 WCHAR wc = c;
3641
3642 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3643 (char*)mb, 2, 0, 0) > 1)
3644 {
3645 ScreenLines[off] = mb[0];
3646 ScreenLines[off + 1] = mb[1];
3647 cell.width = mb_ptr2cells(mb);
3648 }
3649 else
3650 ScreenLines[off] = c;
3651 }
3652#endif
3653 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003654 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003655 ScreenLines[off] = c;
3656 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003657 ScreenAttrs[off] = cell2attr(term, wp, cell.attrs, cell.fg, cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003658
3659 ++pos->col;
3660 ++off;
3661 if (cell.width == 2)
3662 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003663 // don't set the second byte to NUL for a DBCS encoding, it
3664 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003665 if (enc_utf8)
3666 {
3667 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003668 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003669 }
3670 else if (!has_mbyte)
3671 {
3672 // Can't show a double-width character with a single-byte
3673 // 'encoding', just use a space.
3674 ScreenLines[off] = ' ';
3675 ScreenAttrs[off] = ScreenAttrs[off - 1];
3676 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003677
3678 ++pos->col;
3679 ++off;
3680 }
3681 }
3682}
3683
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003684#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003685 static void
3686update_system_term(term_T *term)
3687{
3688 VTermPos pos;
3689 VTermScreen *screen;
3690
3691 if (term->tl_vterm == NULL)
3692 return;
3693 screen = vterm_obtain_screen(term->tl_vterm);
3694
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003695 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003696 while (term->tl_toprow > 0
3697 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3698 {
3699 int save_p_more = p_more;
3700
3701 p_more = FALSE;
3702 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003703 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003704 p_more = save_p_more;
3705 --term->tl_toprow;
3706 }
3707
3708 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3709 && pos.row < Rows; ++pos.row)
3710 {
3711 if (pos.row < term->tl_rows)
3712 {
3713 int max_col = MIN(Columns, term->tl_cols);
3714
Bram Moolenaar83d47902020-03-26 20:34:00 +01003715 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003716 }
3717 else
3718 pos.col = 0;
3719
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003720 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003721 }
3722
3723 term->tl_dirty_row_start = MAX_ROW;
3724 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003725}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003726#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003727
3728/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003729 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3730 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003731 * Terminal-Normal mode.
3732 */
3733 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003734term_do_update_window(win_T *wp)
3735{
3736 term_T *term = wp->w_buffer->b_term;
3737
3738 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3739}
3740
3741/*
3742 * Called to update a window that contains an active terminal.
3743 */
3744 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003745term_update_window(win_T *wp)
3746{
3747 term_T *term = wp->w_buffer->b_term;
3748 VTerm *vterm;
3749 VTermScreen *screen;
3750 VTermState *state;
3751 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003752 int rows, cols;
3753 int newrows, newcols;
3754 int minsize;
3755 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003756
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003757 vterm = term->tl_vterm;
3758 screen = vterm_obtain_screen(vterm);
3759 state = vterm_obtain_state(vterm);
3760
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003761 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3762 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003763 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003764 {
3765 term->tl_dirty_row_start = 0;
3766 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003767
3768 if (term->tl_postponed_scroll > 0
3769 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003770 // Scrolling is usually faster than redrawing, when there are only
3771 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003772 term_scroll_up(term, 0, term->tl_postponed_scroll);
3773 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003774 }
3775
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003776 /*
3777 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003778 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003779 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003780 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003781
Bram Moolenaar498c2562018-04-15 23:45:15 +02003782 newrows = 99999;
3783 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003784 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003785 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003786 // Always use curwin, it may be a popup window.
3787 win_T *wwp = twp == NULL ? curwin : twp;
3788
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003789 // When more than one window shows the same terminal, use the
3790 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003791 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003792 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003793 newrows = MIN(newrows, wwp->w_height);
3794 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003795 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003796 if (twp == NULL)
3797 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003798 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003799 if (newrows == 99999 || newcols == 99999)
3800 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003801 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3802 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3803
Bram Moolenaareba13e42021-02-23 17:47:23 +01003804 // If no cell is visible there is no point in resizing. Also, vterm can't
3805 // handle a zero height.
3806 if (newrows == 0 || newcols == 0)
3807 return;
3808
Bram Moolenaar498c2562018-04-15 23:45:15 +02003809 if (term->tl_rows != newrows || term->tl_cols != newcols)
3810 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003811 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003812 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003813 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003814 newrows);
3815 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003816
3817 // Updating the terminal size will cause the snapshot to be cleared.
3818 // When not in terminal_loop() we need to restore it.
3819 if (term != in_terminal_loop)
3820 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003821 }
3822
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003823 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003824 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003825 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003826
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003827 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3828 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003829 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003830 if (pos.row < term->tl_rows)
3831 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003832 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003833
Bram Moolenaar83d47902020-03-26 20:34:00 +01003834 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003835 }
3836 else
3837 pos.col = 0;
3838
Bram Moolenaarf118d482018-03-13 13:14:00 +01003839 screen_line(wp->w_winrow + pos.row
3840#ifdef FEAT_MENU
3841 + winbar_height(wp)
3842#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003843 , wp->w_wincol, pos.col, wp->w_width,
3844#ifdef FEAT_PROP_POPUP
3845 popup_is_popup(wp) ? SLF_POPUP :
3846#endif
3847 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003848 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003849 term->tl_dirty_row_start = MAX_ROW;
3850 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003851}
3852
3853/*
3854 * Return TRUE if "wp" is a terminal window where the job has finished.
3855 */
3856 int
3857term_is_finished(buf_T *buf)
3858{
3859 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3860}
3861
3862/*
3863 * Return TRUE if "wp" is a terminal window where the job has finished or we
3864 * are in Terminal-Normal mode, thus we show the buffer contents.
3865 */
3866 int
3867term_show_buffer(buf_T *buf)
3868{
3869 term_T *term = buf->b_term;
3870
3871 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3872}
3873
3874/*
3875 * The current buffer is going to be changed. If there is terminal
3876 * highlighting remove it now.
3877 */
3878 void
3879term_change_in_curbuf(void)
3880{
3881 term_T *term = curbuf->b_term;
3882
3883 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3884 {
3885 free_scrollback(term);
3886 redraw_buf_later(term->tl_buffer, NOT_VALID);
3887
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003888 // The buffer is now like a normal buffer, it cannot be easily
3889 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003890 set_string_option_direct((char_u *)"buftype", -1,
3891 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3892 }
3893}
3894
3895/*
3896 * Get the screen attribute for a position in the buffer.
3897 * Use a negative "col" to get the filler background color.
3898 */
3899 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003900term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003901{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003902 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003903 term_T *term = buf->b_term;
3904 sb_line_T *line;
3905 cellattr_T *cellattr;
3906
3907 if (lnum > term->tl_scrollback.ga_len)
3908 cellattr = &term->tl_default_color;
3909 else
3910 {
3911 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3912 if (col < 0 || col >= line->sb_cols)
3913 cellattr = &line->sb_fill_attr;
3914 else
3915 cellattr = line->sb_cells + col;
3916 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003917 return cell2attr(term, wp, cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003918}
3919
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003920/*
3921 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003922 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003923 */
3924 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003925cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003926{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003927 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3928 if (rgb->index == 0)
3929 rgb->type = VTERM_COLOR_RGB;
3930 else
3931 {
3932 rgb->type = VTERM_COLOR_INDEXED;
3933 --rgb->index;
3934 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003935}
3936
3937/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003938 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003939 */
3940 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003941init_default_colors(term_T *term, win_T *wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003942{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003943 VTermColor *fg, *bg;
3944 int fgval, bgval;
3945 int id;
3946
Bram Moolenaara80faa82020-04-12 19:37:17 +02003947 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003948 term->tl_default_color.width = 1;
3949 fg = &term->tl_default_color.fg;
3950 bg = &term->tl_default_color.bg;
3951
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003952 // Vterm uses a default black background. Set it to white when
3953 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003954 if (*p_bg == 'l')
3955 {
3956 fgval = 0;
3957 bgval = 255;
3958 }
3959 else
3960 {
3961 fgval = 255;
3962 bgval = 0;
3963 }
3964 fg->red = fg->green = fg->blue = fgval;
3965 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003966 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3967 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003968
Bram Moolenaar83d47902020-03-26 20:34:00 +01003969 // The 'wincolor' or the highlight group overrules the defaults.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003970 if (wp != NULL && *wp->w_p_wcr != NUL)
3971 id = syn_name2id(wp->w_p_wcr);
3972 else
Bram Moolenaar83d47902020-03-26 20:34:00 +01003973 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003974
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003975 // Use the actual color for the GUI and when 'termguicolors' is set.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003976#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3977 if (0
3978# ifdef FEAT_GUI
3979 || gui.in_use
3980# endif
3981# ifdef FEAT_TERMGUICOLORS
3982 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003983# ifdef FEAT_VTP
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003984 // Finally get INVALCOLOR on this execution path
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003985 || (!p_tgc && t_colors >= 256)
3986# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003987# endif
3988 )
3989 {
3990 guicolor_T fg_rgb = INVALCOLOR;
3991 guicolor_T bg_rgb = INVALCOLOR;
3992
3993 if (id != 0)
3994 syn_id2colors(id, &fg_rgb, &bg_rgb);
3995
3996# ifdef FEAT_GUI
3997 if (gui.in_use)
3998 {
3999 if (fg_rgb == INVALCOLOR)
4000 fg_rgb = gui.norm_pixel;
4001 if (bg_rgb == INVALCOLOR)
4002 bg_rgb = gui.back_pixel;
4003 }
4004# ifdef FEAT_TERMGUICOLORS
4005 else
4006# endif
4007# endif
4008# ifdef FEAT_TERMGUICOLORS
4009 {
4010 if (fg_rgb == INVALCOLOR)
4011 fg_rgb = cterm_normal_fg_gui_color;
4012 if (bg_rgb == INVALCOLOR)
4013 bg_rgb = cterm_normal_bg_gui_color;
4014 }
4015# endif
4016 if (fg_rgb != INVALCOLOR)
4017 {
4018 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
4019
4020 fg->red = (unsigned)(rgb >> 16);
4021 fg->green = (unsigned)(rgb >> 8) & 255;
4022 fg->blue = (unsigned)rgb & 255;
4023 }
4024 if (bg_rgb != INVALCOLOR)
4025 {
4026 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
4027
4028 bg->red = (unsigned)(rgb >> 16);
4029 bg->green = (unsigned)(rgb >> 8) & 255;
4030 bg->blue = (unsigned)rgb & 255;
4031 }
4032 }
4033 else
4034#endif
4035 if (id != 0 && t_colors >= 16)
4036 {
Bram Moolenaar83d47902020-03-26 20:34:00 +01004037 int cterm_fg = get_default_cterm_fg(term);
4038 int cterm_bg = get_default_cterm_bg(term);
4039
4040 if (cterm_fg >= 0)
4041 cterm_color2vterm(cterm_fg, fg);
4042 if (cterm_bg >= 0)
4043 cterm_color2vterm(cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004044 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004045 else
4046 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004047#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004048 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004049#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004050
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004051 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004052 if (cterm_normal_fg_color > 0)
4053 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004054 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004055# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4056# ifdef VIMDLL
4057 if (!gui.in_use)
4058# endif
4059 {
4060 tmp = fg->red;
4061 fg->red = fg->blue;
4062 fg->blue = tmp;
4063 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004064# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004065 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004066# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004067 else
4068 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004069# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004070
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004071 if (cterm_normal_bg_color > 0)
4072 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004073 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004074# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4075# ifdef VIMDLL
4076 if (!gui.in_use)
4077# endif
4078 {
4079 tmp = fg->red;
4080 fg->red = fg->blue;
4081 fg->blue = tmp;
4082 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004083# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004084 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004085# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004086 else
4087 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004088# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004089 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004090}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004091
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004092#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4093/*
4094 * Set the 16 ANSI colors from array of RGB values
4095 */
4096 static void
4097set_vterm_palette(VTerm *vterm, long_u *rgb)
4098{
4099 int index = 0;
4100 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004101
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004102 for (; index < 16; index++)
4103 {
4104 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004105
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004106 color.red = (unsigned)(rgb[index] >> 16);
4107 color.green = (unsigned)(rgb[index] >> 8) & 255;
4108 color.blue = (unsigned)rgb[index] & 255;
4109 vterm_state_set_palette_color(state, index, &color);
4110 }
4111}
4112
4113/*
4114 * Set the ANSI color palette from a list of colors
4115 */
4116 static int
4117set_ansi_colors_list(VTerm *vterm, list_T *list)
4118{
4119 int n = 0;
4120 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004121 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004122
Bram Moolenaarb0992022020-01-30 14:55:42 +01004123 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004124 {
4125 char_u *color_name;
4126 guicolor_T guicolor;
4127
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004128 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004129 if (color_name == NULL)
4130 return FAIL;
4131
4132 guicolor = GUI_GET_COLOR(color_name);
4133 if (guicolor == INVALCOLOR)
4134 return FAIL;
4135
4136 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4137 }
4138
4139 if (n != 16 || li != NULL)
4140 return FAIL;
4141
4142 set_vterm_palette(vterm, rgb);
4143
4144 return OK;
4145}
4146
4147/*
4148 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4149 */
4150 static void
4151init_vterm_ansi_colors(VTerm *vterm)
4152{
4153 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4154
4155 if (var != NULL
4156 && (var->di_tv.v_type != VAR_LIST
4157 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004158 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004159 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004160 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004161}
4162#endif
4163
Bram Moolenaar52acb112018-03-18 19:20:22 +01004164/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004165 * Handles a "drop" command from the job in the terminal.
4166 * "item" is the file name, "item->li_next" may have options.
4167 */
4168 static void
4169handle_drop_command(listitem_T *item)
4170{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004171 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004172 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004173 int bufnr;
4174 win_T *wp;
4175 tabpage_T *tp;
4176 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004177 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004178
4179 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4180 FOR_ALL_TAB_WINDOWS(tp, wp)
4181 {
4182 if (wp->w_buffer->b_fnum == bufnr)
4183 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004184 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004185 goto_tabpage_win(tp, wp);
4186 return;
4187 }
4188 }
4189
Bram Moolenaara80faa82020-04-12 19:37:17 +02004190 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004191
4192 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4193 && opt_item->li_tv.vval.v_dict != NULL)
4194 {
4195 dict_T *dict = opt_item->li_tv.vval.v_dict;
4196 char_u *p;
4197
Bram Moolenaar8f667172018-12-14 15:38:31 +01004198 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004199 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004200 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004201 if (p != NULL)
4202 {
4203 if (check_ff_value(p) == FAIL)
4204 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4205 else
4206 ea.force_ff = *p;
4207 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004208 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004209 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004210 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004211 if (p != NULL)
4212 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004213 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004214 if (ea.cmd != NULL)
4215 {
4216 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4217 ea.force_enc = 11;
4218 tofree = ea.cmd;
4219 }
4220 }
4221
Bram Moolenaar8f667172018-12-14 15:38:31 +01004222 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004223 if (p != NULL)
4224 get_bad_opt(p, &ea);
4225
4226 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4227 ea.force_bin = FORCE_BIN;
4228 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4229 ea.force_bin = FORCE_BIN;
4230 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4231 ea.force_bin = FORCE_NOBIN;
4232 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4233 ea.force_bin = FORCE_NOBIN;
4234 }
4235
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004236 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004237 if (ea.cmd == NULL)
4238 ea.cmd = (char_u *)"split";
4239 ea.arg = fname;
4240 ea.cmdidx = CMD_split;
4241 ex_splitview(&ea);
4242
4243 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004244}
4245
4246/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004247 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4248 */
4249 static int
4250is_permitted_term_api(char_u *func, char_u *pat)
4251{
4252 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4253}
4254
4255/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004256 * Handles a function call from the job running in a terminal.
4257 * "item" is the function name, "item->li_next" has the arguments.
4258 */
4259 static void
4260handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4261{
4262 char_u *func;
4263 typval_T argvars[2];
4264 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004265 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004266
4267 if (item->li_next == NULL)
4268 {
4269 ch_log(channel, "Missing function arguments for call");
4270 return;
4271 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004272 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004273
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004274 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004275 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004276 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004277 return;
4278 }
4279
4280 argvars[0].v_type = VAR_NUMBER;
4281 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4282 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004283 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004284 funcexe.firstline = 1L;
4285 funcexe.lastline = 1L;
4286 funcexe.evaluate = TRUE;
4287 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004288 {
4289 clear_tv(&rettv);
4290 ch_log(channel, "Function %s called", func);
4291 }
4292 else
4293 ch_log(channel, "Calling function %s failed", func);
4294}
4295
4296/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004297 * URL decoding (also know as Percent-encoding).
4298 *
4299 * Note this function currently is only used for decoding shell's
4300 * OSC 7 escape sequence which we can assume all bytes are valid
4301 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4302 * encoding bytes like 0xfe, 0xff.
4303 */
4304 static size_t
4305url_decode(const char *src, const size_t len, char_u *dst)
4306{
4307 size_t i = 0, j = 0;
4308
4309 while (i < len)
4310 {
4311 if (src[i] == '%' && i + 2 < len)
4312 {
4313 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4314 j++;
4315 i += 3;
4316 }
4317 else
4318 {
4319 dst[j] = src[i];
4320 i++;
4321 j++;
4322 }
4323 }
4324 dst[j] = '\0';
4325 return j;
4326}
4327
4328/*
4329 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4330 *
4331 * The OSC 7 sequence has the format of
4332 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4333 * and what VTerm provides via VTermStringFragment is
4334 * "file://HOSTNAME/CURRENT/DIR"
4335 */
4336 static void
4337sync_shell_dir(VTermStringFragment *frag)
4338{
4339 int offset = 7; // len of "file://" is 7
4340 char *pos = (char *)frag->str + offset;
4341 char_u *new_dir;
4342
4343 // remove HOSTNAME to get PWD
4344 while (*pos != '/' && offset < frag->len)
4345 {
4346 offset += 1;
4347 pos += 1;
4348 }
4349
4350 if (offset >= frag->len)
4351 {
4352 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
4353 frag->str);
4354 return;
4355 }
4356
4357 new_dir = alloc(frag->len - offset + 1);
4358 url_decode(pos, frag->len-offset, new_dir);
4359 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4360 vim_free(new_dir);
4361}
4362
4363/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004364 * Called by libvterm when it cannot recognize an OSC sequence.
4365 * We recognize a terminal API command.
4366 */
4367 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004368parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004369{
4370 term_T *term = (term_T *)user;
4371 js_read_T reader;
4372 typval_T tv;
4373 channel_T *channel = term->tl_job == NULL ? NULL
4374 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004375 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004376
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004377 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
4378 if (p_asd && command == 7)
4379 {
4380 sync_shell_dir(&frag);
4381 return 1;
4382 }
4383
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004384 if (command != 51)
4385 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004386
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004387 // Concatenate what was received until the final piece is found.
4388 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4389 {
4390 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004391 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004392 }
4393 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004394 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004395 if (!frag.final)
4396 return 1;
4397
4398 ((char *)gap->ga_data)[gap->ga_len] = 0;
4399 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004400 reader.js_fill = NULL;
4401 reader.js_used = 0;
4402 if (json_decode(&reader, &tv, 0) == OK
4403 && tv.v_type == VAR_LIST
4404 && tv.vval.v_list != NULL)
4405 {
4406 listitem_T *item = tv.vval.v_list->lv_first;
4407
4408 if (item == NULL)
4409 ch_log(channel, "Missing command");
4410 else
4411 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004412 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004413
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004414 // Make sure an invoked command doesn't delete the buffer (and the
4415 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004416 ++term->tl_buffer->b_locked;
4417
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004418 item = item->li_next;
4419 if (item == NULL)
4420 ch_log(channel, "Missing argument for %s", cmd);
4421 else if (STRCMP(cmd, "drop") == 0)
4422 handle_drop_command(item);
4423 else if (STRCMP(cmd, "call") == 0)
4424 handle_call_command(term, channel, item);
4425 else
4426 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004427 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004428 }
4429 }
4430 else
4431 ch_log(channel, "Invalid JSON received");
4432
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004433 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004434 clear_tv(&tv);
4435 return 1;
4436}
4437
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004438/*
4439 * Called by libvterm when it cannot recognize a CSI sequence.
4440 * We recognize the window position report.
4441 */
4442 static int
4443parse_csi(
4444 const char *leader UNUSED,
4445 const long args[],
4446 int argcount,
4447 const char *intermed UNUSED,
4448 char command,
4449 void *user)
4450{
4451 term_T *term = (term_T *)user;
4452 char buf[100];
4453 int len;
4454 int x = 0;
4455 int y = 0;
4456 win_T *wp;
4457
4458 // We recognize only CSI 13 t
4459 if (command != 't' || argcount != 1 || args[0] != 13)
4460 return 0; // not handled
4461
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004462 // When getting the window position is not possible or it fails it results
4463 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004464#if defined(FEAT_GUI) \
4465 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4466 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004467 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004468#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004469
4470 FOR_ALL_WINDOWS(wp)
4471 if (wp->w_buffer == term->tl_buffer)
4472 break;
4473 if (wp != NULL)
4474 {
4475#ifdef FEAT_GUI
4476 if (gui.in_use)
4477 {
4478 x += wp->w_wincol * gui.char_width;
4479 y += W_WINROW(wp) * gui.char_height;
4480 }
4481 else
4482#endif
4483 {
4484 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004485 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004486 x += wp->w_wincol * 7;
4487 y += W_WINROW(wp) * 10;
4488 }
4489 }
4490
4491 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4492 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4493 (char_u *)buf, len, NULL);
4494 return 1;
4495}
4496
Bram Moolenaard8637282020-05-20 18:41:41 +02004497static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004498 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004499 parse_csi, // csi
4500 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004501 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004502};
4503
4504/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004505 * Use Vim's allocation functions for vterm so profiling works.
4506 */
4507 static void *
4508vterm_malloc(size_t size, void *data UNUSED)
4509{
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004510 return alloc_clear(size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004511}
4512
4513 static void
4514vterm_memfree(void *ptr, void *data UNUSED)
4515{
4516 vim_free(ptr);
4517}
4518
4519static VTermAllocatorFunctions vterm_allocator = {
4520 &vterm_malloc,
4521 &vterm_memfree
4522};
4523
4524/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004525 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004526 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004527 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004528 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004529create_vterm(term_T *term, int rows, int cols)
4530{
4531 VTerm *vterm;
4532 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004533 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004534 VTermValue value;
4535
Bram Moolenaar756ef112018-04-10 12:04:27 +02004536 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004537 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004538 if (vterm == NULL)
4539 return FAIL;
4540
4541 // Allocate screen and state here, so we can bail out if that fails.
4542 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004543 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004544 if (state == NULL || screen == NULL)
4545 {
4546 vterm_free(vterm);
4547 return FAIL;
4548 }
4549
Bram Moolenaar52acb112018-03-18 19:20:22 +01004550 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004551 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004552 vterm_set_utf8(vterm, 1);
4553
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004554 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004555
4556 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004557 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004558 &term->tl_default_color.fg,
4559 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004560
Bram Moolenaar9e587872019-05-13 20:27:23 +02004561 if (t_colors < 16)
4562 // Less than 16 colors: assume that bold means using a bright color for
4563 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004564 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4565
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004566 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004567 vterm_screen_reset(screen, 1 /* hard */);
4568
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004569 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004570 vterm_screen_enable_altscreen(screen, 1);
4571
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004572 // For unix do not use a blinking cursor. In an xterm this causes the
4573 // cursor to blink if it's blinking in the xterm.
4574 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004575#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004576 if (GetCaretBlinkTime() == INFINITE)
4577 value.boolean = 0;
4578 else
4579 value.boolean = 1;
4580#else
4581 value.boolean = 0;
4582#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004583 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004584 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004585
4586 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004587}
4588
4589/*
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004590 * Called when 'wincolor' was set.
4591 */
4592 void
Bram Moolenaarad431992021-05-03 20:40:38 +02004593term_update_colors(term_T *term)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004594{
Bram Moolenaarad431992021-05-03 20:40:38 +02004595 win_T *wp;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004596
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01004597 if (term->tl_vterm == NULL)
4598 return;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004599 init_default_colors(term, curwin);
4600 vterm_state_set_default_colors(
4601 vterm_obtain_state(term->tl_vterm),
4602 &term->tl_default_color.fg,
4603 &term->tl_default_color.bg);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01004604
Bram Moolenaarad431992021-05-03 20:40:38 +02004605 FOR_ALL_WINDOWS(wp)
4606 if (wp->w_buffer == term->tl_buffer)
4607 redraw_win_later(wp, NOT_VALID);
4608}
4609
4610/*
4611 * Called when 'background' was set.
4612 */
4613 void
4614term_update_colors_all(void)
4615{
4616 term_T *tp;
4617
4618 FOR_ALL_TERMS(tp)
4619 term_update_colors(tp);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004620}
4621
4622/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004623 * Return the text to show for the buffer name and status.
4624 */
4625 char_u *
4626term_get_status_text(term_T *term)
4627{
4628 if (term->tl_status_text == NULL)
4629 {
4630 char_u *txt;
4631 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004632 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004633
4634 if (term->tl_normal_mode)
4635 {
4636 if (term_job_running(term))
4637 txt = (char_u *)_("Terminal");
4638 else
4639 txt = (char_u *)_("Terminal-finished");
4640 }
4641 else if (term->tl_title != NULL)
4642 txt = term->tl_title;
4643 else if (term_none_open(term))
4644 txt = (char_u *)_("active");
4645 else if (term_job_running(term))
4646 txt = (char_u *)_("running");
4647 else
4648 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004649 fname = buf_get_fname(term->tl_buffer);
4650 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004651 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004652 if (term->tl_status_text != NULL)
4653 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004654 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004655 }
4656 return term->tl_status_text;
4657}
4658
4659/*
4660 * Mark references in jobs of terminals.
4661 */
4662 int
4663set_ref_in_term(int copyID)
4664{
4665 int abort = FALSE;
4666 term_T *term;
4667 typval_T tv;
4668
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004669 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004670 if (term->tl_job != NULL)
4671 {
4672 tv.v_type = VAR_JOB;
4673 tv.vval.v_job = term->tl_job;
4674 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4675 }
4676 return abort;
4677}
4678
4679/*
4680 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004681 * Returns NULL when the buffer is not for a terminal window and logs a message
4682 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004683 */
4684 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004685term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004686{
4687 buf_T *buf;
4688
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004689 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004690 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004691 --emsg_off;
4692 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004693 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004694 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004695 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004696 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004697 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004698 return buf;
4699}
4700
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004701 static void
4702clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004703{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004704 CLEAR_FIELD(*cell);
4705 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4706 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004707}
4708
4709 static void
4710dump_term_color(FILE *fd, VTermColor *color)
4711{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004712 int index;
4713
4714 if (VTERM_COLOR_IS_INDEXED(color))
4715 index = color->index + 1;
4716 else if (color->type == 0)
4717 // use RGB values
4718 index = 255;
4719 else
4720 // default color
4721 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004722 fprintf(fd, "%02x%02x%02x%d",
4723 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004724 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004725}
4726
4727/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004728 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004729 *
4730 * Each screen cell in full is:
4731 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4732 * {characters} is a space for an empty cell
4733 * For a double-width character "+" is changed to "*" and the next cell is
4734 * skipped.
4735 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4736 * when "&" use the same as the previous cell.
4737 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4738 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4739 * {color-idx} is a number from 0 to 255
4740 *
4741 * Screen cell with same width, attributes and color as the previous one:
4742 * |{characters}
4743 *
4744 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4745 *
4746 * Repeating the previous screen cell:
4747 * @{count}
4748 */
4749 void
4750f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4751{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004752 buf_T *buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004753 term_T *term;
4754 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004755 int max_height = 0;
4756 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004757 stat_T st;
4758 FILE *fd;
4759 VTermPos pos;
4760 VTermScreen *screen;
4761 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004762 VTermState *state;
4763 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004764
4765 if (check_restricted() || check_secure())
4766 return;
4767 if (buf == NULL)
4768 return;
4769 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004770 if (term->tl_vterm == NULL)
4771 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004772 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004773 return;
4774 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004775
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004776 if (argvars[2].v_type != VAR_UNKNOWN)
4777 {
4778 dict_T *d;
4779
4780 if (argvars[2].v_type != VAR_DICT)
4781 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004782 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004783 return;
4784 }
4785 d = argvars[2].vval.v_dict;
4786 if (d != NULL)
4787 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004788 max_height = dict_get_number(d, (char_u *)"rows");
4789 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004790 }
4791 }
4792
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004793 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004794 if (fname == NULL)
4795 return;
4796 if (mch_stat((char *)fname, &st) >= 0)
4797 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004798 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004799 return;
4800 }
4801
Bram Moolenaard96ff162018-02-18 22:13:29 +01004802 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4803 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004804 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004805 return;
4806 }
4807
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004808 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004809
4810 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004811 state = vterm_obtain_state(term->tl_vterm);
4812 vterm_state_get_cursorpos(state, &cursor_pos);
4813
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004814 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4815 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004816 {
4817 int repeat = 0;
4818
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004819 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4820 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004821 {
4822 VTermScreenCell cell;
4823 int same_attr;
4824 int same_chars = TRUE;
4825 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004826 int is_cursor_pos = (pos.col == cursor_pos.col
4827 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004828
4829 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004830 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004831
4832 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4833 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004834 int c = cell.chars[i];
4835 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004836 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004837
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004838 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004839 if (i == 0)
4840 {
4841 c = (c == NUL) ? ' ' : c;
4842 pc = (pc == NUL) ? ' ' : pc;
4843 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004844 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004845 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004846 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004847 break;
4848 }
4849 same_attr = vtermAttr2hl(cell.attrs)
4850 == vtermAttr2hl(prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004851 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4852 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004853 if (same_chars && cell.width == prev_cell.width && same_attr
4854 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004855 {
4856 ++repeat;
4857 }
4858 else
4859 {
4860 if (repeat > 0)
4861 {
4862 fprintf(fd, "@%d", repeat);
4863 repeat = 0;
4864 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004865 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004866
4867 if (cell.chars[0] == NUL)
4868 fputs(" ", fd);
4869 else
4870 {
4871 char_u charbuf[10];
4872 int len;
4873
4874 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4875 && cell.chars[i] != NUL; ++i)
4876 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004877 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004878 fwrite(charbuf, len, 1, fd);
4879 }
4880 }
4881
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004882 // When only the characters differ we don't write anything, the
4883 // following "|", "@" or NL will indicate using the same
4884 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004885 if (cell.width != prev_cell.width || !same_attr)
4886 {
4887 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004888 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004889 else
4890 fputs("+", fd);
4891
4892 if (same_attr)
4893 {
4894 fputs("&", fd);
4895 }
4896 else
4897 {
4898 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004899 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004900 fputs("&", fd);
4901 else
4902 {
4903 fputs("#", fd);
4904 dump_term_color(fd, &cell.fg);
4905 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004906 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004907 fputs("&", fd);
4908 else
4909 {
4910 fputs("#", fd);
4911 dump_term_color(fd, &cell.bg);
4912 }
4913 }
4914 }
4915
4916 prev_cell = cell;
4917 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004918
4919 if (cell.width == 2)
4920 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004921 }
4922 if (repeat > 0)
4923 fprintf(fd, "@%d", repeat);
4924 fputs("\n", fd);
4925 }
4926
4927 fclose(fd);
4928}
4929
4930/*
4931 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4932 */
4933 static void
4934dump_is_corrupt(garray_T *gap)
4935{
4936 ga_concat(gap, (char_u *)"CORRUPT");
4937}
4938
4939 static void
4940append_cell(garray_T *gap, cellattr_T *cell)
4941{
4942 if (ga_grow(gap, 1) == OK)
4943 {
4944 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4945 ++gap->ga_len;
4946 }
4947}
4948
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004949 static void
4950clear_cellattr(cellattr_T *cell)
4951{
4952 CLEAR_FIELD(*cell);
4953 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4954 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4955}
4956
Bram Moolenaard96ff162018-02-18 22:13:29 +01004957/*
4958 * Read the dump file from "fd" and append lines to the current buffer.
4959 * Return the cell width of the longest line.
4960 */
4961 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004962read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004963{
4964 int c;
4965 garray_T ga_text;
4966 garray_T ga_cell;
4967 char_u *prev_char = NULL;
4968 int attr = 0;
4969 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004970 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004971 term_T *term = curbuf->b_term;
4972 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004973 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004974
4975 ga_init2(&ga_text, 1, 90);
4976 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004977 clear_cellattr(&cell);
4978 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004979 cursor_pos->row = -1;
4980 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004981
4982 c = fgetc(fd);
4983 for (;;)
4984 {
4985 if (c == EOF)
4986 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004987 if (c == '\r')
4988 {
4989 // DOS line endings? Ignore.
4990 c = fgetc(fd);
4991 }
4992 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004993 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004994 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004995 if (ga_text.ga_data == NULL)
4996 dump_is_corrupt(&ga_text);
4997 if (ga_grow(&term->tl_scrollback, 1) == OK)
4998 {
4999 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
5000 + term->tl_scrollback.ga_len;
5001
5002 if (max_cells < ga_cell.ga_len)
5003 max_cells = ga_cell.ga_len;
5004 line->sb_cols = ga_cell.ga_len;
5005 line->sb_cells = ga_cell.ga_data;
5006 line->sb_fill_attr = term->tl_default_color;
5007 ++term->tl_scrollback.ga_len;
5008 ga_init(&ga_cell);
5009
5010 ga_append(&ga_text, NUL);
5011 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5012 ga_text.ga_len, FALSE);
5013 }
5014 else
5015 ga_clear(&ga_cell);
5016 ga_text.ga_len = 0;
5017
5018 c = fgetc(fd);
5019 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005020 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005021 {
5022 int prev_len = ga_text.ga_len;
5023
Bram Moolenaar9271d052018-02-25 21:39:46 +01005024 if (c == '>')
5025 {
5026 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005027 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005028 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5029 cursor_pos->col = ga_cell.ga_len;
5030 }
5031
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005032 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005033 c = fgetc(fd);
5034 if (c != EOF)
5035 ga_append(&ga_text, c);
5036 for (;;)
5037 {
5038 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005039 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005040 || c == EOF || c == '\n')
5041 break;
5042 ga_append(&ga_text, c);
5043 }
5044
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005045 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005046 vim_free(prev_char);
5047 if (ga_text.ga_data != NULL)
5048 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5049 ga_text.ga_len - prev_len);
5050
Bram Moolenaar9271d052018-02-25 21:39:46 +01005051 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005052 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005053 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005054 }
5055 else if (c == '+' || c == '*')
5056 {
5057 int is_bg;
5058
5059 cell.width = c == '+' ? 1 : 2;
5060
5061 c = fgetc(fd);
5062 if (c == '&')
5063 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005064 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005065 c = fgetc(fd);
5066 }
5067 else if (isdigit(c))
5068 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005069 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005070 attr = 0;
5071 while (isdigit(c))
5072 {
5073 attr = attr * 10 + (c - '0');
5074 c = fgetc(fd);
5075 }
5076 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005077
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005078 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005079 for (is_bg = 0; is_bg <= 1; ++is_bg)
5080 {
5081 if (c == '&')
5082 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005083 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005084 c = fgetc(fd);
5085 }
5086 else if (c == '#')
5087 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005088 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005089
5090 c = fgetc(fd);
5091 red = hex2nr(c);
5092 c = fgetc(fd);
5093 red = (red << 4) + hex2nr(c);
5094 c = fgetc(fd);
5095 green = hex2nr(c);
5096 c = fgetc(fd);
5097 green = (green << 4) + hex2nr(c);
5098 c = fgetc(fd);
5099 blue = hex2nr(c);
5100 c = fgetc(fd);
5101 blue = (blue << 4) + hex2nr(c);
5102 c = fgetc(fd);
5103 if (!isdigit(c))
5104 dump_is_corrupt(&ga_text);
5105 while (isdigit(c))
5106 {
5107 index = index * 10 + (c - '0');
5108 c = fgetc(fd);
5109 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005110 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005111 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005112 type = VTERM_COLOR_RGB;
5113 if (index == 0)
5114 {
5115 if (is_bg)
5116 type |= VTERM_COLOR_DEFAULT_BG;
5117 else
5118 type |= VTERM_COLOR_DEFAULT_FG;
5119 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005120 }
5121 else
5122 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005123 type = VTERM_COLOR_INDEXED;
5124 index -= 1;
5125 }
5126 if (is_bg)
5127 {
5128 cell.bg.type = type;
5129 cell.bg.red = red;
5130 cell.bg.green = green;
5131 cell.bg.blue = blue;
5132 cell.bg.index = index;
5133 }
5134 else
5135 {
5136 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005137 cell.fg.red = red;
5138 cell.fg.green = green;
5139 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005140 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005141 }
5142 }
5143 else
5144 dump_is_corrupt(&ga_text);
5145 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005146 }
5147 else
5148 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005149 }
5150 else
5151 dump_is_corrupt(&ga_text);
5152
5153 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005154 if (cell.width == 2)
5155 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005156 }
5157 else if (c == '@')
5158 {
5159 if (prev_char == NULL)
5160 dump_is_corrupt(&ga_text);
5161 else
5162 {
5163 int count = 0;
5164
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005165 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005166 for (;;)
5167 {
5168 c = fgetc(fd);
5169 if (!isdigit(c))
5170 break;
5171 count = count * 10 + (c - '0');
5172 }
5173
5174 while (count-- > 0)
5175 {
5176 ga_concat(&ga_text, prev_char);
5177 append_cell(&ga_cell, &cell);
5178 }
5179 }
5180 }
5181 else
5182 {
5183 dump_is_corrupt(&ga_text);
5184 c = fgetc(fd);
5185 }
5186 }
5187
5188 if (ga_text.ga_len > 0)
5189 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005190 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005191 dump_is_corrupt(&ga_text);
5192 ga_append(&ga_text, NUL);
5193 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5194 ga_text.ga_len, FALSE);
5195 }
5196
5197 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005198 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005199 vim_free(prev_char);
5200
5201 return max_cells;
5202}
5203
5204/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005205 * Return an allocated string with at least "text_width" "=" characters and
5206 * "fname" inserted in the middle.
5207 */
5208 static char_u *
5209get_separator(int text_width, char_u *fname)
5210{
5211 int width = MAX(text_width, curwin->w_width);
5212 char_u *textline;
5213 int fname_size;
5214 char_u *p = fname;
5215 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005216 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005217
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005218 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005219 if (textline == NULL)
5220 return NULL;
5221
5222 fname_size = vim_strsize(fname);
5223 if (fname_size < width - 8)
5224 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005225 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005226 width = MAX(text_width, fname_size + 8);
5227 }
5228 else if (fname_size > width - 8)
5229 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005230 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005231 p = gettail(fname);
5232 fname_size = vim_strsize(p);
5233 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005234 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005235 while (fname_size > width - 8)
5236 {
5237 p += (*mb_ptr2len)(p);
5238 fname_size = vim_strsize(p);
5239 }
5240
5241 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5242 textline[i] = '=';
5243 textline[i++] = ' ';
5244
5245 STRCPY(textline + i, p);
5246 off = STRLEN(textline);
5247 textline[off] = ' ';
5248 for (i = 1; i < (width - fname_size) / 2; ++i)
5249 textline[off + i] = '=';
5250 textline[off + i] = NUL;
5251
5252 return textline;
5253}
5254
5255/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005256 * Common for "term_dumpdiff()" and "term_dumpload()".
5257 */
5258 static void
5259term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5260{
5261 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005262 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005263 char_u buf1[NUMBUFLEN];
5264 char_u buf2[NUMBUFLEN];
5265 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005266 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005267 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005268 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005269 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005270 char_u *textline = NULL;
5271
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005272 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005273 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005274 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005275 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005276 if (fname1 == NULL || (do_diff && fname2 == NULL))
5277 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005278 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005279 return;
5280 }
5281 fd1 = mch_fopen((char *)fname1, READBIN);
5282 if (fd1 == NULL)
5283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005284 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005285 return;
5286 }
5287 if (do_diff)
5288 {
5289 fd2 = mch_fopen((char *)fname2, READBIN);
5290 if (fd2 == NULL)
5291 {
5292 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005293 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005294 return;
5295 }
5296 }
5297
5298 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005299 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5300 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5301 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5302 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5303 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005304
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005305 if (opt.jo_term_name == NULL)
5306 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005307 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005308
Bram Moolenaar51e14382019-05-25 20:21:28 +02005309 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005310 if (fname_tofree != NULL)
5311 {
5312 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5313 opt.jo_term_name = fname_tofree;
5314 }
5315 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005316
Bram Moolenaar87abab92019-06-03 21:14:59 +02005317 if (opt.jo_bufnr_buf != NULL)
5318 {
5319 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5320
5321 // With "bufnr" argument: enter the window with this buffer and make it
5322 // empty.
5323 if (wp == NULL)
5324 semsg(_(e_invarg2), "bufnr");
5325 else
5326 {
5327 buf = curbuf;
5328 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005329 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005330 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005331 redraw_later(NOT_VALID);
5332 }
5333 }
5334 else
5335 // Create a new terminal window.
5336 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5337
Bram Moolenaard96ff162018-02-18 22:13:29 +01005338 if (buf != NULL && buf->b_term != NULL)
5339 {
5340 int i;
5341 linenr_T bot_lnum;
5342 linenr_T lnum;
5343 term_T *term = buf->b_term;
5344 int width;
5345 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005346 VTermPos cursor_pos1;
5347 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005348
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005349 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005350
Bram Moolenaard96ff162018-02-18 22:13:29 +01005351 rettv->vval.v_number = buf->b_fnum;
5352
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005353 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005354 width = read_dump_file(fd1, &cursor_pos1);
5355
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005356 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005357 if (cursor_pos1.row >= 0)
5358 {
5359 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5360 coladvance(cursor_pos1.col);
5361 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005362
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005363 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005364 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005365
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005366 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005367 if (!do_diff)
5368 goto theend;
5369
5370 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5371
Bram Moolenaar4a696342018-04-05 18:45:26 +02005372 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005373 if (textline == NULL)
5374 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005375 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5376 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5377 vim_free(textline);
5378
5379 textline = get_separator(width, fname2);
5380 if (textline == NULL)
5381 goto theend;
5382 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5383 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005384 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005385
5386 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005387 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005388 if (width2 > width)
5389 {
5390 vim_free(textline);
5391 textline = alloc(width2 + 1);
5392 if (textline == NULL)
5393 goto theend;
5394 width = width2;
5395 textline[width] = NUL;
5396 }
5397 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5398
5399 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5400 {
5401 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5402 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005403 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005404 for (i = 0; i < width; ++i)
5405 textline[i] = '-';
5406 }
5407 else
5408 {
5409 char_u *line1;
5410 char_u *line2;
5411 char_u *p1;
5412 char_u *p2;
5413 int col;
5414 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5415 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5416 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5417 ->sb_cells;
5418
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005419 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005420 line1 = vim_strsave(ml_get(lnum));
5421 if (line1 == NULL)
5422 break;
5423 p1 = line1;
5424
5425 line2 = ml_get(lnum + bot_lnum);
5426 p2 = line2;
5427 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5428 {
5429 int len1 = utfc_ptr2len(p1);
5430 int len2 = utfc_ptr2len(p2);
5431
5432 textline[col] = ' ';
5433 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005434 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005435 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005436 else if (lnum == cursor_pos1.row + 1
5437 && col == cursor_pos1.col
5438 && (cursor_pos1.row != cursor_pos2.row
5439 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005440 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005441 textline[col] = '>';
5442 else if (lnum == cursor_pos2.row + 1
5443 && col == cursor_pos2.col
5444 && (cursor_pos1.row != cursor_pos2.row
5445 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005446 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005447 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005448 else if (cellattr1 != NULL && cellattr2 != NULL)
5449 {
5450 if ((cellattr1 + col)->width
5451 != (cellattr2 + col)->width)
5452 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005453 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005454 &(cellattr2 + col)->fg))
5455 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005456 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005457 &(cellattr2 + col)->bg))
5458 textline[col] = 'b';
5459 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5460 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5461 textline[col] = 'a';
5462 }
5463 p1 += len1;
5464 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005465 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005466 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005467
5468 while (col < width)
5469 {
5470 if (*p1 == NUL && *p2 == NUL)
5471 textline[col] = '?';
5472 else if (*p1 == NUL)
5473 {
5474 textline[col] = '+';
5475 p2 += utfc_ptr2len(p2);
5476 }
5477 else
5478 {
5479 textline[col] = '-';
5480 p1 += utfc_ptr2len(p1);
5481 }
5482 ++col;
5483 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005484
5485 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005486 }
5487 if (add_empty_scrollback(term, &term->tl_default_color,
5488 term->tl_top_diff_rows) == OK)
5489 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5490 ++bot_lnum;
5491 }
5492
5493 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5494 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005495 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005496 for (i = 0; i < width; ++i)
5497 textline[i] = '+';
5498 if (add_empty_scrollback(term, &term->tl_default_color,
5499 term->tl_top_diff_rows) == OK)
5500 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5501 ++lnum;
5502 ++bot_lnum;
5503 }
5504
5505 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005506
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005507 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005508 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005509 }
5510
5511theend:
5512 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005513 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005514 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005515 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005516 fclose(fd2);
5517}
5518
5519/*
5520 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5521 * bottom files.
5522 * Return FAIL when this is not possible.
5523 */
5524 int
5525term_swap_diff()
5526{
5527 term_T *term = curbuf->b_term;
5528 linenr_T line_count;
5529 linenr_T top_rows;
5530 linenr_T bot_rows;
5531 linenr_T bot_start;
5532 linenr_T lnum;
5533 char_u *p;
5534 sb_line_T *sb_line;
5535
5536 if (term == NULL
5537 || !term_is_finished(curbuf)
5538 || term->tl_top_diff_rows == 0
5539 || term->tl_scrollback.ga_len == 0)
5540 return FAIL;
5541
5542 line_count = curbuf->b_ml.ml_line_count;
5543 top_rows = term->tl_top_diff_rows;
5544 bot_rows = term->tl_bot_diff_rows;
5545 bot_start = line_count - bot_rows;
5546 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5547
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005548 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005549 for (lnum = 1; lnum <= top_rows; ++lnum)
5550 {
5551 p = vim_strsave(ml_get(1));
5552 if (p == NULL)
5553 return OK;
5554 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005555 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005556 vim_free(p);
5557 }
5558
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005559 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005560 for (lnum = 1; lnum <= bot_rows; ++lnum)
5561 {
5562 p = vim_strsave(ml_get(bot_start + lnum));
5563 if (p == NULL)
5564 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005565 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005566 ml_append(lnum - 1, p, 0, FALSE);
5567 vim_free(p);
5568 }
5569
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005570 // move top title to bottom
5571 p = vim_strsave(ml_get(bot_rows + 1));
5572 if (p == NULL)
5573 return OK;
5574 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005575 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005576 vim_free(p);
5577
5578 // move bottom title to top
5579 p = vim_strsave(ml_get(line_count - top_rows));
5580 if (p == NULL)
5581 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005582 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005583 ml_append(bot_rows, p, 0, FALSE);
5584 vim_free(p);
5585
Bram Moolenaard96ff162018-02-18 22:13:29 +01005586 if (top_rows == bot_rows)
5587 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005588 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005589 for (lnum = 0; lnum < top_rows; ++lnum)
5590 {
5591 sb_line_T temp;
5592
5593 temp = *(sb_line + lnum);
5594 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5595 *(sb_line + bot_start + lnum) = temp;
5596 }
5597 }
5598 else
5599 {
5600 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005601 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005602
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005603 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005604 if (temp != NULL)
5605 {
5606 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5607 mch_memmove(term->tl_scrollback.ga_data,
5608 temp + bot_start,
5609 sizeof(sb_line_T) * bot_rows);
5610 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5611 temp + top_rows,
5612 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5613 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5614 + line_count - top_rows,
5615 temp,
5616 sizeof(sb_line_T) * top_rows);
5617 vim_free(temp);
5618 }
5619 }
5620
5621 term->tl_top_diff_rows = bot_rows;
5622 term->tl_bot_diff_rows = top_rows;
5623
5624 update_screen(NOT_VALID);
5625 return OK;
5626}
5627
5628/*
5629 * "term_dumpdiff(filename, filename, options)" function
5630 */
5631 void
5632f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5633{
5634 term_load_dump(argvars, rettv, TRUE);
5635}
5636
5637/*
5638 * "term_dumpload(filename, options)" function
5639 */
5640 void
5641f_term_dumpload(typval_T *argvars, typval_T *rettv)
5642{
5643 term_load_dump(argvars, rettv, FALSE);
5644}
5645
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005646/*
5647 * "term_getaltscreen(buf)" function
5648 */
5649 void
5650f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5651{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005652 buf_T *buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005653
5654 if (buf == NULL)
5655 return;
5656 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5657}
5658
5659/*
5660 * "term_getattr(attr, name)" function
5661 */
5662 void
5663f_term_getattr(typval_T *argvars, typval_T *rettv)
5664{
5665 int attr;
5666 size_t i;
5667 char_u *name;
5668
5669 static struct {
5670 char *name;
5671 int attr;
5672 } attrs[] = {
5673 {"bold", HL_BOLD},
5674 {"italic", HL_ITALIC},
5675 {"underline", HL_UNDERLINE},
5676 {"strike", HL_STRIKETHROUGH},
5677 {"reverse", HL_INVERSE},
5678 };
5679
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005680 attr = tv_get_number(&argvars[0]);
5681 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005682 if (name == NULL)
5683 return;
5684
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005685 if (attr > HL_ALL)
5686 attr = syn_attr2attr(attr);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005687 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
5688 if (STRCMP(name, attrs[i].name) == 0)
5689 {
5690 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5691 break;
5692 }
5693}
5694
5695/*
5696 * "term_getcursor(buf)" function
5697 */
5698 void
5699f_term_getcursor(typval_T *argvars, typval_T *rettv)
5700{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005701 buf_T *buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005702 term_T *term;
5703 list_T *l;
5704 dict_T *d;
5705
5706 if (rettv_list_alloc(rettv) == FAIL)
5707 return;
5708 if (buf == NULL)
5709 return;
5710 term = buf->b_term;
5711
5712 l = rettv->vval.v_list;
5713 list_append_number(l, term->tl_cursor_pos.row + 1);
5714 list_append_number(l, term->tl_cursor_pos.col + 1);
5715
5716 d = dict_alloc();
5717 if (d != NULL)
5718 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005719 dict_add_number(d, "visible", term->tl_cursor_visible);
5720 dict_add_number(d, "blink", blink_state_is_inverted()
5721 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5722 dict_add_number(d, "shape", term->tl_cursor_shape);
5723 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005724 list_append_dict(l, d);
5725 }
5726}
5727
5728/*
5729 * "term_getjob(buf)" function
5730 */
5731 void
5732f_term_getjob(typval_T *argvars, typval_T *rettv)
5733{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005734 buf_T *buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005735
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005736 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005737 {
5738 rettv->v_type = VAR_SPECIAL;
5739 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005740 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005741 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005742
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005743 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005744 rettv->vval.v_job = buf->b_term->tl_job;
5745 if (rettv->vval.v_job != NULL)
5746 ++rettv->vval.v_job->jv_refcount;
5747}
5748
5749 static int
5750get_row_number(typval_T *tv, term_T *term)
5751{
5752 if (tv->v_type == VAR_STRING
5753 && tv->vval.v_string != NULL
5754 && STRCMP(tv->vval.v_string, ".") == 0)
5755 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005756 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005757}
5758
5759/*
5760 * "term_getline(buf, row)" function
5761 */
5762 void
5763f_term_getline(typval_T *argvars, typval_T *rettv)
5764{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005765 buf_T *buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005766 term_T *term;
5767 int row;
5768
5769 rettv->v_type = VAR_STRING;
5770 if (buf == NULL)
5771 return;
5772 term = buf->b_term;
5773 row = get_row_number(&argvars[1], term);
5774
5775 if (term->tl_vterm == NULL)
5776 {
5777 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5778
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005779 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005780 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5781 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5782 }
5783 else
5784 {
5785 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5786 VTermRect rect;
5787 int len;
5788 char_u *p;
5789
5790 if (row < 0 || row >= term->tl_rows)
5791 return;
5792 len = term->tl_cols * MB_MAXBYTES + 1;
5793 p = alloc(len);
5794 if (p == NULL)
5795 return;
5796 rettv->vval.v_string = p;
5797
5798 rect.start_col = 0;
5799 rect.end_col = term->tl_cols;
5800 rect.start_row = row;
5801 rect.end_row = row + 1;
5802 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5803 }
5804}
5805
5806/*
5807 * "term_getscrolled(buf)" function
5808 */
5809 void
5810f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5811{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005812 buf_T *buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005813
5814 if (buf == NULL)
5815 return;
5816 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5817}
5818
5819/*
5820 * "term_getsize(buf)" function
5821 */
5822 void
5823f_term_getsize(typval_T *argvars, typval_T *rettv)
5824{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005825 buf_T *buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005826 list_T *l;
5827
5828 if (rettv_list_alloc(rettv) == FAIL)
5829 return;
5830 if (buf == NULL)
5831 return;
5832
5833 l = rettv->vval.v_list;
5834 list_append_number(l, buf->b_term->tl_rows);
5835 list_append_number(l, buf->b_term->tl_cols);
5836}
5837
5838/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005839 * "term_setsize(buf, rows, cols)" function
5840 */
5841 void
5842f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5843{
5844 buf_T *buf = term_get_buf(argvars, "term_setsize()");
5845 term_T *term;
5846 varnumber_T rows, cols;
5847
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005848 if (buf == NULL)
5849 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005850 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005851 return;
5852 }
5853 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005854 return;
5855 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005856 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005857 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005858 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005859 cols = cols <= 0 ? term->tl_cols : cols;
5860 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005861 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005862
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005863 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005864 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5865 term_report_winsize(term, term->tl_rows, term->tl_cols);
5866}
5867
5868/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005869 * "term_getstatus(buf)" function
5870 */
5871 void
5872f_term_getstatus(typval_T *argvars, typval_T *rettv)
5873{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005874 buf_T *buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005875 term_T *term;
5876 char_u val[100];
5877
5878 rettv->v_type = VAR_STRING;
5879 if (buf == NULL)
5880 return;
5881 term = buf->b_term;
5882
5883 if (term_job_running(term))
5884 STRCPY(val, "running");
5885 else
5886 STRCPY(val, "finished");
5887 if (term->tl_normal_mode)
5888 STRCAT(val, ",normal");
5889 rettv->vval.v_string = vim_strsave(val);
5890}
5891
5892/*
5893 * "term_gettitle(buf)" function
5894 */
5895 void
5896f_term_gettitle(typval_T *argvars, typval_T *rettv)
5897{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005898 buf_T *buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005899
5900 rettv->v_type = VAR_STRING;
5901 if (buf == NULL)
5902 return;
5903
5904 if (buf->b_term->tl_title != NULL)
5905 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5906}
5907
5908/*
5909 * "term_gettty(buf)" function
5910 */
5911 void
5912f_term_gettty(typval_T *argvars, typval_T *rettv)
5913{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005914 buf_T *buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005915 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005916 int num = 0;
5917
5918 rettv->v_type = VAR_STRING;
5919 if (buf == NULL)
5920 return;
5921 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02005922 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005923
5924 switch (num)
5925 {
5926 case 0:
5927 if (buf->b_term->tl_job != NULL)
5928 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005929 break;
5930 case 1:
5931 if (buf->b_term->tl_job != NULL)
5932 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005933 break;
5934 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005935 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005936 return;
5937 }
5938 if (p != NULL)
5939 rettv->vval.v_string = vim_strsave(p);
5940}
5941
5942/*
5943 * "term_list()" function
5944 */
5945 void
5946f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
5947{
5948 term_T *tp;
5949 list_T *l;
5950
5951 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
5952 return;
5953
5954 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005955 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02005956 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005957 if (list_append_number(l,
5958 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
5959 return;
5960}
5961
5962/*
5963 * "term_scrape(buf, row)" function
5964 */
5965 void
5966f_term_scrape(typval_T *argvars, typval_T *rettv)
5967{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005968 buf_T *buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005969 VTermScreen *screen = NULL;
5970 VTermPos pos;
5971 list_T *l;
5972 term_T *term;
5973 char_u *p;
5974 sb_line_T *line;
5975
5976 if (rettv_list_alloc(rettv) == FAIL)
5977 return;
5978 if (buf == NULL)
5979 return;
5980 term = buf->b_term;
5981
5982 l = rettv->vval.v_list;
5983 pos.row = get_row_number(&argvars[1], term);
5984
5985 if (term->tl_vterm != NULL)
5986 {
5987 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01005988 if (screen == NULL) // can't really happen
5989 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005990 p = NULL;
5991 line = NULL;
5992 }
5993 else
5994 {
5995 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
5996
5997 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
5998 return;
5999 p = ml_get_buf(buf, lnum + 1, FALSE);
6000 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6001 }
6002
6003 for (pos.col = 0; pos.col < term->tl_cols; )
6004 {
6005 dict_T *dcell;
6006 int width;
6007 VTermScreenCellAttrs attrs;
6008 VTermColor fg, bg;
6009 char_u rgb[8];
6010 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6011 int off = 0;
6012 int i;
6013
6014 if (screen == NULL)
6015 {
6016 cellattr_T *cellattr;
6017 int len;
6018
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006019 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006020 if (pos.col >= line->sb_cols)
6021 break;
6022 cellattr = line->sb_cells + pos.col;
6023 width = cellattr->width;
6024 attrs = cellattr->attrs;
6025 fg = cellattr->fg;
6026 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006027 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006028 mch_memmove(mbs, p, len);
6029 mbs[len] = NUL;
6030 p += len;
6031 }
6032 else
6033 {
6034 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006035
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006036 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6037 break;
6038 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6039 {
6040 if (cell.chars[i] == 0)
6041 break;
6042 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6043 }
6044 mbs[off] = NUL;
6045 width = cell.width;
6046 attrs = cell.attrs;
6047 fg = cell.fg;
6048 bg = cell.bg;
6049 }
6050 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006051 if (dcell == NULL)
6052 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006053 list_append_dict(l, dcell);
6054
Bram Moolenaare0be1672018-07-08 16:50:37 +02006055 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006056
6057 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6058 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006059 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006060 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6061 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006062 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006063
Bram Moolenaar83d47902020-03-26 20:34:00 +01006064 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006065 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006066
6067 ++pos.col;
6068 if (width == 2)
6069 ++pos.col;
6070 }
6071}
6072
6073/*
6074 * "term_sendkeys(buf, keys)" function
6075 */
6076 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006077f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006078{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006079 buf_T *buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006080 char_u *msg;
6081 term_T *term;
6082
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006083 if (buf == NULL)
6084 return;
6085
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006086 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006087 if (msg == NULL)
6088 return;
6089 term = buf->b_term;
6090 if (term->tl_vterm == NULL)
6091 return;
6092
6093 while (*msg != NUL)
6094 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006095 int c;
6096
6097 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6098 {
6099 c = TO_SPECIAL(msg[1], msg[2]);
6100 msg += 3;
6101 }
6102 else
6103 {
6104 c = PTR2CHAR(msg);
6105 msg += MB_CPTR2LEN(msg);
6106 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006107 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006108 }
6109}
6110
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006111#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6112/*
6113 * "term_getansicolors(buf)" function
6114 */
6115 void
6116f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6117{
6118 buf_T *buf = term_get_buf(argvars, "term_getansicolors()");
6119 term_T *term;
6120 VTermState *state;
6121 VTermColor color;
6122 char_u hexbuf[10];
6123 int index;
6124 list_T *list;
6125
6126 if (rettv_list_alloc(rettv) == FAIL)
6127 return;
6128
6129 if (buf == NULL)
6130 return;
6131 term = buf->b_term;
6132 if (term->tl_vterm == NULL)
6133 return;
6134
6135 list = rettv->vval.v_list;
6136 state = vterm_obtain_state(term->tl_vterm);
6137 for (index = 0; index < 16; index++)
6138 {
6139 vterm_state_get_palette_color(state, index, &color);
6140 sprintf((char *)hexbuf, "#%02x%02x%02x",
6141 color.red, color.green, color.blue);
6142 if (list_append_string(list, hexbuf, 7) == FAIL)
6143 return;
6144 }
6145}
6146
6147/*
6148 * "term_setansicolors(buf, list)" function
6149 */
6150 void
6151f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6152{
6153 buf_T *buf = term_get_buf(argvars, "term_setansicolors()");
6154 term_T *term;
6155
6156 if (buf == NULL)
6157 return;
6158 term = buf->b_term;
6159 if (term->tl_vterm == NULL)
6160 return;
6161
6162 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6163 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006164 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006165 return;
6166 }
6167
6168 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006169 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006170}
6171#endif
6172
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006173/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006174 * "term_setapi(buf, api)" function
6175 */
6176 void
6177f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6178{
6179 buf_T *buf = term_get_buf(argvars, "term_setapi()");
6180 term_T *term;
6181 char_u *api;
6182
6183 if (buf == NULL)
6184 return;
6185 term = buf->b_term;
6186 vim_free(term->tl_api);
6187 api = tv_get_string_chk(&argvars[1]);
6188 if (api != NULL)
6189 term->tl_api = vim_strsave(api);
6190 else
6191 term->tl_api = NULL;
6192}
6193
6194/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006195 * "term_setrestore(buf, command)" function
6196 */
6197 void
6198f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6199{
6200#if defined(FEAT_SESSION)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006201 buf_T *buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006202 term_T *term;
6203 char_u *cmd;
6204
6205 if (buf == NULL)
6206 return;
6207 term = buf->b_term;
6208 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006209 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006210 if (cmd != NULL)
6211 term->tl_command = vim_strsave(cmd);
6212 else
6213 term->tl_command = NULL;
6214#endif
6215}
6216
6217/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006218 * "term_setkill(buf, how)" function
6219 */
6220 void
6221f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6222{
6223 buf_T *buf = term_get_buf(argvars, "term_setkill()");
6224 term_T *term;
6225 char_u *how;
6226
6227 if (buf == NULL)
6228 return;
6229 term = buf->b_term;
6230 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006231 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006232 if (how != NULL)
6233 term->tl_kill = vim_strsave(how);
6234 else
6235 term->tl_kill = NULL;
6236}
6237
6238/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006239 * "term_start(command, options)" function
6240 */
6241 void
6242f_term_start(typval_T *argvars, typval_T *rettv)
6243{
6244 jobopt_T opt;
6245 buf_T *buf;
6246
6247 init_job_options(&opt);
6248 if (argvars[1].v_type != VAR_UNKNOWN
6249 && get_job_options(&argvars[1], &opt,
6250 JO_TIMEOUT_ALL + JO_STOPONEXIT
6251 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6252 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6253 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6254 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006255 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006256 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006257 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006258 return;
6259
Bram Moolenaar13568252018-03-16 20:46:58 +01006260 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006261
6262 if (buf != NULL && buf->b_term != NULL)
6263 rettv->vval.v_number = buf->b_fnum;
6264}
6265
6266/*
6267 * "term_wait" function
6268 */
6269 void
6270f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6271{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006272 buf_T *buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006273
6274 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006275 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006276 if (buf->b_term->tl_job == NULL)
6277 {
6278 ch_log(NULL, "term_wait(): no job to wait for");
6279 return;
6280 }
6281 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006282 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006283 return;
6284
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006285 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006286 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006287 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6288 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006289 // The job is dead, keep reading channel I/O until the channel is
6290 // closed. buf->b_term may become NULL if the terminal was closed while
6291 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006292 ch_log(NULL, "term_wait(): waiting for channel to close");
6293 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6294 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006295 term_flush_messages();
6296
Bram Moolenaard45aa552018-05-21 22:50:29 +02006297 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006298 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006299 // If the terminal is closed when the channel is closed the
6300 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006301 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006302 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006303
6304 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006305 }
6306 else
6307 {
6308 long wait = 10L;
6309
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006310 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006311
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006312 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006313 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006314 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006315 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006316
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006317 // Flushing messages on channels is hopefully sufficient.
6318 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006319 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006320 }
6321}
6322
6323/*
6324 * Called when a channel has sent all the lines to a terminal.
6325 * Send a CTRL-D to mark the end of the text.
6326 */
6327 void
6328term_send_eof(channel_T *ch)
6329{
6330 term_T *term;
6331
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006332 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006333 if (term->tl_job == ch->ch_job)
6334 {
6335 if (term->tl_eof_chars != NULL)
6336 {
6337 channel_send(ch, PART_IN, term->tl_eof_chars,
6338 (int)STRLEN(term->tl_eof_chars), NULL);
6339 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6340 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006341# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006342 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006343 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006344 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6345# endif
6346 }
6347}
6348
Bram Moolenaar113e1072019-01-20 15:30:40 +01006349#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006350 job_T *
6351term_getjob(term_T *term)
6352{
6353 return term != NULL ? term->tl_job : NULL;
6354}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006355#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006356
Bram Moolenaar4f974752019-02-17 17:44:42 +01006357# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006358
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006359///////////////////////////////////////
6360// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006361#ifdef PROTO
6362typedef int COORD;
6363typedef int DWORD;
6364typedef int HANDLE;
6365typedef int *DWORD_PTR;
6366typedef int HPCON;
6367typedef int HRESULT;
6368typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006369typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006370typedef int PSIZE_T;
6371typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006372typedef int BOOL;
6373# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006374#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006375
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006376HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6377HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6378HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006379BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6380BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6381void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006382
6383 static int
6384dyn_conpty_init(int verbose)
6385{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006386 static HMODULE hKerneldll = NULL;
6387 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006388 static struct
6389 {
6390 char *name;
6391 FARPROC *ptr;
6392 } conpty_entry[] =
6393 {
6394 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6395 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6396 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6397 {"InitializeProcThreadAttributeList",
6398 (FARPROC*)&pInitializeProcThreadAttributeList},
6399 {"UpdateProcThreadAttribute",
6400 (FARPROC*)&pUpdateProcThreadAttribute},
6401 {"DeleteProcThreadAttributeList",
6402 (FARPROC*)&pDeleteProcThreadAttributeList},
6403 {NULL, NULL}
6404 };
6405
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006406 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006407 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006408 if (verbose)
6409 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006410 return FAIL;
6411 }
6412
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006413 // No need to initialize twice.
6414 if (hKerneldll)
6415 return OK;
6416
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006417 hKerneldll = vimLoadLib("kernel32.dll");
6418 for (i = 0; conpty_entry[i].name != NULL
6419 && conpty_entry[i].ptr != NULL; ++i)
6420 {
6421 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6422 conpty_entry[i].name)) == NULL)
6423 {
6424 if (verbose)
6425 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006426 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006427 return FAIL;
6428 }
6429 }
6430
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006431 return OK;
6432}
6433
6434 static int
6435conpty_term_and_job_init(
6436 term_T *term,
6437 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006438 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006439 jobopt_T *opt,
6440 jobopt_T *orig_opt)
6441{
6442 WCHAR *cmd_wchar = NULL;
6443 WCHAR *cmd_wchar_copy = NULL;
6444 WCHAR *cwd_wchar = NULL;
6445 WCHAR *env_wchar = NULL;
6446 channel_T *channel = NULL;
6447 job_T *job = NULL;
6448 HANDLE jo = NULL;
6449 garray_T ga_cmd, ga_env;
6450 char_u *cmd = NULL;
6451 HRESULT hr;
6452 COORD consize;
6453 SIZE_T breq;
6454 PROCESS_INFORMATION proc_info;
6455 HANDLE i_theirs = NULL;
6456 HANDLE o_theirs = NULL;
6457 HANDLE i_ours = NULL;
6458 HANDLE o_ours = NULL;
6459
6460 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6461 ga_init2(&ga_env, (int)sizeof(char*), 20);
6462
6463 if (argvar->v_type == VAR_STRING)
6464 {
6465 cmd = argvar->vval.v_string;
6466 }
6467 else if (argvar->v_type == VAR_LIST)
6468 {
6469 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6470 goto failed;
6471 cmd = ga_cmd.ga_data;
6472 }
6473 if (cmd == NULL || *cmd == NUL)
6474 {
6475 emsg(_(e_invarg));
6476 goto failed;
6477 }
6478
6479 term->tl_arg0_cmd = vim_strsave(cmd);
6480
6481 cmd_wchar = enc_to_utf16(cmd, NULL);
6482
6483 if (cmd_wchar != NULL)
6484 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006485 // Request by CreateProcessW
6486 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006487 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006488 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6489 }
6490
6491 ga_clear(&ga_cmd);
6492 if (cmd_wchar == NULL)
6493 goto failed;
6494 if (opt->jo_cwd != NULL)
6495 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6496
6497 win32_build_env(opt->jo_env, &ga_env, TRUE);
6498 env_wchar = ga_env.ga_data;
6499
6500 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6501 goto failed;
6502 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6503 goto failed;
6504
6505 consize.X = term->tl_cols;
6506 consize.Y = term->tl_rows;
6507 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6508 &term->tl_conpty);
6509 if (FAILED(hr))
6510 goto failed;
6511
6512 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6513
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006514 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006515 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006516 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006517 if (!term->tl_siex.lpAttributeList)
6518 goto failed;
6519 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6520 0, &breq))
6521 goto failed;
6522 if (!pUpdateProcThreadAttribute(
6523 term->tl_siex.lpAttributeList, 0,
6524 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6525 sizeof(HPCON), NULL, NULL))
6526 goto failed;
6527
6528 channel = add_channel();
6529 if (channel == NULL)
6530 goto failed;
6531
6532 job = job_alloc();
6533 if (job == NULL)
6534 goto failed;
6535 if (argvar->v_type == VAR_STRING)
6536 {
6537 int argc;
6538
6539 build_argv_from_string(cmd, &job->jv_argv, &argc);
6540 }
6541 else
6542 {
6543 int argc;
6544
6545 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6546 }
6547
6548 if (opt->jo_set & JO_IN_BUF)
6549 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6550
6551 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6552 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006553 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006554 env_wchar, cwd_wchar,
6555 &term->tl_siex.StartupInfo, &proc_info))
6556 goto failed;
6557
6558 CloseHandle(i_theirs);
6559 CloseHandle(o_theirs);
6560
6561 channel_set_pipes(channel,
6562 (sock_T)i_ours,
6563 (sock_T)o_ours,
6564 (sock_T)o_ours);
6565
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006566 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006567 channel->ch_write_text_mode = TRUE;
6568
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006569 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006570 channel->ch_anonymous_pipe = TRUE;
6571
6572 jo = CreateJobObject(NULL, NULL);
6573 if (jo == NULL)
6574 goto failed;
6575
6576 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6577 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006578 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006579 CloseHandle(jo);
6580 jo = NULL;
6581 }
6582
6583 ResumeThread(proc_info.hThread);
6584 CloseHandle(proc_info.hThread);
6585
6586 vim_free(cmd_wchar);
6587 vim_free(cmd_wchar_copy);
6588 vim_free(cwd_wchar);
6589 vim_free(env_wchar);
6590
6591 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6592 goto failed;
6593
6594#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6595 if (opt->jo_set2 & JO2_ANSI_COLORS)
6596 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6597 else
6598 init_vterm_ansi_colors(term->tl_vterm);
6599#endif
6600
6601 channel_set_job(channel, job, opt);
6602 job_set_options(job, opt);
6603
6604 job->jv_channel = channel;
6605 job->jv_proc_info = proc_info;
6606 job->jv_job_object = jo;
6607 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006608 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006609 ++job->jv_refcount;
6610 term->tl_job = job;
6611
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006612 // Redirecting stdout and stderr doesn't work at the job level. Instead
6613 // open the file here and handle it in. opt->jo_io was changed in
6614 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006615 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6616 {
6617 char_u *fname = opt->jo_io_name[PART_OUT];
6618
6619 ch_log(channel, "Opening output file %s", fname);
6620 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6621 if (term->tl_out_fd == NULL)
6622 semsg(_(e_notopen), fname);
6623 }
6624
6625 return OK;
6626
6627failed:
6628 ga_clear(&ga_cmd);
6629 ga_clear(&ga_env);
6630 vim_free(cmd_wchar);
6631 vim_free(cmd_wchar_copy);
6632 vim_free(cwd_wchar);
6633 if (channel != NULL)
6634 channel_clear(channel);
6635 if (job != NULL)
6636 {
6637 job->jv_channel = NULL;
6638 job_cleanup(job);
6639 }
6640 term->tl_job = NULL;
6641 if (jo != NULL)
6642 CloseHandle(jo);
6643
6644 if (term->tl_siex.lpAttributeList != NULL)
6645 {
6646 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6647 vim_free(term->tl_siex.lpAttributeList);
6648 }
6649 term->tl_siex.lpAttributeList = NULL;
6650 if (o_theirs != NULL)
6651 CloseHandle(o_theirs);
6652 if (o_ours != NULL)
6653 CloseHandle(o_ours);
6654 if (i_ours != NULL)
6655 CloseHandle(i_ours);
6656 if (i_theirs != NULL)
6657 CloseHandle(i_theirs);
6658 if (term->tl_conpty != NULL)
6659 pClosePseudoConsole(term->tl_conpty);
6660 term->tl_conpty = NULL;
6661 return FAIL;
6662}
6663
6664 static void
6665conpty_term_report_winsize(term_T *term, int rows, int cols)
6666{
6667 COORD consize;
6668
6669 consize.X = cols;
6670 consize.Y = rows;
6671 pResizePseudoConsole(term->tl_conpty, consize);
6672}
6673
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006674 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006675term_free_conpty(term_T *term)
6676{
6677 if (term->tl_siex.lpAttributeList != NULL)
6678 {
6679 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6680 vim_free(term->tl_siex.lpAttributeList);
6681 }
6682 term->tl_siex.lpAttributeList = NULL;
6683 if (term->tl_conpty != NULL)
6684 pClosePseudoConsole(term->tl_conpty);
6685 term->tl_conpty = NULL;
6686}
6687
6688 int
6689use_conpty(void)
6690{
6691 return has_conpty;
6692}
6693
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006694# ifndef PROTO
6695
6696#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6697#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006698#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006699
6700void* (*winpty_config_new)(UINT64, void*);
6701void* (*winpty_open)(void*, void*);
6702void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6703BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6704void (*winpty_config_set_mouse_mode)(void*, int);
6705void (*winpty_config_set_initial_size)(void*, int, int);
6706LPCWSTR (*winpty_conin_name)(void*);
6707LPCWSTR (*winpty_conout_name)(void*);
6708LPCWSTR (*winpty_conerr_name)(void*);
6709void (*winpty_free)(void*);
6710void (*winpty_config_free)(void*);
6711void (*winpty_spawn_config_free)(void*);
6712void (*winpty_error_free)(void*);
6713LPCWSTR (*winpty_error_msg)(void*);
6714BOOL (*winpty_set_size)(void*, int, int, void*);
6715HANDLE (*winpty_agent_process)(void*);
6716
6717#define WINPTY_DLL "winpty.dll"
6718
6719static HINSTANCE hWinPtyDLL = NULL;
6720# endif
6721
6722 static int
6723dyn_winpty_init(int verbose)
6724{
6725 int i;
6726 static struct
6727 {
6728 char *name;
6729 FARPROC *ptr;
6730 } winpty_entry[] =
6731 {
6732 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6733 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6734 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6735 {"winpty_config_set_mouse_mode",
6736 (FARPROC*)&winpty_config_set_mouse_mode},
6737 {"winpty_config_set_initial_size",
6738 (FARPROC*)&winpty_config_set_initial_size},
6739 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6740 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6741 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6742 {"winpty_free", (FARPROC*)&winpty_free},
6743 {"winpty_open", (FARPROC*)&winpty_open},
6744 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6745 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6746 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6747 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6748 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6749 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6750 {NULL, NULL}
6751 };
6752
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006753 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006754 if (hWinPtyDLL)
6755 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006756 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6757 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006758 if (*p_winptydll != NUL)
6759 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6760 if (!hWinPtyDLL)
6761 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6762 if (!hWinPtyDLL)
6763 {
6764 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006765 semsg(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006766 : (char_u *)WINPTY_DLL);
6767 return FAIL;
6768 }
6769 for (i = 0; winpty_entry[i].name != NULL
6770 && winpty_entry[i].ptr != NULL; ++i)
6771 {
6772 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6773 winpty_entry[i].name)) == NULL)
6774 {
6775 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006776 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006777 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006778 return FAIL;
6779 }
6780 }
6781
6782 return OK;
6783}
6784
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006785 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006786winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006787 term_T *term,
6788 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006789 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006790 jobopt_T *opt,
6791 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006792{
6793 WCHAR *cmd_wchar = NULL;
6794 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006795 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006796 channel_T *channel = NULL;
6797 job_T *job = NULL;
6798 DWORD error;
6799 HANDLE jo = NULL;
6800 HANDLE child_process_handle;
6801 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006802 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006803 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006804 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006805 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006806
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006807 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6808 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006809
6810 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006811 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006812 cmd = argvar->vval.v_string;
6813 }
6814 else if (argvar->v_type == VAR_LIST)
6815 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006816 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006817 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006818 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006819 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006820 if (cmd == NULL || *cmd == NUL)
6821 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006822 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006823 goto failed;
6824 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006825
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006826 term->tl_arg0_cmd = vim_strsave(cmd);
6827
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006828 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006829 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006830 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006831 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006832 if (opt->jo_cwd != NULL)
6833 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006834
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006835 win32_build_env(opt->jo_env, &ga_env, TRUE);
6836 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006837
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006838 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6839 if (term->tl_winpty_config == NULL)
6840 goto failed;
6841
6842 winpty_config_set_mouse_mode(term->tl_winpty_config,
6843 WINPTY_MOUSE_MODE_FORCE);
6844 winpty_config_set_initial_size(term->tl_winpty_config,
6845 term->tl_cols, term->tl_rows);
6846 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6847 if (term->tl_winpty == NULL)
6848 goto failed;
6849
6850 spawn_config = winpty_spawn_config_new(
6851 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6852 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6853 NULL,
6854 cmd_wchar,
6855 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006856 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006857 &winpty_err);
6858 if (spawn_config == NULL)
6859 goto failed;
6860
6861 channel = add_channel();
6862 if (channel == NULL)
6863 goto failed;
6864
6865 job = job_alloc();
6866 if (job == NULL)
6867 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006868 if (argvar->v_type == VAR_STRING)
6869 {
6870 int argc;
6871
6872 build_argv_from_string(cmd, &job->jv_argv, &argc);
6873 }
6874 else
6875 {
6876 int argc;
6877
6878 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6879 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006880
6881 if (opt->jo_set & JO_IN_BUF)
6882 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6883
6884 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
6885 &child_thread_handle, &error, &winpty_err))
6886 goto failed;
6887
6888 channel_set_pipes(channel,
6889 (sock_T)CreateFileW(
6890 winpty_conin_name(term->tl_winpty),
6891 GENERIC_WRITE, 0, NULL,
6892 OPEN_EXISTING, 0, NULL),
6893 (sock_T)CreateFileW(
6894 winpty_conout_name(term->tl_winpty),
6895 GENERIC_READ, 0, NULL,
6896 OPEN_EXISTING, 0, NULL),
6897 (sock_T)CreateFileW(
6898 winpty_conerr_name(term->tl_winpty),
6899 GENERIC_READ, 0, NULL,
6900 OPEN_EXISTING, 0, NULL));
6901
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006902 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006903 channel->ch_write_text_mode = TRUE;
6904
6905 jo = CreateJobObject(NULL, NULL);
6906 if (jo == NULL)
6907 goto failed;
6908
6909 if (!AssignProcessToJobObject(jo, child_process_handle))
6910 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006911 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006912 CloseHandle(jo);
6913 jo = NULL;
6914 }
6915
6916 winpty_spawn_config_free(spawn_config);
6917 vim_free(cmd_wchar);
6918 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006919 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006920
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006921 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6922 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006923
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006924#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6925 if (opt->jo_set2 & JO2_ANSI_COLORS)
6926 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6927 else
6928 init_vterm_ansi_colors(term->tl_vterm);
6929#endif
6930
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006931 channel_set_job(channel, job, opt);
6932 job_set_options(job, opt);
6933
6934 job->jv_channel = channel;
6935 job->jv_proc_info.hProcess = child_process_handle;
6936 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
6937 job->jv_job_object = jo;
6938 job->jv_status = JOB_STARTED;
6939 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006940 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006941 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006942 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006943 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006944 ++job->jv_refcount;
6945 term->tl_job = job;
6946
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006947 // Redirecting stdout and stderr doesn't work at the job level. Instead
6948 // open the file here and handle it in. opt->jo_io was changed in
6949 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006950 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6951 {
6952 char_u *fname = opt->jo_io_name[PART_OUT];
6953
6954 ch_log(channel, "Opening output file %s", fname);
6955 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6956 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006957 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006958 }
6959
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006960 return OK;
6961
6962failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006963 ga_clear(&ga_cmd);
6964 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006965 vim_free(cmd_wchar);
6966 vim_free(cwd_wchar);
6967 if (spawn_config != NULL)
6968 winpty_spawn_config_free(spawn_config);
6969 if (channel != NULL)
6970 channel_clear(channel);
6971 if (job != NULL)
6972 {
6973 job->jv_channel = NULL;
6974 job_cleanup(job);
6975 }
6976 term->tl_job = NULL;
6977 if (jo != NULL)
6978 CloseHandle(jo);
6979 if (term->tl_winpty != NULL)
6980 winpty_free(term->tl_winpty);
6981 term->tl_winpty = NULL;
6982 if (term->tl_winpty_config != NULL)
6983 winpty_config_free(term->tl_winpty_config);
6984 term->tl_winpty_config = NULL;
6985 if (winpty_err != NULL)
6986 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006987 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006988 (short_u *)winpty_error_msg(winpty_err), NULL);
6989
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006990 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006991 winpty_error_free(winpty_err);
6992 }
6993 return FAIL;
6994}
6995
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006996/*
6997 * Create a new terminal of "rows" by "cols" cells.
6998 * Store a reference in "term".
6999 * Return OK or FAIL.
7000 */
7001 static int
7002term_and_job_init(
7003 term_T *term,
7004 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007005 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007006 jobopt_T *opt,
7007 jobopt_T *orig_opt)
7008{
7009 int use_winpty = FALSE;
7010 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007011 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007012
7013 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7014 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7015
7016 if (!has_winpty && !has_conpty)
7017 // If neither is available give the errors for winpty, since when
7018 // conpty is not available it can't be installed either.
7019 return dyn_winpty_init(TRUE);
7020
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007021 if (opt->jo_tty_type != NUL)
7022 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007023
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007024 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007025 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007026 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007027 use_conpty = TRUE;
7028 else if (has_winpty)
7029 use_winpty = TRUE;
7030 // else: error
7031 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007032 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007033 {
7034 if (has_winpty)
7035 use_winpty = TRUE;
7036 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007037 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007038 {
7039 if (has_conpty)
7040 use_conpty = TRUE;
7041 else
7042 return dyn_conpty_init(TRUE);
7043 }
7044
7045 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007046 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007047
7048 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007049 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007050
7051 // error
7052 return dyn_winpty_init(TRUE);
7053}
7054
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007055 static int
7056create_pty_only(term_T *term, jobopt_T *options)
7057{
7058 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7059 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7060 char in_name[80], out_name[80];
7061 channel_T *channel = NULL;
7062
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007063 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7064 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007065
7066 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7067 GetCurrentProcessId(),
7068 curbuf->b_fnum);
7069 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7070 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7071 PIPE_UNLIMITED_INSTANCES,
7072 0, 0, NMPWAIT_NOWAIT, NULL);
7073 if (hPipeIn == INVALID_HANDLE_VALUE)
7074 goto failed;
7075
7076 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7077 GetCurrentProcessId(),
7078 curbuf->b_fnum);
7079 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7080 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7081 PIPE_UNLIMITED_INSTANCES,
7082 0, 0, 0, NULL);
7083 if (hPipeOut == INVALID_HANDLE_VALUE)
7084 goto failed;
7085
7086 ConnectNamedPipe(hPipeIn, NULL);
7087 ConnectNamedPipe(hPipeOut, NULL);
7088
7089 term->tl_job = job_alloc();
7090 if (term->tl_job == NULL)
7091 goto failed;
7092 ++term->tl_job->jv_refcount;
7093
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007094 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007095 term->tl_job->jv_status = JOB_FINISHED;
7096
7097 channel = add_channel();
7098 if (channel == NULL)
7099 goto failed;
7100 term->tl_job->jv_channel = channel;
7101 channel->ch_keep_open = TRUE;
7102 channel->ch_named_pipe = TRUE;
7103
7104 channel_set_pipes(channel,
7105 (sock_T)hPipeIn,
7106 (sock_T)hPipeOut,
7107 (sock_T)hPipeOut);
7108 channel_set_job(channel, term->tl_job, options);
7109 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7110 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7111
7112 return OK;
7113
7114failed:
7115 if (hPipeIn != NULL)
7116 CloseHandle(hPipeIn);
7117 if (hPipeOut != NULL)
7118 CloseHandle(hPipeOut);
7119 return FAIL;
7120}
7121
7122/*
7123 * Free the terminal emulator part of "term".
7124 */
7125 static void
7126term_free_vterm(term_T *term)
7127{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007128 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007129 if (term->tl_winpty != NULL)
7130 winpty_free(term->tl_winpty);
7131 term->tl_winpty = NULL;
7132 if (term->tl_winpty_config != NULL)
7133 winpty_config_free(term->tl_winpty_config);
7134 term->tl_winpty_config = NULL;
7135 if (term->tl_vterm != NULL)
7136 vterm_free(term->tl_vterm);
7137 term->tl_vterm = NULL;
7138}
7139
7140/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007141 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007142 */
7143 static void
7144term_report_winsize(term_T *term, int rows, int cols)
7145{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007146 if (term->tl_conpty)
7147 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007148 if (term->tl_winpty)
7149 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7150}
7151
7152 int
7153terminal_enabled(void)
7154{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007155 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007156}
7157
7158# else
7159
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007160///////////////////////////////////////
7161// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007162
7163/*
7164 * Create a new terminal of "rows" by "cols" cells.
7165 * Start job for "cmd".
7166 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007167 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007168 * Return OK or FAIL.
7169 */
7170 static int
7171term_and_job_init(
7172 term_T *term,
7173 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007174 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007175 jobopt_T *opt,
7176 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007177{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007178 term->tl_arg0_cmd = NULL;
7179
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007180 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7181 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007182
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007183#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7184 if (opt->jo_set2 & JO2_ANSI_COLORS)
7185 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7186 else
7187 init_vterm_ansi_colors(term->tl_vterm);
7188#endif
7189
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007190 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007191 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007192 if (term->tl_job != NULL)
7193 ++term->tl_job->jv_refcount;
7194
7195 return term->tl_job != NULL
7196 && term->tl_job->jv_channel != NULL
7197 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7198}
7199
7200 static int
7201create_pty_only(term_T *term, jobopt_T *opt)
7202{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007203 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7204 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007205
7206 term->tl_job = job_alloc();
7207 if (term->tl_job == NULL)
7208 return FAIL;
7209 ++term->tl_job->jv_refcount;
7210
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007211 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007212 term->tl_job->jv_status = JOB_FINISHED;
7213
7214 return mch_create_pty_channel(term->tl_job, opt);
7215}
7216
7217/*
7218 * Free the terminal emulator part of "term".
7219 */
7220 static void
7221term_free_vterm(term_T *term)
7222{
7223 if (term->tl_vterm != NULL)
7224 vterm_free(term->tl_vterm);
7225 term->tl_vterm = NULL;
7226}
7227
7228/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007229 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007230 */
7231 static void
7232term_report_winsize(term_T *term, int rows, int cols)
7233{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007234 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007235 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7236 {
7237 int fd = -1;
7238 int part;
7239
7240 for (part = PART_OUT; part < PART_COUNT; ++part)
7241 {
7242 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007243 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007244 break;
7245 }
7246 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7247 mch_signal_job(term->tl_job, (char_u *)"winch");
7248 }
7249}
7250
7251# endif
7252
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007253#endif // FEAT_TERMINAL