blob: cab9cb9c665e0d3b6bf778ae07b67326756a52ed [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
938term_write_session(FILE *fd, win_T *wp)
939{
940 term_T *term = wp->w_buffer->b_term;
941
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100942 // Create the terminal and run the command. This is not without
943 // risk, but let's assume the user only creates a session when this
944 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100945 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
946 term->tl_cols, term->tl_rows) < 0)
947 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100948#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100949 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
950 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100951#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100952 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
953 return FAIL;
954
955 return put_eol(fd);
956}
957
958/*
959 * Return TRUE if "buf" has a terminal that should be restored.
960 */
961 int
962term_should_restore(buf_T *buf)
963{
964 term_T *term = buf->b_term;
965
966 return term != NULL && (term->tl_command == NULL
967 || STRCMP(term->tl_command, "NONE") != 0);
968}
969#endif
970
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200971/*
972 * Free the scrollback buffer for "term".
973 */
974 static void
975free_scrollback(term_T *term)
976{
977 int i;
978
979 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
980 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
981 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100982 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
983 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
984 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200985}
986
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100987
988// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +0200989static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100990
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200991/*
992 * Free a terminal and everything it refers to.
993 * Kills the job if there is one.
994 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100995 * The actual terminal structure is freed later in free_unused_terminals(),
996 * because callbacks may wipe out a buffer while the terminal is still
997 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200998 */
999 void
1000free_terminal(buf_T *buf)
1001{
1002 term_T *term = buf->b_term;
1003 term_T *tp;
1004
1005 if (term == NULL)
1006 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001007
1008 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001009 if (first_term == term)
1010 first_term = term->tl_next;
1011 else
1012 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
1013 if (tp->tl_next == term)
1014 {
1015 tp->tl_next = term->tl_next;
1016 break;
1017 }
1018
1019 if (term->tl_job != NULL)
1020 {
1021 if (term->tl_job->jv_status != JOB_ENDED
1022 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +01001023 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001024 job_stop(term->tl_job, NULL, "kill");
1025 job_unref(term->tl_job);
1026 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001027 term->tl_next = terminals_to_free;
1028 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001029
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001030 buf->b_term = NULL;
1031 if (in_terminal_loop == term)
1032 in_terminal_loop = NULL;
1033}
1034
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001035 void
1036free_unused_terminals()
1037{
1038 while (terminals_to_free != NULL)
1039 {
1040 term_T *term = terminals_to_free;
1041
1042 terminals_to_free = term->tl_next;
1043
1044 free_scrollback(term);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02001045 ga_clear(&term->tl_osc_buf);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001046
1047 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001048 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001049 vim_free(term->tl_title);
1050#ifdef FEAT_SESSION
1051 vim_free(term->tl_command);
1052#endif
1053 vim_free(term->tl_kill);
1054 vim_free(term->tl_status_text);
1055 vim_free(term->tl_opencmd);
1056 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001057 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001058#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001059 if (term->tl_out_fd != NULL)
1060 fclose(term->tl_out_fd);
1061#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001062 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001063 vim_free(term->tl_cursor_color);
1064 vim_free(term);
1065 }
1066}
1067
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001068/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001069 * Get the part that is connected to the tty. Normally this is PART_IN, but
1070 * when writing buffer lines to the job it can be another. This makes it
1071 * possible to do "1,5term vim -".
1072 */
1073 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001074get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001075{
1076#ifdef UNIX
1077 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1078 int i;
1079
1080 for (i = 0; i < 3; ++i)
1081 {
1082 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1083
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001084 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001085 return parts[i];
1086 }
1087#endif
1088 return PART_IN;
1089}
1090
1091/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001092 * Write job output "msg[len]" to the vterm.
1093 */
1094 static void
1095term_write_job_output(term_T *term, char_u *msg, size_t len)
1096{
1097 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001098 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001099
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001100 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001101
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001102 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001103 if (prevlen != vterm_output_get_buffer_current(vterm))
1104 {
1105 char buf[KEY_BUF_LEN];
1106 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1107
1108 if (curlen > 0)
1109 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1110 (char_u *)buf, (int)curlen, NULL);
1111 }
1112
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001113 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001114 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1115}
1116
1117 static void
1118update_cursor(term_T *term, int redraw)
1119{
1120 if (term->tl_normal_mode)
1121 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001122#ifdef FEAT_GUI
1123 if (term->tl_system)
1124 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1125 term->tl_cursor_pos.col);
1126 else
1127#endif
1128 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001129 if (redraw)
1130 {
1131 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1132 cursor_on();
1133 out_flush();
1134#ifdef FEAT_GUI
1135 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001136 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001137 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001138 gui_mch_flush();
1139 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001140#endif
1141 }
1142}
1143
1144/*
1145 * Invoked when "msg" output from a job was received. Write it to the terminal
1146 * of "buffer".
1147 */
1148 void
1149write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1150{
1151 size_t len = STRLEN(msg);
1152 term_T *term = buffer->b_term;
1153
Bram Moolenaar4f974752019-02-17 17:44:42 +01001154#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001155 // Win32: Cannot redirect output of the job, intercept it here and write to
1156 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001157 if (term->tl_out_fd != NULL)
1158 {
1159 ch_log(channel, "Writing %d bytes to output file", (int)len);
1160 fwrite(msg, len, 1, term->tl_out_fd);
1161 return;
1162 }
1163#endif
1164
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001165 if (term->tl_vterm == NULL)
1166 {
1167 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1168 return;
1169 }
1170 ch_log(channel, "writing %d bytes to terminal", (int)len);
1171 term_write_job_output(term, msg, len);
1172
Bram Moolenaar13568252018-03-16 20:46:58 +01001173#ifdef FEAT_GUI
1174 if (term->tl_system)
1175 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001176 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001177 update_system_term(term);
1178 update_cursor(term, TRUE);
1179 }
1180 else
1181#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001182 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1183 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001184 if (!term->tl_normal_mode)
1185 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001186 // Don't use update_screen() when editing the command line, it gets
1187 // cleared.
1188 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001189 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001190 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001191 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001192 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001193 // update_screen() can be slow, check the terminal wasn't closed
1194 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001195 if (buffer == curbuf && curbuf->b_term != NULL)
1196 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001197 }
1198 else
1199 redraw_after_callback(TRUE);
1200 }
1201}
1202
1203/*
1204 * Send a mouse position and click to the vterm
1205 */
1206 static int
1207term_send_mouse(VTerm *vterm, int button, int pressed)
1208{
1209 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001210 int row = mouse_row - W_WINROW(curwin);
1211 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001212
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001213#ifdef FEAT_PROP_POPUP
1214 if (popup_is_popup(curwin))
1215 {
1216 row -= popup_top_extra(curwin);
1217 col -= popup_left_extra(curwin);
1218 }
1219#endif
1220 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001221 if (button != 0)
1222 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001223 return TRUE;
1224}
1225
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001226static int enter_mouse_col = -1;
1227static int enter_mouse_row = -1;
1228
1229/*
1230 * Handle a mouse click, drag or release.
1231 * Return TRUE when a mouse event is sent to the terminal.
1232 */
1233 static int
1234term_mouse_click(VTerm *vterm, int key)
1235{
1236#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001237 // For modeless selection mouse drag and release events are ignored, unless
1238 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001239 static int ignore_drag_release = TRUE;
1240 VTermMouseState mouse_state;
1241
1242 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1243 if (mouse_state.flags == 0)
1244 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001245 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001246 switch (key)
1247 {
1248 case K_LEFTDRAG:
1249 case K_LEFTRELEASE:
1250 case K_RIGHTDRAG:
1251 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001252 // Ignore drag and release events when the button-down wasn't
1253 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001254 if (ignore_drag_release)
1255 {
1256 int save_mouse_col, save_mouse_row;
1257
1258 if (enter_mouse_col < 0)
1259 break;
1260
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001261 // mouse click in the window gave us focus, handle that
1262 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001263 save_mouse_col = mouse_col;
1264 save_mouse_row = mouse_row;
1265 mouse_col = enter_mouse_col;
1266 mouse_row = enter_mouse_row;
1267 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1268 mouse_col = save_mouse_col;
1269 mouse_row = save_mouse_row;
1270 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001271 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001272 case K_LEFTMOUSE:
1273 case K_RIGHTMOUSE:
1274 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1275 ignore_drag_release = TRUE;
1276 else
1277 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001278 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001279 if (clip_star.available)
1280 {
1281 int button, is_click, is_drag;
1282
1283 button = get_mouse_button(KEY2TERMCAP1(key),
1284 &is_click, &is_drag);
1285 if (mouse_model_popup() && button == MOUSE_LEFT
1286 && (mod_mask & MOD_MASK_SHIFT))
1287 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001288 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001289 button = MOUSE_RIGHT;
1290 mod_mask &= ~MOD_MASK_SHIFT;
1291 }
1292 clip_modeless(button, is_click, is_drag);
1293 }
1294 break;
1295
1296 case K_MIDDLEMOUSE:
1297 if (clip_star.available)
1298 insert_reg('*', TRUE);
1299 break;
1300 }
1301 enter_mouse_col = -1;
1302 return FALSE;
1303 }
1304#endif
1305 enter_mouse_col = -1;
1306
1307 switch (key)
1308 {
1309 case K_LEFTMOUSE:
1310 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1311 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1312 case K_LEFTRELEASE:
1313 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1314 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1315 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1316 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1317 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1318 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1319 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1320 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1321 }
1322 return TRUE;
1323}
1324
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001325/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001326 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1327 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001328 * Return the number of bytes in "buf".
1329 */
1330 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001331term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001332{
1333 VTerm *vterm = term->tl_vterm;
1334 VTermKey key = VTERM_KEY_NONE;
1335 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001336 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001337
1338 switch (c)
1339 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001340 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001341
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001342 // don't use VTERM_KEY_BACKSPACE, it always
1343 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001344 case K_BS: c = term_backspace_char; break;
1345
1346 case ESC: key = VTERM_KEY_ESCAPE; break;
1347 case K_DEL: key = VTERM_KEY_DEL; break;
1348 case K_DOWN: key = VTERM_KEY_DOWN; break;
1349 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1350 key = VTERM_KEY_DOWN; break;
1351 case K_END: key = VTERM_KEY_END; break;
1352 case K_S_END: mod = VTERM_MOD_SHIFT;
1353 key = VTERM_KEY_END; break;
1354 case K_C_END: mod = VTERM_MOD_CTRL;
1355 key = VTERM_KEY_END; break;
1356 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1357 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1358 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1359 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1360 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1361 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1362 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1363 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1364 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1365 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1366 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1367 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1368 case K_HOME: key = VTERM_KEY_HOME; break;
1369 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1370 key = VTERM_KEY_HOME; break;
1371 case K_C_HOME: mod = VTERM_MOD_CTRL;
1372 key = VTERM_KEY_HOME; break;
1373 case K_INS: key = VTERM_KEY_INS; break;
1374 case K_K0: key = VTERM_KEY_KP_0; break;
1375 case K_K1: key = VTERM_KEY_KP_1; break;
1376 case K_K2: key = VTERM_KEY_KP_2; break;
1377 case K_K3: key = VTERM_KEY_KP_3; break;
1378 case K_K4: key = VTERM_KEY_KP_4; break;
1379 case K_K5: key = VTERM_KEY_KP_5; break;
1380 case K_K6: key = VTERM_KEY_KP_6; break;
1381 case K_K7: key = VTERM_KEY_KP_7; break;
1382 case K_K8: key = VTERM_KEY_KP_8; break;
1383 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001384 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001385 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001386 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001387 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001388 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1389 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001390 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1391 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001392 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1393 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001394 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1395 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1396 case K_LEFT: key = VTERM_KEY_LEFT; break;
1397 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1398 key = VTERM_KEY_LEFT; break;
1399 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1400 key = VTERM_KEY_LEFT; break;
1401 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1402 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1403 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1404 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1405 key = VTERM_KEY_RIGHT; break;
1406 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1407 key = VTERM_KEY_RIGHT; break;
1408 case K_UP: key = VTERM_KEY_UP; break;
1409 case K_S_UP: mod = VTERM_MOD_SHIFT;
1410 key = VTERM_KEY_UP; break;
1411 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001412 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1413 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001414
Bram Moolenaara42ad572017-11-16 13:08:04 +01001415 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1416 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001417 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1418 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001419
1420 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001421 case K_LEFTMOUSE_NM:
1422 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001423 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001424 case K_LEFTRELEASE_NM:
1425 case K_MOUSEMOVE:
1426 case K_MIDDLEMOUSE:
1427 case K_MIDDLEDRAG:
1428 case K_MIDDLERELEASE:
1429 case K_RIGHTMOUSE:
1430 case K_RIGHTDRAG:
1431 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1432 return 0;
1433 other = TRUE;
1434 break;
1435
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001436 case K_X1MOUSE: /* TODO */ return 0;
1437 case K_X1DRAG: /* TODO */ return 0;
1438 case K_X1RELEASE: /* TODO */ return 0;
1439 case K_X2MOUSE: /* TODO */ return 0;
1440 case K_X2DRAG: /* TODO */ return 0;
1441 case K_X2RELEASE: /* TODO */ return 0;
1442
1443 case K_IGNORE: return 0;
1444 case K_NOP: return 0;
1445 case K_UNDO: return 0;
1446 case K_HELP: return 0;
1447 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1448 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1449 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1450 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1451 case K_SELECT: return 0;
1452#ifdef FEAT_GUI
1453 case K_VER_SCROLLBAR: return 0;
1454 case K_HOR_SCROLLBAR: return 0;
1455#endif
1456#ifdef FEAT_GUI_TABLINE
1457 case K_TABLINE: return 0;
1458 case K_TABMENU: return 0;
1459#endif
1460#ifdef FEAT_NETBEANS_INTG
1461 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1462#endif
1463#ifdef FEAT_DND
1464 case K_DROP: return 0;
1465#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001466 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001467 case K_PS: vterm_keyboard_start_paste(vterm);
1468 other = TRUE;
1469 break;
1470 case K_PE: vterm_keyboard_end_paste(vterm);
1471 other = TRUE;
1472 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001473 }
1474
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001475 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001476 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001477 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001478 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001479 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001480 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001481 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001482
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001483 /*
1484 * Convert special keys to vterm keys:
1485 * - Write keys to vterm: vterm_keyboard_key()
1486 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001487 */
1488 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001489 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001490 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001491 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001492 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001493 vterm_keyboard_unichar(vterm, c, mod);
1494
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001495 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001496 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1497}
1498
1499/*
1500 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001501 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001502 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001503 */
1504 static int
1505term_job_running_check(term_T *term, int check_job_status)
1506{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001507 // Also consider the job finished when the channel is closed, to avoid a
1508 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001509 if (term != NULL
1510 && term->tl_job != NULL
1511 && channel_is_open(term->tl_job->jv_channel))
1512 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001513 job_T *job = term->tl_job;
1514
1515 // Careful: Checking the job status may invoked callbacks, which close
1516 // the buffer and terminate "term". However, "job" will not be freed
1517 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001518 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001519 job_status(job);
1520 return (job->jv_status == JOB_STARTED
1521 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001522 }
1523 return FALSE;
1524}
1525
1526/*
1527 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001528 */
1529 int
1530term_job_running(term_T *term)
1531{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001532 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001533}
1534
1535/*
1536 * Return TRUE if "term" has an active channel and used ":term NONE".
1537 */
1538 int
1539term_none_open(term_T *term)
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 Moolenaar2e6ab182017-09-20 10:03:07 +02001543 return term != NULL
1544 && term->tl_job != NULL
1545 && channel_is_open(term->tl_job->jv_channel)
1546 && term->tl_job->jv_channel->ch_keep_open;
1547}
1548
1549/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001550 * Used when exiting: kill the job in "buf" if so desired.
1551 * Return OK when the job finished.
1552 * Return FAIL when the job is still running.
1553 */
1554 int
1555term_try_stop_job(buf_T *buf)
1556{
1557 int count;
1558 char *how = (char *)buf->b_term->tl_kill;
1559
1560#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1561 if ((how == NULL || *how == NUL) && (p_confirm || cmdmod.confirm))
1562 {
1563 char_u buff[DIALOG_MSG_SIZE];
1564 int ret;
1565
1566 dialog_msg(buff, _("Kill job in \"%s\"?"), buf->b_fname);
1567 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1568 if (ret == VIM_YES)
1569 how = "kill";
1570 else if (ret == VIM_CANCEL)
1571 return FAIL;
1572 }
1573#endif
1574 if (how == NULL || *how == NUL)
1575 return FAIL;
1576
1577 job_stop(buf->b_term->tl_job, NULL, how);
1578
Bram Moolenaar9172d232019-01-29 23:06:54 +01001579 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001580 for (count = 0; count < 100; ++count)
1581 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001582 job_T *job;
1583
1584 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001585 if (!buf_valid(buf)
1586 || buf->b_term == NULL
1587 || buf->b_term->tl_job == NULL)
1588 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001589 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001590
Bram Moolenaar9172d232019-01-29 23:06:54 +01001591 // Call job_status() to update jv_status. It may cause the job to be
1592 // cleaned up but it won't be freed.
1593 job_status(job);
1594 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001595 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001596
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001597 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001598 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001599 }
1600 return FAIL;
1601}
1602
1603/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001604 * Add the last line of the scrollback buffer to the buffer in the window.
1605 */
1606 static void
1607add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1608{
1609 buf_T *buf = term->tl_buffer;
1610 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1611 linenr_T lnum = buf->b_ml.ml_line_count;
1612
Bram Moolenaar4f974752019-02-17 17:44:42 +01001613#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001614 if (!enc_utf8 && enc_codepage > 0)
1615 {
1616 WCHAR *ret = NULL;
1617 int length = 0;
1618
1619 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1620 &ret, &length);
1621 if (ret != NULL)
1622 {
1623 WideCharToMultiByte_alloc(enc_codepage, 0,
1624 ret, length, (char **)&text, &len, 0, 0);
1625 vim_free(ret);
1626 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1627 vim_free(text);
1628 }
1629 }
1630 else
1631#endif
1632 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1633 if (empty)
1634 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001635 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001636 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001637 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001638 curbuf = curwin->w_buffer;
1639 }
1640}
1641
1642 static void
1643cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1644{
1645 attr->width = cell->width;
1646 attr->attrs = cell->attrs;
1647 attr->fg = cell->fg;
1648 attr->bg = cell->bg;
1649}
1650
1651 static int
1652equal_celattr(cellattr_T *a, cellattr_T *b)
1653{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001654 // We only compare the RGB colors, ignoring the ANSI index and type.
1655 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001656 return a->fg.red == b->fg.red
1657 && a->fg.green == b->fg.green
1658 && a->fg.blue == b->fg.blue
1659 && a->bg.red == b->bg.red
1660 && a->bg.green == b->bg.green
1661 && a->bg.blue == b->bg.blue;
1662}
1663
Bram Moolenaard96ff162018-02-18 22:13:29 +01001664/*
1665 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1666 * line at this position. Otherwise at the end.
1667 */
1668 static int
1669add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1670{
1671 if (ga_grow(&term->tl_scrollback, 1) == OK)
1672 {
1673 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1674 + term->tl_scrollback.ga_len;
1675
1676 if (lnum > 0)
1677 {
1678 int i;
1679
1680 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1681 {
1682 *line = *(line - 1);
1683 --line;
1684 }
1685 }
1686 line->sb_cols = 0;
1687 line->sb_cells = NULL;
1688 line->sb_fill_attr = *fill_attr;
1689 ++term->tl_scrollback.ga_len;
1690 return OK;
1691 }
1692 return FALSE;
1693}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001694
1695/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001696 * Remove the terminal contents from the scrollback and the buffer.
1697 * Used before adding a new scrollback line or updating the buffer for lines
1698 * displayed in the terminal.
1699 */
1700 static void
1701cleanup_scrollback(term_T *term)
1702{
1703 sb_line_T *line;
1704 garray_T *gap;
1705
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001706 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001707 gap = &term->tl_scrollback;
1708 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1709 && gap->ga_len > 0)
1710 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001711 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001712 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1713 vim_free(line->sb_cells);
1714 --gap->ga_len;
1715 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001716 curbuf = curwin->w_buffer;
1717 if (curbuf == term->tl_buffer)
1718 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001719}
1720
1721/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001722 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001723 */
1724 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001725update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001726{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001727 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001728 int len;
1729 int lines_skipped = 0;
1730 VTermPos pos;
1731 VTermScreenCell cell;
1732 cellattr_T fill_attr, new_fill_attr;
1733 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001734
1735 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1736 "Adding terminal window snapshot to buffer");
1737
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001738 // First remove the lines that were appended before, they might be
1739 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001740 cleanup_scrollback(term);
1741
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001742 screen = vterm_obtain_screen(term->tl_vterm);
1743 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001744 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1745 {
1746 len = 0;
1747 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1748 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1749 && cell.chars[0] != NUL)
1750 {
1751 len = pos.col + 1;
1752 new_fill_attr = term->tl_default_color;
1753 }
1754 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001755 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001756 cell2cellattr(&cell, &new_fill_attr);
1757
1758 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1759 ++lines_skipped;
1760 else
1761 {
1762 while (lines_skipped > 0)
1763 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001764 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001765 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001766 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001767 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001768 }
1769
1770 if (len == 0)
1771 p = NULL;
1772 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001773 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001774 if ((p != NULL || len == 0)
1775 && ga_grow(&term->tl_scrollback, 1) == OK)
1776 {
1777 garray_T ga;
1778 int width;
1779 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1780 + term->tl_scrollback.ga_len;
1781
1782 ga_init2(&ga, 1, 100);
1783 for (pos.col = 0; pos.col < len; pos.col += width)
1784 {
1785 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1786 {
1787 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001788 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001789 if (ga_grow(&ga, 1) == OK)
1790 ga.ga_len += utf_char2bytes(' ',
1791 (char_u *)ga.ga_data + ga.ga_len);
1792 }
1793 else
1794 {
1795 width = cell.width;
1796
1797 cell2cellattr(&cell, &p[pos.col]);
1798
Bram Moolenaara79fd562018-12-20 20:47:32 +01001799 // Each character can be up to 6 bytes.
1800 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001801 {
1802 int i;
1803 int c;
1804
1805 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1806 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1807 (char_u *)ga.ga_data + ga.ga_len);
1808 }
1809 }
1810 }
1811 line->sb_cols = len;
1812 line->sb_cells = p;
1813 line->sb_fill_attr = new_fill_attr;
1814 fill_attr = new_fill_attr;
1815 ++term->tl_scrollback.ga_len;
1816
1817 if (ga_grow(&ga, 1) == FAIL)
1818 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1819 else
1820 {
1821 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1822 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1823 }
1824 ga_clear(&ga);
1825 }
1826 else
1827 vim_free(p);
1828 }
1829 }
1830
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001831 // Add trailing empty lines.
1832 for (pos.row = term->tl_scrollback.ga_len;
1833 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1834 ++pos.row)
1835 {
1836 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1837 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1838 }
1839
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001840 term->tl_dirty_snapshot = FALSE;
1841#ifdef FEAT_TIMERS
1842 term->tl_timer_set = FALSE;
1843#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001844}
1845
1846/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001847 * Loop over all windows in the current tab, and also curwin, which is not
1848 * encountered when using a terminal in a popup window.
1849 * Return TRUE if "*wp" was set to the next window.
1850 */
1851 static int
1852for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1853{
1854 if (*wp == NULL)
1855 *wp = firstwin;
1856 else if ((*wp)->w_next != NULL)
1857 *wp = (*wp)->w_next;
1858 else if (!*did_curwin)
1859 *wp = curwin;
1860 else
1861 return FALSE;
1862 if (*wp == curwin)
1863 *did_curwin = TRUE;
1864 return TRUE;
1865}
1866
1867/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001868 * If needed, add the current lines of the terminal to scrollback and to the
1869 * buffer. Called after the job has ended and when switching to
1870 * Terminal-Normal mode.
1871 * When "redraw" is TRUE redraw the windows that show the terminal.
1872 */
1873 static void
1874may_move_terminal_to_buffer(term_T *term, int redraw)
1875{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001876 if (term->tl_vterm == NULL)
1877 return;
1878
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001879 // Update the snapshot only if something changes or the buffer does not
1880 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001881 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1882 <= term->tl_scrollback_scrolled)
1883 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001884
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001885 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001886 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1887 &term->tl_default_color.fg, &term->tl_default_color.bg);
1888
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001889 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001890 {
1891 win_T *wp = NULL;
1892 int did_curwin = FALSE;
1893
1894 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001895 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001896 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001897 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001898 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1899 wp->w_cursor.col = 0;
1900 wp->w_valid = 0;
1901 if (wp->w_cursor.lnum >= wp->w_height)
1902 {
1903 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001904
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001905 if (wp->w_topline < min_topline)
1906 wp->w_topline = min_topline;
1907 }
1908 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001909 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001910 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001911 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001912}
1913
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001914#if defined(FEAT_TIMERS) || defined(PROTO)
1915/*
1916 * Check if any terminal timer expired. If so, copy text from the terminal to
1917 * the buffer.
1918 * Return the time until the next timer will expire.
1919 */
1920 int
1921term_check_timers(int next_due_arg, proftime_T *now)
1922{
1923 term_T *term;
1924 int next_due = next_due_arg;
1925
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001926 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001927 {
1928 if (term->tl_timer_set && !term->tl_normal_mode)
1929 {
1930 long this_due = proftime_time_left(&term->tl_timer_due, now);
1931
1932 if (this_due <= 1)
1933 {
1934 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001935 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001936 }
1937 else if (next_due == -1 || next_due > this_due)
1938 next_due = this_due;
1939 }
1940 }
1941
1942 return next_due;
1943}
1944#endif
1945
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001946/*
1947 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
1948 * otherwise end it.
1949 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001950 static void
1951set_terminal_mode(term_T *term, int normal_mode)
1952{
1953 term->tl_normal_mode = normal_mode;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001954 if (!normal_mode)
1955 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001956 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001957 if (term->tl_buffer == curbuf)
1958 maketitle();
1959}
1960
1961/*
Bram Moolenaare2978022020-04-26 14:47:44 +02001962 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001963 * Move the vterm contents into the scrollback buffer and free the vterm.
1964 */
1965 static void
1966cleanup_vterm(term_T *term)
1967{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001968 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01001969 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001970 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001971 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001972}
1973
1974/*
1975 * Switch from Terminal-Job mode to Terminal-Normal mode.
1976 * Suspends updating the terminal window.
1977 */
1978 static void
1979term_enter_normal_mode(void)
1980{
1981 term_T *term = curbuf->b_term;
1982
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001983 set_terminal_mode(term, TRUE);
1984
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001985 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001986 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001987
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001988 // Move the window cursor to the position of the cursor in the
1989 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001990 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1991 + term->tl_cursor_pos.row + 1;
1992 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02001993 if (coladvance(term->tl_cursor_pos.col) == FAIL)
1994 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001995 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001996
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001997 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001998 curwin->w_topline = term->tl_scrollback_scrolled + 1;
1999}
2000
2001/*
2002 * Returns TRUE if the current window contains a terminal and we are in
2003 * Terminal-Normal mode.
2004 */
2005 int
2006term_in_normal_mode(void)
2007{
2008 term_T *term = curbuf->b_term;
2009
2010 return term != NULL && term->tl_normal_mode;
2011}
2012
2013/*
2014 * Switch from Terminal-Normal mode to Terminal-Job mode.
2015 * Restores updating the terminal window.
2016 */
2017 void
2018term_enter_job_mode()
2019{
2020 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002021
2022 set_terminal_mode(term, FALSE);
2023
2024 if (term->tl_channel_closed)
2025 cleanup_vterm(term);
2026 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002027#ifdef FEAT_PROP_POPUP
2028 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002029 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002030#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002031}
2032
2033/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002034 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002035 * Note: while waiting a terminal may be closed and freed if the channel is
2036 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002037 */
2038 static int
2039term_vgetc()
2040{
2041 int c;
2042 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002043 int modify_other_keys =
2044 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002045
2046 State = TERMINAL;
2047 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002048#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002049 ctrl_break_was_pressed = FALSE;
2050#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002051 if (modify_other_keys)
2052 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002053 c = vgetc();
2054 got_int = FALSE;
2055 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002056 if (modify_other_keys)
2057 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002058 return c;
2059}
2060
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002061static int mouse_was_outside = FALSE;
2062
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002063/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002064 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002065 * Return FAIL when the key needs to be handled in Normal mode.
2066 * Return OK when the key was dropped or sent to the terminal.
2067 */
2068 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002069send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002070{
2071 char msg[KEY_BUF_LEN];
2072 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002073 int dragging_outside = FALSE;
2074
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002075 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002076 switch (c)
2077 {
2078 case NUL:
2079 case K_ZERO:
2080 if (typed)
2081 stuffcharReadbuff(c);
2082 return FAIL;
2083
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002084 case K_TABLINE:
2085 stuffcharReadbuff(c);
2086 return FAIL;
2087
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002088 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002089 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002090 return FAIL;
2091
2092 case K_LEFTDRAG:
2093 case K_MIDDLEDRAG:
2094 case K_RIGHTDRAG:
2095 case K_X1DRAG:
2096 case K_X2DRAG:
2097 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002098 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002099 case K_LEFTMOUSE:
2100 case K_LEFTMOUSE_NM:
2101 case K_LEFTRELEASE:
2102 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002103 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002104 case K_MIDDLEMOUSE:
2105 case K_MIDDLERELEASE:
2106 case K_RIGHTMOUSE:
2107 case K_RIGHTRELEASE:
2108 case K_X1MOUSE:
2109 case K_X1RELEASE:
2110 case K_X2MOUSE:
2111 case K_X2RELEASE:
2112
2113 case K_MOUSEUP:
2114 case K_MOUSEDOWN:
2115 case K_MOUSELEFT:
2116 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002117 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002118 int row = mouse_row;
2119 int col = mouse_col;
2120
2121#ifdef FEAT_PROP_POPUP
2122 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002123 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002124 row -= popup_top_extra(curwin);
2125 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002126 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002127#endif
2128 if (row < W_WINROW(curwin)
2129 || row >= (W_WINROW(curwin) + curwin->w_height)
2130 || col < curwin->w_wincol
2131 || col >= W_ENDCOL(curwin)
2132 || dragging_outside)
2133 {
2134 // click or scroll outside the current window or on status
2135 // line or vertical separator
2136 if (typed)
2137 {
2138 stuffcharReadbuff(c);
2139 mouse_was_outside = TRUE;
2140 }
2141 return FAIL;
2142 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002143 }
2144 }
2145 if (typed)
2146 mouse_was_outside = FALSE;
2147
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002148 // Convert the typed key to a sequence of bytes for the job.
2149 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002150 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002151 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002152 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2153 (char_u *)msg, (int)len, NULL);
2154
2155 return OK;
2156}
2157
2158 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002159position_cursor(win_T *wp, VTermPos *pos, int add_off UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002160{
2161 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2162 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002163#ifdef FEAT_PROP_POPUP
2164 if (add_off && popup_is_popup(curwin))
2165 {
2166 wp->w_wrow += popup_top_extra(curwin);
2167 wp->w_wcol += popup_left_extra(curwin);
2168 }
2169#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002170 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2171}
2172
2173/*
2174 * Handle CTRL-W "": send register contents to the job.
2175 */
2176 static void
2177term_paste_register(int prev_c UNUSED)
2178{
2179 int c;
2180 list_T *l;
2181 listitem_T *item;
2182 long reglen = 0;
2183 int type;
2184
2185#ifdef FEAT_CMDL_INFO
2186 if (add_to_showcmd(prev_c))
2187 if (add_to_showcmd('"'))
2188 out_flush();
2189#endif
2190 c = term_vgetc();
2191#ifdef FEAT_CMDL_INFO
2192 clear_showcmd();
2193#endif
2194 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002195 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002196 return;
2197
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002198 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002199 if (c == '=' && get_expr_register() != '=')
2200 return;
2201 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002202 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002203 return;
2204
2205 l = (list_T *)get_reg_contents(c, GREG_LIST);
2206 if (l != NULL)
2207 {
2208 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002209 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002210 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002211 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002212#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002213 char_u *tmp = s;
2214
2215 if (!enc_utf8 && enc_codepage > 0)
2216 {
2217 WCHAR *ret = NULL;
2218 int length = 0;
2219
2220 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2221 (int)STRLEN(s), &ret, &length);
2222 if (ret != NULL)
2223 {
2224 WideCharToMultiByte_alloc(CP_UTF8, 0,
2225 ret, length, (char **)&s, &length, 0, 0);
2226 vim_free(ret);
2227 }
2228 }
2229#endif
2230 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2231 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002232#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002233 if (tmp != s)
2234 vim_free(s);
2235#endif
2236
2237 if (item->li_next != NULL || type == MLINE)
2238 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2239 (char_u *)"\r", 1, NULL);
2240 }
2241 list_free(l);
2242 }
2243}
2244
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002245/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002246 * Return TRUE when waiting for a character in the terminal, the cursor of the
2247 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002248 */
2249 int
2250terminal_is_active()
2251{
2252 return in_terminal_loop != NULL;
2253}
2254
Bram Moolenaar83d47902020-03-26 20:34:00 +01002255/*
2256 * Return the highight group name for the terminal; "Terminal" if not set.
2257 */
2258 static char_u *
2259term_get_highlight_name(term_T *term)
2260{
2261 if (term->tl_highlight_name == NULL)
2262 return (char_u *)"Terminal";
2263 return term->tl_highlight_name;
2264}
2265
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002266#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002267 cursorentry_T *
2268term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2269{
2270 term_T *term = in_terminal_loop;
2271 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002272 int id;
2273 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002274
Bram Moolenaara80faa82020-04-12 19:37:17 +02002275 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002276 entry.shape = entry.mshape =
2277 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2278 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2279 SHAPE_BLOCK;
2280 entry.percentage = 20;
2281 if (term->tl_cursor_blink)
2282 {
2283 entry.blinkwait = 700;
2284 entry.blinkon = 400;
2285 entry.blinkoff = 250;
2286 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002287
Bram Moolenaar83d47902020-03-26 20:34:00 +01002288 // The highlight group overrules the defaults.
2289 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002290 if (id != 0)
2291 {
2292 syn_id2colors(id, &term_fg, &term_bg);
2293 *fg = term_bg;
2294 }
2295 else
2296 *fg = gui.back_pixel;
2297
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002298 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002299 {
2300 if (id != 0)
2301 *bg = term_fg;
2302 else
2303 *bg = gui.norm_pixel;
2304 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002305 else
2306 *bg = color_name2handle(term->tl_cursor_color);
2307 entry.name = "n";
2308 entry.used_for = SHAPE_CURSOR;
2309
2310 return &entry;
2311}
2312#endif
2313
Bram Moolenaard317b382018-02-08 22:33:31 +01002314 static void
2315may_output_cursor_props(void)
2316{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002317 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002318 || last_set_cursor_shape != desired_cursor_shape
2319 || last_set_cursor_blink != desired_cursor_blink)
2320 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002321 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002322 last_set_cursor_shape = desired_cursor_shape;
2323 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002324 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002325 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002326 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002327 ui_cursor_shape_forced(TRUE);
2328 else
2329 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2330 }
2331}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002332
Bram Moolenaard317b382018-02-08 22:33:31 +01002333/*
2334 * Set the cursor color and shape, if not last set to these.
2335 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002336 static void
2337may_set_cursor_props(term_T *term)
2338{
2339#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002340 // For the GUI the cursor properties are obtained with
2341 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002342 if (gui.in_use)
2343 return;
2344#endif
2345 if (in_terminal_loop == term)
2346 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002347 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002348 desired_cursor_shape = term->tl_cursor_shape;
2349 desired_cursor_blink = term->tl_cursor_blink;
2350 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002351 }
2352}
2353
Bram Moolenaard317b382018-02-08 22:33:31 +01002354/*
2355 * Reset the desired cursor properties and restore them when needed.
2356 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002357 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002358prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002359{
2360#ifdef FEAT_GUI
2361 if (gui.in_use)
2362 return;
2363#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002364 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002365 desired_cursor_shape = -1;
2366 desired_cursor_blink = -1;
2367 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002368}
2369
2370/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002371 * Returns TRUE if the current window contains a terminal and we are sending
2372 * keys to the job.
2373 * If "check_job_status" is TRUE update the job status.
2374 */
2375 static int
2376term_use_loop_check(int check_job_status)
2377{
2378 term_T *term = curbuf->b_term;
2379
2380 return term != NULL
2381 && !term->tl_normal_mode
2382 && term->tl_vterm != NULL
2383 && term_job_running_check(term, check_job_status);
2384}
2385
2386/*
2387 * Returns TRUE if the current window contains a terminal and we are sending
2388 * keys to the job.
2389 */
2390 int
2391term_use_loop(void)
2392{
2393 return term_use_loop_check(FALSE);
2394}
2395
2396/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002397 * Called when entering a window with the mouse. If this is a terminal window
2398 * we may want to change state.
2399 */
2400 void
2401term_win_entered()
2402{
2403 term_T *term = curbuf->b_term;
2404
2405 if (term != NULL)
2406 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002407 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002408 {
2409 reset_VIsual_and_resel();
2410 if (State & INSERT)
2411 stop_insert_mode = TRUE;
2412 }
2413 mouse_was_outside = FALSE;
2414 enter_mouse_col = mouse_col;
2415 enter_mouse_row = mouse_row;
2416 }
2417}
2418
2419/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002420 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2421 * Return the Ctrl-key value in that case.
2422 */
2423 static int
2424raw_c_to_ctrl(int c)
2425{
2426 if ((mod_mask & MOD_MASK_CTRL)
2427 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2428 return c & 0x1f;
2429 return c;
2430}
2431
2432/*
2433 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2434 * May set "mod_mask".
2435 */
2436 static int
2437ctrl_to_raw_c(int c)
2438{
2439 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2440 {
2441 mod_mask |= MOD_MASK_CTRL;
2442 return c + '@';
2443 }
2444 return c;
2445}
2446
2447/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002448 * Wait for input and send it to the job.
2449 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2450 * when there is no more typahead.
2451 * Return when the start of a CTRL-W command is typed or anything else that
2452 * should be handled as a Normal mode command.
2453 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2454 * the terminal was closed.
2455 */
2456 int
2457terminal_loop(int blocking)
2458{
2459 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002460 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002461 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002462 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002463#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002464 int tty_fd = curbuf->b_term->tl_job->jv_channel
2465 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002466#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002467 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002468
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002469 // Remember the terminal we are sending keys to. However, the terminal
2470 // might be closed while waiting for a character, e.g. typing "exit" in a
2471 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2472 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002473 in_terminal_loop = curbuf->b_term;
2474
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002475 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002476 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002477 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002478 if (termwinkey == Ctrl_W)
2479 termwinkey = 0;
2480 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002481 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002482 may_set_cursor_props(curbuf->b_term);
2483
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002484 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002485 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002486#ifdef FEAT_GUI
2487 if (!curbuf->b_term->tl_system)
2488#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002489 // TODO: skip screen update when handling a sequence of keys.
2490 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002491 while (must_redraw != 0)
2492 if (update_screen(0) == FAIL)
2493 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002494 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002495 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002496 break;
2497
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002498 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002499 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002500
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002501 raw_c = term_vgetc();
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002502if (raw_c > 0)
2503 ch_log(NULL, "terminal_loop() got %d", raw_c);
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002504 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002505 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002506 // Job finished while waiting for a character. Push back the
2507 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002508 if (raw_c != K_IGNORE)
2509 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002510 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002511 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002512 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002513 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002514 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002515
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002516#ifdef UNIX
2517 /*
2518 * The shell or another program may change the tty settings. Getting
2519 * them for every typed character is a bit of overhead, but it's needed
2520 * for the first character typed, e.g. when Vim starts in a shell.
2521 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002522 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002523 {
2524 ttyinfo_T info;
2525
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002526 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002527 if (get_tty_info(tty_fd, &info) == OK)
2528 term_backspace_char = info.backspace;
2529 }
2530#endif
2531
Bram Moolenaar4f974752019-02-17 17:44:42 +01002532#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002533 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2534 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002535 if (ctrl_break_was_pressed)
2536 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2537#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002538 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2539 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002540 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002541#ifdef FEAT_GUI
2542 && !curbuf->b_term->tl_system
2543#endif
2544 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002545 {
2546 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002547 int prev_raw_c = raw_c;
2548 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002549
2550#ifdef FEAT_CMDL_INFO
2551 if (add_to_showcmd(c))
2552 out_flush();
2553#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002554 raw_c = term_vgetc();
2555 c = raw_c_to_ctrl(raw_c);
2556
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002557#ifdef FEAT_CMDL_INFO
2558 clear_showcmd();
2559#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002560 if (!term_use_loop_check(TRUE)
2561 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002562 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002563 break;
2564
2565 if (prev_c == Ctrl_BSL)
2566 {
2567 if (c == Ctrl_N)
2568 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002569 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002570 term_enter_normal_mode();
2571 ret = FAIL;
2572 goto theend;
2573 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002574 // Send both keys to the terminal, first one here, second one
2575 // below.
2576 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2577 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002578 }
2579 else if (c == Ctrl_C)
2580 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002581 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002582 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2583 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002584 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002585 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002586 // "CTRL-W .": send CTRL-W to the job
2587 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002588 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002589 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002590 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002591 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002592 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002593 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002594 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002595 else if (c == 'N')
2596 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002597 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002598 term_enter_normal_mode();
2599 ret = FAIL;
2600 goto theend;
2601 }
2602 else if (c == '"')
2603 {
2604 term_paste_register(prev_c);
2605 continue;
2606 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002607 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002608 {
Bram Moolenaara4b26992019-08-15 20:58:54 +02002609 char_u buf[MB_MAXBYTES + 2];
2610
2611 // Put the command into the typeahead buffer, when using the
2612 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2613 buf[0] = Ctrl_W;
2614 buf[(*mb_char2bytes)(c, buf + 1) + 1] = NUL;
2615 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002616 ret = OK;
2617 goto theend;
2618 }
2619 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002620# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002621 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002622 {
2623 WCHAR wc;
2624 char_u mb[3];
2625
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002626 mb[0] = (unsigned)raw_c >> 8;
2627 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002628 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002629 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002630 }
2631# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002632 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002633 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002634 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002635 // We are sure to come back here, don't reset the cursor color
2636 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002637 restore_cursor = FALSE;
2638
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002639 ret = OK;
2640 goto theend;
2641 }
2642 }
2643 ret = FAIL;
2644
2645theend:
2646 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002647 if (restore_cursor)
2648 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002649
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002650 // Move a snapshot of the screen contents to the buffer, so that completion
2651 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002652 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2653 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002654
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002655 return ret;
2656}
2657
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002658 static void
2659may_toggle_cursor(term_T *term)
2660{
2661 if (in_terminal_loop == term)
2662 {
2663 if (term->tl_cursor_visible)
2664 cursor_on();
2665 else
2666 cursor_off();
2667 }
2668}
2669
2670/*
Bram Moolenaar83d47902020-03-26 20:34:00 +01002671 * Cache "Terminal" highlight group colors.
2672 */
2673 void
2674set_terminal_default_colors(int cterm_fg, int cterm_bg)
2675{
2676 term_default_cterm_fg = cterm_fg - 1;
2677 term_default_cterm_bg = cterm_bg - 1;
2678}
2679
2680 static int
2681get_default_cterm_fg(term_T *term)
2682{
2683 if (term->tl_highlight_name != NULL)
2684 {
2685 int id = syn_name2id(term->tl_highlight_name);
2686 int fg = -1;
2687 int bg = -1;
2688
2689 if (id > 0)
2690 syn_id2cterm_bg(id, &fg, &bg);
2691 return fg;
2692 }
2693 return term_default_cterm_fg;
2694}
2695
2696 static int
2697get_default_cterm_bg(term_T *term)
2698{
2699 if (term->tl_highlight_name != NULL)
2700 {
2701 int id = syn_name2id(term->tl_highlight_name);
2702 int fg = -1;
2703 int bg = -1;
2704
2705 if (id > 0)
2706 syn_id2cterm_bg(id, &fg, &bg);
2707 return bg;
2708 }
2709 return term_default_cterm_bg;
2710}
2711
2712/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002713 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002714 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002715 */
2716 static int
2717color2index(VTermColor *color, int fg, int *boldp)
2718{
2719 int red = color->red;
2720 int blue = color->blue;
2721 int green = color->green;
2722
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002723 if (VTERM_COLOR_IS_DEFAULT_FG(color)
2724 || VTERM_COLOR_IS_DEFAULT_BG(color))
2725 return 0;
2726 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002727 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002728 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002729 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002730 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002731 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002732 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2733 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2734 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002735 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002736 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2737 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2738 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2739 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2740 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2741 case 10: return lookup_color(20, fg, boldp) + 1; // red
2742 case 11: return lookup_color(16, fg, boldp) + 1; // green
2743 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2744 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2745 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2746 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2747 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002748 }
2749 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002750
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002751 if (t_colors >= 256)
2752 {
2753 if (red == blue && red == green)
2754 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002755 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002756 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002757 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2758 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2759 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002760 int i;
2761
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002762 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002763 return 17; // 00/00/00
2764 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002765 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002766 for (i = 0; i < 23; ++i)
2767 if (red < cutoff[i])
2768 return i + 233;
2769 return 256;
2770 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002771 {
2772 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2773 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002774
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002775 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002776 for (ri = 0; ri < 5; ++ri)
2777 if (red < cutoff[ri])
2778 break;
2779 for (gi = 0; gi < 5; ++gi)
2780 if (green < cutoff[gi])
2781 break;
2782 for (bi = 0; bi < 5; ++bi)
2783 if (blue < cutoff[bi])
2784 break;
2785 return 17 + ri * 36 + gi * 6 + bi;
2786 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002787 }
2788 return 0;
2789}
2790
2791/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002792 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002793 */
2794 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002795vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002796{
2797 int attr = 0;
2798
2799 if (cellattrs.bold)
2800 attr |= HL_BOLD;
2801 if (cellattrs.underline)
2802 attr |= HL_UNDERLINE;
2803 if (cellattrs.italic)
2804 attr |= HL_ITALIC;
2805 if (cellattrs.strike)
2806 attr |= HL_STRIKETHROUGH;
2807 if (cellattrs.reverse)
2808 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002809 return attr;
2810}
2811
2812/*
2813 * Store Vterm attributes in "cell" from highlight flags.
2814 */
2815 static void
2816hl2vtermAttr(int attr, cellattr_T *cell)
2817{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002818 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002819 if (attr & HL_BOLD)
2820 cell->attrs.bold = 1;
2821 if (attr & HL_UNDERLINE)
2822 cell->attrs.underline = 1;
2823 if (attr & HL_ITALIC)
2824 cell->attrs.italic = 1;
2825 if (attr & HL_STRIKETHROUGH)
2826 cell->attrs.strike = 1;
2827 if (attr & HL_INVERSE)
2828 cell->attrs.reverse = 1;
2829}
2830
2831/*
2832 * Convert the attributes of a vterm cell into an attribute index.
2833 */
2834 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002835cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002836 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002837 win_T *wp,
2838 VTermScreenCellAttrs cellattrs,
2839 VTermColor cellfg,
2840 VTermColor cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002841{
2842 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002843
2844#ifdef FEAT_GUI
2845 if (gui.in_use)
2846 {
2847 guicolor_T fg, bg;
2848
2849 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2850 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2851 return get_gui_attr_idx(attr, fg, bg);
2852 }
2853 else
2854#endif
2855#ifdef FEAT_TERMGUICOLORS
2856 if (p_tgc)
2857 {
2858 guicolor_T fg, bg;
2859
2860 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2861 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2862
2863 return get_tgc_attr_idx(attr, fg, bg);
2864 }
2865 else
2866#endif
2867 {
2868 int bold = MAYBE;
2869 int fg = color2index(&cellfg, TRUE, &bold);
2870 int bg = color2index(&cellbg, FALSE, &bold);
2871
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002872 // Use the 'wincolor' or "Terminal" highlighting for the default
2873 // colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002874 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002875 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002876 int wincolor_fg = -1;
2877 int wincolor_bg = -1;
2878
2879 if (wp != NULL && *wp->w_p_wcr != NUL)
2880 {
2881 int id = syn_name2id(curwin->w_p_wcr);
2882
2883 // Get the 'wincolor' group colors.
2884 if (id > 0)
2885 syn_id2cterm_bg(id, &wincolor_fg, &wincolor_bg);
2886 }
2887 if (fg == 0)
2888 {
2889 if (wincolor_fg >= 0)
2890 fg = wincolor_fg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002891 else
2892 {
2893 int cterm_fg = get_default_cterm_fg(term);
2894
2895 if (cterm_fg >= 0)
2896 fg = cterm_fg + 1;
2897 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002898 }
2899 if (bg == 0)
2900 {
2901 if (wincolor_bg >= 0)
2902 bg = wincolor_bg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002903 else
2904 {
2905 int cterm_bg = get_default_cterm_bg(term);
2906
2907 if (cterm_bg >= 0)
2908 bg = cterm_bg + 1;
2909 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002910 }
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002911 }
2912
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002913 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002914 if (bold == TRUE)
2915 attr |= HL_BOLD;
2916 return get_cterm_attr_idx(attr, fg, bg);
2917 }
2918 return 0;
2919}
2920
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002921 static void
2922set_dirty_snapshot(term_T *term)
2923{
2924 term->tl_dirty_snapshot = TRUE;
2925#ifdef FEAT_TIMERS
2926 if (!term->tl_normal_mode)
2927 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002928 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002929 profile_setlimit(100L, &term->tl_timer_due);
2930 term->tl_timer_set = TRUE;
2931 }
2932#endif
2933}
2934
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002935 static int
2936handle_damage(VTermRect rect, void *user)
2937{
2938 term_T *term = (term_T *)user;
2939
2940 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2941 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002942 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002943 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002944 return 1;
2945}
2946
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002947 static void
2948term_scroll_up(term_T *term, int start_row, int count)
2949{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002950 win_T *wp = NULL;
2951 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002952 VTermColor fg, bg;
2953 VTermScreenCellAttrs attr;
2954 int clear_attr;
2955
Bram Moolenaara80faa82020-04-12 19:37:17 +02002956 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002957
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002958 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002959 {
2960 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002961 {
2962 // Set the color to clear lines with.
2963 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2964 &fg, &bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002965 clear_attr = cell2attr(term, wp, attr, fg, bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002966 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002967 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002968 }
2969}
2970
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002971 static int
2972handle_moverect(VTermRect dest, VTermRect src, void *user)
2973{
2974 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002975 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002976
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002977 // Scrolling up is done much more efficiently by deleting lines instead of
2978 // redrawing the text. But avoid doing this multiple times, postpone until
2979 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002980 if (dest.start_col == src.start_col
2981 && dest.end_col == src.end_col
2982 && dest.start_row < src.start_row)
2983 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002984 if (dest.start_row == 0)
2985 term->tl_postponed_scroll += count;
2986 else
2987 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002988 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02002989
2990 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
2991 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002992 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02002993
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002994 // Note sure if the scrolling will work correctly, let's do a complete
2995 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002996 redraw_buf_later(term->tl_buffer, NOT_VALID);
2997 return 1;
2998}
2999
3000 static int
3001handle_movecursor(
3002 VTermPos pos,
3003 VTermPos oldpos UNUSED,
3004 int visible,
3005 void *user)
3006{
3007 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003008 win_T *wp = NULL;
3009 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003010
3011 term->tl_cursor_pos = pos;
3012 term->tl_cursor_visible = visible;
3013
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003014 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003015 {
3016 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003017 position_cursor(wp, &pos, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003018 }
3019 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
3020 {
3021 may_toggle_cursor(term);
3022 update_cursor(term, term->tl_cursor_visible);
3023 }
3024
3025 return 1;
3026}
3027
3028 static int
3029handle_settermprop(
3030 VTermProp prop,
3031 VTermValue *value,
3032 void *user)
3033{
3034 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003035 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003036
3037 switch (prop)
3038 {
3039 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003040 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003041 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003042 if (strval == NULL)
3043 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003044 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003045 // a blank title isn't useful, make it empty, so that "running" is
3046 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003047 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003048 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003049 // Same as blank
3050 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003051 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003052 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3053 term->tl_title = NULL;
3054 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003055 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003056 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003057#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003058 else if (!enc_utf8 && enc_codepage > 0)
3059 {
3060 WCHAR *ret = NULL;
3061 int length = 0;
3062
3063 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003064 (char*)value->string.str,
3065 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003066 if (ret != NULL)
3067 {
3068 WideCharToMultiByte_alloc(enc_codepage, 0,
3069 ret, length, (char**)&term->tl_title,
3070 &length, 0, 0);
3071 vim_free(ret);
3072 }
3073 }
3074#endif
3075 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003076 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003077 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003078 strval = NULL;
3079 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003080 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003081 if (term == curbuf->b_term)
3082 maketitle();
3083 break;
3084
3085 case VTERM_PROP_CURSORVISIBLE:
3086 term->tl_cursor_visible = value->boolean;
3087 may_toggle_cursor(term);
3088 out_flush();
3089 break;
3090
3091 case VTERM_PROP_CURSORBLINK:
3092 term->tl_cursor_blink = value->boolean;
3093 may_set_cursor_props(term);
3094 break;
3095
3096 case VTERM_PROP_CURSORSHAPE:
3097 term->tl_cursor_shape = value->number;
3098 may_set_cursor_props(term);
3099 break;
3100
3101 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003102 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003103 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003104 if (strval == NULL)
3105 break;
3106 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003107 may_set_cursor_props(term);
3108 break;
3109
3110 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003111 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003112 term->tl_using_altscreen = value->boolean;
3113 break;
3114
3115 default:
3116 break;
3117 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003118 vim_free(strval);
3119
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003120 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003121 return 1;
3122}
3123
3124/*
3125 * The job running in the terminal resized the terminal.
3126 */
3127 static int
3128handle_resize(int rows, int cols, void *user)
3129{
3130 term_T *term = (term_T *)user;
3131 win_T *wp;
3132
3133 term->tl_rows = rows;
3134 term->tl_cols = cols;
3135 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003136 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003137 term->tl_vterm_size_changed = FALSE;
3138 else
3139 {
3140 FOR_ALL_WINDOWS(wp)
3141 {
3142 if (wp->w_buffer == term->tl_buffer)
3143 {
3144 win_setheight_win(rows, wp);
3145 win_setwidth_win(cols, wp);
3146 }
3147 }
3148 redraw_buf_later(term->tl_buffer, NOT_VALID);
3149 }
3150 return 1;
3151}
3152
3153/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003154 * If the number of lines that are stored goes over 'termscrollback' then
3155 * delete the first 10%.
3156 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3157 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003158 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003159 static void
3160limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003161{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003162 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003163 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003164 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003165 int i;
3166
3167 curbuf = term->tl_buffer;
3168 for (i = 0; i < todo; ++i)
3169 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003170 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3171 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003172 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003173 }
3174 curbuf = curwin->w_buffer;
3175
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003176 gap->ga_len -= todo;
3177 mch_memmove(gap->ga_data,
3178 (sb_line_T *)gap->ga_data + todo,
3179 sizeof(sb_line_T) * gap->ga_len);
3180 if (update_buffer)
3181 term->tl_scrollback_scrolled -= todo;
3182 }
3183}
3184
3185/*
3186 * Handle a line that is pushed off the top of the screen.
3187 */
3188 static int
3189handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3190{
3191 term_T *term = (term_T *)user;
3192 garray_T *gap;
3193 int update_buffer;
3194
3195 if (term->tl_normal_mode)
3196 {
3197 // In Terminal-Normal mode the user interacts with the buffer, thus we
3198 // must not change it. Postpone adding the scrollback lines.
3199 gap = &term->tl_scrollback_postponed;
3200 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003201 }
3202 else
3203 {
3204 // First remove the lines that were appended before, the pushed line
3205 // goes above it.
3206 cleanup_scrollback(term);
3207 gap = &term->tl_scrollback;
3208 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003209 }
3210
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003211 limit_scrollback(term, gap, update_buffer);
3212
3213 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003214 {
3215 cellattr_T *p = NULL;
3216 int len = 0;
3217 int i;
3218 int c;
3219 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003220 int text_len;
3221 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003222 sb_line_T *line;
3223 garray_T ga;
3224 cellattr_T fill_attr = term->tl_default_color;
3225
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003226 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003227 for (i = 0; i < cols; ++i)
3228 if (cells[i].chars[0] != 0)
3229 len = i + 1;
3230 else
3231 cell2cellattr(&cells[i], &fill_attr);
3232
3233 ga_init2(&ga, 1, 100);
3234 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003235 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003236 if (p != NULL)
3237 {
3238 for (col = 0; col < len; col += cells[col].width)
3239 {
3240 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3241 {
3242 ga.ga_len = 0;
3243 break;
3244 }
3245 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3246 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3247 (char_u *)ga.ga_data + ga.ga_len);
3248 cell2cellattr(&cells[col], &p[col]);
3249 }
3250 }
3251 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003252 {
3253 if (update_buffer)
3254 text = (char_u *)"";
3255 else
3256 text = vim_strsave((char_u *)"");
3257 text_len = 0;
3258 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003259 else
3260 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003261 text = ga.ga_data;
3262 text_len = ga.ga_len;
3263 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003264 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003265 if (update_buffer)
3266 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003267
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003268 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003269 line->sb_cols = len;
3270 line->sb_cells = p;
3271 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003272 if (update_buffer)
3273 {
3274 line->sb_text = NULL;
3275 ++term->tl_scrollback_scrolled;
3276 ga_clear(&ga); // free the text
3277 }
3278 else
3279 {
3280 line->sb_text = text;
3281 ga_init(&ga); // text is kept in tl_scrollback_postponed
3282 }
3283 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003284 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003285 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003286}
3287
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003288/*
3289 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3290 * received and stored in tl_scrollback_postponed.
3291 */
3292 static void
3293handle_postponed_scrollback(term_T *term)
3294{
3295 int i;
3296
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003297 if (term->tl_scrollback_postponed.ga_len == 0)
3298 return;
3299 ch_log(NULL, "Moving postponed scrollback to scrollback");
3300
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003301 // First remove the lines that were appended before, the pushed lines go
3302 // above it.
3303 cleanup_scrollback(term);
3304
3305 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3306 {
3307 char_u *text;
3308 sb_line_T *pp_line;
3309 sb_line_T *line;
3310
3311 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3312 break;
3313 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3314
3315 text = pp_line->sb_text;
3316 if (text == NULL)
3317 text = (char_u *)"";
3318 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3319 vim_free(pp_line->sb_text);
3320
3321 line = (sb_line_T *)term->tl_scrollback.ga_data
3322 + term->tl_scrollback.ga_len;
3323 line->sb_cols = pp_line->sb_cols;
3324 line->sb_cells = pp_line->sb_cells;
3325 line->sb_fill_attr = pp_line->sb_fill_attr;
3326 line->sb_text = NULL;
3327 ++term->tl_scrollback_scrolled;
3328 ++term->tl_scrollback.ga_len;
3329 }
3330
3331 ga_clear(&term->tl_scrollback_postponed);
3332 limit_scrollback(term, &term->tl_scrollback, TRUE);
3333}
3334
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003335static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003336 handle_damage, // damage
3337 handle_moverect, // moverect
3338 handle_movecursor, // movecursor
3339 handle_settermprop, // settermprop
3340 NULL, // bell
3341 handle_resize, // resize
3342 handle_pushline, // sb_pushline
3343 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003344};
3345
3346/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003347 * Do the work after the channel of a terminal was closed.
3348 * Must be called only when updating_screen is FALSE.
3349 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3350 */
3351 static int
3352term_after_channel_closed(term_T *term)
3353{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003354 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003355 if (!term->tl_normal_mode)
3356 {
3357 int fnum = term->tl_buffer->b_fnum;
3358
3359 cleanup_vterm(term);
3360
3361 if (term->tl_finish == TL_FINISH_CLOSE)
3362 {
3363 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003364 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003365#ifdef FEAT_PROP_POPUP
3366 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003367
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003368 // If this was a terminal in a popup window, go back to the
3369 // previous window.
3370 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3371 {
3372 pwin = curwin;
3373 if (win_valid(prevwin))
3374 win_enter(prevwin, FALSE);
3375 }
3376 else
3377#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003378 // If this is the last normal window: exit Vim.
3379 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3380 {
3381 exarg_T ea;
3382
Bram Moolenaara80faa82020-04-12 19:37:17 +02003383 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003384 ex_quit(&ea);
3385 return TRUE;
3386 }
3387
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003388 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003389 ch_log(NULL, "terminal job finished, closing window");
3390 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003391 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003392 if (curwin == aucmd_win)
3393 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003394 if (do_set_w_closing)
3395 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003396 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003397 if (do_set_w_closing)
3398 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003399 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003400#ifdef FEAT_PROP_POPUP
3401 if (pwin != NULL)
3402 popup_close_with_retval(pwin, 0);
3403#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003404 return TRUE;
3405 }
3406 if (term->tl_finish == TL_FINISH_OPEN
3407 && term->tl_buffer->b_nwindows == 0)
3408 {
3409 char buf[50];
3410
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003411 // TODO: use term_opencmd
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003412 ch_log(NULL, "terminal job finished, opening window");
3413 vim_snprintf(buf, sizeof(buf),
3414 term->tl_opencmd == NULL
3415 ? "botright sbuf %d"
3416 : (char *)term->tl_opencmd, fnum);
3417 do_cmdline_cmd((char_u *)buf);
3418 }
3419 else
3420 ch_log(NULL, "terminal job finished");
3421 }
3422
3423 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3424 return FALSE;
3425}
3426
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003427#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3428/*
3429 * If the current window is a terminal in a popup window and the job has
3430 * finished, close the popup window and to back to the previous window.
3431 * Otherwise return FAIL.
3432 */
3433 int
3434may_close_term_popup(void)
3435{
3436 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3437 && !term_job_running(curbuf->b_term))
3438 {
3439 win_T *pwin = curwin;
3440
3441 if (win_valid(prevwin))
3442 win_enter(prevwin, FALSE);
3443 popup_close_with_retval(pwin, 0);
3444 return OK;
3445 }
3446 return FAIL;
3447}
3448#endif
3449
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003450/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003451 * Called when a channel has been closed.
3452 * If this was a channel for a terminal window then finish it up.
3453 */
3454 void
3455term_channel_closed(channel_T *ch)
3456{
3457 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003458 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003459 int did_one = FALSE;
3460
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003461 for (term = first_term; term != NULL; term = next_term)
3462 {
3463 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003464 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003465 {
3466 term->tl_channel_closed = TRUE;
3467 did_one = TRUE;
3468
Bram Moolenaard23a8232018-02-10 18:45:26 +01003469 VIM_CLEAR(term->tl_title);
3470 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003471#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003472 if (term->tl_out_fd != NULL)
3473 {
3474 fclose(term->tl_out_fd);
3475 term->tl_out_fd = NULL;
3476 }
3477#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003478
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003479 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003480 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003481 // Cannot open or close windows now. Can happen when
3482 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003483 term->tl_channel_recently_closed = TRUE;
3484 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003485 }
3486
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003487 if (term_after_channel_closed(term))
3488 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003489 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003490 }
3491
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003492 if (did_one)
3493 {
3494 redraw_statuslines();
3495
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003496 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003497 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003498 typebuf_was_filled = TRUE;
3499
3500 term = curbuf->b_term;
3501 if (term != NULL)
3502 {
3503 if (term->tl_job == ch->ch_job)
3504 maketitle();
3505 update_cursor(term, term->tl_cursor_visible);
3506 }
3507 }
3508}
3509
3510/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003511 * To be called after resetting updating_screen: handle any terminal where the
3512 * channel was closed.
3513 */
3514 void
3515term_check_channel_closed_recently()
3516{
3517 term_T *term;
3518 term_T *next_term;
3519
3520 for (term = first_term; term != NULL; term = next_term)
3521 {
3522 next_term = term->tl_next;
3523 if (term->tl_channel_recently_closed)
3524 {
3525 term->tl_channel_recently_closed = FALSE;
3526 if (term_after_channel_closed(term))
3527 // start over, the list may have changed
3528 next_term = first_term;
3529 }
3530 }
3531}
3532
3533/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003534 * Fill one screen line from a line of the terminal.
3535 * Advances "pos" to past the last column.
3536 */
3537 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003538term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003539 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003540 win_T *wp,
3541 VTermScreen *screen,
3542 VTermPos *pos,
3543 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003544{
3545 int off = screen_get_current_line_off();
3546
3547 for (pos->col = 0; pos->col < max_col; )
3548 {
3549 VTermScreenCell cell;
3550 int c;
3551
3552 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003553 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003554
3555 c = cell.chars[0];
3556 if (c == NUL)
3557 {
3558 ScreenLines[off] = ' ';
3559 if (enc_utf8)
3560 ScreenLinesUC[off] = NUL;
3561 }
3562 else
3563 {
3564 if (enc_utf8)
3565 {
3566 int i;
3567
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003568 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003569 for (i = 0; i < Screen_mco
3570 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3571 {
3572 ScreenLinesC[i][off] = cell.chars[i + 1];
3573 if (cell.chars[i + 1] == 0)
3574 break;
3575 }
3576 if (c >= 0x80 || (Screen_mco > 0
3577 && ScreenLinesC[0][off] != 0))
3578 {
3579 ScreenLines[off] = ' ';
3580 ScreenLinesUC[off] = c;
3581 }
3582 else
3583 {
3584 ScreenLines[off] = c;
3585 ScreenLinesUC[off] = NUL;
3586 }
3587 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003588#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003589 else if (has_mbyte && c >= 0x80)
3590 {
3591 char_u mb[MB_MAXBYTES+1];
3592 WCHAR wc = c;
3593
3594 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3595 (char*)mb, 2, 0, 0) > 1)
3596 {
3597 ScreenLines[off] = mb[0];
3598 ScreenLines[off + 1] = mb[1];
3599 cell.width = mb_ptr2cells(mb);
3600 }
3601 else
3602 ScreenLines[off] = c;
3603 }
3604#endif
3605 else
3606 ScreenLines[off] = c;
3607 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003608 ScreenAttrs[off] = cell2attr(term, wp, cell.attrs, cell.fg, cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003609
3610 ++pos->col;
3611 ++off;
3612 if (cell.width == 2)
3613 {
3614 if (enc_utf8)
3615 ScreenLinesUC[off] = NUL;
3616
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003617 // don't set the second byte to NUL for a DBCS encoding, it
3618 // has been set above
Bram Moolenaar13568252018-03-16 20:46:58 +01003619 if (enc_utf8 || !has_mbyte)
3620 ScreenLines[off] = NUL;
3621
3622 ++pos->col;
3623 ++off;
3624 }
3625 }
3626}
3627
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003628#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003629 static void
3630update_system_term(term_T *term)
3631{
3632 VTermPos pos;
3633 VTermScreen *screen;
3634
3635 if (term->tl_vterm == NULL)
3636 return;
3637 screen = vterm_obtain_screen(term->tl_vterm);
3638
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003639 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003640 while (term->tl_toprow > 0
3641 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3642 {
3643 int save_p_more = p_more;
3644
3645 p_more = FALSE;
3646 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003647 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003648 p_more = save_p_more;
3649 --term->tl_toprow;
3650 }
3651
3652 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3653 && pos.row < Rows; ++pos.row)
3654 {
3655 if (pos.row < term->tl_rows)
3656 {
3657 int max_col = MIN(Columns, term->tl_cols);
3658
Bram Moolenaar83d47902020-03-26 20:34:00 +01003659 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003660 }
3661 else
3662 pos.col = 0;
3663
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003664 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003665 }
3666
3667 term->tl_dirty_row_start = MAX_ROW;
3668 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003669}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003670#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003671
3672/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003673 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3674 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003675 * Terminal-Normal mode.
3676 */
3677 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003678term_do_update_window(win_T *wp)
3679{
3680 term_T *term = wp->w_buffer->b_term;
3681
3682 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3683}
3684
3685/*
3686 * Called to update a window that contains an active terminal.
3687 */
3688 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003689term_update_window(win_T *wp)
3690{
3691 term_T *term = wp->w_buffer->b_term;
3692 VTerm *vterm;
3693 VTermScreen *screen;
3694 VTermState *state;
3695 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003696 int rows, cols;
3697 int newrows, newcols;
3698 int minsize;
3699 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003700
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003701 vterm = term->tl_vterm;
3702 screen = vterm_obtain_screen(vterm);
3703 state = vterm_obtain_state(vterm);
3704
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003705 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3706 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003707 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003708 {
3709 term->tl_dirty_row_start = 0;
3710 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003711
3712 if (term->tl_postponed_scroll > 0
3713 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003714 // Scrolling is usually faster than redrawing, when there are only
3715 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003716 term_scroll_up(term, 0, term->tl_postponed_scroll);
3717 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003718 }
3719
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003720 /*
3721 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003722 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003723 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003724 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003725
Bram Moolenaar498c2562018-04-15 23:45:15 +02003726 newrows = 99999;
3727 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003728 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003729 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003730 // Always use curwin, it may be a popup window.
3731 win_T *wwp = twp == NULL ? curwin : twp;
3732
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003733 // When more than one window shows the same terminal, use the
3734 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003735 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003736 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003737 newrows = MIN(newrows, wwp->w_height);
3738 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003739 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003740 if (twp == NULL)
3741 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003742 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003743 if (newrows == 99999 || newcols == 99999)
3744 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003745 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3746 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3747
3748 if (term->tl_rows != newrows || term->tl_cols != newcols)
3749 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003750 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003751 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003752 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003753 newrows);
3754 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003755
3756 // Updating the terminal size will cause the snapshot to be cleared.
3757 // When not in terminal_loop() we need to restore it.
3758 if (term != in_terminal_loop)
3759 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003760 }
3761
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003762 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003763 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003764 position_cursor(wp, &pos, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003765
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003766 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3767 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003768 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003769 if (pos.row < term->tl_rows)
3770 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003771 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003772
Bram Moolenaar83d47902020-03-26 20:34:00 +01003773 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003774 }
3775 else
3776 pos.col = 0;
3777
Bram Moolenaarf118d482018-03-13 13:14:00 +01003778 screen_line(wp->w_winrow + pos.row
3779#ifdef FEAT_MENU
3780 + winbar_height(wp)
3781#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003782 , wp->w_wincol, pos.col, wp->w_width,
3783#ifdef FEAT_PROP_POPUP
3784 popup_is_popup(wp) ? SLF_POPUP :
3785#endif
3786 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003787 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003788 term->tl_dirty_row_start = MAX_ROW;
3789 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003790}
3791
3792/*
3793 * Return TRUE if "wp" is a terminal window where the job has finished.
3794 */
3795 int
3796term_is_finished(buf_T *buf)
3797{
3798 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3799}
3800
3801/*
3802 * Return TRUE if "wp" is a terminal window where the job has finished or we
3803 * are in Terminal-Normal mode, thus we show the buffer contents.
3804 */
3805 int
3806term_show_buffer(buf_T *buf)
3807{
3808 term_T *term = buf->b_term;
3809
3810 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3811}
3812
3813/*
3814 * The current buffer is going to be changed. If there is terminal
3815 * highlighting remove it now.
3816 */
3817 void
3818term_change_in_curbuf(void)
3819{
3820 term_T *term = curbuf->b_term;
3821
3822 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3823 {
3824 free_scrollback(term);
3825 redraw_buf_later(term->tl_buffer, NOT_VALID);
3826
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003827 // The buffer is now like a normal buffer, it cannot be easily
3828 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003829 set_string_option_direct((char_u *)"buftype", -1,
3830 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3831 }
3832}
3833
3834/*
3835 * Get the screen attribute for a position in the buffer.
3836 * Use a negative "col" to get the filler background color.
3837 */
3838 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003839term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003840{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003841 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003842 term_T *term = buf->b_term;
3843 sb_line_T *line;
3844 cellattr_T *cellattr;
3845
3846 if (lnum > term->tl_scrollback.ga_len)
3847 cellattr = &term->tl_default_color;
3848 else
3849 {
3850 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3851 if (col < 0 || col >= line->sb_cols)
3852 cellattr = &line->sb_fill_attr;
3853 else
3854 cellattr = line->sb_cells + col;
3855 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003856 return cell2attr(term, wp, cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003857}
3858
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003859/*
3860 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003861 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003862 */
3863 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003864cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003865{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003866 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3867 if (rgb->index == 0)
3868 rgb->type = VTERM_COLOR_RGB;
3869 else
3870 {
3871 rgb->type = VTERM_COLOR_INDEXED;
3872 --rgb->index;
3873 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003874}
3875
3876/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003877 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003878 */
3879 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003880init_default_colors(term_T *term, win_T *wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003881{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003882 VTermColor *fg, *bg;
3883 int fgval, bgval;
3884 int id;
3885
Bram Moolenaara80faa82020-04-12 19:37:17 +02003886 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003887 term->tl_default_color.width = 1;
3888 fg = &term->tl_default_color.fg;
3889 bg = &term->tl_default_color.bg;
3890
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003891 // Vterm uses a default black background. Set it to white when
3892 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003893 if (*p_bg == 'l')
3894 {
3895 fgval = 0;
3896 bgval = 255;
3897 }
3898 else
3899 {
3900 fgval = 255;
3901 bgval = 0;
3902 }
3903 fg->red = fg->green = fg->blue = fgval;
3904 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003905 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3906 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003907
Bram Moolenaar83d47902020-03-26 20:34:00 +01003908 // The 'wincolor' or the highlight group overrules the defaults.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003909 if (wp != NULL && *wp->w_p_wcr != NUL)
3910 id = syn_name2id(wp->w_p_wcr);
3911 else
Bram Moolenaar83d47902020-03-26 20:34:00 +01003912 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003913
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003914 // Use the actual color for the GUI and when 'termguicolors' is set.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003915#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3916 if (0
3917# ifdef FEAT_GUI
3918 || gui.in_use
3919# endif
3920# ifdef FEAT_TERMGUICOLORS
3921 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003922# ifdef FEAT_VTP
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003923 // Finally get INVALCOLOR on this execution path
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003924 || (!p_tgc && t_colors >= 256)
3925# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003926# endif
3927 )
3928 {
3929 guicolor_T fg_rgb = INVALCOLOR;
3930 guicolor_T bg_rgb = INVALCOLOR;
3931
3932 if (id != 0)
3933 syn_id2colors(id, &fg_rgb, &bg_rgb);
3934
3935# ifdef FEAT_GUI
3936 if (gui.in_use)
3937 {
3938 if (fg_rgb == INVALCOLOR)
3939 fg_rgb = gui.norm_pixel;
3940 if (bg_rgb == INVALCOLOR)
3941 bg_rgb = gui.back_pixel;
3942 }
3943# ifdef FEAT_TERMGUICOLORS
3944 else
3945# endif
3946# endif
3947# ifdef FEAT_TERMGUICOLORS
3948 {
3949 if (fg_rgb == INVALCOLOR)
3950 fg_rgb = cterm_normal_fg_gui_color;
3951 if (bg_rgb == INVALCOLOR)
3952 bg_rgb = cterm_normal_bg_gui_color;
3953 }
3954# endif
3955 if (fg_rgb != INVALCOLOR)
3956 {
3957 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3958
3959 fg->red = (unsigned)(rgb >> 16);
3960 fg->green = (unsigned)(rgb >> 8) & 255;
3961 fg->blue = (unsigned)rgb & 255;
3962 }
3963 if (bg_rgb != INVALCOLOR)
3964 {
3965 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
3966
3967 bg->red = (unsigned)(rgb >> 16);
3968 bg->green = (unsigned)(rgb >> 8) & 255;
3969 bg->blue = (unsigned)rgb & 255;
3970 }
3971 }
3972 else
3973#endif
3974 if (id != 0 && t_colors >= 16)
3975 {
Bram Moolenaar83d47902020-03-26 20:34:00 +01003976 int cterm_fg = get_default_cterm_fg(term);
3977 int cterm_bg = get_default_cterm_bg(term);
3978
3979 if (cterm_fg >= 0)
3980 cterm_color2vterm(cterm_fg, fg);
3981 if (cterm_bg >= 0)
3982 cterm_color2vterm(cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003983 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003984 else
3985 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003986#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003987 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003988#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003989
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003990 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003991 if (cterm_normal_fg_color > 0)
3992 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003993 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003994# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3995# ifdef VIMDLL
3996 if (!gui.in_use)
3997# endif
3998 {
3999 tmp = fg->red;
4000 fg->red = fg->blue;
4001 fg->blue = tmp;
4002 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004003# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004004 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004005# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004006 else
4007 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004008# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004009
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004010 if (cterm_normal_bg_color > 0)
4011 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004012 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004013# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4014# ifdef VIMDLL
4015 if (!gui.in_use)
4016# endif
4017 {
4018 tmp = fg->red;
4019 fg->red = fg->blue;
4020 fg->blue = tmp;
4021 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004022# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004023 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004024# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004025 else
4026 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004027# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004028 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004029}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004030
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004031#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4032/*
4033 * Set the 16 ANSI colors from array of RGB values
4034 */
4035 static void
4036set_vterm_palette(VTerm *vterm, long_u *rgb)
4037{
4038 int index = 0;
4039 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004040
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004041 for (; index < 16; index++)
4042 {
4043 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004044
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004045 color.red = (unsigned)(rgb[index] >> 16);
4046 color.green = (unsigned)(rgb[index] >> 8) & 255;
4047 color.blue = (unsigned)rgb[index] & 255;
4048 vterm_state_set_palette_color(state, index, &color);
4049 }
4050}
4051
4052/*
4053 * Set the ANSI color palette from a list of colors
4054 */
4055 static int
4056set_ansi_colors_list(VTerm *vterm, list_T *list)
4057{
4058 int n = 0;
4059 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004060 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004061
Bram Moolenaarb0992022020-01-30 14:55:42 +01004062 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004063 {
4064 char_u *color_name;
4065 guicolor_T guicolor;
4066
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004067 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004068 if (color_name == NULL)
4069 return FAIL;
4070
4071 guicolor = GUI_GET_COLOR(color_name);
4072 if (guicolor == INVALCOLOR)
4073 return FAIL;
4074
4075 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4076 }
4077
4078 if (n != 16 || li != NULL)
4079 return FAIL;
4080
4081 set_vterm_palette(vterm, rgb);
4082
4083 return OK;
4084}
4085
4086/*
4087 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4088 */
4089 static void
4090init_vterm_ansi_colors(VTerm *vterm)
4091{
4092 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4093
4094 if (var != NULL
4095 && (var->di_tv.v_type != VAR_LIST
4096 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004097 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004098 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004099 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004100}
4101#endif
4102
Bram Moolenaar52acb112018-03-18 19:20:22 +01004103/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004104 * Handles a "drop" command from the job in the terminal.
4105 * "item" is the file name, "item->li_next" may have options.
4106 */
4107 static void
4108handle_drop_command(listitem_T *item)
4109{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004110 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004111 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004112 int bufnr;
4113 win_T *wp;
4114 tabpage_T *tp;
4115 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004116 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004117
4118 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4119 FOR_ALL_TAB_WINDOWS(tp, wp)
4120 {
4121 if (wp->w_buffer->b_fnum == bufnr)
4122 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004123 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004124 goto_tabpage_win(tp, wp);
4125 return;
4126 }
4127 }
4128
Bram Moolenaara80faa82020-04-12 19:37:17 +02004129 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004130
4131 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4132 && opt_item->li_tv.vval.v_dict != NULL)
4133 {
4134 dict_T *dict = opt_item->li_tv.vval.v_dict;
4135 char_u *p;
4136
Bram Moolenaar8f667172018-12-14 15:38:31 +01004137 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004138 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004139 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004140 if (p != NULL)
4141 {
4142 if (check_ff_value(p) == FAIL)
4143 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4144 else
4145 ea.force_ff = *p;
4146 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004147 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004148 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004149 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004150 if (p != NULL)
4151 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004152 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004153 if (ea.cmd != NULL)
4154 {
4155 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4156 ea.force_enc = 11;
4157 tofree = ea.cmd;
4158 }
4159 }
4160
Bram Moolenaar8f667172018-12-14 15:38:31 +01004161 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004162 if (p != NULL)
4163 get_bad_opt(p, &ea);
4164
4165 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4166 ea.force_bin = FORCE_BIN;
4167 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4168 ea.force_bin = FORCE_BIN;
4169 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4170 ea.force_bin = FORCE_NOBIN;
4171 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4172 ea.force_bin = FORCE_NOBIN;
4173 }
4174
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004175 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004176 if (ea.cmd == NULL)
4177 ea.cmd = (char_u *)"split";
4178 ea.arg = fname;
4179 ea.cmdidx = CMD_split;
4180 ex_splitview(&ea);
4181
4182 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004183}
4184
4185/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004186 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4187 */
4188 static int
4189is_permitted_term_api(char_u *func, char_u *pat)
4190{
4191 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4192}
4193
4194/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004195 * Handles a function call from the job running in a terminal.
4196 * "item" is the function name, "item->li_next" has the arguments.
4197 */
4198 static void
4199handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4200{
4201 char_u *func;
4202 typval_T argvars[2];
4203 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004204 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004205
4206 if (item->li_next == NULL)
4207 {
4208 ch_log(channel, "Missing function arguments for call");
4209 return;
4210 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004211 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004212
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004213 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004214 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004215 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004216 return;
4217 }
4218
4219 argvars[0].v_type = VAR_NUMBER;
4220 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4221 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004222 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004223 funcexe.firstline = 1L;
4224 funcexe.lastline = 1L;
4225 funcexe.evaluate = TRUE;
4226 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004227 {
4228 clear_tv(&rettv);
4229 ch_log(channel, "Function %s called", func);
4230 }
4231 else
4232 ch_log(channel, "Calling function %s failed", func);
4233}
4234
4235/*
4236 * Called by libvterm when it cannot recognize an OSC sequence.
4237 * We recognize a terminal API command.
4238 */
4239 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004240parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004241{
4242 term_T *term = (term_T *)user;
4243 js_read_T reader;
4244 typval_T tv;
4245 channel_T *channel = term->tl_job == NULL ? NULL
4246 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004247 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004248
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004249 // We recognize only OSC 5 1 ; {command}
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004250 if (command != 51)
4251 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004252
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004253 // Concatenate what was received until the final piece is found.
4254 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4255 {
4256 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004257 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004258 }
4259 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004260 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004261 if (!frag.final)
4262 return 1;
4263
4264 ((char *)gap->ga_data)[gap->ga_len] = 0;
4265 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004266 reader.js_fill = NULL;
4267 reader.js_used = 0;
4268 if (json_decode(&reader, &tv, 0) == OK
4269 && tv.v_type == VAR_LIST
4270 && tv.vval.v_list != NULL)
4271 {
4272 listitem_T *item = tv.vval.v_list->lv_first;
4273
4274 if (item == NULL)
4275 ch_log(channel, "Missing command");
4276 else
4277 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004278 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004279
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004280 // Make sure an invoked command doesn't delete the buffer (and the
4281 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004282 ++term->tl_buffer->b_locked;
4283
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004284 item = item->li_next;
4285 if (item == NULL)
4286 ch_log(channel, "Missing argument for %s", cmd);
4287 else if (STRCMP(cmd, "drop") == 0)
4288 handle_drop_command(item);
4289 else if (STRCMP(cmd, "call") == 0)
4290 handle_call_command(term, channel, item);
4291 else
4292 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004293 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004294 }
4295 }
4296 else
4297 ch_log(channel, "Invalid JSON received");
4298
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004299 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004300 clear_tv(&tv);
4301 return 1;
4302}
4303
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004304/*
4305 * Called by libvterm when it cannot recognize a CSI sequence.
4306 * We recognize the window position report.
4307 */
4308 static int
4309parse_csi(
4310 const char *leader UNUSED,
4311 const long args[],
4312 int argcount,
4313 const char *intermed UNUSED,
4314 char command,
4315 void *user)
4316{
4317 term_T *term = (term_T *)user;
4318 char buf[100];
4319 int len;
4320 int x = 0;
4321 int y = 0;
4322 win_T *wp;
4323
4324 // We recognize only CSI 13 t
4325 if (command != 't' || argcount != 1 || args[0] != 13)
4326 return 0; // not handled
4327
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004328 // When getting the window position is not possible or it fails it results
4329 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004330#if defined(FEAT_GUI) \
4331 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4332 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004333 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004334#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004335
4336 FOR_ALL_WINDOWS(wp)
4337 if (wp->w_buffer == term->tl_buffer)
4338 break;
4339 if (wp != NULL)
4340 {
4341#ifdef FEAT_GUI
4342 if (gui.in_use)
4343 {
4344 x += wp->w_wincol * gui.char_width;
4345 y += W_WINROW(wp) * gui.char_height;
4346 }
4347 else
4348#endif
4349 {
4350 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004351 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004352 x += wp->w_wincol * 7;
4353 y += W_WINROW(wp) * 10;
4354 }
4355 }
4356
4357 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4358 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4359 (char_u *)buf, len, NULL);
4360 return 1;
4361}
4362
Bram Moolenaard8637282020-05-20 18:41:41 +02004363static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004364 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004365 parse_csi, // csi
4366 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004367 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004368};
4369
4370/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004371 * Use Vim's allocation functions for vterm so profiling works.
4372 */
4373 static void *
4374vterm_malloc(size_t size, void *data UNUSED)
4375{
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004376 return alloc_clear(size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004377}
4378
4379 static void
4380vterm_memfree(void *ptr, void *data UNUSED)
4381{
4382 vim_free(ptr);
4383}
4384
4385static VTermAllocatorFunctions vterm_allocator = {
4386 &vterm_malloc,
4387 &vterm_memfree
4388};
4389
4390/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004391 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004392 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004393 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004394 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004395create_vterm(term_T *term, int rows, int cols)
4396{
4397 VTerm *vterm;
4398 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004399 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004400 VTermValue value;
4401
Bram Moolenaar756ef112018-04-10 12:04:27 +02004402 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004403 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004404 if (vterm == NULL)
4405 return FAIL;
4406
4407 // Allocate screen and state here, so we can bail out if that fails.
4408 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004409 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004410 if (state == NULL || screen == NULL)
4411 {
4412 vterm_free(vterm);
4413 return FAIL;
4414 }
4415
Bram Moolenaar52acb112018-03-18 19:20:22 +01004416 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004417 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004418 vterm_set_utf8(vterm, 1);
4419
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004420 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004421
4422 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004423 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004424 &term->tl_default_color.fg,
4425 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004426
Bram Moolenaar9e587872019-05-13 20:27:23 +02004427 if (t_colors < 16)
4428 // Less than 16 colors: assume that bold means using a bright color for
4429 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004430 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4431
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004432 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004433 vterm_screen_reset(screen, 1 /* hard */);
4434
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004435 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004436 vterm_screen_enable_altscreen(screen, 1);
4437
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004438 // For unix do not use a blinking cursor. In an xterm this causes the
4439 // cursor to blink if it's blinking in the xterm.
4440 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004441#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004442 if (GetCaretBlinkTime() == INFINITE)
4443 value.boolean = 0;
4444 else
4445 value.boolean = 1;
4446#else
4447 value.boolean = 0;
4448#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004449 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004450 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004451
4452 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004453}
4454
4455/*
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004456 * Called when 'wincolor' was set.
4457 */
4458 void
4459term_update_colors(void)
4460{
4461 term_T *term = curwin->w_buffer->b_term;
4462
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01004463 if (term->tl_vterm == NULL)
4464 return;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004465 init_default_colors(term, curwin);
4466 vterm_state_set_default_colors(
4467 vterm_obtain_state(term->tl_vterm),
4468 &term->tl_default_color.fg,
4469 &term->tl_default_color.bg);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01004470
4471 redraw_later(NOT_VALID);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004472}
4473
4474/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004475 * Return the text to show for the buffer name and status.
4476 */
4477 char_u *
4478term_get_status_text(term_T *term)
4479{
4480 if (term->tl_status_text == NULL)
4481 {
4482 char_u *txt;
4483 size_t len;
4484
4485 if (term->tl_normal_mode)
4486 {
4487 if (term_job_running(term))
4488 txt = (char_u *)_("Terminal");
4489 else
4490 txt = (char_u *)_("Terminal-finished");
4491 }
4492 else if (term->tl_title != NULL)
4493 txt = term->tl_title;
4494 else if (term_none_open(term))
4495 txt = (char_u *)_("active");
4496 else if (term_job_running(term))
4497 txt = (char_u *)_("running");
4498 else
4499 txt = (char_u *)_("finished");
4500 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004501 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004502 if (term->tl_status_text != NULL)
4503 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
4504 term->tl_buffer->b_fname, txt);
4505 }
4506 return term->tl_status_text;
4507}
4508
4509/*
4510 * Mark references in jobs of terminals.
4511 */
4512 int
4513set_ref_in_term(int copyID)
4514{
4515 int abort = FALSE;
4516 term_T *term;
4517 typval_T tv;
4518
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004519 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004520 if (term->tl_job != NULL)
4521 {
4522 tv.v_type = VAR_JOB;
4523 tv.vval.v_job = term->tl_job;
4524 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4525 }
4526 return abort;
4527}
4528
4529/*
4530 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004531 * Returns NULL when the buffer is not for a terminal window and logs a message
4532 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004533 */
4534 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004535term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004536{
4537 buf_T *buf;
4538
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004539 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004540 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004541 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004542 --emsg_off;
4543 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004544 {
4545 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004546 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004547 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004548 return buf;
4549}
4550
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004551 static void
4552clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004553{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004554 CLEAR_FIELD(*cell);
4555 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4556 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004557}
4558
4559 static void
4560dump_term_color(FILE *fd, VTermColor *color)
4561{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004562 int index;
4563
4564 if (VTERM_COLOR_IS_INDEXED(color))
4565 index = color->index + 1;
4566 else if (color->type == 0)
4567 // use RGB values
4568 index = 255;
4569 else
4570 // default color
4571 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004572 fprintf(fd, "%02x%02x%02x%d",
4573 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004574 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004575}
4576
4577/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004578 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004579 *
4580 * Each screen cell in full is:
4581 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4582 * {characters} is a space for an empty cell
4583 * For a double-width character "+" is changed to "*" and the next cell is
4584 * skipped.
4585 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4586 * when "&" use the same as the previous cell.
4587 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4588 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4589 * {color-idx} is a number from 0 to 255
4590 *
4591 * Screen cell with same width, attributes and color as the previous one:
4592 * |{characters}
4593 *
4594 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4595 *
4596 * Repeating the previous screen cell:
4597 * @{count}
4598 */
4599 void
4600f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4601{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004602 buf_T *buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004603 term_T *term;
4604 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004605 int max_height = 0;
4606 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004607 stat_T st;
4608 FILE *fd;
4609 VTermPos pos;
4610 VTermScreen *screen;
4611 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004612 VTermState *state;
4613 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004614
4615 if (check_restricted() || check_secure())
4616 return;
4617 if (buf == NULL)
4618 return;
4619 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004620 if (term->tl_vterm == NULL)
4621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004622 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004623 return;
4624 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004625
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004626 if (argvars[2].v_type != VAR_UNKNOWN)
4627 {
4628 dict_T *d;
4629
4630 if (argvars[2].v_type != VAR_DICT)
4631 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004632 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004633 return;
4634 }
4635 d = argvars[2].vval.v_dict;
4636 if (d != NULL)
4637 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004638 max_height = dict_get_number(d, (char_u *)"rows");
4639 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004640 }
4641 }
4642
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004643 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004644 if (fname == NULL)
4645 return;
4646 if (mch_stat((char *)fname, &st) >= 0)
4647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004648 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004649 return;
4650 }
4651
Bram Moolenaard96ff162018-02-18 22:13:29 +01004652 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4653 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004654 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004655 return;
4656 }
4657
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004658 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004659
4660 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004661 state = vterm_obtain_state(term->tl_vterm);
4662 vterm_state_get_cursorpos(state, &cursor_pos);
4663
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004664 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4665 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004666 {
4667 int repeat = 0;
4668
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004669 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4670 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004671 {
4672 VTermScreenCell cell;
4673 int same_attr;
4674 int same_chars = TRUE;
4675 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004676 int is_cursor_pos = (pos.col == cursor_pos.col
4677 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004678
4679 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004680 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004681
4682 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4683 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004684 int c = cell.chars[i];
4685 int pc = prev_cell.chars[i];
4686
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004687 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004688 if (i == 0)
4689 {
4690 c = (c == NUL) ? ' ' : c;
4691 pc = (pc == NUL) ? ' ' : pc;
4692 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004693 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004694 same_chars = FALSE;
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004695 if (c == NUL || pc == NUL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004696 break;
4697 }
4698 same_attr = vtermAttr2hl(cell.attrs)
4699 == vtermAttr2hl(prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004700 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4701 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004702 if (same_chars && cell.width == prev_cell.width && same_attr
4703 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004704 {
4705 ++repeat;
4706 }
4707 else
4708 {
4709 if (repeat > 0)
4710 {
4711 fprintf(fd, "@%d", repeat);
4712 repeat = 0;
4713 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004714 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004715
4716 if (cell.chars[0] == NUL)
4717 fputs(" ", fd);
4718 else
4719 {
4720 char_u charbuf[10];
4721 int len;
4722
4723 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4724 && cell.chars[i] != NUL; ++i)
4725 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004726 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004727 fwrite(charbuf, len, 1, fd);
4728 }
4729 }
4730
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004731 // When only the characters differ we don't write anything, the
4732 // following "|", "@" or NL will indicate using the same
4733 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004734 if (cell.width != prev_cell.width || !same_attr)
4735 {
4736 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004737 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004738 else
4739 fputs("+", fd);
4740
4741 if (same_attr)
4742 {
4743 fputs("&", fd);
4744 }
4745 else
4746 {
4747 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004748 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004749 fputs("&", fd);
4750 else
4751 {
4752 fputs("#", fd);
4753 dump_term_color(fd, &cell.fg);
4754 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004755 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004756 fputs("&", fd);
4757 else
4758 {
4759 fputs("#", fd);
4760 dump_term_color(fd, &cell.bg);
4761 }
4762 }
4763 }
4764
4765 prev_cell = cell;
4766 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004767
4768 if (cell.width == 2)
4769 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004770 }
4771 if (repeat > 0)
4772 fprintf(fd, "@%d", repeat);
4773 fputs("\n", fd);
4774 }
4775
4776 fclose(fd);
4777}
4778
4779/*
4780 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4781 */
4782 static void
4783dump_is_corrupt(garray_T *gap)
4784{
4785 ga_concat(gap, (char_u *)"CORRUPT");
4786}
4787
4788 static void
4789append_cell(garray_T *gap, cellattr_T *cell)
4790{
4791 if (ga_grow(gap, 1) == OK)
4792 {
4793 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4794 ++gap->ga_len;
4795 }
4796}
4797
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004798 static void
4799clear_cellattr(cellattr_T *cell)
4800{
4801 CLEAR_FIELD(*cell);
4802 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4803 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4804}
4805
Bram Moolenaard96ff162018-02-18 22:13:29 +01004806/*
4807 * Read the dump file from "fd" and append lines to the current buffer.
4808 * Return the cell width of the longest line.
4809 */
4810 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004811read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004812{
4813 int c;
4814 garray_T ga_text;
4815 garray_T ga_cell;
4816 char_u *prev_char = NULL;
4817 int attr = 0;
4818 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004819 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004820 term_T *term = curbuf->b_term;
4821 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004822 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004823
4824 ga_init2(&ga_text, 1, 90);
4825 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004826 clear_cellattr(&cell);
4827 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004828 cursor_pos->row = -1;
4829 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004830
4831 c = fgetc(fd);
4832 for (;;)
4833 {
4834 if (c == EOF)
4835 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004836 if (c == '\r')
4837 {
4838 // DOS line endings? Ignore.
4839 c = fgetc(fd);
4840 }
4841 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004842 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004843 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004844 if (ga_text.ga_data == NULL)
4845 dump_is_corrupt(&ga_text);
4846 if (ga_grow(&term->tl_scrollback, 1) == OK)
4847 {
4848 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
4849 + term->tl_scrollback.ga_len;
4850
4851 if (max_cells < ga_cell.ga_len)
4852 max_cells = ga_cell.ga_len;
4853 line->sb_cols = ga_cell.ga_len;
4854 line->sb_cells = ga_cell.ga_data;
4855 line->sb_fill_attr = term->tl_default_color;
4856 ++term->tl_scrollback.ga_len;
4857 ga_init(&ga_cell);
4858
4859 ga_append(&ga_text, NUL);
4860 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4861 ga_text.ga_len, FALSE);
4862 }
4863 else
4864 ga_clear(&ga_cell);
4865 ga_text.ga_len = 0;
4866
4867 c = fgetc(fd);
4868 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004869 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004870 {
4871 int prev_len = ga_text.ga_len;
4872
Bram Moolenaar9271d052018-02-25 21:39:46 +01004873 if (c == '>')
4874 {
4875 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004876 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01004877 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
4878 cursor_pos->col = ga_cell.ga_len;
4879 }
4880
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004881 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01004882 c = fgetc(fd);
4883 if (c != EOF)
4884 ga_append(&ga_text, c);
4885 for (;;)
4886 {
4887 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004888 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01004889 || c == EOF || c == '\n')
4890 break;
4891 ga_append(&ga_text, c);
4892 }
4893
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004894 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01004895 vim_free(prev_char);
4896 if (ga_text.ga_data != NULL)
4897 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
4898 ga_text.ga_len - prev_len);
4899
Bram Moolenaar9271d052018-02-25 21:39:46 +01004900 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004901 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004902 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01004903 }
4904 else if (c == '+' || c == '*')
4905 {
4906 int is_bg;
4907
4908 cell.width = c == '+' ? 1 : 2;
4909
4910 c = fgetc(fd);
4911 if (c == '&')
4912 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004913 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01004914 c = fgetc(fd);
4915 }
4916 else if (isdigit(c))
4917 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004918 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01004919 attr = 0;
4920 while (isdigit(c))
4921 {
4922 attr = attr * 10 + (c - '0');
4923 c = fgetc(fd);
4924 }
4925 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004926
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004927 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004928 for (is_bg = 0; is_bg <= 1; ++is_bg)
4929 {
4930 if (c == '&')
4931 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004932 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004933 c = fgetc(fd);
4934 }
4935 else if (c == '#')
4936 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004937 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004938
4939 c = fgetc(fd);
4940 red = hex2nr(c);
4941 c = fgetc(fd);
4942 red = (red << 4) + hex2nr(c);
4943 c = fgetc(fd);
4944 green = hex2nr(c);
4945 c = fgetc(fd);
4946 green = (green << 4) + hex2nr(c);
4947 c = fgetc(fd);
4948 blue = hex2nr(c);
4949 c = fgetc(fd);
4950 blue = (blue << 4) + hex2nr(c);
4951 c = fgetc(fd);
4952 if (!isdigit(c))
4953 dump_is_corrupt(&ga_text);
4954 while (isdigit(c))
4955 {
4956 index = index * 10 + (c - '0');
4957 c = fgetc(fd);
4958 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004959 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004960 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004961 type = VTERM_COLOR_RGB;
4962 if (index == 0)
4963 {
4964 if (is_bg)
4965 type |= VTERM_COLOR_DEFAULT_BG;
4966 else
4967 type |= VTERM_COLOR_DEFAULT_FG;
4968 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004969 }
4970 else
4971 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004972 type = VTERM_COLOR_INDEXED;
4973 index -= 1;
4974 }
4975 if (is_bg)
4976 {
4977 cell.bg.type = type;
4978 cell.bg.red = red;
4979 cell.bg.green = green;
4980 cell.bg.blue = blue;
4981 cell.bg.index = index;
4982 }
4983 else
4984 {
4985 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004986 cell.fg.red = red;
4987 cell.fg.green = green;
4988 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004989 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004990 }
4991 }
4992 else
4993 dump_is_corrupt(&ga_text);
4994 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004995 }
4996 else
4997 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004998 }
4999 else
5000 dump_is_corrupt(&ga_text);
5001
5002 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005003 if (cell.width == 2)
5004 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005005 }
5006 else if (c == '@')
5007 {
5008 if (prev_char == NULL)
5009 dump_is_corrupt(&ga_text);
5010 else
5011 {
5012 int count = 0;
5013
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005014 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005015 for (;;)
5016 {
5017 c = fgetc(fd);
5018 if (!isdigit(c))
5019 break;
5020 count = count * 10 + (c - '0');
5021 }
5022
5023 while (count-- > 0)
5024 {
5025 ga_concat(&ga_text, prev_char);
5026 append_cell(&ga_cell, &cell);
5027 }
5028 }
5029 }
5030 else
5031 {
5032 dump_is_corrupt(&ga_text);
5033 c = fgetc(fd);
5034 }
5035 }
5036
5037 if (ga_text.ga_len > 0)
5038 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005039 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005040 dump_is_corrupt(&ga_text);
5041 ga_append(&ga_text, NUL);
5042 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5043 ga_text.ga_len, FALSE);
5044 }
5045
5046 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005047 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005048 vim_free(prev_char);
5049
5050 return max_cells;
5051}
5052
5053/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005054 * Return an allocated string with at least "text_width" "=" characters and
5055 * "fname" inserted in the middle.
5056 */
5057 static char_u *
5058get_separator(int text_width, char_u *fname)
5059{
5060 int width = MAX(text_width, curwin->w_width);
5061 char_u *textline;
5062 int fname_size;
5063 char_u *p = fname;
5064 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005065 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005066
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005067 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005068 if (textline == NULL)
5069 return NULL;
5070
5071 fname_size = vim_strsize(fname);
5072 if (fname_size < width - 8)
5073 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005074 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005075 width = MAX(text_width, fname_size + 8);
5076 }
5077 else if (fname_size > width - 8)
5078 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005079 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005080 p = gettail(fname);
5081 fname_size = vim_strsize(p);
5082 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005083 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005084 while (fname_size > width - 8)
5085 {
5086 p += (*mb_ptr2len)(p);
5087 fname_size = vim_strsize(p);
5088 }
5089
5090 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5091 textline[i] = '=';
5092 textline[i++] = ' ';
5093
5094 STRCPY(textline + i, p);
5095 off = STRLEN(textline);
5096 textline[off] = ' ';
5097 for (i = 1; i < (width - fname_size) / 2; ++i)
5098 textline[off + i] = '=';
5099 textline[off + i] = NUL;
5100
5101 return textline;
5102}
5103
5104/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005105 * Common for "term_dumpdiff()" and "term_dumpload()".
5106 */
5107 static void
5108term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5109{
5110 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005111 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005112 char_u buf1[NUMBUFLEN];
5113 char_u buf2[NUMBUFLEN];
5114 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005115 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005116 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005117 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005118 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005119 char_u *textline = NULL;
5120
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005121 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005122 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005123 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005124 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005125 if (fname1 == NULL || (do_diff && fname2 == NULL))
5126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005127 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005128 return;
5129 }
5130 fd1 = mch_fopen((char *)fname1, READBIN);
5131 if (fd1 == NULL)
5132 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005133 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005134 return;
5135 }
5136 if (do_diff)
5137 {
5138 fd2 = mch_fopen((char *)fname2, READBIN);
5139 if (fd2 == NULL)
5140 {
5141 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005142 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005143 return;
5144 }
5145 }
5146
5147 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005148 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5149 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5150 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5151 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5152 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005153
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005154 if (opt.jo_term_name == NULL)
5155 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005156 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005157
Bram Moolenaar51e14382019-05-25 20:21:28 +02005158 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005159 if (fname_tofree != NULL)
5160 {
5161 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5162 opt.jo_term_name = fname_tofree;
5163 }
5164 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005165
Bram Moolenaar87abab92019-06-03 21:14:59 +02005166 if (opt.jo_bufnr_buf != NULL)
5167 {
5168 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5169
5170 // With "bufnr" argument: enter the window with this buffer and make it
5171 // empty.
5172 if (wp == NULL)
5173 semsg(_(e_invarg2), "bufnr");
5174 else
5175 {
5176 buf = curbuf;
5177 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005178 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005179 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005180 redraw_later(NOT_VALID);
5181 }
5182 }
5183 else
5184 // Create a new terminal window.
5185 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5186
Bram Moolenaard96ff162018-02-18 22:13:29 +01005187 if (buf != NULL && buf->b_term != NULL)
5188 {
5189 int i;
5190 linenr_T bot_lnum;
5191 linenr_T lnum;
5192 term_T *term = buf->b_term;
5193 int width;
5194 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005195 VTermPos cursor_pos1;
5196 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005197
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005198 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005199
Bram Moolenaard96ff162018-02-18 22:13:29 +01005200 rettv->vval.v_number = buf->b_fnum;
5201
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005202 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005203 width = read_dump_file(fd1, &cursor_pos1);
5204
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005205 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005206 if (cursor_pos1.row >= 0)
5207 {
5208 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5209 coladvance(cursor_pos1.col);
5210 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005211
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005212 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005213 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005214
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005215 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005216 if (!do_diff)
5217 goto theend;
5218
5219 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5220
Bram Moolenaar4a696342018-04-05 18:45:26 +02005221 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005222 if (textline == NULL)
5223 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005224 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5225 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5226 vim_free(textline);
5227
5228 textline = get_separator(width, fname2);
5229 if (textline == NULL)
5230 goto theend;
5231 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5232 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005233 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005234
5235 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005236 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005237 if (width2 > width)
5238 {
5239 vim_free(textline);
5240 textline = alloc(width2 + 1);
5241 if (textline == NULL)
5242 goto theend;
5243 width = width2;
5244 textline[width] = NUL;
5245 }
5246 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5247
5248 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5249 {
5250 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5251 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005252 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005253 for (i = 0; i < width; ++i)
5254 textline[i] = '-';
5255 }
5256 else
5257 {
5258 char_u *line1;
5259 char_u *line2;
5260 char_u *p1;
5261 char_u *p2;
5262 int col;
5263 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5264 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5265 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5266 ->sb_cells;
5267
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005268 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005269 line1 = vim_strsave(ml_get(lnum));
5270 if (line1 == NULL)
5271 break;
5272 p1 = line1;
5273
5274 line2 = ml_get(lnum + bot_lnum);
5275 p2 = line2;
5276 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5277 {
5278 int len1 = utfc_ptr2len(p1);
5279 int len2 = utfc_ptr2len(p2);
5280
5281 textline[col] = ' ';
5282 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005283 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005284 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005285 else if (lnum == cursor_pos1.row + 1
5286 && col == cursor_pos1.col
5287 && (cursor_pos1.row != cursor_pos2.row
5288 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005289 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005290 textline[col] = '>';
5291 else if (lnum == cursor_pos2.row + 1
5292 && col == cursor_pos2.col
5293 && (cursor_pos1.row != cursor_pos2.row
5294 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005295 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005296 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005297 else if (cellattr1 != NULL && cellattr2 != NULL)
5298 {
5299 if ((cellattr1 + col)->width
5300 != (cellattr2 + col)->width)
5301 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005302 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005303 &(cellattr2 + col)->fg))
5304 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005305 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005306 &(cellattr2 + col)->bg))
5307 textline[col] = 'b';
5308 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5309 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5310 textline[col] = 'a';
5311 }
5312 p1 += len1;
5313 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005314 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005315 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005316
5317 while (col < width)
5318 {
5319 if (*p1 == NUL && *p2 == NUL)
5320 textline[col] = '?';
5321 else if (*p1 == NUL)
5322 {
5323 textline[col] = '+';
5324 p2 += utfc_ptr2len(p2);
5325 }
5326 else
5327 {
5328 textline[col] = '-';
5329 p1 += utfc_ptr2len(p1);
5330 }
5331 ++col;
5332 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005333
5334 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005335 }
5336 if (add_empty_scrollback(term, &term->tl_default_color,
5337 term->tl_top_diff_rows) == OK)
5338 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5339 ++bot_lnum;
5340 }
5341
5342 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5343 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005344 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005345 for (i = 0; i < width; ++i)
5346 textline[i] = '+';
5347 if (add_empty_scrollback(term, &term->tl_default_color,
5348 term->tl_top_diff_rows) == OK)
5349 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5350 ++lnum;
5351 ++bot_lnum;
5352 }
5353
5354 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005355
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005356 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005357 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005358 }
5359
5360theend:
5361 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005362 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005363 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005364 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005365 fclose(fd2);
5366}
5367
5368/*
5369 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5370 * bottom files.
5371 * Return FAIL when this is not possible.
5372 */
5373 int
5374term_swap_diff()
5375{
5376 term_T *term = curbuf->b_term;
5377 linenr_T line_count;
5378 linenr_T top_rows;
5379 linenr_T bot_rows;
5380 linenr_T bot_start;
5381 linenr_T lnum;
5382 char_u *p;
5383 sb_line_T *sb_line;
5384
5385 if (term == NULL
5386 || !term_is_finished(curbuf)
5387 || term->tl_top_diff_rows == 0
5388 || term->tl_scrollback.ga_len == 0)
5389 return FAIL;
5390
5391 line_count = curbuf->b_ml.ml_line_count;
5392 top_rows = term->tl_top_diff_rows;
5393 bot_rows = term->tl_bot_diff_rows;
5394 bot_start = line_count - bot_rows;
5395 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5396
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005397 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005398 for (lnum = 1; lnum <= top_rows; ++lnum)
5399 {
5400 p = vim_strsave(ml_get(1));
5401 if (p == NULL)
5402 return OK;
5403 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005404 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005405 vim_free(p);
5406 }
5407
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005408 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005409 for (lnum = 1; lnum <= bot_rows; ++lnum)
5410 {
5411 p = vim_strsave(ml_get(bot_start + lnum));
5412 if (p == NULL)
5413 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005414 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005415 ml_append(lnum - 1, p, 0, FALSE);
5416 vim_free(p);
5417 }
5418
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005419 // move top title to bottom
5420 p = vim_strsave(ml_get(bot_rows + 1));
5421 if (p == NULL)
5422 return OK;
5423 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005424 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005425 vim_free(p);
5426
5427 // move bottom title to top
5428 p = vim_strsave(ml_get(line_count - top_rows));
5429 if (p == NULL)
5430 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005431 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005432 ml_append(bot_rows, p, 0, FALSE);
5433 vim_free(p);
5434
Bram Moolenaard96ff162018-02-18 22:13:29 +01005435 if (top_rows == bot_rows)
5436 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005437 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005438 for (lnum = 0; lnum < top_rows; ++lnum)
5439 {
5440 sb_line_T temp;
5441
5442 temp = *(sb_line + lnum);
5443 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5444 *(sb_line + bot_start + lnum) = temp;
5445 }
5446 }
5447 else
5448 {
5449 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005450 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005451
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005452 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005453 if (temp != NULL)
5454 {
5455 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5456 mch_memmove(term->tl_scrollback.ga_data,
5457 temp + bot_start,
5458 sizeof(sb_line_T) * bot_rows);
5459 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5460 temp + top_rows,
5461 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5462 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5463 + line_count - top_rows,
5464 temp,
5465 sizeof(sb_line_T) * top_rows);
5466 vim_free(temp);
5467 }
5468 }
5469
5470 term->tl_top_diff_rows = bot_rows;
5471 term->tl_bot_diff_rows = top_rows;
5472
5473 update_screen(NOT_VALID);
5474 return OK;
5475}
5476
5477/*
5478 * "term_dumpdiff(filename, filename, options)" function
5479 */
5480 void
5481f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5482{
5483 term_load_dump(argvars, rettv, TRUE);
5484}
5485
5486/*
5487 * "term_dumpload(filename, options)" function
5488 */
5489 void
5490f_term_dumpload(typval_T *argvars, typval_T *rettv)
5491{
5492 term_load_dump(argvars, rettv, FALSE);
5493}
5494
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005495/*
5496 * "term_getaltscreen(buf)" function
5497 */
5498 void
5499f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5500{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005501 buf_T *buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005502
5503 if (buf == NULL)
5504 return;
5505 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5506}
5507
5508/*
5509 * "term_getattr(attr, name)" function
5510 */
5511 void
5512f_term_getattr(typval_T *argvars, typval_T *rettv)
5513{
5514 int attr;
5515 size_t i;
5516 char_u *name;
5517
5518 static struct {
5519 char *name;
5520 int attr;
5521 } attrs[] = {
5522 {"bold", HL_BOLD},
5523 {"italic", HL_ITALIC},
5524 {"underline", HL_UNDERLINE},
5525 {"strike", HL_STRIKETHROUGH},
5526 {"reverse", HL_INVERSE},
5527 };
5528
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005529 attr = tv_get_number(&argvars[0]);
5530 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005531 if (name == NULL)
5532 return;
5533
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005534 if (attr > HL_ALL)
5535 attr = syn_attr2attr(attr);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005536 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
5537 if (STRCMP(name, attrs[i].name) == 0)
5538 {
5539 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5540 break;
5541 }
5542}
5543
5544/*
5545 * "term_getcursor(buf)" function
5546 */
5547 void
5548f_term_getcursor(typval_T *argvars, typval_T *rettv)
5549{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005550 buf_T *buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005551 term_T *term;
5552 list_T *l;
5553 dict_T *d;
5554
5555 if (rettv_list_alloc(rettv) == FAIL)
5556 return;
5557 if (buf == NULL)
5558 return;
5559 term = buf->b_term;
5560
5561 l = rettv->vval.v_list;
5562 list_append_number(l, term->tl_cursor_pos.row + 1);
5563 list_append_number(l, term->tl_cursor_pos.col + 1);
5564
5565 d = dict_alloc();
5566 if (d != NULL)
5567 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005568 dict_add_number(d, "visible", term->tl_cursor_visible);
5569 dict_add_number(d, "blink", blink_state_is_inverted()
5570 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5571 dict_add_number(d, "shape", term->tl_cursor_shape);
5572 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005573 list_append_dict(l, d);
5574 }
5575}
5576
5577/*
5578 * "term_getjob(buf)" function
5579 */
5580 void
5581f_term_getjob(typval_T *argvars, typval_T *rettv)
5582{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005583 buf_T *buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005584
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005585 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005586 {
5587 rettv->v_type = VAR_SPECIAL;
5588 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005589 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005590 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005591
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005592 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005593 rettv->vval.v_job = buf->b_term->tl_job;
5594 if (rettv->vval.v_job != NULL)
5595 ++rettv->vval.v_job->jv_refcount;
5596}
5597
5598 static int
5599get_row_number(typval_T *tv, term_T *term)
5600{
5601 if (tv->v_type == VAR_STRING
5602 && tv->vval.v_string != NULL
5603 && STRCMP(tv->vval.v_string, ".") == 0)
5604 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005605 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005606}
5607
5608/*
5609 * "term_getline(buf, row)" function
5610 */
5611 void
5612f_term_getline(typval_T *argvars, typval_T *rettv)
5613{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005614 buf_T *buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005615 term_T *term;
5616 int row;
5617
5618 rettv->v_type = VAR_STRING;
5619 if (buf == NULL)
5620 return;
5621 term = buf->b_term;
5622 row = get_row_number(&argvars[1], term);
5623
5624 if (term->tl_vterm == NULL)
5625 {
5626 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5627
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005628 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005629 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5630 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5631 }
5632 else
5633 {
5634 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5635 VTermRect rect;
5636 int len;
5637 char_u *p;
5638
5639 if (row < 0 || row >= term->tl_rows)
5640 return;
5641 len = term->tl_cols * MB_MAXBYTES + 1;
5642 p = alloc(len);
5643 if (p == NULL)
5644 return;
5645 rettv->vval.v_string = p;
5646
5647 rect.start_col = 0;
5648 rect.end_col = term->tl_cols;
5649 rect.start_row = row;
5650 rect.end_row = row + 1;
5651 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5652 }
5653}
5654
5655/*
5656 * "term_getscrolled(buf)" function
5657 */
5658 void
5659f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5660{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005661 buf_T *buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005662
5663 if (buf == NULL)
5664 return;
5665 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5666}
5667
5668/*
5669 * "term_getsize(buf)" function
5670 */
5671 void
5672f_term_getsize(typval_T *argvars, typval_T *rettv)
5673{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005674 buf_T *buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005675 list_T *l;
5676
5677 if (rettv_list_alloc(rettv) == FAIL)
5678 return;
5679 if (buf == NULL)
5680 return;
5681
5682 l = rettv->vval.v_list;
5683 list_append_number(l, buf->b_term->tl_rows);
5684 list_append_number(l, buf->b_term->tl_cols);
5685}
5686
5687/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005688 * "term_setsize(buf, rows, cols)" function
5689 */
5690 void
5691f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5692{
5693 buf_T *buf = term_get_buf(argvars, "term_setsize()");
5694 term_T *term;
5695 varnumber_T rows, cols;
5696
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005697 if (buf == NULL)
5698 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005699 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005700 return;
5701 }
5702 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005703 return;
5704 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005705 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005706 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005707 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005708 cols = cols <= 0 ? term->tl_cols : cols;
5709 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005710 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005711
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005712 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005713 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5714 term_report_winsize(term, term->tl_rows, term->tl_cols);
5715}
5716
5717/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005718 * "term_getstatus(buf)" function
5719 */
5720 void
5721f_term_getstatus(typval_T *argvars, typval_T *rettv)
5722{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005723 buf_T *buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005724 term_T *term;
5725 char_u val[100];
5726
5727 rettv->v_type = VAR_STRING;
5728 if (buf == NULL)
5729 return;
5730 term = buf->b_term;
5731
5732 if (term_job_running(term))
5733 STRCPY(val, "running");
5734 else
5735 STRCPY(val, "finished");
5736 if (term->tl_normal_mode)
5737 STRCAT(val, ",normal");
5738 rettv->vval.v_string = vim_strsave(val);
5739}
5740
5741/*
5742 * "term_gettitle(buf)" function
5743 */
5744 void
5745f_term_gettitle(typval_T *argvars, typval_T *rettv)
5746{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005747 buf_T *buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005748
5749 rettv->v_type = VAR_STRING;
5750 if (buf == NULL)
5751 return;
5752
5753 if (buf->b_term->tl_title != NULL)
5754 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5755}
5756
5757/*
5758 * "term_gettty(buf)" function
5759 */
5760 void
5761f_term_gettty(typval_T *argvars, typval_T *rettv)
5762{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005763 buf_T *buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005764 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005765 int num = 0;
5766
5767 rettv->v_type = VAR_STRING;
5768 if (buf == NULL)
5769 return;
5770 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005771 num = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005772
5773 switch (num)
5774 {
5775 case 0:
5776 if (buf->b_term->tl_job != NULL)
5777 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005778 break;
5779 case 1:
5780 if (buf->b_term->tl_job != NULL)
5781 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005782 break;
5783 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005784 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005785 return;
5786 }
5787 if (p != NULL)
5788 rettv->vval.v_string = vim_strsave(p);
5789}
5790
5791/*
5792 * "term_list()" function
5793 */
5794 void
5795f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
5796{
5797 term_T *tp;
5798 list_T *l;
5799
5800 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
5801 return;
5802
5803 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005804 FOR_ALL_TERMS(tp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005805 if (tp != NULL && tp->tl_buffer != NULL)
5806 if (list_append_number(l,
5807 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
5808 return;
5809}
5810
5811/*
5812 * "term_scrape(buf, row)" function
5813 */
5814 void
5815f_term_scrape(typval_T *argvars, typval_T *rettv)
5816{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005817 buf_T *buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005818 VTermScreen *screen = NULL;
5819 VTermPos pos;
5820 list_T *l;
5821 term_T *term;
5822 char_u *p;
5823 sb_line_T *line;
5824
5825 if (rettv_list_alloc(rettv) == FAIL)
5826 return;
5827 if (buf == NULL)
5828 return;
5829 term = buf->b_term;
5830
5831 l = rettv->vval.v_list;
5832 pos.row = get_row_number(&argvars[1], term);
5833
5834 if (term->tl_vterm != NULL)
5835 {
5836 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01005837 if (screen == NULL) // can't really happen
5838 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005839 p = NULL;
5840 line = NULL;
5841 }
5842 else
5843 {
5844 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
5845
5846 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
5847 return;
5848 p = ml_get_buf(buf, lnum + 1, FALSE);
5849 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
5850 }
5851
5852 for (pos.col = 0; pos.col < term->tl_cols; )
5853 {
5854 dict_T *dcell;
5855 int width;
5856 VTermScreenCellAttrs attrs;
5857 VTermColor fg, bg;
5858 char_u rgb[8];
5859 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
5860 int off = 0;
5861 int i;
5862
5863 if (screen == NULL)
5864 {
5865 cellattr_T *cellattr;
5866 int len;
5867
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005868 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005869 if (pos.col >= line->sb_cols)
5870 break;
5871 cellattr = line->sb_cells + pos.col;
5872 width = cellattr->width;
5873 attrs = cellattr->attrs;
5874 fg = cellattr->fg;
5875 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02005876 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005877 mch_memmove(mbs, p, len);
5878 mbs[len] = NUL;
5879 p += len;
5880 }
5881 else
5882 {
5883 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005884
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005885 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
5886 break;
5887 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
5888 {
5889 if (cell.chars[i] == 0)
5890 break;
5891 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
5892 }
5893 mbs[off] = NUL;
5894 width = cell.width;
5895 attrs = cell.attrs;
5896 fg = cell.fg;
5897 bg = cell.bg;
5898 }
5899 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01005900 if (dcell == NULL)
5901 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005902 list_append_dict(l, dcell);
5903
Bram Moolenaare0be1672018-07-08 16:50:37 +02005904 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005905
5906 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5907 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005908 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005909 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5910 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005911 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005912
Bram Moolenaar83d47902020-03-26 20:34:00 +01005913 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02005914 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005915
5916 ++pos.col;
5917 if (width == 2)
5918 ++pos.col;
5919 }
5920}
5921
5922/*
5923 * "term_sendkeys(buf, keys)" function
5924 */
5925 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01005926f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005927{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005928 buf_T *buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005929 char_u *msg;
5930 term_T *term;
5931
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005932 if (buf == NULL)
5933 return;
5934
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005935 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005936 if (msg == NULL)
5937 return;
5938 term = buf->b_term;
5939 if (term->tl_vterm == NULL)
5940 return;
5941
5942 while (*msg != NUL)
5943 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02005944 int c;
5945
5946 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
5947 {
5948 c = TO_SPECIAL(msg[1], msg[2]);
5949 msg += 3;
5950 }
5951 else
5952 {
5953 c = PTR2CHAR(msg);
5954 msg += MB_CPTR2LEN(msg);
5955 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01005956 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005957 }
5958}
5959
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005960#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
5961/*
5962 * "term_getansicolors(buf)" function
5963 */
5964 void
5965f_term_getansicolors(typval_T *argvars, typval_T *rettv)
5966{
5967 buf_T *buf = term_get_buf(argvars, "term_getansicolors()");
5968 term_T *term;
5969 VTermState *state;
5970 VTermColor color;
5971 char_u hexbuf[10];
5972 int index;
5973 list_T *list;
5974
5975 if (rettv_list_alloc(rettv) == FAIL)
5976 return;
5977
5978 if (buf == NULL)
5979 return;
5980 term = buf->b_term;
5981 if (term->tl_vterm == NULL)
5982 return;
5983
5984 list = rettv->vval.v_list;
5985 state = vterm_obtain_state(term->tl_vterm);
5986 for (index = 0; index < 16; index++)
5987 {
5988 vterm_state_get_palette_color(state, index, &color);
5989 sprintf((char *)hexbuf, "#%02x%02x%02x",
5990 color.red, color.green, color.blue);
5991 if (list_append_string(list, hexbuf, 7) == FAIL)
5992 return;
5993 }
5994}
5995
5996/*
5997 * "term_setansicolors(buf, list)" function
5998 */
5999 void
6000f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6001{
6002 buf_T *buf = term_get_buf(argvars, "term_setansicolors()");
6003 term_T *term;
6004
6005 if (buf == NULL)
6006 return;
6007 term = buf->b_term;
6008 if (term->tl_vterm == NULL)
6009 return;
6010
6011 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6012 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006013 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006014 return;
6015 }
6016
6017 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006018 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006019}
6020#endif
6021
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006022/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006023 * "term_setapi(buf, api)" function
6024 */
6025 void
6026f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6027{
6028 buf_T *buf = term_get_buf(argvars, "term_setapi()");
6029 term_T *term;
6030 char_u *api;
6031
6032 if (buf == NULL)
6033 return;
6034 term = buf->b_term;
6035 vim_free(term->tl_api);
6036 api = tv_get_string_chk(&argvars[1]);
6037 if (api != NULL)
6038 term->tl_api = vim_strsave(api);
6039 else
6040 term->tl_api = NULL;
6041}
6042
6043/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006044 * "term_setrestore(buf, command)" function
6045 */
6046 void
6047f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6048{
6049#if defined(FEAT_SESSION)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006050 buf_T *buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006051 term_T *term;
6052 char_u *cmd;
6053
6054 if (buf == NULL)
6055 return;
6056 term = buf->b_term;
6057 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006058 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006059 if (cmd != NULL)
6060 term->tl_command = vim_strsave(cmd);
6061 else
6062 term->tl_command = NULL;
6063#endif
6064}
6065
6066/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006067 * "term_setkill(buf, how)" function
6068 */
6069 void
6070f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6071{
6072 buf_T *buf = term_get_buf(argvars, "term_setkill()");
6073 term_T *term;
6074 char_u *how;
6075
6076 if (buf == NULL)
6077 return;
6078 term = buf->b_term;
6079 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006080 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006081 if (how != NULL)
6082 term->tl_kill = vim_strsave(how);
6083 else
6084 term->tl_kill = NULL;
6085}
6086
6087/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006088 * "term_start(command, options)" function
6089 */
6090 void
6091f_term_start(typval_T *argvars, typval_T *rettv)
6092{
6093 jobopt_T opt;
6094 buf_T *buf;
6095
6096 init_job_options(&opt);
6097 if (argvars[1].v_type != VAR_UNKNOWN
6098 && get_job_options(&argvars[1], &opt,
6099 JO_TIMEOUT_ALL + JO_STOPONEXIT
6100 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6101 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6102 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6103 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006104 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006105 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006106 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006107 return;
6108
Bram Moolenaar13568252018-03-16 20:46:58 +01006109 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006110
6111 if (buf != NULL && buf->b_term != NULL)
6112 rettv->vval.v_number = buf->b_fnum;
6113}
6114
6115/*
6116 * "term_wait" function
6117 */
6118 void
6119f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6120{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006121 buf_T *buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006122
6123 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006124 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006125 if (buf->b_term->tl_job == NULL)
6126 {
6127 ch_log(NULL, "term_wait(): no job to wait for");
6128 return;
6129 }
6130 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006131 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006132 return;
6133
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006134 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006135 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006136 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6137 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006138 // The job is dead, keep reading channel I/O until the channel is
6139 // closed. buf->b_term may become NULL if the terminal was closed while
6140 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006141 ch_log(NULL, "term_wait(): waiting for channel to close");
6142 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6143 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006144 term_flush_messages();
6145
Bram Moolenaard45aa552018-05-21 22:50:29 +02006146 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006147 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006148 // If the terminal is closed when the channel is closed the
6149 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006150 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006151 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006152
6153 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006154 }
6155 else
6156 {
6157 long wait = 10L;
6158
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006159 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006160
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006161 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006162 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006163 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006164 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006165
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006166 // Flushing messages on channels is hopefully sufficient.
6167 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006168 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006169 }
6170}
6171
6172/*
6173 * Called when a channel has sent all the lines to a terminal.
6174 * Send a CTRL-D to mark the end of the text.
6175 */
6176 void
6177term_send_eof(channel_T *ch)
6178{
6179 term_T *term;
6180
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006181 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006182 if (term->tl_job == ch->ch_job)
6183 {
6184 if (term->tl_eof_chars != NULL)
6185 {
6186 channel_send(ch, PART_IN, term->tl_eof_chars,
6187 (int)STRLEN(term->tl_eof_chars), NULL);
6188 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6189 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006190# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006191 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006192 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006193 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6194# endif
6195 }
6196}
6197
Bram Moolenaar113e1072019-01-20 15:30:40 +01006198#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006199 job_T *
6200term_getjob(term_T *term)
6201{
6202 return term != NULL ? term->tl_job : NULL;
6203}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006204#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006205
Bram Moolenaar4f974752019-02-17 17:44:42 +01006206# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006208///////////////////////////////////////
6209// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006210#ifdef PROTO
6211typedef int COORD;
6212typedef int DWORD;
6213typedef int HANDLE;
6214typedef int *DWORD_PTR;
6215typedef int HPCON;
6216typedef int HRESULT;
6217typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006218typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006219typedef int PSIZE_T;
6220typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006221typedef int BOOL;
6222# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006223#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006224
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006225HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6226HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6227HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006228BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6229BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6230void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006231
6232 static int
6233dyn_conpty_init(int verbose)
6234{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006235 static HMODULE hKerneldll = NULL;
6236 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006237 static struct
6238 {
6239 char *name;
6240 FARPROC *ptr;
6241 } conpty_entry[] =
6242 {
6243 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6244 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6245 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6246 {"InitializeProcThreadAttributeList",
6247 (FARPROC*)&pInitializeProcThreadAttributeList},
6248 {"UpdateProcThreadAttribute",
6249 (FARPROC*)&pUpdateProcThreadAttribute},
6250 {"DeleteProcThreadAttributeList",
6251 (FARPROC*)&pDeleteProcThreadAttributeList},
6252 {NULL, NULL}
6253 };
6254
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006255 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006256 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006257 if (verbose)
6258 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006259 return FAIL;
6260 }
6261
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006262 // No need to initialize twice.
6263 if (hKerneldll)
6264 return OK;
6265
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006266 hKerneldll = vimLoadLib("kernel32.dll");
6267 for (i = 0; conpty_entry[i].name != NULL
6268 && conpty_entry[i].ptr != NULL; ++i)
6269 {
6270 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6271 conpty_entry[i].name)) == NULL)
6272 {
6273 if (verbose)
6274 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006275 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006276 return FAIL;
6277 }
6278 }
6279
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006280 return OK;
6281}
6282
6283 static int
6284conpty_term_and_job_init(
6285 term_T *term,
6286 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006287 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006288 jobopt_T *opt,
6289 jobopt_T *orig_opt)
6290{
6291 WCHAR *cmd_wchar = NULL;
6292 WCHAR *cmd_wchar_copy = NULL;
6293 WCHAR *cwd_wchar = NULL;
6294 WCHAR *env_wchar = NULL;
6295 channel_T *channel = NULL;
6296 job_T *job = NULL;
6297 HANDLE jo = NULL;
6298 garray_T ga_cmd, ga_env;
6299 char_u *cmd = NULL;
6300 HRESULT hr;
6301 COORD consize;
6302 SIZE_T breq;
6303 PROCESS_INFORMATION proc_info;
6304 HANDLE i_theirs = NULL;
6305 HANDLE o_theirs = NULL;
6306 HANDLE i_ours = NULL;
6307 HANDLE o_ours = NULL;
6308
6309 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6310 ga_init2(&ga_env, (int)sizeof(char*), 20);
6311
6312 if (argvar->v_type == VAR_STRING)
6313 {
6314 cmd = argvar->vval.v_string;
6315 }
6316 else if (argvar->v_type == VAR_LIST)
6317 {
6318 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6319 goto failed;
6320 cmd = ga_cmd.ga_data;
6321 }
6322 if (cmd == NULL || *cmd == NUL)
6323 {
6324 emsg(_(e_invarg));
6325 goto failed;
6326 }
6327
6328 term->tl_arg0_cmd = vim_strsave(cmd);
6329
6330 cmd_wchar = enc_to_utf16(cmd, NULL);
6331
6332 if (cmd_wchar != NULL)
6333 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006334 // Request by CreateProcessW
6335 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006336 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006337 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6338 }
6339
6340 ga_clear(&ga_cmd);
6341 if (cmd_wchar == NULL)
6342 goto failed;
6343 if (opt->jo_cwd != NULL)
6344 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6345
6346 win32_build_env(opt->jo_env, &ga_env, TRUE);
6347 env_wchar = ga_env.ga_data;
6348
6349 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6350 goto failed;
6351 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6352 goto failed;
6353
6354 consize.X = term->tl_cols;
6355 consize.Y = term->tl_rows;
6356 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6357 &term->tl_conpty);
6358 if (FAILED(hr))
6359 goto failed;
6360
6361 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6362
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006363 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006364 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006365 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006366 if (!term->tl_siex.lpAttributeList)
6367 goto failed;
6368 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6369 0, &breq))
6370 goto failed;
6371 if (!pUpdateProcThreadAttribute(
6372 term->tl_siex.lpAttributeList, 0,
6373 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6374 sizeof(HPCON), NULL, NULL))
6375 goto failed;
6376
6377 channel = add_channel();
6378 if (channel == NULL)
6379 goto failed;
6380
6381 job = job_alloc();
6382 if (job == NULL)
6383 goto failed;
6384 if (argvar->v_type == VAR_STRING)
6385 {
6386 int argc;
6387
6388 build_argv_from_string(cmd, &job->jv_argv, &argc);
6389 }
6390 else
6391 {
6392 int argc;
6393
6394 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6395 }
6396
6397 if (opt->jo_set & JO_IN_BUF)
6398 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6399
6400 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6401 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006402 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006403 env_wchar, cwd_wchar,
6404 &term->tl_siex.StartupInfo, &proc_info))
6405 goto failed;
6406
6407 CloseHandle(i_theirs);
6408 CloseHandle(o_theirs);
6409
6410 channel_set_pipes(channel,
6411 (sock_T)i_ours,
6412 (sock_T)o_ours,
6413 (sock_T)o_ours);
6414
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006415 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006416 channel->ch_write_text_mode = TRUE;
6417
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006418 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006419 channel->ch_anonymous_pipe = TRUE;
6420
6421 jo = CreateJobObject(NULL, NULL);
6422 if (jo == NULL)
6423 goto failed;
6424
6425 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6426 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006427 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006428 CloseHandle(jo);
6429 jo = NULL;
6430 }
6431
6432 ResumeThread(proc_info.hThread);
6433 CloseHandle(proc_info.hThread);
6434
6435 vim_free(cmd_wchar);
6436 vim_free(cmd_wchar_copy);
6437 vim_free(cwd_wchar);
6438 vim_free(env_wchar);
6439
6440 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6441 goto failed;
6442
6443#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6444 if (opt->jo_set2 & JO2_ANSI_COLORS)
6445 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6446 else
6447 init_vterm_ansi_colors(term->tl_vterm);
6448#endif
6449
6450 channel_set_job(channel, job, opt);
6451 job_set_options(job, opt);
6452
6453 job->jv_channel = channel;
6454 job->jv_proc_info = proc_info;
6455 job->jv_job_object = jo;
6456 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006457 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006458 ++job->jv_refcount;
6459 term->tl_job = job;
6460
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006461 // Redirecting stdout and stderr doesn't work at the job level. Instead
6462 // open the file here and handle it in. opt->jo_io was changed in
6463 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006464 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6465 {
6466 char_u *fname = opt->jo_io_name[PART_OUT];
6467
6468 ch_log(channel, "Opening output file %s", fname);
6469 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6470 if (term->tl_out_fd == NULL)
6471 semsg(_(e_notopen), fname);
6472 }
6473
6474 return OK;
6475
6476failed:
6477 ga_clear(&ga_cmd);
6478 ga_clear(&ga_env);
6479 vim_free(cmd_wchar);
6480 vim_free(cmd_wchar_copy);
6481 vim_free(cwd_wchar);
6482 if (channel != NULL)
6483 channel_clear(channel);
6484 if (job != NULL)
6485 {
6486 job->jv_channel = NULL;
6487 job_cleanup(job);
6488 }
6489 term->tl_job = NULL;
6490 if (jo != NULL)
6491 CloseHandle(jo);
6492
6493 if (term->tl_siex.lpAttributeList != NULL)
6494 {
6495 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6496 vim_free(term->tl_siex.lpAttributeList);
6497 }
6498 term->tl_siex.lpAttributeList = NULL;
6499 if (o_theirs != NULL)
6500 CloseHandle(o_theirs);
6501 if (o_ours != NULL)
6502 CloseHandle(o_ours);
6503 if (i_ours != NULL)
6504 CloseHandle(i_ours);
6505 if (i_theirs != NULL)
6506 CloseHandle(i_theirs);
6507 if (term->tl_conpty != NULL)
6508 pClosePseudoConsole(term->tl_conpty);
6509 term->tl_conpty = NULL;
6510 return FAIL;
6511}
6512
6513 static void
6514conpty_term_report_winsize(term_T *term, int rows, int cols)
6515{
6516 COORD consize;
6517
6518 consize.X = cols;
6519 consize.Y = rows;
6520 pResizePseudoConsole(term->tl_conpty, consize);
6521}
6522
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006523 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006524term_free_conpty(term_T *term)
6525{
6526 if (term->tl_siex.lpAttributeList != NULL)
6527 {
6528 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6529 vim_free(term->tl_siex.lpAttributeList);
6530 }
6531 term->tl_siex.lpAttributeList = NULL;
6532 if (term->tl_conpty != NULL)
6533 pClosePseudoConsole(term->tl_conpty);
6534 term->tl_conpty = NULL;
6535}
6536
6537 int
6538use_conpty(void)
6539{
6540 return has_conpty;
6541}
6542
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006543# ifndef PROTO
6544
6545#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6546#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006547#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006548
6549void* (*winpty_config_new)(UINT64, void*);
6550void* (*winpty_open)(void*, void*);
6551void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6552BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6553void (*winpty_config_set_mouse_mode)(void*, int);
6554void (*winpty_config_set_initial_size)(void*, int, int);
6555LPCWSTR (*winpty_conin_name)(void*);
6556LPCWSTR (*winpty_conout_name)(void*);
6557LPCWSTR (*winpty_conerr_name)(void*);
6558void (*winpty_free)(void*);
6559void (*winpty_config_free)(void*);
6560void (*winpty_spawn_config_free)(void*);
6561void (*winpty_error_free)(void*);
6562LPCWSTR (*winpty_error_msg)(void*);
6563BOOL (*winpty_set_size)(void*, int, int, void*);
6564HANDLE (*winpty_agent_process)(void*);
6565
6566#define WINPTY_DLL "winpty.dll"
6567
6568static HINSTANCE hWinPtyDLL = NULL;
6569# endif
6570
6571 static int
6572dyn_winpty_init(int verbose)
6573{
6574 int i;
6575 static struct
6576 {
6577 char *name;
6578 FARPROC *ptr;
6579 } winpty_entry[] =
6580 {
6581 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6582 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6583 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6584 {"winpty_config_set_mouse_mode",
6585 (FARPROC*)&winpty_config_set_mouse_mode},
6586 {"winpty_config_set_initial_size",
6587 (FARPROC*)&winpty_config_set_initial_size},
6588 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6589 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6590 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6591 {"winpty_free", (FARPROC*)&winpty_free},
6592 {"winpty_open", (FARPROC*)&winpty_open},
6593 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6594 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6595 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6596 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6597 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6598 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6599 {NULL, NULL}
6600 };
6601
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006602 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006603 if (hWinPtyDLL)
6604 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006605 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6606 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006607 if (*p_winptydll != NUL)
6608 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6609 if (!hWinPtyDLL)
6610 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6611 if (!hWinPtyDLL)
6612 {
6613 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006614 semsg(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006615 : (char_u *)WINPTY_DLL);
6616 return FAIL;
6617 }
6618 for (i = 0; winpty_entry[i].name != NULL
6619 && winpty_entry[i].ptr != NULL; ++i)
6620 {
6621 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6622 winpty_entry[i].name)) == NULL)
6623 {
6624 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006625 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006626 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006627 return FAIL;
6628 }
6629 }
6630
6631 return OK;
6632}
6633
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006634 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006635winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006636 term_T *term,
6637 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006638 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006639 jobopt_T *opt,
6640 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006641{
6642 WCHAR *cmd_wchar = NULL;
6643 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006644 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006645 channel_T *channel = NULL;
6646 job_T *job = NULL;
6647 DWORD error;
6648 HANDLE jo = NULL;
6649 HANDLE child_process_handle;
6650 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006651 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006652 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006653 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006654 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006655
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006656 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6657 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006658
6659 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006660 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006661 cmd = argvar->vval.v_string;
6662 }
6663 else if (argvar->v_type == VAR_LIST)
6664 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006665 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006666 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006667 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006668 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006669 if (cmd == NULL || *cmd == NUL)
6670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006671 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006672 goto failed;
6673 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006674
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006675 term->tl_arg0_cmd = vim_strsave(cmd);
6676
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006677 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006678 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006679 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006680 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006681 if (opt->jo_cwd != NULL)
6682 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006683
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006684 win32_build_env(opt->jo_env, &ga_env, TRUE);
6685 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006686
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006687 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6688 if (term->tl_winpty_config == NULL)
6689 goto failed;
6690
6691 winpty_config_set_mouse_mode(term->tl_winpty_config,
6692 WINPTY_MOUSE_MODE_FORCE);
6693 winpty_config_set_initial_size(term->tl_winpty_config,
6694 term->tl_cols, term->tl_rows);
6695 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6696 if (term->tl_winpty == NULL)
6697 goto failed;
6698
6699 spawn_config = winpty_spawn_config_new(
6700 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6701 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6702 NULL,
6703 cmd_wchar,
6704 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006705 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006706 &winpty_err);
6707 if (spawn_config == NULL)
6708 goto failed;
6709
6710 channel = add_channel();
6711 if (channel == NULL)
6712 goto failed;
6713
6714 job = job_alloc();
6715 if (job == NULL)
6716 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006717 if (argvar->v_type == VAR_STRING)
6718 {
6719 int argc;
6720
6721 build_argv_from_string(cmd, &job->jv_argv, &argc);
6722 }
6723 else
6724 {
6725 int argc;
6726
6727 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6728 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006729
6730 if (opt->jo_set & JO_IN_BUF)
6731 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6732
6733 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
6734 &child_thread_handle, &error, &winpty_err))
6735 goto failed;
6736
6737 channel_set_pipes(channel,
6738 (sock_T)CreateFileW(
6739 winpty_conin_name(term->tl_winpty),
6740 GENERIC_WRITE, 0, NULL,
6741 OPEN_EXISTING, 0, NULL),
6742 (sock_T)CreateFileW(
6743 winpty_conout_name(term->tl_winpty),
6744 GENERIC_READ, 0, NULL,
6745 OPEN_EXISTING, 0, NULL),
6746 (sock_T)CreateFileW(
6747 winpty_conerr_name(term->tl_winpty),
6748 GENERIC_READ, 0, NULL,
6749 OPEN_EXISTING, 0, NULL));
6750
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006751 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006752 channel->ch_write_text_mode = TRUE;
6753
6754 jo = CreateJobObject(NULL, NULL);
6755 if (jo == NULL)
6756 goto failed;
6757
6758 if (!AssignProcessToJobObject(jo, child_process_handle))
6759 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006760 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006761 CloseHandle(jo);
6762 jo = NULL;
6763 }
6764
6765 winpty_spawn_config_free(spawn_config);
6766 vim_free(cmd_wchar);
6767 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006768 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006769
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006770 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6771 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006772
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006773#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6774 if (opt->jo_set2 & JO2_ANSI_COLORS)
6775 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6776 else
6777 init_vterm_ansi_colors(term->tl_vterm);
6778#endif
6779
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006780 channel_set_job(channel, job, opt);
6781 job_set_options(job, opt);
6782
6783 job->jv_channel = channel;
6784 job->jv_proc_info.hProcess = child_process_handle;
6785 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
6786 job->jv_job_object = jo;
6787 job->jv_status = JOB_STARTED;
6788 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006789 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006790 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006791 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006792 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006793 ++job->jv_refcount;
6794 term->tl_job = job;
6795
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006796 // Redirecting stdout and stderr doesn't work at the job level. Instead
6797 // open the file here and handle it in. opt->jo_io was changed in
6798 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006799 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6800 {
6801 char_u *fname = opt->jo_io_name[PART_OUT];
6802
6803 ch_log(channel, "Opening output file %s", fname);
6804 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6805 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006806 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006807 }
6808
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006809 return OK;
6810
6811failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006812 ga_clear(&ga_cmd);
6813 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006814 vim_free(cmd_wchar);
6815 vim_free(cwd_wchar);
6816 if (spawn_config != NULL)
6817 winpty_spawn_config_free(spawn_config);
6818 if (channel != NULL)
6819 channel_clear(channel);
6820 if (job != NULL)
6821 {
6822 job->jv_channel = NULL;
6823 job_cleanup(job);
6824 }
6825 term->tl_job = NULL;
6826 if (jo != NULL)
6827 CloseHandle(jo);
6828 if (term->tl_winpty != NULL)
6829 winpty_free(term->tl_winpty);
6830 term->tl_winpty = NULL;
6831 if (term->tl_winpty_config != NULL)
6832 winpty_config_free(term->tl_winpty_config);
6833 term->tl_winpty_config = NULL;
6834 if (winpty_err != NULL)
6835 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006836 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006837 (short_u *)winpty_error_msg(winpty_err), NULL);
6838
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006839 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006840 winpty_error_free(winpty_err);
6841 }
6842 return FAIL;
6843}
6844
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006845/*
6846 * Create a new terminal of "rows" by "cols" cells.
6847 * Store a reference in "term".
6848 * Return OK or FAIL.
6849 */
6850 static int
6851term_and_job_init(
6852 term_T *term,
6853 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01006854 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006855 jobopt_T *opt,
6856 jobopt_T *orig_opt)
6857{
6858 int use_winpty = FALSE;
6859 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006860 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006861
6862 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
6863 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
6864
6865 if (!has_winpty && !has_conpty)
6866 // If neither is available give the errors for winpty, since when
6867 // conpty is not available it can't be installed either.
6868 return dyn_winpty_init(TRUE);
6869
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006870 if (opt->jo_tty_type != NUL)
6871 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006872
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006873 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006874 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006875 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006876 use_conpty = TRUE;
6877 else if (has_winpty)
6878 use_winpty = TRUE;
6879 // else: error
6880 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006881 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006882 {
6883 if (has_winpty)
6884 use_winpty = TRUE;
6885 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006886 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006887 {
6888 if (has_conpty)
6889 use_conpty = TRUE;
6890 else
6891 return dyn_conpty_init(TRUE);
6892 }
6893
6894 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006895 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006896
6897 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006898 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006899
6900 // error
6901 return dyn_winpty_init(TRUE);
6902}
6903
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006904 static int
6905create_pty_only(term_T *term, jobopt_T *options)
6906{
6907 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
6908 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
6909 char in_name[80], out_name[80];
6910 channel_T *channel = NULL;
6911
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006912 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6913 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006914
6915 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
6916 GetCurrentProcessId(),
6917 curbuf->b_fnum);
6918 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
6919 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6920 PIPE_UNLIMITED_INSTANCES,
6921 0, 0, NMPWAIT_NOWAIT, NULL);
6922 if (hPipeIn == INVALID_HANDLE_VALUE)
6923 goto failed;
6924
6925 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
6926 GetCurrentProcessId(),
6927 curbuf->b_fnum);
6928 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
6929 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6930 PIPE_UNLIMITED_INSTANCES,
6931 0, 0, 0, NULL);
6932 if (hPipeOut == INVALID_HANDLE_VALUE)
6933 goto failed;
6934
6935 ConnectNamedPipe(hPipeIn, NULL);
6936 ConnectNamedPipe(hPipeOut, NULL);
6937
6938 term->tl_job = job_alloc();
6939 if (term->tl_job == NULL)
6940 goto failed;
6941 ++term->tl_job->jv_refcount;
6942
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006943 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006944 term->tl_job->jv_status = JOB_FINISHED;
6945
6946 channel = add_channel();
6947 if (channel == NULL)
6948 goto failed;
6949 term->tl_job->jv_channel = channel;
6950 channel->ch_keep_open = TRUE;
6951 channel->ch_named_pipe = TRUE;
6952
6953 channel_set_pipes(channel,
6954 (sock_T)hPipeIn,
6955 (sock_T)hPipeOut,
6956 (sock_T)hPipeOut);
6957 channel_set_job(channel, term->tl_job, options);
6958 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
6959 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
6960
6961 return OK;
6962
6963failed:
6964 if (hPipeIn != NULL)
6965 CloseHandle(hPipeIn);
6966 if (hPipeOut != NULL)
6967 CloseHandle(hPipeOut);
6968 return FAIL;
6969}
6970
6971/*
6972 * Free the terminal emulator part of "term".
6973 */
6974 static void
6975term_free_vterm(term_T *term)
6976{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006977 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006978 if (term->tl_winpty != NULL)
6979 winpty_free(term->tl_winpty);
6980 term->tl_winpty = NULL;
6981 if (term->tl_winpty_config != NULL)
6982 winpty_config_free(term->tl_winpty_config);
6983 term->tl_winpty_config = NULL;
6984 if (term->tl_vterm != NULL)
6985 vterm_free(term->tl_vterm);
6986 term->tl_vterm = NULL;
6987}
6988
6989/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02006990 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006991 */
6992 static void
6993term_report_winsize(term_T *term, int rows, int cols)
6994{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006995 if (term->tl_conpty)
6996 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006997 if (term->tl_winpty)
6998 winpty_set_size(term->tl_winpty, cols, rows, NULL);
6999}
7000
7001 int
7002terminal_enabled(void)
7003{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007004 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007005}
7006
7007# else
7008
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007009///////////////////////////////////////
7010// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007011
7012/*
7013 * Create a new terminal of "rows" by "cols" cells.
7014 * Start job for "cmd".
7015 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007016 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007017 * Return OK or FAIL.
7018 */
7019 static int
7020term_and_job_init(
7021 term_T *term,
7022 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007023 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007024 jobopt_T *opt,
7025 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007026{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007027 term->tl_arg0_cmd = NULL;
7028
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007029 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7030 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007031
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007032#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7033 if (opt->jo_set2 & JO2_ANSI_COLORS)
7034 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7035 else
7036 init_vterm_ansi_colors(term->tl_vterm);
7037#endif
7038
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007039 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007040 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007041 if (term->tl_job != NULL)
7042 ++term->tl_job->jv_refcount;
7043
7044 return term->tl_job != NULL
7045 && term->tl_job->jv_channel != NULL
7046 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7047}
7048
7049 static int
7050create_pty_only(term_T *term, jobopt_T *opt)
7051{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007052 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7053 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007054
7055 term->tl_job = job_alloc();
7056 if (term->tl_job == NULL)
7057 return FAIL;
7058 ++term->tl_job->jv_refcount;
7059
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007060 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007061 term->tl_job->jv_status = JOB_FINISHED;
7062
7063 return mch_create_pty_channel(term->tl_job, opt);
7064}
7065
7066/*
7067 * Free the terminal emulator part of "term".
7068 */
7069 static void
7070term_free_vterm(term_T *term)
7071{
7072 if (term->tl_vterm != NULL)
7073 vterm_free(term->tl_vterm);
7074 term->tl_vterm = NULL;
7075}
7076
7077/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007078 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007079 */
7080 static void
7081term_report_winsize(term_T *term, int rows, int cols)
7082{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007083 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007084 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7085 {
7086 int fd = -1;
7087 int part;
7088
7089 for (part = PART_OUT; part < PART_COUNT; ++part)
7090 {
7091 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007092 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007093 break;
7094 }
7095 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7096 mch_signal_job(term->tl_job, (char_u *)"winch");
7097 }
7098}
7099
7100# endif
7101
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007102#endif // FEAT_TERMINAL