blob: 7f38bf75ca00cc13cd67567af8347d99de89ba9f [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 Moolenaara80faa82020-04-12 19:37:17 +0200476 CLEAR_FIELD(split_ea);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200477 if (opt->jo_curwin)
478 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100479 // Create a new buffer in the current window.
Bram Moolenaar13568252018-03-16 20:46:58 +0100480 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200481 {
482 no_write_message();
483 vim_free(term);
484 return NULL;
485 }
486 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaarb1009092020-05-31 16:04:42 +0200487 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
488 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
489 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200490 {
491 vim_free(term);
492 return NULL;
493 }
494 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100495 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200496 {
497 buf_T *buf;
498
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100499 // Create a new buffer without a window. Make it the current buffer for
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100500 // a moment to be able to do the initializations.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200501 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
502 BLN_NEW | BLN_LISTED);
503 if (buf == NULL || ml_open(buf) == FAIL)
504 {
505 vim_free(term);
506 return NULL;
507 }
508 old_curbuf = curbuf;
509 --curbuf->b_nwindows;
510 curbuf = buf;
511 curwin->w_buffer = buf;
512 ++curbuf->b_nwindows;
513 }
514 else
515 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100516 // Open a new window or tab.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200517 split_ea.cmdidx = CMD_new;
518 split_ea.cmd = (char_u *)"new";
519 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100520 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200521 {
522 split_ea.line2 = opt->jo_term_rows;
523 split_ea.addr_count = 1;
524 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100525 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200526 {
527 split_ea.line2 = opt->jo_term_cols;
528 split_ea.addr_count = 1;
529 }
530
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100531 if (vertical)
Bram Moolenaare1004402020-10-24 20:49:43 +0200532 cmdmod.cmod_split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200533 ex_splitview(&split_ea);
534 if (curwin == old_curwin)
535 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100536 // split failed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200537 vim_free(term);
538 return NULL;
539 }
540 }
541 term->tl_buffer = curbuf;
542 curbuf->b_term = term;
543
544 if (!opt->jo_hidden)
545 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100546 // Only one size was taken care of with :new, do the other one. With
547 // "curwin" both need to be done.
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100548 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200549 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100550 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200551 win_setwidth(opt->jo_term_cols);
552 }
553
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100554 // Link the new terminal in the list of active terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200555 term->tl_next = first_term;
556 first_term = term;
557
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100558 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
559
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200560 if (opt->jo_term_name != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100561 {
562 vim_free(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200563 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100564 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100565 else if (argv != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100566 {
567 vim_free(curbuf->b_ffname);
Bram Moolenaar13568252018-03-16 20:46:58 +0100568 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100569 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200570 else
571 {
572 int i;
573 size_t len;
574 char_u *cmd, *p;
575
576 if (argvar->v_type == VAR_STRING)
577 {
578 cmd = argvar->vval.v_string;
579 if (cmd == NULL)
580 cmd = (char_u *)"";
581 else if (STRCMP(cmd, "NONE") == 0)
582 cmd = (char_u *)"pty";
583 }
584 else if (argvar->v_type != VAR_LIST
585 || argvar->vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +0100586 || argvar->vval.v_list->lv_len == 0
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100587 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200588 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
589 cmd = (char_u*)"";
590
591 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200592 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200593
594 for (i = 0; p != NULL; ++i)
595 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100596 // Prepend a ! to the command name to avoid the buffer name equals
597 // the executable, otherwise ":w!" would overwrite it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200598 if (i == 0)
599 vim_snprintf((char *)p, len, "!%s", cmd);
600 else
601 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
602 if (buflist_findname(p) == NULL)
603 {
604 vim_free(curbuf->b_ffname);
605 curbuf->b_ffname = p;
606 break;
607 }
608 }
609 }
Bram Moolenaare010c722020-02-24 21:37:54 +0100610 vim_free(curbuf->b_sfname);
611 curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200612 curbuf->b_fname = curbuf->b_ffname;
613
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100614 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
615
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200616 if (opt->jo_term_opencmd != NULL)
617 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
618
619 if (opt->jo_eof_chars != NULL)
620 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
621
622 set_string_option_direct((char_u *)"buftype", -1,
623 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200624 // Avoid that 'buftype' is reset when this buffer is entered.
625 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200626
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100627 // Mark the buffer as not modifiable. It can only be made modifiable after
628 // the job finished.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200629 curbuf->b_p_ma = FALSE;
630
Bram Moolenaarb936b792020-09-04 18:34:09 +0200631 set_term_and_win_size(term, opt);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100632#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200633 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
634#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200635 setup_job_options(opt, term->tl_rows, term->tl_cols);
636
Bram Moolenaar13568252018-03-16 20:46:58 +0100637 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100638 return curbuf;
639
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100640#if defined(FEAT_SESSION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100641 // Remember the command for the session file.
Bram Moolenaar13568252018-03-16 20:46:58 +0100642 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100643 term->tl_command = vim_strsave((char_u *)"NONE");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100644 else if (argvar->v_type == VAR_STRING)
645 {
646 char_u *cmd = argvar->vval.v_string;
647
648 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
649 term->tl_command = vim_strsave(cmd);
650 }
651 else if (argvar->v_type == VAR_LIST
652 && argvar->vval.v_list != NULL
653 && argvar->vval.v_list->lv_len > 0)
654 {
655 garray_T ga;
656 listitem_T *item;
657
658 ga_init2(&ga, 1, 100);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200659 FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100660 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100661 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100662 char_u *p;
663
664 if (s == NULL)
665 break;
666 p = vim_strsave_fnameescape(s, FALSE);
667 if (p == NULL)
668 break;
669 ga_concat(&ga, p);
670 vim_free(p);
671 ga_append(&ga, ' ');
672 }
673 if (item == NULL)
674 {
675 ga_append(&ga, NUL);
676 term->tl_command = ga.ga_data;
677 }
678 else
679 ga_clear(&ga);
680 }
681#endif
682
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100683 if (opt->jo_term_kill != NULL)
684 {
685 char_u *p = skiptowhite(opt->jo_term_kill);
686
687 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
688 }
689
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200690 if (opt->jo_term_api != NULL)
Bram Moolenaar21109272020-01-30 16:27:20 +0100691 {
692 char_u *p = skiptowhite(opt->jo_term_api);
693
694 term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
695 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200696 else
697 term->tl_api = vim_strsave((char_u *)"Tapi_");
698
Bram Moolenaar83d47902020-03-26 20:34:00 +0100699 if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
700 term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
701
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100702 // System dependent: setup the vterm and maybe start the job in it.
Bram Moolenaar13568252018-03-16 20:46:58 +0100703 if (argv == NULL
704 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200705 && argvar->vval.v_string != NULL
706 && STRCMP(argvar->vval.v_string, "NONE") == 0)
707 res = create_pty_only(term, opt);
708 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200709 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200710
711 newbuf = curbuf;
712 if (res == OK)
713 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100714 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200715 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
716 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100717#ifdef FEAT_GUI
718 if (term->tl_system)
719 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100720 // display first line below typed command
Bram Moolenaar13568252018-03-16 20:46:58 +0100721 term->tl_toprow = msg_row + 1;
722 term->tl_dirty_row_end = 0;
723 }
724#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200725
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100726 // Make sure we don't get stuck on sending keys to the job, it leads to
727 // a deadlock if the job is waiting for Vim to read.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200728 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
729
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200730 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200731 {
732 --curbuf->b_nwindows;
733 curbuf = old_curbuf;
734 curwin->w_buffer = curbuf;
735 ++curbuf->b_nwindows;
736 }
737 }
738 else
739 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100740 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200741 return NULL;
742 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100743
Bram Moolenaar13568252018-03-16 20:46:58 +0100744 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar28ed4df2019-10-26 16:21:40 +0200745 if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
746 apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200747 return newbuf;
748}
749
750/*
751 * ":terminal": open a terminal window and execute a job in it.
752 */
753 void
754ex_terminal(exarg_T *eap)
755{
756 typval_T argvar[2];
757 jobopt_T opt;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100758 int opt_shell = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200759 char_u *cmd;
760 char_u *tofree = NULL;
761
762 init_job_options(&opt);
763
764 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100765 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200766 {
767 char_u *p, *ep;
768
769 cmd += 2;
770 p = skiptowhite(cmd);
771 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200772 if (ep != NULL)
773 {
774 if (ep < p)
775 p = ep;
776 else
777 ep = NULL;
778 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200779
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200780# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
781 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
782 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200783 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200784 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100785 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200786 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200787 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200788 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200789 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200790 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200791 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200792 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100793 opt.jo_term_norestore = 1;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100794 else if (OPTARG_HAS("shell"))
795 opt_shell = TRUE;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200796 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100797 {
798 opt.jo_set2 |= JO2_TERM_KILL;
799 opt.jo_term_kill = ep + 1;
800 p = skiptowhite(cmd);
801 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200802 else if (OPTARG_HAS("api"))
803 {
804 opt.jo_set2 |= JO2_TERM_API;
805 if (ep != NULL)
806 {
807 opt.jo_term_api = ep + 1;
808 p = skiptowhite(cmd);
809 }
810 else
811 opt.jo_term_api = NULL;
812 }
813 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200814 {
815 opt.jo_set2 |= JO2_TERM_ROWS;
816 opt.jo_term_rows = atoi((char *)ep + 1);
817 p = skiptowhite(cmd);
818 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200819 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200820 {
821 opt.jo_set2 |= JO2_TERM_COLS;
822 opt.jo_term_cols = atoi((char *)ep + 1);
823 p = skiptowhite(cmd);
824 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200825 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200826 {
827 char_u *buf = NULL;
828 char_u *keys;
829
Bram Moolenaar21109272020-01-30 16:27:20 +0100830 vim_free(opt.jo_eof_chars);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200831 p = skiptowhite(cmd);
832 *p = NUL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200833 keys = replace_termcodes(ep + 1, &buf,
834 REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200835 opt.jo_set2 |= JO2_EOF_CHARS;
836 opt.jo_eof_chars = vim_strsave(keys);
837 vim_free(buf);
838 *p = ' ';
839 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100840#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100841 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
842 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100843 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100844 int tty_type = NUL;
845
846 p = skiptowhite(cmd);
847 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
848 tty_type = 'w';
849 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
850 tty_type = 'c';
851 else
852 {
853 semsg(e_invargval, "type");
854 goto theend;
855 }
856 opt.jo_set2 |= JO2_TTY_TYPE;
857 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100858 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100859#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200860 else
861 {
862 if (*p)
863 *p = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100864 semsg(_("E181: Invalid attribute: %s"), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100865 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200866 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200867# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200868 cmd = skipwhite(p);
869 }
870 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100871 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100872 // Make a copy of 'shell', an autocommand may change the option.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200873 tofree = cmd = vim_strsave(p_sh);
874
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100875 // default to close when the shell exits
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100876 if (opt.jo_term_finish == NUL)
Bram Moolenaare2978022020-04-26 14:47:44 +0200877 opt.jo_term_finish = TL_FINISH_CLOSE;
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100878 }
879
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200880 if (eap->addr_count > 0)
881 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100882 // Write lines from current buffer to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200883 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
884 opt.jo_io[PART_IN] = JIO_BUFFER;
885 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
886 opt.jo_in_top = eap->line1;
887 opt.jo_in_bot = eap->line2;
888 }
889
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100890 if (opt_shell && tofree == NULL)
891 {
892#ifdef UNIX
893 char **argv = NULL;
894 char_u *tofree1 = NULL;
895 char_u *tofree2 = NULL;
896
897 // :term ++shell command
898 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
899 term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaaradf4aa22019-11-10 22:36:44 +0100900 vim_free(argv);
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100901 vim_free(tofree1);
902 vim_free(tofree2);
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100903 goto theend;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100904#else
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100905# ifdef MSWIN
906 long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
907 char_u *newcmd;
908
909 newcmd = alloc(cmdlen);
910 if (newcmd == NULL)
911 goto theend;
912 tofree = newcmd;
913 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
914 cmd = newcmd;
915# else
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100916 emsg(_("E279: Sorry, ++shell is not supported on this system"));
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100917 goto theend;
918# endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100919#endif
920 }
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100921 argvar[0].v_type = VAR_STRING;
922 argvar[0].vval.v_string = cmd;
923 argvar[1].v_type = VAR_UNKNOWN;
924 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100925
926theend:
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100927 vim_free(tofree);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200928 vim_free(opt.jo_eof_chars);
929}
930
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100931#if defined(FEAT_SESSION) || defined(PROTO)
932/*
933 * Write a :terminal command to the session file to restore the terminal in
934 * window "wp".
935 * Return FAIL if writing fails.
936 */
937 int
Bram Moolenaar0e655112020-09-11 20:36:36 +0200938term_write_session(FILE *fd, win_T *wp, hashtab_T *terminal_bufs)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100939{
Bram Moolenaar0e655112020-09-11 20:36:36 +0200940 const int bufnr = wp->w_buffer->b_fnum;
941 term_T *term = wp->w_buffer->b_term;
942
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200943 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200944 {
945 // There are multiple views into this terminal buffer. We don't want to
946 // create the terminal multiple times. If it's the first time, create,
947 // otherwise link to the first buffer.
948 char id_as_str[NUMBUFLEN];
949 hashitem_T *entry;
950
951 vim_snprintf(id_as_str, sizeof(id_as_str), "%d", bufnr);
952
953 entry = hash_find(terminal_bufs, (char_u *)id_as_str);
954 if (!HASHITEM_EMPTY(entry))
955 {
956 // we've already opened this terminal buffer
957 if (fprintf(fd, "execute 'buffer ' . s:term_buf_%d", bufnr) < 0)
958 return FAIL;
959 return put_eol(fd);
960 }
961 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100962
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100963 // Create the terminal and run the command. This is not without
964 // risk, but let's assume the user only creates a session when this
965 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100966 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
967 term->tl_cols, term->tl_rows) < 0)
968 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100969#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100970 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
971 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100972#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100973 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
974 return FAIL;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200975 if (put_eol(fd) != OK)
976 return FAIL;
977
978 if (fprintf(fd, "let s:term_buf_%d = bufnr()", bufnr) < 0)
979 return FAIL;
980
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200981 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200982 {
983 char *hash_key = alloc(NUMBUFLEN);
984
985 vim_snprintf(hash_key, NUMBUFLEN, "%d", bufnr);
986 hash_add(terminal_bufs, (char_u *)hash_key);
987 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100988
989 return put_eol(fd);
990}
991
992/*
993 * Return TRUE if "buf" has a terminal that should be restored.
994 */
995 int
996term_should_restore(buf_T *buf)
997{
998 term_T *term = buf->b_term;
999
1000 return term != NULL && (term->tl_command == NULL
1001 || STRCMP(term->tl_command, "NONE") != 0);
1002}
1003#endif
1004
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001005/*
1006 * Free the scrollback buffer for "term".
1007 */
1008 static void
1009free_scrollback(term_T *term)
1010{
1011 int i;
1012
1013 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
1014 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
1015 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001016 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
1017 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
1018 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001019}
1020
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001021
1022// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +02001023static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001024
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001025/*
1026 * Free a terminal and everything it refers to.
1027 * Kills the job if there is one.
1028 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001029 * The actual terminal structure is freed later in free_unused_terminals(),
1030 * because callbacks may wipe out a buffer while the terminal is still
1031 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001032 */
1033 void
1034free_terminal(buf_T *buf)
1035{
1036 term_T *term = buf->b_term;
1037 term_T *tp;
1038
1039 if (term == NULL)
1040 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001041
1042 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001043 if (first_term == term)
1044 first_term = term->tl_next;
1045 else
1046 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
1047 if (tp->tl_next == term)
1048 {
1049 tp->tl_next = term->tl_next;
1050 break;
1051 }
1052
1053 if (term->tl_job != NULL)
1054 {
1055 if (term->tl_job->jv_status != JOB_ENDED
1056 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +01001057 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001058 job_stop(term->tl_job, NULL, "kill");
1059 job_unref(term->tl_job);
1060 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001061 term->tl_next = terminals_to_free;
1062 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001063
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001064 buf->b_term = NULL;
1065 if (in_terminal_loop == term)
1066 in_terminal_loop = NULL;
1067}
1068
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001069 void
1070free_unused_terminals()
1071{
1072 while (terminals_to_free != NULL)
1073 {
1074 term_T *term = terminals_to_free;
1075
1076 terminals_to_free = term->tl_next;
1077
1078 free_scrollback(term);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02001079 ga_clear(&term->tl_osc_buf);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001080
1081 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001082 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001083 vim_free(term->tl_title);
1084#ifdef FEAT_SESSION
1085 vim_free(term->tl_command);
1086#endif
1087 vim_free(term->tl_kill);
1088 vim_free(term->tl_status_text);
1089 vim_free(term->tl_opencmd);
1090 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001091 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001092#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001093 if (term->tl_out_fd != NULL)
1094 fclose(term->tl_out_fd);
1095#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001096 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001097 vim_free(term->tl_cursor_color);
1098 vim_free(term);
1099 }
1100}
1101
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001102/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001103 * Get the part that is connected to the tty. Normally this is PART_IN, but
1104 * when writing buffer lines to the job it can be another. This makes it
1105 * possible to do "1,5term vim -".
1106 */
1107 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001108get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001109{
1110#ifdef UNIX
1111 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1112 int i;
1113
1114 for (i = 0; i < 3; ++i)
1115 {
1116 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1117
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001118 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001119 return parts[i];
1120 }
1121#endif
1122 return PART_IN;
1123}
1124
1125/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001126 * Write job output "msg[len]" to the vterm.
1127 */
1128 static void
1129term_write_job_output(term_T *term, char_u *msg, size_t len)
1130{
1131 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001132 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001133
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001134 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001135
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001136 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001137 if (prevlen != vterm_output_get_buffer_current(vterm))
1138 {
1139 char buf[KEY_BUF_LEN];
1140 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1141
1142 if (curlen > 0)
1143 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1144 (char_u *)buf, (int)curlen, NULL);
1145 }
1146
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001147 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001148 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1149}
1150
1151 static void
1152update_cursor(term_T *term, int redraw)
1153{
1154 if (term->tl_normal_mode)
1155 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001156#ifdef FEAT_GUI
1157 if (term->tl_system)
1158 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1159 term->tl_cursor_pos.col);
1160 else
1161#endif
1162 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001163 if (redraw)
1164 {
1165 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1166 cursor_on();
1167 out_flush();
1168#ifdef FEAT_GUI
1169 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001170 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001171 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001172 gui_mch_flush();
1173 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001174#endif
1175 }
1176}
1177
1178/*
1179 * Invoked when "msg" output from a job was received. Write it to the terminal
1180 * of "buffer".
1181 */
1182 void
1183write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1184{
1185 size_t len = STRLEN(msg);
1186 term_T *term = buffer->b_term;
1187
Bram Moolenaar4f974752019-02-17 17:44:42 +01001188#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001189 // Win32: Cannot redirect output of the job, intercept it here and write to
1190 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001191 if (term->tl_out_fd != NULL)
1192 {
1193 ch_log(channel, "Writing %d bytes to output file", (int)len);
1194 fwrite(msg, len, 1, term->tl_out_fd);
1195 return;
1196 }
1197#endif
1198
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001199 if (term->tl_vterm == NULL)
1200 {
1201 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1202 return;
1203 }
1204 ch_log(channel, "writing %d bytes to terminal", (int)len);
1205 term_write_job_output(term, msg, len);
1206
Bram Moolenaar13568252018-03-16 20:46:58 +01001207#ifdef FEAT_GUI
1208 if (term->tl_system)
1209 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001210 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001211 update_system_term(term);
1212 update_cursor(term, TRUE);
1213 }
1214 else
1215#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001216 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1217 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001218 if (!term->tl_normal_mode)
1219 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001220 // Don't use update_screen() when editing the command line, it gets
1221 // cleared.
1222 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001223 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001224 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001225 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001226 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001227 // update_screen() can be slow, check the terminal wasn't closed
1228 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001229 if (buffer == curbuf && curbuf->b_term != NULL)
1230 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001231 }
1232 else
1233 redraw_after_callback(TRUE);
1234 }
1235}
1236
1237/*
1238 * Send a mouse position and click to the vterm
1239 */
1240 static int
1241term_send_mouse(VTerm *vterm, int button, int pressed)
1242{
1243 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001244 int row = mouse_row - W_WINROW(curwin);
1245 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001246
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001247#ifdef FEAT_PROP_POPUP
1248 if (popup_is_popup(curwin))
1249 {
1250 row -= popup_top_extra(curwin);
1251 col -= popup_left_extra(curwin);
1252 }
1253#endif
1254 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001255 if (button != 0)
1256 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001257 return TRUE;
1258}
1259
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001260static int enter_mouse_col = -1;
1261static int enter_mouse_row = -1;
1262
1263/*
1264 * Handle a mouse click, drag or release.
1265 * Return TRUE when a mouse event is sent to the terminal.
1266 */
1267 static int
1268term_mouse_click(VTerm *vterm, int key)
1269{
1270#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001271 // For modeless selection mouse drag and release events are ignored, unless
1272 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001273 static int ignore_drag_release = TRUE;
1274 VTermMouseState mouse_state;
1275
1276 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1277 if (mouse_state.flags == 0)
1278 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001279 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001280 switch (key)
1281 {
1282 case K_LEFTDRAG:
1283 case K_LEFTRELEASE:
1284 case K_RIGHTDRAG:
1285 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001286 // Ignore drag and release events when the button-down wasn't
1287 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001288 if (ignore_drag_release)
1289 {
1290 int save_mouse_col, save_mouse_row;
1291
1292 if (enter_mouse_col < 0)
1293 break;
1294
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001295 // mouse click in the window gave us focus, handle that
1296 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001297 save_mouse_col = mouse_col;
1298 save_mouse_row = mouse_row;
1299 mouse_col = enter_mouse_col;
1300 mouse_row = enter_mouse_row;
1301 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1302 mouse_col = save_mouse_col;
1303 mouse_row = save_mouse_row;
1304 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001305 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001306 case K_LEFTMOUSE:
1307 case K_RIGHTMOUSE:
1308 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1309 ignore_drag_release = TRUE;
1310 else
1311 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001312 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001313 if (clip_star.available)
1314 {
1315 int button, is_click, is_drag;
1316
1317 button = get_mouse_button(KEY2TERMCAP1(key),
1318 &is_click, &is_drag);
1319 if (mouse_model_popup() && button == MOUSE_LEFT
1320 && (mod_mask & MOD_MASK_SHIFT))
1321 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001322 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001323 button = MOUSE_RIGHT;
1324 mod_mask &= ~MOD_MASK_SHIFT;
1325 }
1326 clip_modeless(button, is_click, is_drag);
1327 }
1328 break;
1329
1330 case K_MIDDLEMOUSE:
1331 if (clip_star.available)
1332 insert_reg('*', TRUE);
1333 break;
1334 }
1335 enter_mouse_col = -1;
1336 return FALSE;
1337 }
1338#endif
1339 enter_mouse_col = -1;
1340
1341 switch (key)
1342 {
1343 case K_LEFTMOUSE:
1344 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1345 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1346 case K_LEFTRELEASE:
1347 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1348 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1349 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1350 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1351 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1352 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1353 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1354 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1355 }
1356 return TRUE;
1357}
1358
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001359/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001360 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1361 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001362 * Return the number of bytes in "buf".
1363 */
1364 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001365term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001366{
1367 VTerm *vterm = term->tl_vterm;
1368 VTermKey key = VTERM_KEY_NONE;
1369 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001370 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001371
1372 switch (c)
1373 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001374 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001375
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001376 // don't use VTERM_KEY_BACKSPACE, it always
1377 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001378 case K_BS: c = term_backspace_char; break;
1379
1380 case ESC: key = VTERM_KEY_ESCAPE; break;
1381 case K_DEL: key = VTERM_KEY_DEL; break;
1382 case K_DOWN: key = VTERM_KEY_DOWN; break;
1383 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1384 key = VTERM_KEY_DOWN; break;
1385 case K_END: key = VTERM_KEY_END; break;
1386 case K_S_END: mod = VTERM_MOD_SHIFT;
1387 key = VTERM_KEY_END; break;
1388 case K_C_END: mod = VTERM_MOD_CTRL;
1389 key = VTERM_KEY_END; break;
1390 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1391 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1392 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1393 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1394 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1395 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1396 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1397 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1398 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1399 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1400 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1401 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1402 case K_HOME: key = VTERM_KEY_HOME; break;
1403 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1404 key = VTERM_KEY_HOME; break;
1405 case K_C_HOME: mod = VTERM_MOD_CTRL;
1406 key = VTERM_KEY_HOME; break;
1407 case K_INS: key = VTERM_KEY_INS; break;
1408 case K_K0: key = VTERM_KEY_KP_0; break;
1409 case K_K1: key = VTERM_KEY_KP_1; break;
1410 case K_K2: key = VTERM_KEY_KP_2; break;
1411 case K_K3: key = VTERM_KEY_KP_3; break;
1412 case K_K4: key = VTERM_KEY_KP_4; break;
1413 case K_K5: key = VTERM_KEY_KP_5; break;
1414 case K_K6: key = VTERM_KEY_KP_6; break;
1415 case K_K7: key = VTERM_KEY_KP_7; break;
1416 case K_K8: key = VTERM_KEY_KP_8; break;
1417 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001418 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001419 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001420 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001421 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001422 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1423 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001424 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1425 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001426 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1427 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001428 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1429 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1430 case K_LEFT: key = VTERM_KEY_LEFT; break;
1431 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1432 key = VTERM_KEY_LEFT; break;
1433 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1434 key = VTERM_KEY_LEFT; break;
1435 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1436 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1437 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1438 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1439 key = VTERM_KEY_RIGHT; break;
1440 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1441 key = VTERM_KEY_RIGHT; break;
1442 case K_UP: key = VTERM_KEY_UP; break;
1443 case K_S_UP: mod = VTERM_MOD_SHIFT;
1444 key = VTERM_KEY_UP; break;
1445 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001446 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1447 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001448
Bram Moolenaara42ad572017-11-16 13:08:04 +01001449 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1450 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001451 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1452 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001453
1454 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001455 case K_LEFTMOUSE_NM:
1456 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001457 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001458 case K_LEFTRELEASE_NM:
1459 case K_MOUSEMOVE:
1460 case K_MIDDLEMOUSE:
1461 case K_MIDDLEDRAG:
1462 case K_MIDDLERELEASE:
1463 case K_RIGHTMOUSE:
1464 case K_RIGHTDRAG:
1465 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1466 return 0;
1467 other = TRUE;
1468 break;
1469
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001470 case K_X1MOUSE: /* TODO */ return 0;
1471 case K_X1DRAG: /* TODO */ return 0;
1472 case K_X1RELEASE: /* TODO */ return 0;
1473 case K_X2MOUSE: /* TODO */ return 0;
1474 case K_X2DRAG: /* TODO */ return 0;
1475 case K_X2RELEASE: /* TODO */ return 0;
1476
1477 case K_IGNORE: return 0;
1478 case K_NOP: return 0;
1479 case K_UNDO: return 0;
1480 case K_HELP: return 0;
1481 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1482 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1483 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1484 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1485 case K_SELECT: return 0;
1486#ifdef FEAT_GUI
1487 case K_VER_SCROLLBAR: return 0;
1488 case K_HOR_SCROLLBAR: return 0;
1489#endif
1490#ifdef FEAT_GUI_TABLINE
1491 case K_TABLINE: return 0;
1492 case K_TABMENU: return 0;
1493#endif
1494#ifdef FEAT_NETBEANS_INTG
1495 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1496#endif
1497#ifdef FEAT_DND
1498 case K_DROP: return 0;
1499#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001500 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001501 case K_PS: vterm_keyboard_start_paste(vterm);
1502 other = TRUE;
1503 break;
1504 case K_PE: vterm_keyboard_end_paste(vterm);
1505 other = TRUE;
1506 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001507 }
1508
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001509 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001510 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001511 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001512 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001513 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001514 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001515 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001516
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001517 /*
1518 * Convert special keys to vterm keys:
1519 * - Write keys to vterm: vterm_keyboard_key()
1520 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001521 */
1522 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001523 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001524 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001525 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001526 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001527 vterm_keyboard_unichar(vterm, c, mod);
1528
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001529 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001530 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1531}
1532
1533/*
1534 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001535 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001536 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001537 */
1538 static int
1539term_job_running_check(term_T *term, int check_job_status)
1540{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001541 // Also consider the job finished when the channel is closed, to avoid a
1542 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001543 if (term != NULL
1544 && term->tl_job != NULL
1545 && channel_is_open(term->tl_job->jv_channel))
1546 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001547 job_T *job = term->tl_job;
1548
1549 // Careful: Checking the job status may invoked callbacks, which close
1550 // the buffer and terminate "term". However, "job" will not be freed
1551 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001552 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001553 job_status(job);
1554 return (job->jv_status == JOB_STARTED
1555 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001556 }
1557 return FALSE;
1558}
1559
1560/*
1561 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001562 */
1563 int
1564term_job_running(term_T *term)
1565{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001566 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001567}
1568
1569/*
1570 * Return TRUE if "term" has an active channel and used ":term NONE".
1571 */
1572 int
1573term_none_open(term_T *term)
1574{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001575 // Also consider the job finished when the channel is closed, to avoid a
1576 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001577 return term != NULL
1578 && term->tl_job != NULL
1579 && channel_is_open(term->tl_job->jv_channel)
1580 && term->tl_job->jv_channel->ch_keep_open;
1581}
1582
1583/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001584 * Used when exiting: kill the job in "buf" if so desired.
1585 * Return OK when the job finished.
1586 * Return FAIL when the job is still running.
1587 */
1588 int
1589term_try_stop_job(buf_T *buf)
1590{
1591 int count;
1592 char *how = (char *)buf->b_term->tl_kill;
1593
1594#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001595 if ((how == NULL || *how == NUL)
1596 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001597 {
1598 char_u buff[DIALOG_MSG_SIZE];
1599 int ret;
1600
Bram Moolenaar00806bc2020-11-05 19:36:38 +01001601 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001602 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1603 if (ret == VIM_YES)
1604 how = "kill";
1605 else if (ret == VIM_CANCEL)
1606 return FAIL;
1607 }
1608#endif
1609 if (how == NULL || *how == NUL)
1610 return FAIL;
1611
1612 job_stop(buf->b_term->tl_job, NULL, how);
1613
Bram Moolenaar9172d232019-01-29 23:06:54 +01001614 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001615 for (count = 0; count < 100; ++count)
1616 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001617 job_T *job;
1618
1619 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001620 if (!buf_valid(buf)
1621 || buf->b_term == NULL
1622 || buf->b_term->tl_job == NULL)
1623 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001624 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001625
Bram Moolenaar9172d232019-01-29 23:06:54 +01001626 // Call job_status() to update jv_status. It may cause the job to be
1627 // cleaned up but it won't be freed.
1628 job_status(job);
1629 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001630 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001631
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001632 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001633 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001634 }
1635 return FAIL;
1636}
1637
1638/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001639 * Add the last line of the scrollback buffer to the buffer in the window.
1640 */
1641 static void
1642add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1643{
1644 buf_T *buf = term->tl_buffer;
1645 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1646 linenr_T lnum = buf->b_ml.ml_line_count;
1647
Bram Moolenaar4f974752019-02-17 17:44:42 +01001648#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001649 if (!enc_utf8 && enc_codepage > 0)
1650 {
1651 WCHAR *ret = NULL;
1652 int length = 0;
1653
1654 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1655 &ret, &length);
1656 if (ret != NULL)
1657 {
1658 WideCharToMultiByte_alloc(enc_codepage, 0,
1659 ret, length, (char **)&text, &len, 0, 0);
1660 vim_free(ret);
1661 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1662 vim_free(text);
1663 }
1664 }
1665 else
1666#endif
1667 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1668 if (empty)
1669 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001670 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001671 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001672 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001673 curbuf = curwin->w_buffer;
1674 }
1675}
1676
1677 static void
1678cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1679{
1680 attr->width = cell->width;
1681 attr->attrs = cell->attrs;
1682 attr->fg = cell->fg;
1683 attr->bg = cell->bg;
1684}
1685
1686 static int
1687equal_celattr(cellattr_T *a, cellattr_T *b)
1688{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001689 // We only compare the RGB colors, ignoring the ANSI index and type.
1690 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001691 return a->fg.red == b->fg.red
1692 && a->fg.green == b->fg.green
1693 && a->fg.blue == b->fg.blue
1694 && a->bg.red == b->bg.red
1695 && a->bg.green == b->bg.green
1696 && a->bg.blue == b->bg.blue;
1697}
1698
Bram Moolenaard96ff162018-02-18 22:13:29 +01001699/*
1700 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1701 * line at this position. Otherwise at the end.
1702 */
1703 static int
1704add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1705{
1706 if (ga_grow(&term->tl_scrollback, 1) == OK)
1707 {
1708 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1709 + term->tl_scrollback.ga_len;
1710
1711 if (lnum > 0)
1712 {
1713 int i;
1714
1715 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1716 {
1717 *line = *(line - 1);
1718 --line;
1719 }
1720 }
1721 line->sb_cols = 0;
1722 line->sb_cells = NULL;
1723 line->sb_fill_attr = *fill_attr;
1724 ++term->tl_scrollback.ga_len;
1725 return OK;
1726 }
1727 return FALSE;
1728}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001729
1730/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001731 * Remove the terminal contents from the scrollback and the buffer.
1732 * Used before adding a new scrollback line or updating the buffer for lines
1733 * displayed in the terminal.
1734 */
1735 static void
1736cleanup_scrollback(term_T *term)
1737{
1738 sb_line_T *line;
1739 garray_T *gap;
1740
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001741 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001742 gap = &term->tl_scrollback;
1743 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1744 && gap->ga_len > 0)
1745 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001746 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001747 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1748 vim_free(line->sb_cells);
1749 --gap->ga_len;
1750 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001751 curbuf = curwin->w_buffer;
1752 if (curbuf == term->tl_buffer)
1753 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001754}
1755
1756/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001757 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001758 */
1759 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001760update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001761{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001762 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001763 int len;
1764 int lines_skipped = 0;
1765 VTermPos pos;
1766 VTermScreenCell cell;
1767 cellattr_T fill_attr, new_fill_attr;
1768 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001769
1770 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1771 "Adding terminal window snapshot to buffer");
1772
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001773 // First remove the lines that were appended before, they might be
1774 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001775 cleanup_scrollback(term);
1776
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001777 screen = vterm_obtain_screen(term->tl_vterm);
1778 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001779 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1780 {
1781 len = 0;
1782 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1783 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1784 && cell.chars[0] != NUL)
1785 {
1786 len = pos.col + 1;
1787 new_fill_attr = term->tl_default_color;
1788 }
1789 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001790 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001791 cell2cellattr(&cell, &new_fill_attr);
1792
1793 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1794 ++lines_skipped;
1795 else
1796 {
1797 while (lines_skipped > 0)
1798 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001799 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001800 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001801 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001802 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001803 }
1804
1805 if (len == 0)
1806 p = NULL;
1807 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001808 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001809 if ((p != NULL || len == 0)
1810 && ga_grow(&term->tl_scrollback, 1) == OK)
1811 {
1812 garray_T ga;
1813 int width;
1814 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1815 + term->tl_scrollback.ga_len;
1816
1817 ga_init2(&ga, 1, 100);
1818 for (pos.col = 0; pos.col < len; pos.col += width)
1819 {
1820 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1821 {
1822 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001823 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001824 if (ga_grow(&ga, 1) == OK)
1825 ga.ga_len += utf_char2bytes(' ',
1826 (char_u *)ga.ga_data + ga.ga_len);
1827 }
1828 else
1829 {
1830 width = cell.width;
1831
1832 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001833 if (width == 2)
1834 // second cell of double-width character has the
1835 // same attributes.
1836 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001837
Bram Moolenaara79fd562018-12-20 20:47:32 +01001838 // Each character can be up to 6 bytes.
1839 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001840 {
1841 int i;
1842 int c;
1843
1844 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1845 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1846 (char_u *)ga.ga_data + ga.ga_len);
1847 }
1848 }
1849 }
1850 line->sb_cols = len;
1851 line->sb_cells = p;
1852 line->sb_fill_attr = new_fill_attr;
1853 fill_attr = new_fill_attr;
1854 ++term->tl_scrollback.ga_len;
1855
1856 if (ga_grow(&ga, 1) == FAIL)
1857 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1858 else
1859 {
1860 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1861 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1862 }
1863 ga_clear(&ga);
1864 }
1865 else
1866 vim_free(p);
1867 }
1868 }
1869
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001870 // Add trailing empty lines.
1871 for (pos.row = term->tl_scrollback.ga_len;
1872 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1873 ++pos.row)
1874 {
1875 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1876 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1877 }
1878
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001879 term->tl_dirty_snapshot = FALSE;
1880#ifdef FEAT_TIMERS
1881 term->tl_timer_set = FALSE;
1882#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001883}
1884
1885/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001886 * Loop over all windows in the current tab, and also curwin, which is not
1887 * encountered when using a terminal in a popup window.
1888 * Return TRUE if "*wp" was set to the next window.
1889 */
1890 static int
1891for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1892{
1893 if (*wp == NULL)
1894 *wp = firstwin;
1895 else if ((*wp)->w_next != NULL)
1896 *wp = (*wp)->w_next;
1897 else if (!*did_curwin)
1898 *wp = curwin;
1899 else
1900 return FALSE;
1901 if (*wp == curwin)
1902 *did_curwin = TRUE;
1903 return TRUE;
1904}
1905
1906/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001907 * If needed, add the current lines of the terminal to scrollback and to the
1908 * buffer. Called after the job has ended and when switching to
1909 * Terminal-Normal mode.
1910 * When "redraw" is TRUE redraw the windows that show the terminal.
1911 */
1912 static void
1913may_move_terminal_to_buffer(term_T *term, int redraw)
1914{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001915 if (term->tl_vterm == NULL)
1916 return;
1917
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001918 // Update the snapshot only if something changes or the buffer does not
1919 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001920 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1921 <= term->tl_scrollback_scrolled)
1922 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001923
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001924 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001925 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1926 &term->tl_default_color.fg, &term->tl_default_color.bg);
1927
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001928 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001929 {
1930 win_T *wp = NULL;
1931 int did_curwin = FALSE;
1932
1933 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001934 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001935 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001936 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001937 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1938 wp->w_cursor.col = 0;
1939 wp->w_valid = 0;
1940 if (wp->w_cursor.lnum >= wp->w_height)
1941 {
1942 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001943
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001944 if (wp->w_topline < min_topline)
1945 wp->w_topline = min_topline;
1946 }
1947 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001948 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001949 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001950 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001951}
1952
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001953#if defined(FEAT_TIMERS) || defined(PROTO)
1954/*
1955 * Check if any terminal timer expired. If so, copy text from the terminal to
1956 * the buffer.
1957 * Return the time until the next timer will expire.
1958 */
1959 int
1960term_check_timers(int next_due_arg, proftime_T *now)
1961{
1962 term_T *term;
1963 int next_due = next_due_arg;
1964
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001965 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001966 {
1967 if (term->tl_timer_set && !term->tl_normal_mode)
1968 {
1969 long this_due = proftime_time_left(&term->tl_timer_due, now);
1970
1971 if (this_due <= 1)
1972 {
1973 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001974 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001975 }
1976 else if (next_due == -1 || next_due > this_due)
1977 next_due = this_due;
1978 }
1979 }
1980
1981 return next_due;
1982}
1983#endif
1984
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001985/*
1986 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
1987 * otherwise end it.
1988 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001989 static void
1990set_terminal_mode(term_T *term, int normal_mode)
1991{
1992 term->tl_normal_mode = normal_mode;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001993 if (!normal_mode)
1994 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001995 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001996 if (term->tl_buffer == curbuf)
1997 maketitle();
1998}
1999
2000/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002001 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002002 * Move the vterm contents into the scrollback buffer and free the vterm.
2003 */
2004 static void
2005cleanup_vterm(term_T *term)
2006{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002007 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002008 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002009 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002010 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002011}
2012
2013/*
2014 * Switch from Terminal-Job mode to Terminal-Normal mode.
2015 * Suspends updating the terminal window.
2016 */
2017 static void
2018term_enter_normal_mode(void)
2019{
2020 term_T *term = curbuf->b_term;
2021
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002022 set_terminal_mode(term, TRUE);
2023
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002024 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002025 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002026
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002027 // Move the window cursor to the position of the cursor in the
2028 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002029 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2030 + term->tl_cursor_pos.row + 1;
2031 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002032 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2033 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002034 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002035
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002036 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002037 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2038}
2039
2040/*
2041 * Returns TRUE if the current window contains a terminal and we are in
2042 * Terminal-Normal mode.
2043 */
2044 int
2045term_in_normal_mode(void)
2046{
2047 term_T *term = curbuf->b_term;
2048
2049 return term != NULL && term->tl_normal_mode;
2050}
2051
2052/*
2053 * Switch from Terminal-Normal mode to Terminal-Job mode.
2054 * Restores updating the terminal window.
2055 */
2056 void
2057term_enter_job_mode()
2058{
2059 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002060
2061 set_terminal_mode(term, FALSE);
2062
2063 if (term->tl_channel_closed)
2064 cleanup_vterm(term);
2065 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002066#ifdef FEAT_PROP_POPUP
2067 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002068 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002069#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002070}
2071
2072/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002073 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002074 * Note: while waiting a terminal may be closed and freed if the channel is
2075 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002076 */
2077 static int
2078term_vgetc()
2079{
2080 int c;
2081 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002082 int modify_other_keys =
2083 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002084
2085 State = TERMINAL;
2086 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002087#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002088 ctrl_break_was_pressed = FALSE;
2089#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002090 if (modify_other_keys)
2091 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002092 c = vgetc();
2093 got_int = FALSE;
2094 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002095 if (modify_other_keys)
2096 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002097 return c;
2098}
2099
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002100static int mouse_was_outside = FALSE;
2101
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002102/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002103 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002104 * Return FAIL when the key needs to be handled in Normal mode.
2105 * Return OK when the key was dropped or sent to the terminal.
2106 */
2107 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002108send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002109{
2110 char msg[KEY_BUF_LEN];
2111 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002112 int dragging_outside = FALSE;
2113
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002114 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002115 switch (c)
2116 {
2117 case NUL:
2118 case K_ZERO:
2119 if (typed)
2120 stuffcharReadbuff(c);
2121 return FAIL;
2122
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002123 case K_TABLINE:
2124 stuffcharReadbuff(c);
2125 return FAIL;
2126
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002127 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002128 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002129 return FAIL;
2130
2131 case K_LEFTDRAG:
2132 case K_MIDDLEDRAG:
2133 case K_RIGHTDRAG:
2134 case K_X1DRAG:
2135 case K_X2DRAG:
2136 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002137 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002138 case K_LEFTMOUSE:
2139 case K_LEFTMOUSE_NM:
2140 case K_LEFTRELEASE:
2141 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002142 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002143 case K_MIDDLEMOUSE:
2144 case K_MIDDLERELEASE:
2145 case K_RIGHTMOUSE:
2146 case K_RIGHTRELEASE:
2147 case K_X1MOUSE:
2148 case K_X1RELEASE:
2149 case K_X2MOUSE:
2150 case K_X2RELEASE:
2151
2152 case K_MOUSEUP:
2153 case K_MOUSEDOWN:
2154 case K_MOUSELEFT:
2155 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002156 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002157 int row = mouse_row;
2158 int col = mouse_col;
2159
2160#ifdef FEAT_PROP_POPUP
2161 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002162 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002163 row -= popup_top_extra(curwin);
2164 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002165 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002166#endif
2167 if (row < W_WINROW(curwin)
2168 || row >= (W_WINROW(curwin) + curwin->w_height)
2169 || col < curwin->w_wincol
2170 || col >= W_ENDCOL(curwin)
2171 || dragging_outside)
2172 {
2173 // click or scroll outside the current window or on status
2174 // line or vertical separator
2175 if (typed)
2176 {
2177 stuffcharReadbuff(c);
2178 mouse_was_outside = TRUE;
2179 }
2180 return FAIL;
2181 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002182 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002183 break;
2184
2185 case K_COMMAND:
2186 return do_cmdline(NULL, getcmdkeycmd, NULL, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002187 }
2188 if (typed)
2189 mouse_was_outside = FALSE;
2190
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002191 // Convert the typed key to a sequence of bytes for the job.
2192 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002193 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002194 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002195 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2196 (char_u *)msg, (int)len, NULL);
2197
2198 return OK;
2199}
2200
2201 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002202position_cursor(win_T *wp, VTermPos *pos, int add_off UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002203{
2204 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2205 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002206#ifdef FEAT_PROP_POPUP
2207 if (add_off && popup_is_popup(curwin))
2208 {
2209 wp->w_wrow += popup_top_extra(curwin);
2210 wp->w_wcol += popup_left_extra(curwin);
2211 }
2212#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002213 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2214}
2215
2216/*
2217 * Handle CTRL-W "": send register contents to the job.
2218 */
2219 static void
2220term_paste_register(int prev_c UNUSED)
2221{
2222 int c;
2223 list_T *l;
2224 listitem_T *item;
2225 long reglen = 0;
2226 int type;
2227
2228#ifdef FEAT_CMDL_INFO
2229 if (add_to_showcmd(prev_c))
2230 if (add_to_showcmd('"'))
2231 out_flush();
2232#endif
2233 c = term_vgetc();
2234#ifdef FEAT_CMDL_INFO
2235 clear_showcmd();
2236#endif
2237 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002238 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002239 return;
2240
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002241 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002242 if (c == '=' && get_expr_register() != '=')
2243 return;
2244 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002245 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002246 return;
2247
2248 l = (list_T *)get_reg_contents(c, GREG_LIST);
2249 if (l != NULL)
2250 {
2251 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002252 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002253 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002254 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002255#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002256 char_u *tmp = s;
2257
2258 if (!enc_utf8 && enc_codepage > 0)
2259 {
2260 WCHAR *ret = NULL;
2261 int length = 0;
2262
2263 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2264 (int)STRLEN(s), &ret, &length);
2265 if (ret != NULL)
2266 {
2267 WideCharToMultiByte_alloc(CP_UTF8, 0,
2268 ret, length, (char **)&s, &length, 0, 0);
2269 vim_free(ret);
2270 }
2271 }
2272#endif
2273 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2274 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002275#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002276 if (tmp != s)
2277 vim_free(s);
2278#endif
2279
2280 if (item->li_next != NULL || type == MLINE)
2281 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2282 (char_u *)"\r", 1, NULL);
2283 }
2284 list_free(l);
2285 }
2286}
2287
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002288/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002289 * Return TRUE when waiting for a character in the terminal, the cursor of the
2290 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002291 */
2292 int
2293terminal_is_active()
2294{
2295 return in_terminal_loop != NULL;
2296}
2297
Bram Moolenaar83d47902020-03-26 20:34:00 +01002298/*
2299 * Return the highight group name for the terminal; "Terminal" if not set.
2300 */
2301 static char_u *
2302term_get_highlight_name(term_T *term)
2303{
2304 if (term->tl_highlight_name == NULL)
2305 return (char_u *)"Terminal";
2306 return term->tl_highlight_name;
2307}
2308
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002309#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002310 cursorentry_T *
2311term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2312{
2313 term_T *term = in_terminal_loop;
2314 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002315 int id;
2316 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002317
Bram Moolenaara80faa82020-04-12 19:37:17 +02002318 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002319 entry.shape = entry.mshape =
2320 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2321 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2322 SHAPE_BLOCK;
2323 entry.percentage = 20;
2324 if (term->tl_cursor_blink)
2325 {
2326 entry.blinkwait = 700;
2327 entry.blinkon = 400;
2328 entry.blinkoff = 250;
2329 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002330
Bram Moolenaar83d47902020-03-26 20:34:00 +01002331 // The highlight group overrules the defaults.
2332 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002333 if (id != 0)
2334 {
2335 syn_id2colors(id, &term_fg, &term_bg);
2336 *fg = term_bg;
2337 }
2338 else
2339 *fg = gui.back_pixel;
2340
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002341 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002342 {
2343 if (id != 0)
2344 *bg = term_fg;
2345 else
2346 *bg = gui.norm_pixel;
2347 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002348 else
2349 *bg = color_name2handle(term->tl_cursor_color);
2350 entry.name = "n";
2351 entry.used_for = SHAPE_CURSOR;
2352
2353 return &entry;
2354}
2355#endif
2356
Bram Moolenaard317b382018-02-08 22:33:31 +01002357 static void
2358may_output_cursor_props(void)
2359{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002360 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002361 || last_set_cursor_shape != desired_cursor_shape
2362 || last_set_cursor_blink != desired_cursor_blink)
2363 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002364 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002365 last_set_cursor_shape = desired_cursor_shape;
2366 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002367 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002368 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002369 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002370 ui_cursor_shape_forced(TRUE);
2371 else
2372 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2373 }
2374}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002375
Bram Moolenaard317b382018-02-08 22:33:31 +01002376/*
2377 * Set the cursor color and shape, if not last set to these.
2378 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002379 static void
2380may_set_cursor_props(term_T *term)
2381{
2382#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002383 // For the GUI the cursor properties are obtained with
2384 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002385 if (gui.in_use)
2386 return;
2387#endif
2388 if (in_terminal_loop == term)
2389 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002390 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002391 desired_cursor_shape = term->tl_cursor_shape;
2392 desired_cursor_blink = term->tl_cursor_blink;
2393 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002394 }
2395}
2396
Bram Moolenaard317b382018-02-08 22:33:31 +01002397/*
2398 * Reset the desired cursor properties and restore them when needed.
2399 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002400 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002401prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002402{
2403#ifdef FEAT_GUI
2404 if (gui.in_use)
2405 return;
2406#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002407 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002408 desired_cursor_shape = -1;
2409 desired_cursor_blink = -1;
2410 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002411}
2412
2413/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002414 * Returns TRUE if the current window contains a terminal and we are sending
2415 * keys to the job.
2416 * If "check_job_status" is TRUE update the job status.
2417 */
2418 static int
2419term_use_loop_check(int check_job_status)
2420{
2421 term_T *term = curbuf->b_term;
2422
2423 return term != NULL
2424 && !term->tl_normal_mode
2425 && term->tl_vterm != NULL
2426 && term_job_running_check(term, check_job_status);
2427}
2428
2429/*
2430 * Returns TRUE if the current window contains a terminal and we are sending
2431 * keys to the job.
2432 */
2433 int
2434term_use_loop(void)
2435{
2436 return term_use_loop_check(FALSE);
2437}
2438
2439/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002440 * Called when entering a window with the mouse. If this is a terminal window
2441 * we may want to change state.
2442 */
2443 void
2444term_win_entered()
2445{
2446 term_T *term = curbuf->b_term;
2447
2448 if (term != NULL)
2449 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002450 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002451 {
2452 reset_VIsual_and_resel();
2453 if (State & INSERT)
2454 stop_insert_mode = TRUE;
2455 }
2456 mouse_was_outside = FALSE;
2457 enter_mouse_col = mouse_col;
2458 enter_mouse_row = mouse_row;
2459 }
2460}
2461
2462/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002463 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2464 * Return the Ctrl-key value in that case.
2465 */
2466 static int
2467raw_c_to_ctrl(int c)
2468{
2469 if ((mod_mask & MOD_MASK_CTRL)
2470 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2471 return c & 0x1f;
2472 return c;
2473}
2474
2475/*
2476 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2477 * May set "mod_mask".
2478 */
2479 static int
2480ctrl_to_raw_c(int c)
2481{
2482 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2483 {
2484 mod_mask |= MOD_MASK_CTRL;
2485 return c + '@';
2486 }
2487 return c;
2488}
2489
2490/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002491 * Wait for input and send it to the job.
2492 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2493 * when there is no more typahead.
2494 * Return when the start of a CTRL-W command is typed or anything else that
2495 * should be handled as a Normal mode command.
2496 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2497 * the terminal was closed.
2498 */
2499 int
2500terminal_loop(int blocking)
2501{
2502 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002503 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002504 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002505 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002506#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002507 int tty_fd = curbuf->b_term->tl_job->jv_channel
2508 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002509#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002510 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002511
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002512 // Remember the terminal we are sending keys to. However, the terminal
2513 // might be closed while waiting for a character, e.g. typing "exit" in a
2514 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2515 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002516 in_terminal_loop = curbuf->b_term;
2517
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002518 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002519 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002520 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002521 if (termwinkey == Ctrl_W)
2522 termwinkey = 0;
2523 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002524 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002525 may_set_cursor_props(curbuf->b_term);
2526
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002527 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002528 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002529#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002530 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002531#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002532 // TODO: skip screen update when handling a sequence of keys.
2533 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002534 while (must_redraw != 0)
2535 if (update_screen(0) == FAIL)
2536 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002537 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002538 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002539 break;
2540
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002541 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002542 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002543
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002544 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002545 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002546 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002547 // Job finished while waiting for a character. Push back the
2548 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002549 if (raw_c != K_IGNORE)
2550 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002551 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002552 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002553 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002554 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002555 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002556
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002557#ifdef UNIX
2558 /*
2559 * The shell or another program may change the tty settings. Getting
2560 * them for every typed character is a bit of overhead, but it's needed
2561 * for the first character typed, e.g. when Vim starts in a shell.
2562 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002563 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002564 {
2565 ttyinfo_T info;
2566
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002567 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002568 if (get_tty_info(tty_fd, &info) == OK)
2569 term_backspace_char = info.backspace;
2570 }
2571#endif
2572
Bram Moolenaar4f974752019-02-17 17:44:42 +01002573#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002574 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2575 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002576 if (ctrl_break_was_pressed)
2577 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2578#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002579 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2580 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002581 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002582#ifdef FEAT_GUI
2583 && !curbuf->b_term->tl_system
2584#endif
2585 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002586 {
2587 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002588 int prev_raw_c = raw_c;
2589 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002590
2591#ifdef FEAT_CMDL_INFO
2592 if (add_to_showcmd(c))
2593 out_flush();
2594#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002595 raw_c = term_vgetc();
2596 c = raw_c_to_ctrl(raw_c);
2597
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002598#ifdef FEAT_CMDL_INFO
2599 clear_showcmd();
2600#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002601 if (!term_use_loop_check(TRUE)
2602 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002603 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002604 break;
2605
2606 if (prev_c == Ctrl_BSL)
2607 {
2608 if (c == Ctrl_N)
2609 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002610 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002611 term_enter_normal_mode();
2612 ret = FAIL;
2613 goto theend;
2614 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002615 // Send both keys to the terminal, first one here, second one
2616 // below.
2617 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2618 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002619 }
2620 else if (c == Ctrl_C)
2621 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002622 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002623 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2624 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002625 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002626 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002627 // "CTRL-W .": send CTRL-W to the job
2628 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002629 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002630 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002631 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002632 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002633 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002634 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002635 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002636 else if (c == 'N')
2637 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002638 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002639 term_enter_normal_mode();
2640 ret = FAIL;
2641 goto theend;
2642 }
2643 else if (c == '"')
2644 {
2645 term_paste_register(prev_c);
2646 continue;
2647 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002648 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002649 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002650 // space for CTRL-W, modifier, multi-byte char and NUL
2651 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002652
2653 // Put the command into the typeahead buffer, when using the
2654 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2655 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002656 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002657 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002658 ret = OK;
2659 goto theend;
2660 }
2661 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002662# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002663 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002664 {
2665 WCHAR wc;
2666 char_u mb[3];
2667
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002668 mb[0] = (unsigned)raw_c >> 8;
2669 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002670 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002671 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002672 }
2673# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002674 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002675 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002676 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002677 // We are sure to come back here, don't reset the cursor color
2678 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002679 restore_cursor = FALSE;
2680
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002681 ret = OK;
2682 goto theend;
2683 }
2684 }
2685 ret = FAIL;
2686
2687theend:
2688 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002689 if (restore_cursor)
2690 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002691
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002692 // Move a snapshot of the screen contents to the buffer, so that completion
2693 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002694 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2695 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002696
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002697 return ret;
2698}
2699
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002700 static void
2701may_toggle_cursor(term_T *term)
2702{
2703 if (in_terminal_loop == term)
2704 {
2705 if (term->tl_cursor_visible)
2706 cursor_on();
2707 else
2708 cursor_off();
2709 }
2710}
2711
2712/*
Bram Moolenaar83d47902020-03-26 20:34:00 +01002713 * Cache "Terminal" highlight group colors.
2714 */
2715 void
2716set_terminal_default_colors(int cterm_fg, int cterm_bg)
2717{
2718 term_default_cterm_fg = cterm_fg - 1;
2719 term_default_cterm_bg = cterm_bg - 1;
2720}
2721
2722 static int
2723get_default_cterm_fg(term_T *term)
2724{
2725 if (term->tl_highlight_name != NULL)
2726 {
2727 int id = syn_name2id(term->tl_highlight_name);
2728 int fg = -1;
2729 int bg = -1;
2730
2731 if (id > 0)
2732 syn_id2cterm_bg(id, &fg, &bg);
2733 return fg;
2734 }
2735 return term_default_cterm_fg;
2736}
2737
2738 static int
2739get_default_cterm_bg(term_T *term)
2740{
2741 if (term->tl_highlight_name != NULL)
2742 {
2743 int id = syn_name2id(term->tl_highlight_name);
2744 int fg = -1;
2745 int bg = -1;
2746
2747 if (id > 0)
2748 syn_id2cterm_bg(id, &fg, &bg);
2749 return bg;
2750 }
2751 return term_default_cterm_bg;
2752}
2753
2754/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002755 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002756 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002757 */
2758 static int
2759color2index(VTermColor *color, int fg, int *boldp)
2760{
2761 int red = color->red;
2762 int blue = color->blue;
2763 int green = color->green;
2764
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002765 if (VTERM_COLOR_IS_DEFAULT_FG(color)
2766 || VTERM_COLOR_IS_DEFAULT_BG(color))
2767 return 0;
2768 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002769 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002770 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002771 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002772 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002773 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002774 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2775 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2776 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002777 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002778 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2779 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2780 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2781 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2782 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2783 case 10: return lookup_color(20, fg, boldp) + 1; // red
2784 case 11: return lookup_color(16, fg, boldp) + 1; // green
2785 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2786 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2787 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2788 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2789 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002790 }
2791 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002792
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002793 if (t_colors >= 256)
2794 {
2795 if (red == blue && red == green)
2796 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002797 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002798 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002799 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2800 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2801 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002802 int i;
2803
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002804 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002805 return 17; // 00/00/00
2806 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002807 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002808 for (i = 0; i < 23; ++i)
2809 if (red < cutoff[i])
2810 return i + 233;
2811 return 256;
2812 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002813 {
2814 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2815 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002816
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002817 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002818 for (ri = 0; ri < 5; ++ri)
2819 if (red < cutoff[ri])
2820 break;
2821 for (gi = 0; gi < 5; ++gi)
2822 if (green < cutoff[gi])
2823 break;
2824 for (bi = 0; bi < 5; ++bi)
2825 if (blue < cutoff[bi])
2826 break;
2827 return 17 + ri * 36 + gi * 6 + bi;
2828 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002829 }
2830 return 0;
2831}
2832
2833/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002834 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002835 */
2836 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002837vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002838{
2839 int attr = 0;
2840
2841 if (cellattrs.bold)
2842 attr |= HL_BOLD;
2843 if (cellattrs.underline)
2844 attr |= HL_UNDERLINE;
2845 if (cellattrs.italic)
2846 attr |= HL_ITALIC;
2847 if (cellattrs.strike)
2848 attr |= HL_STRIKETHROUGH;
2849 if (cellattrs.reverse)
2850 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002851 return attr;
2852}
2853
2854/*
2855 * Store Vterm attributes in "cell" from highlight flags.
2856 */
2857 static void
2858hl2vtermAttr(int attr, cellattr_T *cell)
2859{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002860 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002861 if (attr & HL_BOLD)
2862 cell->attrs.bold = 1;
2863 if (attr & HL_UNDERLINE)
2864 cell->attrs.underline = 1;
2865 if (attr & HL_ITALIC)
2866 cell->attrs.italic = 1;
2867 if (attr & HL_STRIKETHROUGH)
2868 cell->attrs.strike = 1;
2869 if (attr & HL_INVERSE)
2870 cell->attrs.reverse = 1;
2871}
2872
2873/*
2874 * Convert the attributes of a vterm cell into an attribute index.
2875 */
2876 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002877cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002878 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002879 win_T *wp,
2880 VTermScreenCellAttrs cellattrs,
2881 VTermColor cellfg,
2882 VTermColor cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002883{
2884 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002885
2886#ifdef FEAT_GUI
2887 if (gui.in_use)
2888 {
2889 guicolor_T fg, bg;
2890
2891 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2892 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2893 return get_gui_attr_idx(attr, fg, bg);
2894 }
2895 else
2896#endif
2897#ifdef FEAT_TERMGUICOLORS
2898 if (p_tgc)
2899 {
2900 guicolor_T fg, bg;
2901
2902 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2903 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2904
2905 return get_tgc_attr_idx(attr, fg, bg);
2906 }
2907 else
2908#endif
2909 {
2910 int bold = MAYBE;
2911 int fg = color2index(&cellfg, TRUE, &bold);
2912 int bg = color2index(&cellbg, FALSE, &bold);
2913
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002914 // Use the 'wincolor' or "Terminal" highlighting for the default
2915 // colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002916 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002917 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002918 int wincolor_fg = -1;
2919 int wincolor_bg = -1;
2920
2921 if (wp != NULL && *wp->w_p_wcr != NUL)
2922 {
2923 int id = syn_name2id(curwin->w_p_wcr);
2924
2925 // Get the 'wincolor' group colors.
2926 if (id > 0)
2927 syn_id2cterm_bg(id, &wincolor_fg, &wincolor_bg);
2928 }
2929 if (fg == 0)
2930 {
2931 if (wincolor_fg >= 0)
2932 fg = wincolor_fg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002933 else
2934 {
2935 int cterm_fg = get_default_cterm_fg(term);
2936
2937 if (cterm_fg >= 0)
2938 fg = cterm_fg + 1;
2939 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002940 }
2941 if (bg == 0)
2942 {
2943 if (wincolor_bg >= 0)
2944 bg = wincolor_bg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002945 else
2946 {
2947 int cterm_bg = get_default_cterm_bg(term);
2948
2949 if (cterm_bg >= 0)
2950 bg = cterm_bg + 1;
2951 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002952 }
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002953 }
2954
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002955 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002956 if (bold == TRUE)
2957 attr |= HL_BOLD;
2958 return get_cterm_attr_idx(attr, fg, bg);
2959 }
2960 return 0;
2961}
2962
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002963 static void
2964set_dirty_snapshot(term_T *term)
2965{
2966 term->tl_dirty_snapshot = TRUE;
2967#ifdef FEAT_TIMERS
2968 if (!term->tl_normal_mode)
2969 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002970 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002971 profile_setlimit(100L, &term->tl_timer_due);
2972 term->tl_timer_set = TRUE;
2973 }
2974#endif
2975}
2976
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002977 static int
2978handle_damage(VTermRect rect, void *user)
2979{
2980 term_T *term = (term_T *)user;
2981
2982 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2983 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002984 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002985 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002986 return 1;
2987}
2988
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002989 static void
2990term_scroll_up(term_T *term, int start_row, int count)
2991{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002992 win_T *wp = NULL;
2993 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002994 VTermColor fg, bg;
2995 VTermScreenCellAttrs attr;
2996 int clear_attr;
2997
Bram Moolenaara80faa82020-04-12 19:37:17 +02002998 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002999
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003000 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003001 {
3002 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003003 {
3004 // Set the color to clear lines with.
3005 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
3006 &fg, &bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01003007 clear_attr = cell2attr(term, wp, attr, fg, bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003008 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003009 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003010 }
3011}
3012
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003013 static int
3014handle_moverect(VTermRect dest, VTermRect src, void *user)
3015{
3016 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003017 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003018
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003019 // Scrolling up is done much more efficiently by deleting lines instead of
3020 // redrawing the text. But avoid doing this multiple times, postpone until
3021 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003022 if (dest.start_col == src.start_col
3023 && dest.end_col == src.end_col
3024 && dest.start_row < src.start_row)
3025 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003026 if (dest.start_row == 0)
3027 term->tl_postponed_scroll += count;
3028 else
3029 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003030 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003031
3032 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3033 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003034 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003035
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003036 // Note sure if the scrolling will work correctly, let's do a complete
3037 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003038 redraw_buf_later(term->tl_buffer, NOT_VALID);
3039 return 1;
3040}
3041
3042 static int
3043handle_movecursor(
3044 VTermPos pos,
3045 VTermPos oldpos UNUSED,
3046 int visible,
3047 void *user)
3048{
3049 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003050 win_T *wp = NULL;
3051 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003052
3053 term->tl_cursor_pos = pos;
3054 term->tl_cursor_visible = visible;
3055
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003056 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003057 {
3058 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003059 position_cursor(wp, &pos, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003060 }
3061 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
3062 {
3063 may_toggle_cursor(term);
3064 update_cursor(term, term->tl_cursor_visible);
3065 }
3066
3067 return 1;
3068}
3069
3070 static int
3071handle_settermprop(
3072 VTermProp prop,
3073 VTermValue *value,
3074 void *user)
3075{
3076 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003077 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003078
3079 switch (prop)
3080 {
3081 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003082 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003083 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003084 if (strval == NULL)
3085 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003086 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003087 // a blank title isn't useful, make it empty, so that "running" is
3088 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003089 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003090 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003091 // Same as blank
3092 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003093 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003094 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3095 term->tl_title = NULL;
3096 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003097 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003098 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003099#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003100 else if (!enc_utf8 && enc_codepage > 0)
3101 {
3102 WCHAR *ret = NULL;
3103 int length = 0;
3104
3105 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003106 (char*)value->string.str,
3107 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003108 if (ret != NULL)
3109 {
3110 WideCharToMultiByte_alloc(enc_codepage, 0,
3111 ret, length, (char**)&term->tl_title,
3112 &length, 0, 0);
3113 vim_free(ret);
3114 }
3115 }
3116#endif
3117 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003118 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003119 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003120 strval = NULL;
3121 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003122 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003123 if (term == curbuf->b_term)
3124 maketitle();
3125 break;
3126
3127 case VTERM_PROP_CURSORVISIBLE:
3128 term->tl_cursor_visible = value->boolean;
3129 may_toggle_cursor(term);
3130 out_flush();
3131 break;
3132
3133 case VTERM_PROP_CURSORBLINK:
3134 term->tl_cursor_blink = value->boolean;
3135 may_set_cursor_props(term);
3136 break;
3137
3138 case VTERM_PROP_CURSORSHAPE:
3139 term->tl_cursor_shape = value->number;
3140 may_set_cursor_props(term);
3141 break;
3142
3143 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003144 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003145 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003146 if (strval == NULL)
3147 break;
3148 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003149 may_set_cursor_props(term);
3150 break;
3151
3152 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003153 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003154 term->tl_using_altscreen = value->boolean;
3155 break;
3156
3157 default:
3158 break;
3159 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003160 vim_free(strval);
3161
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003162 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003163 return 1;
3164}
3165
3166/*
3167 * The job running in the terminal resized the terminal.
3168 */
3169 static int
3170handle_resize(int rows, int cols, void *user)
3171{
3172 term_T *term = (term_T *)user;
3173 win_T *wp;
3174
3175 term->tl_rows = rows;
3176 term->tl_cols = cols;
3177 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003178 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003179 term->tl_vterm_size_changed = FALSE;
3180 else
3181 {
3182 FOR_ALL_WINDOWS(wp)
3183 {
3184 if (wp->w_buffer == term->tl_buffer)
3185 {
3186 win_setheight_win(rows, wp);
3187 win_setwidth_win(cols, wp);
3188 }
3189 }
3190 redraw_buf_later(term->tl_buffer, NOT_VALID);
3191 }
3192 return 1;
3193}
3194
3195/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003196 * If the number of lines that are stored goes over 'termscrollback' then
3197 * delete the first 10%.
3198 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3199 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003200 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003201 static void
3202limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003203{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003204 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003205 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003206 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003207 int i;
3208
3209 curbuf = term->tl_buffer;
3210 for (i = 0; i < todo; ++i)
3211 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003212 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3213 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003214 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003215 }
3216 curbuf = curwin->w_buffer;
3217
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003218 gap->ga_len -= todo;
3219 mch_memmove(gap->ga_data,
3220 (sb_line_T *)gap->ga_data + todo,
3221 sizeof(sb_line_T) * gap->ga_len);
3222 if (update_buffer)
3223 term->tl_scrollback_scrolled -= todo;
3224 }
3225}
3226
3227/*
3228 * Handle a line that is pushed off the top of the screen.
3229 */
3230 static int
3231handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3232{
3233 term_T *term = (term_T *)user;
3234 garray_T *gap;
3235 int update_buffer;
3236
3237 if (term->tl_normal_mode)
3238 {
3239 // In Terminal-Normal mode the user interacts with the buffer, thus we
3240 // must not change it. Postpone adding the scrollback lines.
3241 gap = &term->tl_scrollback_postponed;
3242 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003243 }
3244 else
3245 {
3246 // First remove the lines that were appended before, the pushed line
3247 // goes above it.
3248 cleanup_scrollback(term);
3249 gap = &term->tl_scrollback;
3250 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003251 }
3252
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003253 limit_scrollback(term, gap, update_buffer);
3254
3255 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003256 {
3257 cellattr_T *p = NULL;
3258 int len = 0;
3259 int i;
3260 int c;
3261 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003262 int text_len;
3263 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003264 sb_line_T *line;
3265 garray_T ga;
3266 cellattr_T fill_attr = term->tl_default_color;
3267
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003268 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003269 for (i = 0; i < cols; ++i)
3270 if (cells[i].chars[0] != 0)
3271 len = i + 1;
3272 else
3273 cell2cellattr(&cells[i], &fill_attr);
3274
3275 ga_init2(&ga, 1, 100);
3276 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003277 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003278 if (p != NULL)
3279 {
3280 for (col = 0; col < len; col += cells[col].width)
3281 {
3282 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3283 {
3284 ga.ga_len = 0;
3285 break;
3286 }
3287 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3288 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3289 (char_u *)ga.ga_data + ga.ga_len);
3290 cell2cellattr(&cells[col], &p[col]);
3291 }
3292 }
3293 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003294 {
3295 if (update_buffer)
3296 text = (char_u *)"";
3297 else
3298 text = vim_strsave((char_u *)"");
3299 text_len = 0;
3300 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003301 else
3302 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003303 text = ga.ga_data;
3304 text_len = ga.ga_len;
3305 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003306 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003307 if (update_buffer)
3308 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003309
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003310 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003311 line->sb_cols = len;
3312 line->sb_cells = p;
3313 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003314 if (update_buffer)
3315 {
3316 line->sb_text = NULL;
3317 ++term->tl_scrollback_scrolled;
3318 ga_clear(&ga); // free the text
3319 }
3320 else
3321 {
3322 line->sb_text = text;
3323 ga_init(&ga); // text is kept in tl_scrollback_postponed
3324 }
3325 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003326 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003327 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003328}
3329
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003330/*
3331 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3332 * received and stored in tl_scrollback_postponed.
3333 */
3334 static void
3335handle_postponed_scrollback(term_T *term)
3336{
3337 int i;
3338
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003339 if (term->tl_scrollback_postponed.ga_len == 0)
3340 return;
3341 ch_log(NULL, "Moving postponed scrollback to scrollback");
3342
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003343 // First remove the lines that were appended before, the pushed lines go
3344 // above it.
3345 cleanup_scrollback(term);
3346
3347 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3348 {
3349 char_u *text;
3350 sb_line_T *pp_line;
3351 sb_line_T *line;
3352
3353 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3354 break;
3355 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3356
3357 text = pp_line->sb_text;
3358 if (text == NULL)
3359 text = (char_u *)"";
3360 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3361 vim_free(pp_line->sb_text);
3362
3363 line = (sb_line_T *)term->tl_scrollback.ga_data
3364 + term->tl_scrollback.ga_len;
3365 line->sb_cols = pp_line->sb_cols;
3366 line->sb_cells = pp_line->sb_cells;
3367 line->sb_fill_attr = pp_line->sb_fill_attr;
3368 line->sb_text = NULL;
3369 ++term->tl_scrollback_scrolled;
3370 ++term->tl_scrollback.ga_len;
3371 }
3372
3373 ga_clear(&term->tl_scrollback_postponed);
3374 limit_scrollback(term, &term->tl_scrollback, TRUE);
3375}
3376
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003377static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003378 handle_damage, // damage
3379 handle_moverect, // moverect
3380 handle_movecursor, // movecursor
3381 handle_settermprop, // settermprop
3382 NULL, // bell
3383 handle_resize, // resize
3384 handle_pushline, // sb_pushline
3385 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003386};
3387
3388/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003389 * Do the work after the channel of a terminal was closed.
3390 * Must be called only when updating_screen is FALSE.
3391 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3392 */
3393 static int
3394term_after_channel_closed(term_T *term)
3395{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003396 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003397 if (!term->tl_normal_mode)
3398 {
3399 int fnum = term->tl_buffer->b_fnum;
3400
3401 cleanup_vterm(term);
3402
3403 if (term->tl_finish == TL_FINISH_CLOSE)
3404 {
3405 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003406 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003407#ifdef FEAT_PROP_POPUP
3408 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003409
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003410 // If this was a terminal in a popup window, go back to the
3411 // previous window.
3412 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3413 {
3414 pwin = curwin;
3415 if (win_valid(prevwin))
3416 win_enter(prevwin, FALSE);
3417 }
3418 else
3419#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003420 // If this is the last normal window: exit Vim.
3421 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3422 {
3423 exarg_T ea;
3424
Bram Moolenaara80faa82020-04-12 19:37:17 +02003425 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003426 ex_quit(&ea);
3427 return TRUE;
3428 }
3429
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003430 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003431 ch_log(NULL, "terminal job finished, closing window");
3432 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003433 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003434 if (curwin == aucmd_win)
3435 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003436 if (do_set_w_closing)
3437 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003438 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003439 if (do_set_w_closing)
3440 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003441 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003442#ifdef FEAT_PROP_POPUP
3443 if (pwin != NULL)
3444 popup_close_with_retval(pwin, 0);
3445#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003446 return TRUE;
3447 }
3448 if (term->tl_finish == TL_FINISH_OPEN
3449 && term->tl_buffer->b_nwindows == 0)
3450 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003451 char *cmd = term->tl_opencmd == NULL
3452 ? "botright sbuf %d"
3453 : (char *)term->tl_opencmd;
3454 size_t len = strlen(cmd) + 50;
3455 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003456
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003457 if (buf != NULL)
3458 {
3459 ch_log(NULL, "terminal job finished, opening window");
3460 vim_snprintf(buf, len, cmd, fnum);
3461 do_cmdline_cmd((char_u *)buf);
3462 vim_free(buf);
3463 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003464 }
3465 else
3466 ch_log(NULL, "terminal job finished");
3467 }
3468
3469 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3470 return FALSE;
3471}
3472
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003473#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3474/*
3475 * If the current window is a terminal in a popup window and the job has
3476 * finished, close the popup window and to back to the previous window.
3477 * Otherwise return FAIL.
3478 */
3479 int
3480may_close_term_popup(void)
3481{
3482 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3483 && !term_job_running(curbuf->b_term))
3484 {
3485 win_T *pwin = curwin;
3486
3487 if (win_valid(prevwin))
3488 win_enter(prevwin, FALSE);
3489 popup_close_with_retval(pwin, 0);
3490 return OK;
3491 }
3492 return FAIL;
3493}
3494#endif
3495
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003496/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003497 * Called when a channel has been closed.
3498 * If this was a channel for a terminal window then finish it up.
3499 */
3500 void
3501term_channel_closed(channel_T *ch)
3502{
3503 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003504 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003505 int did_one = FALSE;
3506
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003507 for (term = first_term; term != NULL; term = next_term)
3508 {
3509 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003510 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003511 {
3512 term->tl_channel_closed = TRUE;
3513 did_one = TRUE;
3514
Bram Moolenaard23a8232018-02-10 18:45:26 +01003515 VIM_CLEAR(term->tl_title);
3516 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003517#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003518 if (term->tl_out_fd != NULL)
3519 {
3520 fclose(term->tl_out_fd);
3521 term->tl_out_fd = NULL;
3522 }
3523#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003524
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003525 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003526 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003527 // Cannot open or close windows now. Can happen when
3528 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003529 term->tl_channel_recently_closed = TRUE;
3530 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003531 }
3532
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003533 if (term_after_channel_closed(term))
3534 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003535 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003536 }
3537
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003538 if (did_one)
3539 {
3540 redraw_statuslines();
3541
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003542 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003543 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003544 typebuf_was_filled = TRUE;
3545
3546 term = curbuf->b_term;
3547 if (term != NULL)
3548 {
3549 if (term->tl_job == ch->ch_job)
3550 maketitle();
3551 update_cursor(term, term->tl_cursor_visible);
3552 }
3553 }
3554}
3555
3556/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003557 * To be called after resetting updating_screen: handle any terminal where the
3558 * channel was closed.
3559 */
3560 void
3561term_check_channel_closed_recently()
3562{
3563 term_T *term;
3564 term_T *next_term;
3565
3566 for (term = first_term; term != NULL; term = next_term)
3567 {
3568 next_term = term->tl_next;
3569 if (term->tl_channel_recently_closed)
3570 {
3571 term->tl_channel_recently_closed = FALSE;
3572 if (term_after_channel_closed(term))
3573 // start over, the list may have changed
3574 next_term = first_term;
3575 }
3576 }
3577}
3578
3579/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003580 * Fill one screen line from a line of the terminal.
3581 * Advances "pos" to past the last column.
3582 */
3583 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003584term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003585 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003586 win_T *wp,
3587 VTermScreen *screen,
3588 VTermPos *pos,
3589 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003590{
3591 int off = screen_get_current_line_off();
3592
3593 for (pos->col = 0; pos->col < max_col; )
3594 {
3595 VTermScreenCell cell;
3596 int c;
3597
3598 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003599 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003600
3601 c = cell.chars[0];
3602 if (c == NUL)
3603 {
3604 ScreenLines[off] = ' ';
3605 if (enc_utf8)
3606 ScreenLinesUC[off] = NUL;
3607 }
3608 else
3609 {
3610 if (enc_utf8)
3611 {
3612 int i;
3613
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003614 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003615 for (i = 0; i < Screen_mco
3616 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3617 {
3618 ScreenLinesC[i][off] = cell.chars[i + 1];
3619 if (cell.chars[i + 1] == 0)
3620 break;
3621 }
3622 if (c >= 0x80 || (Screen_mco > 0
3623 && ScreenLinesC[0][off] != 0))
3624 {
3625 ScreenLines[off] = ' ';
3626 ScreenLinesUC[off] = c;
3627 }
3628 else
3629 {
3630 ScreenLines[off] = c;
3631 ScreenLinesUC[off] = NUL;
3632 }
3633 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003634#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003635 else if (has_mbyte && c >= 0x80)
3636 {
3637 char_u mb[MB_MAXBYTES+1];
3638 WCHAR wc = c;
3639
3640 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3641 (char*)mb, 2, 0, 0) > 1)
3642 {
3643 ScreenLines[off] = mb[0];
3644 ScreenLines[off + 1] = mb[1];
3645 cell.width = mb_ptr2cells(mb);
3646 }
3647 else
3648 ScreenLines[off] = c;
3649 }
3650#endif
3651 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003652 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003653 ScreenLines[off] = c;
3654 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003655 ScreenAttrs[off] = cell2attr(term, wp, cell.attrs, cell.fg, cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003656
3657 ++pos->col;
3658 ++off;
3659 if (cell.width == 2)
3660 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003661 // don't set the second byte to NUL for a DBCS encoding, it
3662 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003663 if (enc_utf8)
3664 {
3665 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003666 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003667 }
3668 else if (!has_mbyte)
3669 {
3670 // Can't show a double-width character with a single-byte
3671 // 'encoding', just use a space.
3672 ScreenLines[off] = ' ';
3673 ScreenAttrs[off] = ScreenAttrs[off - 1];
3674 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003675
3676 ++pos->col;
3677 ++off;
3678 }
3679 }
3680}
3681
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003682#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003683 static void
3684update_system_term(term_T *term)
3685{
3686 VTermPos pos;
3687 VTermScreen *screen;
3688
3689 if (term->tl_vterm == NULL)
3690 return;
3691 screen = vterm_obtain_screen(term->tl_vterm);
3692
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003693 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003694 while (term->tl_toprow > 0
3695 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3696 {
3697 int save_p_more = p_more;
3698
3699 p_more = FALSE;
3700 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003701 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003702 p_more = save_p_more;
3703 --term->tl_toprow;
3704 }
3705
3706 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3707 && pos.row < Rows; ++pos.row)
3708 {
3709 if (pos.row < term->tl_rows)
3710 {
3711 int max_col = MIN(Columns, term->tl_cols);
3712
Bram Moolenaar83d47902020-03-26 20:34:00 +01003713 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003714 }
3715 else
3716 pos.col = 0;
3717
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003718 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003719 }
3720
3721 term->tl_dirty_row_start = MAX_ROW;
3722 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003723}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003724#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003725
3726/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003727 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3728 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003729 * Terminal-Normal mode.
3730 */
3731 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003732term_do_update_window(win_T *wp)
3733{
3734 term_T *term = wp->w_buffer->b_term;
3735
3736 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3737}
3738
3739/*
3740 * Called to update a window that contains an active terminal.
3741 */
3742 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003743term_update_window(win_T *wp)
3744{
3745 term_T *term = wp->w_buffer->b_term;
3746 VTerm *vterm;
3747 VTermScreen *screen;
3748 VTermState *state;
3749 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003750 int rows, cols;
3751 int newrows, newcols;
3752 int minsize;
3753 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003754
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003755 vterm = term->tl_vterm;
3756 screen = vterm_obtain_screen(vterm);
3757 state = vterm_obtain_state(vterm);
3758
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003759 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3760 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003761 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003762 {
3763 term->tl_dirty_row_start = 0;
3764 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003765
3766 if (term->tl_postponed_scroll > 0
3767 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003768 // Scrolling is usually faster than redrawing, when there are only
3769 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003770 term_scroll_up(term, 0, term->tl_postponed_scroll);
3771 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003772 }
3773
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003774 /*
3775 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003776 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003777 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003778 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003779
Bram Moolenaar498c2562018-04-15 23:45:15 +02003780 newrows = 99999;
3781 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003782 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003783 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003784 // Always use curwin, it may be a popup window.
3785 win_T *wwp = twp == NULL ? curwin : twp;
3786
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003787 // When more than one window shows the same terminal, use the
3788 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003789 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003790 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003791 newrows = MIN(newrows, wwp->w_height);
3792 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003793 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003794 if (twp == NULL)
3795 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003796 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003797 if (newrows == 99999 || newcols == 99999)
3798 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003799 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3800 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3801
3802 if (term->tl_rows != newrows || term->tl_cols != newcols)
3803 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003804 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003805 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003806 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003807 newrows);
3808 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003809
3810 // Updating the terminal size will cause the snapshot to be cleared.
3811 // When not in terminal_loop() we need to restore it.
3812 if (term != in_terminal_loop)
3813 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003814 }
3815
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003816 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003817 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003818 position_cursor(wp, &pos, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003819
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003820 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3821 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003822 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003823 if (pos.row < term->tl_rows)
3824 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003825 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003826
Bram Moolenaar83d47902020-03-26 20:34:00 +01003827 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003828 }
3829 else
3830 pos.col = 0;
3831
Bram Moolenaarf118d482018-03-13 13:14:00 +01003832 screen_line(wp->w_winrow + pos.row
3833#ifdef FEAT_MENU
3834 + winbar_height(wp)
3835#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003836 , wp->w_wincol, pos.col, wp->w_width,
3837#ifdef FEAT_PROP_POPUP
3838 popup_is_popup(wp) ? SLF_POPUP :
3839#endif
3840 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003841 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003842 term->tl_dirty_row_start = MAX_ROW;
3843 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003844}
3845
3846/*
3847 * Return TRUE if "wp" is a terminal window where the job has finished.
3848 */
3849 int
3850term_is_finished(buf_T *buf)
3851{
3852 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3853}
3854
3855/*
3856 * Return TRUE if "wp" is a terminal window where the job has finished or we
3857 * are in Terminal-Normal mode, thus we show the buffer contents.
3858 */
3859 int
3860term_show_buffer(buf_T *buf)
3861{
3862 term_T *term = buf->b_term;
3863
3864 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3865}
3866
3867/*
3868 * The current buffer is going to be changed. If there is terminal
3869 * highlighting remove it now.
3870 */
3871 void
3872term_change_in_curbuf(void)
3873{
3874 term_T *term = curbuf->b_term;
3875
3876 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3877 {
3878 free_scrollback(term);
3879 redraw_buf_later(term->tl_buffer, NOT_VALID);
3880
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003881 // The buffer is now like a normal buffer, it cannot be easily
3882 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003883 set_string_option_direct((char_u *)"buftype", -1,
3884 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3885 }
3886}
3887
3888/*
3889 * Get the screen attribute for a position in the buffer.
3890 * Use a negative "col" to get the filler background color.
3891 */
3892 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003893term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003894{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003895 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003896 term_T *term = buf->b_term;
3897 sb_line_T *line;
3898 cellattr_T *cellattr;
3899
3900 if (lnum > term->tl_scrollback.ga_len)
3901 cellattr = &term->tl_default_color;
3902 else
3903 {
3904 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3905 if (col < 0 || col >= line->sb_cols)
3906 cellattr = &line->sb_fill_attr;
3907 else
3908 cellattr = line->sb_cells + col;
3909 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003910 return cell2attr(term, wp, cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003911}
3912
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003913/*
3914 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003915 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003916 */
3917 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003918cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003919{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003920 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3921 if (rgb->index == 0)
3922 rgb->type = VTERM_COLOR_RGB;
3923 else
3924 {
3925 rgb->type = VTERM_COLOR_INDEXED;
3926 --rgb->index;
3927 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003928}
3929
3930/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003931 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003932 */
3933 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003934init_default_colors(term_T *term, win_T *wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003935{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003936 VTermColor *fg, *bg;
3937 int fgval, bgval;
3938 int id;
3939
Bram Moolenaara80faa82020-04-12 19:37:17 +02003940 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003941 term->tl_default_color.width = 1;
3942 fg = &term->tl_default_color.fg;
3943 bg = &term->tl_default_color.bg;
3944
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003945 // Vterm uses a default black background. Set it to white when
3946 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003947 if (*p_bg == 'l')
3948 {
3949 fgval = 0;
3950 bgval = 255;
3951 }
3952 else
3953 {
3954 fgval = 255;
3955 bgval = 0;
3956 }
3957 fg->red = fg->green = fg->blue = fgval;
3958 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003959 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3960 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003961
Bram Moolenaar83d47902020-03-26 20:34:00 +01003962 // The 'wincolor' or the highlight group overrules the defaults.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003963 if (wp != NULL && *wp->w_p_wcr != NUL)
3964 id = syn_name2id(wp->w_p_wcr);
3965 else
Bram Moolenaar83d47902020-03-26 20:34:00 +01003966 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003967
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003968 // Use the actual color for the GUI and when 'termguicolors' is set.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003969#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3970 if (0
3971# ifdef FEAT_GUI
3972 || gui.in_use
3973# endif
3974# ifdef FEAT_TERMGUICOLORS
3975 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003976# ifdef FEAT_VTP
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003977 // Finally get INVALCOLOR on this execution path
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003978 || (!p_tgc && t_colors >= 256)
3979# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003980# endif
3981 )
3982 {
3983 guicolor_T fg_rgb = INVALCOLOR;
3984 guicolor_T bg_rgb = INVALCOLOR;
3985
3986 if (id != 0)
3987 syn_id2colors(id, &fg_rgb, &bg_rgb);
3988
3989# ifdef FEAT_GUI
3990 if (gui.in_use)
3991 {
3992 if (fg_rgb == INVALCOLOR)
3993 fg_rgb = gui.norm_pixel;
3994 if (bg_rgb == INVALCOLOR)
3995 bg_rgb = gui.back_pixel;
3996 }
3997# ifdef FEAT_TERMGUICOLORS
3998 else
3999# endif
4000# endif
4001# ifdef FEAT_TERMGUICOLORS
4002 {
4003 if (fg_rgb == INVALCOLOR)
4004 fg_rgb = cterm_normal_fg_gui_color;
4005 if (bg_rgb == INVALCOLOR)
4006 bg_rgb = cterm_normal_bg_gui_color;
4007 }
4008# endif
4009 if (fg_rgb != INVALCOLOR)
4010 {
4011 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
4012
4013 fg->red = (unsigned)(rgb >> 16);
4014 fg->green = (unsigned)(rgb >> 8) & 255;
4015 fg->blue = (unsigned)rgb & 255;
4016 }
4017 if (bg_rgb != INVALCOLOR)
4018 {
4019 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
4020
4021 bg->red = (unsigned)(rgb >> 16);
4022 bg->green = (unsigned)(rgb >> 8) & 255;
4023 bg->blue = (unsigned)rgb & 255;
4024 }
4025 }
4026 else
4027#endif
4028 if (id != 0 && t_colors >= 16)
4029 {
Bram Moolenaar83d47902020-03-26 20:34:00 +01004030 int cterm_fg = get_default_cterm_fg(term);
4031 int cterm_bg = get_default_cterm_bg(term);
4032
4033 if (cterm_fg >= 0)
4034 cterm_color2vterm(cterm_fg, fg);
4035 if (cterm_bg >= 0)
4036 cterm_color2vterm(cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004037 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004038 else
4039 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004040#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004041 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004042#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004043
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004044 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004045 if (cterm_normal_fg_color > 0)
4046 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004047 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004048# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4049# ifdef VIMDLL
4050 if (!gui.in_use)
4051# endif
4052 {
4053 tmp = fg->red;
4054 fg->red = fg->blue;
4055 fg->blue = tmp;
4056 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004057# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004058 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004059# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004060 else
4061 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004062# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004063
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004064 if (cterm_normal_bg_color > 0)
4065 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004066 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004067# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4068# ifdef VIMDLL
4069 if (!gui.in_use)
4070# endif
4071 {
4072 tmp = fg->red;
4073 fg->red = fg->blue;
4074 fg->blue = tmp;
4075 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004076# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004077 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004078# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004079 else
4080 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004081# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004082 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004083}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004084
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004085#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4086/*
4087 * Set the 16 ANSI colors from array of RGB values
4088 */
4089 static void
4090set_vterm_palette(VTerm *vterm, long_u *rgb)
4091{
4092 int index = 0;
4093 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004094
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004095 for (; index < 16; index++)
4096 {
4097 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004098
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004099 color.red = (unsigned)(rgb[index] >> 16);
4100 color.green = (unsigned)(rgb[index] >> 8) & 255;
4101 color.blue = (unsigned)rgb[index] & 255;
4102 vterm_state_set_palette_color(state, index, &color);
4103 }
4104}
4105
4106/*
4107 * Set the ANSI color palette from a list of colors
4108 */
4109 static int
4110set_ansi_colors_list(VTerm *vterm, list_T *list)
4111{
4112 int n = 0;
4113 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004114 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004115
Bram Moolenaarb0992022020-01-30 14:55:42 +01004116 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004117 {
4118 char_u *color_name;
4119 guicolor_T guicolor;
4120
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004121 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004122 if (color_name == NULL)
4123 return FAIL;
4124
4125 guicolor = GUI_GET_COLOR(color_name);
4126 if (guicolor == INVALCOLOR)
4127 return FAIL;
4128
4129 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4130 }
4131
4132 if (n != 16 || li != NULL)
4133 return FAIL;
4134
4135 set_vterm_palette(vterm, rgb);
4136
4137 return OK;
4138}
4139
4140/*
4141 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4142 */
4143 static void
4144init_vterm_ansi_colors(VTerm *vterm)
4145{
4146 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4147
4148 if (var != NULL
4149 && (var->di_tv.v_type != VAR_LIST
4150 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004151 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004152 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004153 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004154}
4155#endif
4156
Bram Moolenaar52acb112018-03-18 19:20:22 +01004157/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004158 * Handles a "drop" command from the job in the terminal.
4159 * "item" is the file name, "item->li_next" may have options.
4160 */
4161 static void
4162handle_drop_command(listitem_T *item)
4163{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004164 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004165 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004166 int bufnr;
4167 win_T *wp;
4168 tabpage_T *tp;
4169 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004170 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004171
4172 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4173 FOR_ALL_TAB_WINDOWS(tp, wp)
4174 {
4175 if (wp->w_buffer->b_fnum == bufnr)
4176 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004177 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004178 goto_tabpage_win(tp, wp);
4179 return;
4180 }
4181 }
4182
Bram Moolenaara80faa82020-04-12 19:37:17 +02004183 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004184
4185 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4186 && opt_item->li_tv.vval.v_dict != NULL)
4187 {
4188 dict_T *dict = opt_item->li_tv.vval.v_dict;
4189 char_u *p;
4190
Bram Moolenaar8f667172018-12-14 15:38:31 +01004191 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004192 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004193 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004194 if (p != NULL)
4195 {
4196 if (check_ff_value(p) == FAIL)
4197 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4198 else
4199 ea.force_ff = *p;
4200 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004201 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004202 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004203 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004204 if (p != NULL)
4205 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004206 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004207 if (ea.cmd != NULL)
4208 {
4209 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4210 ea.force_enc = 11;
4211 tofree = ea.cmd;
4212 }
4213 }
4214
Bram Moolenaar8f667172018-12-14 15:38:31 +01004215 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004216 if (p != NULL)
4217 get_bad_opt(p, &ea);
4218
4219 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4220 ea.force_bin = FORCE_BIN;
4221 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4222 ea.force_bin = FORCE_BIN;
4223 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4224 ea.force_bin = FORCE_NOBIN;
4225 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4226 ea.force_bin = FORCE_NOBIN;
4227 }
4228
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004229 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004230 if (ea.cmd == NULL)
4231 ea.cmd = (char_u *)"split";
4232 ea.arg = fname;
4233 ea.cmdidx = CMD_split;
4234 ex_splitview(&ea);
4235
4236 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004237}
4238
4239/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004240 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4241 */
4242 static int
4243is_permitted_term_api(char_u *func, char_u *pat)
4244{
4245 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4246}
4247
4248/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004249 * Handles a function call from the job running in a terminal.
4250 * "item" is the function name, "item->li_next" has the arguments.
4251 */
4252 static void
4253handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4254{
4255 char_u *func;
4256 typval_T argvars[2];
4257 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004258 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004259
4260 if (item->li_next == NULL)
4261 {
4262 ch_log(channel, "Missing function arguments for call");
4263 return;
4264 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004265 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004266
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004267 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004268 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004269 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004270 return;
4271 }
4272
4273 argvars[0].v_type = VAR_NUMBER;
4274 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4275 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004276 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004277 funcexe.firstline = 1L;
4278 funcexe.lastline = 1L;
4279 funcexe.evaluate = TRUE;
4280 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004281 {
4282 clear_tv(&rettv);
4283 ch_log(channel, "Function %s called", func);
4284 }
4285 else
4286 ch_log(channel, "Calling function %s failed", func);
4287}
4288
4289/*
4290 * Called by libvterm when it cannot recognize an OSC sequence.
4291 * We recognize a terminal API command.
4292 */
4293 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004294parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004295{
4296 term_T *term = (term_T *)user;
4297 js_read_T reader;
4298 typval_T tv;
4299 channel_T *channel = term->tl_job == NULL ? NULL
4300 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004301 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004302
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004303 // We recognize only OSC 5 1 ; {command}
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004304 if (command != 51)
4305 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004306
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004307 // Concatenate what was received until the final piece is found.
4308 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4309 {
4310 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004311 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004312 }
4313 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004314 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004315 if (!frag.final)
4316 return 1;
4317
4318 ((char *)gap->ga_data)[gap->ga_len] = 0;
4319 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004320 reader.js_fill = NULL;
4321 reader.js_used = 0;
4322 if (json_decode(&reader, &tv, 0) == OK
4323 && tv.v_type == VAR_LIST
4324 && tv.vval.v_list != NULL)
4325 {
4326 listitem_T *item = tv.vval.v_list->lv_first;
4327
4328 if (item == NULL)
4329 ch_log(channel, "Missing command");
4330 else
4331 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004332 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004333
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004334 // Make sure an invoked command doesn't delete the buffer (and the
4335 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004336 ++term->tl_buffer->b_locked;
4337
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004338 item = item->li_next;
4339 if (item == NULL)
4340 ch_log(channel, "Missing argument for %s", cmd);
4341 else if (STRCMP(cmd, "drop") == 0)
4342 handle_drop_command(item);
4343 else if (STRCMP(cmd, "call") == 0)
4344 handle_call_command(term, channel, item);
4345 else
4346 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004347 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004348 }
4349 }
4350 else
4351 ch_log(channel, "Invalid JSON received");
4352
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004353 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004354 clear_tv(&tv);
4355 return 1;
4356}
4357
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004358/*
4359 * Called by libvterm when it cannot recognize a CSI sequence.
4360 * We recognize the window position report.
4361 */
4362 static int
4363parse_csi(
4364 const char *leader UNUSED,
4365 const long args[],
4366 int argcount,
4367 const char *intermed UNUSED,
4368 char command,
4369 void *user)
4370{
4371 term_T *term = (term_T *)user;
4372 char buf[100];
4373 int len;
4374 int x = 0;
4375 int y = 0;
4376 win_T *wp;
4377
4378 // We recognize only CSI 13 t
4379 if (command != 't' || argcount != 1 || args[0] != 13)
4380 return 0; // not handled
4381
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004382 // When getting the window position is not possible or it fails it results
4383 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004384#if defined(FEAT_GUI) \
4385 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4386 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004387 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004388#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004389
4390 FOR_ALL_WINDOWS(wp)
4391 if (wp->w_buffer == term->tl_buffer)
4392 break;
4393 if (wp != NULL)
4394 {
4395#ifdef FEAT_GUI
4396 if (gui.in_use)
4397 {
4398 x += wp->w_wincol * gui.char_width;
4399 y += W_WINROW(wp) * gui.char_height;
4400 }
4401 else
4402#endif
4403 {
4404 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004405 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004406 x += wp->w_wincol * 7;
4407 y += W_WINROW(wp) * 10;
4408 }
4409 }
4410
4411 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4412 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4413 (char_u *)buf, len, NULL);
4414 return 1;
4415}
4416
Bram Moolenaard8637282020-05-20 18:41:41 +02004417static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004418 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004419 parse_csi, // csi
4420 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004421 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004422};
4423
4424/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004425 * Use Vim's allocation functions for vterm so profiling works.
4426 */
4427 static void *
4428vterm_malloc(size_t size, void *data UNUSED)
4429{
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004430 return alloc_clear(size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004431}
4432
4433 static void
4434vterm_memfree(void *ptr, void *data UNUSED)
4435{
4436 vim_free(ptr);
4437}
4438
4439static VTermAllocatorFunctions vterm_allocator = {
4440 &vterm_malloc,
4441 &vterm_memfree
4442};
4443
4444/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004445 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004446 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004447 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004448 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004449create_vterm(term_T *term, int rows, int cols)
4450{
4451 VTerm *vterm;
4452 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004453 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004454 VTermValue value;
4455
Bram Moolenaar756ef112018-04-10 12:04:27 +02004456 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004457 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004458 if (vterm == NULL)
4459 return FAIL;
4460
4461 // Allocate screen and state here, so we can bail out if that fails.
4462 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004463 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004464 if (state == NULL || screen == NULL)
4465 {
4466 vterm_free(vterm);
4467 return FAIL;
4468 }
4469
Bram Moolenaar52acb112018-03-18 19:20:22 +01004470 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004471 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004472 vterm_set_utf8(vterm, 1);
4473
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004474 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004475
4476 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004477 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004478 &term->tl_default_color.fg,
4479 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004480
Bram Moolenaar9e587872019-05-13 20:27:23 +02004481 if (t_colors < 16)
4482 // Less than 16 colors: assume that bold means using a bright color for
4483 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004484 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4485
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004486 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004487 vterm_screen_reset(screen, 1 /* hard */);
4488
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004489 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004490 vterm_screen_enable_altscreen(screen, 1);
4491
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004492 // For unix do not use a blinking cursor. In an xterm this causes the
4493 // cursor to blink if it's blinking in the xterm.
4494 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004495#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004496 if (GetCaretBlinkTime() == INFINITE)
4497 value.boolean = 0;
4498 else
4499 value.boolean = 1;
4500#else
4501 value.boolean = 0;
4502#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004503 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004504 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004505
4506 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004507}
4508
4509/*
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004510 * Called when 'wincolor' was set.
4511 */
4512 void
4513term_update_colors(void)
4514{
4515 term_T *term = curwin->w_buffer->b_term;
4516
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01004517 if (term->tl_vterm == NULL)
4518 return;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004519 init_default_colors(term, curwin);
4520 vterm_state_set_default_colors(
4521 vterm_obtain_state(term->tl_vterm),
4522 &term->tl_default_color.fg,
4523 &term->tl_default_color.bg);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01004524
4525 redraw_later(NOT_VALID);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004526}
4527
4528/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004529 * Return the text to show for the buffer name and status.
4530 */
4531 char_u *
4532term_get_status_text(term_T *term)
4533{
4534 if (term->tl_status_text == NULL)
4535 {
4536 char_u *txt;
4537 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004538 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004539
4540 if (term->tl_normal_mode)
4541 {
4542 if (term_job_running(term))
4543 txt = (char_u *)_("Terminal");
4544 else
4545 txt = (char_u *)_("Terminal-finished");
4546 }
4547 else if (term->tl_title != NULL)
4548 txt = term->tl_title;
4549 else if (term_none_open(term))
4550 txt = (char_u *)_("active");
4551 else if (term_job_running(term))
4552 txt = (char_u *)_("running");
4553 else
4554 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004555 fname = buf_get_fname(term->tl_buffer);
4556 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004557 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004558 if (term->tl_status_text != NULL)
4559 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004560 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004561 }
4562 return term->tl_status_text;
4563}
4564
4565/*
4566 * Mark references in jobs of terminals.
4567 */
4568 int
4569set_ref_in_term(int copyID)
4570{
4571 int abort = FALSE;
4572 term_T *term;
4573 typval_T tv;
4574
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004575 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004576 if (term->tl_job != NULL)
4577 {
4578 tv.v_type = VAR_JOB;
4579 tv.vval.v_job = term->tl_job;
4580 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4581 }
4582 return abort;
4583}
4584
4585/*
4586 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004587 * Returns NULL when the buffer is not for a terminal window and logs a message
4588 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004589 */
4590 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004591term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004592{
4593 buf_T *buf;
4594
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004595 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004596 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004597 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004598 --emsg_off;
4599 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004600 {
4601 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004602 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004603 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004604 return buf;
4605}
4606
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004607 static void
4608clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004609{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004610 CLEAR_FIELD(*cell);
4611 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4612 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004613}
4614
4615 static void
4616dump_term_color(FILE *fd, VTermColor *color)
4617{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004618 int index;
4619
4620 if (VTERM_COLOR_IS_INDEXED(color))
4621 index = color->index + 1;
4622 else if (color->type == 0)
4623 // use RGB values
4624 index = 255;
4625 else
4626 // default color
4627 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004628 fprintf(fd, "%02x%02x%02x%d",
4629 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004630 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004631}
4632
4633/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004634 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004635 *
4636 * Each screen cell in full is:
4637 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4638 * {characters} is a space for an empty cell
4639 * For a double-width character "+" is changed to "*" and the next cell is
4640 * skipped.
4641 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4642 * when "&" use the same as the previous cell.
4643 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4644 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4645 * {color-idx} is a number from 0 to 255
4646 *
4647 * Screen cell with same width, attributes and color as the previous one:
4648 * |{characters}
4649 *
4650 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4651 *
4652 * Repeating the previous screen cell:
4653 * @{count}
4654 */
4655 void
4656f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4657{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004658 buf_T *buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004659 term_T *term;
4660 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004661 int max_height = 0;
4662 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004663 stat_T st;
4664 FILE *fd;
4665 VTermPos pos;
4666 VTermScreen *screen;
4667 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004668 VTermState *state;
4669 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004670
4671 if (check_restricted() || check_secure())
4672 return;
4673 if (buf == NULL)
4674 return;
4675 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004676 if (term->tl_vterm == NULL)
4677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004678 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004679 return;
4680 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004681
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004682 if (argvars[2].v_type != VAR_UNKNOWN)
4683 {
4684 dict_T *d;
4685
4686 if (argvars[2].v_type != VAR_DICT)
4687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004688 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004689 return;
4690 }
4691 d = argvars[2].vval.v_dict;
4692 if (d != NULL)
4693 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004694 max_height = dict_get_number(d, (char_u *)"rows");
4695 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004696 }
4697 }
4698
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004699 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004700 if (fname == NULL)
4701 return;
4702 if (mch_stat((char *)fname, &st) >= 0)
4703 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004704 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004705 return;
4706 }
4707
Bram Moolenaard96ff162018-02-18 22:13:29 +01004708 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4709 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004710 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004711 return;
4712 }
4713
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004714 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004715
4716 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004717 state = vterm_obtain_state(term->tl_vterm);
4718 vterm_state_get_cursorpos(state, &cursor_pos);
4719
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004720 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4721 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004722 {
4723 int repeat = 0;
4724
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004725 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4726 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004727 {
4728 VTermScreenCell cell;
4729 int same_attr;
4730 int same_chars = TRUE;
4731 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004732 int is_cursor_pos = (pos.col == cursor_pos.col
4733 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004734
4735 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004736 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004737
4738 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4739 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004740 int c = cell.chars[i];
4741 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004742 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004743
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004744 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004745 if (i == 0)
4746 {
4747 c = (c == NUL) ? ' ' : c;
4748 pc = (pc == NUL) ? ' ' : pc;
4749 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004750 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004751 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004752 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004753 break;
4754 }
4755 same_attr = vtermAttr2hl(cell.attrs)
4756 == vtermAttr2hl(prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004757 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4758 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004759 if (same_chars && cell.width == prev_cell.width && same_attr
4760 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004761 {
4762 ++repeat;
4763 }
4764 else
4765 {
4766 if (repeat > 0)
4767 {
4768 fprintf(fd, "@%d", repeat);
4769 repeat = 0;
4770 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004771 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004772
4773 if (cell.chars[0] == NUL)
4774 fputs(" ", fd);
4775 else
4776 {
4777 char_u charbuf[10];
4778 int len;
4779
4780 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4781 && cell.chars[i] != NUL; ++i)
4782 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004783 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004784 fwrite(charbuf, len, 1, fd);
4785 }
4786 }
4787
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004788 // When only the characters differ we don't write anything, the
4789 // following "|", "@" or NL will indicate using the same
4790 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004791 if (cell.width != prev_cell.width || !same_attr)
4792 {
4793 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004794 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004795 else
4796 fputs("+", fd);
4797
4798 if (same_attr)
4799 {
4800 fputs("&", fd);
4801 }
4802 else
4803 {
4804 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004805 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004806 fputs("&", fd);
4807 else
4808 {
4809 fputs("#", fd);
4810 dump_term_color(fd, &cell.fg);
4811 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004812 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004813 fputs("&", fd);
4814 else
4815 {
4816 fputs("#", fd);
4817 dump_term_color(fd, &cell.bg);
4818 }
4819 }
4820 }
4821
4822 prev_cell = cell;
4823 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004824
4825 if (cell.width == 2)
4826 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004827 }
4828 if (repeat > 0)
4829 fprintf(fd, "@%d", repeat);
4830 fputs("\n", fd);
4831 }
4832
4833 fclose(fd);
4834}
4835
4836/*
4837 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4838 */
4839 static void
4840dump_is_corrupt(garray_T *gap)
4841{
4842 ga_concat(gap, (char_u *)"CORRUPT");
4843}
4844
4845 static void
4846append_cell(garray_T *gap, cellattr_T *cell)
4847{
4848 if (ga_grow(gap, 1) == OK)
4849 {
4850 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4851 ++gap->ga_len;
4852 }
4853}
4854
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004855 static void
4856clear_cellattr(cellattr_T *cell)
4857{
4858 CLEAR_FIELD(*cell);
4859 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4860 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4861}
4862
Bram Moolenaard96ff162018-02-18 22:13:29 +01004863/*
4864 * Read the dump file from "fd" and append lines to the current buffer.
4865 * Return the cell width of the longest line.
4866 */
4867 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004868read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004869{
4870 int c;
4871 garray_T ga_text;
4872 garray_T ga_cell;
4873 char_u *prev_char = NULL;
4874 int attr = 0;
4875 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004876 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004877 term_T *term = curbuf->b_term;
4878 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004879 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004880
4881 ga_init2(&ga_text, 1, 90);
4882 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004883 clear_cellattr(&cell);
4884 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004885 cursor_pos->row = -1;
4886 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004887
4888 c = fgetc(fd);
4889 for (;;)
4890 {
4891 if (c == EOF)
4892 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004893 if (c == '\r')
4894 {
4895 // DOS line endings? Ignore.
4896 c = fgetc(fd);
4897 }
4898 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004899 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004900 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004901 if (ga_text.ga_data == NULL)
4902 dump_is_corrupt(&ga_text);
4903 if (ga_grow(&term->tl_scrollback, 1) == OK)
4904 {
4905 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
4906 + term->tl_scrollback.ga_len;
4907
4908 if (max_cells < ga_cell.ga_len)
4909 max_cells = ga_cell.ga_len;
4910 line->sb_cols = ga_cell.ga_len;
4911 line->sb_cells = ga_cell.ga_data;
4912 line->sb_fill_attr = term->tl_default_color;
4913 ++term->tl_scrollback.ga_len;
4914 ga_init(&ga_cell);
4915
4916 ga_append(&ga_text, NUL);
4917 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4918 ga_text.ga_len, FALSE);
4919 }
4920 else
4921 ga_clear(&ga_cell);
4922 ga_text.ga_len = 0;
4923
4924 c = fgetc(fd);
4925 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004926 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004927 {
4928 int prev_len = ga_text.ga_len;
4929
Bram Moolenaar9271d052018-02-25 21:39:46 +01004930 if (c == '>')
4931 {
4932 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004933 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01004934 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
4935 cursor_pos->col = ga_cell.ga_len;
4936 }
4937
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004938 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01004939 c = fgetc(fd);
4940 if (c != EOF)
4941 ga_append(&ga_text, c);
4942 for (;;)
4943 {
4944 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004945 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01004946 || c == EOF || c == '\n')
4947 break;
4948 ga_append(&ga_text, c);
4949 }
4950
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004951 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01004952 vim_free(prev_char);
4953 if (ga_text.ga_data != NULL)
4954 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
4955 ga_text.ga_len - prev_len);
4956
Bram Moolenaar9271d052018-02-25 21:39:46 +01004957 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004958 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004959 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01004960 }
4961 else if (c == '+' || c == '*')
4962 {
4963 int is_bg;
4964
4965 cell.width = c == '+' ? 1 : 2;
4966
4967 c = fgetc(fd);
4968 if (c == '&')
4969 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004970 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01004971 c = fgetc(fd);
4972 }
4973 else if (isdigit(c))
4974 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004975 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01004976 attr = 0;
4977 while (isdigit(c))
4978 {
4979 attr = attr * 10 + (c - '0');
4980 c = fgetc(fd);
4981 }
4982 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004983
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004984 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004985 for (is_bg = 0; is_bg <= 1; ++is_bg)
4986 {
4987 if (c == '&')
4988 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004989 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004990 c = fgetc(fd);
4991 }
4992 else if (c == '#')
4993 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004994 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004995
4996 c = fgetc(fd);
4997 red = hex2nr(c);
4998 c = fgetc(fd);
4999 red = (red << 4) + hex2nr(c);
5000 c = fgetc(fd);
5001 green = hex2nr(c);
5002 c = fgetc(fd);
5003 green = (green << 4) + hex2nr(c);
5004 c = fgetc(fd);
5005 blue = hex2nr(c);
5006 c = fgetc(fd);
5007 blue = (blue << 4) + hex2nr(c);
5008 c = fgetc(fd);
5009 if (!isdigit(c))
5010 dump_is_corrupt(&ga_text);
5011 while (isdigit(c))
5012 {
5013 index = index * 10 + (c - '0');
5014 c = fgetc(fd);
5015 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005016 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005017 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005018 type = VTERM_COLOR_RGB;
5019 if (index == 0)
5020 {
5021 if (is_bg)
5022 type |= VTERM_COLOR_DEFAULT_BG;
5023 else
5024 type |= VTERM_COLOR_DEFAULT_FG;
5025 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005026 }
5027 else
5028 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005029 type = VTERM_COLOR_INDEXED;
5030 index -= 1;
5031 }
5032 if (is_bg)
5033 {
5034 cell.bg.type = type;
5035 cell.bg.red = red;
5036 cell.bg.green = green;
5037 cell.bg.blue = blue;
5038 cell.bg.index = index;
5039 }
5040 else
5041 {
5042 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005043 cell.fg.red = red;
5044 cell.fg.green = green;
5045 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005046 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005047 }
5048 }
5049 else
5050 dump_is_corrupt(&ga_text);
5051 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005052 }
5053 else
5054 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005055 }
5056 else
5057 dump_is_corrupt(&ga_text);
5058
5059 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005060 if (cell.width == 2)
5061 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005062 }
5063 else if (c == '@')
5064 {
5065 if (prev_char == NULL)
5066 dump_is_corrupt(&ga_text);
5067 else
5068 {
5069 int count = 0;
5070
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005071 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005072 for (;;)
5073 {
5074 c = fgetc(fd);
5075 if (!isdigit(c))
5076 break;
5077 count = count * 10 + (c - '0');
5078 }
5079
5080 while (count-- > 0)
5081 {
5082 ga_concat(&ga_text, prev_char);
5083 append_cell(&ga_cell, &cell);
5084 }
5085 }
5086 }
5087 else
5088 {
5089 dump_is_corrupt(&ga_text);
5090 c = fgetc(fd);
5091 }
5092 }
5093
5094 if (ga_text.ga_len > 0)
5095 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005096 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005097 dump_is_corrupt(&ga_text);
5098 ga_append(&ga_text, NUL);
5099 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5100 ga_text.ga_len, FALSE);
5101 }
5102
5103 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005104 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005105 vim_free(prev_char);
5106
5107 return max_cells;
5108}
5109
5110/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005111 * Return an allocated string with at least "text_width" "=" characters and
5112 * "fname" inserted in the middle.
5113 */
5114 static char_u *
5115get_separator(int text_width, char_u *fname)
5116{
5117 int width = MAX(text_width, curwin->w_width);
5118 char_u *textline;
5119 int fname_size;
5120 char_u *p = fname;
5121 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005122 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005123
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005124 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005125 if (textline == NULL)
5126 return NULL;
5127
5128 fname_size = vim_strsize(fname);
5129 if (fname_size < width - 8)
5130 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005131 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005132 width = MAX(text_width, fname_size + 8);
5133 }
5134 else if (fname_size > width - 8)
5135 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005136 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005137 p = gettail(fname);
5138 fname_size = vim_strsize(p);
5139 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005140 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005141 while (fname_size > width - 8)
5142 {
5143 p += (*mb_ptr2len)(p);
5144 fname_size = vim_strsize(p);
5145 }
5146
5147 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5148 textline[i] = '=';
5149 textline[i++] = ' ';
5150
5151 STRCPY(textline + i, p);
5152 off = STRLEN(textline);
5153 textline[off] = ' ';
5154 for (i = 1; i < (width - fname_size) / 2; ++i)
5155 textline[off + i] = '=';
5156 textline[off + i] = NUL;
5157
5158 return textline;
5159}
5160
5161/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005162 * Common for "term_dumpdiff()" and "term_dumpload()".
5163 */
5164 static void
5165term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5166{
5167 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005168 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005169 char_u buf1[NUMBUFLEN];
5170 char_u buf2[NUMBUFLEN];
5171 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005172 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005173 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005174 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005175 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005176 char_u *textline = NULL;
5177
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005178 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005179 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005180 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005181 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005182 if (fname1 == NULL || (do_diff && fname2 == NULL))
5183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005184 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005185 return;
5186 }
5187 fd1 = mch_fopen((char *)fname1, READBIN);
5188 if (fd1 == NULL)
5189 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005190 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005191 return;
5192 }
5193 if (do_diff)
5194 {
5195 fd2 = mch_fopen((char *)fname2, READBIN);
5196 if (fd2 == NULL)
5197 {
5198 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005199 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005200 return;
5201 }
5202 }
5203
5204 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005205 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5206 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5207 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5208 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5209 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005210
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005211 if (opt.jo_term_name == NULL)
5212 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005213 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005214
Bram Moolenaar51e14382019-05-25 20:21:28 +02005215 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005216 if (fname_tofree != NULL)
5217 {
5218 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5219 opt.jo_term_name = fname_tofree;
5220 }
5221 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005222
Bram Moolenaar87abab92019-06-03 21:14:59 +02005223 if (opt.jo_bufnr_buf != NULL)
5224 {
5225 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5226
5227 // With "bufnr" argument: enter the window with this buffer and make it
5228 // empty.
5229 if (wp == NULL)
5230 semsg(_(e_invarg2), "bufnr");
5231 else
5232 {
5233 buf = curbuf;
5234 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005235 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005236 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005237 redraw_later(NOT_VALID);
5238 }
5239 }
5240 else
5241 // Create a new terminal window.
5242 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5243
Bram Moolenaard96ff162018-02-18 22:13:29 +01005244 if (buf != NULL && buf->b_term != NULL)
5245 {
5246 int i;
5247 linenr_T bot_lnum;
5248 linenr_T lnum;
5249 term_T *term = buf->b_term;
5250 int width;
5251 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005252 VTermPos cursor_pos1;
5253 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005254
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005255 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005256
Bram Moolenaard96ff162018-02-18 22:13:29 +01005257 rettv->vval.v_number = buf->b_fnum;
5258
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005259 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005260 width = read_dump_file(fd1, &cursor_pos1);
5261
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005262 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005263 if (cursor_pos1.row >= 0)
5264 {
5265 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5266 coladvance(cursor_pos1.col);
5267 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005268
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005269 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005270 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005271
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005272 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005273 if (!do_diff)
5274 goto theend;
5275
5276 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5277
Bram Moolenaar4a696342018-04-05 18:45:26 +02005278 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005279 if (textline == NULL)
5280 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005281 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5282 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5283 vim_free(textline);
5284
5285 textline = get_separator(width, fname2);
5286 if (textline == NULL)
5287 goto theend;
5288 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5289 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005290 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005291
5292 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005293 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005294 if (width2 > width)
5295 {
5296 vim_free(textline);
5297 textline = alloc(width2 + 1);
5298 if (textline == NULL)
5299 goto theend;
5300 width = width2;
5301 textline[width] = NUL;
5302 }
5303 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5304
5305 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5306 {
5307 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5308 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005309 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005310 for (i = 0; i < width; ++i)
5311 textline[i] = '-';
5312 }
5313 else
5314 {
5315 char_u *line1;
5316 char_u *line2;
5317 char_u *p1;
5318 char_u *p2;
5319 int col;
5320 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5321 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5322 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5323 ->sb_cells;
5324
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005325 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005326 line1 = vim_strsave(ml_get(lnum));
5327 if (line1 == NULL)
5328 break;
5329 p1 = line1;
5330
5331 line2 = ml_get(lnum + bot_lnum);
5332 p2 = line2;
5333 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5334 {
5335 int len1 = utfc_ptr2len(p1);
5336 int len2 = utfc_ptr2len(p2);
5337
5338 textline[col] = ' ';
5339 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005340 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005341 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005342 else if (lnum == cursor_pos1.row + 1
5343 && col == cursor_pos1.col
5344 && (cursor_pos1.row != cursor_pos2.row
5345 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005346 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005347 textline[col] = '>';
5348 else if (lnum == cursor_pos2.row + 1
5349 && col == cursor_pos2.col
5350 && (cursor_pos1.row != cursor_pos2.row
5351 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005352 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005353 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005354 else if (cellattr1 != NULL && cellattr2 != NULL)
5355 {
5356 if ((cellattr1 + col)->width
5357 != (cellattr2 + col)->width)
5358 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005359 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005360 &(cellattr2 + col)->fg))
5361 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005362 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005363 &(cellattr2 + col)->bg))
5364 textline[col] = 'b';
5365 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5366 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5367 textline[col] = 'a';
5368 }
5369 p1 += len1;
5370 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005371 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005372 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005373
5374 while (col < width)
5375 {
5376 if (*p1 == NUL && *p2 == NUL)
5377 textline[col] = '?';
5378 else if (*p1 == NUL)
5379 {
5380 textline[col] = '+';
5381 p2 += utfc_ptr2len(p2);
5382 }
5383 else
5384 {
5385 textline[col] = '-';
5386 p1 += utfc_ptr2len(p1);
5387 }
5388 ++col;
5389 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005390
5391 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005392 }
5393 if (add_empty_scrollback(term, &term->tl_default_color,
5394 term->tl_top_diff_rows) == OK)
5395 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5396 ++bot_lnum;
5397 }
5398
5399 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5400 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005401 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005402 for (i = 0; i < width; ++i)
5403 textline[i] = '+';
5404 if (add_empty_scrollback(term, &term->tl_default_color,
5405 term->tl_top_diff_rows) == OK)
5406 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5407 ++lnum;
5408 ++bot_lnum;
5409 }
5410
5411 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005412
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005413 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005414 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005415 }
5416
5417theend:
5418 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005419 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005420 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005421 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005422 fclose(fd2);
5423}
5424
5425/*
5426 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5427 * bottom files.
5428 * Return FAIL when this is not possible.
5429 */
5430 int
5431term_swap_diff()
5432{
5433 term_T *term = curbuf->b_term;
5434 linenr_T line_count;
5435 linenr_T top_rows;
5436 linenr_T bot_rows;
5437 linenr_T bot_start;
5438 linenr_T lnum;
5439 char_u *p;
5440 sb_line_T *sb_line;
5441
5442 if (term == NULL
5443 || !term_is_finished(curbuf)
5444 || term->tl_top_diff_rows == 0
5445 || term->tl_scrollback.ga_len == 0)
5446 return FAIL;
5447
5448 line_count = curbuf->b_ml.ml_line_count;
5449 top_rows = term->tl_top_diff_rows;
5450 bot_rows = term->tl_bot_diff_rows;
5451 bot_start = line_count - bot_rows;
5452 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5453
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005454 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005455 for (lnum = 1; lnum <= top_rows; ++lnum)
5456 {
5457 p = vim_strsave(ml_get(1));
5458 if (p == NULL)
5459 return OK;
5460 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005461 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005462 vim_free(p);
5463 }
5464
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005465 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005466 for (lnum = 1; lnum <= bot_rows; ++lnum)
5467 {
5468 p = vim_strsave(ml_get(bot_start + lnum));
5469 if (p == NULL)
5470 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005471 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005472 ml_append(lnum - 1, p, 0, FALSE);
5473 vim_free(p);
5474 }
5475
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005476 // move top title to bottom
5477 p = vim_strsave(ml_get(bot_rows + 1));
5478 if (p == NULL)
5479 return OK;
5480 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005481 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005482 vim_free(p);
5483
5484 // move bottom title to top
5485 p = vim_strsave(ml_get(line_count - top_rows));
5486 if (p == NULL)
5487 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005488 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005489 ml_append(bot_rows, p, 0, FALSE);
5490 vim_free(p);
5491
Bram Moolenaard96ff162018-02-18 22:13:29 +01005492 if (top_rows == bot_rows)
5493 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005494 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005495 for (lnum = 0; lnum < top_rows; ++lnum)
5496 {
5497 sb_line_T temp;
5498
5499 temp = *(sb_line + lnum);
5500 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5501 *(sb_line + bot_start + lnum) = temp;
5502 }
5503 }
5504 else
5505 {
5506 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005507 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005508
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005509 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005510 if (temp != NULL)
5511 {
5512 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5513 mch_memmove(term->tl_scrollback.ga_data,
5514 temp + bot_start,
5515 sizeof(sb_line_T) * bot_rows);
5516 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5517 temp + top_rows,
5518 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5519 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5520 + line_count - top_rows,
5521 temp,
5522 sizeof(sb_line_T) * top_rows);
5523 vim_free(temp);
5524 }
5525 }
5526
5527 term->tl_top_diff_rows = bot_rows;
5528 term->tl_bot_diff_rows = top_rows;
5529
5530 update_screen(NOT_VALID);
5531 return OK;
5532}
5533
5534/*
5535 * "term_dumpdiff(filename, filename, options)" function
5536 */
5537 void
5538f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5539{
5540 term_load_dump(argvars, rettv, TRUE);
5541}
5542
5543/*
5544 * "term_dumpload(filename, options)" function
5545 */
5546 void
5547f_term_dumpload(typval_T *argvars, typval_T *rettv)
5548{
5549 term_load_dump(argvars, rettv, FALSE);
5550}
5551
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005552/*
5553 * "term_getaltscreen(buf)" function
5554 */
5555 void
5556f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5557{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005558 buf_T *buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005559
5560 if (buf == NULL)
5561 return;
5562 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5563}
5564
5565/*
5566 * "term_getattr(attr, name)" function
5567 */
5568 void
5569f_term_getattr(typval_T *argvars, typval_T *rettv)
5570{
5571 int attr;
5572 size_t i;
5573 char_u *name;
5574
5575 static struct {
5576 char *name;
5577 int attr;
5578 } attrs[] = {
5579 {"bold", HL_BOLD},
5580 {"italic", HL_ITALIC},
5581 {"underline", HL_UNDERLINE},
5582 {"strike", HL_STRIKETHROUGH},
5583 {"reverse", HL_INVERSE},
5584 };
5585
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005586 attr = tv_get_number(&argvars[0]);
5587 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005588 if (name == NULL)
5589 return;
5590
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005591 if (attr > HL_ALL)
5592 attr = syn_attr2attr(attr);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005593 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
5594 if (STRCMP(name, attrs[i].name) == 0)
5595 {
5596 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5597 break;
5598 }
5599}
5600
5601/*
5602 * "term_getcursor(buf)" function
5603 */
5604 void
5605f_term_getcursor(typval_T *argvars, typval_T *rettv)
5606{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005607 buf_T *buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005608 term_T *term;
5609 list_T *l;
5610 dict_T *d;
5611
5612 if (rettv_list_alloc(rettv) == FAIL)
5613 return;
5614 if (buf == NULL)
5615 return;
5616 term = buf->b_term;
5617
5618 l = rettv->vval.v_list;
5619 list_append_number(l, term->tl_cursor_pos.row + 1);
5620 list_append_number(l, term->tl_cursor_pos.col + 1);
5621
5622 d = dict_alloc();
5623 if (d != NULL)
5624 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005625 dict_add_number(d, "visible", term->tl_cursor_visible);
5626 dict_add_number(d, "blink", blink_state_is_inverted()
5627 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5628 dict_add_number(d, "shape", term->tl_cursor_shape);
5629 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005630 list_append_dict(l, d);
5631 }
5632}
5633
5634/*
5635 * "term_getjob(buf)" function
5636 */
5637 void
5638f_term_getjob(typval_T *argvars, typval_T *rettv)
5639{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005640 buf_T *buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005641
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005642 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005643 {
5644 rettv->v_type = VAR_SPECIAL;
5645 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005646 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005647 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005648
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005649 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005650 rettv->vval.v_job = buf->b_term->tl_job;
5651 if (rettv->vval.v_job != NULL)
5652 ++rettv->vval.v_job->jv_refcount;
5653}
5654
5655 static int
5656get_row_number(typval_T *tv, term_T *term)
5657{
5658 if (tv->v_type == VAR_STRING
5659 && tv->vval.v_string != NULL
5660 && STRCMP(tv->vval.v_string, ".") == 0)
5661 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005662 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005663}
5664
5665/*
5666 * "term_getline(buf, row)" function
5667 */
5668 void
5669f_term_getline(typval_T *argvars, typval_T *rettv)
5670{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005671 buf_T *buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005672 term_T *term;
5673 int row;
5674
5675 rettv->v_type = VAR_STRING;
5676 if (buf == NULL)
5677 return;
5678 term = buf->b_term;
5679 row = get_row_number(&argvars[1], term);
5680
5681 if (term->tl_vterm == NULL)
5682 {
5683 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5684
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005685 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005686 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5687 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5688 }
5689 else
5690 {
5691 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5692 VTermRect rect;
5693 int len;
5694 char_u *p;
5695
5696 if (row < 0 || row >= term->tl_rows)
5697 return;
5698 len = term->tl_cols * MB_MAXBYTES + 1;
5699 p = alloc(len);
5700 if (p == NULL)
5701 return;
5702 rettv->vval.v_string = p;
5703
5704 rect.start_col = 0;
5705 rect.end_col = term->tl_cols;
5706 rect.start_row = row;
5707 rect.end_row = row + 1;
5708 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5709 }
5710}
5711
5712/*
5713 * "term_getscrolled(buf)" function
5714 */
5715 void
5716f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5717{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005718 buf_T *buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005719
5720 if (buf == NULL)
5721 return;
5722 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5723}
5724
5725/*
5726 * "term_getsize(buf)" function
5727 */
5728 void
5729f_term_getsize(typval_T *argvars, typval_T *rettv)
5730{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005731 buf_T *buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005732 list_T *l;
5733
5734 if (rettv_list_alloc(rettv) == FAIL)
5735 return;
5736 if (buf == NULL)
5737 return;
5738
5739 l = rettv->vval.v_list;
5740 list_append_number(l, buf->b_term->tl_rows);
5741 list_append_number(l, buf->b_term->tl_cols);
5742}
5743
5744/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005745 * "term_setsize(buf, rows, cols)" function
5746 */
5747 void
5748f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5749{
5750 buf_T *buf = term_get_buf(argvars, "term_setsize()");
5751 term_T *term;
5752 varnumber_T rows, cols;
5753
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005754 if (buf == NULL)
5755 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005756 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005757 return;
5758 }
5759 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005760 return;
5761 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005762 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005763 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005764 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005765 cols = cols <= 0 ? term->tl_cols : cols;
5766 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005767 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005768
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005769 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005770 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5771 term_report_winsize(term, term->tl_rows, term->tl_cols);
5772}
5773
5774/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005775 * "term_getstatus(buf)" function
5776 */
5777 void
5778f_term_getstatus(typval_T *argvars, typval_T *rettv)
5779{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005780 buf_T *buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005781 term_T *term;
5782 char_u val[100];
5783
5784 rettv->v_type = VAR_STRING;
5785 if (buf == NULL)
5786 return;
5787 term = buf->b_term;
5788
5789 if (term_job_running(term))
5790 STRCPY(val, "running");
5791 else
5792 STRCPY(val, "finished");
5793 if (term->tl_normal_mode)
5794 STRCAT(val, ",normal");
5795 rettv->vval.v_string = vim_strsave(val);
5796}
5797
5798/*
5799 * "term_gettitle(buf)" function
5800 */
5801 void
5802f_term_gettitle(typval_T *argvars, typval_T *rettv)
5803{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005804 buf_T *buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005805
5806 rettv->v_type = VAR_STRING;
5807 if (buf == NULL)
5808 return;
5809
5810 if (buf->b_term->tl_title != NULL)
5811 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5812}
5813
5814/*
5815 * "term_gettty(buf)" function
5816 */
5817 void
5818f_term_gettty(typval_T *argvars, typval_T *rettv)
5819{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005820 buf_T *buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005821 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005822 int num = 0;
5823
5824 rettv->v_type = VAR_STRING;
5825 if (buf == NULL)
5826 return;
5827 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02005828 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005829
5830 switch (num)
5831 {
5832 case 0:
5833 if (buf->b_term->tl_job != NULL)
5834 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005835 break;
5836 case 1:
5837 if (buf->b_term->tl_job != NULL)
5838 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005839 break;
5840 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005841 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005842 return;
5843 }
5844 if (p != NULL)
5845 rettv->vval.v_string = vim_strsave(p);
5846}
5847
5848/*
5849 * "term_list()" function
5850 */
5851 void
5852f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
5853{
5854 term_T *tp;
5855 list_T *l;
5856
5857 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
5858 return;
5859
5860 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005861 FOR_ALL_TERMS(tp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005862 if (tp != NULL && tp->tl_buffer != NULL)
5863 if (list_append_number(l,
5864 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
5865 return;
5866}
5867
5868/*
5869 * "term_scrape(buf, row)" function
5870 */
5871 void
5872f_term_scrape(typval_T *argvars, typval_T *rettv)
5873{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005874 buf_T *buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005875 VTermScreen *screen = NULL;
5876 VTermPos pos;
5877 list_T *l;
5878 term_T *term;
5879 char_u *p;
5880 sb_line_T *line;
5881
5882 if (rettv_list_alloc(rettv) == FAIL)
5883 return;
5884 if (buf == NULL)
5885 return;
5886 term = buf->b_term;
5887
5888 l = rettv->vval.v_list;
5889 pos.row = get_row_number(&argvars[1], term);
5890
5891 if (term->tl_vterm != NULL)
5892 {
5893 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01005894 if (screen == NULL) // can't really happen
5895 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005896 p = NULL;
5897 line = NULL;
5898 }
5899 else
5900 {
5901 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
5902
5903 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
5904 return;
5905 p = ml_get_buf(buf, lnum + 1, FALSE);
5906 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
5907 }
5908
5909 for (pos.col = 0; pos.col < term->tl_cols; )
5910 {
5911 dict_T *dcell;
5912 int width;
5913 VTermScreenCellAttrs attrs;
5914 VTermColor fg, bg;
5915 char_u rgb[8];
5916 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
5917 int off = 0;
5918 int i;
5919
5920 if (screen == NULL)
5921 {
5922 cellattr_T *cellattr;
5923 int len;
5924
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005925 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005926 if (pos.col >= line->sb_cols)
5927 break;
5928 cellattr = line->sb_cells + pos.col;
5929 width = cellattr->width;
5930 attrs = cellattr->attrs;
5931 fg = cellattr->fg;
5932 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02005933 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005934 mch_memmove(mbs, p, len);
5935 mbs[len] = NUL;
5936 p += len;
5937 }
5938 else
5939 {
5940 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005941
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005942 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
5943 break;
5944 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
5945 {
5946 if (cell.chars[i] == 0)
5947 break;
5948 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
5949 }
5950 mbs[off] = NUL;
5951 width = cell.width;
5952 attrs = cell.attrs;
5953 fg = cell.fg;
5954 bg = cell.bg;
5955 }
5956 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01005957 if (dcell == NULL)
5958 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005959 list_append_dict(l, dcell);
5960
Bram Moolenaare0be1672018-07-08 16:50:37 +02005961 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005962
5963 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5964 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005965 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005966 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5967 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005968 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005969
Bram Moolenaar83d47902020-03-26 20:34:00 +01005970 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02005971 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005972
5973 ++pos.col;
5974 if (width == 2)
5975 ++pos.col;
5976 }
5977}
5978
5979/*
5980 * "term_sendkeys(buf, keys)" function
5981 */
5982 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01005983f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005984{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005985 buf_T *buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005986 char_u *msg;
5987 term_T *term;
5988
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005989 if (buf == NULL)
5990 return;
5991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005992 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005993 if (msg == NULL)
5994 return;
5995 term = buf->b_term;
5996 if (term->tl_vterm == NULL)
5997 return;
5998
5999 while (*msg != NUL)
6000 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006001 int c;
6002
6003 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6004 {
6005 c = TO_SPECIAL(msg[1], msg[2]);
6006 msg += 3;
6007 }
6008 else
6009 {
6010 c = PTR2CHAR(msg);
6011 msg += MB_CPTR2LEN(msg);
6012 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006013 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006014 }
6015}
6016
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006017#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6018/*
6019 * "term_getansicolors(buf)" function
6020 */
6021 void
6022f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6023{
6024 buf_T *buf = term_get_buf(argvars, "term_getansicolors()");
6025 term_T *term;
6026 VTermState *state;
6027 VTermColor color;
6028 char_u hexbuf[10];
6029 int index;
6030 list_T *list;
6031
6032 if (rettv_list_alloc(rettv) == FAIL)
6033 return;
6034
6035 if (buf == NULL)
6036 return;
6037 term = buf->b_term;
6038 if (term->tl_vterm == NULL)
6039 return;
6040
6041 list = rettv->vval.v_list;
6042 state = vterm_obtain_state(term->tl_vterm);
6043 for (index = 0; index < 16; index++)
6044 {
6045 vterm_state_get_palette_color(state, index, &color);
6046 sprintf((char *)hexbuf, "#%02x%02x%02x",
6047 color.red, color.green, color.blue);
6048 if (list_append_string(list, hexbuf, 7) == FAIL)
6049 return;
6050 }
6051}
6052
6053/*
6054 * "term_setansicolors(buf, list)" function
6055 */
6056 void
6057f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6058{
6059 buf_T *buf = term_get_buf(argvars, "term_setansicolors()");
6060 term_T *term;
6061
6062 if (buf == NULL)
6063 return;
6064 term = buf->b_term;
6065 if (term->tl_vterm == NULL)
6066 return;
6067
6068 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6069 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006070 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006071 return;
6072 }
6073
6074 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006075 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006076}
6077#endif
6078
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006079/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006080 * "term_setapi(buf, api)" function
6081 */
6082 void
6083f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6084{
6085 buf_T *buf = term_get_buf(argvars, "term_setapi()");
6086 term_T *term;
6087 char_u *api;
6088
6089 if (buf == NULL)
6090 return;
6091 term = buf->b_term;
6092 vim_free(term->tl_api);
6093 api = tv_get_string_chk(&argvars[1]);
6094 if (api != NULL)
6095 term->tl_api = vim_strsave(api);
6096 else
6097 term->tl_api = NULL;
6098}
6099
6100/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006101 * "term_setrestore(buf, command)" function
6102 */
6103 void
6104f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6105{
6106#if defined(FEAT_SESSION)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006107 buf_T *buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006108 term_T *term;
6109 char_u *cmd;
6110
6111 if (buf == NULL)
6112 return;
6113 term = buf->b_term;
6114 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006115 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006116 if (cmd != NULL)
6117 term->tl_command = vim_strsave(cmd);
6118 else
6119 term->tl_command = NULL;
6120#endif
6121}
6122
6123/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006124 * "term_setkill(buf, how)" function
6125 */
6126 void
6127f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6128{
6129 buf_T *buf = term_get_buf(argvars, "term_setkill()");
6130 term_T *term;
6131 char_u *how;
6132
6133 if (buf == NULL)
6134 return;
6135 term = buf->b_term;
6136 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006137 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006138 if (how != NULL)
6139 term->tl_kill = vim_strsave(how);
6140 else
6141 term->tl_kill = NULL;
6142}
6143
6144/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006145 * "term_start(command, options)" function
6146 */
6147 void
6148f_term_start(typval_T *argvars, typval_T *rettv)
6149{
6150 jobopt_T opt;
6151 buf_T *buf;
6152
6153 init_job_options(&opt);
6154 if (argvars[1].v_type != VAR_UNKNOWN
6155 && get_job_options(&argvars[1], &opt,
6156 JO_TIMEOUT_ALL + JO_STOPONEXIT
6157 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6158 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6159 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6160 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006161 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006162 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006163 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006164 return;
6165
Bram Moolenaar13568252018-03-16 20:46:58 +01006166 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006167
6168 if (buf != NULL && buf->b_term != NULL)
6169 rettv->vval.v_number = buf->b_fnum;
6170}
6171
6172/*
6173 * "term_wait" function
6174 */
6175 void
6176f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6177{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006178 buf_T *buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006179
6180 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006181 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006182 if (buf->b_term->tl_job == NULL)
6183 {
6184 ch_log(NULL, "term_wait(): no job to wait for");
6185 return;
6186 }
6187 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006188 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006189 return;
6190
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006191 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006192 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006193 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6194 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006195 // The job is dead, keep reading channel I/O until the channel is
6196 // closed. buf->b_term may become NULL if the terminal was closed while
6197 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006198 ch_log(NULL, "term_wait(): waiting for channel to close");
6199 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6200 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006201 term_flush_messages();
6202
Bram Moolenaard45aa552018-05-21 22:50:29 +02006203 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006204 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006205 // If the terminal is closed when the channel is closed the
6206 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006207 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006208 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006209
6210 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006211 }
6212 else
6213 {
6214 long wait = 10L;
6215
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006216 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006217
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006218 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006219 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006220 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006221 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006222
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006223 // Flushing messages on channels is hopefully sufficient.
6224 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006225 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006226 }
6227}
6228
6229/*
6230 * Called when a channel has sent all the lines to a terminal.
6231 * Send a CTRL-D to mark the end of the text.
6232 */
6233 void
6234term_send_eof(channel_T *ch)
6235{
6236 term_T *term;
6237
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006238 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006239 if (term->tl_job == ch->ch_job)
6240 {
6241 if (term->tl_eof_chars != NULL)
6242 {
6243 channel_send(ch, PART_IN, term->tl_eof_chars,
6244 (int)STRLEN(term->tl_eof_chars), NULL);
6245 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6246 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006247# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006248 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006249 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006250 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6251# endif
6252 }
6253}
6254
Bram Moolenaar113e1072019-01-20 15:30:40 +01006255#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006256 job_T *
6257term_getjob(term_T *term)
6258{
6259 return term != NULL ? term->tl_job : NULL;
6260}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006261#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006262
Bram Moolenaar4f974752019-02-17 17:44:42 +01006263# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006264
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006265///////////////////////////////////////
6266// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006267#ifdef PROTO
6268typedef int COORD;
6269typedef int DWORD;
6270typedef int HANDLE;
6271typedef int *DWORD_PTR;
6272typedef int HPCON;
6273typedef int HRESULT;
6274typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006275typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006276typedef int PSIZE_T;
6277typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006278typedef int BOOL;
6279# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006280#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006281
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006282HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6283HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6284HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006285BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6286BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6287void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006288
6289 static int
6290dyn_conpty_init(int verbose)
6291{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006292 static HMODULE hKerneldll = NULL;
6293 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006294 static struct
6295 {
6296 char *name;
6297 FARPROC *ptr;
6298 } conpty_entry[] =
6299 {
6300 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6301 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6302 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6303 {"InitializeProcThreadAttributeList",
6304 (FARPROC*)&pInitializeProcThreadAttributeList},
6305 {"UpdateProcThreadAttribute",
6306 (FARPROC*)&pUpdateProcThreadAttribute},
6307 {"DeleteProcThreadAttributeList",
6308 (FARPROC*)&pDeleteProcThreadAttributeList},
6309 {NULL, NULL}
6310 };
6311
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006312 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006313 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006314 if (verbose)
6315 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006316 return FAIL;
6317 }
6318
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006319 // No need to initialize twice.
6320 if (hKerneldll)
6321 return OK;
6322
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006323 hKerneldll = vimLoadLib("kernel32.dll");
6324 for (i = 0; conpty_entry[i].name != NULL
6325 && conpty_entry[i].ptr != NULL; ++i)
6326 {
6327 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6328 conpty_entry[i].name)) == NULL)
6329 {
6330 if (verbose)
6331 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006332 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006333 return FAIL;
6334 }
6335 }
6336
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006337 return OK;
6338}
6339
6340 static int
6341conpty_term_and_job_init(
6342 term_T *term,
6343 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006344 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006345 jobopt_T *opt,
6346 jobopt_T *orig_opt)
6347{
6348 WCHAR *cmd_wchar = NULL;
6349 WCHAR *cmd_wchar_copy = NULL;
6350 WCHAR *cwd_wchar = NULL;
6351 WCHAR *env_wchar = NULL;
6352 channel_T *channel = NULL;
6353 job_T *job = NULL;
6354 HANDLE jo = NULL;
6355 garray_T ga_cmd, ga_env;
6356 char_u *cmd = NULL;
6357 HRESULT hr;
6358 COORD consize;
6359 SIZE_T breq;
6360 PROCESS_INFORMATION proc_info;
6361 HANDLE i_theirs = NULL;
6362 HANDLE o_theirs = NULL;
6363 HANDLE i_ours = NULL;
6364 HANDLE o_ours = NULL;
6365
6366 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6367 ga_init2(&ga_env, (int)sizeof(char*), 20);
6368
6369 if (argvar->v_type == VAR_STRING)
6370 {
6371 cmd = argvar->vval.v_string;
6372 }
6373 else if (argvar->v_type == VAR_LIST)
6374 {
6375 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6376 goto failed;
6377 cmd = ga_cmd.ga_data;
6378 }
6379 if (cmd == NULL || *cmd == NUL)
6380 {
6381 emsg(_(e_invarg));
6382 goto failed;
6383 }
6384
6385 term->tl_arg0_cmd = vim_strsave(cmd);
6386
6387 cmd_wchar = enc_to_utf16(cmd, NULL);
6388
6389 if (cmd_wchar != NULL)
6390 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006391 // Request by CreateProcessW
6392 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006393 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006394 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6395 }
6396
6397 ga_clear(&ga_cmd);
6398 if (cmd_wchar == NULL)
6399 goto failed;
6400 if (opt->jo_cwd != NULL)
6401 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6402
6403 win32_build_env(opt->jo_env, &ga_env, TRUE);
6404 env_wchar = ga_env.ga_data;
6405
6406 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6407 goto failed;
6408 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6409 goto failed;
6410
6411 consize.X = term->tl_cols;
6412 consize.Y = term->tl_rows;
6413 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6414 &term->tl_conpty);
6415 if (FAILED(hr))
6416 goto failed;
6417
6418 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6419
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006420 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006421 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006422 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006423 if (!term->tl_siex.lpAttributeList)
6424 goto failed;
6425 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6426 0, &breq))
6427 goto failed;
6428 if (!pUpdateProcThreadAttribute(
6429 term->tl_siex.lpAttributeList, 0,
6430 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6431 sizeof(HPCON), NULL, NULL))
6432 goto failed;
6433
6434 channel = add_channel();
6435 if (channel == NULL)
6436 goto failed;
6437
6438 job = job_alloc();
6439 if (job == NULL)
6440 goto failed;
6441 if (argvar->v_type == VAR_STRING)
6442 {
6443 int argc;
6444
6445 build_argv_from_string(cmd, &job->jv_argv, &argc);
6446 }
6447 else
6448 {
6449 int argc;
6450
6451 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6452 }
6453
6454 if (opt->jo_set & JO_IN_BUF)
6455 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6456
6457 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6458 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006459 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006460 env_wchar, cwd_wchar,
6461 &term->tl_siex.StartupInfo, &proc_info))
6462 goto failed;
6463
6464 CloseHandle(i_theirs);
6465 CloseHandle(o_theirs);
6466
6467 channel_set_pipes(channel,
6468 (sock_T)i_ours,
6469 (sock_T)o_ours,
6470 (sock_T)o_ours);
6471
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006472 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006473 channel->ch_write_text_mode = TRUE;
6474
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006475 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006476 channel->ch_anonymous_pipe = TRUE;
6477
6478 jo = CreateJobObject(NULL, NULL);
6479 if (jo == NULL)
6480 goto failed;
6481
6482 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6483 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006484 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006485 CloseHandle(jo);
6486 jo = NULL;
6487 }
6488
6489 ResumeThread(proc_info.hThread);
6490 CloseHandle(proc_info.hThread);
6491
6492 vim_free(cmd_wchar);
6493 vim_free(cmd_wchar_copy);
6494 vim_free(cwd_wchar);
6495 vim_free(env_wchar);
6496
6497 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6498 goto failed;
6499
6500#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6501 if (opt->jo_set2 & JO2_ANSI_COLORS)
6502 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6503 else
6504 init_vterm_ansi_colors(term->tl_vterm);
6505#endif
6506
6507 channel_set_job(channel, job, opt);
6508 job_set_options(job, opt);
6509
6510 job->jv_channel = channel;
6511 job->jv_proc_info = proc_info;
6512 job->jv_job_object = jo;
6513 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006514 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006515 ++job->jv_refcount;
6516 term->tl_job = job;
6517
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006518 // Redirecting stdout and stderr doesn't work at the job level. Instead
6519 // open the file here and handle it in. opt->jo_io was changed in
6520 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006521 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6522 {
6523 char_u *fname = opt->jo_io_name[PART_OUT];
6524
6525 ch_log(channel, "Opening output file %s", fname);
6526 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6527 if (term->tl_out_fd == NULL)
6528 semsg(_(e_notopen), fname);
6529 }
6530
6531 return OK;
6532
6533failed:
6534 ga_clear(&ga_cmd);
6535 ga_clear(&ga_env);
6536 vim_free(cmd_wchar);
6537 vim_free(cmd_wchar_copy);
6538 vim_free(cwd_wchar);
6539 if (channel != NULL)
6540 channel_clear(channel);
6541 if (job != NULL)
6542 {
6543 job->jv_channel = NULL;
6544 job_cleanup(job);
6545 }
6546 term->tl_job = NULL;
6547 if (jo != NULL)
6548 CloseHandle(jo);
6549
6550 if (term->tl_siex.lpAttributeList != NULL)
6551 {
6552 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6553 vim_free(term->tl_siex.lpAttributeList);
6554 }
6555 term->tl_siex.lpAttributeList = NULL;
6556 if (o_theirs != NULL)
6557 CloseHandle(o_theirs);
6558 if (o_ours != NULL)
6559 CloseHandle(o_ours);
6560 if (i_ours != NULL)
6561 CloseHandle(i_ours);
6562 if (i_theirs != NULL)
6563 CloseHandle(i_theirs);
6564 if (term->tl_conpty != NULL)
6565 pClosePseudoConsole(term->tl_conpty);
6566 term->tl_conpty = NULL;
6567 return FAIL;
6568}
6569
6570 static void
6571conpty_term_report_winsize(term_T *term, int rows, int cols)
6572{
6573 COORD consize;
6574
6575 consize.X = cols;
6576 consize.Y = rows;
6577 pResizePseudoConsole(term->tl_conpty, consize);
6578}
6579
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006580 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006581term_free_conpty(term_T *term)
6582{
6583 if (term->tl_siex.lpAttributeList != NULL)
6584 {
6585 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6586 vim_free(term->tl_siex.lpAttributeList);
6587 }
6588 term->tl_siex.lpAttributeList = NULL;
6589 if (term->tl_conpty != NULL)
6590 pClosePseudoConsole(term->tl_conpty);
6591 term->tl_conpty = NULL;
6592}
6593
6594 int
6595use_conpty(void)
6596{
6597 return has_conpty;
6598}
6599
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006600# ifndef PROTO
6601
6602#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6603#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006604#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006605
6606void* (*winpty_config_new)(UINT64, void*);
6607void* (*winpty_open)(void*, void*);
6608void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6609BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6610void (*winpty_config_set_mouse_mode)(void*, int);
6611void (*winpty_config_set_initial_size)(void*, int, int);
6612LPCWSTR (*winpty_conin_name)(void*);
6613LPCWSTR (*winpty_conout_name)(void*);
6614LPCWSTR (*winpty_conerr_name)(void*);
6615void (*winpty_free)(void*);
6616void (*winpty_config_free)(void*);
6617void (*winpty_spawn_config_free)(void*);
6618void (*winpty_error_free)(void*);
6619LPCWSTR (*winpty_error_msg)(void*);
6620BOOL (*winpty_set_size)(void*, int, int, void*);
6621HANDLE (*winpty_agent_process)(void*);
6622
6623#define WINPTY_DLL "winpty.dll"
6624
6625static HINSTANCE hWinPtyDLL = NULL;
6626# endif
6627
6628 static int
6629dyn_winpty_init(int verbose)
6630{
6631 int i;
6632 static struct
6633 {
6634 char *name;
6635 FARPROC *ptr;
6636 } winpty_entry[] =
6637 {
6638 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6639 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6640 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6641 {"winpty_config_set_mouse_mode",
6642 (FARPROC*)&winpty_config_set_mouse_mode},
6643 {"winpty_config_set_initial_size",
6644 (FARPROC*)&winpty_config_set_initial_size},
6645 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6646 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6647 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6648 {"winpty_free", (FARPROC*)&winpty_free},
6649 {"winpty_open", (FARPROC*)&winpty_open},
6650 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6651 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6652 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6653 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6654 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6655 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6656 {NULL, NULL}
6657 };
6658
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006659 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006660 if (hWinPtyDLL)
6661 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006662 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6663 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006664 if (*p_winptydll != NUL)
6665 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6666 if (!hWinPtyDLL)
6667 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6668 if (!hWinPtyDLL)
6669 {
6670 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006671 semsg(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006672 : (char_u *)WINPTY_DLL);
6673 return FAIL;
6674 }
6675 for (i = 0; winpty_entry[i].name != NULL
6676 && winpty_entry[i].ptr != NULL; ++i)
6677 {
6678 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6679 winpty_entry[i].name)) == NULL)
6680 {
6681 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006682 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006683 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006684 return FAIL;
6685 }
6686 }
6687
6688 return OK;
6689}
6690
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006691 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006692winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006693 term_T *term,
6694 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006695 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006696 jobopt_T *opt,
6697 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006698{
6699 WCHAR *cmd_wchar = NULL;
6700 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006701 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006702 channel_T *channel = NULL;
6703 job_T *job = NULL;
6704 DWORD error;
6705 HANDLE jo = NULL;
6706 HANDLE child_process_handle;
6707 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006708 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006709 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006710 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006711 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006712
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006713 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6714 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006715
6716 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006717 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006718 cmd = argvar->vval.v_string;
6719 }
6720 else if (argvar->v_type == VAR_LIST)
6721 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006722 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006723 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006724 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006725 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006726 if (cmd == NULL || *cmd == NUL)
6727 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006728 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006729 goto failed;
6730 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006731
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006732 term->tl_arg0_cmd = vim_strsave(cmd);
6733
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006734 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006735 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006736 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006737 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006738 if (opt->jo_cwd != NULL)
6739 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006740
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006741 win32_build_env(opt->jo_env, &ga_env, TRUE);
6742 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006743
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006744 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6745 if (term->tl_winpty_config == NULL)
6746 goto failed;
6747
6748 winpty_config_set_mouse_mode(term->tl_winpty_config,
6749 WINPTY_MOUSE_MODE_FORCE);
6750 winpty_config_set_initial_size(term->tl_winpty_config,
6751 term->tl_cols, term->tl_rows);
6752 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6753 if (term->tl_winpty == NULL)
6754 goto failed;
6755
6756 spawn_config = winpty_spawn_config_new(
6757 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6758 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6759 NULL,
6760 cmd_wchar,
6761 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006762 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006763 &winpty_err);
6764 if (spawn_config == NULL)
6765 goto failed;
6766
6767 channel = add_channel();
6768 if (channel == NULL)
6769 goto failed;
6770
6771 job = job_alloc();
6772 if (job == NULL)
6773 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006774 if (argvar->v_type == VAR_STRING)
6775 {
6776 int argc;
6777
6778 build_argv_from_string(cmd, &job->jv_argv, &argc);
6779 }
6780 else
6781 {
6782 int argc;
6783
6784 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6785 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006786
6787 if (opt->jo_set & JO_IN_BUF)
6788 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6789
6790 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
6791 &child_thread_handle, &error, &winpty_err))
6792 goto failed;
6793
6794 channel_set_pipes(channel,
6795 (sock_T)CreateFileW(
6796 winpty_conin_name(term->tl_winpty),
6797 GENERIC_WRITE, 0, NULL,
6798 OPEN_EXISTING, 0, NULL),
6799 (sock_T)CreateFileW(
6800 winpty_conout_name(term->tl_winpty),
6801 GENERIC_READ, 0, NULL,
6802 OPEN_EXISTING, 0, NULL),
6803 (sock_T)CreateFileW(
6804 winpty_conerr_name(term->tl_winpty),
6805 GENERIC_READ, 0, NULL,
6806 OPEN_EXISTING, 0, NULL));
6807
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006808 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006809 channel->ch_write_text_mode = TRUE;
6810
6811 jo = CreateJobObject(NULL, NULL);
6812 if (jo == NULL)
6813 goto failed;
6814
6815 if (!AssignProcessToJobObject(jo, child_process_handle))
6816 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006817 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006818 CloseHandle(jo);
6819 jo = NULL;
6820 }
6821
6822 winpty_spawn_config_free(spawn_config);
6823 vim_free(cmd_wchar);
6824 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006825 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006826
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006827 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6828 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006829
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006830#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6831 if (opt->jo_set2 & JO2_ANSI_COLORS)
6832 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6833 else
6834 init_vterm_ansi_colors(term->tl_vterm);
6835#endif
6836
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006837 channel_set_job(channel, job, opt);
6838 job_set_options(job, opt);
6839
6840 job->jv_channel = channel;
6841 job->jv_proc_info.hProcess = child_process_handle;
6842 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
6843 job->jv_job_object = jo;
6844 job->jv_status = JOB_STARTED;
6845 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006846 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006847 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006848 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006849 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006850 ++job->jv_refcount;
6851 term->tl_job = job;
6852
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006853 // Redirecting stdout and stderr doesn't work at the job level. Instead
6854 // open the file here and handle it in. opt->jo_io was changed in
6855 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006856 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6857 {
6858 char_u *fname = opt->jo_io_name[PART_OUT];
6859
6860 ch_log(channel, "Opening output file %s", fname);
6861 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6862 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006863 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006864 }
6865
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006866 return OK;
6867
6868failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006869 ga_clear(&ga_cmd);
6870 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006871 vim_free(cmd_wchar);
6872 vim_free(cwd_wchar);
6873 if (spawn_config != NULL)
6874 winpty_spawn_config_free(spawn_config);
6875 if (channel != NULL)
6876 channel_clear(channel);
6877 if (job != NULL)
6878 {
6879 job->jv_channel = NULL;
6880 job_cleanup(job);
6881 }
6882 term->tl_job = NULL;
6883 if (jo != NULL)
6884 CloseHandle(jo);
6885 if (term->tl_winpty != NULL)
6886 winpty_free(term->tl_winpty);
6887 term->tl_winpty = NULL;
6888 if (term->tl_winpty_config != NULL)
6889 winpty_config_free(term->tl_winpty_config);
6890 term->tl_winpty_config = NULL;
6891 if (winpty_err != NULL)
6892 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006893 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006894 (short_u *)winpty_error_msg(winpty_err), NULL);
6895
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006896 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006897 winpty_error_free(winpty_err);
6898 }
6899 return FAIL;
6900}
6901
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006902/*
6903 * Create a new terminal of "rows" by "cols" cells.
6904 * Store a reference in "term".
6905 * Return OK or FAIL.
6906 */
6907 static int
6908term_and_job_init(
6909 term_T *term,
6910 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01006911 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006912 jobopt_T *opt,
6913 jobopt_T *orig_opt)
6914{
6915 int use_winpty = FALSE;
6916 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006917 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006918
6919 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
6920 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
6921
6922 if (!has_winpty && !has_conpty)
6923 // If neither is available give the errors for winpty, since when
6924 // conpty is not available it can't be installed either.
6925 return dyn_winpty_init(TRUE);
6926
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006927 if (opt->jo_tty_type != NUL)
6928 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006929
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006930 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006931 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006932 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006933 use_conpty = TRUE;
6934 else if (has_winpty)
6935 use_winpty = TRUE;
6936 // else: error
6937 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006938 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006939 {
6940 if (has_winpty)
6941 use_winpty = TRUE;
6942 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006943 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006944 {
6945 if (has_conpty)
6946 use_conpty = TRUE;
6947 else
6948 return dyn_conpty_init(TRUE);
6949 }
6950
6951 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006952 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006953
6954 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006955 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006956
6957 // error
6958 return dyn_winpty_init(TRUE);
6959}
6960
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006961 static int
6962create_pty_only(term_T *term, jobopt_T *options)
6963{
6964 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
6965 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
6966 char in_name[80], out_name[80];
6967 channel_T *channel = NULL;
6968
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006969 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6970 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006971
6972 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
6973 GetCurrentProcessId(),
6974 curbuf->b_fnum);
6975 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
6976 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6977 PIPE_UNLIMITED_INSTANCES,
6978 0, 0, NMPWAIT_NOWAIT, NULL);
6979 if (hPipeIn == INVALID_HANDLE_VALUE)
6980 goto failed;
6981
6982 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
6983 GetCurrentProcessId(),
6984 curbuf->b_fnum);
6985 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
6986 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6987 PIPE_UNLIMITED_INSTANCES,
6988 0, 0, 0, NULL);
6989 if (hPipeOut == INVALID_HANDLE_VALUE)
6990 goto failed;
6991
6992 ConnectNamedPipe(hPipeIn, NULL);
6993 ConnectNamedPipe(hPipeOut, NULL);
6994
6995 term->tl_job = job_alloc();
6996 if (term->tl_job == NULL)
6997 goto failed;
6998 ++term->tl_job->jv_refcount;
6999
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007000 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007001 term->tl_job->jv_status = JOB_FINISHED;
7002
7003 channel = add_channel();
7004 if (channel == NULL)
7005 goto failed;
7006 term->tl_job->jv_channel = channel;
7007 channel->ch_keep_open = TRUE;
7008 channel->ch_named_pipe = TRUE;
7009
7010 channel_set_pipes(channel,
7011 (sock_T)hPipeIn,
7012 (sock_T)hPipeOut,
7013 (sock_T)hPipeOut);
7014 channel_set_job(channel, term->tl_job, options);
7015 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7016 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7017
7018 return OK;
7019
7020failed:
7021 if (hPipeIn != NULL)
7022 CloseHandle(hPipeIn);
7023 if (hPipeOut != NULL)
7024 CloseHandle(hPipeOut);
7025 return FAIL;
7026}
7027
7028/*
7029 * Free the terminal emulator part of "term".
7030 */
7031 static void
7032term_free_vterm(term_T *term)
7033{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007034 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007035 if (term->tl_winpty != NULL)
7036 winpty_free(term->tl_winpty);
7037 term->tl_winpty = NULL;
7038 if (term->tl_winpty_config != NULL)
7039 winpty_config_free(term->tl_winpty_config);
7040 term->tl_winpty_config = NULL;
7041 if (term->tl_vterm != NULL)
7042 vterm_free(term->tl_vterm);
7043 term->tl_vterm = NULL;
7044}
7045
7046/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007047 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007048 */
7049 static void
7050term_report_winsize(term_T *term, int rows, int cols)
7051{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007052 if (term->tl_conpty)
7053 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007054 if (term->tl_winpty)
7055 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7056}
7057
7058 int
7059terminal_enabled(void)
7060{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007061 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007062}
7063
7064# else
7065
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007066///////////////////////////////////////
7067// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007068
7069/*
7070 * Create a new terminal of "rows" by "cols" cells.
7071 * Start job for "cmd".
7072 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007073 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007074 * Return OK or FAIL.
7075 */
7076 static int
7077term_and_job_init(
7078 term_T *term,
7079 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007080 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007081 jobopt_T *opt,
7082 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007083{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007084 term->tl_arg0_cmd = NULL;
7085
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007086 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7087 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007088
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007089#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7090 if (opt->jo_set2 & JO2_ANSI_COLORS)
7091 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7092 else
7093 init_vterm_ansi_colors(term->tl_vterm);
7094#endif
7095
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007096 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007097 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007098 if (term->tl_job != NULL)
7099 ++term->tl_job->jv_refcount;
7100
7101 return term->tl_job != NULL
7102 && term->tl_job->jv_channel != NULL
7103 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7104}
7105
7106 static int
7107create_pty_only(term_T *term, jobopt_T *opt)
7108{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007109 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7110 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007111
7112 term->tl_job = job_alloc();
7113 if (term->tl_job == NULL)
7114 return FAIL;
7115 ++term->tl_job->jv_refcount;
7116
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007117 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007118 term->tl_job->jv_status = JOB_FINISHED;
7119
7120 return mch_create_pty_channel(term->tl_job, opt);
7121}
7122
7123/*
7124 * Free the terminal emulator part of "term".
7125 */
7126 static void
7127term_free_vterm(term_T *term)
7128{
7129 if (term->tl_vterm != NULL)
7130 vterm_free(term->tl_vterm);
7131 term->tl_vterm = NULL;
7132}
7133
7134/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007135 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007136 */
7137 static void
7138term_report_winsize(term_T *term, int rows, int cols)
7139{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007140 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007141 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7142 {
7143 int fd = -1;
7144 int part;
7145
7146 for (part = PART_OUT; part < PART_COUNT; ++part)
7147 {
7148 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007149 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007150 break;
7151 }
7152 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7153 mch_signal_job(term->tl_job, (char_u *)"winch");
7154 }
7155}
7156
7157# endif
7158
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007159#endif // FEAT_TERMINAL