blob: 2043c8487ec019936f642a53e6fb93551a4ba0bd [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 Moolenaar5a3a49e2018-03-20 18:35:53 +0100443 int vertical = opt->jo_vertical || (cmdmod.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)
532 cmdmod.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)
1595 if ((how == NULL || *how == NUL) && (p_confirm || cmdmod.confirm))
1596 {
1597 char_u buff[DIALOG_MSG_SIZE];
1598 int ret;
1599
1600 dialog_msg(buff, _("Kill job in \"%s\"?"), buf->b_fname);
1601 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1602 if (ret == VIM_YES)
1603 how = "kill";
1604 else if (ret == VIM_CANCEL)
1605 return FAIL;
1606 }
1607#endif
1608 if (how == NULL || *how == NUL)
1609 return FAIL;
1610
1611 job_stop(buf->b_term->tl_job, NULL, how);
1612
Bram Moolenaar9172d232019-01-29 23:06:54 +01001613 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001614 for (count = 0; count < 100; ++count)
1615 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001616 job_T *job;
1617
1618 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001619 if (!buf_valid(buf)
1620 || buf->b_term == NULL
1621 || buf->b_term->tl_job == NULL)
1622 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001623 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001624
Bram Moolenaar9172d232019-01-29 23:06:54 +01001625 // Call job_status() to update jv_status. It may cause the job to be
1626 // cleaned up but it won't be freed.
1627 job_status(job);
1628 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001629 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001630
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001631 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001632 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001633 }
1634 return FAIL;
1635}
1636
1637/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001638 * Add the last line of the scrollback buffer to the buffer in the window.
1639 */
1640 static void
1641add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1642{
1643 buf_T *buf = term->tl_buffer;
1644 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1645 linenr_T lnum = buf->b_ml.ml_line_count;
1646
Bram Moolenaar4f974752019-02-17 17:44:42 +01001647#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001648 if (!enc_utf8 && enc_codepage > 0)
1649 {
1650 WCHAR *ret = NULL;
1651 int length = 0;
1652
1653 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1654 &ret, &length);
1655 if (ret != NULL)
1656 {
1657 WideCharToMultiByte_alloc(enc_codepage, 0,
1658 ret, length, (char **)&text, &len, 0, 0);
1659 vim_free(ret);
1660 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1661 vim_free(text);
1662 }
1663 }
1664 else
1665#endif
1666 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1667 if (empty)
1668 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001669 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001670 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001671 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001672 curbuf = curwin->w_buffer;
1673 }
1674}
1675
1676 static void
1677cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1678{
1679 attr->width = cell->width;
1680 attr->attrs = cell->attrs;
1681 attr->fg = cell->fg;
1682 attr->bg = cell->bg;
1683}
1684
1685 static int
1686equal_celattr(cellattr_T *a, cellattr_T *b)
1687{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001688 // We only compare the RGB colors, ignoring the ANSI index and type.
1689 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001690 return a->fg.red == b->fg.red
1691 && a->fg.green == b->fg.green
1692 && a->fg.blue == b->fg.blue
1693 && a->bg.red == b->bg.red
1694 && a->bg.green == b->bg.green
1695 && a->bg.blue == b->bg.blue;
1696}
1697
Bram Moolenaard96ff162018-02-18 22:13:29 +01001698/*
1699 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1700 * line at this position. Otherwise at the end.
1701 */
1702 static int
1703add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1704{
1705 if (ga_grow(&term->tl_scrollback, 1) == OK)
1706 {
1707 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1708 + term->tl_scrollback.ga_len;
1709
1710 if (lnum > 0)
1711 {
1712 int i;
1713
1714 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1715 {
1716 *line = *(line - 1);
1717 --line;
1718 }
1719 }
1720 line->sb_cols = 0;
1721 line->sb_cells = NULL;
1722 line->sb_fill_attr = *fill_attr;
1723 ++term->tl_scrollback.ga_len;
1724 return OK;
1725 }
1726 return FALSE;
1727}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001728
1729/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001730 * Remove the terminal contents from the scrollback and the buffer.
1731 * Used before adding a new scrollback line or updating the buffer for lines
1732 * displayed in the terminal.
1733 */
1734 static void
1735cleanup_scrollback(term_T *term)
1736{
1737 sb_line_T *line;
1738 garray_T *gap;
1739
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001740 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001741 gap = &term->tl_scrollback;
1742 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1743 && gap->ga_len > 0)
1744 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001745 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001746 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1747 vim_free(line->sb_cells);
1748 --gap->ga_len;
1749 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001750 curbuf = curwin->w_buffer;
1751 if (curbuf == term->tl_buffer)
1752 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001753}
1754
1755/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001756 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001757 */
1758 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001759update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001760{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001761 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001762 int len;
1763 int lines_skipped = 0;
1764 VTermPos pos;
1765 VTermScreenCell cell;
1766 cellattr_T fill_attr, new_fill_attr;
1767 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001768
1769 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1770 "Adding terminal window snapshot to buffer");
1771
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001772 // First remove the lines that were appended before, they might be
1773 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001774 cleanup_scrollback(term);
1775
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001776 screen = vterm_obtain_screen(term->tl_vterm);
1777 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001778 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1779 {
1780 len = 0;
1781 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1782 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1783 && cell.chars[0] != NUL)
1784 {
1785 len = pos.col + 1;
1786 new_fill_attr = term->tl_default_color;
1787 }
1788 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001789 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001790 cell2cellattr(&cell, &new_fill_attr);
1791
1792 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1793 ++lines_skipped;
1794 else
1795 {
1796 while (lines_skipped > 0)
1797 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001798 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001799 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001800 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001801 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001802 }
1803
1804 if (len == 0)
1805 p = NULL;
1806 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001807 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001808 if ((p != NULL || len == 0)
1809 && ga_grow(&term->tl_scrollback, 1) == OK)
1810 {
1811 garray_T ga;
1812 int width;
1813 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1814 + term->tl_scrollback.ga_len;
1815
1816 ga_init2(&ga, 1, 100);
1817 for (pos.col = 0; pos.col < len; pos.col += width)
1818 {
1819 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1820 {
1821 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001822 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001823 if (ga_grow(&ga, 1) == OK)
1824 ga.ga_len += utf_char2bytes(' ',
1825 (char_u *)ga.ga_data + ga.ga_len);
1826 }
1827 else
1828 {
1829 width = cell.width;
1830
1831 cell2cellattr(&cell, &p[pos.col]);
1832
Bram Moolenaara79fd562018-12-20 20:47:32 +01001833 // Each character can be up to 6 bytes.
1834 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001835 {
1836 int i;
1837 int c;
1838
1839 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1840 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1841 (char_u *)ga.ga_data + ga.ga_len);
1842 }
1843 }
1844 }
1845 line->sb_cols = len;
1846 line->sb_cells = p;
1847 line->sb_fill_attr = new_fill_attr;
1848 fill_attr = new_fill_attr;
1849 ++term->tl_scrollback.ga_len;
1850
1851 if (ga_grow(&ga, 1) == FAIL)
1852 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1853 else
1854 {
1855 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1856 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1857 }
1858 ga_clear(&ga);
1859 }
1860 else
1861 vim_free(p);
1862 }
1863 }
1864
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001865 // Add trailing empty lines.
1866 for (pos.row = term->tl_scrollback.ga_len;
1867 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1868 ++pos.row)
1869 {
1870 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1871 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1872 }
1873
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001874 term->tl_dirty_snapshot = FALSE;
1875#ifdef FEAT_TIMERS
1876 term->tl_timer_set = FALSE;
1877#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001878}
1879
1880/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001881 * Loop over all windows in the current tab, and also curwin, which is not
1882 * encountered when using a terminal in a popup window.
1883 * Return TRUE if "*wp" was set to the next window.
1884 */
1885 static int
1886for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1887{
1888 if (*wp == NULL)
1889 *wp = firstwin;
1890 else if ((*wp)->w_next != NULL)
1891 *wp = (*wp)->w_next;
1892 else if (!*did_curwin)
1893 *wp = curwin;
1894 else
1895 return FALSE;
1896 if (*wp == curwin)
1897 *did_curwin = TRUE;
1898 return TRUE;
1899}
1900
1901/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001902 * If needed, add the current lines of the terminal to scrollback and to the
1903 * buffer. Called after the job has ended and when switching to
1904 * Terminal-Normal mode.
1905 * When "redraw" is TRUE redraw the windows that show the terminal.
1906 */
1907 static void
1908may_move_terminal_to_buffer(term_T *term, int redraw)
1909{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001910 if (term->tl_vterm == NULL)
1911 return;
1912
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001913 // Update the snapshot only if something changes or the buffer does not
1914 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001915 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1916 <= term->tl_scrollback_scrolled)
1917 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001918
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001919 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001920 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1921 &term->tl_default_color.fg, &term->tl_default_color.bg);
1922
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001923 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001924 {
1925 win_T *wp = NULL;
1926 int did_curwin = FALSE;
1927
1928 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001929 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001930 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001931 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001932 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1933 wp->w_cursor.col = 0;
1934 wp->w_valid = 0;
1935 if (wp->w_cursor.lnum >= wp->w_height)
1936 {
1937 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001938
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001939 if (wp->w_topline < min_topline)
1940 wp->w_topline = min_topline;
1941 }
1942 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001943 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001944 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001945 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001946}
1947
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001948#if defined(FEAT_TIMERS) || defined(PROTO)
1949/*
1950 * Check if any terminal timer expired. If so, copy text from the terminal to
1951 * the buffer.
1952 * Return the time until the next timer will expire.
1953 */
1954 int
1955term_check_timers(int next_due_arg, proftime_T *now)
1956{
1957 term_T *term;
1958 int next_due = next_due_arg;
1959
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001960 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001961 {
1962 if (term->tl_timer_set && !term->tl_normal_mode)
1963 {
1964 long this_due = proftime_time_left(&term->tl_timer_due, now);
1965
1966 if (this_due <= 1)
1967 {
1968 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001969 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001970 }
1971 else if (next_due == -1 || next_due > this_due)
1972 next_due = this_due;
1973 }
1974 }
1975
1976 return next_due;
1977}
1978#endif
1979
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001980/*
1981 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
1982 * otherwise end it.
1983 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001984 static void
1985set_terminal_mode(term_T *term, int normal_mode)
1986{
1987 term->tl_normal_mode = normal_mode;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001988 if (!normal_mode)
1989 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001990 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001991 if (term->tl_buffer == curbuf)
1992 maketitle();
1993}
1994
1995/*
Bram Moolenaare2978022020-04-26 14:47:44 +02001996 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001997 * Move the vterm contents into the scrollback buffer and free the vterm.
1998 */
1999 static void
2000cleanup_vterm(term_T *term)
2001{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002002 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002003 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002004 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002005 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002006}
2007
2008/*
2009 * Switch from Terminal-Job mode to Terminal-Normal mode.
2010 * Suspends updating the terminal window.
2011 */
2012 static void
2013term_enter_normal_mode(void)
2014{
2015 term_T *term = curbuf->b_term;
2016
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002017 set_terminal_mode(term, TRUE);
2018
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002019 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002020 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002021
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002022 // Move the window cursor to the position of the cursor in the
2023 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002024 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2025 + term->tl_cursor_pos.row + 1;
2026 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002027 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2028 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002029 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002030
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002031 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002032 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2033}
2034
2035/*
2036 * Returns TRUE if the current window contains a terminal and we are in
2037 * Terminal-Normal mode.
2038 */
2039 int
2040term_in_normal_mode(void)
2041{
2042 term_T *term = curbuf->b_term;
2043
2044 return term != NULL && term->tl_normal_mode;
2045}
2046
2047/*
2048 * Switch from Terminal-Normal mode to Terminal-Job mode.
2049 * Restores updating the terminal window.
2050 */
2051 void
2052term_enter_job_mode()
2053{
2054 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002055
2056 set_terminal_mode(term, FALSE);
2057
2058 if (term->tl_channel_closed)
2059 cleanup_vterm(term);
2060 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002061#ifdef FEAT_PROP_POPUP
2062 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002063 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002064#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002065}
2066
2067/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002068 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002069 * Note: while waiting a terminal may be closed and freed if the channel is
2070 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002071 */
2072 static int
2073term_vgetc()
2074{
2075 int c;
2076 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002077 int modify_other_keys =
2078 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002079
2080 State = TERMINAL;
2081 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002082#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002083 ctrl_break_was_pressed = FALSE;
2084#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002085 if (modify_other_keys)
2086 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002087 c = vgetc();
2088 got_int = FALSE;
2089 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002090 if (modify_other_keys)
2091 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002092 return c;
2093}
2094
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002095static int mouse_was_outside = FALSE;
2096
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002097/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002098 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002099 * Return FAIL when the key needs to be handled in Normal mode.
2100 * Return OK when the key was dropped or sent to the terminal.
2101 */
2102 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002103send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002104{
2105 char msg[KEY_BUF_LEN];
2106 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002107 int dragging_outside = FALSE;
2108
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002109 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002110 switch (c)
2111 {
2112 case NUL:
2113 case K_ZERO:
2114 if (typed)
2115 stuffcharReadbuff(c);
2116 return FAIL;
2117
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002118 case K_TABLINE:
2119 stuffcharReadbuff(c);
2120 return FAIL;
2121
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002122 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002123 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002124 return FAIL;
2125
2126 case K_LEFTDRAG:
2127 case K_MIDDLEDRAG:
2128 case K_RIGHTDRAG:
2129 case K_X1DRAG:
2130 case K_X2DRAG:
2131 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002132 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002133 case K_LEFTMOUSE:
2134 case K_LEFTMOUSE_NM:
2135 case K_LEFTRELEASE:
2136 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002137 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002138 case K_MIDDLEMOUSE:
2139 case K_MIDDLERELEASE:
2140 case K_RIGHTMOUSE:
2141 case K_RIGHTRELEASE:
2142 case K_X1MOUSE:
2143 case K_X1RELEASE:
2144 case K_X2MOUSE:
2145 case K_X2RELEASE:
2146
2147 case K_MOUSEUP:
2148 case K_MOUSEDOWN:
2149 case K_MOUSELEFT:
2150 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002151 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002152 int row = mouse_row;
2153 int col = mouse_col;
2154
2155#ifdef FEAT_PROP_POPUP
2156 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002157 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002158 row -= popup_top_extra(curwin);
2159 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002160 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002161#endif
2162 if (row < W_WINROW(curwin)
2163 || row >= (W_WINROW(curwin) + curwin->w_height)
2164 || col < curwin->w_wincol
2165 || col >= W_ENDCOL(curwin)
2166 || dragging_outside)
2167 {
2168 // click or scroll outside the current window or on status
2169 // line or vertical separator
2170 if (typed)
2171 {
2172 stuffcharReadbuff(c);
2173 mouse_was_outside = TRUE;
2174 }
2175 return FAIL;
2176 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002177 }
2178 }
2179 if (typed)
2180 mouse_was_outside = FALSE;
2181
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002182 // Convert the typed key to a sequence of bytes for the job.
2183 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002184 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002185 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002186 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2187 (char_u *)msg, (int)len, NULL);
2188
2189 return OK;
2190}
2191
2192 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002193position_cursor(win_T *wp, VTermPos *pos, int add_off UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002194{
2195 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2196 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002197#ifdef FEAT_PROP_POPUP
2198 if (add_off && popup_is_popup(curwin))
2199 {
2200 wp->w_wrow += popup_top_extra(curwin);
2201 wp->w_wcol += popup_left_extra(curwin);
2202 }
2203#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002204 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2205}
2206
2207/*
2208 * Handle CTRL-W "": send register contents to the job.
2209 */
2210 static void
2211term_paste_register(int prev_c UNUSED)
2212{
2213 int c;
2214 list_T *l;
2215 listitem_T *item;
2216 long reglen = 0;
2217 int type;
2218
2219#ifdef FEAT_CMDL_INFO
2220 if (add_to_showcmd(prev_c))
2221 if (add_to_showcmd('"'))
2222 out_flush();
2223#endif
2224 c = term_vgetc();
2225#ifdef FEAT_CMDL_INFO
2226 clear_showcmd();
2227#endif
2228 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002229 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002230 return;
2231
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002232 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002233 if (c == '=' && get_expr_register() != '=')
2234 return;
2235 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002236 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002237 return;
2238
2239 l = (list_T *)get_reg_contents(c, GREG_LIST);
2240 if (l != NULL)
2241 {
2242 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002243 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002244 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002245 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002246#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002247 char_u *tmp = s;
2248
2249 if (!enc_utf8 && enc_codepage > 0)
2250 {
2251 WCHAR *ret = NULL;
2252 int length = 0;
2253
2254 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2255 (int)STRLEN(s), &ret, &length);
2256 if (ret != NULL)
2257 {
2258 WideCharToMultiByte_alloc(CP_UTF8, 0,
2259 ret, length, (char **)&s, &length, 0, 0);
2260 vim_free(ret);
2261 }
2262 }
2263#endif
2264 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2265 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002266#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002267 if (tmp != s)
2268 vim_free(s);
2269#endif
2270
2271 if (item->li_next != NULL || type == MLINE)
2272 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2273 (char_u *)"\r", 1, NULL);
2274 }
2275 list_free(l);
2276 }
2277}
2278
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002279/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002280 * Return TRUE when waiting for a character in the terminal, the cursor of the
2281 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002282 */
2283 int
2284terminal_is_active()
2285{
2286 return in_terminal_loop != NULL;
2287}
2288
Bram Moolenaar83d47902020-03-26 20:34:00 +01002289/*
2290 * Return the highight group name for the terminal; "Terminal" if not set.
2291 */
2292 static char_u *
2293term_get_highlight_name(term_T *term)
2294{
2295 if (term->tl_highlight_name == NULL)
2296 return (char_u *)"Terminal";
2297 return term->tl_highlight_name;
2298}
2299
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002300#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002301 cursorentry_T *
2302term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2303{
2304 term_T *term = in_terminal_loop;
2305 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002306 int id;
2307 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002308
Bram Moolenaara80faa82020-04-12 19:37:17 +02002309 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002310 entry.shape = entry.mshape =
2311 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2312 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2313 SHAPE_BLOCK;
2314 entry.percentage = 20;
2315 if (term->tl_cursor_blink)
2316 {
2317 entry.blinkwait = 700;
2318 entry.blinkon = 400;
2319 entry.blinkoff = 250;
2320 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002321
Bram Moolenaar83d47902020-03-26 20:34:00 +01002322 // The highlight group overrules the defaults.
2323 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002324 if (id != 0)
2325 {
2326 syn_id2colors(id, &term_fg, &term_bg);
2327 *fg = term_bg;
2328 }
2329 else
2330 *fg = gui.back_pixel;
2331
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002332 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002333 {
2334 if (id != 0)
2335 *bg = term_fg;
2336 else
2337 *bg = gui.norm_pixel;
2338 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002339 else
2340 *bg = color_name2handle(term->tl_cursor_color);
2341 entry.name = "n";
2342 entry.used_for = SHAPE_CURSOR;
2343
2344 return &entry;
2345}
2346#endif
2347
Bram Moolenaard317b382018-02-08 22:33:31 +01002348 static void
2349may_output_cursor_props(void)
2350{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002351 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002352 || last_set_cursor_shape != desired_cursor_shape
2353 || last_set_cursor_blink != desired_cursor_blink)
2354 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002355 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002356 last_set_cursor_shape = desired_cursor_shape;
2357 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002358 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002359 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002360 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002361 ui_cursor_shape_forced(TRUE);
2362 else
2363 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2364 }
2365}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002366
Bram Moolenaard317b382018-02-08 22:33:31 +01002367/*
2368 * Set the cursor color and shape, if not last set to these.
2369 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002370 static void
2371may_set_cursor_props(term_T *term)
2372{
2373#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002374 // For the GUI the cursor properties are obtained with
2375 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002376 if (gui.in_use)
2377 return;
2378#endif
2379 if (in_terminal_loop == term)
2380 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002381 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002382 desired_cursor_shape = term->tl_cursor_shape;
2383 desired_cursor_blink = term->tl_cursor_blink;
2384 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002385 }
2386}
2387
Bram Moolenaard317b382018-02-08 22:33:31 +01002388/*
2389 * Reset the desired cursor properties and restore them when needed.
2390 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002391 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002392prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002393{
2394#ifdef FEAT_GUI
2395 if (gui.in_use)
2396 return;
2397#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002398 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002399 desired_cursor_shape = -1;
2400 desired_cursor_blink = -1;
2401 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002402}
2403
2404/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002405 * Returns TRUE if the current window contains a terminal and we are sending
2406 * keys to the job.
2407 * If "check_job_status" is TRUE update the job status.
2408 */
2409 static int
2410term_use_loop_check(int check_job_status)
2411{
2412 term_T *term = curbuf->b_term;
2413
2414 return term != NULL
2415 && !term->tl_normal_mode
2416 && term->tl_vterm != NULL
2417 && term_job_running_check(term, check_job_status);
2418}
2419
2420/*
2421 * Returns TRUE if the current window contains a terminal and we are sending
2422 * keys to the job.
2423 */
2424 int
2425term_use_loop(void)
2426{
2427 return term_use_loop_check(FALSE);
2428}
2429
2430/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002431 * Called when entering a window with the mouse. If this is a terminal window
2432 * we may want to change state.
2433 */
2434 void
2435term_win_entered()
2436{
2437 term_T *term = curbuf->b_term;
2438
2439 if (term != NULL)
2440 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002441 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002442 {
2443 reset_VIsual_and_resel();
2444 if (State & INSERT)
2445 stop_insert_mode = TRUE;
2446 }
2447 mouse_was_outside = FALSE;
2448 enter_mouse_col = mouse_col;
2449 enter_mouse_row = mouse_row;
2450 }
2451}
2452
2453/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002454 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2455 * Return the Ctrl-key value in that case.
2456 */
2457 static int
2458raw_c_to_ctrl(int c)
2459{
2460 if ((mod_mask & MOD_MASK_CTRL)
2461 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2462 return c & 0x1f;
2463 return c;
2464}
2465
2466/*
2467 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2468 * May set "mod_mask".
2469 */
2470 static int
2471ctrl_to_raw_c(int c)
2472{
2473 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2474 {
2475 mod_mask |= MOD_MASK_CTRL;
2476 return c + '@';
2477 }
2478 return c;
2479}
2480
2481/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002482 * Wait for input and send it to the job.
2483 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2484 * when there is no more typahead.
2485 * Return when the start of a CTRL-W command is typed or anything else that
2486 * should be handled as a Normal mode command.
2487 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2488 * the terminal was closed.
2489 */
2490 int
2491terminal_loop(int blocking)
2492{
2493 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002494 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002495 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002496 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002497#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002498 int tty_fd = curbuf->b_term->tl_job->jv_channel
2499 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002500#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002501 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002502
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002503 // Remember the terminal we are sending keys to. However, the terminal
2504 // might be closed while waiting for a character, e.g. typing "exit" in a
2505 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2506 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002507 in_terminal_loop = curbuf->b_term;
2508
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002509 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002510 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002511 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002512 if (termwinkey == Ctrl_W)
2513 termwinkey = 0;
2514 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002515 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002516 may_set_cursor_props(curbuf->b_term);
2517
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002518 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002519 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002520#ifdef FEAT_GUI
2521 if (!curbuf->b_term->tl_system)
2522#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002523 // TODO: skip screen update when handling a sequence of keys.
2524 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002525 while (must_redraw != 0)
2526 if (update_screen(0) == FAIL)
2527 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002528 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002529 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002530 break;
2531
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002532 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002533 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002534
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002535 raw_c = term_vgetc();
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002536if (raw_c > 0)
2537 ch_log(NULL, "terminal_loop() got %d", raw_c);
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002538 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002539 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002540 // Job finished while waiting for a character. Push back the
2541 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002542 if (raw_c != K_IGNORE)
2543 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002544 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002545 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002546 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002547 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002548 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002549
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002550#ifdef UNIX
2551 /*
2552 * The shell or another program may change the tty settings. Getting
2553 * them for every typed character is a bit of overhead, but it's needed
2554 * for the first character typed, e.g. when Vim starts in a shell.
2555 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002556 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002557 {
2558 ttyinfo_T info;
2559
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002560 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002561 if (get_tty_info(tty_fd, &info) == OK)
2562 term_backspace_char = info.backspace;
2563 }
2564#endif
2565
Bram Moolenaar4f974752019-02-17 17:44:42 +01002566#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002567 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2568 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002569 if (ctrl_break_was_pressed)
2570 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2571#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002572 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2573 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002574 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002575#ifdef FEAT_GUI
2576 && !curbuf->b_term->tl_system
2577#endif
2578 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002579 {
2580 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002581 int prev_raw_c = raw_c;
2582 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002583
2584#ifdef FEAT_CMDL_INFO
2585 if (add_to_showcmd(c))
2586 out_flush();
2587#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002588 raw_c = term_vgetc();
2589 c = raw_c_to_ctrl(raw_c);
2590
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002591#ifdef FEAT_CMDL_INFO
2592 clear_showcmd();
2593#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002594 if (!term_use_loop_check(TRUE)
2595 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002596 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002597 break;
2598
2599 if (prev_c == Ctrl_BSL)
2600 {
2601 if (c == Ctrl_N)
2602 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002603 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002604 term_enter_normal_mode();
2605 ret = FAIL;
2606 goto theend;
2607 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002608 // Send both keys to the terminal, first one here, second one
2609 // below.
2610 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2611 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002612 }
2613 else if (c == Ctrl_C)
2614 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002615 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002616 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2617 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002618 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002619 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002620 // "CTRL-W .": send CTRL-W to the job
2621 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002622 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002623 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002624 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002625 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002626 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002627 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002628 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002629 else if (c == 'N')
2630 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002631 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002632 term_enter_normal_mode();
2633 ret = FAIL;
2634 goto theend;
2635 }
2636 else if (c == '"')
2637 {
2638 term_paste_register(prev_c);
2639 continue;
2640 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002641 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002642 {
Bram Moolenaara4b26992019-08-15 20:58:54 +02002643 char_u buf[MB_MAXBYTES + 2];
2644
2645 // Put the command into the typeahead buffer, when using the
2646 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2647 buf[0] = Ctrl_W;
2648 buf[(*mb_char2bytes)(c, buf + 1) + 1] = NUL;
2649 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002650 ret = OK;
2651 goto theend;
2652 }
2653 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002654# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002655 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002656 {
2657 WCHAR wc;
2658 char_u mb[3];
2659
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002660 mb[0] = (unsigned)raw_c >> 8;
2661 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002662 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002663 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002664 }
2665# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002666 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002667 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002668 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002669 // We are sure to come back here, don't reset the cursor color
2670 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002671 restore_cursor = FALSE;
2672
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002673 ret = OK;
2674 goto theend;
2675 }
2676 }
2677 ret = FAIL;
2678
2679theend:
2680 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002681 if (restore_cursor)
2682 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002683
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002684 // Move a snapshot of the screen contents to the buffer, so that completion
2685 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002686 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2687 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002688
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002689 return ret;
2690}
2691
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002692 static void
2693may_toggle_cursor(term_T *term)
2694{
2695 if (in_terminal_loop == term)
2696 {
2697 if (term->tl_cursor_visible)
2698 cursor_on();
2699 else
2700 cursor_off();
2701 }
2702}
2703
2704/*
Bram Moolenaar83d47902020-03-26 20:34:00 +01002705 * Cache "Terminal" highlight group colors.
2706 */
2707 void
2708set_terminal_default_colors(int cterm_fg, int cterm_bg)
2709{
2710 term_default_cterm_fg = cterm_fg - 1;
2711 term_default_cterm_bg = cterm_bg - 1;
2712}
2713
2714 static int
2715get_default_cterm_fg(term_T *term)
2716{
2717 if (term->tl_highlight_name != NULL)
2718 {
2719 int id = syn_name2id(term->tl_highlight_name);
2720 int fg = -1;
2721 int bg = -1;
2722
2723 if (id > 0)
2724 syn_id2cterm_bg(id, &fg, &bg);
2725 return fg;
2726 }
2727 return term_default_cterm_fg;
2728}
2729
2730 static int
2731get_default_cterm_bg(term_T *term)
2732{
2733 if (term->tl_highlight_name != NULL)
2734 {
2735 int id = syn_name2id(term->tl_highlight_name);
2736 int fg = -1;
2737 int bg = -1;
2738
2739 if (id > 0)
2740 syn_id2cterm_bg(id, &fg, &bg);
2741 return bg;
2742 }
2743 return term_default_cterm_bg;
2744}
2745
2746/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002747 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002748 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002749 */
2750 static int
2751color2index(VTermColor *color, int fg, int *boldp)
2752{
2753 int red = color->red;
2754 int blue = color->blue;
2755 int green = color->green;
2756
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002757 if (VTERM_COLOR_IS_DEFAULT_FG(color)
2758 || VTERM_COLOR_IS_DEFAULT_BG(color))
2759 return 0;
2760 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002761 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002762 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002763 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002764 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002765 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002766 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2767 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2768 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002769 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002770 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2771 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2772 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2773 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2774 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2775 case 10: return lookup_color(20, fg, boldp) + 1; // red
2776 case 11: return lookup_color(16, fg, boldp) + 1; // green
2777 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2778 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2779 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2780 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2781 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002782 }
2783 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002784
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002785 if (t_colors >= 256)
2786 {
2787 if (red == blue && red == green)
2788 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002789 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002790 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002791 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2792 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2793 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002794 int i;
2795
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002796 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002797 return 17; // 00/00/00
2798 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002799 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002800 for (i = 0; i < 23; ++i)
2801 if (red < cutoff[i])
2802 return i + 233;
2803 return 256;
2804 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002805 {
2806 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2807 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002808
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002809 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002810 for (ri = 0; ri < 5; ++ri)
2811 if (red < cutoff[ri])
2812 break;
2813 for (gi = 0; gi < 5; ++gi)
2814 if (green < cutoff[gi])
2815 break;
2816 for (bi = 0; bi < 5; ++bi)
2817 if (blue < cutoff[bi])
2818 break;
2819 return 17 + ri * 36 + gi * 6 + bi;
2820 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002821 }
2822 return 0;
2823}
2824
2825/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002826 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002827 */
2828 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002829vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002830{
2831 int attr = 0;
2832
2833 if (cellattrs.bold)
2834 attr |= HL_BOLD;
2835 if (cellattrs.underline)
2836 attr |= HL_UNDERLINE;
2837 if (cellattrs.italic)
2838 attr |= HL_ITALIC;
2839 if (cellattrs.strike)
2840 attr |= HL_STRIKETHROUGH;
2841 if (cellattrs.reverse)
2842 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002843 return attr;
2844}
2845
2846/*
2847 * Store Vterm attributes in "cell" from highlight flags.
2848 */
2849 static void
2850hl2vtermAttr(int attr, cellattr_T *cell)
2851{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002852 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002853 if (attr & HL_BOLD)
2854 cell->attrs.bold = 1;
2855 if (attr & HL_UNDERLINE)
2856 cell->attrs.underline = 1;
2857 if (attr & HL_ITALIC)
2858 cell->attrs.italic = 1;
2859 if (attr & HL_STRIKETHROUGH)
2860 cell->attrs.strike = 1;
2861 if (attr & HL_INVERSE)
2862 cell->attrs.reverse = 1;
2863}
2864
2865/*
2866 * Convert the attributes of a vterm cell into an attribute index.
2867 */
2868 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002869cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002870 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002871 win_T *wp,
2872 VTermScreenCellAttrs cellattrs,
2873 VTermColor cellfg,
2874 VTermColor cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002875{
2876 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002877
2878#ifdef FEAT_GUI
2879 if (gui.in_use)
2880 {
2881 guicolor_T fg, bg;
2882
2883 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2884 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2885 return get_gui_attr_idx(attr, fg, bg);
2886 }
2887 else
2888#endif
2889#ifdef FEAT_TERMGUICOLORS
2890 if (p_tgc)
2891 {
2892 guicolor_T fg, bg;
2893
2894 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2895 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2896
2897 return get_tgc_attr_idx(attr, fg, bg);
2898 }
2899 else
2900#endif
2901 {
2902 int bold = MAYBE;
2903 int fg = color2index(&cellfg, TRUE, &bold);
2904 int bg = color2index(&cellbg, FALSE, &bold);
2905
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002906 // Use the 'wincolor' or "Terminal" highlighting for the default
2907 // colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002908 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002909 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002910 int wincolor_fg = -1;
2911 int wincolor_bg = -1;
2912
2913 if (wp != NULL && *wp->w_p_wcr != NUL)
2914 {
2915 int id = syn_name2id(curwin->w_p_wcr);
2916
2917 // Get the 'wincolor' group colors.
2918 if (id > 0)
2919 syn_id2cterm_bg(id, &wincolor_fg, &wincolor_bg);
2920 }
2921 if (fg == 0)
2922 {
2923 if (wincolor_fg >= 0)
2924 fg = wincolor_fg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002925 else
2926 {
2927 int cterm_fg = get_default_cterm_fg(term);
2928
2929 if (cterm_fg >= 0)
2930 fg = cterm_fg + 1;
2931 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002932 }
2933 if (bg == 0)
2934 {
2935 if (wincolor_bg >= 0)
2936 bg = wincolor_bg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002937 else
2938 {
2939 int cterm_bg = get_default_cterm_bg(term);
2940
2941 if (cterm_bg >= 0)
2942 bg = cterm_bg + 1;
2943 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002944 }
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002945 }
2946
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002947 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002948 if (bold == TRUE)
2949 attr |= HL_BOLD;
2950 return get_cterm_attr_idx(attr, fg, bg);
2951 }
2952 return 0;
2953}
2954
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002955 static void
2956set_dirty_snapshot(term_T *term)
2957{
2958 term->tl_dirty_snapshot = TRUE;
2959#ifdef FEAT_TIMERS
2960 if (!term->tl_normal_mode)
2961 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002962 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002963 profile_setlimit(100L, &term->tl_timer_due);
2964 term->tl_timer_set = TRUE;
2965 }
2966#endif
2967}
2968
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002969 static int
2970handle_damage(VTermRect rect, void *user)
2971{
2972 term_T *term = (term_T *)user;
2973
2974 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2975 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002976 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002977 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002978 return 1;
2979}
2980
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002981 static void
2982term_scroll_up(term_T *term, int start_row, int count)
2983{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002984 win_T *wp = NULL;
2985 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002986 VTermColor fg, bg;
2987 VTermScreenCellAttrs attr;
2988 int clear_attr;
2989
Bram Moolenaara80faa82020-04-12 19:37:17 +02002990 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002991
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002992 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002993 {
2994 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002995 {
2996 // Set the color to clear lines with.
2997 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2998 &fg, &bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002999 clear_attr = cell2attr(term, wp, attr, fg, bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003000 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003001 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003002 }
3003}
3004
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003005 static int
3006handle_moverect(VTermRect dest, VTermRect src, void *user)
3007{
3008 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003009 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003010
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003011 // Scrolling up is done much more efficiently by deleting lines instead of
3012 // redrawing the text. But avoid doing this multiple times, postpone until
3013 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003014 if (dest.start_col == src.start_col
3015 && dest.end_col == src.end_col
3016 && dest.start_row < src.start_row)
3017 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003018 if (dest.start_row == 0)
3019 term->tl_postponed_scroll += count;
3020 else
3021 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003022 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003023
3024 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3025 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003026 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003027
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003028 // Note sure if the scrolling will work correctly, let's do a complete
3029 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003030 redraw_buf_later(term->tl_buffer, NOT_VALID);
3031 return 1;
3032}
3033
3034 static int
3035handle_movecursor(
3036 VTermPos pos,
3037 VTermPos oldpos UNUSED,
3038 int visible,
3039 void *user)
3040{
3041 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003042 win_T *wp = NULL;
3043 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003044
3045 term->tl_cursor_pos = pos;
3046 term->tl_cursor_visible = visible;
3047
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003048 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003049 {
3050 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003051 position_cursor(wp, &pos, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003052 }
3053 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
3054 {
3055 may_toggle_cursor(term);
3056 update_cursor(term, term->tl_cursor_visible);
3057 }
3058
3059 return 1;
3060}
3061
3062 static int
3063handle_settermprop(
3064 VTermProp prop,
3065 VTermValue *value,
3066 void *user)
3067{
3068 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003069 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003070
3071 switch (prop)
3072 {
3073 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003074 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003075 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003076 if (strval == NULL)
3077 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003078 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003079 // a blank title isn't useful, make it empty, so that "running" is
3080 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003081 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003082 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003083 // Same as blank
3084 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003085 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003086 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3087 term->tl_title = NULL;
3088 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003089 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003090 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003091#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003092 else if (!enc_utf8 && enc_codepage > 0)
3093 {
3094 WCHAR *ret = NULL;
3095 int length = 0;
3096
3097 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003098 (char*)value->string.str,
3099 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003100 if (ret != NULL)
3101 {
3102 WideCharToMultiByte_alloc(enc_codepage, 0,
3103 ret, length, (char**)&term->tl_title,
3104 &length, 0, 0);
3105 vim_free(ret);
3106 }
3107 }
3108#endif
3109 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003110 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003111 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003112 strval = NULL;
3113 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003114 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003115 if (term == curbuf->b_term)
3116 maketitle();
3117 break;
3118
3119 case VTERM_PROP_CURSORVISIBLE:
3120 term->tl_cursor_visible = value->boolean;
3121 may_toggle_cursor(term);
3122 out_flush();
3123 break;
3124
3125 case VTERM_PROP_CURSORBLINK:
3126 term->tl_cursor_blink = value->boolean;
3127 may_set_cursor_props(term);
3128 break;
3129
3130 case VTERM_PROP_CURSORSHAPE:
3131 term->tl_cursor_shape = value->number;
3132 may_set_cursor_props(term);
3133 break;
3134
3135 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003136 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003137 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003138 if (strval == NULL)
3139 break;
3140 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003141 may_set_cursor_props(term);
3142 break;
3143
3144 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003145 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003146 term->tl_using_altscreen = value->boolean;
3147 break;
3148
3149 default:
3150 break;
3151 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003152 vim_free(strval);
3153
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003154 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003155 return 1;
3156}
3157
3158/*
3159 * The job running in the terminal resized the terminal.
3160 */
3161 static int
3162handle_resize(int rows, int cols, void *user)
3163{
3164 term_T *term = (term_T *)user;
3165 win_T *wp;
3166
3167 term->tl_rows = rows;
3168 term->tl_cols = cols;
3169 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003170 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003171 term->tl_vterm_size_changed = FALSE;
3172 else
3173 {
3174 FOR_ALL_WINDOWS(wp)
3175 {
3176 if (wp->w_buffer == term->tl_buffer)
3177 {
3178 win_setheight_win(rows, wp);
3179 win_setwidth_win(cols, wp);
3180 }
3181 }
3182 redraw_buf_later(term->tl_buffer, NOT_VALID);
3183 }
3184 return 1;
3185}
3186
3187/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003188 * If the number of lines that are stored goes over 'termscrollback' then
3189 * delete the first 10%.
3190 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3191 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003192 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003193 static void
3194limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003195{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003196 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003197 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003198 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003199 int i;
3200
3201 curbuf = term->tl_buffer;
3202 for (i = 0; i < todo; ++i)
3203 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003204 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3205 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003206 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003207 }
3208 curbuf = curwin->w_buffer;
3209
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003210 gap->ga_len -= todo;
3211 mch_memmove(gap->ga_data,
3212 (sb_line_T *)gap->ga_data + todo,
3213 sizeof(sb_line_T) * gap->ga_len);
3214 if (update_buffer)
3215 term->tl_scrollback_scrolled -= todo;
3216 }
3217}
3218
3219/*
3220 * Handle a line that is pushed off the top of the screen.
3221 */
3222 static int
3223handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3224{
3225 term_T *term = (term_T *)user;
3226 garray_T *gap;
3227 int update_buffer;
3228
3229 if (term->tl_normal_mode)
3230 {
3231 // In Terminal-Normal mode the user interacts with the buffer, thus we
3232 // must not change it. Postpone adding the scrollback lines.
3233 gap = &term->tl_scrollback_postponed;
3234 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003235 }
3236 else
3237 {
3238 // First remove the lines that were appended before, the pushed line
3239 // goes above it.
3240 cleanup_scrollback(term);
3241 gap = &term->tl_scrollback;
3242 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003243 }
3244
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003245 limit_scrollback(term, gap, update_buffer);
3246
3247 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003248 {
3249 cellattr_T *p = NULL;
3250 int len = 0;
3251 int i;
3252 int c;
3253 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003254 int text_len;
3255 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003256 sb_line_T *line;
3257 garray_T ga;
3258 cellattr_T fill_attr = term->tl_default_color;
3259
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003260 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003261 for (i = 0; i < cols; ++i)
3262 if (cells[i].chars[0] != 0)
3263 len = i + 1;
3264 else
3265 cell2cellattr(&cells[i], &fill_attr);
3266
3267 ga_init2(&ga, 1, 100);
3268 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003269 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003270 if (p != NULL)
3271 {
3272 for (col = 0; col < len; col += cells[col].width)
3273 {
3274 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3275 {
3276 ga.ga_len = 0;
3277 break;
3278 }
3279 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3280 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3281 (char_u *)ga.ga_data + ga.ga_len);
3282 cell2cellattr(&cells[col], &p[col]);
3283 }
3284 }
3285 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003286 {
3287 if (update_buffer)
3288 text = (char_u *)"";
3289 else
3290 text = vim_strsave((char_u *)"");
3291 text_len = 0;
3292 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003293 else
3294 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003295 text = ga.ga_data;
3296 text_len = ga.ga_len;
3297 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003298 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003299 if (update_buffer)
3300 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003301
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003302 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003303 line->sb_cols = len;
3304 line->sb_cells = p;
3305 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003306 if (update_buffer)
3307 {
3308 line->sb_text = NULL;
3309 ++term->tl_scrollback_scrolled;
3310 ga_clear(&ga); // free the text
3311 }
3312 else
3313 {
3314 line->sb_text = text;
3315 ga_init(&ga); // text is kept in tl_scrollback_postponed
3316 }
3317 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003318 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003319 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003320}
3321
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003322/*
3323 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3324 * received and stored in tl_scrollback_postponed.
3325 */
3326 static void
3327handle_postponed_scrollback(term_T *term)
3328{
3329 int i;
3330
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003331 if (term->tl_scrollback_postponed.ga_len == 0)
3332 return;
3333 ch_log(NULL, "Moving postponed scrollback to scrollback");
3334
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003335 // First remove the lines that were appended before, the pushed lines go
3336 // above it.
3337 cleanup_scrollback(term);
3338
3339 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3340 {
3341 char_u *text;
3342 sb_line_T *pp_line;
3343 sb_line_T *line;
3344
3345 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3346 break;
3347 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3348
3349 text = pp_line->sb_text;
3350 if (text == NULL)
3351 text = (char_u *)"";
3352 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3353 vim_free(pp_line->sb_text);
3354
3355 line = (sb_line_T *)term->tl_scrollback.ga_data
3356 + term->tl_scrollback.ga_len;
3357 line->sb_cols = pp_line->sb_cols;
3358 line->sb_cells = pp_line->sb_cells;
3359 line->sb_fill_attr = pp_line->sb_fill_attr;
3360 line->sb_text = NULL;
3361 ++term->tl_scrollback_scrolled;
3362 ++term->tl_scrollback.ga_len;
3363 }
3364
3365 ga_clear(&term->tl_scrollback_postponed);
3366 limit_scrollback(term, &term->tl_scrollback, TRUE);
3367}
3368
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003369static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003370 handle_damage, // damage
3371 handle_moverect, // moverect
3372 handle_movecursor, // movecursor
3373 handle_settermprop, // settermprop
3374 NULL, // bell
3375 handle_resize, // resize
3376 handle_pushline, // sb_pushline
3377 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003378};
3379
3380/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003381 * Do the work after the channel of a terminal was closed.
3382 * Must be called only when updating_screen is FALSE.
3383 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3384 */
3385 static int
3386term_after_channel_closed(term_T *term)
3387{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003388 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003389 if (!term->tl_normal_mode)
3390 {
3391 int fnum = term->tl_buffer->b_fnum;
3392
3393 cleanup_vterm(term);
3394
3395 if (term->tl_finish == TL_FINISH_CLOSE)
3396 {
3397 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003398 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003399#ifdef FEAT_PROP_POPUP
3400 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003401
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003402 // If this was a terminal in a popup window, go back to the
3403 // previous window.
3404 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3405 {
3406 pwin = curwin;
3407 if (win_valid(prevwin))
3408 win_enter(prevwin, FALSE);
3409 }
3410 else
3411#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003412 // If this is the last normal window: exit Vim.
3413 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3414 {
3415 exarg_T ea;
3416
Bram Moolenaara80faa82020-04-12 19:37:17 +02003417 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003418 ex_quit(&ea);
3419 return TRUE;
3420 }
3421
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003422 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003423 ch_log(NULL, "terminal job finished, closing window");
3424 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003425 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003426 if (curwin == aucmd_win)
3427 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003428 if (do_set_w_closing)
3429 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003430 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003431 if (do_set_w_closing)
3432 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003433 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003434#ifdef FEAT_PROP_POPUP
3435 if (pwin != NULL)
3436 popup_close_with_retval(pwin, 0);
3437#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003438 return TRUE;
3439 }
3440 if (term->tl_finish == TL_FINISH_OPEN
3441 && term->tl_buffer->b_nwindows == 0)
3442 {
3443 char buf[50];
3444
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003445 // TODO: use term_opencmd
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003446 ch_log(NULL, "terminal job finished, opening window");
3447 vim_snprintf(buf, sizeof(buf),
3448 term->tl_opencmd == NULL
3449 ? "botright sbuf %d"
3450 : (char *)term->tl_opencmd, fnum);
3451 do_cmdline_cmd((char_u *)buf);
3452 }
3453 else
3454 ch_log(NULL, "terminal job finished");
3455 }
3456
3457 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3458 return FALSE;
3459}
3460
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003461#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3462/*
3463 * If the current window is a terminal in a popup window and the job has
3464 * finished, close the popup window and to back to the previous window.
3465 * Otherwise return FAIL.
3466 */
3467 int
3468may_close_term_popup(void)
3469{
3470 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3471 && !term_job_running(curbuf->b_term))
3472 {
3473 win_T *pwin = curwin;
3474
3475 if (win_valid(prevwin))
3476 win_enter(prevwin, FALSE);
3477 popup_close_with_retval(pwin, 0);
3478 return OK;
3479 }
3480 return FAIL;
3481}
3482#endif
3483
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003484/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003485 * Called when a channel has been closed.
3486 * If this was a channel for a terminal window then finish it up.
3487 */
3488 void
3489term_channel_closed(channel_T *ch)
3490{
3491 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003492 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003493 int did_one = FALSE;
3494
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003495 for (term = first_term; term != NULL; term = next_term)
3496 {
3497 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003498 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003499 {
3500 term->tl_channel_closed = TRUE;
3501 did_one = TRUE;
3502
Bram Moolenaard23a8232018-02-10 18:45:26 +01003503 VIM_CLEAR(term->tl_title);
3504 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003505#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003506 if (term->tl_out_fd != NULL)
3507 {
3508 fclose(term->tl_out_fd);
3509 term->tl_out_fd = NULL;
3510 }
3511#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003512
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003513 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003514 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003515 // Cannot open or close windows now. Can happen when
3516 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003517 term->tl_channel_recently_closed = TRUE;
3518 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003519 }
3520
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003521 if (term_after_channel_closed(term))
3522 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003523 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003524 }
3525
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003526 if (did_one)
3527 {
3528 redraw_statuslines();
3529
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003530 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003531 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003532 typebuf_was_filled = TRUE;
3533
3534 term = curbuf->b_term;
3535 if (term != NULL)
3536 {
3537 if (term->tl_job == ch->ch_job)
3538 maketitle();
3539 update_cursor(term, term->tl_cursor_visible);
3540 }
3541 }
3542}
3543
3544/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003545 * To be called after resetting updating_screen: handle any terminal where the
3546 * channel was closed.
3547 */
3548 void
3549term_check_channel_closed_recently()
3550{
3551 term_T *term;
3552 term_T *next_term;
3553
3554 for (term = first_term; term != NULL; term = next_term)
3555 {
3556 next_term = term->tl_next;
3557 if (term->tl_channel_recently_closed)
3558 {
3559 term->tl_channel_recently_closed = FALSE;
3560 if (term_after_channel_closed(term))
3561 // start over, the list may have changed
3562 next_term = first_term;
3563 }
3564 }
3565}
3566
3567/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003568 * Fill one screen line from a line of the terminal.
3569 * Advances "pos" to past the last column.
3570 */
3571 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003572term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003573 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003574 win_T *wp,
3575 VTermScreen *screen,
3576 VTermPos *pos,
3577 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003578{
3579 int off = screen_get_current_line_off();
3580
3581 for (pos->col = 0; pos->col < max_col; )
3582 {
3583 VTermScreenCell cell;
3584 int c;
3585
3586 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003587 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003588
3589 c = cell.chars[0];
3590 if (c == NUL)
3591 {
3592 ScreenLines[off] = ' ';
3593 if (enc_utf8)
3594 ScreenLinesUC[off] = NUL;
3595 }
3596 else
3597 {
3598 if (enc_utf8)
3599 {
3600 int i;
3601
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003602 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003603 for (i = 0; i < Screen_mco
3604 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3605 {
3606 ScreenLinesC[i][off] = cell.chars[i + 1];
3607 if (cell.chars[i + 1] == 0)
3608 break;
3609 }
3610 if (c >= 0x80 || (Screen_mco > 0
3611 && ScreenLinesC[0][off] != 0))
3612 {
3613 ScreenLines[off] = ' ';
3614 ScreenLinesUC[off] = c;
3615 }
3616 else
3617 {
3618 ScreenLines[off] = c;
3619 ScreenLinesUC[off] = NUL;
3620 }
3621 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003622#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003623 else if (has_mbyte && c >= 0x80)
3624 {
3625 char_u mb[MB_MAXBYTES+1];
3626 WCHAR wc = c;
3627
3628 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3629 (char*)mb, 2, 0, 0) > 1)
3630 {
3631 ScreenLines[off] = mb[0];
3632 ScreenLines[off + 1] = mb[1];
3633 cell.width = mb_ptr2cells(mb);
3634 }
3635 else
3636 ScreenLines[off] = c;
3637 }
3638#endif
3639 else
3640 ScreenLines[off] = c;
3641 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003642 ScreenAttrs[off] = cell2attr(term, wp, cell.attrs, cell.fg, cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003643
3644 ++pos->col;
3645 ++off;
3646 if (cell.width == 2)
3647 {
3648 if (enc_utf8)
3649 ScreenLinesUC[off] = NUL;
3650
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003651 // don't set the second byte to NUL for a DBCS encoding, it
3652 // has been set above
Bram Moolenaar13568252018-03-16 20:46:58 +01003653 if (enc_utf8 || !has_mbyte)
3654 ScreenLines[off] = NUL;
3655
3656 ++pos->col;
3657 ++off;
3658 }
3659 }
3660}
3661
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003662#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003663 static void
3664update_system_term(term_T *term)
3665{
3666 VTermPos pos;
3667 VTermScreen *screen;
3668
3669 if (term->tl_vterm == NULL)
3670 return;
3671 screen = vterm_obtain_screen(term->tl_vterm);
3672
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003673 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003674 while (term->tl_toprow > 0
3675 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3676 {
3677 int save_p_more = p_more;
3678
3679 p_more = FALSE;
3680 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003681 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003682 p_more = save_p_more;
3683 --term->tl_toprow;
3684 }
3685
3686 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3687 && pos.row < Rows; ++pos.row)
3688 {
3689 if (pos.row < term->tl_rows)
3690 {
3691 int max_col = MIN(Columns, term->tl_cols);
3692
Bram Moolenaar83d47902020-03-26 20:34:00 +01003693 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003694 }
3695 else
3696 pos.col = 0;
3697
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003698 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003699 }
3700
3701 term->tl_dirty_row_start = MAX_ROW;
3702 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003703}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003704#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003705
3706/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003707 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3708 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003709 * Terminal-Normal mode.
3710 */
3711 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003712term_do_update_window(win_T *wp)
3713{
3714 term_T *term = wp->w_buffer->b_term;
3715
3716 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3717}
3718
3719/*
3720 * Called to update a window that contains an active terminal.
3721 */
3722 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003723term_update_window(win_T *wp)
3724{
3725 term_T *term = wp->w_buffer->b_term;
3726 VTerm *vterm;
3727 VTermScreen *screen;
3728 VTermState *state;
3729 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003730 int rows, cols;
3731 int newrows, newcols;
3732 int minsize;
3733 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003734
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003735 vterm = term->tl_vterm;
3736 screen = vterm_obtain_screen(vterm);
3737 state = vterm_obtain_state(vterm);
3738
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003739 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3740 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003741 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003742 {
3743 term->tl_dirty_row_start = 0;
3744 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003745
3746 if (term->tl_postponed_scroll > 0
3747 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003748 // Scrolling is usually faster than redrawing, when there are only
3749 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003750 term_scroll_up(term, 0, term->tl_postponed_scroll);
3751 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003752 }
3753
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003754 /*
3755 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003756 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003757 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003758 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003759
Bram Moolenaar498c2562018-04-15 23:45:15 +02003760 newrows = 99999;
3761 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003762 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003763 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003764 // Always use curwin, it may be a popup window.
3765 win_T *wwp = twp == NULL ? curwin : twp;
3766
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003767 // When more than one window shows the same terminal, use the
3768 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003769 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003770 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003771 newrows = MIN(newrows, wwp->w_height);
3772 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003773 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003774 if (twp == NULL)
3775 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003776 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003777 if (newrows == 99999 || newcols == 99999)
3778 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003779 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3780 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3781
3782 if (term->tl_rows != newrows || term->tl_cols != newcols)
3783 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003784 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003785 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003786 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003787 newrows);
3788 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003789
3790 // Updating the terminal size will cause the snapshot to be cleared.
3791 // When not in terminal_loop() we need to restore it.
3792 if (term != in_terminal_loop)
3793 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003794 }
3795
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003796 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003797 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003798 position_cursor(wp, &pos, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003799
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003800 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3801 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003802 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003803 if (pos.row < term->tl_rows)
3804 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003805 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003806
Bram Moolenaar83d47902020-03-26 20:34:00 +01003807 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003808 }
3809 else
3810 pos.col = 0;
3811
Bram Moolenaarf118d482018-03-13 13:14:00 +01003812 screen_line(wp->w_winrow + pos.row
3813#ifdef FEAT_MENU
3814 + winbar_height(wp)
3815#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003816 , wp->w_wincol, pos.col, wp->w_width,
3817#ifdef FEAT_PROP_POPUP
3818 popup_is_popup(wp) ? SLF_POPUP :
3819#endif
3820 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003821 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003822 term->tl_dirty_row_start = MAX_ROW;
3823 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003824}
3825
3826/*
3827 * Return TRUE if "wp" is a terminal window where the job has finished.
3828 */
3829 int
3830term_is_finished(buf_T *buf)
3831{
3832 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3833}
3834
3835/*
3836 * Return TRUE if "wp" is a terminal window where the job has finished or we
3837 * are in Terminal-Normal mode, thus we show the buffer contents.
3838 */
3839 int
3840term_show_buffer(buf_T *buf)
3841{
3842 term_T *term = buf->b_term;
3843
3844 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3845}
3846
3847/*
3848 * The current buffer is going to be changed. If there is terminal
3849 * highlighting remove it now.
3850 */
3851 void
3852term_change_in_curbuf(void)
3853{
3854 term_T *term = curbuf->b_term;
3855
3856 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3857 {
3858 free_scrollback(term);
3859 redraw_buf_later(term->tl_buffer, NOT_VALID);
3860
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003861 // The buffer is now like a normal buffer, it cannot be easily
3862 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003863 set_string_option_direct((char_u *)"buftype", -1,
3864 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3865 }
3866}
3867
3868/*
3869 * Get the screen attribute for a position in the buffer.
3870 * Use a negative "col" to get the filler background color.
3871 */
3872 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003873term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003874{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003875 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003876 term_T *term = buf->b_term;
3877 sb_line_T *line;
3878 cellattr_T *cellattr;
3879
3880 if (lnum > term->tl_scrollback.ga_len)
3881 cellattr = &term->tl_default_color;
3882 else
3883 {
3884 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3885 if (col < 0 || col >= line->sb_cols)
3886 cellattr = &line->sb_fill_attr;
3887 else
3888 cellattr = line->sb_cells + col;
3889 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003890 return cell2attr(term, wp, cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003891}
3892
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003893/*
3894 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003895 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003896 */
3897 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003898cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003899{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003900 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3901 if (rgb->index == 0)
3902 rgb->type = VTERM_COLOR_RGB;
3903 else
3904 {
3905 rgb->type = VTERM_COLOR_INDEXED;
3906 --rgb->index;
3907 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003908}
3909
3910/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003911 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003912 */
3913 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003914init_default_colors(term_T *term, win_T *wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003915{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003916 VTermColor *fg, *bg;
3917 int fgval, bgval;
3918 int id;
3919
Bram Moolenaara80faa82020-04-12 19:37:17 +02003920 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003921 term->tl_default_color.width = 1;
3922 fg = &term->tl_default_color.fg;
3923 bg = &term->tl_default_color.bg;
3924
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003925 // Vterm uses a default black background. Set it to white when
3926 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003927 if (*p_bg == 'l')
3928 {
3929 fgval = 0;
3930 bgval = 255;
3931 }
3932 else
3933 {
3934 fgval = 255;
3935 bgval = 0;
3936 }
3937 fg->red = fg->green = fg->blue = fgval;
3938 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003939 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3940 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003941
Bram Moolenaar83d47902020-03-26 20:34:00 +01003942 // The 'wincolor' or the highlight group overrules the defaults.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003943 if (wp != NULL && *wp->w_p_wcr != NUL)
3944 id = syn_name2id(wp->w_p_wcr);
3945 else
Bram Moolenaar83d47902020-03-26 20:34:00 +01003946 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003947
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003948 // Use the actual color for the GUI and when 'termguicolors' is set.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003949#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3950 if (0
3951# ifdef FEAT_GUI
3952 || gui.in_use
3953# endif
3954# ifdef FEAT_TERMGUICOLORS
3955 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003956# ifdef FEAT_VTP
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003957 // Finally get INVALCOLOR on this execution path
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003958 || (!p_tgc && t_colors >= 256)
3959# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003960# endif
3961 )
3962 {
3963 guicolor_T fg_rgb = INVALCOLOR;
3964 guicolor_T bg_rgb = INVALCOLOR;
3965
3966 if (id != 0)
3967 syn_id2colors(id, &fg_rgb, &bg_rgb);
3968
3969# ifdef FEAT_GUI
3970 if (gui.in_use)
3971 {
3972 if (fg_rgb == INVALCOLOR)
3973 fg_rgb = gui.norm_pixel;
3974 if (bg_rgb == INVALCOLOR)
3975 bg_rgb = gui.back_pixel;
3976 }
3977# ifdef FEAT_TERMGUICOLORS
3978 else
3979# endif
3980# endif
3981# ifdef FEAT_TERMGUICOLORS
3982 {
3983 if (fg_rgb == INVALCOLOR)
3984 fg_rgb = cterm_normal_fg_gui_color;
3985 if (bg_rgb == INVALCOLOR)
3986 bg_rgb = cterm_normal_bg_gui_color;
3987 }
3988# endif
3989 if (fg_rgb != INVALCOLOR)
3990 {
3991 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3992
3993 fg->red = (unsigned)(rgb >> 16);
3994 fg->green = (unsigned)(rgb >> 8) & 255;
3995 fg->blue = (unsigned)rgb & 255;
3996 }
3997 if (bg_rgb != INVALCOLOR)
3998 {
3999 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
4000
4001 bg->red = (unsigned)(rgb >> 16);
4002 bg->green = (unsigned)(rgb >> 8) & 255;
4003 bg->blue = (unsigned)rgb & 255;
4004 }
4005 }
4006 else
4007#endif
4008 if (id != 0 && t_colors >= 16)
4009 {
Bram Moolenaar83d47902020-03-26 20:34:00 +01004010 int cterm_fg = get_default_cterm_fg(term);
4011 int cterm_bg = get_default_cterm_bg(term);
4012
4013 if (cterm_fg >= 0)
4014 cterm_color2vterm(cterm_fg, fg);
4015 if (cterm_bg >= 0)
4016 cterm_color2vterm(cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004017 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004018 else
4019 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004020#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004021 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004022#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004023
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004024 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004025 if (cterm_normal_fg_color > 0)
4026 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004027 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004028# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4029# ifdef VIMDLL
4030 if (!gui.in_use)
4031# endif
4032 {
4033 tmp = fg->red;
4034 fg->red = fg->blue;
4035 fg->blue = tmp;
4036 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004037# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004038 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004039# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004040 else
4041 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004042# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004043
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004044 if (cterm_normal_bg_color > 0)
4045 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004046 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004047# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4048# ifdef VIMDLL
4049 if (!gui.in_use)
4050# endif
4051 {
4052 tmp = fg->red;
4053 fg->red = fg->blue;
4054 fg->blue = tmp;
4055 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004056# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004057 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004058# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004059 else
4060 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004061# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004062 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004063}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004064
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004065#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4066/*
4067 * Set the 16 ANSI colors from array of RGB values
4068 */
4069 static void
4070set_vterm_palette(VTerm *vterm, long_u *rgb)
4071{
4072 int index = 0;
4073 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004074
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004075 for (; index < 16; index++)
4076 {
4077 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004078
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004079 color.red = (unsigned)(rgb[index] >> 16);
4080 color.green = (unsigned)(rgb[index] >> 8) & 255;
4081 color.blue = (unsigned)rgb[index] & 255;
4082 vterm_state_set_palette_color(state, index, &color);
4083 }
4084}
4085
4086/*
4087 * Set the ANSI color palette from a list of colors
4088 */
4089 static int
4090set_ansi_colors_list(VTerm *vterm, list_T *list)
4091{
4092 int n = 0;
4093 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004094 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004095
Bram Moolenaarb0992022020-01-30 14:55:42 +01004096 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004097 {
4098 char_u *color_name;
4099 guicolor_T guicolor;
4100
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004101 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004102 if (color_name == NULL)
4103 return FAIL;
4104
4105 guicolor = GUI_GET_COLOR(color_name);
4106 if (guicolor == INVALCOLOR)
4107 return FAIL;
4108
4109 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4110 }
4111
4112 if (n != 16 || li != NULL)
4113 return FAIL;
4114
4115 set_vterm_palette(vterm, rgb);
4116
4117 return OK;
4118}
4119
4120/*
4121 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4122 */
4123 static void
4124init_vterm_ansi_colors(VTerm *vterm)
4125{
4126 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4127
4128 if (var != NULL
4129 && (var->di_tv.v_type != VAR_LIST
4130 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004131 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004132 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004133 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004134}
4135#endif
4136
Bram Moolenaar52acb112018-03-18 19:20:22 +01004137/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004138 * Handles a "drop" command from the job in the terminal.
4139 * "item" is the file name, "item->li_next" may have options.
4140 */
4141 static void
4142handle_drop_command(listitem_T *item)
4143{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004144 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004145 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004146 int bufnr;
4147 win_T *wp;
4148 tabpage_T *tp;
4149 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004150 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004151
4152 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4153 FOR_ALL_TAB_WINDOWS(tp, wp)
4154 {
4155 if (wp->w_buffer->b_fnum == bufnr)
4156 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004157 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004158 goto_tabpage_win(tp, wp);
4159 return;
4160 }
4161 }
4162
Bram Moolenaara80faa82020-04-12 19:37:17 +02004163 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004164
4165 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4166 && opt_item->li_tv.vval.v_dict != NULL)
4167 {
4168 dict_T *dict = opt_item->li_tv.vval.v_dict;
4169 char_u *p;
4170
Bram Moolenaar8f667172018-12-14 15:38:31 +01004171 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004172 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004173 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004174 if (p != NULL)
4175 {
4176 if (check_ff_value(p) == FAIL)
4177 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4178 else
4179 ea.force_ff = *p;
4180 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004181 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004182 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004183 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004184 if (p != NULL)
4185 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004186 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004187 if (ea.cmd != NULL)
4188 {
4189 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4190 ea.force_enc = 11;
4191 tofree = ea.cmd;
4192 }
4193 }
4194
Bram Moolenaar8f667172018-12-14 15:38:31 +01004195 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004196 if (p != NULL)
4197 get_bad_opt(p, &ea);
4198
4199 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4200 ea.force_bin = FORCE_BIN;
4201 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4202 ea.force_bin = FORCE_BIN;
4203 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4204 ea.force_bin = FORCE_NOBIN;
4205 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4206 ea.force_bin = FORCE_NOBIN;
4207 }
4208
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004209 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004210 if (ea.cmd == NULL)
4211 ea.cmd = (char_u *)"split";
4212 ea.arg = fname;
4213 ea.cmdidx = CMD_split;
4214 ex_splitview(&ea);
4215
4216 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004217}
4218
4219/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004220 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4221 */
4222 static int
4223is_permitted_term_api(char_u *func, char_u *pat)
4224{
4225 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4226}
4227
4228/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004229 * Handles a function call from the job running in a terminal.
4230 * "item" is the function name, "item->li_next" has the arguments.
4231 */
4232 static void
4233handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4234{
4235 char_u *func;
4236 typval_T argvars[2];
4237 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004238 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004239
4240 if (item->li_next == NULL)
4241 {
4242 ch_log(channel, "Missing function arguments for call");
4243 return;
4244 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004245 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004246
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004247 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004248 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004249 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004250 return;
4251 }
4252
4253 argvars[0].v_type = VAR_NUMBER;
4254 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4255 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004256 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004257 funcexe.firstline = 1L;
4258 funcexe.lastline = 1L;
4259 funcexe.evaluate = TRUE;
4260 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004261 {
4262 clear_tv(&rettv);
4263 ch_log(channel, "Function %s called", func);
4264 }
4265 else
4266 ch_log(channel, "Calling function %s failed", func);
4267}
4268
4269/*
4270 * Called by libvterm when it cannot recognize an OSC sequence.
4271 * We recognize a terminal API command.
4272 */
4273 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004274parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004275{
4276 term_T *term = (term_T *)user;
4277 js_read_T reader;
4278 typval_T tv;
4279 channel_T *channel = term->tl_job == NULL ? NULL
4280 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004281 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004282
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004283 // We recognize only OSC 5 1 ; {command}
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004284 if (command != 51)
4285 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004286
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004287 // Concatenate what was received until the final piece is found.
4288 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4289 {
4290 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004291 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004292 }
4293 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004294 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004295 if (!frag.final)
4296 return 1;
4297
4298 ((char *)gap->ga_data)[gap->ga_len] = 0;
4299 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004300 reader.js_fill = NULL;
4301 reader.js_used = 0;
4302 if (json_decode(&reader, &tv, 0) == OK
4303 && tv.v_type == VAR_LIST
4304 && tv.vval.v_list != NULL)
4305 {
4306 listitem_T *item = tv.vval.v_list->lv_first;
4307
4308 if (item == NULL)
4309 ch_log(channel, "Missing command");
4310 else
4311 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004312 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004313
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004314 // Make sure an invoked command doesn't delete the buffer (and the
4315 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004316 ++term->tl_buffer->b_locked;
4317
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004318 item = item->li_next;
4319 if (item == NULL)
4320 ch_log(channel, "Missing argument for %s", cmd);
4321 else if (STRCMP(cmd, "drop") == 0)
4322 handle_drop_command(item);
4323 else if (STRCMP(cmd, "call") == 0)
4324 handle_call_command(term, channel, item);
4325 else
4326 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004327 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004328 }
4329 }
4330 else
4331 ch_log(channel, "Invalid JSON received");
4332
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004333 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004334 clear_tv(&tv);
4335 return 1;
4336}
4337
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004338/*
4339 * Called by libvterm when it cannot recognize a CSI sequence.
4340 * We recognize the window position report.
4341 */
4342 static int
4343parse_csi(
4344 const char *leader UNUSED,
4345 const long args[],
4346 int argcount,
4347 const char *intermed UNUSED,
4348 char command,
4349 void *user)
4350{
4351 term_T *term = (term_T *)user;
4352 char buf[100];
4353 int len;
4354 int x = 0;
4355 int y = 0;
4356 win_T *wp;
4357
4358 // We recognize only CSI 13 t
4359 if (command != 't' || argcount != 1 || args[0] != 13)
4360 return 0; // not handled
4361
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004362 // When getting the window position is not possible or it fails it results
4363 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004364#if defined(FEAT_GUI) \
4365 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4366 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004367 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004368#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004369
4370 FOR_ALL_WINDOWS(wp)
4371 if (wp->w_buffer == term->tl_buffer)
4372 break;
4373 if (wp != NULL)
4374 {
4375#ifdef FEAT_GUI
4376 if (gui.in_use)
4377 {
4378 x += wp->w_wincol * gui.char_width;
4379 y += W_WINROW(wp) * gui.char_height;
4380 }
4381 else
4382#endif
4383 {
4384 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004385 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004386 x += wp->w_wincol * 7;
4387 y += W_WINROW(wp) * 10;
4388 }
4389 }
4390
4391 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4392 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4393 (char_u *)buf, len, NULL);
4394 return 1;
4395}
4396
Bram Moolenaard8637282020-05-20 18:41:41 +02004397static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004398 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004399 parse_csi, // csi
4400 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004401 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004402};
4403
4404/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004405 * Use Vim's allocation functions for vterm so profiling works.
4406 */
4407 static void *
4408vterm_malloc(size_t size, void *data UNUSED)
4409{
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004410 return alloc_clear(size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004411}
4412
4413 static void
4414vterm_memfree(void *ptr, void *data UNUSED)
4415{
4416 vim_free(ptr);
4417}
4418
4419static VTermAllocatorFunctions vterm_allocator = {
4420 &vterm_malloc,
4421 &vterm_memfree
4422};
4423
4424/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004425 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004426 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004427 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004428 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004429create_vterm(term_T *term, int rows, int cols)
4430{
4431 VTerm *vterm;
4432 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004433 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004434 VTermValue value;
4435
Bram Moolenaar756ef112018-04-10 12:04:27 +02004436 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004437 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004438 if (vterm == NULL)
4439 return FAIL;
4440
4441 // Allocate screen and state here, so we can bail out if that fails.
4442 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004443 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004444 if (state == NULL || screen == NULL)
4445 {
4446 vterm_free(vterm);
4447 return FAIL;
4448 }
4449
Bram Moolenaar52acb112018-03-18 19:20:22 +01004450 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004451 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004452 vterm_set_utf8(vterm, 1);
4453
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004454 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004455
4456 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004457 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004458 &term->tl_default_color.fg,
4459 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004460
Bram Moolenaar9e587872019-05-13 20:27:23 +02004461 if (t_colors < 16)
4462 // Less than 16 colors: assume that bold means using a bright color for
4463 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004464 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4465
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004466 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004467 vterm_screen_reset(screen, 1 /* hard */);
4468
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004469 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004470 vterm_screen_enable_altscreen(screen, 1);
4471
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004472 // For unix do not use a blinking cursor. In an xterm this causes the
4473 // cursor to blink if it's blinking in the xterm.
4474 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004475#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004476 if (GetCaretBlinkTime() == INFINITE)
4477 value.boolean = 0;
4478 else
4479 value.boolean = 1;
4480#else
4481 value.boolean = 0;
4482#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004483 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004484 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004485
4486 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004487}
4488
4489/*
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004490 * Called when 'wincolor' was set.
4491 */
4492 void
4493term_update_colors(void)
4494{
4495 term_T *term = curwin->w_buffer->b_term;
4496
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01004497 if (term->tl_vterm == NULL)
4498 return;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004499 init_default_colors(term, curwin);
4500 vterm_state_set_default_colors(
4501 vterm_obtain_state(term->tl_vterm),
4502 &term->tl_default_color.fg,
4503 &term->tl_default_color.bg);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01004504
4505 redraw_later(NOT_VALID);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004506}
4507
4508/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004509 * Return the text to show for the buffer name and status.
4510 */
4511 char_u *
4512term_get_status_text(term_T *term)
4513{
4514 if (term->tl_status_text == NULL)
4515 {
4516 char_u *txt;
4517 size_t len;
4518
4519 if (term->tl_normal_mode)
4520 {
4521 if (term_job_running(term))
4522 txt = (char_u *)_("Terminal");
4523 else
4524 txt = (char_u *)_("Terminal-finished");
4525 }
4526 else if (term->tl_title != NULL)
4527 txt = term->tl_title;
4528 else if (term_none_open(term))
4529 txt = (char_u *)_("active");
4530 else if (term_job_running(term))
4531 txt = (char_u *)_("running");
4532 else
4533 txt = (char_u *)_("finished");
4534 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004535 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004536 if (term->tl_status_text != NULL)
4537 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
4538 term->tl_buffer->b_fname, txt);
4539 }
4540 return term->tl_status_text;
4541}
4542
4543/*
4544 * Mark references in jobs of terminals.
4545 */
4546 int
4547set_ref_in_term(int copyID)
4548{
4549 int abort = FALSE;
4550 term_T *term;
4551 typval_T tv;
4552
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004553 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004554 if (term->tl_job != NULL)
4555 {
4556 tv.v_type = VAR_JOB;
4557 tv.vval.v_job = term->tl_job;
4558 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4559 }
4560 return abort;
4561}
4562
4563/*
4564 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004565 * Returns NULL when the buffer is not for a terminal window and logs a message
4566 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004567 */
4568 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004569term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004570{
4571 buf_T *buf;
4572
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004573 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004574 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004575 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004576 --emsg_off;
4577 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004578 {
4579 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004580 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004581 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004582 return buf;
4583}
4584
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004585 static void
4586clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004587{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004588 CLEAR_FIELD(*cell);
4589 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4590 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004591}
4592
4593 static void
4594dump_term_color(FILE *fd, VTermColor *color)
4595{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004596 int index;
4597
4598 if (VTERM_COLOR_IS_INDEXED(color))
4599 index = color->index + 1;
4600 else if (color->type == 0)
4601 // use RGB values
4602 index = 255;
4603 else
4604 // default color
4605 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004606 fprintf(fd, "%02x%02x%02x%d",
4607 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004608 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004609}
4610
4611/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004612 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004613 *
4614 * Each screen cell in full is:
4615 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4616 * {characters} is a space for an empty cell
4617 * For a double-width character "+" is changed to "*" and the next cell is
4618 * skipped.
4619 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4620 * when "&" use the same as the previous cell.
4621 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4622 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4623 * {color-idx} is a number from 0 to 255
4624 *
4625 * Screen cell with same width, attributes and color as the previous one:
4626 * |{characters}
4627 *
4628 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4629 *
4630 * Repeating the previous screen cell:
4631 * @{count}
4632 */
4633 void
4634f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4635{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004636 buf_T *buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004637 term_T *term;
4638 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004639 int max_height = 0;
4640 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004641 stat_T st;
4642 FILE *fd;
4643 VTermPos pos;
4644 VTermScreen *screen;
4645 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004646 VTermState *state;
4647 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004648
4649 if (check_restricted() || check_secure())
4650 return;
4651 if (buf == NULL)
4652 return;
4653 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004654 if (term->tl_vterm == NULL)
4655 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004656 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004657 return;
4658 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004659
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004660 if (argvars[2].v_type != VAR_UNKNOWN)
4661 {
4662 dict_T *d;
4663
4664 if (argvars[2].v_type != VAR_DICT)
4665 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004666 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004667 return;
4668 }
4669 d = argvars[2].vval.v_dict;
4670 if (d != NULL)
4671 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004672 max_height = dict_get_number(d, (char_u *)"rows");
4673 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004674 }
4675 }
4676
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004677 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004678 if (fname == NULL)
4679 return;
4680 if (mch_stat((char *)fname, &st) >= 0)
4681 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004682 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004683 return;
4684 }
4685
Bram Moolenaard96ff162018-02-18 22:13:29 +01004686 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004688 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004689 return;
4690 }
4691
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004692 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004693
4694 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004695 state = vterm_obtain_state(term->tl_vterm);
4696 vterm_state_get_cursorpos(state, &cursor_pos);
4697
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004698 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4699 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004700 {
4701 int repeat = 0;
4702
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004703 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4704 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004705 {
4706 VTermScreenCell cell;
4707 int same_attr;
4708 int same_chars = TRUE;
4709 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004710 int is_cursor_pos = (pos.col == cursor_pos.col
4711 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004712
4713 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004714 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004715
4716 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4717 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004718 int c = cell.chars[i];
4719 int pc = prev_cell.chars[i];
4720
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004721 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004722 if (i == 0)
4723 {
4724 c = (c == NUL) ? ' ' : c;
4725 pc = (pc == NUL) ? ' ' : pc;
4726 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004727 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004728 same_chars = FALSE;
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004729 if (c == NUL || pc == NUL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004730 break;
4731 }
4732 same_attr = vtermAttr2hl(cell.attrs)
4733 == vtermAttr2hl(prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004734 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4735 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004736 if (same_chars && cell.width == prev_cell.width && same_attr
4737 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004738 {
4739 ++repeat;
4740 }
4741 else
4742 {
4743 if (repeat > 0)
4744 {
4745 fprintf(fd, "@%d", repeat);
4746 repeat = 0;
4747 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004748 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004749
4750 if (cell.chars[0] == NUL)
4751 fputs(" ", fd);
4752 else
4753 {
4754 char_u charbuf[10];
4755 int len;
4756
4757 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4758 && cell.chars[i] != NUL; ++i)
4759 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004760 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004761 fwrite(charbuf, len, 1, fd);
4762 }
4763 }
4764
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004765 // When only the characters differ we don't write anything, the
4766 // following "|", "@" or NL will indicate using the same
4767 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004768 if (cell.width != prev_cell.width || !same_attr)
4769 {
4770 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004771 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004772 else
4773 fputs("+", fd);
4774
4775 if (same_attr)
4776 {
4777 fputs("&", fd);
4778 }
4779 else
4780 {
4781 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004782 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004783 fputs("&", fd);
4784 else
4785 {
4786 fputs("#", fd);
4787 dump_term_color(fd, &cell.fg);
4788 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004789 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004790 fputs("&", fd);
4791 else
4792 {
4793 fputs("#", fd);
4794 dump_term_color(fd, &cell.bg);
4795 }
4796 }
4797 }
4798
4799 prev_cell = cell;
4800 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004801
4802 if (cell.width == 2)
4803 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004804 }
4805 if (repeat > 0)
4806 fprintf(fd, "@%d", repeat);
4807 fputs("\n", fd);
4808 }
4809
4810 fclose(fd);
4811}
4812
4813/*
4814 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4815 */
4816 static void
4817dump_is_corrupt(garray_T *gap)
4818{
4819 ga_concat(gap, (char_u *)"CORRUPT");
4820}
4821
4822 static void
4823append_cell(garray_T *gap, cellattr_T *cell)
4824{
4825 if (ga_grow(gap, 1) == OK)
4826 {
4827 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4828 ++gap->ga_len;
4829 }
4830}
4831
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004832 static void
4833clear_cellattr(cellattr_T *cell)
4834{
4835 CLEAR_FIELD(*cell);
4836 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4837 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4838}
4839
Bram Moolenaard96ff162018-02-18 22:13:29 +01004840/*
4841 * Read the dump file from "fd" and append lines to the current buffer.
4842 * Return the cell width of the longest line.
4843 */
4844 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004845read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004846{
4847 int c;
4848 garray_T ga_text;
4849 garray_T ga_cell;
4850 char_u *prev_char = NULL;
4851 int attr = 0;
4852 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004853 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004854 term_T *term = curbuf->b_term;
4855 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004856 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004857
4858 ga_init2(&ga_text, 1, 90);
4859 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004860 clear_cellattr(&cell);
4861 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004862 cursor_pos->row = -1;
4863 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004864
4865 c = fgetc(fd);
4866 for (;;)
4867 {
4868 if (c == EOF)
4869 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004870 if (c == '\r')
4871 {
4872 // DOS line endings? Ignore.
4873 c = fgetc(fd);
4874 }
4875 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004876 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004877 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004878 if (ga_text.ga_data == NULL)
4879 dump_is_corrupt(&ga_text);
4880 if (ga_grow(&term->tl_scrollback, 1) == OK)
4881 {
4882 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
4883 + term->tl_scrollback.ga_len;
4884
4885 if (max_cells < ga_cell.ga_len)
4886 max_cells = ga_cell.ga_len;
4887 line->sb_cols = ga_cell.ga_len;
4888 line->sb_cells = ga_cell.ga_data;
4889 line->sb_fill_attr = term->tl_default_color;
4890 ++term->tl_scrollback.ga_len;
4891 ga_init(&ga_cell);
4892
4893 ga_append(&ga_text, NUL);
4894 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4895 ga_text.ga_len, FALSE);
4896 }
4897 else
4898 ga_clear(&ga_cell);
4899 ga_text.ga_len = 0;
4900
4901 c = fgetc(fd);
4902 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004903 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004904 {
4905 int prev_len = ga_text.ga_len;
4906
Bram Moolenaar9271d052018-02-25 21:39:46 +01004907 if (c == '>')
4908 {
4909 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004910 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01004911 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
4912 cursor_pos->col = ga_cell.ga_len;
4913 }
4914
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004915 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01004916 c = fgetc(fd);
4917 if (c != EOF)
4918 ga_append(&ga_text, c);
4919 for (;;)
4920 {
4921 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004922 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01004923 || c == EOF || c == '\n')
4924 break;
4925 ga_append(&ga_text, c);
4926 }
4927
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004928 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01004929 vim_free(prev_char);
4930 if (ga_text.ga_data != NULL)
4931 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
4932 ga_text.ga_len - prev_len);
4933
Bram Moolenaar9271d052018-02-25 21:39:46 +01004934 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004935 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004936 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01004937 }
4938 else if (c == '+' || c == '*')
4939 {
4940 int is_bg;
4941
4942 cell.width = c == '+' ? 1 : 2;
4943
4944 c = fgetc(fd);
4945 if (c == '&')
4946 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004947 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01004948 c = fgetc(fd);
4949 }
4950 else if (isdigit(c))
4951 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004952 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01004953 attr = 0;
4954 while (isdigit(c))
4955 {
4956 attr = attr * 10 + (c - '0');
4957 c = fgetc(fd);
4958 }
4959 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004960
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004961 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004962 for (is_bg = 0; is_bg <= 1; ++is_bg)
4963 {
4964 if (c == '&')
4965 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004966 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004967 c = fgetc(fd);
4968 }
4969 else if (c == '#')
4970 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004971 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004972
4973 c = fgetc(fd);
4974 red = hex2nr(c);
4975 c = fgetc(fd);
4976 red = (red << 4) + hex2nr(c);
4977 c = fgetc(fd);
4978 green = hex2nr(c);
4979 c = fgetc(fd);
4980 green = (green << 4) + hex2nr(c);
4981 c = fgetc(fd);
4982 blue = hex2nr(c);
4983 c = fgetc(fd);
4984 blue = (blue << 4) + hex2nr(c);
4985 c = fgetc(fd);
4986 if (!isdigit(c))
4987 dump_is_corrupt(&ga_text);
4988 while (isdigit(c))
4989 {
4990 index = index * 10 + (c - '0');
4991 c = fgetc(fd);
4992 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004993 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004994 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004995 type = VTERM_COLOR_RGB;
4996 if (index == 0)
4997 {
4998 if (is_bg)
4999 type |= VTERM_COLOR_DEFAULT_BG;
5000 else
5001 type |= VTERM_COLOR_DEFAULT_FG;
5002 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005003 }
5004 else
5005 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005006 type = VTERM_COLOR_INDEXED;
5007 index -= 1;
5008 }
5009 if (is_bg)
5010 {
5011 cell.bg.type = type;
5012 cell.bg.red = red;
5013 cell.bg.green = green;
5014 cell.bg.blue = blue;
5015 cell.bg.index = index;
5016 }
5017 else
5018 {
5019 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005020 cell.fg.red = red;
5021 cell.fg.green = green;
5022 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005023 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005024 }
5025 }
5026 else
5027 dump_is_corrupt(&ga_text);
5028 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005029 }
5030 else
5031 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005032 }
5033 else
5034 dump_is_corrupt(&ga_text);
5035
5036 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005037 if (cell.width == 2)
5038 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005039 }
5040 else if (c == '@')
5041 {
5042 if (prev_char == NULL)
5043 dump_is_corrupt(&ga_text);
5044 else
5045 {
5046 int count = 0;
5047
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005048 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005049 for (;;)
5050 {
5051 c = fgetc(fd);
5052 if (!isdigit(c))
5053 break;
5054 count = count * 10 + (c - '0');
5055 }
5056
5057 while (count-- > 0)
5058 {
5059 ga_concat(&ga_text, prev_char);
5060 append_cell(&ga_cell, &cell);
5061 }
5062 }
5063 }
5064 else
5065 {
5066 dump_is_corrupt(&ga_text);
5067 c = fgetc(fd);
5068 }
5069 }
5070
5071 if (ga_text.ga_len > 0)
5072 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005073 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005074 dump_is_corrupt(&ga_text);
5075 ga_append(&ga_text, NUL);
5076 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5077 ga_text.ga_len, FALSE);
5078 }
5079
5080 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005081 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005082 vim_free(prev_char);
5083
5084 return max_cells;
5085}
5086
5087/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005088 * Return an allocated string with at least "text_width" "=" characters and
5089 * "fname" inserted in the middle.
5090 */
5091 static char_u *
5092get_separator(int text_width, char_u *fname)
5093{
5094 int width = MAX(text_width, curwin->w_width);
5095 char_u *textline;
5096 int fname_size;
5097 char_u *p = fname;
5098 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005099 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005100
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005101 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005102 if (textline == NULL)
5103 return NULL;
5104
5105 fname_size = vim_strsize(fname);
5106 if (fname_size < width - 8)
5107 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005108 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005109 width = MAX(text_width, fname_size + 8);
5110 }
5111 else if (fname_size > width - 8)
5112 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005113 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005114 p = gettail(fname);
5115 fname_size = vim_strsize(p);
5116 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005117 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005118 while (fname_size > width - 8)
5119 {
5120 p += (*mb_ptr2len)(p);
5121 fname_size = vim_strsize(p);
5122 }
5123
5124 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5125 textline[i] = '=';
5126 textline[i++] = ' ';
5127
5128 STRCPY(textline + i, p);
5129 off = STRLEN(textline);
5130 textline[off] = ' ';
5131 for (i = 1; i < (width - fname_size) / 2; ++i)
5132 textline[off + i] = '=';
5133 textline[off + i] = NUL;
5134
5135 return textline;
5136}
5137
5138/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005139 * Common for "term_dumpdiff()" and "term_dumpload()".
5140 */
5141 static void
5142term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5143{
5144 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005145 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005146 char_u buf1[NUMBUFLEN];
5147 char_u buf2[NUMBUFLEN];
5148 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005149 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005150 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005151 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005152 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005153 char_u *textline = NULL;
5154
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005155 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005156 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005157 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005158 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005159 if (fname1 == NULL || (do_diff && fname2 == NULL))
5160 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005161 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005162 return;
5163 }
5164 fd1 = mch_fopen((char *)fname1, READBIN);
5165 if (fd1 == NULL)
5166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005167 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005168 return;
5169 }
5170 if (do_diff)
5171 {
5172 fd2 = mch_fopen((char *)fname2, READBIN);
5173 if (fd2 == NULL)
5174 {
5175 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005176 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005177 return;
5178 }
5179 }
5180
5181 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005182 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5183 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5184 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5185 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5186 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005187
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005188 if (opt.jo_term_name == NULL)
5189 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005190 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005191
Bram Moolenaar51e14382019-05-25 20:21:28 +02005192 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005193 if (fname_tofree != NULL)
5194 {
5195 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5196 opt.jo_term_name = fname_tofree;
5197 }
5198 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005199
Bram Moolenaar87abab92019-06-03 21:14:59 +02005200 if (opt.jo_bufnr_buf != NULL)
5201 {
5202 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5203
5204 // With "bufnr" argument: enter the window with this buffer and make it
5205 // empty.
5206 if (wp == NULL)
5207 semsg(_(e_invarg2), "bufnr");
5208 else
5209 {
5210 buf = curbuf;
5211 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005212 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005213 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005214 redraw_later(NOT_VALID);
5215 }
5216 }
5217 else
5218 // Create a new terminal window.
5219 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5220
Bram Moolenaard96ff162018-02-18 22:13:29 +01005221 if (buf != NULL && buf->b_term != NULL)
5222 {
5223 int i;
5224 linenr_T bot_lnum;
5225 linenr_T lnum;
5226 term_T *term = buf->b_term;
5227 int width;
5228 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005229 VTermPos cursor_pos1;
5230 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005231
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005232 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005233
Bram Moolenaard96ff162018-02-18 22:13:29 +01005234 rettv->vval.v_number = buf->b_fnum;
5235
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005236 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005237 width = read_dump_file(fd1, &cursor_pos1);
5238
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005239 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005240 if (cursor_pos1.row >= 0)
5241 {
5242 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5243 coladvance(cursor_pos1.col);
5244 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005245
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005246 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005247 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005248
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005249 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005250 if (!do_diff)
5251 goto theend;
5252
5253 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5254
Bram Moolenaar4a696342018-04-05 18:45:26 +02005255 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005256 if (textline == NULL)
5257 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005258 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5259 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5260 vim_free(textline);
5261
5262 textline = get_separator(width, fname2);
5263 if (textline == NULL)
5264 goto theend;
5265 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5266 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005267 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005268
5269 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005270 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005271 if (width2 > width)
5272 {
5273 vim_free(textline);
5274 textline = alloc(width2 + 1);
5275 if (textline == NULL)
5276 goto theend;
5277 width = width2;
5278 textline[width] = NUL;
5279 }
5280 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5281
5282 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5283 {
5284 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5285 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005286 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005287 for (i = 0; i < width; ++i)
5288 textline[i] = '-';
5289 }
5290 else
5291 {
5292 char_u *line1;
5293 char_u *line2;
5294 char_u *p1;
5295 char_u *p2;
5296 int col;
5297 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5298 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5299 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5300 ->sb_cells;
5301
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005302 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005303 line1 = vim_strsave(ml_get(lnum));
5304 if (line1 == NULL)
5305 break;
5306 p1 = line1;
5307
5308 line2 = ml_get(lnum + bot_lnum);
5309 p2 = line2;
5310 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5311 {
5312 int len1 = utfc_ptr2len(p1);
5313 int len2 = utfc_ptr2len(p2);
5314
5315 textline[col] = ' ';
5316 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005317 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005318 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005319 else if (lnum == cursor_pos1.row + 1
5320 && col == cursor_pos1.col
5321 && (cursor_pos1.row != cursor_pos2.row
5322 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005323 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005324 textline[col] = '>';
5325 else if (lnum == cursor_pos2.row + 1
5326 && col == cursor_pos2.col
5327 && (cursor_pos1.row != cursor_pos2.row
5328 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005329 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005330 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005331 else if (cellattr1 != NULL && cellattr2 != NULL)
5332 {
5333 if ((cellattr1 + col)->width
5334 != (cellattr2 + col)->width)
5335 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005336 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005337 &(cellattr2 + col)->fg))
5338 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005339 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005340 &(cellattr2 + col)->bg))
5341 textline[col] = 'b';
5342 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5343 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5344 textline[col] = 'a';
5345 }
5346 p1 += len1;
5347 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005348 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005349 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005350
5351 while (col < width)
5352 {
5353 if (*p1 == NUL && *p2 == NUL)
5354 textline[col] = '?';
5355 else if (*p1 == NUL)
5356 {
5357 textline[col] = '+';
5358 p2 += utfc_ptr2len(p2);
5359 }
5360 else
5361 {
5362 textline[col] = '-';
5363 p1 += utfc_ptr2len(p1);
5364 }
5365 ++col;
5366 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005367
5368 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005369 }
5370 if (add_empty_scrollback(term, &term->tl_default_color,
5371 term->tl_top_diff_rows) == OK)
5372 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5373 ++bot_lnum;
5374 }
5375
5376 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5377 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005378 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005379 for (i = 0; i < width; ++i)
5380 textline[i] = '+';
5381 if (add_empty_scrollback(term, &term->tl_default_color,
5382 term->tl_top_diff_rows) == OK)
5383 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5384 ++lnum;
5385 ++bot_lnum;
5386 }
5387
5388 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005389
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005390 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005391 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005392 }
5393
5394theend:
5395 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005396 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005397 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005398 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005399 fclose(fd2);
5400}
5401
5402/*
5403 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5404 * bottom files.
5405 * Return FAIL when this is not possible.
5406 */
5407 int
5408term_swap_diff()
5409{
5410 term_T *term = curbuf->b_term;
5411 linenr_T line_count;
5412 linenr_T top_rows;
5413 linenr_T bot_rows;
5414 linenr_T bot_start;
5415 linenr_T lnum;
5416 char_u *p;
5417 sb_line_T *sb_line;
5418
5419 if (term == NULL
5420 || !term_is_finished(curbuf)
5421 || term->tl_top_diff_rows == 0
5422 || term->tl_scrollback.ga_len == 0)
5423 return FAIL;
5424
5425 line_count = curbuf->b_ml.ml_line_count;
5426 top_rows = term->tl_top_diff_rows;
5427 bot_rows = term->tl_bot_diff_rows;
5428 bot_start = line_count - bot_rows;
5429 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5430
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005431 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005432 for (lnum = 1; lnum <= top_rows; ++lnum)
5433 {
5434 p = vim_strsave(ml_get(1));
5435 if (p == NULL)
5436 return OK;
5437 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005438 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005439 vim_free(p);
5440 }
5441
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005442 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005443 for (lnum = 1; lnum <= bot_rows; ++lnum)
5444 {
5445 p = vim_strsave(ml_get(bot_start + lnum));
5446 if (p == NULL)
5447 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005448 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005449 ml_append(lnum - 1, p, 0, FALSE);
5450 vim_free(p);
5451 }
5452
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005453 // move top title to bottom
5454 p = vim_strsave(ml_get(bot_rows + 1));
5455 if (p == NULL)
5456 return OK;
5457 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005458 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005459 vim_free(p);
5460
5461 // move bottom title to top
5462 p = vim_strsave(ml_get(line_count - top_rows));
5463 if (p == NULL)
5464 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005465 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005466 ml_append(bot_rows, p, 0, FALSE);
5467 vim_free(p);
5468
Bram Moolenaard96ff162018-02-18 22:13:29 +01005469 if (top_rows == bot_rows)
5470 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005471 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005472 for (lnum = 0; lnum < top_rows; ++lnum)
5473 {
5474 sb_line_T temp;
5475
5476 temp = *(sb_line + lnum);
5477 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5478 *(sb_line + bot_start + lnum) = temp;
5479 }
5480 }
5481 else
5482 {
5483 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005484 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005485
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005486 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005487 if (temp != NULL)
5488 {
5489 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5490 mch_memmove(term->tl_scrollback.ga_data,
5491 temp + bot_start,
5492 sizeof(sb_line_T) * bot_rows);
5493 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5494 temp + top_rows,
5495 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5496 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5497 + line_count - top_rows,
5498 temp,
5499 sizeof(sb_line_T) * top_rows);
5500 vim_free(temp);
5501 }
5502 }
5503
5504 term->tl_top_diff_rows = bot_rows;
5505 term->tl_bot_diff_rows = top_rows;
5506
5507 update_screen(NOT_VALID);
5508 return OK;
5509}
5510
5511/*
5512 * "term_dumpdiff(filename, filename, options)" function
5513 */
5514 void
5515f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5516{
5517 term_load_dump(argvars, rettv, TRUE);
5518}
5519
5520/*
5521 * "term_dumpload(filename, options)" function
5522 */
5523 void
5524f_term_dumpload(typval_T *argvars, typval_T *rettv)
5525{
5526 term_load_dump(argvars, rettv, FALSE);
5527}
5528
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005529/*
5530 * "term_getaltscreen(buf)" function
5531 */
5532 void
5533f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5534{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005535 buf_T *buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005536
5537 if (buf == NULL)
5538 return;
5539 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5540}
5541
5542/*
5543 * "term_getattr(attr, name)" function
5544 */
5545 void
5546f_term_getattr(typval_T *argvars, typval_T *rettv)
5547{
5548 int attr;
5549 size_t i;
5550 char_u *name;
5551
5552 static struct {
5553 char *name;
5554 int attr;
5555 } attrs[] = {
5556 {"bold", HL_BOLD},
5557 {"italic", HL_ITALIC},
5558 {"underline", HL_UNDERLINE},
5559 {"strike", HL_STRIKETHROUGH},
5560 {"reverse", HL_INVERSE},
5561 };
5562
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005563 attr = tv_get_number(&argvars[0]);
5564 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005565 if (name == NULL)
5566 return;
5567
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005568 if (attr > HL_ALL)
5569 attr = syn_attr2attr(attr);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005570 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
5571 if (STRCMP(name, attrs[i].name) == 0)
5572 {
5573 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5574 break;
5575 }
5576}
5577
5578/*
5579 * "term_getcursor(buf)" function
5580 */
5581 void
5582f_term_getcursor(typval_T *argvars, typval_T *rettv)
5583{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005584 buf_T *buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005585 term_T *term;
5586 list_T *l;
5587 dict_T *d;
5588
5589 if (rettv_list_alloc(rettv) == FAIL)
5590 return;
5591 if (buf == NULL)
5592 return;
5593 term = buf->b_term;
5594
5595 l = rettv->vval.v_list;
5596 list_append_number(l, term->tl_cursor_pos.row + 1);
5597 list_append_number(l, term->tl_cursor_pos.col + 1);
5598
5599 d = dict_alloc();
5600 if (d != NULL)
5601 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005602 dict_add_number(d, "visible", term->tl_cursor_visible);
5603 dict_add_number(d, "blink", blink_state_is_inverted()
5604 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5605 dict_add_number(d, "shape", term->tl_cursor_shape);
5606 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005607 list_append_dict(l, d);
5608 }
5609}
5610
5611/*
5612 * "term_getjob(buf)" function
5613 */
5614 void
5615f_term_getjob(typval_T *argvars, typval_T *rettv)
5616{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005617 buf_T *buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005618
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005619 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005620 {
5621 rettv->v_type = VAR_SPECIAL;
5622 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005623 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005624 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005625
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005626 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005627 rettv->vval.v_job = buf->b_term->tl_job;
5628 if (rettv->vval.v_job != NULL)
5629 ++rettv->vval.v_job->jv_refcount;
5630}
5631
5632 static int
5633get_row_number(typval_T *tv, term_T *term)
5634{
5635 if (tv->v_type == VAR_STRING
5636 && tv->vval.v_string != NULL
5637 && STRCMP(tv->vval.v_string, ".") == 0)
5638 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005639 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005640}
5641
5642/*
5643 * "term_getline(buf, row)" function
5644 */
5645 void
5646f_term_getline(typval_T *argvars, typval_T *rettv)
5647{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005648 buf_T *buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005649 term_T *term;
5650 int row;
5651
5652 rettv->v_type = VAR_STRING;
5653 if (buf == NULL)
5654 return;
5655 term = buf->b_term;
5656 row = get_row_number(&argvars[1], term);
5657
5658 if (term->tl_vterm == NULL)
5659 {
5660 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5661
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005662 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005663 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5664 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5665 }
5666 else
5667 {
5668 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5669 VTermRect rect;
5670 int len;
5671 char_u *p;
5672
5673 if (row < 0 || row >= term->tl_rows)
5674 return;
5675 len = term->tl_cols * MB_MAXBYTES + 1;
5676 p = alloc(len);
5677 if (p == NULL)
5678 return;
5679 rettv->vval.v_string = p;
5680
5681 rect.start_col = 0;
5682 rect.end_col = term->tl_cols;
5683 rect.start_row = row;
5684 rect.end_row = row + 1;
5685 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5686 }
5687}
5688
5689/*
5690 * "term_getscrolled(buf)" function
5691 */
5692 void
5693f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5694{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005695 buf_T *buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005696
5697 if (buf == NULL)
5698 return;
5699 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5700}
5701
5702/*
5703 * "term_getsize(buf)" function
5704 */
5705 void
5706f_term_getsize(typval_T *argvars, typval_T *rettv)
5707{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005708 buf_T *buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005709 list_T *l;
5710
5711 if (rettv_list_alloc(rettv) == FAIL)
5712 return;
5713 if (buf == NULL)
5714 return;
5715
5716 l = rettv->vval.v_list;
5717 list_append_number(l, buf->b_term->tl_rows);
5718 list_append_number(l, buf->b_term->tl_cols);
5719}
5720
5721/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005722 * "term_setsize(buf, rows, cols)" function
5723 */
5724 void
5725f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5726{
5727 buf_T *buf = term_get_buf(argvars, "term_setsize()");
5728 term_T *term;
5729 varnumber_T rows, cols;
5730
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005731 if (buf == NULL)
5732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005733 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005734 return;
5735 }
5736 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005737 return;
5738 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005739 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005740 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005741 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005742 cols = cols <= 0 ? term->tl_cols : cols;
5743 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005744 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005745
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005746 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005747 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5748 term_report_winsize(term, term->tl_rows, term->tl_cols);
5749}
5750
5751/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005752 * "term_getstatus(buf)" function
5753 */
5754 void
5755f_term_getstatus(typval_T *argvars, typval_T *rettv)
5756{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005757 buf_T *buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005758 term_T *term;
5759 char_u val[100];
5760
5761 rettv->v_type = VAR_STRING;
5762 if (buf == NULL)
5763 return;
5764 term = buf->b_term;
5765
5766 if (term_job_running(term))
5767 STRCPY(val, "running");
5768 else
5769 STRCPY(val, "finished");
5770 if (term->tl_normal_mode)
5771 STRCAT(val, ",normal");
5772 rettv->vval.v_string = vim_strsave(val);
5773}
5774
5775/*
5776 * "term_gettitle(buf)" function
5777 */
5778 void
5779f_term_gettitle(typval_T *argvars, typval_T *rettv)
5780{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005781 buf_T *buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005782
5783 rettv->v_type = VAR_STRING;
5784 if (buf == NULL)
5785 return;
5786
5787 if (buf->b_term->tl_title != NULL)
5788 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5789}
5790
5791/*
5792 * "term_gettty(buf)" function
5793 */
5794 void
5795f_term_gettty(typval_T *argvars, typval_T *rettv)
5796{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005797 buf_T *buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005798 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005799 int num = 0;
5800
5801 rettv->v_type = VAR_STRING;
5802 if (buf == NULL)
5803 return;
5804 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02005805 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005806
5807 switch (num)
5808 {
5809 case 0:
5810 if (buf->b_term->tl_job != NULL)
5811 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005812 break;
5813 case 1:
5814 if (buf->b_term->tl_job != NULL)
5815 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005816 break;
5817 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005818 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005819 return;
5820 }
5821 if (p != NULL)
5822 rettv->vval.v_string = vim_strsave(p);
5823}
5824
5825/*
5826 * "term_list()" function
5827 */
5828 void
5829f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
5830{
5831 term_T *tp;
5832 list_T *l;
5833
5834 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
5835 return;
5836
5837 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005838 FOR_ALL_TERMS(tp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005839 if (tp != NULL && tp->tl_buffer != NULL)
5840 if (list_append_number(l,
5841 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
5842 return;
5843}
5844
5845/*
5846 * "term_scrape(buf, row)" function
5847 */
5848 void
5849f_term_scrape(typval_T *argvars, typval_T *rettv)
5850{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005851 buf_T *buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005852 VTermScreen *screen = NULL;
5853 VTermPos pos;
5854 list_T *l;
5855 term_T *term;
5856 char_u *p;
5857 sb_line_T *line;
5858
5859 if (rettv_list_alloc(rettv) == FAIL)
5860 return;
5861 if (buf == NULL)
5862 return;
5863 term = buf->b_term;
5864
5865 l = rettv->vval.v_list;
5866 pos.row = get_row_number(&argvars[1], term);
5867
5868 if (term->tl_vterm != NULL)
5869 {
5870 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01005871 if (screen == NULL) // can't really happen
5872 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005873 p = NULL;
5874 line = NULL;
5875 }
5876 else
5877 {
5878 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
5879
5880 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
5881 return;
5882 p = ml_get_buf(buf, lnum + 1, FALSE);
5883 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
5884 }
5885
5886 for (pos.col = 0; pos.col < term->tl_cols; )
5887 {
5888 dict_T *dcell;
5889 int width;
5890 VTermScreenCellAttrs attrs;
5891 VTermColor fg, bg;
5892 char_u rgb[8];
5893 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
5894 int off = 0;
5895 int i;
5896
5897 if (screen == NULL)
5898 {
5899 cellattr_T *cellattr;
5900 int len;
5901
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005902 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005903 if (pos.col >= line->sb_cols)
5904 break;
5905 cellattr = line->sb_cells + pos.col;
5906 width = cellattr->width;
5907 attrs = cellattr->attrs;
5908 fg = cellattr->fg;
5909 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02005910 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005911 mch_memmove(mbs, p, len);
5912 mbs[len] = NUL;
5913 p += len;
5914 }
5915 else
5916 {
5917 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005918
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005919 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
5920 break;
5921 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
5922 {
5923 if (cell.chars[i] == 0)
5924 break;
5925 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
5926 }
5927 mbs[off] = NUL;
5928 width = cell.width;
5929 attrs = cell.attrs;
5930 fg = cell.fg;
5931 bg = cell.bg;
5932 }
5933 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01005934 if (dcell == NULL)
5935 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005936 list_append_dict(l, dcell);
5937
Bram Moolenaare0be1672018-07-08 16:50:37 +02005938 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005939
5940 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5941 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005942 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005943 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5944 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005945 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005946
Bram Moolenaar83d47902020-03-26 20:34:00 +01005947 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02005948 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005949
5950 ++pos.col;
5951 if (width == 2)
5952 ++pos.col;
5953 }
5954}
5955
5956/*
5957 * "term_sendkeys(buf, keys)" function
5958 */
5959 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01005960f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005961{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005962 buf_T *buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005963 char_u *msg;
5964 term_T *term;
5965
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005966 if (buf == NULL)
5967 return;
5968
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005969 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005970 if (msg == NULL)
5971 return;
5972 term = buf->b_term;
5973 if (term->tl_vterm == NULL)
5974 return;
5975
5976 while (*msg != NUL)
5977 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02005978 int c;
5979
5980 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
5981 {
5982 c = TO_SPECIAL(msg[1], msg[2]);
5983 msg += 3;
5984 }
5985 else
5986 {
5987 c = PTR2CHAR(msg);
5988 msg += MB_CPTR2LEN(msg);
5989 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01005990 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005991 }
5992}
5993
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005994#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
5995/*
5996 * "term_getansicolors(buf)" function
5997 */
5998 void
5999f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6000{
6001 buf_T *buf = term_get_buf(argvars, "term_getansicolors()");
6002 term_T *term;
6003 VTermState *state;
6004 VTermColor color;
6005 char_u hexbuf[10];
6006 int index;
6007 list_T *list;
6008
6009 if (rettv_list_alloc(rettv) == FAIL)
6010 return;
6011
6012 if (buf == NULL)
6013 return;
6014 term = buf->b_term;
6015 if (term->tl_vterm == NULL)
6016 return;
6017
6018 list = rettv->vval.v_list;
6019 state = vterm_obtain_state(term->tl_vterm);
6020 for (index = 0; index < 16; index++)
6021 {
6022 vterm_state_get_palette_color(state, index, &color);
6023 sprintf((char *)hexbuf, "#%02x%02x%02x",
6024 color.red, color.green, color.blue);
6025 if (list_append_string(list, hexbuf, 7) == FAIL)
6026 return;
6027 }
6028}
6029
6030/*
6031 * "term_setansicolors(buf, list)" function
6032 */
6033 void
6034f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6035{
6036 buf_T *buf = term_get_buf(argvars, "term_setansicolors()");
6037 term_T *term;
6038
6039 if (buf == NULL)
6040 return;
6041 term = buf->b_term;
6042 if (term->tl_vterm == NULL)
6043 return;
6044
6045 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6046 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006047 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006048 return;
6049 }
6050
6051 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006052 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006053}
6054#endif
6055
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006056/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006057 * "term_setapi(buf, api)" function
6058 */
6059 void
6060f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6061{
6062 buf_T *buf = term_get_buf(argvars, "term_setapi()");
6063 term_T *term;
6064 char_u *api;
6065
6066 if (buf == NULL)
6067 return;
6068 term = buf->b_term;
6069 vim_free(term->tl_api);
6070 api = tv_get_string_chk(&argvars[1]);
6071 if (api != NULL)
6072 term->tl_api = vim_strsave(api);
6073 else
6074 term->tl_api = NULL;
6075}
6076
6077/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006078 * "term_setrestore(buf, command)" function
6079 */
6080 void
6081f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6082{
6083#if defined(FEAT_SESSION)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006084 buf_T *buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006085 term_T *term;
6086 char_u *cmd;
6087
6088 if (buf == NULL)
6089 return;
6090 term = buf->b_term;
6091 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006092 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006093 if (cmd != NULL)
6094 term->tl_command = vim_strsave(cmd);
6095 else
6096 term->tl_command = NULL;
6097#endif
6098}
6099
6100/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006101 * "term_setkill(buf, how)" function
6102 */
6103 void
6104f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6105{
6106 buf_T *buf = term_get_buf(argvars, "term_setkill()");
6107 term_T *term;
6108 char_u *how;
6109
6110 if (buf == NULL)
6111 return;
6112 term = buf->b_term;
6113 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006114 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006115 if (how != NULL)
6116 term->tl_kill = vim_strsave(how);
6117 else
6118 term->tl_kill = NULL;
6119}
6120
6121/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006122 * "term_start(command, options)" function
6123 */
6124 void
6125f_term_start(typval_T *argvars, typval_T *rettv)
6126{
6127 jobopt_T opt;
6128 buf_T *buf;
6129
6130 init_job_options(&opt);
6131 if (argvars[1].v_type != VAR_UNKNOWN
6132 && get_job_options(&argvars[1], &opt,
6133 JO_TIMEOUT_ALL + JO_STOPONEXIT
6134 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6135 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6136 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6137 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006138 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006139 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006140 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006141 return;
6142
Bram Moolenaar13568252018-03-16 20:46:58 +01006143 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006144
6145 if (buf != NULL && buf->b_term != NULL)
6146 rettv->vval.v_number = buf->b_fnum;
6147}
6148
6149/*
6150 * "term_wait" function
6151 */
6152 void
6153f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6154{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006155 buf_T *buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006156
6157 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006158 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006159 if (buf->b_term->tl_job == NULL)
6160 {
6161 ch_log(NULL, "term_wait(): no job to wait for");
6162 return;
6163 }
6164 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006165 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006166 return;
6167
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006168 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006169 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006170 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6171 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006172 // The job is dead, keep reading channel I/O until the channel is
6173 // closed. buf->b_term may become NULL if the terminal was closed while
6174 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006175 ch_log(NULL, "term_wait(): waiting for channel to close");
6176 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6177 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006178 term_flush_messages();
6179
Bram Moolenaard45aa552018-05-21 22:50:29 +02006180 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006181 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006182 // If the terminal is closed when the channel is closed the
6183 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006184 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006185 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006186
6187 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006188 }
6189 else
6190 {
6191 long wait = 10L;
6192
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006193 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006194
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006195 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006196 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006197 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006198 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006199
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006200 // Flushing messages on channels is hopefully sufficient.
6201 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006202 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006203 }
6204}
6205
6206/*
6207 * Called when a channel has sent all the lines to a terminal.
6208 * Send a CTRL-D to mark the end of the text.
6209 */
6210 void
6211term_send_eof(channel_T *ch)
6212{
6213 term_T *term;
6214
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006215 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006216 if (term->tl_job == ch->ch_job)
6217 {
6218 if (term->tl_eof_chars != NULL)
6219 {
6220 channel_send(ch, PART_IN, term->tl_eof_chars,
6221 (int)STRLEN(term->tl_eof_chars), NULL);
6222 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6223 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006224# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006225 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006226 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006227 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6228# endif
6229 }
6230}
6231
Bram Moolenaar113e1072019-01-20 15:30:40 +01006232#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006233 job_T *
6234term_getjob(term_T *term)
6235{
6236 return term != NULL ? term->tl_job : NULL;
6237}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006238#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006239
Bram Moolenaar4f974752019-02-17 17:44:42 +01006240# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006241
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006242///////////////////////////////////////
6243// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006244#ifdef PROTO
6245typedef int COORD;
6246typedef int DWORD;
6247typedef int HANDLE;
6248typedef int *DWORD_PTR;
6249typedef int HPCON;
6250typedef int HRESULT;
6251typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006252typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006253typedef int PSIZE_T;
6254typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006255typedef int BOOL;
6256# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006257#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006258
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006259HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6260HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6261HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006262BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6263BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6264void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006265
6266 static int
6267dyn_conpty_init(int verbose)
6268{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006269 static HMODULE hKerneldll = NULL;
6270 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006271 static struct
6272 {
6273 char *name;
6274 FARPROC *ptr;
6275 } conpty_entry[] =
6276 {
6277 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6278 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6279 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6280 {"InitializeProcThreadAttributeList",
6281 (FARPROC*)&pInitializeProcThreadAttributeList},
6282 {"UpdateProcThreadAttribute",
6283 (FARPROC*)&pUpdateProcThreadAttribute},
6284 {"DeleteProcThreadAttributeList",
6285 (FARPROC*)&pDeleteProcThreadAttributeList},
6286 {NULL, NULL}
6287 };
6288
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006289 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006290 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006291 if (verbose)
6292 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006293 return FAIL;
6294 }
6295
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006296 // No need to initialize twice.
6297 if (hKerneldll)
6298 return OK;
6299
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006300 hKerneldll = vimLoadLib("kernel32.dll");
6301 for (i = 0; conpty_entry[i].name != NULL
6302 && conpty_entry[i].ptr != NULL; ++i)
6303 {
6304 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6305 conpty_entry[i].name)) == NULL)
6306 {
6307 if (verbose)
6308 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006309 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006310 return FAIL;
6311 }
6312 }
6313
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006314 return OK;
6315}
6316
6317 static int
6318conpty_term_and_job_init(
6319 term_T *term,
6320 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006321 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006322 jobopt_T *opt,
6323 jobopt_T *orig_opt)
6324{
6325 WCHAR *cmd_wchar = NULL;
6326 WCHAR *cmd_wchar_copy = NULL;
6327 WCHAR *cwd_wchar = NULL;
6328 WCHAR *env_wchar = NULL;
6329 channel_T *channel = NULL;
6330 job_T *job = NULL;
6331 HANDLE jo = NULL;
6332 garray_T ga_cmd, ga_env;
6333 char_u *cmd = NULL;
6334 HRESULT hr;
6335 COORD consize;
6336 SIZE_T breq;
6337 PROCESS_INFORMATION proc_info;
6338 HANDLE i_theirs = NULL;
6339 HANDLE o_theirs = NULL;
6340 HANDLE i_ours = NULL;
6341 HANDLE o_ours = NULL;
6342
6343 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6344 ga_init2(&ga_env, (int)sizeof(char*), 20);
6345
6346 if (argvar->v_type == VAR_STRING)
6347 {
6348 cmd = argvar->vval.v_string;
6349 }
6350 else if (argvar->v_type == VAR_LIST)
6351 {
6352 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6353 goto failed;
6354 cmd = ga_cmd.ga_data;
6355 }
6356 if (cmd == NULL || *cmd == NUL)
6357 {
6358 emsg(_(e_invarg));
6359 goto failed;
6360 }
6361
6362 term->tl_arg0_cmd = vim_strsave(cmd);
6363
6364 cmd_wchar = enc_to_utf16(cmd, NULL);
6365
6366 if (cmd_wchar != NULL)
6367 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006368 // Request by CreateProcessW
6369 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006370 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006371 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6372 }
6373
6374 ga_clear(&ga_cmd);
6375 if (cmd_wchar == NULL)
6376 goto failed;
6377 if (opt->jo_cwd != NULL)
6378 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6379
6380 win32_build_env(opt->jo_env, &ga_env, TRUE);
6381 env_wchar = ga_env.ga_data;
6382
6383 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6384 goto failed;
6385 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6386 goto failed;
6387
6388 consize.X = term->tl_cols;
6389 consize.Y = term->tl_rows;
6390 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6391 &term->tl_conpty);
6392 if (FAILED(hr))
6393 goto failed;
6394
6395 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6396
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006397 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006398 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006399 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006400 if (!term->tl_siex.lpAttributeList)
6401 goto failed;
6402 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6403 0, &breq))
6404 goto failed;
6405 if (!pUpdateProcThreadAttribute(
6406 term->tl_siex.lpAttributeList, 0,
6407 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6408 sizeof(HPCON), NULL, NULL))
6409 goto failed;
6410
6411 channel = add_channel();
6412 if (channel == NULL)
6413 goto failed;
6414
6415 job = job_alloc();
6416 if (job == NULL)
6417 goto failed;
6418 if (argvar->v_type == VAR_STRING)
6419 {
6420 int argc;
6421
6422 build_argv_from_string(cmd, &job->jv_argv, &argc);
6423 }
6424 else
6425 {
6426 int argc;
6427
6428 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6429 }
6430
6431 if (opt->jo_set & JO_IN_BUF)
6432 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6433
6434 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6435 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006436 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006437 env_wchar, cwd_wchar,
6438 &term->tl_siex.StartupInfo, &proc_info))
6439 goto failed;
6440
6441 CloseHandle(i_theirs);
6442 CloseHandle(o_theirs);
6443
6444 channel_set_pipes(channel,
6445 (sock_T)i_ours,
6446 (sock_T)o_ours,
6447 (sock_T)o_ours);
6448
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006449 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006450 channel->ch_write_text_mode = TRUE;
6451
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006452 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006453 channel->ch_anonymous_pipe = TRUE;
6454
6455 jo = CreateJobObject(NULL, NULL);
6456 if (jo == NULL)
6457 goto failed;
6458
6459 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6460 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006461 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006462 CloseHandle(jo);
6463 jo = NULL;
6464 }
6465
6466 ResumeThread(proc_info.hThread);
6467 CloseHandle(proc_info.hThread);
6468
6469 vim_free(cmd_wchar);
6470 vim_free(cmd_wchar_copy);
6471 vim_free(cwd_wchar);
6472 vim_free(env_wchar);
6473
6474 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6475 goto failed;
6476
6477#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6478 if (opt->jo_set2 & JO2_ANSI_COLORS)
6479 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6480 else
6481 init_vterm_ansi_colors(term->tl_vterm);
6482#endif
6483
6484 channel_set_job(channel, job, opt);
6485 job_set_options(job, opt);
6486
6487 job->jv_channel = channel;
6488 job->jv_proc_info = proc_info;
6489 job->jv_job_object = jo;
6490 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006491 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006492 ++job->jv_refcount;
6493 term->tl_job = job;
6494
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006495 // Redirecting stdout and stderr doesn't work at the job level. Instead
6496 // open the file here and handle it in. opt->jo_io was changed in
6497 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006498 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6499 {
6500 char_u *fname = opt->jo_io_name[PART_OUT];
6501
6502 ch_log(channel, "Opening output file %s", fname);
6503 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6504 if (term->tl_out_fd == NULL)
6505 semsg(_(e_notopen), fname);
6506 }
6507
6508 return OK;
6509
6510failed:
6511 ga_clear(&ga_cmd);
6512 ga_clear(&ga_env);
6513 vim_free(cmd_wchar);
6514 vim_free(cmd_wchar_copy);
6515 vim_free(cwd_wchar);
6516 if (channel != NULL)
6517 channel_clear(channel);
6518 if (job != NULL)
6519 {
6520 job->jv_channel = NULL;
6521 job_cleanup(job);
6522 }
6523 term->tl_job = NULL;
6524 if (jo != NULL)
6525 CloseHandle(jo);
6526
6527 if (term->tl_siex.lpAttributeList != NULL)
6528 {
6529 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6530 vim_free(term->tl_siex.lpAttributeList);
6531 }
6532 term->tl_siex.lpAttributeList = NULL;
6533 if (o_theirs != NULL)
6534 CloseHandle(o_theirs);
6535 if (o_ours != NULL)
6536 CloseHandle(o_ours);
6537 if (i_ours != NULL)
6538 CloseHandle(i_ours);
6539 if (i_theirs != NULL)
6540 CloseHandle(i_theirs);
6541 if (term->tl_conpty != NULL)
6542 pClosePseudoConsole(term->tl_conpty);
6543 term->tl_conpty = NULL;
6544 return FAIL;
6545}
6546
6547 static void
6548conpty_term_report_winsize(term_T *term, int rows, int cols)
6549{
6550 COORD consize;
6551
6552 consize.X = cols;
6553 consize.Y = rows;
6554 pResizePseudoConsole(term->tl_conpty, consize);
6555}
6556
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006557 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006558term_free_conpty(term_T *term)
6559{
6560 if (term->tl_siex.lpAttributeList != NULL)
6561 {
6562 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6563 vim_free(term->tl_siex.lpAttributeList);
6564 }
6565 term->tl_siex.lpAttributeList = NULL;
6566 if (term->tl_conpty != NULL)
6567 pClosePseudoConsole(term->tl_conpty);
6568 term->tl_conpty = NULL;
6569}
6570
6571 int
6572use_conpty(void)
6573{
6574 return has_conpty;
6575}
6576
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006577# ifndef PROTO
6578
6579#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6580#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006581#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006582
6583void* (*winpty_config_new)(UINT64, void*);
6584void* (*winpty_open)(void*, void*);
6585void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6586BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6587void (*winpty_config_set_mouse_mode)(void*, int);
6588void (*winpty_config_set_initial_size)(void*, int, int);
6589LPCWSTR (*winpty_conin_name)(void*);
6590LPCWSTR (*winpty_conout_name)(void*);
6591LPCWSTR (*winpty_conerr_name)(void*);
6592void (*winpty_free)(void*);
6593void (*winpty_config_free)(void*);
6594void (*winpty_spawn_config_free)(void*);
6595void (*winpty_error_free)(void*);
6596LPCWSTR (*winpty_error_msg)(void*);
6597BOOL (*winpty_set_size)(void*, int, int, void*);
6598HANDLE (*winpty_agent_process)(void*);
6599
6600#define WINPTY_DLL "winpty.dll"
6601
6602static HINSTANCE hWinPtyDLL = NULL;
6603# endif
6604
6605 static int
6606dyn_winpty_init(int verbose)
6607{
6608 int i;
6609 static struct
6610 {
6611 char *name;
6612 FARPROC *ptr;
6613 } winpty_entry[] =
6614 {
6615 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6616 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6617 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6618 {"winpty_config_set_mouse_mode",
6619 (FARPROC*)&winpty_config_set_mouse_mode},
6620 {"winpty_config_set_initial_size",
6621 (FARPROC*)&winpty_config_set_initial_size},
6622 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6623 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6624 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6625 {"winpty_free", (FARPROC*)&winpty_free},
6626 {"winpty_open", (FARPROC*)&winpty_open},
6627 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6628 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6629 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6630 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6631 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6632 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6633 {NULL, NULL}
6634 };
6635
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006636 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006637 if (hWinPtyDLL)
6638 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006639 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6640 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006641 if (*p_winptydll != NUL)
6642 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6643 if (!hWinPtyDLL)
6644 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6645 if (!hWinPtyDLL)
6646 {
6647 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006648 semsg(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006649 : (char_u *)WINPTY_DLL);
6650 return FAIL;
6651 }
6652 for (i = 0; winpty_entry[i].name != NULL
6653 && winpty_entry[i].ptr != NULL; ++i)
6654 {
6655 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6656 winpty_entry[i].name)) == NULL)
6657 {
6658 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006659 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006660 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006661 return FAIL;
6662 }
6663 }
6664
6665 return OK;
6666}
6667
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006668 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006669winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006670 term_T *term,
6671 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006672 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006673 jobopt_T *opt,
6674 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006675{
6676 WCHAR *cmd_wchar = NULL;
6677 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006678 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006679 channel_T *channel = NULL;
6680 job_T *job = NULL;
6681 DWORD error;
6682 HANDLE jo = NULL;
6683 HANDLE child_process_handle;
6684 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006685 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006686 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006687 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006688 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006689
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006690 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6691 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006692
6693 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006694 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006695 cmd = argvar->vval.v_string;
6696 }
6697 else if (argvar->v_type == VAR_LIST)
6698 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006699 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006700 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006701 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006702 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006703 if (cmd == NULL || *cmd == NUL)
6704 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006705 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006706 goto failed;
6707 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006708
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006709 term->tl_arg0_cmd = vim_strsave(cmd);
6710
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006711 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006712 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006713 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006714 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006715 if (opt->jo_cwd != NULL)
6716 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006717
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006718 win32_build_env(opt->jo_env, &ga_env, TRUE);
6719 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006720
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006721 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6722 if (term->tl_winpty_config == NULL)
6723 goto failed;
6724
6725 winpty_config_set_mouse_mode(term->tl_winpty_config,
6726 WINPTY_MOUSE_MODE_FORCE);
6727 winpty_config_set_initial_size(term->tl_winpty_config,
6728 term->tl_cols, term->tl_rows);
6729 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6730 if (term->tl_winpty == NULL)
6731 goto failed;
6732
6733 spawn_config = winpty_spawn_config_new(
6734 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6735 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6736 NULL,
6737 cmd_wchar,
6738 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006739 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006740 &winpty_err);
6741 if (spawn_config == NULL)
6742 goto failed;
6743
6744 channel = add_channel();
6745 if (channel == NULL)
6746 goto failed;
6747
6748 job = job_alloc();
6749 if (job == NULL)
6750 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006751 if (argvar->v_type == VAR_STRING)
6752 {
6753 int argc;
6754
6755 build_argv_from_string(cmd, &job->jv_argv, &argc);
6756 }
6757 else
6758 {
6759 int argc;
6760
6761 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6762 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006763
6764 if (opt->jo_set & JO_IN_BUF)
6765 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6766
6767 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
6768 &child_thread_handle, &error, &winpty_err))
6769 goto failed;
6770
6771 channel_set_pipes(channel,
6772 (sock_T)CreateFileW(
6773 winpty_conin_name(term->tl_winpty),
6774 GENERIC_WRITE, 0, NULL,
6775 OPEN_EXISTING, 0, NULL),
6776 (sock_T)CreateFileW(
6777 winpty_conout_name(term->tl_winpty),
6778 GENERIC_READ, 0, NULL,
6779 OPEN_EXISTING, 0, NULL),
6780 (sock_T)CreateFileW(
6781 winpty_conerr_name(term->tl_winpty),
6782 GENERIC_READ, 0, NULL,
6783 OPEN_EXISTING, 0, NULL));
6784
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006785 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006786 channel->ch_write_text_mode = TRUE;
6787
6788 jo = CreateJobObject(NULL, NULL);
6789 if (jo == NULL)
6790 goto failed;
6791
6792 if (!AssignProcessToJobObject(jo, child_process_handle))
6793 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006794 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006795 CloseHandle(jo);
6796 jo = NULL;
6797 }
6798
6799 winpty_spawn_config_free(spawn_config);
6800 vim_free(cmd_wchar);
6801 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006802 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006803
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006804 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6805 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006806
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006807#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6808 if (opt->jo_set2 & JO2_ANSI_COLORS)
6809 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6810 else
6811 init_vterm_ansi_colors(term->tl_vterm);
6812#endif
6813
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006814 channel_set_job(channel, job, opt);
6815 job_set_options(job, opt);
6816
6817 job->jv_channel = channel;
6818 job->jv_proc_info.hProcess = child_process_handle;
6819 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
6820 job->jv_job_object = jo;
6821 job->jv_status = JOB_STARTED;
6822 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006823 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006824 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006825 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006826 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006827 ++job->jv_refcount;
6828 term->tl_job = job;
6829
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006830 // Redirecting stdout and stderr doesn't work at the job level. Instead
6831 // open the file here and handle it in. opt->jo_io was changed in
6832 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006833 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6834 {
6835 char_u *fname = opt->jo_io_name[PART_OUT];
6836
6837 ch_log(channel, "Opening output file %s", fname);
6838 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6839 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006840 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006841 }
6842
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006843 return OK;
6844
6845failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006846 ga_clear(&ga_cmd);
6847 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006848 vim_free(cmd_wchar);
6849 vim_free(cwd_wchar);
6850 if (spawn_config != NULL)
6851 winpty_spawn_config_free(spawn_config);
6852 if (channel != NULL)
6853 channel_clear(channel);
6854 if (job != NULL)
6855 {
6856 job->jv_channel = NULL;
6857 job_cleanup(job);
6858 }
6859 term->tl_job = NULL;
6860 if (jo != NULL)
6861 CloseHandle(jo);
6862 if (term->tl_winpty != NULL)
6863 winpty_free(term->tl_winpty);
6864 term->tl_winpty = NULL;
6865 if (term->tl_winpty_config != NULL)
6866 winpty_config_free(term->tl_winpty_config);
6867 term->tl_winpty_config = NULL;
6868 if (winpty_err != NULL)
6869 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006870 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006871 (short_u *)winpty_error_msg(winpty_err), NULL);
6872
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006873 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006874 winpty_error_free(winpty_err);
6875 }
6876 return FAIL;
6877}
6878
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006879/*
6880 * Create a new terminal of "rows" by "cols" cells.
6881 * Store a reference in "term".
6882 * Return OK or FAIL.
6883 */
6884 static int
6885term_and_job_init(
6886 term_T *term,
6887 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01006888 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006889 jobopt_T *opt,
6890 jobopt_T *orig_opt)
6891{
6892 int use_winpty = FALSE;
6893 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006894 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006895
6896 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
6897 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
6898
6899 if (!has_winpty && !has_conpty)
6900 // If neither is available give the errors for winpty, since when
6901 // conpty is not available it can't be installed either.
6902 return dyn_winpty_init(TRUE);
6903
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006904 if (opt->jo_tty_type != NUL)
6905 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006906
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006907 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006908 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006909 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006910 use_conpty = TRUE;
6911 else if (has_winpty)
6912 use_winpty = TRUE;
6913 // else: error
6914 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006915 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006916 {
6917 if (has_winpty)
6918 use_winpty = TRUE;
6919 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006920 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006921 {
6922 if (has_conpty)
6923 use_conpty = TRUE;
6924 else
6925 return dyn_conpty_init(TRUE);
6926 }
6927
6928 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006929 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006930
6931 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006932 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006933
6934 // error
6935 return dyn_winpty_init(TRUE);
6936}
6937
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006938 static int
6939create_pty_only(term_T *term, jobopt_T *options)
6940{
6941 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
6942 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
6943 char in_name[80], out_name[80];
6944 channel_T *channel = NULL;
6945
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006946 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6947 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006948
6949 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
6950 GetCurrentProcessId(),
6951 curbuf->b_fnum);
6952 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
6953 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6954 PIPE_UNLIMITED_INSTANCES,
6955 0, 0, NMPWAIT_NOWAIT, NULL);
6956 if (hPipeIn == INVALID_HANDLE_VALUE)
6957 goto failed;
6958
6959 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
6960 GetCurrentProcessId(),
6961 curbuf->b_fnum);
6962 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
6963 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6964 PIPE_UNLIMITED_INSTANCES,
6965 0, 0, 0, NULL);
6966 if (hPipeOut == INVALID_HANDLE_VALUE)
6967 goto failed;
6968
6969 ConnectNamedPipe(hPipeIn, NULL);
6970 ConnectNamedPipe(hPipeOut, NULL);
6971
6972 term->tl_job = job_alloc();
6973 if (term->tl_job == NULL)
6974 goto failed;
6975 ++term->tl_job->jv_refcount;
6976
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006977 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006978 term->tl_job->jv_status = JOB_FINISHED;
6979
6980 channel = add_channel();
6981 if (channel == NULL)
6982 goto failed;
6983 term->tl_job->jv_channel = channel;
6984 channel->ch_keep_open = TRUE;
6985 channel->ch_named_pipe = TRUE;
6986
6987 channel_set_pipes(channel,
6988 (sock_T)hPipeIn,
6989 (sock_T)hPipeOut,
6990 (sock_T)hPipeOut);
6991 channel_set_job(channel, term->tl_job, options);
6992 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
6993 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
6994
6995 return OK;
6996
6997failed:
6998 if (hPipeIn != NULL)
6999 CloseHandle(hPipeIn);
7000 if (hPipeOut != NULL)
7001 CloseHandle(hPipeOut);
7002 return FAIL;
7003}
7004
7005/*
7006 * Free the terminal emulator part of "term".
7007 */
7008 static void
7009term_free_vterm(term_T *term)
7010{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007011 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007012 if (term->tl_winpty != NULL)
7013 winpty_free(term->tl_winpty);
7014 term->tl_winpty = NULL;
7015 if (term->tl_winpty_config != NULL)
7016 winpty_config_free(term->tl_winpty_config);
7017 term->tl_winpty_config = NULL;
7018 if (term->tl_vterm != NULL)
7019 vterm_free(term->tl_vterm);
7020 term->tl_vterm = NULL;
7021}
7022
7023/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007024 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007025 */
7026 static void
7027term_report_winsize(term_T *term, int rows, int cols)
7028{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007029 if (term->tl_conpty)
7030 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007031 if (term->tl_winpty)
7032 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7033}
7034
7035 int
7036terminal_enabled(void)
7037{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007038 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007039}
7040
7041# else
7042
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007043///////////////////////////////////////
7044// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007045
7046/*
7047 * Create a new terminal of "rows" by "cols" cells.
7048 * Start job for "cmd".
7049 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007050 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007051 * Return OK or FAIL.
7052 */
7053 static int
7054term_and_job_init(
7055 term_T *term,
7056 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007057 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007058 jobopt_T *opt,
7059 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007060{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007061 term->tl_arg0_cmd = NULL;
7062
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007063 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7064 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007065
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007066#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7067 if (opt->jo_set2 & JO2_ANSI_COLORS)
7068 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7069 else
7070 init_vterm_ansi_colors(term->tl_vterm);
7071#endif
7072
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007073 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007074 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007075 if (term->tl_job != NULL)
7076 ++term->tl_job->jv_refcount;
7077
7078 return term->tl_job != NULL
7079 && term->tl_job->jv_channel != NULL
7080 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7081}
7082
7083 static int
7084create_pty_only(term_T *term, jobopt_T *opt)
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
7089 term->tl_job = job_alloc();
7090 if (term->tl_job == NULL)
7091 return FAIL;
7092 ++term->tl_job->jv_refcount;
7093
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007094 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007095 term->tl_job->jv_status = JOB_FINISHED;
7096
7097 return mch_create_pty_channel(term->tl_job, opt);
7098}
7099
7100/*
7101 * Free the terminal emulator part of "term".
7102 */
7103 static void
7104term_free_vterm(term_T *term)
7105{
7106 if (term->tl_vterm != NULL)
7107 vterm_free(term->tl_vterm);
7108 term->tl_vterm = NULL;
7109}
7110
7111/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007112 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007113 */
7114 static void
7115term_report_winsize(term_T *term, int rows, int cols)
7116{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007117 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007118 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7119 {
7120 int fd = -1;
7121 int part;
7122
7123 for (part = PART_OUT; part < PART_COUNT; ++part)
7124 {
7125 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007126 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007127 break;
7128 }
7129 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7130 mch_signal_job(term->tl_job, (char_u *)"winch");
7131 }
7132}
7133
7134# endif
7135
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007136#endif // FEAT_TERMINAL