blob: 7856ad586ca90f4571bdbb2411f1a861460c363b [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
318 if (term->tl_rows != curwin->w_height)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200319 win_setheight_win(term->tl_rows, curwin);
Bram Moolenaarb936b792020-09-04 18:34:09 +0200320 if (term->tl_cols != curwin->w_width)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200321 win_setwidth_win(term->tl_cols, curwin);
Bram Moolenaarb936b792020-09-04 18:34:09 +0200322
323 // Set 'winsize' now to avoid a resize at the next redraw.
324 if (!minsize && *curwin->w_p_tws != NUL)
325 {
326 char_u buf[100];
327
328 vim_snprintf((char *)buf, 100, "%dx%d", term->tl_rows, term->tl_cols);
329 set_option_value((char_u *)"termwinsize", 0L, buf, OPT_LOCAL);
330 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200331}
332
333/*
334 * Initialize job options for a terminal job.
335 * Caller may overrule some of them.
336 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100337 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200338init_job_options(jobopt_T *opt)
339{
340 clear_job_options(opt);
341
342 opt->jo_mode = MODE_RAW;
343 opt->jo_out_mode = MODE_RAW;
344 opt->jo_err_mode = MODE_RAW;
345 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
346}
347
348/*
349 * Set job options mandatory for a terminal job.
350 */
351 static void
352setup_job_options(jobopt_T *opt, int rows, int cols)
353{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100354#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100355 // Win32: Redirecting the job output won't work, thus always connect stdout
356 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200357 if (!(opt->jo_set & JO_OUT_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200358#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200359 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100360 // Connect stdout to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200361 opt->jo_io[PART_OUT] = JIO_BUFFER;
362 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
363 opt->jo_modifiable[PART_OUT] = 0;
364 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
365 }
366
Bram Moolenaar4f974752019-02-17 17:44:42 +0100367#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100368 // Win32: Redirecting the job output won't work, thus always connect stderr
369 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200370 if (!(opt->jo_set & JO_ERR_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200371#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200372 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100373 // Connect stderr to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200374 opt->jo_io[PART_ERR] = JIO_BUFFER;
375 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
376 opt->jo_modifiable[PART_ERR] = 0;
377 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
378 }
379
380 opt->jo_pty = TRUE;
381 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
382 opt->jo_term_rows = rows;
383 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
384 opt->jo_term_cols = cols;
385}
386
387/*
Bram Moolenaar5c381eb2019-06-25 06:50:31 +0200388 * Flush messages on channels.
389 */
390 static void
391term_flush_messages()
392{
393 mch_check_messages();
394 parse_queued_messages();
395}
396
397/*
Bram Moolenaard96ff162018-02-18 22:13:29 +0100398 * Close a terminal buffer (and its window). Used when creating the terminal
399 * fails.
400 */
401 static void
402term_close_buffer(buf_T *buf, buf_T *old_curbuf)
403{
404 free_terminal(buf);
405 if (old_curbuf != NULL)
406 {
407 --curbuf->b_nwindows;
408 curbuf = old_curbuf;
409 curwin->w_buffer = curbuf;
410 ++curbuf->b_nwindows;
411 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100412 CHECK_CURBUF;
Bram Moolenaard96ff162018-02-18 22:13:29 +0100413
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100414 // Wiping out the buffer will also close the window and call
415 // free_terminal().
Bram Moolenaard96ff162018-02-18 22:13:29 +0100416 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
417}
418
419/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200420 * Start a terminal window and return its buffer.
Bram Moolenaar13568252018-03-16 20:46:58 +0100421 * Use either "argvar" or "argv", the other must be NULL.
422 * When "flags" has TERM_START_NOJOB only create the buffer, b_term and open
423 * the window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200424 * Returns NULL when failed.
425 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100426 buf_T *
427term_start(
428 typval_T *argvar,
429 char **argv,
430 jobopt_T *opt,
431 int flags)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200432{
433 exarg_T split_ea;
434 win_T *old_curwin = curwin;
435 term_T *term;
436 buf_T *old_curbuf = NULL;
437 int res;
438 buf_T *newbuf;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100439 int vertical = opt->jo_vertical || (cmdmod.split & WSP_VERT);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200440 jobopt_T orig_opt; // only partly filled
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200441
442 if (check_restricted() || check_secure())
443 return NULL;
444
445 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
446 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
447 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
Bram Moolenaarb0992022020-01-30 14:55:42 +0100448 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))
449 || (argvar != NULL
450 && argvar->v_type == VAR_LIST
451 && argvar->vval.v_list != NULL
452 && argvar->vval.v_list->lv_first == &range_list_item))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200453 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100454 emsg(_(e_invarg));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200455 return NULL;
456 }
457
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200458 term = ALLOC_CLEAR_ONE(term_T);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200459 if (term == NULL)
460 return NULL;
461 term->tl_dirty_row_end = MAX_ROW;
462 term->tl_cursor_visible = TRUE;
463 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
464 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100465#ifdef FEAT_GUI
466 term->tl_system = (flags & TERM_START_SYSTEM);
467#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200468 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100469 ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200470 ga_init2(&term->tl_osc_buf, sizeof(char), 300);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200471
Bram Moolenaara80faa82020-04-12 19:37:17 +0200472 CLEAR_FIELD(split_ea);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200473 if (opt->jo_curwin)
474 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100475 // Create a new buffer in the current window.
Bram Moolenaar13568252018-03-16 20:46:58 +0100476 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200477 {
478 no_write_message();
479 vim_free(term);
480 return NULL;
481 }
482 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaarb1009092020-05-31 16:04:42 +0200483 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
484 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
485 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200486 {
487 vim_free(term);
488 return NULL;
489 }
490 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100491 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200492 {
493 buf_T *buf;
494
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100495 // Create a new buffer without a window. Make it the current buffer for
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100496 // a moment to be able to do the initializations.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200497 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
498 BLN_NEW | BLN_LISTED);
499 if (buf == NULL || ml_open(buf) == FAIL)
500 {
501 vim_free(term);
502 return NULL;
503 }
504 old_curbuf = curbuf;
505 --curbuf->b_nwindows;
506 curbuf = buf;
507 curwin->w_buffer = buf;
508 ++curbuf->b_nwindows;
509 }
510 else
511 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100512 // Open a new window or tab.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200513 split_ea.cmdidx = CMD_new;
514 split_ea.cmd = (char_u *)"new";
515 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100516 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200517 {
518 split_ea.line2 = opt->jo_term_rows;
519 split_ea.addr_count = 1;
520 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100521 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200522 {
523 split_ea.line2 = opt->jo_term_cols;
524 split_ea.addr_count = 1;
525 }
526
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100527 if (vertical)
528 cmdmod.split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200529 ex_splitview(&split_ea);
530 if (curwin == old_curwin)
531 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100532 // split failed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200533 vim_free(term);
534 return NULL;
535 }
536 }
537 term->tl_buffer = curbuf;
538 curbuf->b_term = term;
539
540 if (!opt->jo_hidden)
541 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100542 // Only one size was taken care of with :new, do the other one. With
543 // "curwin" both need to be done.
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100544 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200545 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100546 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200547 win_setwidth(opt->jo_term_cols);
548 }
549
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100550 // Link the new terminal in the list of active terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200551 term->tl_next = first_term;
552 first_term = term;
553
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100554 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
555
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200556 if (opt->jo_term_name != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100557 {
558 vim_free(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200559 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100560 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100561 else if (argv != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100562 {
563 vim_free(curbuf->b_ffname);
Bram Moolenaar13568252018-03-16 20:46:58 +0100564 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100565 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200566 else
567 {
568 int i;
569 size_t len;
570 char_u *cmd, *p;
571
572 if (argvar->v_type == VAR_STRING)
573 {
574 cmd = argvar->vval.v_string;
575 if (cmd == NULL)
576 cmd = (char_u *)"";
577 else if (STRCMP(cmd, "NONE") == 0)
578 cmd = (char_u *)"pty";
579 }
580 else if (argvar->v_type != VAR_LIST
581 || argvar->vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +0100582 || argvar->vval.v_list->lv_len == 0
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100583 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200584 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
585 cmd = (char_u*)"";
586
587 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200588 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200589
590 for (i = 0; p != NULL; ++i)
591 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100592 // Prepend a ! to the command name to avoid the buffer name equals
593 // the executable, otherwise ":w!" would overwrite it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200594 if (i == 0)
595 vim_snprintf((char *)p, len, "!%s", cmd);
596 else
597 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
598 if (buflist_findname(p) == NULL)
599 {
600 vim_free(curbuf->b_ffname);
601 curbuf->b_ffname = p;
602 break;
603 }
604 }
605 }
Bram Moolenaare010c722020-02-24 21:37:54 +0100606 vim_free(curbuf->b_sfname);
607 curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200608 curbuf->b_fname = curbuf->b_ffname;
609
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100610 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
611
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200612 if (opt->jo_term_opencmd != NULL)
613 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
614
615 if (opt->jo_eof_chars != NULL)
616 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
617
618 set_string_option_direct((char_u *)"buftype", -1,
619 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200620 // Avoid that 'buftype' is reset when this buffer is entered.
621 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200622
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100623 // Mark the buffer as not modifiable. It can only be made modifiable after
624 // the job finished.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200625 curbuf->b_p_ma = FALSE;
626
Bram Moolenaarb936b792020-09-04 18:34:09 +0200627 set_term_and_win_size(term, opt);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100628#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200629 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
630#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200631 setup_job_options(opt, term->tl_rows, term->tl_cols);
632
Bram Moolenaar13568252018-03-16 20:46:58 +0100633 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100634 return curbuf;
635
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100636#if defined(FEAT_SESSION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100637 // Remember the command for the session file.
Bram Moolenaar13568252018-03-16 20:46:58 +0100638 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100639 term->tl_command = vim_strsave((char_u *)"NONE");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100640 else if (argvar->v_type == VAR_STRING)
641 {
642 char_u *cmd = argvar->vval.v_string;
643
644 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
645 term->tl_command = vim_strsave(cmd);
646 }
647 else if (argvar->v_type == VAR_LIST
648 && argvar->vval.v_list != NULL
649 && argvar->vval.v_list->lv_len > 0)
650 {
651 garray_T ga;
652 listitem_T *item;
653
654 ga_init2(&ga, 1, 100);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200655 FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100656 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100657 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100658 char_u *p;
659
660 if (s == NULL)
661 break;
662 p = vim_strsave_fnameescape(s, FALSE);
663 if (p == NULL)
664 break;
665 ga_concat(&ga, p);
666 vim_free(p);
667 ga_append(&ga, ' ');
668 }
669 if (item == NULL)
670 {
671 ga_append(&ga, NUL);
672 term->tl_command = ga.ga_data;
673 }
674 else
675 ga_clear(&ga);
676 }
677#endif
678
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100679 if (opt->jo_term_kill != NULL)
680 {
681 char_u *p = skiptowhite(opt->jo_term_kill);
682
683 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
684 }
685
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200686 if (opt->jo_term_api != NULL)
Bram Moolenaar21109272020-01-30 16:27:20 +0100687 {
688 char_u *p = skiptowhite(opt->jo_term_api);
689
690 term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
691 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200692 else
693 term->tl_api = vim_strsave((char_u *)"Tapi_");
694
Bram Moolenaar83d47902020-03-26 20:34:00 +0100695 if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
696 term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
697
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100698 // System dependent: setup the vterm and maybe start the job in it.
Bram Moolenaar13568252018-03-16 20:46:58 +0100699 if (argv == NULL
700 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200701 && argvar->vval.v_string != NULL
702 && STRCMP(argvar->vval.v_string, "NONE") == 0)
703 res = create_pty_only(term, opt);
704 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200705 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200706
707 newbuf = curbuf;
708 if (res == OK)
709 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100710 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200711 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
712 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100713#ifdef FEAT_GUI
714 if (term->tl_system)
715 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100716 // display first line below typed command
Bram Moolenaar13568252018-03-16 20:46:58 +0100717 term->tl_toprow = msg_row + 1;
718 term->tl_dirty_row_end = 0;
719 }
720#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200721
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100722 // Make sure we don't get stuck on sending keys to the job, it leads to
723 // a deadlock if the job is waiting for Vim to read.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200724 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
725
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200726 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200727 {
728 --curbuf->b_nwindows;
729 curbuf = old_curbuf;
730 curwin->w_buffer = curbuf;
731 ++curbuf->b_nwindows;
732 }
733 }
734 else
735 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100736 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200737 return NULL;
738 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100739
Bram Moolenaar13568252018-03-16 20:46:58 +0100740 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar28ed4df2019-10-26 16:21:40 +0200741 if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
742 apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200743 return newbuf;
744}
745
746/*
747 * ":terminal": open a terminal window and execute a job in it.
748 */
749 void
750ex_terminal(exarg_T *eap)
751{
752 typval_T argvar[2];
753 jobopt_T opt;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100754 int opt_shell = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200755 char_u *cmd;
756 char_u *tofree = NULL;
757
758 init_job_options(&opt);
759
760 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100761 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200762 {
763 char_u *p, *ep;
764
765 cmd += 2;
766 p = skiptowhite(cmd);
767 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200768 if (ep != NULL)
769 {
770 if (ep < p)
771 p = ep;
772 else
773 ep = NULL;
774 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200775
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200776# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
777 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
778 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200779 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200780 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100781 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200782 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200783 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200784 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200785 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200786 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200787 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200788 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100789 opt.jo_term_norestore = 1;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100790 else if (OPTARG_HAS("shell"))
791 opt_shell = TRUE;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200792 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100793 {
794 opt.jo_set2 |= JO2_TERM_KILL;
795 opt.jo_term_kill = ep + 1;
796 p = skiptowhite(cmd);
797 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200798 else if (OPTARG_HAS("api"))
799 {
800 opt.jo_set2 |= JO2_TERM_API;
801 if (ep != NULL)
802 {
803 opt.jo_term_api = ep + 1;
804 p = skiptowhite(cmd);
805 }
806 else
807 opt.jo_term_api = NULL;
808 }
809 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200810 {
811 opt.jo_set2 |= JO2_TERM_ROWS;
812 opt.jo_term_rows = atoi((char *)ep + 1);
813 p = skiptowhite(cmd);
814 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200815 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200816 {
817 opt.jo_set2 |= JO2_TERM_COLS;
818 opt.jo_term_cols = atoi((char *)ep + 1);
819 p = skiptowhite(cmd);
820 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200821 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200822 {
823 char_u *buf = NULL;
824 char_u *keys;
825
Bram Moolenaar21109272020-01-30 16:27:20 +0100826 vim_free(opt.jo_eof_chars);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200827 p = skiptowhite(cmd);
828 *p = NUL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200829 keys = replace_termcodes(ep + 1, &buf,
830 REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200831 opt.jo_set2 |= JO2_EOF_CHARS;
832 opt.jo_eof_chars = vim_strsave(keys);
833 vim_free(buf);
834 *p = ' ';
835 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100836#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100837 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
838 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100839 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100840 int tty_type = NUL;
841
842 p = skiptowhite(cmd);
843 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
844 tty_type = 'w';
845 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
846 tty_type = 'c';
847 else
848 {
849 semsg(e_invargval, "type");
850 goto theend;
851 }
852 opt.jo_set2 |= JO2_TTY_TYPE;
853 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100854 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100855#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200856 else
857 {
858 if (*p)
859 *p = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100860 semsg(_("E181: Invalid attribute: %s"), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100861 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200862 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200863# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200864 cmd = skipwhite(p);
865 }
866 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100867 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100868 // Make a copy of 'shell', an autocommand may change the option.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200869 tofree = cmd = vim_strsave(p_sh);
870
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100871 // default to close when the shell exits
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100872 if (opt.jo_term_finish == NUL)
Bram Moolenaare2978022020-04-26 14:47:44 +0200873 opt.jo_term_finish = TL_FINISH_CLOSE;
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100874 }
875
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200876 if (eap->addr_count > 0)
877 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100878 // Write lines from current buffer to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200879 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
880 opt.jo_io[PART_IN] = JIO_BUFFER;
881 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
882 opt.jo_in_top = eap->line1;
883 opt.jo_in_bot = eap->line2;
884 }
885
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100886 if (opt_shell && tofree == NULL)
887 {
888#ifdef UNIX
889 char **argv = NULL;
890 char_u *tofree1 = NULL;
891 char_u *tofree2 = NULL;
892
893 // :term ++shell command
894 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
895 term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaaradf4aa22019-11-10 22:36:44 +0100896 vim_free(argv);
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100897 vim_free(tofree1);
898 vim_free(tofree2);
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100899 goto theend;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100900#else
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100901# ifdef MSWIN
902 long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
903 char_u *newcmd;
904
905 newcmd = alloc(cmdlen);
906 if (newcmd == NULL)
907 goto theend;
908 tofree = newcmd;
909 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
910 cmd = newcmd;
911# else
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100912 emsg(_("E279: Sorry, ++shell is not supported on this system"));
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100913 goto theend;
914# endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100915#endif
916 }
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100917 argvar[0].v_type = VAR_STRING;
918 argvar[0].vval.v_string = cmd;
919 argvar[1].v_type = VAR_UNKNOWN;
920 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100921
922theend:
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100923 vim_free(tofree);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200924 vim_free(opt.jo_eof_chars);
925}
926
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100927#if defined(FEAT_SESSION) || defined(PROTO)
928/*
929 * Write a :terminal command to the session file to restore the terminal in
930 * window "wp".
931 * Return FAIL if writing fails.
932 */
933 int
934term_write_session(FILE *fd, win_T *wp)
935{
936 term_T *term = wp->w_buffer->b_term;
937
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100938 // Create the terminal and run the command. This is not without
939 // risk, but let's assume the user only creates a session when this
940 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100941 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
942 term->tl_cols, term->tl_rows) < 0)
943 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100944#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100945 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
946 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100947#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100948 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
949 return FAIL;
950
951 return put_eol(fd);
952}
953
954/*
955 * Return TRUE if "buf" has a terminal that should be restored.
956 */
957 int
958term_should_restore(buf_T *buf)
959{
960 term_T *term = buf->b_term;
961
962 return term != NULL && (term->tl_command == NULL
963 || STRCMP(term->tl_command, "NONE") != 0);
964}
965#endif
966
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200967/*
968 * Free the scrollback buffer for "term".
969 */
970 static void
971free_scrollback(term_T *term)
972{
973 int i;
974
975 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
976 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
977 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100978 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
979 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
980 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200981}
982
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100983
984// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +0200985static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100986
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200987/*
988 * Free a terminal and everything it refers to.
989 * Kills the job if there is one.
990 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100991 * The actual terminal structure is freed later in free_unused_terminals(),
992 * because callbacks may wipe out a buffer while the terminal is still
993 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200994 */
995 void
996free_terminal(buf_T *buf)
997{
998 term_T *term = buf->b_term;
999 term_T *tp;
1000
1001 if (term == NULL)
1002 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001003
1004 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001005 if (first_term == term)
1006 first_term = term->tl_next;
1007 else
1008 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
1009 if (tp->tl_next == term)
1010 {
1011 tp->tl_next = term->tl_next;
1012 break;
1013 }
1014
1015 if (term->tl_job != NULL)
1016 {
1017 if (term->tl_job->jv_status != JOB_ENDED
1018 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +01001019 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001020 job_stop(term->tl_job, NULL, "kill");
1021 job_unref(term->tl_job);
1022 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001023 term->tl_next = terminals_to_free;
1024 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001025
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001026 buf->b_term = NULL;
1027 if (in_terminal_loop == term)
1028 in_terminal_loop = NULL;
1029}
1030
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001031 void
1032free_unused_terminals()
1033{
1034 while (terminals_to_free != NULL)
1035 {
1036 term_T *term = terminals_to_free;
1037
1038 terminals_to_free = term->tl_next;
1039
1040 free_scrollback(term);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02001041 ga_clear(&term->tl_osc_buf);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001042
1043 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001044 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001045 vim_free(term->tl_title);
1046#ifdef FEAT_SESSION
1047 vim_free(term->tl_command);
1048#endif
1049 vim_free(term->tl_kill);
1050 vim_free(term->tl_status_text);
1051 vim_free(term->tl_opencmd);
1052 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001053 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001054#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001055 if (term->tl_out_fd != NULL)
1056 fclose(term->tl_out_fd);
1057#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001058 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001059 vim_free(term->tl_cursor_color);
1060 vim_free(term);
1061 }
1062}
1063
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001064/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001065 * Get the part that is connected to the tty. Normally this is PART_IN, but
1066 * when writing buffer lines to the job it can be another. This makes it
1067 * possible to do "1,5term vim -".
1068 */
1069 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001070get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001071{
1072#ifdef UNIX
1073 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1074 int i;
1075
1076 for (i = 0; i < 3; ++i)
1077 {
1078 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1079
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001080 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001081 return parts[i];
1082 }
1083#endif
1084 return PART_IN;
1085}
1086
1087/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001088 * Write job output "msg[len]" to the vterm.
1089 */
1090 static void
1091term_write_job_output(term_T *term, char_u *msg, size_t len)
1092{
1093 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001094 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001095
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001096 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001097
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001098 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001099 if (prevlen != vterm_output_get_buffer_current(vterm))
1100 {
1101 char buf[KEY_BUF_LEN];
1102 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1103
1104 if (curlen > 0)
1105 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1106 (char_u *)buf, (int)curlen, NULL);
1107 }
1108
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001109 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001110 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1111}
1112
1113 static void
1114update_cursor(term_T *term, int redraw)
1115{
1116 if (term->tl_normal_mode)
1117 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001118#ifdef FEAT_GUI
1119 if (term->tl_system)
1120 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1121 term->tl_cursor_pos.col);
1122 else
1123#endif
1124 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001125 if (redraw)
1126 {
1127 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1128 cursor_on();
1129 out_flush();
1130#ifdef FEAT_GUI
1131 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001132 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001133 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001134 gui_mch_flush();
1135 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001136#endif
1137 }
1138}
1139
1140/*
1141 * Invoked when "msg" output from a job was received. Write it to the terminal
1142 * of "buffer".
1143 */
1144 void
1145write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1146{
1147 size_t len = STRLEN(msg);
1148 term_T *term = buffer->b_term;
1149
Bram Moolenaar4f974752019-02-17 17:44:42 +01001150#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001151 // Win32: Cannot redirect output of the job, intercept it here and write to
1152 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001153 if (term->tl_out_fd != NULL)
1154 {
1155 ch_log(channel, "Writing %d bytes to output file", (int)len);
1156 fwrite(msg, len, 1, term->tl_out_fd);
1157 return;
1158 }
1159#endif
1160
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001161 if (term->tl_vterm == NULL)
1162 {
1163 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1164 return;
1165 }
1166 ch_log(channel, "writing %d bytes to terminal", (int)len);
1167 term_write_job_output(term, msg, len);
1168
Bram Moolenaar13568252018-03-16 20:46:58 +01001169#ifdef FEAT_GUI
1170 if (term->tl_system)
1171 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001172 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001173 update_system_term(term);
1174 update_cursor(term, TRUE);
1175 }
1176 else
1177#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001178 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1179 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001180 if (!term->tl_normal_mode)
1181 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001182 // Don't use update_screen() when editing the command line, it gets
1183 // cleared.
1184 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001185 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001186 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001187 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001188 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001189 // update_screen() can be slow, check the terminal wasn't closed
1190 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001191 if (buffer == curbuf && curbuf->b_term != NULL)
1192 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001193 }
1194 else
1195 redraw_after_callback(TRUE);
1196 }
1197}
1198
1199/*
1200 * Send a mouse position and click to the vterm
1201 */
1202 static int
1203term_send_mouse(VTerm *vterm, int button, int pressed)
1204{
1205 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001206 int row = mouse_row - W_WINROW(curwin);
1207 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001208
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001209#ifdef FEAT_PROP_POPUP
1210 if (popup_is_popup(curwin))
1211 {
1212 row -= popup_top_extra(curwin);
1213 col -= popup_left_extra(curwin);
1214 }
1215#endif
1216 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001217 if (button != 0)
1218 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001219 return TRUE;
1220}
1221
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001222static int enter_mouse_col = -1;
1223static int enter_mouse_row = -1;
1224
1225/*
1226 * Handle a mouse click, drag or release.
1227 * Return TRUE when a mouse event is sent to the terminal.
1228 */
1229 static int
1230term_mouse_click(VTerm *vterm, int key)
1231{
1232#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001233 // For modeless selection mouse drag and release events are ignored, unless
1234 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001235 static int ignore_drag_release = TRUE;
1236 VTermMouseState mouse_state;
1237
1238 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1239 if (mouse_state.flags == 0)
1240 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001241 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001242 switch (key)
1243 {
1244 case K_LEFTDRAG:
1245 case K_LEFTRELEASE:
1246 case K_RIGHTDRAG:
1247 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001248 // Ignore drag and release events when the button-down wasn't
1249 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001250 if (ignore_drag_release)
1251 {
1252 int save_mouse_col, save_mouse_row;
1253
1254 if (enter_mouse_col < 0)
1255 break;
1256
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001257 // mouse click in the window gave us focus, handle that
1258 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001259 save_mouse_col = mouse_col;
1260 save_mouse_row = mouse_row;
1261 mouse_col = enter_mouse_col;
1262 mouse_row = enter_mouse_row;
1263 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1264 mouse_col = save_mouse_col;
1265 mouse_row = save_mouse_row;
1266 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001267 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001268 case K_LEFTMOUSE:
1269 case K_RIGHTMOUSE:
1270 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1271 ignore_drag_release = TRUE;
1272 else
1273 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001274 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001275 if (clip_star.available)
1276 {
1277 int button, is_click, is_drag;
1278
1279 button = get_mouse_button(KEY2TERMCAP1(key),
1280 &is_click, &is_drag);
1281 if (mouse_model_popup() && button == MOUSE_LEFT
1282 && (mod_mask & MOD_MASK_SHIFT))
1283 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001284 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001285 button = MOUSE_RIGHT;
1286 mod_mask &= ~MOD_MASK_SHIFT;
1287 }
1288 clip_modeless(button, is_click, is_drag);
1289 }
1290 break;
1291
1292 case K_MIDDLEMOUSE:
1293 if (clip_star.available)
1294 insert_reg('*', TRUE);
1295 break;
1296 }
1297 enter_mouse_col = -1;
1298 return FALSE;
1299 }
1300#endif
1301 enter_mouse_col = -1;
1302
1303 switch (key)
1304 {
1305 case K_LEFTMOUSE:
1306 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1307 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1308 case K_LEFTRELEASE:
1309 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1310 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1311 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1312 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1313 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1314 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1315 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1316 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1317 }
1318 return TRUE;
1319}
1320
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001321/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001322 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1323 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001324 * Return the number of bytes in "buf".
1325 */
1326 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001327term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001328{
1329 VTerm *vterm = term->tl_vterm;
1330 VTermKey key = VTERM_KEY_NONE;
1331 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001332 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001333
1334 switch (c)
1335 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001336 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001337
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001338 // don't use VTERM_KEY_BACKSPACE, it always
1339 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001340 case K_BS: c = term_backspace_char; break;
1341
1342 case ESC: key = VTERM_KEY_ESCAPE; break;
1343 case K_DEL: key = VTERM_KEY_DEL; break;
1344 case K_DOWN: key = VTERM_KEY_DOWN; break;
1345 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1346 key = VTERM_KEY_DOWN; break;
1347 case K_END: key = VTERM_KEY_END; break;
1348 case K_S_END: mod = VTERM_MOD_SHIFT;
1349 key = VTERM_KEY_END; break;
1350 case K_C_END: mod = VTERM_MOD_CTRL;
1351 key = VTERM_KEY_END; break;
1352 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1353 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1354 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1355 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1356 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1357 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1358 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1359 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1360 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1361 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1362 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1363 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1364 case K_HOME: key = VTERM_KEY_HOME; break;
1365 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1366 key = VTERM_KEY_HOME; break;
1367 case K_C_HOME: mod = VTERM_MOD_CTRL;
1368 key = VTERM_KEY_HOME; break;
1369 case K_INS: key = VTERM_KEY_INS; break;
1370 case K_K0: key = VTERM_KEY_KP_0; break;
1371 case K_K1: key = VTERM_KEY_KP_1; break;
1372 case K_K2: key = VTERM_KEY_KP_2; break;
1373 case K_K3: key = VTERM_KEY_KP_3; break;
1374 case K_K4: key = VTERM_KEY_KP_4; break;
1375 case K_K5: key = VTERM_KEY_KP_5; break;
1376 case K_K6: key = VTERM_KEY_KP_6; break;
1377 case K_K7: key = VTERM_KEY_KP_7; break;
1378 case K_K8: key = VTERM_KEY_KP_8; break;
1379 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001380 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001381 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001382 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001383 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001384 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1385 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001386 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1387 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001388 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1389 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001390 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1391 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1392 case K_LEFT: key = VTERM_KEY_LEFT; break;
1393 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1394 key = VTERM_KEY_LEFT; break;
1395 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1396 key = VTERM_KEY_LEFT; break;
1397 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1398 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1399 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1400 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1401 key = VTERM_KEY_RIGHT; break;
1402 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1403 key = VTERM_KEY_RIGHT; break;
1404 case K_UP: key = VTERM_KEY_UP; break;
1405 case K_S_UP: mod = VTERM_MOD_SHIFT;
1406 key = VTERM_KEY_UP; break;
1407 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001408 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1409 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001410
Bram Moolenaara42ad572017-11-16 13:08:04 +01001411 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1412 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001413 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1414 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001415
1416 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001417 case K_LEFTMOUSE_NM:
1418 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001419 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001420 case K_LEFTRELEASE_NM:
1421 case K_MOUSEMOVE:
1422 case K_MIDDLEMOUSE:
1423 case K_MIDDLEDRAG:
1424 case K_MIDDLERELEASE:
1425 case K_RIGHTMOUSE:
1426 case K_RIGHTDRAG:
1427 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1428 return 0;
1429 other = TRUE;
1430 break;
1431
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001432 case K_X1MOUSE: /* TODO */ return 0;
1433 case K_X1DRAG: /* TODO */ return 0;
1434 case K_X1RELEASE: /* TODO */ return 0;
1435 case K_X2MOUSE: /* TODO */ return 0;
1436 case K_X2DRAG: /* TODO */ return 0;
1437 case K_X2RELEASE: /* TODO */ return 0;
1438
1439 case K_IGNORE: return 0;
1440 case K_NOP: return 0;
1441 case K_UNDO: return 0;
1442 case K_HELP: return 0;
1443 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1444 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1445 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1446 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1447 case K_SELECT: return 0;
1448#ifdef FEAT_GUI
1449 case K_VER_SCROLLBAR: return 0;
1450 case K_HOR_SCROLLBAR: return 0;
1451#endif
1452#ifdef FEAT_GUI_TABLINE
1453 case K_TABLINE: return 0;
1454 case K_TABMENU: return 0;
1455#endif
1456#ifdef FEAT_NETBEANS_INTG
1457 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1458#endif
1459#ifdef FEAT_DND
1460 case K_DROP: return 0;
1461#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001462 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001463 case K_PS: vterm_keyboard_start_paste(vterm);
1464 other = TRUE;
1465 break;
1466 case K_PE: vterm_keyboard_end_paste(vterm);
1467 other = TRUE;
1468 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001469 }
1470
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001471 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001472 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001473 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001474 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001475 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001476 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001477 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001478
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001479 /*
1480 * Convert special keys to vterm keys:
1481 * - Write keys to vterm: vterm_keyboard_key()
1482 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001483 */
1484 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001485 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001486 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001487 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001488 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001489 vterm_keyboard_unichar(vterm, c, mod);
1490
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001491 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001492 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1493}
1494
1495/*
1496 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001497 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001498 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001499 */
1500 static int
1501term_job_running_check(term_T *term, int check_job_status)
1502{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001503 // Also consider the job finished when the channel is closed, to avoid a
1504 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001505 if (term != NULL
1506 && term->tl_job != NULL
1507 && channel_is_open(term->tl_job->jv_channel))
1508 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001509 job_T *job = term->tl_job;
1510
1511 // Careful: Checking the job status may invoked callbacks, which close
1512 // the buffer and terminate "term". However, "job" will not be freed
1513 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001514 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001515 job_status(job);
1516 return (job->jv_status == JOB_STARTED
1517 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001518 }
1519 return FALSE;
1520}
1521
1522/*
1523 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001524 */
1525 int
1526term_job_running(term_T *term)
1527{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001528 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001529}
1530
1531/*
1532 * Return TRUE if "term" has an active channel and used ":term NONE".
1533 */
1534 int
1535term_none_open(term_T *term)
1536{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001537 // Also consider the job finished when the channel is closed, to avoid a
1538 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001539 return term != NULL
1540 && term->tl_job != NULL
1541 && channel_is_open(term->tl_job->jv_channel)
1542 && term->tl_job->jv_channel->ch_keep_open;
1543}
1544
1545/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001546 * Used when exiting: kill the job in "buf" if so desired.
1547 * Return OK when the job finished.
1548 * Return FAIL when the job is still running.
1549 */
1550 int
1551term_try_stop_job(buf_T *buf)
1552{
1553 int count;
1554 char *how = (char *)buf->b_term->tl_kill;
1555
1556#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1557 if ((how == NULL || *how == NUL) && (p_confirm || cmdmod.confirm))
1558 {
1559 char_u buff[DIALOG_MSG_SIZE];
1560 int ret;
1561
1562 dialog_msg(buff, _("Kill job in \"%s\"?"), buf->b_fname);
1563 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1564 if (ret == VIM_YES)
1565 how = "kill";
1566 else if (ret == VIM_CANCEL)
1567 return FAIL;
1568 }
1569#endif
1570 if (how == NULL || *how == NUL)
1571 return FAIL;
1572
1573 job_stop(buf->b_term->tl_job, NULL, how);
1574
Bram Moolenaar9172d232019-01-29 23:06:54 +01001575 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001576 for (count = 0; count < 100; ++count)
1577 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001578 job_T *job;
1579
1580 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001581 if (!buf_valid(buf)
1582 || buf->b_term == NULL
1583 || buf->b_term->tl_job == NULL)
1584 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001585 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001586
Bram Moolenaar9172d232019-01-29 23:06:54 +01001587 // Call job_status() to update jv_status. It may cause the job to be
1588 // cleaned up but it won't be freed.
1589 job_status(job);
1590 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001591 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001592
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001593 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001594 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001595 }
1596 return FAIL;
1597}
1598
1599/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001600 * Add the last line of the scrollback buffer to the buffer in the window.
1601 */
1602 static void
1603add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1604{
1605 buf_T *buf = term->tl_buffer;
1606 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1607 linenr_T lnum = buf->b_ml.ml_line_count;
1608
Bram Moolenaar4f974752019-02-17 17:44:42 +01001609#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001610 if (!enc_utf8 && enc_codepage > 0)
1611 {
1612 WCHAR *ret = NULL;
1613 int length = 0;
1614
1615 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1616 &ret, &length);
1617 if (ret != NULL)
1618 {
1619 WideCharToMultiByte_alloc(enc_codepage, 0,
1620 ret, length, (char **)&text, &len, 0, 0);
1621 vim_free(ret);
1622 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1623 vim_free(text);
1624 }
1625 }
1626 else
1627#endif
1628 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1629 if (empty)
1630 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001631 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001632 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001633 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001634 curbuf = curwin->w_buffer;
1635 }
1636}
1637
1638 static void
1639cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1640{
1641 attr->width = cell->width;
1642 attr->attrs = cell->attrs;
1643 attr->fg = cell->fg;
1644 attr->bg = cell->bg;
1645}
1646
1647 static int
1648equal_celattr(cellattr_T *a, cellattr_T *b)
1649{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001650 // We only compare the RGB colors, ignoring the ANSI index and type.
1651 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001652 return a->fg.red == b->fg.red
1653 && a->fg.green == b->fg.green
1654 && a->fg.blue == b->fg.blue
1655 && a->bg.red == b->bg.red
1656 && a->bg.green == b->bg.green
1657 && a->bg.blue == b->bg.blue;
1658}
1659
Bram Moolenaard96ff162018-02-18 22:13:29 +01001660/*
1661 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1662 * line at this position. Otherwise at the end.
1663 */
1664 static int
1665add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1666{
1667 if (ga_grow(&term->tl_scrollback, 1) == OK)
1668 {
1669 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1670 + term->tl_scrollback.ga_len;
1671
1672 if (lnum > 0)
1673 {
1674 int i;
1675
1676 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1677 {
1678 *line = *(line - 1);
1679 --line;
1680 }
1681 }
1682 line->sb_cols = 0;
1683 line->sb_cells = NULL;
1684 line->sb_fill_attr = *fill_attr;
1685 ++term->tl_scrollback.ga_len;
1686 return OK;
1687 }
1688 return FALSE;
1689}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001690
1691/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001692 * Remove the terminal contents from the scrollback and the buffer.
1693 * Used before adding a new scrollback line or updating the buffer for lines
1694 * displayed in the terminal.
1695 */
1696 static void
1697cleanup_scrollback(term_T *term)
1698{
1699 sb_line_T *line;
1700 garray_T *gap;
1701
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001702 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001703 gap = &term->tl_scrollback;
1704 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1705 && gap->ga_len > 0)
1706 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001707 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001708 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1709 vim_free(line->sb_cells);
1710 --gap->ga_len;
1711 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001712 curbuf = curwin->w_buffer;
1713 if (curbuf == term->tl_buffer)
1714 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001715}
1716
1717/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001718 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001719 */
1720 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001721update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001722{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001723 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001724 int len;
1725 int lines_skipped = 0;
1726 VTermPos pos;
1727 VTermScreenCell cell;
1728 cellattr_T fill_attr, new_fill_attr;
1729 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001730
1731 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1732 "Adding terminal window snapshot to buffer");
1733
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001734 // First remove the lines that were appended before, they might be
1735 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001736 cleanup_scrollback(term);
1737
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001738 screen = vterm_obtain_screen(term->tl_vterm);
1739 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001740 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1741 {
1742 len = 0;
1743 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1744 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1745 && cell.chars[0] != NUL)
1746 {
1747 len = pos.col + 1;
1748 new_fill_attr = term->tl_default_color;
1749 }
1750 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001751 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001752 cell2cellattr(&cell, &new_fill_attr);
1753
1754 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1755 ++lines_skipped;
1756 else
1757 {
1758 while (lines_skipped > 0)
1759 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001760 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001761 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001762 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001763 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001764 }
1765
1766 if (len == 0)
1767 p = NULL;
1768 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001769 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001770 if ((p != NULL || len == 0)
1771 && ga_grow(&term->tl_scrollback, 1) == OK)
1772 {
1773 garray_T ga;
1774 int width;
1775 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1776 + term->tl_scrollback.ga_len;
1777
1778 ga_init2(&ga, 1, 100);
1779 for (pos.col = 0; pos.col < len; pos.col += width)
1780 {
1781 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1782 {
1783 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001784 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001785 if (ga_grow(&ga, 1) == OK)
1786 ga.ga_len += utf_char2bytes(' ',
1787 (char_u *)ga.ga_data + ga.ga_len);
1788 }
1789 else
1790 {
1791 width = cell.width;
1792
1793 cell2cellattr(&cell, &p[pos.col]);
1794
Bram Moolenaara79fd562018-12-20 20:47:32 +01001795 // Each character can be up to 6 bytes.
1796 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001797 {
1798 int i;
1799 int c;
1800
1801 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1802 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1803 (char_u *)ga.ga_data + ga.ga_len);
1804 }
1805 }
1806 }
1807 line->sb_cols = len;
1808 line->sb_cells = p;
1809 line->sb_fill_attr = new_fill_attr;
1810 fill_attr = new_fill_attr;
1811 ++term->tl_scrollback.ga_len;
1812
1813 if (ga_grow(&ga, 1) == FAIL)
1814 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1815 else
1816 {
1817 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1818 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1819 }
1820 ga_clear(&ga);
1821 }
1822 else
1823 vim_free(p);
1824 }
1825 }
1826
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001827 // Add trailing empty lines.
1828 for (pos.row = term->tl_scrollback.ga_len;
1829 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1830 ++pos.row)
1831 {
1832 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1833 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1834 }
1835
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001836 term->tl_dirty_snapshot = FALSE;
1837#ifdef FEAT_TIMERS
1838 term->tl_timer_set = FALSE;
1839#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001840}
1841
1842/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001843 * Loop over all windows in the current tab, and also curwin, which is not
1844 * encountered when using a terminal in a popup window.
1845 * Return TRUE if "*wp" was set to the next window.
1846 */
1847 static int
1848for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1849{
1850 if (*wp == NULL)
1851 *wp = firstwin;
1852 else if ((*wp)->w_next != NULL)
1853 *wp = (*wp)->w_next;
1854 else if (!*did_curwin)
1855 *wp = curwin;
1856 else
1857 return FALSE;
1858 if (*wp == curwin)
1859 *did_curwin = TRUE;
1860 return TRUE;
1861}
1862
1863/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001864 * If needed, add the current lines of the terminal to scrollback and to the
1865 * buffer. Called after the job has ended and when switching to
1866 * Terminal-Normal mode.
1867 * When "redraw" is TRUE redraw the windows that show the terminal.
1868 */
1869 static void
1870may_move_terminal_to_buffer(term_T *term, int redraw)
1871{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001872 if (term->tl_vterm == NULL)
1873 return;
1874
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001875 // Update the snapshot only if something changes or the buffer does not
1876 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001877 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1878 <= term->tl_scrollback_scrolled)
1879 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001880
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001881 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001882 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1883 &term->tl_default_color.fg, &term->tl_default_color.bg);
1884
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001885 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001886 {
1887 win_T *wp = NULL;
1888 int did_curwin = FALSE;
1889
1890 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001891 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001892 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001893 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001894 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1895 wp->w_cursor.col = 0;
1896 wp->w_valid = 0;
1897 if (wp->w_cursor.lnum >= wp->w_height)
1898 {
1899 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001900
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001901 if (wp->w_topline < min_topline)
1902 wp->w_topline = min_topline;
1903 }
1904 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001905 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001906 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001907 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001908}
1909
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001910#if defined(FEAT_TIMERS) || defined(PROTO)
1911/*
1912 * Check if any terminal timer expired. If so, copy text from the terminal to
1913 * the buffer.
1914 * Return the time until the next timer will expire.
1915 */
1916 int
1917term_check_timers(int next_due_arg, proftime_T *now)
1918{
1919 term_T *term;
1920 int next_due = next_due_arg;
1921
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001922 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001923 {
1924 if (term->tl_timer_set && !term->tl_normal_mode)
1925 {
1926 long this_due = proftime_time_left(&term->tl_timer_due, now);
1927
1928 if (this_due <= 1)
1929 {
1930 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001931 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001932 }
1933 else if (next_due == -1 || next_due > this_due)
1934 next_due = this_due;
1935 }
1936 }
1937
1938 return next_due;
1939}
1940#endif
1941
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001942/*
1943 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
1944 * otherwise end it.
1945 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001946 static void
1947set_terminal_mode(term_T *term, int normal_mode)
1948{
1949 term->tl_normal_mode = normal_mode;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001950 if (!normal_mode)
1951 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001952 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001953 if (term->tl_buffer == curbuf)
1954 maketitle();
1955}
1956
1957/*
Bram Moolenaare2978022020-04-26 14:47:44 +02001958 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001959 * Move the vterm contents into the scrollback buffer and free the vterm.
1960 */
1961 static void
1962cleanup_vterm(term_T *term)
1963{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001964 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01001965 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001966 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001967 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001968}
1969
1970/*
1971 * Switch from Terminal-Job mode to Terminal-Normal mode.
1972 * Suspends updating the terminal window.
1973 */
1974 static void
1975term_enter_normal_mode(void)
1976{
1977 term_T *term = curbuf->b_term;
1978
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001979 set_terminal_mode(term, TRUE);
1980
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001981 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001982 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001983
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001984 // Move the window cursor to the position of the cursor in the
1985 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001986 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1987 + term->tl_cursor_pos.row + 1;
1988 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02001989 if (coladvance(term->tl_cursor_pos.col) == FAIL)
1990 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001991 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001992
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001993 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001994 curwin->w_topline = term->tl_scrollback_scrolled + 1;
1995}
1996
1997/*
1998 * Returns TRUE if the current window contains a terminal and we are in
1999 * Terminal-Normal mode.
2000 */
2001 int
2002term_in_normal_mode(void)
2003{
2004 term_T *term = curbuf->b_term;
2005
2006 return term != NULL && term->tl_normal_mode;
2007}
2008
2009/*
2010 * Switch from Terminal-Normal mode to Terminal-Job mode.
2011 * Restores updating the terminal window.
2012 */
2013 void
2014term_enter_job_mode()
2015{
2016 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002017
2018 set_terminal_mode(term, FALSE);
2019
2020 if (term->tl_channel_closed)
2021 cleanup_vterm(term);
2022 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002023#ifdef FEAT_PROP_POPUP
2024 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002025 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002026#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002027}
2028
2029/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002030 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002031 * Note: while waiting a terminal may be closed and freed if the channel is
2032 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002033 */
2034 static int
2035term_vgetc()
2036{
2037 int c;
2038 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002039 int modify_other_keys =
2040 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002041
2042 State = TERMINAL;
2043 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002044#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002045 ctrl_break_was_pressed = FALSE;
2046#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002047 if (modify_other_keys)
2048 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002049 c = vgetc();
2050 got_int = FALSE;
2051 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002052 if (modify_other_keys)
2053 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002054 return c;
2055}
2056
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002057static int mouse_was_outside = FALSE;
2058
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002059/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002060 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002061 * Return FAIL when the key needs to be handled in Normal mode.
2062 * Return OK when the key was dropped or sent to the terminal.
2063 */
2064 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002065send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002066{
2067 char msg[KEY_BUF_LEN];
2068 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002069 int dragging_outside = FALSE;
2070
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002071 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002072 switch (c)
2073 {
2074 case NUL:
2075 case K_ZERO:
2076 if (typed)
2077 stuffcharReadbuff(c);
2078 return FAIL;
2079
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002080 case K_TABLINE:
2081 stuffcharReadbuff(c);
2082 return FAIL;
2083
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002084 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002085 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002086 return FAIL;
2087
2088 case K_LEFTDRAG:
2089 case K_MIDDLEDRAG:
2090 case K_RIGHTDRAG:
2091 case K_X1DRAG:
2092 case K_X2DRAG:
2093 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002094 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002095 case K_LEFTMOUSE:
2096 case K_LEFTMOUSE_NM:
2097 case K_LEFTRELEASE:
2098 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002099 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002100 case K_MIDDLEMOUSE:
2101 case K_MIDDLERELEASE:
2102 case K_RIGHTMOUSE:
2103 case K_RIGHTRELEASE:
2104 case K_X1MOUSE:
2105 case K_X1RELEASE:
2106 case K_X2MOUSE:
2107 case K_X2RELEASE:
2108
2109 case K_MOUSEUP:
2110 case K_MOUSEDOWN:
2111 case K_MOUSELEFT:
2112 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002113 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002114 int row = mouse_row;
2115 int col = mouse_col;
2116
2117#ifdef FEAT_PROP_POPUP
2118 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002119 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002120 row -= popup_top_extra(curwin);
2121 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002122 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002123#endif
2124 if (row < W_WINROW(curwin)
2125 || row >= (W_WINROW(curwin) + curwin->w_height)
2126 || col < curwin->w_wincol
2127 || col >= W_ENDCOL(curwin)
2128 || dragging_outside)
2129 {
2130 // click or scroll outside the current window or on status
2131 // line or vertical separator
2132 if (typed)
2133 {
2134 stuffcharReadbuff(c);
2135 mouse_was_outside = TRUE;
2136 }
2137 return FAIL;
2138 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002139 }
2140 }
2141 if (typed)
2142 mouse_was_outside = FALSE;
2143
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002144 // Convert the typed key to a sequence of bytes for the job.
2145 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002146 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002147 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002148 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2149 (char_u *)msg, (int)len, NULL);
2150
2151 return OK;
2152}
2153
2154 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002155position_cursor(win_T *wp, VTermPos *pos, int add_off UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002156{
2157 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2158 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002159#ifdef FEAT_PROP_POPUP
2160 if (add_off && popup_is_popup(curwin))
2161 {
2162 wp->w_wrow += popup_top_extra(curwin);
2163 wp->w_wcol += popup_left_extra(curwin);
2164 }
2165#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002166 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2167}
2168
2169/*
2170 * Handle CTRL-W "": send register contents to the job.
2171 */
2172 static void
2173term_paste_register(int prev_c UNUSED)
2174{
2175 int c;
2176 list_T *l;
2177 listitem_T *item;
2178 long reglen = 0;
2179 int type;
2180
2181#ifdef FEAT_CMDL_INFO
2182 if (add_to_showcmd(prev_c))
2183 if (add_to_showcmd('"'))
2184 out_flush();
2185#endif
2186 c = term_vgetc();
2187#ifdef FEAT_CMDL_INFO
2188 clear_showcmd();
2189#endif
2190 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002191 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002192 return;
2193
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002194 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002195 if (c == '=' && get_expr_register() != '=')
2196 return;
2197 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002198 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002199 return;
2200
2201 l = (list_T *)get_reg_contents(c, GREG_LIST);
2202 if (l != NULL)
2203 {
2204 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002205 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002206 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002207 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002208#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002209 char_u *tmp = s;
2210
2211 if (!enc_utf8 && enc_codepage > 0)
2212 {
2213 WCHAR *ret = NULL;
2214 int length = 0;
2215
2216 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2217 (int)STRLEN(s), &ret, &length);
2218 if (ret != NULL)
2219 {
2220 WideCharToMultiByte_alloc(CP_UTF8, 0,
2221 ret, length, (char **)&s, &length, 0, 0);
2222 vim_free(ret);
2223 }
2224 }
2225#endif
2226 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2227 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002228#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002229 if (tmp != s)
2230 vim_free(s);
2231#endif
2232
2233 if (item->li_next != NULL || type == MLINE)
2234 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2235 (char_u *)"\r", 1, NULL);
2236 }
2237 list_free(l);
2238 }
2239}
2240
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002241/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002242 * Return TRUE when waiting for a character in the terminal, the cursor of the
2243 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002244 */
2245 int
2246terminal_is_active()
2247{
2248 return in_terminal_loop != NULL;
2249}
2250
Bram Moolenaar83d47902020-03-26 20:34:00 +01002251/*
2252 * Return the highight group name for the terminal; "Terminal" if not set.
2253 */
2254 static char_u *
2255term_get_highlight_name(term_T *term)
2256{
2257 if (term->tl_highlight_name == NULL)
2258 return (char_u *)"Terminal";
2259 return term->tl_highlight_name;
2260}
2261
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002262#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002263 cursorentry_T *
2264term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2265{
2266 term_T *term = in_terminal_loop;
2267 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002268 int id;
2269 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002270
Bram Moolenaara80faa82020-04-12 19:37:17 +02002271 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002272 entry.shape = entry.mshape =
2273 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2274 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2275 SHAPE_BLOCK;
2276 entry.percentage = 20;
2277 if (term->tl_cursor_blink)
2278 {
2279 entry.blinkwait = 700;
2280 entry.blinkon = 400;
2281 entry.blinkoff = 250;
2282 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002283
Bram Moolenaar83d47902020-03-26 20:34:00 +01002284 // The highlight group overrules the defaults.
2285 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002286 if (id != 0)
2287 {
2288 syn_id2colors(id, &term_fg, &term_bg);
2289 *fg = term_bg;
2290 }
2291 else
2292 *fg = gui.back_pixel;
2293
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002294 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002295 {
2296 if (id != 0)
2297 *bg = term_fg;
2298 else
2299 *bg = gui.norm_pixel;
2300 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002301 else
2302 *bg = color_name2handle(term->tl_cursor_color);
2303 entry.name = "n";
2304 entry.used_for = SHAPE_CURSOR;
2305
2306 return &entry;
2307}
2308#endif
2309
Bram Moolenaard317b382018-02-08 22:33:31 +01002310 static void
2311may_output_cursor_props(void)
2312{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002313 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002314 || last_set_cursor_shape != desired_cursor_shape
2315 || last_set_cursor_blink != desired_cursor_blink)
2316 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002317 cursor_color_copy(&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;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002320 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002321 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002322 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002323 ui_cursor_shape_forced(TRUE);
2324 else
2325 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2326 }
2327}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002328
Bram Moolenaard317b382018-02-08 22:33:31 +01002329/*
2330 * Set the cursor color and shape, if not last set to these.
2331 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002332 static void
2333may_set_cursor_props(term_T *term)
2334{
2335#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002336 // For the GUI the cursor properties are obtained with
2337 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002338 if (gui.in_use)
2339 return;
2340#endif
2341 if (in_terminal_loop == term)
2342 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002343 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002344 desired_cursor_shape = term->tl_cursor_shape;
2345 desired_cursor_blink = term->tl_cursor_blink;
2346 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002347 }
2348}
2349
Bram Moolenaard317b382018-02-08 22:33:31 +01002350/*
2351 * Reset the desired cursor properties and restore them when needed.
2352 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002353 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002354prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002355{
2356#ifdef FEAT_GUI
2357 if (gui.in_use)
2358 return;
2359#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002360 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002361 desired_cursor_shape = -1;
2362 desired_cursor_blink = -1;
2363 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002364}
2365
2366/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002367 * Returns TRUE if the current window contains a terminal and we are sending
2368 * keys to the job.
2369 * If "check_job_status" is TRUE update the job status.
2370 */
2371 static int
2372term_use_loop_check(int check_job_status)
2373{
2374 term_T *term = curbuf->b_term;
2375
2376 return term != NULL
2377 && !term->tl_normal_mode
2378 && term->tl_vterm != NULL
2379 && term_job_running_check(term, check_job_status);
2380}
2381
2382/*
2383 * Returns TRUE if the current window contains a terminal and we are sending
2384 * keys to the job.
2385 */
2386 int
2387term_use_loop(void)
2388{
2389 return term_use_loop_check(FALSE);
2390}
2391
2392/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002393 * Called when entering a window with the mouse. If this is a terminal window
2394 * we may want to change state.
2395 */
2396 void
2397term_win_entered()
2398{
2399 term_T *term = curbuf->b_term;
2400
2401 if (term != NULL)
2402 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002403 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002404 {
2405 reset_VIsual_and_resel();
2406 if (State & INSERT)
2407 stop_insert_mode = TRUE;
2408 }
2409 mouse_was_outside = FALSE;
2410 enter_mouse_col = mouse_col;
2411 enter_mouse_row = mouse_row;
2412 }
2413}
2414
2415/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002416 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2417 * Return the Ctrl-key value in that case.
2418 */
2419 static int
2420raw_c_to_ctrl(int c)
2421{
2422 if ((mod_mask & MOD_MASK_CTRL)
2423 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2424 return c & 0x1f;
2425 return c;
2426}
2427
2428/*
2429 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2430 * May set "mod_mask".
2431 */
2432 static int
2433ctrl_to_raw_c(int c)
2434{
2435 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2436 {
2437 mod_mask |= MOD_MASK_CTRL;
2438 return c + '@';
2439 }
2440 return c;
2441}
2442
2443/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002444 * Wait for input and send it to the job.
2445 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2446 * when there is no more typahead.
2447 * Return when the start of a CTRL-W command is typed or anything else that
2448 * should be handled as a Normal mode command.
2449 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2450 * the terminal was closed.
2451 */
2452 int
2453terminal_loop(int blocking)
2454{
2455 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002456 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002457 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002458 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002459#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002460 int tty_fd = curbuf->b_term->tl_job->jv_channel
2461 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002462#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002463 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002464
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002465 // Remember the terminal we are sending keys to. However, the terminal
2466 // might be closed while waiting for a character, e.g. typing "exit" in a
2467 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2468 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002469 in_terminal_loop = curbuf->b_term;
2470
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002471 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002472 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002473 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002474 if (termwinkey == Ctrl_W)
2475 termwinkey = 0;
2476 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002477 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002478 may_set_cursor_props(curbuf->b_term);
2479
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002480 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002481 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002482#ifdef FEAT_GUI
2483 if (!curbuf->b_term->tl_system)
2484#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002485 // TODO: skip screen update when handling a sequence of keys.
2486 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002487 while (must_redraw != 0)
2488 if (update_screen(0) == FAIL)
2489 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002490 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002491 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002492 break;
2493
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002494 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002495 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002496
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002497 raw_c = term_vgetc();
Bram Moolenaard58d4f92020-07-01 15:49:29 +02002498if (raw_c > 0)
2499 ch_log(NULL, "terminal_loop() got %d", raw_c);
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002500 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002501 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002502 // Job finished while waiting for a character. Push back the
2503 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002504 if (raw_c != K_IGNORE)
2505 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002506 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002507 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002508 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002509 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002510 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002511
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002512#ifdef UNIX
2513 /*
2514 * The shell or another program may change the tty settings. Getting
2515 * them for every typed character is a bit of overhead, but it's needed
2516 * for the first character typed, e.g. when Vim starts in a shell.
2517 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002518 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002519 {
2520 ttyinfo_T info;
2521
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002522 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002523 if (get_tty_info(tty_fd, &info) == OK)
2524 term_backspace_char = info.backspace;
2525 }
2526#endif
2527
Bram Moolenaar4f974752019-02-17 17:44:42 +01002528#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002529 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2530 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002531 if (ctrl_break_was_pressed)
2532 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2533#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002534 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2535 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002536 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002537#ifdef FEAT_GUI
2538 && !curbuf->b_term->tl_system
2539#endif
2540 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002541 {
2542 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002543 int prev_raw_c = raw_c;
2544 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002545
2546#ifdef FEAT_CMDL_INFO
2547 if (add_to_showcmd(c))
2548 out_flush();
2549#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002550 raw_c = term_vgetc();
2551 c = raw_c_to_ctrl(raw_c);
2552
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002553#ifdef FEAT_CMDL_INFO
2554 clear_showcmd();
2555#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002556 if (!term_use_loop_check(TRUE)
2557 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002558 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002559 break;
2560
2561 if (prev_c == Ctrl_BSL)
2562 {
2563 if (c == Ctrl_N)
2564 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002565 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002566 term_enter_normal_mode();
2567 ret = FAIL;
2568 goto theend;
2569 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002570 // Send both keys to the terminal, first one here, second one
2571 // below.
2572 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2573 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002574 }
2575 else if (c == Ctrl_C)
2576 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002577 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002578 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2579 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002580 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002581 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002582 // "CTRL-W .": send CTRL-W to the job
2583 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002584 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002585 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002586 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002587 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002588 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002589 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002590 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002591 else if (c == 'N')
2592 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002593 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002594 term_enter_normal_mode();
2595 ret = FAIL;
2596 goto theend;
2597 }
2598 else if (c == '"')
2599 {
2600 term_paste_register(prev_c);
2601 continue;
2602 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002603 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002604 {
Bram Moolenaara4b26992019-08-15 20:58:54 +02002605 char_u buf[MB_MAXBYTES + 2];
2606
2607 // Put the command into the typeahead buffer, when using the
2608 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2609 buf[0] = Ctrl_W;
2610 buf[(*mb_char2bytes)(c, buf + 1) + 1] = NUL;
2611 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002612 ret = OK;
2613 goto theend;
2614 }
2615 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002616# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002617 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002618 {
2619 WCHAR wc;
2620 char_u mb[3];
2621
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002622 mb[0] = (unsigned)raw_c >> 8;
2623 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002624 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002625 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002626 }
2627# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002628 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002629 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002630 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002631 // We are sure to come back here, don't reset the cursor color
2632 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002633 restore_cursor = FALSE;
2634
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002635 ret = OK;
2636 goto theend;
2637 }
2638 }
2639 ret = FAIL;
2640
2641theend:
2642 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002643 if (restore_cursor)
2644 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002645
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002646 // Move a snapshot of the screen contents to the buffer, so that completion
2647 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002648 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2649 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002650
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002651 return ret;
2652}
2653
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002654 static void
2655may_toggle_cursor(term_T *term)
2656{
2657 if (in_terminal_loop == term)
2658 {
2659 if (term->tl_cursor_visible)
2660 cursor_on();
2661 else
2662 cursor_off();
2663 }
2664}
2665
2666/*
Bram Moolenaar83d47902020-03-26 20:34:00 +01002667 * Cache "Terminal" highlight group colors.
2668 */
2669 void
2670set_terminal_default_colors(int cterm_fg, int cterm_bg)
2671{
2672 term_default_cterm_fg = cterm_fg - 1;
2673 term_default_cterm_bg = cterm_bg - 1;
2674}
2675
2676 static int
2677get_default_cterm_fg(term_T *term)
2678{
2679 if (term->tl_highlight_name != NULL)
2680 {
2681 int id = syn_name2id(term->tl_highlight_name);
2682 int fg = -1;
2683 int bg = -1;
2684
2685 if (id > 0)
2686 syn_id2cterm_bg(id, &fg, &bg);
2687 return fg;
2688 }
2689 return term_default_cterm_fg;
2690}
2691
2692 static int
2693get_default_cterm_bg(term_T *term)
2694{
2695 if (term->tl_highlight_name != NULL)
2696 {
2697 int id = syn_name2id(term->tl_highlight_name);
2698 int fg = -1;
2699 int bg = -1;
2700
2701 if (id > 0)
2702 syn_id2cterm_bg(id, &fg, &bg);
2703 return bg;
2704 }
2705 return term_default_cterm_bg;
2706}
2707
2708/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002709 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002710 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002711 */
2712 static int
2713color2index(VTermColor *color, int fg, int *boldp)
2714{
2715 int red = color->red;
2716 int blue = color->blue;
2717 int green = color->green;
2718
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002719 if (VTERM_COLOR_IS_DEFAULT_FG(color)
2720 || VTERM_COLOR_IS_DEFAULT_BG(color))
2721 return 0;
2722 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002723 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002724 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002725 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002726 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002727 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002728 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2729 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2730 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002731 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002732 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2733 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2734 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2735 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2736 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2737 case 10: return lookup_color(20, fg, boldp) + 1; // red
2738 case 11: return lookup_color(16, fg, boldp) + 1; // green
2739 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2740 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2741 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2742 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2743 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002744 }
2745 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002746
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002747 if (t_colors >= 256)
2748 {
2749 if (red == blue && red == green)
2750 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002751 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002752 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002753 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2754 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2755 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002756 int i;
2757
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002758 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002759 return 17; // 00/00/00
2760 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002761 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002762 for (i = 0; i < 23; ++i)
2763 if (red < cutoff[i])
2764 return i + 233;
2765 return 256;
2766 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002767 {
2768 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2769 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002770
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002771 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002772 for (ri = 0; ri < 5; ++ri)
2773 if (red < cutoff[ri])
2774 break;
2775 for (gi = 0; gi < 5; ++gi)
2776 if (green < cutoff[gi])
2777 break;
2778 for (bi = 0; bi < 5; ++bi)
2779 if (blue < cutoff[bi])
2780 break;
2781 return 17 + ri * 36 + gi * 6 + bi;
2782 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002783 }
2784 return 0;
2785}
2786
2787/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002788 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002789 */
2790 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002791vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002792{
2793 int attr = 0;
2794
2795 if (cellattrs.bold)
2796 attr |= HL_BOLD;
2797 if (cellattrs.underline)
2798 attr |= HL_UNDERLINE;
2799 if (cellattrs.italic)
2800 attr |= HL_ITALIC;
2801 if (cellattrs.strike)
2802 attr |= HL_STRIKETHROUGH;
2803 if (cellattrs.reverse)
2804 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002805 return attr;
2806}
2807
2808/*
2809 * Store Vterm attributes in "cell" from highlight flags.
2810 */
2811 static void
2812hl2vtermAttr(int attr, cellattr_T *cell)
2813{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002814 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002815 if (attr & HL_BOLD)
2816 cell->attrs.bold = 1;
2817 if (attr & HL_UNDERLINE)
2818 cell->attrs.underline = 1;
2819 if (attr & HL_ITALIC)
2820 cell->attrs.italic = 1;
2821 if (attr & HL_STRIKETHROUGH)
2822 cell->attrs.strike = 1;
2823 if (attr & HL_INVERSE)
2824 cell->attrs.reverse = 1;
2825}
2826
2827/*
2828 * Convert the attributes of a vterm cell into an attribute index.
2829 */
2830 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002831cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002832 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002833 win_T *wp,
2834 VTermScreenCellAttrs cellattrs,
2835 VTermColor cellfg,
2836 VTermColor cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002837{
2838 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002839
2840#ifdef FEAT_GUI
2841 if (gui.in_use)
2842 {
2843 guicolor_T fg, bg;
2844
2845 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2846 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2847 return get_gui_attr_idx(attr, fg, bg);
2848 }
2849 else
2850#endif
2851#ifdef FEAT_TERMGUICOLORS
2852 if (p_tgc)
2853 {
2854 guicolor_T fg, bg;
2855
2856 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2857 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2858
2859 return get_tgc_attr_idx(attr, fg, bg);
2860 }
2861 else
2862#endif
2863 {
2864 int bold = MAYBE;
2865 int fg = color2index(&cellfg, TRUE, &bold);
2866 int bg = color2index(&cellbg, FALSE, &bold);
2867
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002868 // Use the 'wincolor' or "Terminal" highlighting for the default
2869 // colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002870 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002871 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002872 int wincolor_fg = -1;
2873 int wincolor_bg = -1;
2874
2875 if (wp != NULL && *wp->w_p_wcr != NUL)
2876 {
2877 int id = syn_name2id(curwin->w_p_wcr);
2878
2879 // Get the 'wincolor' group colors.
2880 if (id > 0)
2881 syn_id2cterm_bg(id, &wincolor_fg, &wincolor_bg);
2882 }
2883 if (fg == 0)
2884 {
2885 if (wincolor_fg >= 0)
2886 fg = wincolor_fg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002887 else
2888 {
2889 int cterm_fg = get_default_cterm_fg(term);
2890
2891 if (cterm_fg >= 0)
2892 fg = cterm_fg + 1;
2893 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002894 }
2895 if (bg == 0)
2896 {
2897 if (wincolor_bg >= 0)
2898 bg = wincolor_bg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002899 else
2900 {
2901 int cterm_bg = get_default_cterm_bg(term);
2902
2903 if (cterm_bg >= 0)
2904 bg = cterm_bg + 1;
2905 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002906 }
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002907 }
2908
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002909 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002910 if (bold == TRUE)
2911 attr |= HL_BOLD;
2912 return get_cterm_attr_idx(attr, fg, bg);
2913 }
2914 return 0;
2915}
2916
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002917 static void
2918set_dirty_snapshot(term_T *term)
2919{
2920 term->tl_dirty_snapshot = TRUE;
2921#ifdef FEAT_TIMERS
2922 if (!term->tl_normal_mode)
2923 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002924 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002925 profile_setlimit(100L, &term->tl_timer_due);
2926 term->tl_timer_set = TRUE;
2927 }
2928#endif
2929}
2930
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002931 static int
2932handle_damage(VTermRect rect, void *user)
2933{
2934 term_T *term = (term_T *)user;
2935
2936 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2937 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002938 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002939 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002940 return 1;
2941}
2942
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002943 static void
2944term_scroll_up(term_T *term, int start_row, int count)
2945{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002946 win_T *wp = NULL;
2947 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002948 VTermColor fg, bg;
2949 VTermScreenCellAttrs attr;
2950 int clear_attr;
2951
Bram Moolenaara80faa82020-04-12 19:37:17 +02002952 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002953
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002954 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002955 {
2956 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002957 {
2958 // Set the color to clear lines with.
2959 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2960 &fg, &bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002961 clear_attr = cell2attr(term, wp, attr, fg, bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002962 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002963 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002964 }
2965}
2966
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002967 static int
2968handle_moverect(VTermRect dest, VTermRect src, void *user)
2969{
2970 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002971 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002972
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002973 // Scrolling up is done much more efficiently by deleting lines instead of
2974 // redrawing the text. But avoid doing this multiple times, postpone until
2975 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002976 if (dest.start_col == src.start_col
2977 && dest.end_col == src.end_col
2978 && dest.start_row < src.start_row)
2979 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002980 if (dest.start_row == 0)
2981 term->tl_postponed_scroll += count;
2982 else
2983 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002984 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02002985
2986 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
2987 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002988 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02002989
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002990 // Note sure if the scrolling will work correctly, let's do a complete
2991 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002992 redraw_buf_later(term->tl_buffer, NOT_VALID);
2993 return 1;
2994}
2995
2996 static int
2997handle_movecursor(
2998 VTermPos pos,
2999 VTermPos oldpos UNUSED,
3000 int visible,
3001 void *user)
3002{
3003 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003004 win_T *wp = NULL;
3005 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003006
3007 term->tl_cursor_pos = pos;
3008 term->tl_cursor_visible = visible;
3009
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003010 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003011 {
3012 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003013 position_cursor(wp, &pos, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003014 }
3015 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
3016 {
3017 may_toggle_cursor(term);
3018 update_cursor(term, term->tl_cursor_visible);
3019 }
3020
3021 return 1;
3022}
3023
3024 static int
3025handle_settermprop(
3026 VTermProp prop,
3027 VTermValue *value,
3028 void *user)
3029{
3030 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003031 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003032
3033 switch (prop)
3034 {
3035 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003036 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003037 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003038 if (strval == NULL)
3039 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003040 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003041 // a blank title isn't useful, make it empty, so that "running" is
3042 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003043 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003044 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003045 // Same as blank
3046 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003047 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003048 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3049 term->tl_title = NULL;
3050 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003051 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003052 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003053#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003054 else if (!enc_utf8 && enc_codepage > 0)
3055 {
3056 WCHAR *ret = NULL;
3057 int length = 0;
3058
3059 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003060 (char*)value->string.str,
3061 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003062 if (ret != NULL)
3063 {
3064 WideCharToMultiByte_alloc(enc_codepage, 0,
3065 ret, length, (char**)&term->tl_title,
3066 &length, 0, 0);
3067 vim_free(ret);
3068 }
3069 }
3070#endif
3071 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003072 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003073 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003074 strval = NULL;
3075 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003076 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003077 if (term == curbuf->b_term)
3078 maketitle();
3079 break;
3080
3081 case VTERM_PROP_CURSORVISIBLE:
3082 term->tl_cursor_visible = value->boolean;
3083 may_toggle_cursor(term);
3084 out_flush();
3085 break;
3086
3087 case VTERM_PROP_CURSORBLINK:
3088 term->tl_cursor_blink = value->boolean;
3089 may_set_cursor_props(term);
3090 break;
3091
3092 case VTERM_PROP_CURSORSHAPE:
3093 term->tl_cursor_shape = value->number;
3094 may_set_cursor_props(term);
3095 break;
3096
3097 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003098 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003099 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003100 if (strval == NULL)
3101 break;
3102 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003103 may_set_cursor_props(term);
3104 break;
3105
3106 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003107 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003108 term->tl_using_altscreen = value->boolean;
3109 break;
3110
3111 default:
3112 break;
3113 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003114 vim_free(strval);
3115
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003116 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003117 return 1;
3118}
3119
3120/*
3121 * The job running in the terminal resized the terminal.
3122 */
3123 static int
3124handle_resize(int rows, int cols, void *user)
3125{
3126 term_T *term = (term_T *)user;
3127 win_T *wp;
3128
3129 term->tl_rows = rows;
3130 term->tl_cols = cols;
3131 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003132 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003133 term->tl_vterm_size_changed = FALSE;
3134 else
3135 {
3136 FOR_ALL_WINDOWS(wp)
3137 {
3138 if (wp->w_buffer == term->tl_buffer)
3139 {
3140 win_setheight_win(rows, wp);
3141 win_setwidth_win(cols, wp);
3142 }
3143 }
3144 redraw_buf_later(term->tl_buffer, NOT_VALID);
3145 }
3146 return 1;
3147}
3148
3149/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003150 * If the number of lines that are stored goes over 'termscrollback' then
3151 * delete the first 10%.
3152 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3153 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003154 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003155 static void
3156limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003157{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003158 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003159 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003160 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003161 int i;
3162
3163 curbuf = term->tl_buffer;
3164 for (i = 0; i < todo; ++i)
3165 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003166 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3167 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003168 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003169 }
3170 curbuf = curwin->w_buffer;
3171
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003172 gap->ga_len -= todo;
3173 mch_memmove(gap->ga_data,
3174 (sb_line_T *)gap->ga_data + todo,
3175 sizeof(sb_line_T) * gap->ga_len);
3176 if (update_buffer)
3177 term->tl_scrollback_scrolled -= todo;
3178 }
3179}
3180
3181/*
3182 * Handle a line that is pushed off the top of the screen.
3183 */
3184 static int
3185handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3186{
3187 term_T *term = (term_T *)user;
3188 garray_T *gap;
3189 int update_buffer;
3190
3191 if (term->tl_normal_mode)
3192 {
3193 // In Terminal-Normal mode the user interacts with the buffer, thus we
3194 // must not change it. Postpone adding the scrollback lines.
3195 gap = &term->tl_scrollback_postponed;
3196 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003197 }
3198 else
3199 {
3200 // First remove the lines that were appended before, the pushed line
3201 // goes above it.
3202 cleanup_scrollback(term);
3203 gap = &term->tl_scrollback;
3204 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003205 }
3206
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003207 limit_scrollback(term, gap, update_buffer);
3208
3209 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003210 {
3211 cellattr_T *p = NULL;
3212 int len = 0;
3213 int i;
3214 int c;
3215 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003216 int text_len;
3217 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003218 sb_line_T *line;
3219 garray_T ga;
3220 cellattr_T fill_attr = term->tl_default_color;
3221
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003222 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003223 for (i = 0; i < cols; ++i)
3224 if (cells[i].chars[0] != 0)
3225 len = i + 1;
3226 else
3227 cell2cellattr(&cells[i], &fill_attr);
3228
3229 ga_init2(&ga, 1, 100);
3230 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003231 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003232 if (p != NULL)
3233 {
3234 for (col = 0; col < len; col += cells[col].width)
3235 {
3236 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3237 {
3238 ga.ga_len = 0;
3239 break;
3240 }
3241 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3242 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3243 (char_u *)ga.ga_data + ga.ga_len);
3244 cell2cellattr(&cells[col], &p[col]);
3245 }
3246 }
3247 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003248 {
3249 if (update_buffer)
3250 text = (char_u *)"";
3251 else
3252 text = vim_strsave((char_u *)"");
3253 text_len = 0;
3254 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003255 else
3256 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003257 text = ga.ga_data;
3258 text_len = ga.ga_len;
3259 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003260 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003261 if (update_buffer)
3262 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003263
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003264 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003265 line->sb_cols = len;
3266 line->sb_cells = p;
3267 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003268 if (update_buffer)
3269 {
3270 line->sb_text = NULL;
3271 ++term->tl_scrollback_scrolled;
3272 ga_clear(&ga); // free the text
3273 }
3274 else
3275 {
3276 line->sb_text = text;
3277 ga_init(&ga); // text is kept in tl_scrollback_postponed
3278 }
3279 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003280 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003281 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003282}
3283
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003284/*
3285 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3286 * received and stored in tl_scrollback_postponed.
3287 */
3288 static void
3289handle_postponed_scrollback(term_T *term)
3290{
3291 int i;
3292
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003293 if (term->tl_scrollback_postponed.ga_len == 0)
3294 return;
3295 ch_log(NULL, "Moving postponed scrollback to scrollback");
3296
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003297 // First remove the lines that were appended before, the pushed lines go
3298 // above it.
3299 cleanup_scrollback(term);
3300
3301 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3302 {
3303 char_u *text;
3304 sb_line_T *pp_line;
3305 sb_line_T *line;
3306
3307 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3308 break;
3309 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3310
3311 text = pp_line->sb_text;
3312 if (text == NULL)
3313 text = (char_u *)"";
3314 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3315 vim_free(pp_line->sb_text);
3316
3317 line = (sb_line_T *)term->tl_scrollback.ga_data
3318 + term->tl_scrollback.ga_len;
3319 line->sb_cols = pp_line->sb_cols;
3320 line->sb_cells = pp_line->sb_cells;
3321 line->sb_fill_attr = pp_line->sb_fill_attr;
3322 line->sb_text = NULL;
3323 ++term->tl_scrollback_scrolled;
3324 ++term->tl_scrollback.ga_len;
3325 }
3326
3327 ga_clear(&term->tl_scrollback_postponed);
3328 limit_scrollback(term, &term->tl_scrollback, TRUE);
3329}
3330
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003331static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003332 handle_damage, // damage
3333 handle_moverect, // moverect
3334 handle_movecursor, // movecursor
3335 handle_settermprop, // settermprop
3336 NULL, // bell
3337 handle_resize, // resize
3338 handle_pushline, // sb_pushline
3339 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003340};
3341
3342/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003343 * Do the work after the channel of a terminal was closed.
3344 * Must be called only when updating_screen is FALSE.
3345 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3346 */
3347 static int
3348term_after_channel_closed(term_T *term)
3349{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003350 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003351 if (!term->tl_normal_mode)
3352 {
3353 int fnum = term->tl_buffer->b_fnum;
3354
3355 cleanup_vterm(term);
3356
3357 if (term->tl_finish == TL_FINISH_CLOSE)
3358 {
3359 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003360 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003361#ifdef FEAT_PROP_POPUP
3362 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003363
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003364 // If this was a terminal in a popup window, go back to the
3365 // previous window.
3366 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3367 {
3368 pwin = curwin;
3369 if (win_valid(prevwin))
3370 win_enter(prevwin, FALSE);
3371 }
3372 else
3373#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003374 // If this is the last normal window: exit Vim.
3375 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3376 {
3377 exarg_T ea;
3378
Bram Moolenaara80faa82020-04-12 19:37:17 +02003379 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003380 ex_quit(&ea);
3381 return TRUE;
3382 }
3383
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003384 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003385 ch_log(NULL, "terminal job finished, closing window");
3386 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003387 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003388 if (curwin == aucmd_win)
3389 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003390 if (do_set_w_closing)
3391 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003392 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003393 if (do_set_w_closing)
3394 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003395 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003396#ifdef FEAT_PROP_POPUP
3397 if (pwin != NULL)
3398 popup_close_with_retval(pwin, 0);
3399#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003400 return TRUE;
3401 }
3402 if (term->tl_finish == TL_FINISH_OPEN
3403 && term->tl_buffer->b_nwindows == 0)
3404 {
3405 char buf[50];
3406
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003407 // TODO: use term_opencmd
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003408 ch_log(NULL, "terminal job finished, opening window");
3409 vim_snprintf(buf, sizeof(buf),
3410 term->tl_opencmd == NULL
3411 ? "botright sbuf %d"
3412 : (char *)term->tl_opencmd, fnum);
3413 do_cmdline_cmd((char_u *)buf);
3414 }
3415 else
3416 ch_log(NULL, "terminal job finished");
3417 }
3418
3419 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3420 return FALSE;
3421}
3422
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003423#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3424/*
3425 * If the current window is a terminal in a popup window and the job has
3426 * finished, close the popup window and to back to the previous window.
3427 * Otherwise return FAIL.
3428 */
3429 int
3430may_close_term_popup(void)
3431{
3432 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3433 && !term_job_running(curbuf->b_term))
3434 {
3435 win_T *pwin = curwin;
3436
3437 if (win_valid(prevwin))
3438 win_enter(prevwin, FALSE);
3439 popup_close_with_retval(pwin, 0);
3440 return OK;
3441 }
3442 return FAIL;
3443}
3444#endif
3445
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003446/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003447 * Called when a channel has been closed.
3448 * If this was a channel for a terminal window then finish it up.
3449 */
3450 void
3451term_channel_closed(channel_T *ch)
3452{
3453 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003454 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003455 int did_one = FALSE;
3456
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003457 for (term = first_term; term != NULL; term = next_term)
3458 {
3459 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003460 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003461 {
3462 term->tl_channel_closed = TRUE;
3463 did_one = TRUE;
3464
Bram Moolenaard23a8232018-02-10 18:45:26 +01003465 VIM_CLEAR(term->tl_title);
3466 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003467#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003468 if (term->tl_out_fd != NULL)
3469 {
3470 fclose(term->tl_out_fd);
3471 term->tl_out_fd = NULL;
3472 }
3473#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003474
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003475 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003476 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003477 // Cannot open or close windows now. Can happen when
3478 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003479 term->tl_channel_recently_closed = TRUE;
3480 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003481 }
3482
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003483 if (term_after_channel_closed(term))
3484 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003485 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003486 }
3487
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003488 if (did_one)
3489 {
3490 redraw_statuslines();
3491
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003492 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003493 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003494 typebuf_was_filled = TRUE;
3495
3496 term = curbuf->b_term;
3497 if (term != NULL)
3498 {
3499 if (term->tl_job == ch->ch_job)
3500 maketitle();
3501 update_cursor(term, term->tl_cursor_visible);
3502 }
3503 }
3504}
3505
3506/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003507 * To be called after resetting updating_screen: handle any terminal where the
3508 * channel was closed.
3509 */
3510 void
3511term_check_channel_closed_recently()
3512{
3513 term_T *term;
3514 term_T *next_term;
3515
3516 for (term = first_term; term != NULL; term = next_term)
3517 {
3518 next_term = term->tl_next;
3519 if (term->tl_channel_recently_closed)
3520 {
3521 term->tl_channel_recently_closed = FALSE;
3522 if (term_after_channel_closed(term))
3523 // start over, the list may have changed
3524 next_term = first_term;
3525 }
3526 }
3527}
3528
3529/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003530 * Fill one screen line from a line of the terminal.
3531 * Advances "pos" to past the last column.
3532 */
3533 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003534term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003535 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003536 win_T *wp,
3537 VTermScreen *screen,
3538 VTermPos *pos,
3539 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003540{
3541 int off = screen_get_current_line_off();
3542
3543 for (pos->col = 0; pos->col < max_col; )
3544 {
3545 VTermScreenCell cell;
3546 int c;
3547
3548 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003549 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003550
3551 c = cell.chars[0];
3552 if (c == NUL)
3553 {
3554 ScreenLines[off] = ' ';
3555 if (enc_utf8)
3556 ScreenLinesUC[off] = NUL;
3557 }
3558 else
3559 {
3560 if (enc_utf8)
3561 {
3562 int i;
3563
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003564 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003565 for (i = 0; i < Screen_mco
3566 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3567 {
3568 ScreenLinesC[i][off] = cell.chars[i + 1];
3569 if (cell.chars[i + 1] == 0)
3570 break;
3571 }
3572 if (c >= 0x80 || (Screen_mco > 0
3573 && ScreenLinesC[0][off] != 0))
3574 {
3575 ScreenLines[off] = ' ';
3576 ScreenLinesUC[off] = c;
3577 }
3578 else
3579 {
3580 ScreenLines[off] = c;
3581 ScreenLinesUC[off] = NUL;
3582 }
3583 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003584#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003585 else if (has_mbyte && c >= 0x80)
3586 {
3587 char_u mb[MB_MAXBYTES+1];
3588 WCHAR wc = c;
3589
3590 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3591 (char*)mb, 2, 0, 0) > 1)
3592 {
3593 ScreenLines[off] = mb[0];
3594 ScreenLines[off + 1] = mb[1];
3595 cell.width = mb_ptr2cells(mb);
3596 }
3597 else
3598 ScreenLines[off] = c;
3599 }
3600#endif
3601 else
3602 ScreenLines[off] = c;
3603 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003604 ScreenAttrs[off] = cell2attr(term, wp, cell.attrs, cell.fg, cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003605
3606 ++pos->col;
3607 ++off;
3608 if (cell.width == 2)
3609 {
3610 if (enc_utf8)
3611 ScreenLinesUC[off] = NUL;
3612
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003613 // don't set the second byte to NUL for a DBCS encoding, it
3614 // has been set above
Bram Moolenaar13568252018-03-16 20:46:58 +01003615 if (enc_utf8 || !has_mbyte)
3616 ScreenLines[off] = NUL;
3617
3618 ++pos->col;
3619 ++off;
3620 }
3621 }
3622}
3623
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003624#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003625 static void
3626update_system_term(term_T *term)
3627{
3628 VTermPos pos;
3629 VTermScreen *screen;
3630
3631 if (term->tl_vterm == NULL)
3632 return;
3633 screen = vterm_obtain_screen(term->tl_vterm);
3634
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003635 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003636 while (term->tl_toprow > 0
3637 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3638 {
3639 int save_p_more = p_more;
3640
3641 p_more = FALSE;
3642 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003643 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003644 p_more = save_p_more;
3645 --term->tl_toprow;
3646 }
3647
3648 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3649 && pos.row < Rows; ++pos.row)
3650 {
3651 if (pos.row < term->tl_rows)
3652 {
3653 int max_col = MIN(Columns, term->tl_cols);
3654
Bram Moolenaar83d47902020-03-26 20:34:00 +01003655 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003656 }
3657 else
3658 pos.col = 0;
3659
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003660 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003661 }
3662
3663 term->tl_dirty_row_start = MAX_ROW;
3664 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003665}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003666#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003667
3668/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003669 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3670 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003671 * Terminal-Normal mode.
3672 */
3673 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003674term_do_update_window(win_T *wp)
3675{
3676 term_T *term = wp->w_buffer->b_term;
3677
3678 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3679}
3680
3681/*
3682 * Called to update a window that contains an active terminal.
3683 */
3684 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003685term_update_window(win_T *wp)
3686{
3687 term_T *term = wp->w_buffer->b_term;
3688 VTerm *vterm;
3689 VTermScreen *screen;
3690 VTermState *state;
3691 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003692 int rows, cols;
3693 int newrows, newcols;
3694 int minsize;
3695 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003696
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003697 vterm = term->tl_vterm;
3698 screen = vterm_obtain_screen(vterm);
3699 state = vterm_obtain_state(vterm);
3700
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003701 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3702 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003703 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003704 {
3705 term->tl_dirty_row_start = 0;
3706 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003707
3708 if (term->tl_postponed_scroll > 0
3709 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003710 // Scrolling is usually faster than redrawing, when there are only
3711 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003712 term_scroll_up(term, 0, term->tl_postponed_scroll);
3713 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003714 }
3715
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003716 /*
3717 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003718 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003719 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003720 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003721
Bram Moolenaar498c2562018-04-15 23:45:15 +02003722 newrows = 99999;
3723 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003724 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003725 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003726 // Always use curwin, it may be a popup window.
3727 win_T *wwp = twp == NULL ? curwin : twp;
3728
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003729 // When more than one window shows the same terminal, use the
3730 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003731 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003732 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003733 newrows = MIN(newrows, wwp->w_height);
3734 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003735 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003736 if (twp == NULL)
3737 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003738 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003739 if (newrows == 99999 || newcols == 99999)
3740 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003741 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3742 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3743
3744 if (term->tl_rows != newrows || term->tl_cols != newcols)
3745 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003746 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003747 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003748 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003749 newrows);
3750 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003751
3752 // Updating the terminal size will cause the snapshot to be cleared.
3753 // When not in terminal_loop() we need to restore it.
3754 if (term != in_terminal_loop)
3755 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003756 }
3757
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003758 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003759 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003760 position_cursor(wp, &pos, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003761
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003762 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3763 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003764 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003765 if (pos.row < term->tl_rows)
3766 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003767 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003768
Bram Moolenaar83d47902020-03-26 20:34:00 +01003769 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003770 }
3771 else
3772 pos.col = 0;
3773
Bram Moolenaarf118d482018-03-13 13:14:00 +01003774 screen_line(wp->w_winrow + pos.row
3775#ifdef FEAT_MENU
3776 + winbar_height(wp)
3777#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003778 , wp->w_wincol, pos.col, wp->w_width,
3779#ifdef FEAT_PROP_POPUP
3780 popup_is_popup(wp) ? SLF_POPUP :
3781#endif
3782 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003783 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003784 term->tl_dirty_row_start = MAX_ROW;
3785 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003786}
3787
3788/*
3789 * Return TRUE if "wp" is a terminal window where the job has finished.
3790 */
3791 int
3792term_is_finished(buf_T *buf)
3793{
3794 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3795}
3796
3797/*
3798 * Return TRUE if "wp" is a terminal window where the job has finished or we
3799 * are in Terminal-Normal mode, thus we show the buffer contents.
3800 */
3801 int
3802term_show_buffer(buf_T *buf)
3803{
3804 term_T *term = buf->b_term;
3805
3806 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3807}
3808
3809/*
3810 * The current buffer is going to be changed. If there is terminal
3811 * highlighting remove it now.
3812 */
3813 void
3814term_change_in_curbuf(void)
3815{
3816 term_T *term = curbuf->b_term;
3817
3818 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3819 {
3820 free_scrollback(term);
3821 redraw_buf_later(term->tl_buffer, NOT_VALID);
3822
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003823 // The buffer is now like a normal buffer, it cannot be easily
3824 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003825 set_string_option_direct((char_u *)"buftype", -1,
3826 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3827 }
3828}
3829
3830/*
3831 * Get the screen attribute for a position in the buffer.
3832 * Use a negative "col" to get the filler background color.
3833 */
3834 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003835term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003836{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003837 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003838 term_T *term = buf->b_term;
3839 sb_line_T *line;
3840 cellattr_T *cellattr;
3841
3842 if (lnum > term->tl_scrollback.ga_len)
3843 cellattr = &term->tl_default_color;
3844 else
3845 {
3846 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3847 if (col < 0 || col >= line->sb_cols)
3848 cellattr = &line->sb_fill_attr;
3849 else
3850 cellattr = line->sb_cells + col;
3851 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003852 return cell2attr(term, wp, cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003853}
3854
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003855/*
3856 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003857 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003858 */
3859 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003860cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003861{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003862 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3863 if (rgb->index == 0)
3864 rgb->type = VTERM_COLOR_RGB;
3865 else
3866 {
3867 rgb->type = VTERM_COLOR_INDEXED;
3868 --rgb->index;
3869 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003870}
3871
3872/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003873 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003874 */
3875 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003876init_default_colors(term_T *term, win_T *wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003877{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003878 VTermColor *fg, *bg;
3879 int fgval, bgval;
3880 int id;
3881
Bram Moolenaara80faa82020-04-12 19:37:17 +02003882 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003883 term->tl_default_color.width = 1;
3884 fg = &term->tl_default_color.fg;
3885 bg = &term->tl_default_color.bg;
3886
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003887 // Vterm uses a default black background. Set it to white when
3888 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003889 if (*p_bg == 'l')
3890 {
3891 fgval = 0;
3892 bgval = 255;
3893 }
3894 else
3895 {
3896 fgval = 255;
3897 bgval = 0;
3898 }
3899 fg->red = fg->green = fg->blue = fgval;
3900 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003901 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3902 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003903
Bram Moolenaar83d47902020-03-26 20:34:00 +01003904 // The 'wincolor' or the highlight group overrules the defaults.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003905 if (wp != NULL && *wp->w_p_wcr != NUL)
3906 id = syn_name2id(wp->w_p_wcr);
3907 else
Bram Moolenaar83d47902020-03-26 20:34:00 +01003908 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003909
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003910 // Use the actual color for the GUI and when 'termguicolors' is set.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003911#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3912 if (0
3913# ifdef FEAT_GUI
3914 || gui.in_use
3915# endif
3916# ifdef FEAT_TERMGUICOLORS
3917 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003918# ifdef FEAT_VTP
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003919 // Finally get INVALCOLOR on this execution path
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003920 || (!p_tgc && t_colors >= 256)
3921# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003922# endif
3923 )
3924 {
3925 guicolor_T fg_rgb = INVALCOLOR;
3926 guicolor_T bg_rgb = INVALCOLOR;
3927
3928 if (id != 0)
3929 syn_id2colors(id, &fg_rgb, &bg_rgb);
3930
3931# ifdef FEAT_GUI
3932 if (gui.in_use)
3933 {
3934 if (fg_rgb == INVALCOLOR)
3935 fg_rgb = gui.norm_pixel;
3936 if (bg_rgb == INVALCOLOR)
3937 bg_rgb = gui.back_pixel;
3938 }
3939# ifdef FEAT_TERMGUICOLORS
3940 else
3941# endif
3942# endif
3943# ifdef FEAT_TERMGUICOLORS
3944 {
3945 if (fg_rgb == INVALCOLOR)
3946 fg_rgb = cterm_normal_fg_gui_color;
3947 if (bg_rgb == INVALCOLOR)
3948 bg_rgb = cterm_normal_bg_gui_color;
3949 }
3950# endif
3951 if (fg_rgb != INVALCOLOR)
3952 {
3953 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3954
3955 fg->red = (unsigned)(rgb >> 16);
3956 fg->green = (unsigned)(rgb >> 8) & 255;
3957 fg->blue = (unsigned)rgb & 255;
3958 }
3959 if (bg_rgb != INVALCOLOR)
3960 {
3961 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
3962
3963 bg->red = (unsigned)(rgb >> 16);
3964 bg->green = (unsigned)(rgb >> 8) & 255;
3965 bg->blue = (unsigned)rgb & 255;
3966 }
3967 }
3968 else
3969#endif
3970 if (id != 0 && t_colors >= 16)
3971 {
Bram Moolenaar83d47902020-03-26 20:34:00 +01003972 int cterm_fg = get_default_cterm_fg(term);
3973 int cterm_bg = get_default_cterm_bg(term);
3974
3975 if (cterm_fg >= 0)
3976 cterm_color2vterm(cterm_fg, fg);
3977 if (cterm_bg >= 0)
3978 cterm_color2vterm(cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003979 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003980 else
3981 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003982#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003983 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003984#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003985
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003986 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003987 if (cterm_normal_fg_color > 0)
3988 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003989 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003990# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3991# ifdef VIMDLL
3992 if (!gui.in_use)
3993# endif
3994 {
3995 tmp = fg->red;
3996 fg->red = fg->blue;
3997 fg->blue = tmp;
3998 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003999# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004000 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004001# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004002 else
4003 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004004# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004005
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004006 if (cterm_normal_bg_color > 0)
4007 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004008 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004009# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4010# ifdef VIMDLL
4011 if (!gui.in_use)
4012# endif
4013 {
4014 tmp = fg->red;
4015 fg->red = fg->blue;
4016 fg->blue = tmp;
4017 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004018# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004019 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004020# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004021 else
4022 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004023# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004024 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004025}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004026
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004027#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4028/*
4029 * Set the 16 ANSI colors from array of RGB values
4030 */
4031 static void
4032set_vterm_palette(VTerm *vterm, long_u *rgb)
4033{
4034 int index = 0;
4035 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004036
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004037 for (; index < 16; index++)
4038 {
4039 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004040
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004041 color.red = (unsigned)(rgb[index] >> 16);
4042 color.green = (unsigned)(rgb[index] >> 8) & 255;
4043 color.blue = (unsigned)rgb[index] & 255;
4044 vterm_state_set_palette_color(state, index, &color);
4045 }
4046}
4047
4048/*
4049 * Set the ANSI color palette from a list of colors
4050 */
4051 static int
4052set_ansi_colors_list(VTerm *vterm, list_T *list)
4053{
4054 int n = 0;
4055 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004056 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004057
Bram Moolenaarb0992022020-01-30 14:55:42 +01004058 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004059 {
4060 char_u *color_name;
4061 guicolor_T guicolor;
4062
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004063 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004064 if (color_name == NULL)
4065 return FAIL;
4066
4067 guicolor = GUI_GET_COLOR(color_name);
4068 if (guicolor == INVALCOLOR)
4069 return FAIL;
4070
4071 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4072 }
4073
4074 if (n != 16 || li != NULL)
4075 return FAIL;
4076
4077 set_vterm_palette(vterm, rgb);
4078
4079 return OK;
4080}
4081
4082/*
4083 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4084 */
4085 static void
4086init_vterm_ansi_colors(VTerm *vterm)
4087{
4088 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4089
4090 if (var != NULL
4091 && (var->di_tv.v_type != VAR_LIST
4092 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004093 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004094 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004095 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004096}
4097#endif
4098
Bram Moolenaar52acb112018-03-18 19:20:22 +01004099/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004100 * Handles a "drop" command from the job in the terminal.
4101 * "item" is the file name, "item->li_next" may have options.
4102 */
4103 static void
4104handle_drop_command(listitem_T *item)
4105{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004106 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004107 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004108 int bufnr;
4109 win_T *wp;
4110 tabpage_T *tp;
4111 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004112 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004113
4114 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4115 FOR_ALL_TAB_WINDOWS(tp, wp)
4116 {
4117 if (wp->w_buffer->b_fnum == bufnr)
4118 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004119 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004120 goto_tabpage_win(tp, wp);
4121 return;
4122 }
4123 }
4124
Bram Moolenaara80faa82020-04-12 19:37:17 +02004125 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004126
4127 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4128 && opt_item->li_tv.vval.v_dict != NULL)
4129 {
4130 dict_T *dict = opt_item->li_tv.vval.v_dict;
4131 char_u *p;
4132
Bram Moolenaar8f667172018-12-14 15:38:31 +01004133 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004134 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004135 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004136 if (p != NULL)
4137 {
4138 if (check_ff_value(p) == FAIL)
4139 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4140 else
4141 ea.force_ff = *p;
4142 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004143 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004144 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004145 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004146 if (p != NULL)
4147 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004148 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004149 if (ea.cmd != NULL)
4150 {
4151 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4152 ea.force_enc = 11;
4153 tofree = ea.cmd;
4154 }
4155 }
4156
Bram Moolenaar8f667172018-12-14 15:38:31 +01004157 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004158 if (p != NULL)
4159 get_bad_opt(p, &ea);
4160
4161 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4162 ea.force_bin = FORCE_BIN;
4163 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4164 ea.force_bin = FORCE_BIN;
4165 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4166 ea.force_bin = FORCE_NOBIN;
4167 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4168 ea.force_bin = FORCE_NOBIN;
4169 }
4170
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004171 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004172 if (ea.cmd == NULL)
4173 ea.cmd = (char_u *)"split";
4174 ea.arg = fname;
4175 ea.cmdidx = CMD_split;
4176 ex_splitview(&ea);
4177
4178 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004179}
4180
4181/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004182 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4183 */
4184 static int
4185is_permitted_term_api(char_u *func, char_u *pat)
4186{
4187 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4188}
4189
4190/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004191 * Handles a function call from the job running in a terminal.
4192 * "item" is the function name, "item->li_next" has the arguments.
4193 */
4194 static void
4195handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4196{
4197 char_u *func;
4198 typval_T argvars[2];
4199 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004200 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004201
4202 if (item->li_next == NULL)
4203 {
4204 ch_log(channel, "Missing function arguments for call");
4205 return;
4206 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004207 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004208
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004209 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004210 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004211 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004212 return;
4213 }
4214
4215 argvars[0].v_type = VAR_NUMBER;
4216 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4217 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004218 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004219 funcexe.firstline = 1L;
4220 funcexe.lastline = 1L;
4221 funcexe.evaluate = TRUE;
4222 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004223 {
4224 clear_tv(&rettv);
4225 ch_log(channel, "Function %s called", func);
4226 }
4227 else
4228 ch_log(channel, "Calling function %s failed", func);
4229}
4230
4231/*
4232 * Called by libvterm when it cannot recognize an OSC sequence.
4233 * We recognize a terminal API command.
4234 */
4235 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004236parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004237{
4238 term_T *term = (term_T *)user;
4239 js_read_T reader;
4240 typval_T tv;
4241 channel_T *channel = term->tl_job == NULL ? NULL
4242 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004243 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004244
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004245 // We recognize only OSC 5 1 ; {command}
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004246 if (command != 51)
4247 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004248
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004249 // Concatenate what was received until the final piece is found.
4250 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4251 {
4252 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004253 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004254 }
4255 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004256 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004257 if (!frag.final)
4258 return 1;
4259
4260 ((char *)gap->ga_data)[gap->ga_len] = 0;
4261 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004262 reader.js_fill = NULL;
4263 reader.js_used = 0;
4264 if (json_decode(&reader, &tv, 0) == OK
4265 && tv.v_type == VAR_LIST
4266 && tv.vval.v_list != NULL)
4267 {
4268 listitem_T *item = tv.vval.v_list->lv_first;
4269
4270 if (item == NULL)
4271 ch_log(channel, "Missing command");
4272 else
4273 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004274 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004275
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004276 // Make sure an invoked command doesn't delete the buffer (and the
4277 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004278 ++term->tl_buffer->b_locked;
4279
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004280 item = item->li_next;
4281 if (item == NULL)
4282 ch_log(channel, "Missing argument for %s", cmd);
4283 else if (STRCMP(cmd, "drop") == 0)
4284 handle_drop_command(item);
4285 else if (STRCMP(cmd, "call") == 0)
4286 handle_call_command(term, channel, item);
4287 else
4288 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004289 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004290 }
4291 }
4292 else
4293 ch_log(channel, "Invalid JSON received");
4294
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004295 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004296 clear_tv(&tv);
4297 return 1;
4298}
4299
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004300/*
4301 * Called by libvterm when it cannot recognize a CSI sequence.
4302 * We recognize the window position report.
4303 */
4304 static int
4305parse_csi(
4306 const char *leader UNUSED,
4307 const long args[],
4308 int argcount,
4309 const char *intermed UNUSED,
4310 char command,
4311 void *user)
4312{
4313 term_T *term = (term_T *)user;
4314 char buf[100];
4315 int len;
4316 int x = 0;
4317 int y = 0;
4318 win_T *wp;
4319
4320 // We recognize only CSI 13 t
4321 if (command != 't' || argcount != 1 || args[0] != 13)
4322 return 0; // not handled
4323
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004324 // When getting the window position is not possible or it fails it results
4325 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004326#if defined(FEAT_GUI) \
4327 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4328 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004329 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004330#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004331
4332 FOR_ALL_WINDOWS(wp)
4333 if (wp->w_buffer == term->tl_buffer)
4334 break;
4335 if (wp != NULL)
4336 {
4337#ifdef FEAT_GUI
4338 if (gui.in_use)
4339 {
4340 x += wp->w_wincol * gui.char_width;
4341 y += W_WINROW(wp) * gui.char_height;
4342 }
4343 else
4344#endif
4345 {
4346 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004347 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004348 x += wp->w_wincol * 7;
4349 y += W_WINROW(wp) * 10;
4350 }
4351 }
4352
4353 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4354 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4355 (char_u *)buf, len, NULL);
4356 return 1;
4357}
4358
Bram Moolenaard8637282020-05-20 18:41:41 +02004359static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004360 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004361 parse_csi, // csi
4362 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004363 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004364};
4365
4366/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004367 * Use Vim's allocation functions for vterm so profiling works.
4368 */
4369 static void *
4370vterm_malloc(size_t size, void *data UNUSED)
4371{
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004372 return alloc_clear(size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004373}
4374
4375 static void
4376vterm_memfree(void *ptr, void *data UNUSED)
4377{
4378 vim_free(ptr);
4379}
4380
4381static VTermAllocatorFunctions vterm_allocator = {
4382 &vterm_malloc,
4383 &vterm_memfree
4384};
4385
4386/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004387 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004388 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004389 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004390 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004391create_vterm(term_T *term, int rows, int cols)
4392{
4393 VTerm *vterm;
4394 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004395 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004396 VTermValue value;
4397
Bram Moolenaar756ef112018-04-10 12:04:27 +02004398 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004399 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004400 if (vterm == NULL)
4401 return FAIL;
4402
4403 // Allocate screen and state here, so we can bail out if that fails.
4404 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004405 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004406 if (state == NULL || screen == NULL)
4407 {
4408 vterm_free(vterm);
4409 return FAIL;
4410 }
4411
Bram Moolenaar52acb112018-03-18 19:20:22 +01004412 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004413 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004414 vterm_set_utf8(vterm, 1);
4415
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004416 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004417
4418 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004419 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004420 &term->tl_default_color.fg,
4421 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004422
Bram Moolenaar9e587872019-05-13 20:27:23 +02004423 if (t_colors < 16)
4424 // Less than 16 colors: assume that bold means using a bright color for
4425 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004426 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4427
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004428 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004429 vterm_screen_reset(screen, 1 /* hard */);
4430
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004431 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004432 vterm_screen_enable_altscreen(screen, 1);
4433
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004434 // For unix do not use a blinking cursor. In an xterm this causes the
4435 // cursor to blink if it's blinking in the xterm.
4436 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004437#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004438 if (GetCaretBlinkTime() == INFINITE)
4439 value.boolean = 0;
4440 else
4441 value.boolean = 1;
4442#else
4443 value.boolean = 0;
4444#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004445 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004446 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004447
4448 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004449}
4450
4451/*
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004452 * Called when 'wincolor' was set.
4453 */
4454 void
4455term_update_colors(void)
4456{
4457 term_T *term = curwin->w_buffer->b_term;
4458
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01004459 if (term->tl_vterm == NULL)
4460 return;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004461 init_default_colors(term, curwin);
4462 vterm_state_set_default_colors(
4463 vterm_obtain_state(term->tl_vterm),
4464 &term->tl_default_color.fg,
4465 &term->tl_default_color.bg);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01004466
4467 redraw_later(NOT_VALID);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004468}
4469
4470/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004471 * Return the text to show for the buffer name and status.
4472 */
4473 char_u *
4474term_get_status_text(term_T *term)
4475{
4476 if (term->tl_status_text == NULL)
4477 {
4478 char_u *txt;
4479 size_t len;
4480
4481 if (term->tl_normal_mode)
4482 {
4483 if (term_job_running(term))
4484 txt = (char_u *)_("Terminal");
4485 else
4486 txt = (char_u *)_("Terminal-finished");
4487 }
4488 else if (term->tl_title != NULL)
4489 txt = term->tl_title;
4490 else if (term_none_open(term))
4491 txt = (char_u *)_("active");
4492 else if (term_job_running(term))
4493 txt = (char_u *)_("running");
4494 else
4495 txt = (char_u *)_("finished");
4496 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004497 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004498 if (term->tl_status_text != NULL)
4499 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
4500 term->tl_buffer->b_fname, txt);
4501 }
4502 return term->tl_status_text;
4503}
4504
4505/*
4506 * Mark references in jobs of terminals.
4507 */
4508 int
4509set_ref_in_term(int copyID)
4510{
4511 int abort = FALSE;
4512 term_T *term;
4513 typval_T tv;
4514
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004515 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004516 if (term->tl_job != NULL)
4517 {
4518 tv.v_type = VAR_JOB;
4519 tv.vval.v_job = term->tl_job;
4520 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4521 }
4522 return abort;
4523}
4524
4525/*
4526 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004527 * Returns NULL when the buffer is not for a terminal window and logs a message
4528 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004529 */
4530 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004531term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004532{
4533 buf_T *buf;
4534
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004535 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004536 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004537 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004538 --emsg_off;
4539 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004540 {
4541 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004542 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004543 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004544 return buf;
4545}
4546
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004547 static void
4548clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004549{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004550 CLEAR_FIELD(*cell);
4551 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4552 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004553}
4554
4555 static void
4556dump_term_color(FILE *fd, VTermColor *color)
4557{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004558 int index;
4559
4560 if (VTERM_COLOR_IS_INDEXED(color))
4561 index = color->index + 1;
4562 else if (color->type == 0)
4563 // use RGB values
4564 index = 255;
4565 else
4566 // default color
4567 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004568 fprintf(fd, "%02x%02x%02x%d",
4569 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004570 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004571}
4572
4573/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004574 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004575 *
4576 * Each screen cell in full is:
4577 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4578 * {characters} is a space for an empty cell
4579 * For a double-width character "+" is changed to "*" and the next cell is
4580 * skipped.
4581 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4582 * when "&" use the same as the previous cell.
4583 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4584 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4585 * {color-idx} is a number from 0 to 255
4586 *
4587 * Screen cell with same width, attributes and color as the previous one:
4588 * |{characters}
4589 *
4590 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4591 *
4592 * Repeating the previous screen cell:
4593 * @{count}
4594 */
4595 void
4596f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4597{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004598 buf_T *buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004599 term_T *term;
4600 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004601 int max_height = 0;
4602 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004603 stat_T st;
4604 FILE *fd;
4605 VTermPos pos;
4606 VTermScreen *screen;
4607 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004608 VTermState *state;
4609 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004610
4611 if (check_restricted() || check_secure())
4612 return;
4613 if (buf == NULL)
4614 return;
4615 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004616 if (term->tl_vterm == NULL)
4617 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004618 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004619 return;
4620 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004621
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004622 if (argvars[2].v_type != VAR_UNKNOWN)
4623 {
4624 dict_T *d;
4625
4626 if (argvars[2].v_type != VAR_DICT)
4627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004628 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004629 return;
4630 }
4631 d = argvars[2].vval.v_dict;
4632 if (d != NULL)
4633 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004634 max_height = dict_get_number(d, (char_u *)"rows");
4635 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004636 }
4637 }
4638
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004639 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004640 if (fname == NULL)
4641 return;
4642 if (mch_stat((char *)fname, &st) >= 0)
4643 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004644 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004645 return;
4646 }
4647
Bram Moolenaard96ff162018-02-18 22:13:29 +01004648 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004650 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004651 return;
4652 }
4653
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004654 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004655
4656 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004657 state = vterm_obtain_state(term->tl_vterm);
4658 vterm_state_get_cursorpos(state, &cursor_pos);
4659
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004660 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4661 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004662 {
4663 int repeat = 0;
4664
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004665 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4666 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004667 {
4668 VTermScreenCell cell;
4669 int same_attr;
4670 int same_chars = TRUE;
4671 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004672 int is_cursor_pos = (pos.col == cursor_pos.col
4673 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004674
4675 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004676 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004677
4678 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4679 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004680 int c = cell.chars[i];
4681 int pc = prev_cell.chars[i];
4682
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004683 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004684 if (i == 0)
4685 {
4686 c = (c == NUL) ? ' ' : c;
4687 pc = (pc == NUL) ? ' ' : pc;
4688 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004689 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004690 same_chars = FALSE;
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004691 if (c == NUL || pc == NUL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004692 break;
4693 }
4694 same_attr = vtermAttr2hl(cell.attrs)
4695 == vtermAttr2hl(prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004696 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4697 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004698 if (same_chars && cell.width == prev_cell.width && same_attr
4699 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004700 {
4701 ++repeat;
4702 }
4703 else
4704 {
4705 if (repeat > 0)
4706 {
4707 fprintf(fd, "@%d", repeat);
4708 repeat = 0;
4709 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004710 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004711
4712 if (cell.chars[0] == NUL)
4713 fputs(" ", fd);
4714 else
4715 {
4716 char_u charbuf[10];
4717 int len;
4718
4719 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4720 && cell.chars[i] != NUL; ++i)
4721 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004722 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004723 fwrite(charbuf, len, 1, fd);
4724 }
4725 }
4726
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004727 // When only the characters differ we don't write anything, the
4728 // following "|", "@" or NL will indicate using the same
4729 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004730 if (cell.width != prev_cell.width || !same_attr)
4731 {
4732 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004733 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004734 else
4735 fputs("+", fd);
4736
4737 if (same_attr)
4738 {
4739 fputs("&", fd);
4740 }
4741 else
4742 {
4743 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004744 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004745 fputs("&", fd);
4746 else
4747 {
4748 fputs("#", fd);
4749 dump_term_color(fd, &cell.fg);
4750 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004751 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004752 fputs("&", fd);
4753 else
4754 {
4755 fputs("#", fd);
4756 dump_term_color(fd, &cell.bg);
4757 }
4758 }
4759 }
4760
4761 prev_cell = cell;
4762 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004763
4764 if (cell.width == 2)
4765 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004766 }
4767 if (repeat > 0)
4768 fprintf(fd, "@%d", repeat);
4769 fputs("\n", fd);
4770 }
4771
4772 fclose(fd);
4773}
4774
4775/*
4776 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4777 */
4778 static void
4779dump_is_corrupt(garray_T *gap)
4780{
4781 ga_concat(gap, (char_u *)"CORRUPT");
4782}
4783
4784 static void
4785append_cell(garray_T *gap, cellattr_T *cell)
4786{
4787 if (ga_grow(gap, 1) == OK)
4788 {
4789 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4790 ++gap->ga_len;
4791 }
4792}
4793
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004794 static void
4795clear_cellattr(cellattr_T *cell)
4796{
4797 CLEAR_FIELD(*cell);
4798 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4799 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4800}
4801
Bram Moolenaard96ff162018-02-18 22:13:29 +01004802/*
4803 * Read the dump file from "fd" and append lines to the current buffer.
4804 * Return the cell width of the longest line.
4805 */
4806 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004807read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004808{
4809 int c;
4810 garray_T ga_text;
4811 garray_T ga_cell;
4812 char_u *prev_char = NULL;
4813 int attr = 0;
4814 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004815 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004816 term_T *term = curbuf->b_term;
4817 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004818 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004819
4820 ga_init2(&ga_text, 1, 90);
4821 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004822 clear_cellattr(&cell);
4823 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004824 cursor_pos->row = -1;
4825 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004826
4827 c = fgetc(fd);
4828 for (;;)
4829 {
4830 if (c == EOF)
4831 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004832 if (c == '\r')
4833 {
4834 // DOS line endings? Ignore.
4835 c = fgetc(fd);
4836 }
4837 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004838 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004839 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004840 if (ga_text.ga_data == NULL)
4841 dump_is_corrupt(&ga_text);
4842 if (ga_grow(&term->tl_scrollback, 1) == OK)
4843 {
4844 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
4845 + term->tl_scrollback.ga_len;
4846
4847 if (max_cells < ga_cell.ga_len)
4848 max_cells = ga_cell.ga_len;
4849 line->sb_cols = ga_cell.ga_len;
4850 line->sb_cells = ga_cell.ga_data;
4851 line->sb_fill_attr = term->tl_default_color;
4852 ++term->tl_scrollback.ga_len;
4853 ga_init(&ga_cell);
4854
4855 ga_append(&ga_text, NUL);
4856 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4857 ga_text.ga_len, FALSE);
4858 }
4859 else
4860 ga_clear(&ga_cell);
4861 ga_text.ga_len = 0;
4862
4863 c = fgetc(fd);
4864 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004865 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004866 {
4867 int prev_len = ga_text.ga_len;
4868
Bram Moolenaar9271d052018-02-25 21:39:46 +01004869 if (c == '>')
4870 {
4871 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004872 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01004873 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
4874 cursor_pos->col = ga_cell.ga_len;
4875 }
4876
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004877 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01004878 c = fgetc(fd);
4879 if (c != EOF)
4880 ga_append(&ga_text, c);
4881 for (;;)
4882 {
4883 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004884 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01004885 || c == EOF || c == '\n')
4886 break;
4887 ga_append(&ga_text, c);
4888 }
4889
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004890 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01004891 vim_free(prev_char);
4892 if (ga_text.ga_data != NULL)
4893 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
4894 ga_text.ga_len - prev_len);
4895
Bram Moolenaar9271d052018-02-25 21:39:46 +01004896 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004897 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004898 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01004899 }
4900 else if (c == '+' || c == '*')
4901 {
4902 int is_bg;
4903
4904 cell.width = c == '+' ? 1 : 2;
4905
4906 c = fgetc(fd);
4907 if (c == '&')
4908 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004909 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01004910 c = fgetc(fd);
4911 }
4912 else if (isdigit(c))
4913 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004914 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01004915 attr = 0;
4916 while (isdigit(c))
4917 {
4918 attr = attr * 10 + (c - '0');
4919 c = fgetc(fd);
4920 }
4921 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004922
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004923 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004924 for (is_bg = 0; is_bg <= 1; ++is_bg)
4925 {
4926 if (c == '&')
4927 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004928 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004929 c = fgetc(fd);
4930 }
4931 else if (c == '#')
4932 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004933 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004934
4935 c = fgetc(fd);
4936 red = hex2nr(c);
4937 c = fgetc(fd);
4938 red = (red << 4) + hex2nr(c);
4939 c = fgetc(fd);
4940 green = hex2nr(c);
4941 c = fgetc(fd);
4942 green = (green << 4) + hex2nr(c);
4943 c = fgetc(fd);
4944 blue = hex2nr(c);
4945 c = fgetc(fd);
4946 blue = (blue << 4) + hex2nr(c);
4947 c = fgetc(fd);
4948 if (!isdigit(c))
4949 dump_is_corrupt(&ga_text);
4950 while (isdigit(c))
4951 {
4952 index = index * 10 + (c - '0');
4953 c = fgetc(fd);
4954 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004955 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004956 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004957 type = VTERM_COLOR_RGB;
4958 if (index == 0)
4959 {
4960 if (is_bg)
4961 type |= VTERM_COLOR_DEFAULT_BG;
4962 else
4963 type |= VTERM_COLOR_DEFAULT_FG;
4964 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004965 }
4966 else
4967 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004968 type = VTERM_COLOR_INDEXED;
4969 index -= 1;
4970 }
4971 if (is_bg)
4972 {
4973 cell.bg.type = type;
4974 cell.bg.red = red;
4975 cell.bg.green = green;
4976 cell.bg.blue = blue;
4977 cell.bg.index = index;
4978 }
4979 else
4980 {
4981 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004982 cell.fg.red = red;
4983 cell.fg.green = green;
4984 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004985 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004986 }
4987 }
4988 else
4989 dump_is_corrupt(&ga_text);
4990 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004991 }
4992 else
4993 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004994 }
4995 else
4996 dump_is_corrupt(&ga_text);
4997
4998 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004999 if (cell.width == 2)
5000 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005001 }
5002 else if (c == '@')
5003 {
5004 if (prev_char == NULL)
5005 dump_is_corrupt(&ga_text);
5006 else
5007 {
5008 int count = 0;
5009
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005010 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005011 for (;;)
5012 {
5013 c = fgetc(fd);
5014 if (!isdigit(c))
5015 break;
5016 count = count * 10 + (c - '0');
5017 }
5018
5019 while (count-- > 0)
5020 {
5021 ga_concat(&ga_text, prev_char);
5022 append_cell(&ga_cell, &cell);
5023 }
5024 }
5025 }
5026 else
5027 {
5028 dump_is_corrupt(&ga_text);
5029 c = fgetc(fd);
5030 }
5031 }
5032
5033 if (ga_text.ga_len > 0)
5034 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005035 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005036 dump_is_corrupt(&ga_text);
5037 ga_append(&ga_text, NUL);
5038 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5039 ga_text.ga_len, FALSE);
5040 }
5041
5042 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005043 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005044 vim_free(prev_char);
5045
5046 return max_cells;
5047}
5048
5049/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005050 * Return an allocated string with at least "text_width" "=" characters and
5051 * "fname" inserted in the middle.
5052 */
5053 static char_u *
5054get_separator(int text_width, char_u *fname)
5055{
5056 int width = MAX(text_width, curwin->w_width);
5057 char_u *textline;
5058 int fname_size;
5059 char_u *p = fname;
5060 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005061 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005062
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005063 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005064 if (textline == NULL)
5065 return NULL;
5066
5067 fname_size = vim_strsize(fname);
5068 if (fname_size < width - 8)
5069 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005070 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005071 width = MAX(text_width, fname_size + 8);
5072 }
5073 else if (fname_size > width - 8)
5074 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005075 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005076 p = gettail(fname);
5077 fname_size = vim_strsize(p);
5078 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005079 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005080 while (fname_size > width - 8)
5081 {
5082 p += (*mb_ptr2len)(p);
5083 fname_size = vim_strsize(p);
5084 }
5085
5086 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5087 textline[i] = '=';
5088 textline[i++] = ' ';
5089
5090 STRCPY(textline + i, p);
5091 off = STRLEN(textline);
5092 textline[off] = ' ';
5093 for (i = 1; i < (width - fname_size) / 2; ++i)
5094 textline[off + i] = '=';
5095 textline[off + i] = NUL;
5096
5097 return textline;
5098}
5099
5100/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005101 * Common for "term_dumpdiff()" and "term_dumpload()".
5102 */
5103 static void
5104term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5105{
5106 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005107 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005108 char_u buf1[NUMBUFLEN];
5109 char_u buf2[NUMBUFLEN];
5110 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005111 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005112 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005113 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005114 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005115 char_u *textline = NULL;
5116
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005117 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005118 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005119 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005120 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005121 if (fname1 == NULL || (do_diff && fname2 == NULL))
5122 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005123 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005124 return;
5125 }
5126 fd1 = mch_fopen((char *)fname1, READBIN);
5127 if (fd1 == NULL)
5128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005129 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005130 return;
5131 }
5132 if (do_diff)
5133 {
5134 fd2 = mch_fopen((char *)fname2, READBIN);
5135 if (fd2 == NULL)
5136 {
5137 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005138 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005139 return;
5140 }
5141 }
5142
5143 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005144 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5145 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5146 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5147 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5148 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005149
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005150 if (opt.jo_term_name == NULL)
5151 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005152 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005153
Bram Moolenaar51e14382019-05-25 20:21:28 +02005154 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005155 if (fname_tofree != NULL)
5156 {
5157 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5158 opt.jo_term_name = fname_tofree;
5159 }
5160 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005161
Bram Moolenaar87abab92019-06-03 21:14:59 +02005162 if (opt.jo_bufnr_buf != NULL)
5163 {
5164 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5165
5166 // With "bufnr" argument: enter the window with this buffer and make it
5167 // empty.
5168 if (wp == NULL)
5169 semsg(_(e_invarg2), "bufnr");
5170 else
5171 {
5172 buf = curbuf;
5173 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005174 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005175 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005176 redraw_later(NOT_VALID);
5177 }
5178 }
5179 else
5180 // Create a new terminal window.
5181 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5182
Bram Moolenaard96ff162018-02-18 22:13:29 +01005183 if (buf != NULL && buf->b_term != NULL)
5184 {
5185 int i;
5186 linenr_T bot_lnum;
5187 linenr_T lnum;
5188 term_T *term = buf->b_term;
5189 int width;
5190 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005191 VTermPos cursor_pos1;
5192 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005193
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005194 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005195
Bram Moolenaard96ff162018-02-18 22:13:29 +01005196 rettv->vval.v_number = buf->b_fnum;
5197
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005198 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005199 width = read_dump_file(fd1, &cursor_pos1);
5200
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005201 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005202 if (cursor_pos1.row >= 0)
5203 {
5204 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5205 coladvance(cursor_pos1.col);
5206 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005207
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005208 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005209 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005210
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005211 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005212 if (!do_diff)
5213 goto theend;
5214
5215 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5216
Bram Moolenaar4a696342018-04-05 18:45:26 +02005217 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005218 if (textline == NULL)
5219 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005220 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5221 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5222 vim_free(textline);
5223
5224 textline = get_separator(width, fname2);
5225 if (textline == NULL)
5226 goto theend;
5227 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5228 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005229 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005230
5231 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005232 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005233 if (width2 > width)
5234 {
5235 vim_free(textline);
5236 textline = alloc(width2 + 1);
5237 if (textline == NULL)
5238 goto theend;
5239 width = width2;
5240 textline[width] = NUL;
5241 }
5242 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5243
5244 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5245 {
5246 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5247 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005248 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005249 for (i = 0; i < width; ++i)
5250 textline[i] = '-';
5251 }
5252 else
5253 {
5254 char_u *line1;
5255 char_u *line2;
5256 char_u *p1;
5257 char_u *p2;
5258 int col;
5259 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5260 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5261 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5262 ->sb_cells;
5263
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005264 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005265 line1 = vim_strsave(ml_get(lnum));
5266 if (line1 == NULL)
5267 break;
5268 p1 = line1;
5269
5270 line2 = ml_get(lnum + bot_lnum);
5271 p2 = line2;
5272 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5273 {
5274 int len1 = utfc_ptr2len(p1);
5275 int len2 = utfc_ptr2len(p2);
5276
5277 textline[col] = ' ';
5278 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005279 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005280 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005281 else if (lnum == cursor_pos1.row + 1
5282 && col == cursor_pos1.col
5283 && (cursor_pos1.row != cursor_pos2.row
5284 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005285 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005286 textline[col] = '>';
5287 else if (lnum == cursor_pos2.row + 1
5288 && col == cursor_pos2.col
5289 && (cursor_pos1.row != cursor_pos2.row
5290 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005291 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005292 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005293 else if (cellattr1 != NULL && cellattr2 != NULL)
5294 {
5295 if ((cellattr1 + col)->width
5296 != (cellattr2 + col)->width)
5297 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005298 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005299 &(cellattr2 + col)->fg))
5300 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005301 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005302 &(cellattr2 + col)->bg))
5303 textline[col] = 'b';
5304 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5305 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5306 textline[col] = 'a';
5307 }
5308 p1 += len1;
5309 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005310 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005311 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005312
5313 while (col < width)
5314 {
5315 if (*p1 == NUL && *p2 == NUL)
5316 textline[col] = '?';
5317 else if (*p1 == NUL)
5318 {
5319 textline[col] = '+';
5320 p2 += utfc_ptr2len(p2);
5321 }
5322 else
5323 {
5324 textline[col] = '-';
5325 p1 += utfc_ptr2len(p1);
5326 }
5327 ++col;
5328 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005329
5330 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005331 }
5332 if (add_empty_scrollback(term, &term->tl_default_color,
5333 term->tl_top_diff_rows) == OK)
5334 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5335 ++bot_lnum;
5336 }
5337
5338 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5339 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005340 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005341 for (i = 0; i < width; ++i)
5342 textline[i] = '+';
5343 if (add_empty_scrollback(term, &term->tl_default_color,
5344 term->tl_top_diff_rows) == OK)
5345 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5346 ++lnum;
5347 ++bot_lnum;
5348 }
5349
5350 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005351
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005352 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005353 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005354 }
5355
5356theend:
5357 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005358 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005359 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005360 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005361 fclose(fd2);
5362}
5363
5364/*
5365 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5366 * bottom files.
5367 * Return FAIL when this is not possible.
5368 */
5369 int
5370term_swap_diff()
5371{
5372 term_T *term = curbuf->b_term;
5373 linenr_T line_count;
5374 linenr_T top_rows;
5375 linenr_T bot_rows;
5376 linenr_T bot_start;
5377 linenr_T lnum;
5378 char_u *p;
5379 sb_line_T *sb_line;
5380
5381 if (term == NULL
5382 || !term_is_finished(curbuf)
5383 || term->tl_top_diff_rows == 0
5384 || term->tl_scrollback.ga_len == 0)
5385 return FAIL;
5386
5387 line_count = curbuf->b_ml.ml_line_count;
5388 top_rows = term->tl_top_diff_rows;
5389 bot_rows = term->tl_bot_diff_rows;
5390 bot_start = line_count - bot_rows;
5391 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5392
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005393 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005394 for (lnum = 1; lnum <= top_rows; ++lnum)
5395 {
5396 p = vim_strsave(ml_get(1));
5397 if (p == NULL)
5398 return OK;
5399 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005400 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005401 vim_free(p);
5402 }
5403
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005404 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005405 for (lnum = 1; lnum <= bot_rows; ++lnum)
5406 {
5407 p = vim_strsave(ml_get(bot_start + lnum));
5408 if (p == NULL)
5409 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005410 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005411 ml_append(lnum - 1, p, 0, FALSE);
5412 vim_free(p);
5413 }
5414
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005415 // move top title to bottom
5416 p = vim_strsave(ml_get(bot_rows + 1));
5417 if (p == NULL)
5418 return OK;
5419 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005420 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005421 vim_free(p);
5422
5423 // move bottom title to top
5424 p = vim_strsave(ml_get(line_count - top_rows));
5425 if (p == NULL)
5426 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005427 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005428 ml_append(bot_rows, p, 0, FALSE);
5429 vim_free(p);
5430
Bram Moolenaard96ff162018-02-18 22:13:29 +01005431 if (top_rows == bot_rows)
5432 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005433 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005434 for (lnum = 0; lnum < top_rows; ++lnum)
5435 {
5436 sb_line_T temp;
5437
5438 temp = *(sb_line + lnum);
5439 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5440 *(sb_line + bot_start + lnum) = temp;
5441 }
5442 }
5443 else
5444 {
5445 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005446 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005447
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005448 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005449 if (temp != NULL)
5450 {
5451 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5452 mch_memmove(term->tl_scrollback.ga_data,
5453 temp + bot_start,
5454 sizeof(sb_line_T) * bot_rows);
5455 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5456 temp + top_rows,
5457 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5458 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5459 + line_count - top_rows,
5460 temp,
5461 sizeof(sb_line_T) * top_rows);
5462 vim_free(temp);
5463 }
5464 }
5465
5466 term->tl_top_diff_rows = bot_rows;
5467 term->tl_bot_diff_rows = top_rows;
5468
5469 update_screen(NOT_VALID);
5470 return OK;
5471}
5472
5473/*
5474 * "term_dumpdiff(filename, filename, options)" function
5475 */
5476 void
5477f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5478{
5479 term_load_dump(argvars, rettv, TRUE);
5480}
5481
5482/*
5483 * "term_dumpload(filename, options)" function
5484 */
5485 void
5486f_term_dumpload(typval_T *argvars, typval_T *rettv)
5487{
5488 term_load_dump(argvars, rettv, FALSE);
5489}
5490
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005491/*
5492 * "term_getaltscreen(buf)" function
5493 */
5494 void
5495f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5496{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005497 buf_T *buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005498
5499 if (buf == NULL)
5500 return;
5501 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5502}
5503
5504/*
5505 * "term_getattr(attr, name)" function
5506 */
5507 void
5508f_term_getattr(typval_T *argvars, typval_T *rettv)
5509{
5510 int attr;
5511 size_t i;
5512 char_u *name;
5513
5514 static struct {
5515 char *name;
5516 int attr;
5517 } attrs[] = {
5518 {"bold", HL_BOLD},
5519 {"italic", HL_ITALIC},
5520 {"underline", HL_UNDERLINE},
5521 {"strike", HL_STRIKETHROUGH},
5522 {"reverse", HL_INVERSE},
5523 };
5524
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005525 attr = tv_get_number(&argvars[0]);
5526 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005527 if (name == NULL)
5528 return;
5529
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005530 if (attr > HL_ALL)
5531 attr = syn_attr2attr(attr);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005532 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
5533 if (STRCMP(name, attrs[i].name) == 0)
5534 {
5535 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5536 break;
5537 }
5538}
5539
5540/*
5541 * "term_getcursor(buf)" function
5542 */
5543 void
5544f_term_getcursor(typval_T *argvars, typval_T *rettv)
5545{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005546 buf_T *buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005547 term_T *term;
5548 list_T *l;
5549 dict_T *d;
5550
5551 if (rettv_list_alloc(rettv) == FAIL)
5552 return;
5553 if (buf == NULL)
5554 return;
5555 term = buf->b_term;
5556
5557 l = rettv->vval.v_list;
5558 list_append_number(l, term->tl_cursor_pos.row + 1);
5559 list_append_number(l, term->tl_cursor_pos.col + 1);
5560
5561 d = dict_alloc();
5562 if (d != NULL)
5563 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005564 dict_add_number(d, "visible", term->tl_cursor_visible);
5565 dict_add_number(d, "blink", blink_state_is_inverted()
5566 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5567 dict_add_number(d, "shape", term->tl_cursor_shape);
5568 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005569 list_append_dict(l, d);
5570 }
5571}
5572
5573/*
5574 * "term_getjob(buf)" function
5575 */
5576 void
5577f_term_getjob(typval_T *argvars, typval_T *rettv)
5578{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005579 buf_T *buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005580
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005581 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005582 {
5583 rettv->v_type = VAR_SPECIAL;
5584 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005585 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005586 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005587
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005588 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005589 rettv->vval.v_job = buf->b_term->tl_job;
5590 if (rettv->vval.v_job != NULL)
5591 ++rettv->vval.v_job->jv_refcount;
5592}
5593
5594 static int
5595get_row_number(typval_T *tv, term_T *term)
5596{
5597 if (tv->v_type == VAR_STRING
5598 && tv->vval.v_string != NULL
5599 && STRCMP(tv->vval.v_string, ".") == 0)
5600 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005601 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005602}
5603
5604/*
5605 * "term_getline(buf, row)" function
5606 */
5607 void
5608f_term_getline(typval_T *argvars, typval_T *rettv)
5609{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005610 buf_T *buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005611 term_T *term;
5612 int row;
5613
5614 rettv->v_type = VAR_STRING;
5615 if (buf == NULL)
5616 return;
5617 term = buf->b_term;
5618 row = get_row_number(&argvars[1], term);
5619
5620 if (term->tl_vterm == NULL)
5621 {
5622 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5623
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005624 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005625 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5626 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5627 }
5628 else
5629 {
5630 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5631 VTermRect rect;
5632 int len;
5633 char_u *p;
5634
5635 if (row < 0 || row >= term->tl_rows)
5636 return;
5637 len = term->tl_cols * MB_MAXBYTES + 1;
5638 p = alloc(len);
5639 if (p == NULL)
5640 return;
5641 rettv->vval.v_string = p;
5642
5643 rect.start_col = 0;
5644 rect.end_col = term->tl_cols;
5645 rect.start_row = row;
5646 rect.end_row = row + 1;
5647 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5648 }
5649}
5650
5651/*
5652 * "term_getscrolled(buf)" function
5653 */
5654 void
5655f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5656{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005657 buf_T *buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005658
5659 if (buf == NULL)
5660 return;
5661 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5662}
5663
5664/*
5665 * "term_getsize(buf)" function
5666 */
5667 void
5668f_term_getsize(typval_T *argvars, typval_T *rettv)
5669{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005670 buf_T *buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005671 list_T *l;
5672
5673 if (rettv_list_alloc(rettv) == FAIL)
5674 return;
5675 if (buf == NULL)
5676 return;
5677
5678 l = rettv->vval.v_list;
5679 list_append_number(l, buf->b_term->tl_rows);
5680 list_append_number(l, buf->b_term->tl_cols);
5681}
5682
5683/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005684 * "term_setsize(buf, rows, cols)" function
5685 */
5686 void
5687f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5688{
5689 buf_T *buf = term_get_buf(argvars, "term_setsize()");
5690 term_T *term;
5691 varnumber_T rows, cols;
5692
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005693 if (buf == NULL)
5694 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005695 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005696 return;
5697 }
5698 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005699 return;
5700 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005701 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005702 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005703 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005704 cols = cols <= 0 ? term->tl_cols : cols;
5705 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005706 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005707
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005708 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005709 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5710 term_report_winsize(term, term->tl_rows, term->tl_cols);
5711}
5712
5713/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005714 * "term_getstatus(buf)" function
5715 */
5716 void
5717f_term_getstatus(typval_T *argvars, typval_T *rettv)
5718{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005719 buf_T *buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005720 term_T *term;
5721 char_u val[100];
5722
5723 rettv->v_type = VAR_STRING;
5724 if (buf == NULL)
5725 return;
5726 term = buf->b_term;
5727
5728 if (term_job_running(term))
5729 STRCPY(val, "running");
5730 else
5731 STRCPY(val, "finished");
5732 if (term->tl_normal_mode)
5733 STRCAT(val, ",normal");
5734 rettv->vval.v_string = vim_strsave(val);
5735}
5736
5737/*
5738 * "term_gettitle(buf)" function
5739 */
5740 void
5741f_term_gettitle(typval_T *argvars, typval_T *rettv)
5742{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005743 buf_T *buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005744
5745 rettv->v_type = VAR_STRING;
5746 if (buf == NULL)
5747 return;
5748
5749 if (buf->b_term->tl_title != NULL)
5750 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5751}
5752
5753/*
5754 * "term_gettty(buf)" function
5755 */
5756 void
5757f_term_gettty(typval_T *argvars, typval_T *rettv)
5758{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005759 buf_T *buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005760 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005761 int num = 0;
5762
5763 rettv->v_type = VAR_STRING;
5764 if (buf == NULL)
5765 return;
5766 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005767 num = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005768
5769 switch (num)
5770 {
5771 case 0:
5772 if (buf->b_term->tl_job != NULL)
5773 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005774 break;
5775 case 1:
5776 if (buf->b_term->tl_job != NULL)
5777 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005778 break;
5779 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005780 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005781 return;
5782 }
5783 if (p != NULL)
5784 rettv->vval.v_string = vim_strsave(p);
5785}
5786
5787/*
5788 * "term_list()" function
5789 */
5790 void
5791f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
5792{
5793 term_T *tp;
5794 list_T *l;
5795
5796 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
5797 return;
5798
5799 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005800 FOR_ALL_TERMS(tp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005801 if (tp != NULL && tp->tl_buffer != NULL)
5802 if (list_append_number(l,
5803 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
5804 return;
5805}
5806
5807/*
5808 * "term_scrape(buf, row)" function
5809 */
5810 void
5811f_term_scrape(typval_T *argvars, typval_T *rettv)
5812{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005813 buf_T *buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005814 VTermScreen *screen = NULL;
5815 VTermPos pos;
5816 list_T *l;
5817 term_T *term;
5818 char_u *p;
5819 sb_line_T *line;
5820
5821 if (rettv_list_alloc(rettv) == FAIL)
5822 return;
5823 if (buf == NULL)
5824 return;
5825 term = buf->b_term;
5826
5827 l = rettv->vval.v_list;
5828 pos.row = get_row_number(&argvars[1], term);
5829
5830 if (term->tl_vterm != NULL)
5831 {
5832 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01005833 if (screen == NULL) // can't really happen
5834 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005835 p = NULL;
5836 line = NULL;
5837 }
5838 else
5839 {
5840 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
5841
5842 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
5843 return;
5844 p = ml_get_buf(buf, lnum + 1, FALSE);
5845 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
5846 }
5847
5848 for (pos.col = 0; pos.col < term->tl_cols; )
5849 {
5850 dict_T *dcell;
5851 int width;
5852 VTermScreenCellAttrs attrs;
5853 VTermColor fg, bg;
5854 char_u rgb[8];
5855 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
5856 int off = 0;
5857 int i;
5858
5859 if (screen == NULL)
5860 {
5861 cellattr_T *cellattr;
5862 int len;
5863
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005864 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005865 if (pos.col >= line->sb_cols)
5866 break;
5867 cellattr = line->sb_cells + pos.col;
5868 width = cellattr->width;
5869 attrs = cellattr->attrs;
5870 fg = cellattr->fg;
5871 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02005872 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005873 mch_memmove(mbs, p, len);
5874 mbs[len] = NUL;
5875 p += len;
5876 }
5877 else
5878 {
5879 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005880
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005881 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
5882 break;
5883 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
5884 {
5885 if (cell.chars[i] == 0)
5886 break;
5887 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
5888 }
5889 mbs[off] = NUL;
5890 width = cell.width;
5891 attrs = cell.attrs;
5892 fg = cell.fg;
5893 bg = cell.bg;
5894 }
5895 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01005896 if (dcell == NULL)
5897 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005898 list_append_dict(l, dcell);
5899
Bram Moolenaare0be1672018-07-08 16:50:37 +02005900 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005901
5902 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5903 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005904 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005905 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5906 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005907 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005908
Bram Moolenaar83d47902020-03-26 20:34:00 +01005909 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02005910 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005911
5912 ++pos.col;
5913 if (width == 2)
5914 ++pos.col;
5915 }
5916}
5917
5918/*
5919 * "term_sendkeys(buf, keys)" function
5920 */
5921 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01005922f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005923{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005924 buf_T *buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005925 char_u *msg;
5926 term_T *term;
5927
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005928 if (buf == NULL)
5929 return;
5930
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005931 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005932 if (msg == NULL)
5933 return;
5934 term = buf->b_term;
5935 if (term->tl_vterm == NULL)
5936 return;
5937
5938 while (*msg != NUL)
5939 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02005940 int c;
5941
5942 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
5943 {
5944 c = TO_SPECIAL(msg[1], msg[2]);
5945 msg += 3;
5946 }
5947 else
5948 {
5949 c = PTR2CHAR(msg);
5950 msg += MB_CPTR2LEN(msg);
5951 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01005952 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005953 }
5954}
5955
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005956#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
5957/*
5958 * "term_getansicolors(buf)" function
5959 */
5960 void
5961f_term_getansicolors(typval_T *argvars, typval_T *rettv)
5962{
5963 buf_T *buf = term_get_buf(argvars, "term_getansicolors()");
5964 term_T *term;
5965 VTermState *state;
5966 VTermColor color;
5967 char_u hexbuf[10];
5968 int index;
5969 list_T *list;
5970
5971 if (rettv_list_alloc(rettv) == FAIL)
5972 return;
5973
5974 if (buf == NULL)
5975 return;
5976 term = buf->b_term;
5977 if (term->tl_vterm == NULL)
5978 return;
5979
5980 list = rettv->vval.v_list;
5981 state = vterm_obtain_state(term->tl_vterm);
5982 for (index = 0; index < 16; index++)
5983 {
5984 vterm_state_get_palette_color(state, index, &color);
5985 sprintf((char *)hexbuf, "#%02x%02x%02x",
5986 color.red, color.green, color.blue);
5987 if (list_append_string(list, hexbuf, 7) == FAIL)
5988 return;
5989 }
5990}
5991
5992/*
5993 * "term_setansicolors(buf, list)" function
5994 */
5995 void
5996f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
5997{
5998 buf_T *buf = term_get_buf(argvars, "term_setansicolors()");
5999 term_T *term;
6000
6001 if (buf == NULL)
6002 return;
6003 term = buf->b_term;
6004 if (term->tl_vterm == NULL)
6005 return;
6006
6007 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6008 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006009 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006010 return;
6011 }
6012
6013 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006014 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006015}
6016#endif
6017
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006018/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006019 * "term_setapi(buf, api)" function
6020 */
6021 void
6022f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6023{
6024 buf_T *buf = term_get_buf(argvars, "term_setapi()");
6025 term_T *term;
6026 char_u *api;
6027
6028 if (buf == NULL)
6029 return;
6030 term = buf->b_term;
6031 vim_free(term->tl_api);
6032 api = tv_get_string_chk(&argvars[1]);
6033 if (api != NULL)
6034 term->tl_api = vim_strsave(api);
6035 else
6036 term->tl_api = NULL;
6037}
6038
6039/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006040 * "term_setrestore(buf, command)" function
6041 */
6042 void
6043f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6044{
6045#if defined(FEAT_SESSION)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006046 buf_T *buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006047 term_T *term;
6048 char_u *cmd;
6049
6050 if (buf == NULL)
6051 return;
6052 term = buf->b_term;
6053 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006054 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006055 if (cmd != NULL)
6056 term->tl_command = vim_strsave(cmd);
6057 else
6058 term->tl_command = NULL;
6059#endif
6060}
6061
6062/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006063 * "term_setkill(buf, how)" function
6064 */
6065 void
6066f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6067{
6068 buf_T *buf = term_get_buf(argvars, "term_setkill()");
6069 term_T *term;
6070 char_u *how;
6071
6072 if (buf == NULL)
6073 return;
6074 term = buf->b_term;
6075 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006076 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006077 if (how != NULL)
6078 term->tl_kill = vim_strsave(how);
6079 else
6080 term->tl_kill = NULL;
6081}
6082
6083/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006084 * "term_start(command, options)" function
6085 */
6086 void
6087f_term_start(typval_T *argvars, typval_T *rettv)
6088{
6089 jobopt_T opt;
6090 buf_T *buf;
6091
6092 init_job_options(&opt);
6093 if (argvars[1].v_type != VAR_UNKNOWN
6094 && get_job_options(&argvars[1], &opt,
6095 JO_TIMEOUT_ALL + JO_STOPONEXIT
6096 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6097 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6098 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6099 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006100 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006101 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006102 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006103 return;
6104
Bram Moolenaar13568252018-03-16 20:46:58 +01006105 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006106
6107 if (buf != NULL && buf->b_term != NULL)
6108 rettv->vval.v_number = buf->b_fnum;
6109}
6110
6111/*
6112 * "term_wait" function
6113 */
6114 void
6115f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6116{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006117 buf_T *buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006118
6119 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006120 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006121 if (buf->b_term->tl_job == NULL)
6122 {
6123 ch_log(NULL, "term_wait(): no job to wait for");
6124 return;
6125 }
6126 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006127 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006128 return;
6129
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006130 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006131 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006132 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6133 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006134 // The job is dead, keep reading channel I/O until the channel is
6135 // closed. buf->b_term may become NULL if the terminal was closed while
6136 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006137 ch_log(NULL, "term_wait(): waiting for channel to close");
6138 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6139 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006140 term_flush_messages();
6141
Bram Moolenaard45aa552018-05-21 22:50:29 +02006142 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006143 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006144 // If the terminal is closed when the channel is closed the
6145 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006146 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006147 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006148
6149 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006150 }
6151 else
6152 {
6153 long wait = 10L;
6154
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006155 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006156
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006157 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006158 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006159 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006160 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006161
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006162 // Flushing messages on channels is hopefully sufficient.
6163 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006164 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006165 }
6166}
6167
6168/*
6169 * Called when a channel has sent all the lines to a terminal.
6170 * Send a CTRL-D to mark the end of the text.
6171 */
6172 void
6173term_send_eof(channel_T *ch)
6174{
6175 term_T *term;
6176
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006177 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006178 if (term->tl_job == ch->ch_job)
6179 {
6180 if (term->tl_eof_chars != NULL)
6181 {
6182 channel_send(ch, PART_IN, term->tl_eof_chars,
6183 (int)STRLEN(term->tl_eof_chars), NULL);
6184 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6185 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006186# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006187 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006188 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006189 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6190# endif
6191 }
6192}
6193
Bram Moolenaar113e1072019-01-20 15:30:40 +01006194#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006195 job_T *
6196term_getjob(term_T *term)
6197{
6198 return term != NULL ? term->tl_job : NULL;
6199}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006200#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006201
Bram Moolenaar4f974752019-02-17 17:44:42 +01006202# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006203
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006204///////////////////////////////////////
6205// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006206#ifdef PROTO
6207typedef int COORD;
6208typedef int DWORD;
6209typedef int HANDLE;
6210typedef int *DWORD_PTR;
6211typedef int HPCON;
6212typedef int HRESULT;
6213typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006214typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006215typedef int PSIZE_T;
6216typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006217typedef int BOOL;
6218# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006219#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006220
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006221HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6222HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6223HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006224BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6225BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6226void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006227
6228 static int
6229dyn_conpty_init(int verbose)
6230{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006231 static HMODULE hKerneldll = NULL;
6232 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006233 static struct
6234 {
6235 char *name;
6236 FARPROC *ptr;
6237 } conpty_entry[] =
6238 {
6239 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6240 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6241 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6242 {"InitializeProcThreadAttributeList",
6243 (FARPROC*)&pInitializeProcThreadAttributeList},
6244 {"UpdateProcThreadAttribute",
6245 (FARPROC*)&pUpdateProcThreadAttribute},
6246 {"DeleteProcThreadAttributeList",
6247 (FARPROC*)&pDeleteProcThreadAttributeList},
6248 {NULL, NULL}
6249 };
6250
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006251 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006252 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006253 if (verbose)
6254 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006255 return FAIL;
6256 }
6257
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006258 // No need to initialize twice.
6259 if (hKerneldll)
6260 return OK;
6261
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006262 hKerneldll = vimLoadLib("kernel32.dll");
6263 for (i = 0; conpty_entry[i].name != NULL
6264 && conpty_entry[i].ptr != NULL; ++i)
6265 {
6266 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6267 conpty_entry[i].name)) == NULL)
6268 {
6269 if (verbose)
6270 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006271 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006272 return FAIL;
6273 }
6274 }
6275
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006276 return OK;
6277}
6278
6279 static int
6280conpty_term_and_job_init(
6281 term_T *term,
6282 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006283 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006284 jobopt_T *opt,
6285 jobopt_T *orig_opt)
6286{
6287 WCHAR *cmd_wchar = NULL;
6288 WCHAR *cmd_wchar_copy = NULL;
6289 WCHAR *cwd_wchar = NULL;
6290 WCHAR *env_wchar = NULL;
6291 channel_T *channel = NULL;
6292 job_T *job = NULL;
6293 HANDLE jo = NULL;
6294 garray_T ga_cmd, ga_env;
6295 char_u *cmd = NULL;
6296 HRESULT hr;
6297 COORD consize;
6298 SIZE_T breq;
6299 PROCESS_INFORMATION proc_info;
6300 HANDLE i_theirs = NULL;
6301 HANDLE o_theirs = NULL;
6302 HANDLE i_ours = NULL;
6303 HANDLE o_ours = NULL;
6304
6305 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6306 ga_init2(&ga_env, (int)sizeof(char*), 20);
6307
6308 if (argvar->v_type == VAR_STRING)
6309 {
6310 cmd = argvar->vval.v_string;
6311 }
6312 else if (argvar->v_type == VAR_LIST)
6313 {
6314 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6315 goto failed;
6316 cmd = ga_cmd.ga_data;
6317 }
6318 if (cmd == NULL || *cmd == NUL)
6319 {
6320 emsg(_(e_invarg));
6321 goto failed;
6322 }
6323
6324 term->tl_arg0_cmd = vim_strsave(cmd);
6325
6326 cmd_wchar = enc_to_utf16(cmd, NULL);
6327
6328 if (cmd_wchar != NULL)
6329 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006330 // Request by CreateProcessW
6331 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006332 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006333 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6334 }
6335
6336 ga_clear(&ga_cmd);
6337 if (cmd_wchar == NULL)
6338 goto failed;
6339 if (opt->jo_cwd != NULL)
6340 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6341
6342 win32_build_env(opt->jo_env, &ga_env, TRUE);
6343 env_wchar = ga_env.ga_data;
6344
6345 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6346 goto failed;
6347 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6348 goto failed;
6349
6350 consize.X = term->tl_cols;
6351 consize.Y = term->tl_rows;
6352 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6353 &term->tl_conpty);
6354 if (FAILED(hr))
6355 goto failed;
6356
6357 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6358
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006359 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006360 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006361 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006362 if (!term->tl_siex.lpAttributeList)
6363 goto failed;
6364 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6365 0, &breq))
6366 goto failed;
6367 if (!pUpdateProcThreadAttribute(
6368 term->tl_siex.lpAttributeList, 0,
6369 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6370 sizeof(HPCON), NULL, NULL))
6371 goto failed;
6372
6373 channel = add_channel();
6374 if (channel == NULL)
6375 goto failed;
6376
6377 job = job_alloc();
6378 if (job == NULL)
6379 goto failed;
6380 if (argvar->v_type == VAR_STRING)
6381 {
6382 int argc;
6383
6384 build_argv_from_string(cmd, &job->jv_argv, &argc);
6385 }
6386 else
6387 {
6388 int argc;
6389
6390 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6391 }
6392
6393 if (opt->jo_set & JO_IN_BUF)
6394 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6395
6396 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6397 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006398 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006399 env_wchar, cwd_wchar,
6400 &term->tl_siex.StartupInfo, &proc_info))
6401 goto failed;
6402
6403 CloseHandle(i_theirs);
6404 CloseHandle(o_theirs);
6405
6406 channel_set_pipes(channel,
6407 (sock_T)i_ours,
6408 (sock_T)o_ours,
6409 (sock_T)o_ours);
6410
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006411 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006412 channel->ch_write_text_mode = TRUE;
6413
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006414 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006415 channel->ch_anonymous_pipe = TRUE;
6416
6417 jo = CreateJobObject(NULL, NULL);
6418 if (jo == NULL)
6419 goto failed;
6420
6421 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6422 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006423 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006424 CloseHandle(jo);
6425 jo = NULL;
6426 }
6427
6428 ResumeThread(proc_info.hThread);
6429 CloseHandle(proc_info.hThread);
6430
6431 vim_free(cmd_wchar);
6432 vim_free(cmd_wchar_copy);
6433 vim_free(cwd_wchar);
6434 vim_free(env_wchar);
6435
6436 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6437 goto failed;
6438
6439#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6440 if (opt->jo_set2 & JO2_ANSI_COLORS)
6441 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6442 else
6443 init_vterm_ansi_colors(term->tl_vterm);
6444#endif
6445
6446 channel_set_job(channel, job, opt);
6447 job_set_options(job, opt);
6448
6449 job->jv_channel = channel;
6450 job->jv_proc_info = proc_info;
6451 job->jv_job_object = jo;
6452 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006453 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006454 ++job->jv_refcount;
6455 term->tl_job = job;
6456
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006457 // Redirecting stdout and stderr doesn't work at the job level. Instead
6458 // open the file here and handle it in. opt->jo_io was changed in
6459 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006460 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6461 {
6462 char_u *fname = opt->jo_io_name[PART_OUT];
6463
6464 ch_log(channel, "Opening output file %s", fname);
6465 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6466 if (term->tl_out_fd == NULL)
6467 semsg(_(e_notopen), fname);
6468 }
6469
6470 return OK;
6471
6472failed:
6473 ga_clear(&ga_cmd);
6474 ga_clear(&ga_env);
6475 vim_free(cmd_wchar);
6476 vim_free(cmd_wchar_copy);
6477 vim_free(cwd_wchar);
6478 if (channel != NULL)
6479 channel_clear(channel);
6480 if (job != NULL)
6481 {
6482 job->jv_channel = NULL;
6483 job_cleanup(job);
6484 }
6485 term->tl_job = NULL;
6486 if (jo != NULL)
6487 CloseHandle(jo);
6488
6489 if (term->tl_siex.lpAttributeList != NULL)
6490 {
6491 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6492 vim_free(term->tl_siex.lpAttributeList);
6493 }
6494 term->tl_siex.lpAttributeList = NULL;
6495 if (o_theirs != NULL)
6496 CloseHandle(o_theirs);
6497 if (o_ours != NULL)
6498 CloseHandle(o_ours);
6499 if (i_ours != NULL)
6500 CloseHandle(i_ours);
6501 if (i_theirs != NULL)
6502 CloseHandle(i_theirs);
6503 if (term->tl_conpty != NULL)
6504 pClosePseudoConsole(term->tl_conpty);
6505 term->tl_conpty = NULL;
6506 return FAIL;
6507}
6508
6509 static void
6510conpty_term_report_winsize(term_T *term, int rows, int cols)
6511{
6512 COORD consize;
6513
6514 consize.X = cols;
6515 consize.Y = rows;
6516 pResizePseudoConsole(term->tl_conpty, consize);
6517}
6518
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006519 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006520term_free_conpty(term_T *term)
6521{
6522 if (term->tl_siex.lpAttributeList != NULL)
6523 {
6524 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6525 vim_free(term->tl_siex.lpAttributeList);
6526 }
6527 term->tl_siex.lpAttributeList = NULL;
6528 if (term->tl_conpty != NULL)
6529 pClosePseudoConsole(term->tl_conpty);
6530 term->tl_conpty = NULL;
6531}
6532
6533 int
6534use_conpty(void)
6535{
6536 return has_conpty;
6537}
6538
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006539# ifndef PROTO
6540
6541#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6542#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006543#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006544
6545void* (*winpty_config_new)(UINT64, void*);
6546void* (*winpty_open)(void*, void*);
6547void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6548BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6549void (*winpty_config_set_mouse_mode)(void*, int);
6550void (*winpty_config_set_initial_size)(void*, int, int);
6551LPCWSTR (*winpty_conin_name)(void*);
6552LPCWSTR (*winpty_conout_name)(void*);
6553LPCWSTR (*winpty_conerr_name)(void*);
6554void (*winpty_free)(void*);
6555void (*winpty_config_free)(void*);
6556void (*winpty_spawn_config_free)(void*);
6557void (*winpty_error_free)(void*);
6558LPCWSTR (*winpty_error_msg)(void*);
6559BOOL (*winpty_set_size)(void*, int, int, void*);
6560HANDLE (*winpty_agent_process)(void*);
6561
6562#define WINPTY_DLL "winpty.dll"
6563
6564static HINSTANCE hWinPtyDLL = NULL;
6565# endif
6566
6567 static int
6568dyn_winpty_init(int verbose)
6569{
6570 int i;
6571 static struct
6572 {
6573 char *name;
6574 FARPROC *ptr;
6575 } winpty_entry[] =
6576 {
6577 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6578 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6579 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6580 {"winpty_config_set_mouse_mode",
6581 (FARPROC*)&winpty_config_set_mouse_mode},
6582 {"winpty_config_set_initial_size",
6583 (FARPROC*)&winpty_config_set_initial_size},
6584 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6585 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6586 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6587 {"winpty_free", (FARPROC*)&winpty_free},
6588 {"winpty_open", (FARPROC*)&winpty_open},
6589 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6590 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6591 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6592 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6593 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6594 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6595 {NULL, NULL}
6596 };
6597
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006598 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006599 if (hWinPtyDLL)
6600 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006601 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6602 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006603 if (*p_winptydll != NUL)
6604 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6605 if (!hWinPtyDLL)
6606 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6607 if (!hWinPtyDLL)
6608 {
6609 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006610 semsg(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006611 : (char_u *)WINPTY_DLL);
6612 return FAIL;
6613 }
6614 for (i = 0; winpty_entry[i].name != NULL
6615 && winpty_entry[i].ptr != NULL; ++i)
6616 {
6617 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6618 winpty_entry[i].name)) == NULL)
6619 {
6620 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006621 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006622 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006623 return FAIL;
6624 }
6625 }
6626
6627 return OK;
6628}
6629
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006630 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006631winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006632 term_T *term,
6633 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006634 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006635 jobopt_T *opt,
6636 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006637{
6638 WCHAR *cmd_wchar = NULL;
6639 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006640 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006641 channel_T *channel = NULL;
6642 job_T *job = NULL;
6643 DWORD error;
6644 HANDLE jo = NULL;
6645 HANDLE child_process_handle;
6646 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006647 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006648 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006649 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006650 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006651
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006652 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6653 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006654
6655 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006656 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006657 cmd = argvar->vval.v_string;
6658 }
6659 else if (argvar->v_type == VAR_LIST)
6660 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006661 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006662 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006663 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006664 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006665 if (cmd == NULL || *cmd == NUL)
6666 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006667 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006668 goto failed;
6669 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006670
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006671 term->tl_arg0_cmd = vim_strsave(cmd);
6672
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006673 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006674 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006675 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006676 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006677 if (opt->jo_cwd != NULL)
6678 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006679
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006680 win32_build_env(opt->jo_env, &ga_env, TRUE);
6681 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006682
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006683 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6684 if (term->tl_winpty_config == NULL)
6685 goto failed;
6686
6687 winpty_config_set_mouse_mode(term->tl_winpty_config,
6688 WINPTY_MOUSE_MODE_FORCE);
6689 winpty_config_set_initial_size(term->tl_winpty_config,
6690 term->tl_cols, term->tl_rows);
6691 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6692 if (term->tl_winpty == NULL)
6693 goto failed;
6694
6695 spawn_config = winpty_spawn_config_new(
6696 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6697 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6698 NULL,
6699 cmd_wchar,
6700 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006701 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006702 &winpty_err);
6703 if (spawn_config == NULL)
6704 goto failed;
6705
6706 channel = add_channel();
6707 if (channel == NULL)
6708 goto failed;
6709
6710 job = job_alloc();
6711 if (job == NULL)
6712 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006713 if (argvar->v_type == VAR_STRING)
6714 {
6715 int argc;
6716
6717 build_argv_from_string(cmd, &job->jv_argv, &argc);
6718 }
6719 else
6720 {
6721 int argc;
6722
6723 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6724 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006725
6726 if (opt->jo_set & JO_IN_BUF)
6727 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6728
6729 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
6730 &child_thread_handle, &error, &winpty_err))
6731 goto failed;
6732
6733 channel_set_pipes(channel,
6734 (sock_T)CreateFileW(
6735 winpty_conin_name(term->tl_winpty),
6736 GENERIC_WRITE, 0, NULL,
6737 OPEN_EXISTING, 0, NULL),
6738 (sock_T)CreateFileW(
6739 winpty_conout_name(term->tl_winpty),
6740 GENERIC_READ, 0, NULL,
6741 OPEN_EXISTING, 0, NULL),
6742 (sock_T)CreateFileW(
6743 winpty_conerr_name(term->tl_winpty),
6744 GENERIC_READ, 0, NULL,
6745 OPEN_EXISTING, 0, NULL));
6746
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006747 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006748 channel->ch_write_text_mode = TRUE;
6749
6750 jo = CreateJobObject(NULL, NULL);
6751 if (jo == NULL)
6752 goto failed;
6753
6754 if (!AssignProcessToJobObject(jo, child_process_handle))
6755 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006756 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006757 CloseHandle(jo);
6758 jo = NULL;
6759 }
6760
6761 winpty_spawn_config_free(spawn_config);
6762 vim_free(cmd_wchar);
6763 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006764 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006765
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006766 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6767 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006768
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006769#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6770 if (opt->jo_set2 & JO2_ANSI_COLORS)
6771 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6772 else
6773 init_vterm_ansi_colors(term->tl_vterm);
6774#endif
6775
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006776 channel_set_job(channel, job, opt);
6777 job_set_options(job, opt);
6778
6779 job->jv_channel = channel;
6780 job->jv_proc_info.hProcess = child_process_handle;
6781 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
6782 job->jv_job_object = jo;
6783 job->jv_status = JOB_STARTED;
6784 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006785 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006786 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006787 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006788 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006789 ++job->jv_refcount;
6790 term->tl_job = job;
6791
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006792 // Redirecting stdout and stderr doesn't work at the job level. Instead
6793 // open the file here and handle it in. opt->jo_io was changed in
6794 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006795 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6796 {
6797 char_u *fname = opt->jo_io_name[PART_OUT];
6798
6799 ch_log(channel, "Opening output file %s", fname);
6800 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6801 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006802 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006803 }
6804
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006805 return OK;
6806
6807failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006808 ga_clear(&ga_cmd);
6809 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006810 vim_free(cmd_wchar);
6811 vim_free(cwd_wchar);
6812 if (spawn_config != NULL)
6813 winpty_spawn_config_free(spawn_config);
6814 if (channel != NULL)
6815 channel_clear(channel);
6816 if (job != NULL)
6817 {
6818 job->jv_channel = NULL;
6819 job_cleanup(job);
6820 }
6821 term->tl_job = NULL;
6822 if (jo != NULL)
6823 CloseHandle(jo);
6824 if (term->tl_winpty != NULL)
6825 winpty_free(term->tl_winpty);
6826 term->tl_winpty = NULL;
6827 if (term->tl_winpty_config != NULL)
6828 winpty_config_free(term->tl_winpty_config);
6829 term->tl_winpty_config = NULL;
6830 if (winpty_err != NULL)
6831 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006832 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006833 (short_u *)winpty_error_msg(winpty_err), NULL);
6834
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006835 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006836 winpty_error_free(winpty_err);
6837 }
6838 return FAIL;
6839}
6840
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006841/*
6842 * Create a new terminal of "rows" by "cols" cells.
6843 * Store a reference in "term".
6844 * Return OK or FAIL.
6845 */
6846 static int
6847term_and_job_init(
6848 term_T *term,
6849 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01006850 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006851 jobopt_T *opt,
6852 jobopt_T *orig_opt)
6853{
6854 int use_winpty = FALSE;
6855 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006856 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006857
6858 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
6859 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
6860
6861 if (!has_winpty && !has_conpty)
6862 // If neither is available give the errors for winpty, since when
6863 // conpty is not available it can't be installed either.
6864 return dyn_winpty_init(TRUE);
6865
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006866 if (opt->jo_tty_type != NUL)
6867 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006868
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006869 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006870 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006871 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006872 use_conpty = TRUE;
6873 else if (has_winpty)
6874 use_winpty = TRUE;
6875 // else: error
6876 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006877 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006878 {
6879 if (has_winpty)
6880 use_winpty = TRUE;
6881 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006882 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006883 {
6884 if (has_conpty)
6885 use_conpty = TRUE;
6886 else
6887 return dyn_conpty_init(TRUE);
6888 }
6889
6890 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006891 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006892
6893 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006894 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006895
6896 // error
6897 return dyn_winpty_init(TRUE);
6898}
6899
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006900 static int
6901create_pty_only(term_T *term, jobopt_T *options)
6902{
6903 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
6904 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
6905 char in_name[80], out_name[80];
6906 channel_T *channel = NULL;
6907
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006908 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6909 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006910
6911 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
6912 GetCurrentProcessId(),
6913 curbuf->b_fnum);
6914 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
6915 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6916 PIPE_UNLIMITED_INSTANCES,
6917 0, 0, NMPWAIT_NOWAIT, NULL);
6918 if (hPipeIn == INVALID_HANDLE_VALUE)
6919 goto failed;
6920
6921 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
6922 GetCurrentProcessId(),
6923 curbuf->b_fnum);
6924 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
6925 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6926 PIPE_UNLIMITED_INSTANCES,
6927 0, 0, 0, NULL);
6928 if (hPipeOut == INVALID_HANDLE_VALUE)
6929 goto failed;
6930
6931 ConnectNamedPipe(hPipeIn, NULL);
6932 ConnectNamedPipe(hPipeOut, NULL);
6933
6934 term->tl_job = job_alloc();
6935 if (term->tl_job == NULL)
6936 goto failed;
6937 ++term->tl_job->jv_refcount;
6938
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006939 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006940 term->tl_job->jv_status = JOB_FINISHED;
6941
6942 channel = add_channel();
6943 if (channel == NULL)
6944 goto failed;
6945 term->tl_job->jv_channel = channel;
6946 channel->ch_keep_open = TRUE;
6947 channel->ch_named_pipe = TRUE;
6948
6949 channel_set_pipes(channel,
6950 (sock_T)hPipeIn,
6951 (sock_T)hPipeOut,
6952 (sock_T)hPipeOut);
6953 channel_set_job(channel, term->tl_job, options);
6954 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
6955 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
6956
6957 return OK;
6958
6959failed:
6960 if (hPipeIn != NULL)
6961 CloseHandle(hPipeIn);
6962 if (hPipeOut != NULL)
6963 CloseHandle(hPipeOut);
6964 return FAIL;
6965}
6966
6967/*
6968 * Free the terminal emulator part of "term".
6969 */
6970 static void
6971term_free_vterm(term_T *term)
6972{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006973 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006974 if (term->tl_winpty != NULL)
6975 winpty_free(term->tl_winpty);
6976 term->tl_winpty = NULL;
6977 if (term->tl_winpty_config != NULL)
6978 winpty_config_free(term->tl_winpty_config);
6979 term->tl_winpty_config = NULL;
6980 if (term->tl_vterm != NULL)
6981 vterm_free(term->tl_vterm);
6982 term->tl_vterm = NULL;
6983}
6984
6985/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02006986 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006987 */
6988 static void
6989term_report_winsize(term_T *term, int rows, int cols)
6990{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006991 if (term->tl_conpty)
6992 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006993 if (term->tl_winpty)
6994 winpty_set_size(term->tl_winpty, cols, rows, NULL);
6995}
6996
6997 int
6998terminal_enabled(void)
6999{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007000 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007001}
7002
7003# else
7004
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007005///////////////////////////////////////
7006// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007007
7008/*
7009 * Create a new terminal of "rows" by "cols" cells.
7010 * Start job for "cmd".
7011 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007012 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007013 * Return OK or FAIL.
7014 */
7015 static int
7016term_and_job_init(
7017 term_T *term,
7018 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007019 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007020 jobopt_T *opt,
7021 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007022{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007023 term->tl_arg0_cmd = NULL;
7024
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007025 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7026 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007027
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007028#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7029 if (opt->jo_set2 & JO2_ANSI_COLORS)
7030 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7031 else
7032 init_vterm_ansi_colors(term->tl_vterm);
7033#endif
7034
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007035 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007036 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007037 if (term->tl_job != NULL)
7038 ++term->tl_job->jv_refcount;
7039
7040 return term->tl_job != NULL
7041 && term->tl_job->jv_channel != NULL
7042 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7043}
7044
7045 static int
7046create_pty_only(term_T *term, jobopt_T *opt)
7047{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007048 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7049 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007050
7051 term->tl_job = job_alloc();
7052 if (term->tl_job == NULL)
7053 return FAIL;
7054 ++term->tl_job->jv_refcount;
7055
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007056 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007057 term->tl_job->jv_status = JOB_FINISHED;
7058
7059 return mch_create_pty_channel(term->tl_job, opt);
7060}
7061
7062/*
7063 * Free the terminal emulator part of "term".
7064 */
7065 static void
7066term_free_vterm(term_T *term)
7067{
7068 if (term->tl_vterm != NULL)
7069 vterm_free(term->tl_vterm);
7070 term->tl_vterm = NULL;
7071}
7072
7073/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007074 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007075 */
7076 static void
7077term_report_winsize(term_T *term, int rows, int cols)
7078{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007079 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007080 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7081 {
7082 int fd = -1;
7083 int part;
7084
7085 for (part = PART_OUT; part < PART_COUNT; ++part)
7086 {
7087 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007088 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007089 break;
7090 }
7091 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7092 mch_signal_job(term->tl_job, (char_u *)"winch");
7093 }
7094}
7095
7096# endif
7097
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007098#endif // FEAT_TERMINAL