blob: cabcf9d1b754cb7103ce8d27469c3ae5b1a9743d [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// Store the last set and the desired cursor properties, so that we only update
208// them when needed. Doing it unnecessary may result in flicker.
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200209static char_u *last_set_cursor_color = NULL;
210static char_u *desired_cursor_color = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +0100211static int last_set_cursor_shape = -1;
212static int desired_cursor_shape = -1;
213static int last_set_cursor_blink = -1;
214static int desired_cursor_blink = -1;
215
216
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100217///////////////////////////////////////
218// 1. Generic code for all systems.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200219
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200220 static int
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200221cursor_color_equal(char_u *lhs_color, char_u *rhs_color)
222{
223 if (lhs_color != NULL && rhs_color != NULL)
224 return STRCMP(lhs_color, rhs_color) == 0;
225 return lhs_color == NULL && rhs_color == NULL;
226}
227
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200228 static void
229cursor_color_copy(char_u **to_color, char_u *from_color)
230{
231 // Avoid a free & alloc if the value is already right.
232 if (cursor_color_equal(*to_color, from_color))
233 return;
234 vim_free(*to_color);
235 *to_color = (from_color == NULL) ? NULL : vim_strsave(from_color);
236}
237
238 static char_u *
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200239cursor_color_get(char_u *color)
240{
241 return (color == NULL) ? (char_u *)"" : color;
242}
243
244
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200245/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200246 * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
Bram Moolenaar498c2562018-04-15 23:45:15 +0200247 * current window.
248 * Sets "rows" and/or "cols" to zero when it should follow the window size.
249 * Return TRUE if the size is the minimum size: "24*80".
250 */
251 static int
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200252parse_termwinsize(win_T *wp, int *rows, int *cols)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200253{
254 int minsize = FALSE;
255
256 *rows = 0;
257 *cols = 0;
258
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200259 if (*wp->w_p_tws != NUL)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200260 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200261 char_u *p = vim_strchr(wp->w_p_tws, 'x');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200262
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100263 // Syntax of value was already checked when it's set.
Bram Moolenaar498c2562018-04-15 23:45:15 +0200264 if (p == NULL)
265 {
266 minsize = TRUE;
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200267 p = vim_strchr(wp->w_p_tws, '*');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200268 }
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200269 *rows = atoi((char *)wp->w_p_tws);
Bram Moolenaar498c2562018-04-15 23:45:15 +0200270 *cols = atoi((char *)p + 1);
271 }
272 return minsize;
273}
274
275/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200276 * Determine the terminal size from 'termwinsize' and the current window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200277 */
278 static void
Bram Moolenaarb936b792020-09-04 18:34:09 +0200279set_term_and_win_size(term_T *term, jobopt_T *opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200280{
Bram Moolenaarb936b792020-09-04 18:34:09 +0200281 int rows, cols;
282 int minsize;
283
Bram Moolenaar13568252018-03-16 20:46:58 +0100284#ifdef FEAT_GUI
285 if (term->tl_system)
286 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100287 // Use the whole screen for the system command. However, it will start
288 // at the command line and scroll up as needed, using tl_toprow.
Bram Moolenaar13568252018-03-16 20:46:58 +0100289 term->tl_rows = Rows;
290 term->tl_cols = Columns;
Bram Moolenaar07b46af2018-04-10 14:56:18 +0200291 return;
Bram Moolenaar13568252018-03-16 20:46:58 +0100292 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100293#endif
Bram Moolenaarb936b792020-09-04 18:34:09 +0200294 term->tl_rows = curwin->w_height;
295 term->tl_cols = curwin->w_width;
296
297 minsize = parse_termwinsize(curwin, &rows, &cols);
298 if (minsize)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200299 {
Bram Moolenaarb936b792020-09-04 18:34:09 +0200300 if (term->tl_rows < rows)
301 term->tl_rows = rows;
302 if (term->tl_cols < cols)
303 term->tl_cols = cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200304 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200305 if ((opt->jo_set2 & JO2_TERM_ROWS))
306 term->tl_rows = opt->jo_term_rows;
307 else if (rows != 0)
308 term->tl_rows = rows;
309 if ((opt->jo_set2 & JO2_TERM_COLS))
310 term->tl_cols = opt->jo_term_cols;
311 else if (cols != 0)
312 term->tl_cols = cols;
313
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200314 if (!opt->jo_hidden)
Bram Moolenaarb936b792020-09-04 18:34:09 +0200315 {
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200316 if (term->tl_rows != curwin->w_height)
317 win_setheight_win(term->tl_rows, curwin);
318 if (term->tl_cols != curwin->w_width)
319 win_setwidth_win(term->tl_cols, curwin);
Bram Moolenaarb936b792020-09-04 18:34:09 +0200320
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200321 // Set 'winsize' now to avoid a resize at the next redraw.
322 if (!minsize && *curwin->w_p_tws != NUL)
323 {
324 char_u buf[100];
325
326 vim_snprintf((char *)buf, 100, "%dx%d",
327 term->tl_rows, term->tl_cols);
328 set_option_value((char_u *)"termwinsize", 0L, buf, OPT_LOCAL);
329 }
Bram Moolenaarb936b792020-09-04 18:34:09 +0200330 }
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 Moolenaare1004402020-10-24 20:49:43 +0200439 int vertical = opt->jo_vertical || (cmdmod.cmod_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;
Bram Moolenaare5b44862021-05-30 13:54:03 +0200444#ifdef FEAT_CMDWIN
445 if (cmdwin_type != 0)
446 {
447 emsg(_(e_cannot_open_terminal_from_command_line_window));
448 return NULL;
449 }
450#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200451
452 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
453 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
454 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
Bram Moolenaarb0992022020-01-30 14:55:42 +0100455 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))
456 || (argvar != NULL
457 && argvar->v_type == VAR_LIST
458 && argvar->vval.v_list != NULL
459 && argvar->vval.v_list->lv_first == &range_list_item))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200460 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100461 emsg(_(e_invarg));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200462 return NULL;
463 }
464
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200465 term = ALLOC_CLEAR_ONE(term_T);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200466 if (term == NULL)
467 return NULL;
468 term->tl_dirty_row_end = MAX_ROW;
469 term->tl_cursor_visible = TRUE;
470 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
471 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100472#ifdef FEAT_GUI
473 term->tl_system = (flags & TERM_START_SYSTEM);
474#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200475 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100476 ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200477 ga_init2(&term->tl_osc_buf, sizeof(char), 300);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200478
Bram Moolenaaraeed2a62021-04-29 20:18:45 +0200479 setpcmark();
Bram Moolenaara80faa82020-04-12 19:37:17 +0200480 CLEAR_FIELD(split_ea);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200481 if (opt->jo_curwin)
482 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100483 // Create a new buffer in the current window.
Bram Moolenaar13568252018-03-16 20:46:58 +0100484 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200485 {
486 no_write_message();
487 vim_free(term);
488 return NULL;
489 }
490 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaarb1009092020-05-31 16:04:42 +0200491 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
492 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
493 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200494 {
495 vim_free(term);
496 return NULL;
497 }
498 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100499 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200500 {
501 buf_T *buf;
502
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100503 // Create a new buffer without a window. Make it the current buffer for
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100504 // a moment to be able to do the initializations.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200505 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
506 BLN_NEW | BLN_LISTED);
507 if (buf == NULL || ml_open(buf) == FAIL)
508 {
509 vim_free(term);
510 return NULL;
511 }
512 old_curbuf = curbuf;
513 --curbuf->b_nwindows;
514 curbuf = buf;
515 curwin->w_buffer = buf;
516 ++curbuf->b_nwindows;
517 }
518 else
519 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100520 // Open a new window or tab.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200521 split_ea.cmdidx = CMD_new;
522 split_ea.cmd = (char_u *)"new";
523 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100524 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200525 {
526 split_ea.line2 = opt->jo_term_rows;
527 split_ea.addr_count = 1;
528 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100529 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200530 {
531 split_ea.line2 = opt->jo_term_cols;
532 split_ea.addr_count = 1;
533 }
534
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100535 if (vertical)
Bram Moolenaare1004402020-10-24 20:49:43 +0200536 cmdmod.cmod_split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200537 ex_splitview(&split_ea);
538 if (curwin == old_curwin)
539 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100540 // split failed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200541 vim_free(term);
542 return NULL;
543 }
544 }
545 term->tl_buffer = curbuf;
546 curbuf->b_term = term;
547
548 if (!opt->jo_hidden)
549 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100550 // Only one size was taken care of with :new, do the other one. With
551 // "curwin" both need to be done.
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100552 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200553 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100554 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200555 win_setwidth(opt->jo_term_cols);
556 }
557
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100558 // Link the new terminal in the list of active terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200559 term->tl_next = first_term;
560 first_term = term;
561
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100562 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
563
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200564 if (opt->jo_term_name != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100565 {
566 vim_free(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200567 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100568 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100569 else if (argv != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100570 {
571 vim_free(curbuf->b_ffname);
Bram Moolenaar13568252018-03-16 20:46:58 +0100572 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100573 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200574 else
575 {
576 int i;
577 size_t len;
578 char_u *cmd, *p;
579
580 if (argvar->v_type == VAR_STRING)
581 {
582 cmd = argvar->vval.v_string;
583 if (cmd == NULL)
584 cmd = (char_u *)"";
585 else if (STRCMP(cmd, "NONE") == 0)
586 cmd = (char_u *)"pty";
587 }
588 else if (argvar->v_type != VAR_LIST
589 || argvar->vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +0100590 || argvar->vval.v_list->lv_len == 0
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100591 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200592 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
593 cmd = (char_u*)"";
594
595 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200596 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200597
598 for (i = 0; p != NULL; ++i)
599 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100600 // Prepend a ! to the command name to avoid the buffer name equals
601 // the executable, otherwise ":w!" would overwrite it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200602 if (i == 0)
603 vim_snprintf((char *)p, len, "!%s", cmd);
604 else
605 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
606 if (buflist_findname(p) == NULL)
607 {
608 vim_free(curbuf->b_ffname);
609 curbuf->b_ffname = p;
610 break;
611 }
612 }
613 }
Bram Moolenaare010c722020-02-24 21:37:54 +0100614 vim_free(curbuf->b_sfname);
615 curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200616 curbuf->b_fname = curbuf->b_ffname;
617
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100618 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
619
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200620 if (opt->jo_term_opencmd != NULL)
621 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
622
623 if (opt->jo_eof_chars != NULL)
624 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
625
626 set_string_option_direct((char_u *)"buftype", -1,
627 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200628 // Avoid that 'buftype' is reset when this buffer is entered.
629 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200630
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100631 // Mark the buffer as not modifiable. It can only be made modifiable after
632 // the job finished.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200633 curbuf->b_p_ma = FALSE;
634
Bram Moolenaarb936b792020-09-04 18:34:09 +0200635 set_term_and_win_size(term, opt);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100636#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200637 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
638#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200639 setup_job_options(opt, term->tl_rows, term->tl_cols);
640
Bram Moolenaar13568252018-03-16 20:46:58 +0100641 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100642 return curbuf;
643
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100644#if defined(FEAT_SESSION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100645 // Remember the command for the session file.
Bram Moolenaar13568252018-03-16 20:46:58 +0100646 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100647 term->tl_command = vim_strsave((char_u *)"NONE");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100648 else if (argvar->v_type == VAR_STRING)
649 {
650 char_u *cmd = argvar->vval.v_string;
651
652 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
653 term->tl_command = vim_strsave(cmd);
654 }
655 else if (argvar->v_type == VAR_LIST
656 && argvar->vval.v_list != NULL
657 && argvar->vval.v_list->lv_len > 0)
658 {
659 garray_T ga;
660 listitem_T *item;
661
662 ga_init2(&ga, 1, 100);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200663 FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100664 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100665 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100666 char_u *p;
667
668 if (s == NULL)
669 break;
670 p = vim_strsave_fnameescape(s, FALSE);
671 if (p == NULL)
672 break;
673 ga_concat(&ga, p);
674 vim_free(p);
675 ga_append(&ga, ' ');
676 }
677 if (item == NULL)
678 {
679 ga_append(&ga, NUL);
680 term->tl_command = ga.ga_data;
681 }
682 else
683 ga_clear(&ga);
684 }
685#endif
686
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100687 if (opt->jo_term_kill != NULL)
688 {
689 char_u *p = skiptowhite(opt->jo_term_kill);
690
691 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
692 }
693
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200694 if (opt->jo_term_api != NULL)
Bram Moolenaar21109272020-01-30 16:27:20 +0100695 {
696 char_u *p = skiptowhite(opt->jo_term_api);
697
698 term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
699 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200700 else
701 term->tl_api = vim_strsave((char_u *)"Tapi_");
702
Bram Moolenaar83d47902020-03-26 20:34:00 +0100703 if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
704 term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
705
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100706 // System dependent: setup the vterm and maybe start the job in it.
Bram Moolenaar13568252018-03-16 20:46:58 +0100707 if (argv == NULL
708 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200709 && argvar->vval.v_string != NULL
710 && STRCMP(argvar->vval.v_string, "NONE") == 0)
711 res = create_pty_only(term, opt);
712 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200713 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200714
715 newbuf = curbuf;
716 if (res == OK)
717 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100718 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200719 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
720 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100721#ifdef FEAT_GUI
722 if (term->tl_system)
723 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100724 // display first line below typed command
Bram Moolenaar13568252018-03-16 20:46:58 +0100725 term->tl_toprow = msg_row + 1;
726 term->tl_dirty_row_end = 0;
727 }
728#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200729
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100730 // Make sure we don't get stuck on sending keys to the job, it leads to
731 // a deadlock if the job is waiting for Vim to read.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200732 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
733
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200734 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200735 {
736 --curbuf->b_nwindows;
737 curbuf = old_curbuf;
738 curwin->w_buffer = curbuf;
739 ++curbuf->b_nwindows;
740 }
741 }
742 else
743 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100744 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200745 return NULL;
746 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100747
Bram Moolenaar13568252018-03-16 20:46:58 +0100748 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar28ed4df2019-10-26 16:21:40 +0200749 if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
750 apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200751 return newbuf;
752}
753
754/*
755 * ":terminal": open a terminal window and execute a job in it.
756 */
757 void
758ex_terminal(exarg_T *eap)
759{
760 typval_T argvar[2];
761 jobopt_T opt;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100762 int opt_shell = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200763 char_u *cmd;
764 char_u *tofree = NULL;
765
766 init_job_options(&opt);
767
768 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100769 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200770 {
771 char_u *p, *ep;
772
773 cmd += 2;
774 p = skiptowhite(cmd);
775 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200776 if (ep != NULL)
777 {
778 if (ep < p)
779 p = ep;
780 else
781 ep = NULL;
782 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200783
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200784# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
785 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
786 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200787 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200788 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100789 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200790 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200791 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200792 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200793 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200794 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200795 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200796 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100797 opt.jo_term_norestore = 1;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100798 else if (OPTARG_HAS("shell"))
799 opt_shell = TRUE;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200800 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100801 {
802 opt.jo_set2 |= JO2_TERM_KILL;
803 opt.jo_term_kill = ep + 1;
804 p = skiptowhite(cmd);
805 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200806 else if (OPTARG_HAS("api"))
807 {
808 opt.jo_set2 |= JO2_TERM_API;
809 if (ep != NULL)
810 {
811 opt.jo_term_api = ep + 1;
812 p = skiptowhite(cmd);
813 }
814 else
815 opt.jo_term_api = NULL;
816 }
817 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200818 {
819 opt.jo_set2 |= JO2_TERM_ROWS;
820 opt.jo_term_rows = atoi((char *)ep + 1);
821 p = skiptowhite(cmd);
822 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200823 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200824 {
825 opt.jo_set2 |= JO2_TERM_COLS;
826 opt.jo_term_cols = atoi((char *)ep + 1);
827 p = skiptowhite(cmd);
828 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200829 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200830 {
831 char_u *buf = NULL;
832 char_u *keys;
833
Bram Moolenaar21109272020-01-30 16:27:20 +0100834 vim_free(opt.jo_eof_chars);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200835 p = skiptowhite(cmd);
836 *p = NUL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200837 keys = replace_termcodes(ep + 1, &buf,
838 REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200839 opt.jo_set2 |= JO2_EOF_CHARS;
840 opt.jo_eof_chars = vim_strsave(keys);
841 vim_free(buf);
842 *p = ' ';
843 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100844#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100845 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
846 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100847 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100848 int tty_type = NUL;
849
850 p = skiptowhite(cmd);
851 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
852 tty_type = 'w';
853 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
854 tty_type = 'c';
855 else
856 {
857 semsg(e_invargval, "type");
858 goto theend;
859 }
860 opt.jo_set2 |= JO2_TTY_TYPE;
861 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100862 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100863#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200864 else
865 {
866 if (*p)
867 *p = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100868 semsg(_("E181: Invalid attribute: %s"), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100869 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200870 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200871# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200872 cmd = skipwhite(p);
873 }
874 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100875 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100876 // Make a copy of 'shell', an autocommand may change the option.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200877 tofree = cmd = vim_strsave(p_sh);
878
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100879 // default to close when the shell exits
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100880 if (opt.jo_term_finish == NUL)
Bram Moolenaare2978022020-04-26 14:47:44 +0200881 opt.jo_term_finish = TL_FINISH_CLOSE;
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100882 }
883
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200884 if (eap->addr_count > 0)
885 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100886 // Write lines from current buffer to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200887 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
888 opt.jo_io[PART_IN] = JIO_BUFFER;
889 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
890 opt.jo_in_top = eap->line1;
891 opt.jo_in_bot = eap->line2;
892 }
893
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100894 if (opt_shell && tofree == NULL)
895 {
896#ifdef UNIX
897 char **argv = NULL;
898 char_u *tofree1 = NULL;
899 char_u *tofree2 = NULL;
900
901 // :term ++shell command
902 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
903 term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaaradf4aa22019-11-10 22:36:44 +0100904 vim_free(argv);
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100905 vim_free(tofree1);
906 vim_free(tofree2);
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100907 goto theend;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100908#else
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100909# ifdef MSWIN
910 long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
911 char_u *newcmd;
912
913 newcmd = alloc(cmdlen);
914 if (newcmd == NULL)
915 goto theend;
916 tofree = newcmd;
917 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
918 cmd = newcmd;
919# else
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100920 emsg(_("E279: Sorry, ++shell is not supported on this system"));
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100921 goto theend;
922# endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100923#endif
924 }
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100925 argvar[0].v_type = VAR_STRING;
926 argvar[0].vval.v_string = cmd;
927 argvar[1].v_type = VAR_UNKNOWN;
928 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100929
930theend:
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100931 vim_free(tofree);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200932 vim_free(opt.jo_eof_chars);
933}
934
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100935#if defined(FEAT_SESSION) || defined(PROTO)
936/*
937 * Write a :terminal command to the session file to restore the terminal in
938 * window "wp".
939 * Return FAIL if writing fails.
940 */
941 int
Bram Moolenaar0e655112020-09-11 20:36:36 +0200942term_write_session(FILE *fd, win_T *wp, hashtab_T *terminal_bufs)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100943{
Bram Moolenaar0e655112020-09-11 20:36:36 +0200944 const int bufnr = wp->w_buffer->b_fnum;
945 term_T *term = wp->w_buffer->b_term;
946
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200947 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200948 {
949 // There are multiple views into this terminal buffer. We don't want to
950 // create the terminal multiple times. If it's the first time, create,
951 // otherwise link to the first buffer.
952 char id_as_str[NUMBUFLEN];
953 hashitem_T *entry;
954
955 vim_snprintf(id_as_str, sizeof(id_as_str), "%d", bufnr);
956
957 entry = hash_find(terminal_bufs, (char_u *)id_as_str);
958 if (!HASHITEM_EMPTY(entry))
959 {
960 // we've already opened this terminal buffer
961 if (fprintf(fd, "execute 'buffer ' . s:term_buf_%d", bufnr) < 0)
962 return FAIL;
963 return put_eol(fd);
964 }
965 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100966
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100967 // Create the terminal and run the command. This is not without
968 // risk, but let's assume the user only creates a session when this
969 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100970 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
971 term->tl_cols, term->tl_rows) < 0)
972 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100973#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100974 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
975 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100976#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100977 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
978 return FAIL;
Bram Moolenaar0e655112020-09-11 20:36:36 +0200979 if (put_eol(fd) != OK)
980 return FAIL;
981
982 if (fprintf(fd, "let s:term_buf_%d = bufnr()", bufnr) < 0)
983 return FAIL;
984
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200985 if (terminal_bufs != NULL && wp->w_buffer->b_nwindows > 1)
Bram Moolenaar0e655112020-09-11 20:36:36 +0200986 {
987 char *hash_key = alloc(NUMBUFLEN);
988
989 vim_snprintf(hash_key, NUMBUFLEN, "%d", bufnr);
990 hash_add(terminal_bufs, (char_u *)hash_key);
991 }
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100992
993 return put_eol(fd);
994}
995
996/*
997 * Return TRUE if "buf" has a terminal that should be restored.
998 */
999 int
1000term_should_restore(buf_T *buf)
1001{
1002 term_T *term = buf->b_term;
1003
1004 return term != NULL && (term->tl_command == NULL
1005 || STRCMP(term->tl_command, "NONE") != 0);
1006}
1007#endif
1008
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001009/*
1010 * Free the scrollback buffer for "term".
1011 */
1012 static void
1013free_scrollback(term_T *term)
1014{
1015 int i;
1016
1017 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
1018 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
1019 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001020 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
1021 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
1022 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001023}
1024
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001025
1026// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +02001027static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001028
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001029/*
1030 * Free a terminal and everything it refers to.
1031 * Kills the job if there is one.
1032 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001033 * The actual terminal structure is freed later in free_unused_terminals(),
1034 * because callbacks may wipe out a buffer while the terminal is still
1035 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001036 */
1037 void
1038free_terminal(buf_T *buf)
1039{
1040 term_T *term = buf->b_term;
1041 term_T *tp;
1042
1043 if (term == NULL)
1044 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001045
1046 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001047 if (first_term == term)
1048 first_term = term->tl_next;
1049 else
1050 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
1051 if (tp->tl_next == term)
1052 {
1053 tp->tl_next = term->tl_next;
1054 break;
1055 }
1056
1057 if (term->tl_job != NULL)
1058 {
1059 if (term->tl_job->jv_status != JOB_ENDED
1060 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +01001061 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001062 job_stop(term->tl_job, NULL, "kill");
1063 job_unref(term->tl_job);
1064 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001065 term->tl_next = terminals_to_free;
1066 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001067
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001068 buf->b_term = NULL;
1069 if (in_terminal_loop == term)
1070 in_terminal_loop = NULL;
1071}
1072
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001073 void
1074free_unused_terminals()
1075{
1076 while (terminals_to_free != NULL)
1077 {
1078 term_T *term = terminals_to_free;
1079
1080 terminals_to_free = term->tl_next;
1081
1082 free_scrollback(term);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02001083 ga_clear(&term->tl_osc_buf);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001084
1085 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001086 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001087 vim_free(term->tl_title);
1088#ifdef FEAT_SESSION
1089 vim_free(term->tl_command);
1090#endif
1091 vim_free(term->tl_kill);
1092 vim_free(term->tl_status_text);
1093 vim_free(term->tl_opencmd);
1094 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001095 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001096#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001097 if (term->tl_out_fd != NULL)
1098 fclose(term->tl_out_fd);
1099#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001100 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001101 vim_free(term->tl_cursor_color);
1102 vim_free(term);
1103 }
1104}
1105
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001106/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001107 * Get the part that is connected to the tty. Normally this is PART_IN, but
1108 * when writing buffer lines to the job it can be another. This makes it
1109 * possible to do "1,5term vim -".
1110 */
1111 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001112get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001113{
1114#ifdef UNIX
1115 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1116 int i;
1117
1118 for (i = 0; i < 3; ++i)
1119 {
1120 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1121
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001122 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001123 return parts[i];
1124 }
1125#endif
1126 return PART_IN;
1127}
1128
1129/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001130 * Write job output "msg[len]" to the vterm.
1131 */
1132 static void
1133term_write_job_output(term_T *term, char_u *msg, size_t len)
1134{
1135 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001136 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001137
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001138 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001139
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001140 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001141 if (prevlen != vterm_output_get_buffer_current(vterm))
1142 {
1143 char buf[KEY_BUF_LEN];
1144 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1145
1146 if (curlen > 0)
1147 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1148 (char_u *)buf, (int)curlen, NULL);
1149 }
1150
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001151 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001152 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1153}
1154
1155 static void
1156update_cursor(term_T *term, int redraw)
1157{
1158 if (term->tl_normal_mode)
1159 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001160#ifdef FEAT_GUI
1161 if (term->tl_system)
1162 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1163 term->tl_cursor_pos.col);
1164 else
1165#endif
1166 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001167 if (redraw)
1168 {
1169 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1170 cursor_on();
1171 out_flush();
1172#ifdef FEAT_GUI
1173 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001174 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001175 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001176 gui_mch_flush();
1177 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001178#endif
1179 }
1180}
1181
1182/*
1183 * Invoked when "msg" output from a job was received. Write it to the terminal
1184 * of "buffer".
1185 */
1186 void
1187write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1188{
1189 size_t len = STRLEN(msg);
1190 term_T *term = buffer->b_term;
1191
Bram Moolenaar4f974752019-02-17 17:44:42 +01001192#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001193 // Win32: Cannot redirect output of the job, intercept it here and write to
1194 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001195 if (term->tl_out_fd != NULL)
1196 {
1197 ch_log(channel, "Writing %d bytes to output file", (int)len);
1198 fwrite(msg, len, 1, term->tl_out_fd);
1199 return;
1200 }
1201#endif
1202
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001203 if (term->tl_vterm == NULL)
1204 {
1205 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1206 return;
1207 }
1208 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01001209 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001210 term_write_job_output(term, msg, len);
1211
Bram Moolenaar13568252018-03-16 20:46:58 +01001212#ifdef FEAT_GUI
1213 if (term->tl_system)
1214 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001215 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001216 update_system_term(term);
1217 update_cursor(term, TRUE);
1218 }
1219 else
1220#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001221 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1222 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001223 if (!term->tl_normal_mode)
1224 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001225 // Don't use update_screen() when editing the command line, it gets
1226 // cleared.
1227 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001228 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001229 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001230 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001231 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001232 // update_screen() can be slow, check the terminal wasn't closed
1233 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001234 if (buffer == curbuf && curbuf->b_term != NULL)
1235 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001236 }
1237 else
1238 redraw_after_callback(TRUE);
1239 }
1240}
1241
1242/*
1243 * Send a mouse position and click to the vterm
1244 */
1245 static int
1246term_send_mouse(VTerm *vterm, int button, int pressed)
1247{
1248 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001249 int row = mouse_row - W_WINROW(curwin);
1250 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001251
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001252#ifdef FEAT_PROP_POPUP
1253 if (popup_is_popup(curwin))
1254 {
1255 row -= popup_top_extra(curwin);
1256 col -= popup_left_extra(curwin);
1257 }
1258#endif
1259 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001260 if (button != 0)
1261 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001262 return TRUE;
1263}
1264
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001265static int enter_mouse_col = -1;
1266static int enter_mouse_row = -1;
1267
1268/*
1269 * Handle a mouse click, drag or release.
1270 * Return TRUE when a mouse event is sent to the terminal.
1271 */
1272 static int
1273term_mouse_click(VTerm *vterm, int key)
1274{
1275#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001276 // For modeless selection mouse drag and release events are ignored, unless
1277 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001278 static int ignore_drag_release = TRUE;
1279 VTermMouseState mouse_state;
1280
1281 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1282 if (mouse_state.flags == 0)
1283 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001284 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001285 switch (key)
1286 {
1287 case K_LEFTDRAG:
1288 case K_LEFTRELEASE:
1289 case K_RIGHTDRAG:
1290 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001291 // Ignore drag and release events when the button-down wasn't
1292 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001293 if (ignore_drag_release)
1294 {
1295 int save_mouse_col, save_mouse_row;
1296
1297 if (enter_mouse_col < 0)
1298 break;
1299
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001300 // mouse click in the window gave us focus, handle that
1301 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001302 save_mouse_col = mouse_col;
1303 save_mouse_row = mouse_row;
1304 mouse_col = enter_mouse_col;
1305 mouse_row = enter_mouse_row;
1306 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1307 mouse_col = save_mouse_col;
1308 mouse_row = save_mouse_row;
1309 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001310 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001311 case K_LEFTMOUSE:
1312 case K_RIGHTMOUSE:
1313 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1314 ignore_drag_release = TRUE;
1315 else
1316 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001317 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001318 if (clip_star.available)
1319 {
1320 int button, is_click, is_drag;
1321
1322 button = get_mouse_button(KEY2TERMCAP1(key),
1323 &is_click, &is_drag);
1324 if (mouse_model_popup() && button == MOUSE_LEFT
1325 && (mod_mask & MOD_MASK_SHIFT))
1326 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001327 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001328 button = MOUSE_RIGHT;
1329 mod_mask &= ~MOD_MASK_SHIFT;
1330 }
1331 clip_modeless(button, is_click, is_drag);
1332 }
1333 break;
1334
1335 case K_MIDDLEMOUSE:
1336 if (clip_star.available)
1337 insert_reg('*', TRUE);
1338 break;
1339 }
1340 enter_mouse_col = -1;
1341 return FALSE;
1342 }
1343#endif
1344 enter_mouse_col = -1;
1345
1346 switch (key)
1347 {
1348 case K_LEFTMOUSE:
1349 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1350 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1351 case K_LEFTRELEASE:
1352 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1353 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1354 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1355 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1356 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1357 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1358 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1359 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1360 }
1361 return TRUE;
1362}
1363
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001364/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001365 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1366 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001367 * Return the number of bytes in "buf".
1368 */
1369 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001370term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001371{
1372 VTerm *vterm = term->tl_vterm;
1373 VTermKey key = VTERM_KEY_NONE;
1374 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001375 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001376
1377 switch (c)
1378 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001379 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001380
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001381 // don't use VTERM_KEY_BACKSPACE, it always
1382 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001383 case K_BS: c = term_backspace_char; break;
1384
1385 case ESC: key = VTERM_KEY_ESCAPE; break;
1386 case K_DEL: key = VTERM_KEY_DEL; break;
1387 case K_DOWN: key = VTERM_KEY_DOWN; break;
1388 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1389 key = VTERM_KEY_DOWN; break;
1390 case K_END: key = VTERM_KEY_END; break;
1391 case K_S_END: mod = VTERM_MOD_SHIFT;
1392 key = VTERM_KEY_END; break;
1393 case K_C_END: mod = VTERM_MOD_CTRL;
1394 key = VTERM_KEY_END; break;
1395 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1396 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1397 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1398 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1399 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1400 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1401 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1402 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1403 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1404 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1405 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1406 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1407 case K_HOME: key = VTERM_KEY_HOME; break;
1408 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1409 key = VTERM_KEY_HOME; break;
1410 case K_C_HOME: mod = VTERM_MOD_CTRL;
1411 key = VTERM_KEY_HOME; break;
1412 case K_INS: key = VTERM_KEY_INS; break;
1413 case K_K0: key = VTERM_KEY_KP_0; break;
1414 case K_K1: key = VTERM_KEY_KP_1; break;
1415 case K_K2: key = VTERM_KEY_KP_2; break;
1416 case K_K3: key = VTERM_KEY_KP_3; break;
1417 case K_K4: key = VTERM_KEY_KP_4; break;
1418 case K_K5: key = VTERM_KEY_KP_5; break;
1419 case K_K6: key = VTERM_KEY_KP_6; break;
1420 case K_K7: key = VTERM_KEY_KP_7; break;
1421 case K_K8: key = VTERM_KEY_KP_8; break;
1422 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001423 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001424 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001425 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001426 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001427 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1428 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001429 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1430 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001431 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1432 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001433 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1434 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1435 case K_LEFT: key = VTERM_KEY_LEFT; break;
1436 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1437 key = VTERM_KEY_LEFT; break;
1438 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1439 key = VTERM_KEY_LEFT; break;
1440 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1441 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1442 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1443 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1444 key = VTERM_KEY_RIGHT; break;
1445 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1446 key = VTERM_KEY_RIGHT; break;
1447 case K_UP: key = VTERM_KEY_UP; break;
1448 case K_S_UP: mod = VTERM_MOD_SHIFT;
1449 key = VTERM_KEY_UP; break;
1450 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001451 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1452 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001453
Bram Moolenaara42ad572017-11-16 13:08:04 +01001454 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1455 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001456 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1457 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001458
1459 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001460 case K_LEFTMOUSE_NM:
1461 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001462 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001463 case K_LEFTRELEASE_NM:
1464 case K_MOUSEMOVE:
1465 case K_MIDDLEMOUSE:
1466 case K_MIDDLEDRAG:
1467 case K_MIDDLERELEASE:
1468 case K_RIGHTMOUSE:
1469 case K_RIGHTDRAG:
1470 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1471 return 0;
1472 other = TRUE;
1473 break;
1474
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001475 case K_X1MOUSE: /* TODO */ return 0;
1476 case K_X1DRAG: /* TODO */ return 0;
1477 case K_X1RELEASE: /* TODO */ return 0;
1478 case K_X2MOUSE: /* TODO */ return 0;
1479 case K_X2DRAG: /* TODO */ return 0;
1480 case K_X2RELEASE: /* TODO */ return 0;
1481
1482 case K_IGNORE: return 0;
1483 case K_NOP: return 0;
1484 case K_UNDO: return 0;
1485 case K_HELP: return 0;
1486 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1487 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1488 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1489 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1490 case K_SELECT: return 0;
1491#ifdef FEAT_GUI
1492 case K_VER_SCROLLBAR: return 0;
1493 case K_HOR_SCROLLBAR: return 0;
1494#endif
1495#ifdef FEAT_GUI_TABLINE
1496 case K_TABLINE: return 0;
1497 case K_TABMENU: return 0;
1498#endif
1499#ifdef FEAT_NETBEANS_INTG
1500 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1501#endif
1502#ifdef FEAT_DND
1503 case K_DROP: return 0;
1504#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001505 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001506 case K_PS: vterm_keyboard_start_paste(vterm);
1507 other = TRUE;
1508 break;
1509 case K_PE: vterm_keyboard_end_paste(vterm);
1510 other = TRUE;
1511 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001512 }
1513
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001514 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001515 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001516 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001517 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001518 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001519 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001520 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001521
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001522 /*
1523 * Convert special keys to vterm keys:
1524 * - Write keys to vterm: vterm_keyboard_key()
1525 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001526 */
1527 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001528 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001529 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001530 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001531 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001532 vterm_keyboard_unichar(vterm, c, mod);
1533
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001534 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001535 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1536}
1537
1538/*
1539 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001540 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001541 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001542 */
1543 static int
1544term_job_running_check(term_T *term, int check_job_status)
1545{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001546 // Also consider the job finished when the channel is closed, to avoid a
1547 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001548 if (term != NULL
1549 && term->tl_job != NULL
1550 && channel_is_open(term->tl_job->jv_channel))
1551 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001552 job_T *job = term->tl_job;
1553
1554 // Careful: Checking the job status may invoked callbacks, which close
1555 // the buffer and terminate "term". However, "job" will not be freed
1556 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001557 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001558 job_status(job);
1559 return (job->jv_status == JOB_STARTED
1560 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001561 }
1562 return FALSE;
1563}
1564
1565/*
1566 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001567 */
1568 int
1569term_job_running(term_T *term)
1570{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001571 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001572}
1573
1574/*
1575 * Return TRUE if "term" has an active channel and used ":term NONE".
1576 */
1577 int
1578term_none_open(term_T *term)
1579{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001580 // Also consider the job finished when the channel is closed, to avoid a
1581 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001582 return term != NULL
1583 && term->tl_job != NULL
1584 && channel_is_open(term->tl_job->jv_channel)
1585 && term->tl_job->jv_channel->ch_keep_open;
1586}
1587
1588/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001589 * Used when exiting: kill the job in "buf" if so desired.
1590 * Return OK when the job finished.
1591 * Return FAIL when the job is still running.
1592 */
1593 int
1594term_try_stop_job(buf_T *buf)
1595{
1596 int count;
1597 char *how = (char *)buf->b_term->tl_kill;
1598
1599#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001600 if ((how == NULL || *how == NUL)
1601 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001602 {
1603 char_u buff[DIALOG_MSG_SIZE];
1604 int ret;
1605
Bram Moolenaar00806bc2020-11-05 19:36:38 +01001606 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001607 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1608 if (ret == VIM_YES)
1609 how = "kill";
1610 else if (ret == VIM_CANCEL)
1611 return FAIL;
1612 }
1613#endif
1614 if (how == NULL || *how == NUL)
1615 return FAIL;
1616
1617 job_stop(buf->b_term->tl_job, NULL, how);
1618
Bram Moolenaar9172d232019-01-29 23:06:54 +01001619 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001620 for (count = 0; count < 100; ++count)
1621 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001622 job_T *job;
1623
1624 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001625 if (!buf_valid(buf)
1626 || buf->b_term == NULL
1627 || buf->b_term->tl_job == NULL)
1628 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001629 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001630
Bram Moolenaar9172d232019-01-29 23:06:54 +01001631 // Call job_status() to update jv_status. It may cause the job to be
1632 // cleaned up but it won't be freed.
1633 job_status(job);
1634 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001635 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001636
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001637 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001638 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001639 }
1640 return FAIL;
1641}
1642
1643/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001644 * Add the last line of the scrollback buffer to the buffer in the window.
1645 */
1646 static void
1647add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1648{
1649 buf_T *buf = term->tl_buffer;
1650 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1651 linenr_T lnum = buf->b_ml.ml_line_count;
1652
Bram Moolenaar4f974752019-02-17 17:44:42 +01001653#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001654 if (!enc_utf8 && enc_codepage > 0)
1655 {
1656 WCHAR *ret = NULL;
1657 int length = 0;
1658
1659 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1660 &ret, &length);
1661 if (ret != NULL)
1662 {
1663 WideCharToMultiByte_alloc(enc_codepage, 0,
1664 ret, length, (char **)&text, &len, 0, 0);
1665 vim_free(ret);
1666 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1667 vim_free(text);
1668 }
1669 }
1670 else
1671#endif
1672 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1673 if (empty)
1674 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001675 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001676 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001677 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001678 curbuf = curwin->w_buffer;
1679 }
1680}
1681
1682 static void
1683cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1684{
1685 attr->width = cell->width;
1686 attr->attrs = cell->attrs;
1687 attr->fg = cell->fg;
1688 attr->bg = cell->bg;
1689}
1690
1691 static int
1692equal_celattr(cellattr_T *a, cellattr_T *b)
1693{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001694 // We only compare the RGB colors, ignoring the ANSI index and type.
1695 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001696 return a->fg.red == b->fg.red
1697 && a->fg.green == b->fg.green
1698 && a->fg.blue == b->fg.blue
1699 && a->bg.red == b->bg.red
1700 && a->bg.green == b->bg.green
1701 && a->bg.blue == b->bg.blue;
1702}
1703
Bram Moolenaard96ff162018-02-18 22:13:29 +01001704/*
1705 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1706 * line at this position. Otherwise at the end.
1707 */
1708 static int
1709add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1710{
1711 if (ga_grow(&term->tl_scrollback, 1) == OK)
1712 {
1713 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1714 + term->tl_scrollback.ga_len;
1715
1716 if (lnum > 0)
1717 {
1718 int i;
1719
1720 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1721 {
1722 *line = *(line - 1);
1723 --line;
1724 }
1725 }
1726 line->sb_cols = 0;
1727 line->sb_cells = NULL;
1728 line->sb_fill_attr = *fill_attr;
1729 ++term->tl_scrollback.ga_len;
1730 return OK;
1731 }
1732 return FALSE;
1733}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001734
1735/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001736 * Remove the terminal contents from the scrollback and the buffer.
1737 * Used before adding a new scrollback line or updating the buffer for lines
1738 * displayed in the terminal.
1739 */
1740 static void
1741cleanup_scrollback(term_T *term)
1742{
1743 sb_line_T *line;
1744 garray_T *gap;
1745
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001746 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001747 gap = &term->tl_scrollback;
1748 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1749 && gap->ga_len > 0)
1750 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001751 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001752 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1753 vim_free(line->sb_cells);
1754 --gap->ga_len;
1755 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001756 curbuf = curwin->w_buffer;
1757 if (curbuf == term->tl_buffer)
1758 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001759}
1760
1761/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001762 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001763 */
1764 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001765update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001766{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001767 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001768 int len;
1769 int lines_skipped = 0;
1770 VTermPos pos;
1771 VTermScreenCell cell;
1772 cellattr_T fill_attr, new_fill_attr;
1773 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001774
1775 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1776 "Adding terminal window snapshot to buffer");
1777
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001778 // First remove the lines that were appended before, they might be
1779 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001780 cleanup_scrollback(term);
1781
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001782 screen = vterm_obtain_screen(term->tl_vterm);
1783 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001784 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1785 {
1786 len = 0;
1787 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1788 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1789 && cell.chars[0] != NUL)
1790 {
1791 len = pos.col + 1;
1792 new_fill_attr = term->tl_default_color;
1793 }
1794 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001795 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001796 cell2cellattr(&cell, &new_fill_attr);
1797
1798 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1799 ++lines_skipped;
1800 else
1801 {
1802 while (lines_skipped > 0)
1803 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001804 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001805 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001806 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001807 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001808 }
1809
1810 if (len == 0)
1811 p = NULL;
1812 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001813 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001814 if ((p != NULL || len == 0)
1815 && ga_grow(&term->tl_scrollback, 1) == OK)
1816 {
1817 garray_T ga;
1818 int width;
1819 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1820 + term->tl_scrollback.ga_len;
1821
1822 ga_init2(&ga, 1, 100);
1823 for (pos.col = 0; pos.col < len; pos.col += width)
1824 {
1825 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1826 {
1827 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001828 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001829 if (ga_grow(&ga, 1) == OK)
1830 ga.ga_len += utf_char2bytes(' ',
1831 (char_u *)ga.ga_data + ga.ga_len);
1832 }
1833 else
1834 {
1835 width = cell.width;
1836
1837 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001838 if (width == 2)
1839 // second cell of double-width character has the
1840 // same attributes.
1841 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001842
Bram Moolenaara79fd562018-12-20 20:47:32 +01001843 // Each character can be up to 6 bytes.
1844 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001845 {
1846 int i;
1847 int c;
1848
1849 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1850 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1851 (char_u *)ga.ga_data + ga.ga_len);
1852 }
1853 }
1854 }
1855 line->sb_cols = len;
1856 line->sb_cells = p;
1857 line->sb_fill_attr = new_fill_attr;
1858 fill_attr = new_fill_attr;
1859 ++term->tl_scrollback.ga_len;
1860
1861 if (ga_grow(&ga, 1) == FAIL)
1862 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1863 else
1864 {
1865 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1866 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1867 }
1868 ga_clear(&ga);
1869 }
1870 else
1871 vim_free(p);
1872 }
1873 }
1874
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001875 // Add trailing empty lines.
1876 for (pos.row = term->tl_scrollback.ga_len;
1877 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1878 ++pos.row)
1879 {
1880 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1881 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1882 }
1883
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001884 term->tl_dirty_snapshot = FALSE;
1885#ifdef FEAT_TIMERS
1886 term->tl_timer_set = FALSE;
1887#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001888}
1889
1890/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001891 * Loop over all windows in the current tab, and also curwin, which is not
1892 * encountered when using a terminal in a popup window.
1893 * Return TRUE if "*wp" was set to the next window.
1894 */
1895 static int
1896for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1897{
1898 if (*wp == NULL)
1899 *wp = firstwin;
1900 else if ((*wp)->w_next != NULL)
1901 *wp = (*wp)->w_next;
1902 else if (!*did_curwin)
1903 *wp = curwin;
1904 else
1905 return FALSE;
1906 if (*wp == curwin)
1907 *did_curwin = TRUE;
1908 return TRUE;
1909}
1910
1911/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001912 * If needed, add the current lines of the terminal to scrollback and to the
1913 * buffer. Called after the job has ended and when switching to
1914 * Terminal-Normal mode.
1915 * When "redraw" is TRUE redraw the windows that show the terminal.
1916 */
1917 static void
1918may_move_terminal_to_buffer(term_T *term, int redraw)
1919{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001920 if (term->tl_vterm == NULL)
1921 return;
1922
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001923 // Update the snapshot only if something changes or the buffer does not
1924 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001925 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1926 <= term->tl_scrollback_scrolled)
1927 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001928
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001929 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001930 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1931 &term->tl_default_color.fg, &term->tl_default_color.bg);
1932
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001933 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001934 {
1935 win_T *wp = NULL;
1936 int did_curwin = FALSE;
1937
1938 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001939 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001940 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001941 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001942 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1943 wp->w_cursor.col = 0;
1944 wp->w_valid = 0;
1945 if (wp->w_cursor.lnum >= wp->w_height)
1946 {
1947 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001948
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001949 if (wp->w_topline < min_topline)
1950 wp->w_topline = min_topline;
1951 }
1952 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001953 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001954 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001955 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001956}
1957
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001958#if defined(FEAT_TIMERS) || defined(PROTO)
1959/*
1960 * Check if any terminal timer expired. If so, copy text from the terminal to
1961 * the buffer.
1962 * Return the time until the next timer will expire.
1963 */
1964 int
1965term_check_timers(int next_due_arg, proftime_T *now)
1966{
1967 term_T *term;
1968 int next_due = next_due_arg;
1969
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001970 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001971 {
1972 if (term->tl_timer_set && !term->tl_normal_mode)
1973 {
1974 long this_due = proftime_time_left(&term->tl_timer_due, now);
1975
1976 if (this_due <= 1)
1977 {
1978 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001979 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001980 }
1981 else if (next_due == -1 || next_due > this_due)
1982 next_due = this_due;
1983 }
1984 }
1985
1986 return next_due;
1987}
1988#endif
1989
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001990/*
1991 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
1992 * otherwise end it.
1993 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001994 static void
1995set_terminal_mode(term_T *term, int normal_mode)
1996{
1997 term->tl_normal_mode = normal_mode;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001998 if (!normal_mode)
1999 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002000 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002001 if (term->tl_buffer == curbuf)
2002 maketitle();
2003}
2004
2005/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002006 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002007 * Move the vterm contents into the scrollback buffer and free the vterm.
2008 */
2009 static void
2010cleanup_vterm(term_T *term)
2011{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002012 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002013 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002014 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002015 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002016}
2017
2018/*
2019 * Switch from Terminal-Job mode to Terminal-Normal mode.
2020 * Suspends updating the terminal window.
2021 */
2022 static void
2023term_enter_normal_mode(void)
2024{
2025 term_T *term = curbuf->b_term;
2026
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002027 set_terminal_mode(term, TRUE);
2028
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002029 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002030 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002031
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002032 // Move the window cursor to the position of the cursor in the
2033 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002034 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2035 + term->tl_cursor_pos.row + 1;
2036 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002037 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2038 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002039 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002040
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002041 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002042 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2043}
2044
2045/*
2046 * Returns TRUE if the current window contains a terminal and we are in
2047 * Terminal-Normal mode.
2048 */
2049 int
2050term_in_normal_mode(void)
2051{
2052 term_T *term = curbuf->b_term;
2053
2054 return term != NULL && term->tl_normal_mode;
2055}
2056
2057/*
2058 * Switch from Terminal-Normal mode to Terminal-Job mode.
2059 * Restores updating the terminal window.
2060 */
2061 void
2062term_enter_job_mode()
2063{
2064 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002065
2066 set_terminal_mode(term, FALSE);
2067
2068 if (term->tl_channel_closed)
2069 cleanup_vterm(term);
2070 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002071#ifdef FEAT_PROP_POPUP
2072 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002073 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002074#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002075}
2076
2077/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002078 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002079 * Note: while waiting a terminal may be closed and freed if the channel is
2080 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002081 */
2082 static int
2083term_vgetc()
2084{
2085 int c;
2086 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002087 int modify_other_keys =
2088 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002089
2090 State = TERMINAL;
2091 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002092#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002093 ctrl_break_was_pressed = FALSE;
2094#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002095 if (modify_other_keys)
2096 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002097 c = vgetc();
2098 got_int = FALSE;
2099 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002100 if (modify_other_keys)
2101 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002102 return c;
2103}
2104
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002105static int mouse_was_outside = FALSE;
2106
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002107/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002108 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002109 * Return FAIL when the key needs to be handled in Normal mode.
2110 * Return OK when the key was dropped or sent to the terminal.
2111 */
2112 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002113send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002114{
2115 char msg[KEY_BUF_LEN];
2116 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002117 int dragging_outside = FALSE;
2118
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002119 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002120 switch (c)
2121 {
2122 case NUL:
2123 case K_ZERO:
2124 if (typed)
2125 stuffcharReadbuff(c);
2126 return FAIL;
2127
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002128 case K_TABLINE:
2129 stuffcharReadbuff(c);
2130 return FAIL;
2131
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002132 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002133 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002134 return FAIL;
2135
2136 case K_LEFTDRAG:
2137 case K_MIDDLEDRAG:
2138 case K_RIGHTDRAG:
2139 case K_X1DRAG:
2140 case K_X2DRAG:
2141 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002142 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002143 case K_LEFTMOUSE:
2144 case K_LEFTMOUSE_NM:
2145 case K_LEFTRELEASE:
2146 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002147 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002148 case K_MIDDLEMOUSE:
2149 case K_MIDDLERELEASE:
2150 case K_RIGHTMOUSE:
2151 case K_RIGHTRELEASE:
2152 case K_X1MOUSE:
2153 case K_X1RELEASE:
2154 case K_X2MOUSE:
2155 case K_X2RELEASE:
2156
2157 case K_MOUSEUP:
2158 case K_MOUSEDOWN:
2159 case K_MOUSELEFT:
2160 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002161 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002162 int row = mouse_row;
2163 int col = mouse_col;
2164
2165#ifdef FEAT_PROP_POPUP
2166 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002167 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002168 row -= popup_top_extra(curwin);
2169 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002170 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002171#endif
2172 if (row < W_WINROW(curwin)
2173 || row >= (W_WINROW(curwin) + curwin->w_height)
2174 || col < curwin->w_wincol
2175 || col >= W_ENDCOL(curwin)
2176 || dragging_outside)
2177 {
2178 // click or scroll outside the current window or on status
2179 // line or vertical separator
2180 if (typed)
2181 {
2182 stuffcharReadbuff(c);
2183 mouse_was_outside = TRUE;
2184 }
2185 return FAIL;
2186 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002187 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002188 break;
2189
2190 case K_COMMAND:
2191 return do_cmdline(NULL, getcmdkeycmd, NULL, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002192 }
2193 if (typed)
2194 mouse_was_outside = FALSE;
2195
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002196 // Convert the typed key to a sequence of bytes for the job.
2197 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002198 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002199 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002200 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2201 (char_u *)msg, (int)len, NULL);
2202
2203 return OK;
2204}
2205
2206 static void
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002207position_cursor(win_T *wp, VTermPos *pos)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002208{
2209 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2210 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002211#ifdef FEAT_PROP_POPUP
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002212 if (popup_is_popup(wp))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002213 {
Bram Moolenaarf5452692020-11-28 21:56:06 +01002214 wp->w_wrow += popup_top_extra(wp);
2215 wp->w_wcol += popup_left_extra(wp);
Bram Moolenaar6a076442020-11-15 20:32:58 +01002216 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002217 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01002218 else
2219 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002220#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002221 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2222}
2223
2224/*
2225 * Handle CTRL-W "": send register contents to the job.
2226 */
2227 static void
2228term_paste_register(int prev_c UNUSED)
2229{
2230 int c;
2231 list_T *l;
2232 listitem_T *item;
2233 long reglen = 0;
2234 int type;
2235
2236#ifdef FEAT_CMDL_INFO
2237 if (add_to_showcmd(prev_c))
2238 if (add_to_showcmd('"'))
2239 out_flush();
2240#endif
2241 c = term_vgetc();
2242#ifdef FEAT_CMDL_INFO
2243 clear_showcmd();
2244#endif
2245 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002246 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002247 return;
2248
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002249 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002250 if (c == '=' && get_expr_register() != '=')
2251 return;
2252 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002253 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002254 return;
2255
2256 l = (list_T *)get_reg_contents(c, GREG_LIST);
2257 if (l != NULL)
2258 {
2259 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002260 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002261 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002262 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002263#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002264 char_u *tmp = s;
2265
2266 if (!enc_utf8 && enc_codepage > 0)
2267 {
2268 WCHAR *ret = NULL;
2269 int length = 0;
2270
2271 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2272 (int)STRLEN(s), &ret, &length);
2273 if (ret != NULL)
2274 {
2275 WideCharToMultiByte_alloc(CP_UTF8, 0,
2276 ret, length, (char **)&s, &length, 0, 0);
2277 vim_free(ret);
2278 }
2279 }
2280#endif
2281 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2282 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002283#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002284 if (tmp != s)
2285 vim_free(s);
2286#endif
2287
2288 if (item->li_next != NULL || type == MLINE)
2289 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2290 (char_u *)"\r", 1, NULL);
2291 }
2292 list_free(l);
2293 }
2294}
2295
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002296/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002297 * Return TRUE when waiting for a character in the terminal, the cursor of the
2298 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002299 */
2300 int
2301terminal_is_active()
2302{
2303 return in_terminal_loop != NULL;
2304}
2305
Bram Moolenaar83d47902020-03-26 20:34:00 +01002306/*
2307 * Return the highight group name for the terminal; "Terminal" if not set.
2308 */
2309 static char_u *
2310term_get_highlight_name(term_T *term)
2311{
2312 if (term->tl_highlight_name == NULL)
2313 return (char_u *)"Terminal";
2314 return term->tl_highlight_name;
2315}
2316
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002317#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002318 cursorentry_T *
2319term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2320{
2321 term_T *term = in_terminal_loop;
2322 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002323 int id;
2324 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002325
Bram Moolenaara80faa82020-04-12 19:37:17 +02002326 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002327 entry.shape = entry.mshape =
2328 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2329 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2330 SHAPE_BLOCK;
2331 entry.percentage = 20;
2332 if (term->tl_cursor_blink)
2333 {
2334 entry.blinkwait = 700;
2335 entry.blinkon = 400;
2336 entry.blinkoff = 250;
2337 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002338
Bram Moolenaar83d47902020-03-26 20:34:00 +01002339 // The highlight group overrules the defaults.
2340 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002341 if (id != 0)
2342 {
2343 syn_id2colors(id, &term_fg, &term_bg);
2344 *fg = term_bg;
2345 }
2346 else
2347 *fg = gui.back_pixel;
2348
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002349 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002350 {
2351 if (id != 0)
2352 *bg = term_fg;
2353 else
2354 *bg = gui.norm_pixel;
2355 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002356 else
2357 *bg = color_name2handle(term->tl_cursor_color);
2358 entry.name = "n";
2359 entry.used_for = SHAPE_CURSOR;
2360
2361 return &entry;
2362}
2363#endif
2364
Bram Moolenaard317b382018-02-08 22:33:31 +01002365 static void
2366may_output_cursor_props(void)
2367{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002368 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002369 || last_set_cursor_shape != desired_cursor_shape
2370 || last_set_cursor_blink != desired_cursor_blink)
2371 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002372 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002373 last_set_cursor_shape = desired_cursor_shape;
2374 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002375 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002376 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002377 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002378 ui_cursor_shape_forced(TRUE);
2379 else
2380 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2381 }
2382}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002383
Bram Moolenaard317b382018-02-08 22:33:31 +01002384/*
2385 * Set the cursor color and shape, if not last set to these.
2386 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002387 static void
2388may_set_cursor_props(term_T *term)
2389{
2390#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002391 // For the GUI the cursor properties are obtained with
2392 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002393 if (gui.in_use)
2394 return;
2395#endif
2396 if (in_terminal_loop == term)
2397 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002398 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002399 desired_cursor_shape = term->tl_cursor_shape;
2400 desired_cursor_blink = term->tl_cursor_blink;
2401 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002402 }
2403}
2404
Bram Moolenaard317b382018-02-08 22:33:31 +01002405/*
2406 * Reset the desired cursor properties and restore them when needed.
2407 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002408 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002409prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002410{
2411#ifdef FEAT_GUI
2412 if (gui.in_use)
2413 return;
2414#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002415 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002416 desired_cursor_shape = -1;
2417 desired_cursor_blink = -1;
2418 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002419}
2420
2421/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002422 * Returns TRUE if the current window contains a terminal and we are sending
2423 * keys to the job.
2424 * If "check_job_status" is TRUE update the job status.
2425 */
2426 static int
2427term_use_loop_check(int check_job_status)
2428{
2429 term_T *term = curbuf->b_term;
2430
2431 return term != NULL
2432 && !term->tl_normal_mode
2433 && term->tl_vterm != NULL
2434 && term_job_running_check(term, check_job_status);
2435}
2436
2437/*
2438 * Returns TRUE if the current window contains a terminal and we are sending
2439 * keys to the job.
2440 */
2441 int
2442term_use_loop(void)
2443{
2444 return term_use_loop_check(FALSE);
2445}
2446
2447/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002448 * Called when entering a window with the mouse. If this is a terminal window
2449 * we may want to change state.
2450 */
2451 void
2452term_win_entered()
2453{
2454 term_T *term = curbuf->b_term;
2455
2456 if (term != NULL)
2457 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002458 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002459 {
2460 reset_VIsual_and_resel();
2461 if (State & INSERT)
2462 stop_insert_mode = TRUE;
2463 }
2464 mouse_was_outside = FALSE;
2465 enter_mouse_col = mouse_col;
2466 enter_mouse_row = mouse_row;
2467 }
2468}
2469
2470/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002471 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2472 * Return the Ctrl-key value in that case.
2473 */
2474 static int
2475raw_c_to_ctrl(int c)
2476{
2477 if ((mod_mask & MOD_MASK_CTRL)
2478 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2479 return c & 0x1f;
2480 return c;
2481}
2482
2483/*
2484 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2485 * May set "mod_mask".
2486 */
2487 static int
2488ctrl_to_raw_c(int c)
2489{
2490 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2491 {
2492 mod_mask |= MOD_MASK_CTRL;
2493 return c + '@';
2494 }
2495 return c;
2496}
2497
2498/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002499 * Wait for input and send it to the job.
2500 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2501 * when there is no more typahead.
2502 * Return when the start of a CTRL-W command is typed or anything else that
2503 * should be handled as a Normal mode command.
2504 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2505 * the terminal was closed.
2506 */
2507 int
2508terminal_loop(int blocking)
2509{
2510 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002511 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002512 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002513 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002514#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002515 int tty_fd = curbuf->b_term->tl_job->jv_channel
2516 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002517#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002518 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002519
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002520 // Remember the terminal we are sending keys to. However, the terminal
2521 // might be closed while waiting for a character, e.g. typing "exit" in a
2522 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2523 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002524 in_terminal_loop = curbuf->b_term;
2525
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002526 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002527 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002528 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002529 if (termwinkey == Ctrl_W)
2530 termwinkey = 0;
2531 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002532 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002533 may_set_cursor_props(curbuf->b_term);
2534
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002535 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002536 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002537#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002538 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002539#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002540 // TODO: skip screen update when handling a sequence of keys.
2541 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002542 while (must_redraw != 0)
2543 if (update_screen(0) == FAIL)
2544 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002545 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002546 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002547 break;
2548
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002549 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002550 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002551
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002552 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002553 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002554 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002555 // Job finished while waiting for a character. Push back the
2556 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002557 if (raw_c != K_IGNORE)
2558 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002559 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002560 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002561 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002562 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002563 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002564
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002565#ifdef UNIX
2566 /*
2567 * The shell or another program may change the tty settings. Getting
2568 * them for every typed character is a bit of overhead, but it's needed
2569 * for the first character typed, e.g. when Vim starts in a shell.
2570 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002571 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002572 {
2573 ttyinfo_T info;
2574
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002575 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002576 if (get_tty_info(tty_fd, &info) == OK)
2577 term_backspace_char = info.backspace;
2578 }
2579#endif
2580
Bram Moolenaar4f974752019-02-17 17:44:42 +01002581#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002582 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2583 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002584 if (ctrl_break_was_pressed)
2585 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2586#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002587 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2588 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002589 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002590#ifdef FEAT_GUI
2591 && !curbuf->b_term->tl_system
2592#endif
2593 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002594 {
2595 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002596 int prev_raw_c = raw_c;
2597 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002598
2599#ifdef FEAT_CMDL_INFO
2600 if (add_to_showcmd(c))
2601 out_flush();
2602#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002603 raw_c = term_vgetc();
2604 c = raw_c_to_ctrl(raw_c);
2605
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002606#ifdef FEAT_CMDL_INFO
2607 clear_showcmd();
2608#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002609 if (!term_use_loop_check(TRUE)
2610 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002611 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002612 break;
2613
2614 if (prev_c == Ctrl_BSL)
2615 {
2616 if (c == Ctrl_N)
2617 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002618 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002619 term_enter_normal_mode();
2620 ret = FAIL;
2621 goto theend;
2622 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002623 // Send both keys to the terminal, first one here, second one
2624 // below.
2625 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2626 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002627 }
2628 else if (c == Ctrl_C)
2629 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002630 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002631 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2632 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002633 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002634 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002635 // "CTRL-W .": send CTRL-W to the job
2636 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002637 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002638 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002639 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002640 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002641 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002642 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002643 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002644 else if (c == 'N')
2645 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002646 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002647 term_enter_normal_mode();
2648 ret = FAIL;
2649 goto theend;
2650 }
2651 else if (c == '"')
2652 {
2653 term_paste_register(prev_c);
2654 continue;
2655 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002656 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002657 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002658 // space for CTRL-W, modifier, multi-byte char and NUL
2659 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002660
2661 // Put the command into the typeahead buffer, when using the
2662 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2663 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002664 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002665 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002666 ret = OK;
2667 goto theend;
2668 }
2669 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002670# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002671 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002672 {
2673 WCHAR wc;
2674 char_u mb[3];
2675
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002676 mb[0] = (unsigned)raw_c >> 8;
2677 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002678 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002679 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002680 }
2681# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002682 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002683 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002684 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002685 // We are sure to come back here, don't reset the cursor color
2686 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002687 restore_cursor = FALSE;
2688
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002689 ret = OK;
2690 goto theend;
2691 }
2692 }
2693 ret = FAIL;
2694
2695theend:
2696 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002697 if (restore_cursor)
2698 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002699
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002700 // Move a snapshot of the screen contents to the buffer, so that completion
2701 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002702 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2703 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002704
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002705 return ret;
2706}
2707
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002708 static void
2709may_toggle_cursor(term_T *term)
2710{
2711 if (in_terminal_loop == term)
2712 {
2713 if (term->tl_cursor_visible)
2714 cursor_on();
2715 else
2716 cursor_off();
2717 }
2718}
2719
2720/*
2721 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002722 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002723 */
2724 static int
2725color2index(VTermColor *color, int fg, int *boldp)
2726{
2727 int red = color->red;
2728 int blue = color->blue;
2729 int green = color->green;
2730
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002731 if (VTERM_COLOR_IS_DEFAULT_FG(color)
2732 || VTERM_COLOR_IS_DEFAULT_BG(color))
2733 return 0;
2734 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002735 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002736 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002737 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002738 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002739 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002740 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2741 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2742 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002743 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002744 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2745 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2746 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2747 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2748 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2749 case 10: return lookup_color(20, fg, boldp) + 1; // red
2750 case 11: return lookup_color(16, fg, boldp) + 1; // green
2751 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2752 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2753 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2754 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2755 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002756 }
2757 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002758
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002759 if (t_colors >= 256)
2760 {
2761 if (red == blue && red == green)
2762 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002763 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002764 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002765 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2766 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2767 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002768 int i;
2769
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002770 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002771 return 17; // 00/00/00
2772 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002773 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002774 for (i = 0; i < 23; ++i)
2775 if (red < cutoff[i])
2776 return i + 233;
2777 return 256;
2778 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002779 {
2780 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2781 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002782
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002783 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002784 for (ri = 0; ri < 5; ++ri)
2785 if (red < cutoff[ri])
2786 break;
2787 for (gi = 0; gi < 5; ++gi)
2788 if (green < cutoff[gi])
2789 break;
2790 for (bi = 0; bi < 5; ++bi)
2791 if (blue < cutoff[bi])
2792 break;
2793 return 17 + ri * 36 + gi * 6 + bi;
2794 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002795 }
2796 return 0;
2797}
2798
2799/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002800 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002801 */
2802 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002803vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002804{
2805 int attr = 0;
2806
2807 if (cellattrs.bold)
2808 attr |= HL_BOLD;
2809 if (cellattrs.underline)
2810 attr |= HL_UNDERLINE;
2811 if (cellattrs.italic)
2812 attr |= HL_ITALIC;
2813 if (cellattrs.strike)
2814 attr |= HL_STRIKETHROUGH;
2815 if (cellattrs.reverse)
2816 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002817 return attr;
2818}
2819
2820/*
2821 * Store Vterm attributes in "cell" from highlight flags.
2822 */
2823 static void
2824hl2vtermAttr(int attr, cellattr_T *cell)
2825{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002826 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002827 if (attr & HL_BOLD)
2828 cell->attrs.bold = 1;
2829 if (attr & HL_UNDERLINE)
2830 cell->attrs.underline = 1;
2831 if (attr & HL_ITALIC)
2832 cell->attrs.italic = 1;
2833 if (attr & HL_STRIKETHROUGH)
2834 cell->attrs.strike = 1;
2835 if (attr & HL_INVERSE)
2836 cell->attrs.reverse = 1;
2837}
2838
2839/*
2840 * Convert the attributes of a vterm cell into an attribute index.
2841 */
2842 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002843cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002844 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002845 win_T *wp,
2846 VTermScreenCellAttrs cellattrs,
2847 VTermColor cellfg,
2848 VTermColor cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002849{
2850 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002851
2852#ifdef FEAT_GUI
2853 if (gui.in_use)
2854 {
2855 guicolor_T fg, bg;
2856
2857 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2858 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2859 return get_gui_attr_idx(attr, fg, bg);
2860 }
2861 else
2862#endif
2863#ifdef FEAT_TERMGUICOLORS
2864 if (p_tgc)
2865 {
Milly7b5f45b2021-10-15 22:25:43 +01002866 guicolor_T fg = INVALCOLOR;
2867 guicolor_T bg = INVALCOLOR;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002868
Milly7b5f45b2021-10-15 22:25:43 +01002869 // Use the 'wincolor' or "Terminal" highlighting for the default
2870 // colors.
2871 if (VTERM_COLOR_IS_DEFAULT_FG(&cellfg)
2872 || VTERM_COLOR_IS_DEFAULT_BG(&cellbg))
2873 {
2874 int id = 0;
2875
2876 if (wp != NULL && *wp->w_p_wcr != NUL)
2877 id = syn_name2id(wp->w_p_wcr);
2878 if (id == 0)
2879 id = syn_name2id(term_get_highlight_name(term));
2880 if (id > 0)
2881 syn_id2colors(id, &fg, &bg);
2882 if (!VTERM_COLOR_IS_DEFAULT_FG(&cellfg))
2883 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green,
2884 cellfg.blue);
2885 if (!VTERM_COLOR_IS_DEFAULT_BG(&cellbg))
2886 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green,
2887 cellbg.blue);
2888 }
2889 else
2890 {
2891 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2892 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2893 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002894
2895 return get_tgc_attr_idx(attr, fg, bg);
2896 }
2897 else
2898#endif
2899 {
2900 int bold = MAYBE;
2901 int fg = color2index(&cellfg, TRUE, &bold);
2902 int bg = color2index(&cellbg, FALSE, &bold);
2903
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002904 // Use the 'wincolor' or "Terminal" highlighting for the default
2905 // colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002906 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002907 {
Milly7b5f45b2021-10-15 22:25:43 +01002908 int cterm_fg = -1;
2909 int cterm_bg = -1;
2910 int id = 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002911
2912 if (wp != NULL && *wp->w_p_wcr != NUL)
Milly7b5f45b2021-10-15 22:25:43 +01002913 id = syn_name2id(wp->w_p_wcr);
2914 if (id == 0)
2915 id = syn_name2id(term_get_highlight_name(term));
2916 if (id > 0)
2917 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
2918 if (fg == 0 && cterm_fg >= 0)
2919 fg = cterm_fg + 1;
2920 if (bg == 0 && cterm_bg >= 0)
2921 bg = cterm_bg + 1;
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002922 }
2923
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002924 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002925 if (bold == TRUE)
2926 attr |= HL_BOLD;
2927 return get_cterm_attr_idx(attr, fg, bg);
2928 }
2929 return 0;
2930}
2931
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002932 static void
2933set_dirty_snapshot(term_T *term)
2934{
2935 term->tl_dirty_snapshot = TRUE;
2936#ifdef FEAT_TIMERS
2937 if (!term->tl_normal_mode)
2938 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002939 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002940 profile_setlimit(100L, &term->tl_timer_due);
2941 term->tl_timer_set = TRUE;
2942 }
2943#endif
2944}
2945
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002946 static int
2947handle_damage(VTermRect rect, void *user)
2948{
2949 term_T *term = (term_T *)user;
2950
2951 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2952 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002953 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002954 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002955 return 1;
2956}
2957
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002958 static void
2959term_scroll_up(term_T *term, int start_row, int count)
2960{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002961 win_T *wp = NULL;
2962 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002963 VTermColor fg, bg;
2964 VTermScreenCellAttrs attr;
2965 int clear_attr;
2966
Bram Moolenaara80faa82020-04-12 19:37:17 +02002967 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002968
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002969 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002970 {
2971 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002972 {
2973 // Set the color to clear lines with.
2974 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2975 &fg, &bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002976 clear_attr = cell2attr(term, wp, attr, fg, bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002977 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002978 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002979 }
2980}
2981
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002982 static int
2983handle_moverect(VTermRect dest, VTermRect src, void *user)
2984{
2985 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002986 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002987
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002988 // Scrolling up is done much more efficiently by deleting lines instead of
2989 // redrawing the text. But avoid doing this multiple times, postpone until
2990 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002991 if (dest.start_col == src.start_col
2992 && dest.end_col == src.end_col
2993 && dest.start_row < src.start_row)
2994 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002995 if (dest.start_row == 0)
2996 term->tl_postponed_scroll += count;
2997 else
2998 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002999 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003000
3001 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3002 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003003 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003004
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003005 // Note sure if the scrolling will work correctly, let's do a complete
3006 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003007 redraw_buf_later(term->tl_buffer, NOT_VALID);
3008 return 1;
3009}
3010
3011 static int
3012handle_movecursor(
3013 VTermPos pos,
3014 VTermPos oldpos UNUSED,
3015 int visible,
3016 void *user)
3017{
3018 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003019 win_T *wp = NULL;
3020 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003021
3022 term->tl_cursor_pos = pos;
3023 term->tl_cursor_visible = visible;
3024
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003025 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003026 {
3027 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003028 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003029 }
3030 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003031 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003032
3033 return 1;
3034}
3035
3036 static int
3037handle_settermprop(
3038 VTermProp prop,
3039 VTermValue *value,
3040 void *user)
3041{
3042 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003043 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003044
3045 switch (prop)
3046 {
3047 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003048 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003049 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003050 if (strval == NULL)
3051 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003052 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003053 // a blank title isn't useful, make it empty, so that "running" is
3054 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003055 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003056 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003057 // Same as blank
3058 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003059 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003060 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3061 term->tl_title = NULL;
3062 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003063 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003064 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003065#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003066 else if (!enc_utf8 && enc_codepage > 0)
3067 {
3068 WCHAR *ret = NULL;
3069 int length = 0;
3070
3071 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003072 (char*)value->string.str,
3073 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003074 if (ret != NULL)
3075 {
3076 WideCharToMultiByte_alloc(enc_codepage, 0,
3077 ret, length, (char**)&term->tl_title,
3078 &length, 0, 0);
3079 vim_free(ret);
3080 }
3081 }
3082#endif
3083 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003084 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003085 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003086 strval = NULL;
3087 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003088 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003089 if (term == curbuf->b_term)
3090 maketitle();
3091 break;
3092
3093 case VTERM_PROP_CURSORVISIBLE:
3094 term->tl_cursor_visible = value->boolean;
3095 may_toggle_cursor(term);
3096 out_flush();
3097 break;
3098
3099 case VTERM_PROP_CURSORBLINK:
3100 term->tl_cursor_blink = value->boolean;
3101 may_set_cursor_props(term);
3102 break;
3103
3104 case VTERM_PROP_CURSORSHAPE:
3105 term->tl_cursor_shape = value->number;
3106 may_set_cursor_props(term);
3107 break;
3108
3109 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003110 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003111 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003112 if (strval == NULL)
3113 break;
3114 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003115 may_set_cursor_props(term);
3116 break;
3117
3118 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003119 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003120 term->tl_using_altscreen = value->boolean;
3121 break;
3122
3123 default:
3124 break;
3125 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003126 vim_free(strval);
3127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003128 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003129 return 1;
3130}
3131
3132/*
3133 * The job running in the terminal resized the terminal.
3134 */
3135 static int
3136handle_resize(int rows, int cols, void *user)
3137{
3138 term_T *term = (term_T *)user;
3139 win_T *wp;
3140
3141 term->tl_rows = rows;
3142 term->tl_cols = cols;
3143 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003144 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003145 term->tl_vterm_size_changed = FALSE;
3146 else
3147 {
3148 FOR_ALL_WINDOWS(wp)
3149 {
3150 if (wp->w_buffer == term->tl_buffer)
3151 {
3152 win_setheight_win(rows, wp);
3153 win_setwidth_win(cols, wp);
3154 }
3155 }
3156 redraw_buf_later(term->tl_buffer, NOT_VALID);
3157 }
3158 return 1;
3159}
3160
3161/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003162 * If the number of lines that are stored goes over 'termscrollback' then
3163 * delete the first 10%.
3164 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3165 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003166 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003167 static void
3168limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003169{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003170 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003171 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003172 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003173 int i;
3174
3175 curbuf = term->tl_buffer;
3176 for (i = 0; i < todo; ++i)
3177 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003178 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3179 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003180 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003181 }
3182 curbuf = curwin->w_buffer;
3183
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003184 gap->ga_len -= todo;
3185 mch_memmove(gap->ga_data,
3186 (sb_line_T *)gap->ga_data + todo,
3187 sizeof(sb_line_T) * gap->ga_len);
3188 if (update_buffer)
3189 term->tl_scrollback_scrolled -= todo;
3190 }
3191}
3192
3193/*
3194 * Handle a line that is pushed off the top of the screen.
3195 */
3196 static int
3197handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3198{
3199 term_T *term = (term_T *)user;
3200 garray_T *gap;
3201 int update_buffer;
3202
3203 if (term->tl_normal_mode)
3204 {
3205 // In Terminal-Normal mode the user interacts with the buffer, thus we
3206 // must not change it. Postpone adding the scrollback lines.
3207 gap = &term->tl_scrollback_postponed;
3208 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003209 }
3210 else
3211 {
3212 // First remove the lines that were appended before, the pushed line
3213 // goes above it.
3214 cleanup_scrollback(term);
3215 gap = &term->tl_scrollback;
3216 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003217 }
3218
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003219 limit_scrollback(term, gap, update_buffer);
3220
3221 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003222 {
3223 cellattr_T *p = NULL;
3224 int len = 0;
3225 int i;
3226 int c;
3227 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003228 int text_len;
3229 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003230 sb_line_T *line;
3231 garray_T ga;
3232 cellattr_T fill_attr = term->tl_default_color;
3233
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003234 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003235 for (i = 0; i < cols; ++i)
3236 if (cells[i].chars[0] != 0)
3237 len = i + 1;
3238 else
3239 cell2cellattr(&cells[i], &fill_attr);
3240
3241 ga_init2(&ga, 1, 100);
3242 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003243 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003244 if (p != NULL)
3245 {
3246 for (col = 0; col < len; col += cells[col].width)
3247 {
3248 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3249 {
3250 ga.ga_len = 0;
3251 break;
3252 }
3253 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3254 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3255 (char_u *)ga.ga_data + ga.ga_len);
3256 cell2cellattr(&cells[col], &p[col]);
3257 }
3258 }
3259 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003260 {
3261 if (update_buffer)
3262 text = (char_u *)"";
3263 else
3264 text = vim_strsave((char_u *)"");
3265 text_len = 0;
3266 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003267 else
3268 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003269 text = ga.ga_data;
3270 text_len = ga.ga_len;
3271 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003272 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003273 if (update_buffer)
3274 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003275
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003276 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003277 line->sb_cols = len;
3278 line->sb_cells = p;
3279 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003280 if (update_buffer)
3281 {
3282 line->sb_text = NULL;
3283 ++term->tl_scrollback_scrolled;
3284 ga_clear(&ga); // free the text
3285 }
3286 else
3287 {
3288 line->sb_text = text;
3289 ga_init(&ga); // text is kept in tl_scrollback_postponed
3290 }
3291 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003292 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003293 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003294}
3295
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003296/*
3297 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3298 * received and stored in tl_scrollback_postponed.
3299 */
3300 static void
3301handle_postponed_scrollback(term_T *term)
3302{
3303 int i;
3304
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003305 if (term->tl_scrollback_postponed.ga_len == 0)
3306 return;
3307 ch_log(NULL, "Moving postponed scrollback to scrollback");
3308
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003309 // First remove the lines that were appended before, the pushed lines go
3310 // above it.
3311 cleanup_scrollback(term);
3312
3313 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3314 {
3315 char_u *text;
3316 sb_line_T *pp_line;
3317 sb_line_T *line;
3318
3319 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3320 break;
3321 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3322
3323 text = pp_line->sb_text;
3324 if (text == NULL)
3325 text = (char_u *)"";
3326 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3327 vim_free(pp_line->sb_text);
3328
3329 line = (sb_line_T *)term->tl_scrollback.ga_data
3330 + term->tl_scrollback.ga_len;
3331 line->sb_cols = pp_line->sb_cols;
3332 line->sb_cells = pp_line->sb_cells;
3333 line->sb_fill_attr = pp_line->sb_fill_attr;
3334 line->sb_text = NULL;
3335 ++term->tl_scrollback_scrolled;
3336 ++term->tl_scrollback.ga_len;
3337 }
3338
3339 ga_clear(&term->tl_scrollback_postponed);
3340 limit_scrollback(term, &term->tl_scrollback, TRUE);
3341}
3342
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003343static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003344 handle_damage, // damage
3345 handle_moverect, // moverect
3346 handle_movecursor, // movecursor
3347 handle_settermprop, // settermprop
3348 NULL, // bell
3349 handle_resize, // resize
3350 handle_pushline, // sb_pushline
3351 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003352};
3353
3354/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003355 * Do the work after the channel of a terminal was closed.
3356 * Must be called only when updating_screen is FALSE.
3357 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3358 */
3359 static int
3360term_after_channel_closed(term_T *term)
3361{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003362 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003363 if (!term->tl_normal_mode)
3364 {
3365 int fnum = term->tl_buffer->b_fnum;
3366
3367 cleanup_vterm(term);
3368
3369 if (term->tl_finish == TL_FINISH_CLOSE)
3370 {
3371 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003372 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003373#ifdef FEAT_PROP_POPUP
3374 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003375
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003376 // If this was a terminal in a popup window, go back to the
3377 // previous window.
3378 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3379 {
3380 pwin = curwin;
3381 if (win_valid(prevwin))
3382 win_enter(prevwin, FALSE);
3383 }
3384 else
3385#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003386 // If this is the last normal window: exit Vim.
3387 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3388 {
3389 exarg_T ea;
3390
Bram Moolenaara80faa82020-04-12 19:37:17 +02003391 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003392 ex_quit(&ea);
3393 return TRUE;
3394 }
3395
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003396 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003397 ch_log(NULL, "terminal job finished, closing window");
3398 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003399 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003400 if (curwin == aucmd_win)
3401 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003402 if (do_set_w_closing)
3403 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003404 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003405 if (do_set_w_closing)
3406 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003407 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003408#ifdef FEAT_PROP_POPUP
3409 if (pwin != NULL)
3410 popup_close_with_retval(pwin, 0);
3411#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003412 return TRUE;
3413 }
3414 if (term->tl_finish == TL_FINISH_OPEN
3415 && term->tl_buffer->b_nwindows == 0)
3416 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003417 char *cmd = term->tl_opencmd == NULL
3418 ? "botright sbuf %d"
3419 : (char *)term->tl_opencmd;
3420 size_t len = strlen(cmd) + 50;
3421 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003422
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003423 if (buf != NULL)
3424 {
3425 ch_log(NULL, "terminal job finished, opening window");
3426 vim_snprintf(buf, len, cmd, fnum);
3427 do_cmdline_cmd((char_u *)buf);
3428 vim_free(buf);
3429 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003430 }
3431 else
3432 ch_log(NULL, "terminal job finished");
3433 }
3434
3435 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3436 return FALSE;
3437}
3438
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003439#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3440/*
3441 * If the current window is a terminal in a popup window and the job has
3442 * finished, close the popup window and to back to the previous window.
3443 * Otherwise return FAIL.
3444 */
3445 int
3446may_close_term_popup(void)
3447{
3448 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3449 && !term_job_running(curbuf->b_term))
3450 {
3451 win_T *pwin = curwin;
3452
3453 if (win_valid(prevwin))
3454 win_enter(prevwin, FALSE);
3455 popup_close_with_retval(pwin, 0);
3456 return OK;
3457 }
3458 return FAIL;
3459}
3460#endif
3461
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003462/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003463 * Called when a channel has been closed.
3464 * If this was a channel for a terminal window then finish it up.
3465 */
3466 void
3467term_channel_closed(channel_T *ch)
3468{
3469 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003470 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003471 int did_one = FALSE;
3472
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003473 for (term = first_term; term != NULL; term = next_term)
3474 {
3475 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003476 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003477 {
3478 term->tl_channel_closed = TRUE;
3479 did_one = TRUE;
3480
Bram Moolenaard23a8232018-02-10 18:45:26 +01003481 VIM_CLEAR(term->tl_title);
3482 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003483#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003484 if (term->tl_out_fd != NULL)
3485 {
3486 fclose(term->tl_out_fd);
3487 term->tl_out_fd = NULL;
3488 }
3489#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003490
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003491 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003492 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003493 // Cannot open or close windows now. Can happen when
3494 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003495 term->tl_channel_recently_closed = TRUE;
3496 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003497 }
3498
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003499 if (term_after_channel_closed(term))
3500 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003501 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003502 }
3503
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003504 if (did_one)
3505 {
3506 redraw_statuslines();
3507
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003508 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003509 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003510 typebuf_was_filled = TRUE;
3511
3512 term = curbuf->b_term;
3513 if (term != NULL)
3514 {
3515 if (term->tl_job == ch->ch_job)
3516 maketitle();
3517 update_cursor(term, term->tl_cursor_visible);
3518 }
3519 }
3520}
3521
3522/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003523 * To be called after resetting updating_screen: handle any terminal where the
3524 * channel was closed.
3525 */
3526 void
3527term_check_channel_closed_recently()
3528{
3529 term_T *term;
3530 term_T *next_term;
3531
3532 for (term = first_term; term != NULL; term = next_term)
3533 {
3534 next_term = term->tl_next;
3535 if (term->tl_channel_recently_closed)
3536 {
3537 term->tl_channel_recently_closed = FALSE;
3538 if (term_after_channel_closed(term))
3539 // start over, the list may have changed
3540 next_term = first_term;
3541 }
3542 }
3543}
3544
3545/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003546 * Fill one screen line from a line of the terminal.
3547 * Advances "pos" to past the last column.
3548 */
3549 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003550term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003551 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003552 win_T *wp,
3553 VTermScreen *screen,
3554 VTermPos *pos,
3555 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003556{
3557 int off = screen_get_current_line_off();
3558
3559 for (pos->col = 0; pos->col < max_col; )
3560 {
3561 VTermScreenCell cell;
3562 int c;
3563
3564 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003565 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003566
3567 c = cell.chars[0];
3568 if (c == NUL)
3569 {
3570 ScreenLines[off] = ' ';
3571 if (enc_utf8)
3572 ScreenLinesUC[off] = NUL;
3573 }
3574 else
3575 {
3576 if (enc_utf8)
3577 {
3578 int i;
3579
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003580 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003581 for (i = 0; i < Screen_mco
3582 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3583 {
3584 ScreenLinesC[i][off] = cell.chars[i + 1];
3585 if (cell.chars[i + 1] == 0)
3586 break;
3587 }
3588 if (c >= 0x80 || (Screen_mco > 0
3589 && ScreenLinesC[0][off] != 0))
3590 {
3591 ScreenLines[off] = ' ';
3592 ScreenLinesUC[off] = c;
3593 }
3594 else
3595 {
3596 ScreenLines[off] = c;
3597 ScreenLinesUC[off] = NUL;
3598 }
3599 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003600#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003601 else if (has_mbyte && c >= 0x80)
3602 {
3603 char_u mb[MB_MAXBYTES+1];
3604 WCHAR wc = c;
3605
3606 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3607 (char*)mb, 2, 0, 0) > 1)
3608 {
3609 ScreenLines[off] = mb[0];
3610 ScreenLines[off + 1] = mb[1];
3611 cell.width = mb_ptr2cells(mb);
3612 }
3613 else
3614 ScreenLines[off] = c;
3615 }
3616#endif
3617 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003618 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003619 ScreenLines[off] = c;
3620 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003621 ScreenAttrs[off] = cell2attr(term, wp, cell.attrs, cell.fg, cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003622
3623 ++pos->col;
3624 ++off;
3625 if (cell.width == 2)
3626 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003627 // don't set the second byte to NUL for a DBCS encoding, it
3628 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003629 if (enc_utf8)
3630 {
3631 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003632 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003633 }
3634 else if (!has_mbyte)
3635 {
3636 // Can't show a double-width character with a single-byte
3637 // 'encoding', just use a space.
3638 ScreenLines[off] = ' ';
3639 ScreenAttrs[off] = ScreenAttrs[off - 1];
3640 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003641
3642 ++pos->col;
3643 ++off;
3644 }
3645 }
3646}
3647
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003648#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003649 static void
3650update_system_term(term_T *term)
3651{
3652 VTermPos pos;
3653 VTermScreen *screen;
3654
3655 if (term->tl_vterm == NULL)
3656 return;
3657 screen = vterm_obtain_screen(term->tl_vterm);
3658
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003659 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003660 while (term->tl_toprow > 0
3661 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3662 {
3663 int save_p_more = p_more;
3664
3665 p_more = FALSE;
3666 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003667 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003668 p_more = save_p_more;
3669 --term->tl_toprow;
3670 }
3671
3672 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3673 && pos.row < Rows; ++pos.row)
3674 {
3675 if (pos.row < term->tl_rows)
3676 {
3677 int max_col = MIN(Columns, term->tl_cols);
3678
Bram Moolenaar83d47902020-03-26 20:34:00 +01003679 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003680 }
3681 else
3682 pos.col = 0;
3683
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003684 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003685 }
3686
3687 term->tl_dirty_row_start = MAX_ROW;
3688 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003689}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003690#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003691
3692/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003693 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3694 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003695 * Terminal-Normal mode.
3696 */
3697 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003698term_do_update_window(win_T *wp)
3699{
3700 term_T *term = wp->w_buffer->b_term;
3701
3702 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3703}
3704
3705/*
3706 * Called to update a window that contains an active terminal.
3707 */
3708 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003709term_update_window(win_T *wp)
3710{
3711 term_T *term = wp->w_buffer->b_term;
3712 VTerm *vterm;
3713 VTermScreen *screen;
3714 VTermState *state;
3715 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003716 int rows, cols;
3717 int newrows, newcols;
3718 int minsize;
3719 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003720
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003721 vterm = term->tl_vterm;
3722 screen = vterm_obtain_screen(vterm);
3723 state = vterm_obtain_state(vterm);
3724
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003725 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3726 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003727 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003728 {
3729 term->tl_dirty_row_start = 0;
3730 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003731
3732 if (term->tl_postponed_scroll > 0
3733 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003734 // Scrolling is usually faster than redrawing, when there are only
3735 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003736 term_scroll_up(term, 0, term->tl_postponed_scroll);
3737 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003738 }
3739
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003740 /*
3741 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003742 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003743 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003744 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003745
Bram Moolenaar498c2562018-04-15 23:45:15 +02003746 newrows = 99999;
3747 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003748 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003749 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003750 // Always use curwin, it may be a popup window.
3751 win_T *wwp = twp == NULL ? curwin : twp;
3752
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003753 // When more than one window shows the same terminal, use the
3754 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003755 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003756 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003757 newrows = MIN(newrows, wwp->w_height);
3758 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003759 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003760 if (twp == NULL)
3761 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003762 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003763 if (newrows == 99999 || newcols == 99999)
3764 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003765 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3766 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3767
Bram Moolenaareba13e42021-02-23 17:47:23 +01003768 // If no cell is visible there is no point in resizing. Also, vterm can't
3769 // handle a zero height.
3770 if (newrows == 0 || newcols == 0)
3771 return;
3772
Bram Moolenaar498c2562018-04-15 23:45:15 +02003773 if (term->tl_rows != newrows || term->tl_cols != newcols)
3774 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003775 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003776 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003777 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003778 newrows);
3779 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003780
3781 // Updating the terminal size will cause the snapshot to be cleared.
3782 // When not in terminal_loop() we need to restore it.
3783 if (term != in_terminal_loop)
3784 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003785 }
3786
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003787 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003788 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003789 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003790
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003791 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3792 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003793 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003794 if (pos.row < term->tl_rows)
3795 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003796 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003797
Bram Moolenaar83d47902020-03-26 20:34:00 +01003798 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003799 }
3800 else
3801 pos.col = 0;
3802
Bram Moolenaarf118d482018-03-13 13:14:00 +01003803 screen_line(wp->w_winrow + pos.row
3804#ifdef FEAT_MENU
3805 + winbar_height(wp)
3806#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003807 , wp->w_wincol, pos.col, wp->w_width,
3808#ifdef FEAT_PROP_POPUP
3809 popup_is_popup(wp) ? SLF_POPUP :
3810#endif
3811 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003812 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003813 term->tl_dirty_row_start = MAX_ROW;
3814 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003815}
3816
3817/*
3818 * Return TRUE if "wp" is a terminal window where the job has finished.
3819 */
3820 int
3821term_is_finished(buf_T *buf)
3822{
3823 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3824}
3825
3826/*
3827 * Return TRUE if "wp" is a terminal window where the job has finished or we
3828 * are in Terminal-Normal mode, thus we show the buffer contents.
3829 */
3830 int
3831term_show_buffer(buf_T *buf)
3832{
3833 term_T *term = buf->b_term;
3834
3835 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3836}
3837
3838/*
3839 * The current buffer is going to be changed. If there is terminal
3840 * highlighting remove it now.
3841 */
3842 void
3843term_change_in_curbuf(void)
3844{
3845 term_T *term = curbuf->b_term;
3846
3847 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3848 {
3849 free_scrollback(term);
3850 redraw_buf_later(term->tl_buffer, NOT_VALID);
3851
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003852 // The buffer is now like a normal buffer, it cannot be easily
3853 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003854 set_string_option_direct((char_u *)"buftype", -1,
3855 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3856 }
3857}
3858
3859/*
3860 * Get the screen attribute for a position in the buffer.
3861 * Use a negative "col" to get the filler background color.
3862 */
3863 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003864term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003865{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003866 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003867 term_T *term = buf->b_term;
3868 sb_line_T *line;
3869 cellattr_T *cellattr;
3870
3871 if (lnum > term->tl_scrollback.ga_len)
3872 cellattr = &term->tl_default_color;
3873 else
3874 {
3875 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3876 if (col < 0 || col >= line->sb_cols)
3877 cellattr = &line->sb_fill_attr;
3878 else
3879 cellattr = line->sb_cells + col;
3880 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003881 return cell2attr(term, wp, cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003882}
3883
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003884/*
3885 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003886 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003887 */
3888 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003889cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003890{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003891 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3892 if (rgb->index == 0)
3893 rgb->type = VTERM_COLOR_RGB;
3894 else
3895 {
3896 rgb->type = VTERM_COLOR_INDEXED;
3897 --rgb->index;
3898 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003899}
3900
3901/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003902 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003903 */
3904 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003905init_default_colors(term_T *term, win_T *wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003906{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003907 VTermColor *fg, *bg;
3908 int fgval, bgval;
3909 int id;
3910
Bram Moolenaara80faa82020-04-12 19:37:17 +02003911 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003912 term->tl_default_color.width = 1;
3913 fg = &term->tl_default_color.fg;
3914 bg = &term->tl_default_color.bg;
3915
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003916 // Vterm uses a default black background. Set it to white when
3917 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003918 if (*p_bg == 'l')
3919 {
3920 fgval = 0;
3921 bgval = 255;
3922 }
3923 else
3924 {
3925 fgval = 255;
3926 bgval = 0;
3927 }
3928 fg->red = fg->green = fg->blue = fgval;
3929 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003930 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3931 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003932
Bram Moolenaar83d47902020-03-26 20:34:00 +01003933 // The 'wincolor' or the highlight group overrules the defaults.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003934 if (wp != NULL && *wp->w_p_wcr != NUL)
3935 id = syn_name2id(wp->w_p_wcr);
3936 else
Bram Moolenaar83d47902020-03-26 20:34:00 +01003937 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003938
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003939 // Use the actual color for the GUI and when 'termguicolors' is set.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003940#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3941 if (0
3942# ifdef FEAT_GUI
3943 || gui.in_use
3944# endif
3945# ifdef FEAT_TERMGUICOLORS
3946 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003947# ifdef FEAT_VTP
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003948 // Finally get INVALCOLOR on this execution path
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003949 || (!p_tgc && t_colors >= 256)
3950# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003951# endif
3952 )
3953 {
3954 guicolor_T fg_rgb = INVALCOLOR;
3955 guicolor_T bg_rgb = INVALCOLOR;
3956
3957 if (id != 0)
3958 syn_id2colors(id, &fg_rgb, &bg_rgb);
3959
3960# ifdef FEAT_GUI
3961 if (gui.in_use)
3962 {
3963 if (fg_rgb == INVALCOLOR)
3964 fg_rgb = gui.norm_pixel;
3965 if (bg_rgb == INVALCOLOR)
3966 bg_rgb = gui.back_pixel;
3967 }
3968# ifdef FEAT_TERMGUICOLORS
3969 else
3970# endif
3971# endif
3972# ifdef FEAT_TERMGUICOLORS
3973 {
3974 if (fg_rgb == INVALCOLOR)
3975 fg_rgb = cterm_normal_fg_gui_color;
3976 if (bg_rgb == INVALCOLOR)
3977 bg_rgb = cterm_normal_bg_gui_color;
3978 }
3979# endif
3980 if (fg_rgb != INVALCOLOR)
3981 {
3982 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3983
3984 fg->red = (unsigned)(rgb >> 16);
3985 fg->green = (unsigned)(rgb >> 8) & 255;
3986 fg->blue = (unsigned)rgb & 255;
3987 }
3988 if (bg_rgb != INVALCOLOR)
3989 {
3990 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
3991
3992 bg->red = (unsigned)(rgb >> 16);
3993 bg->green = (unsigned)(rgb >> 8) & 255;
3994 bg->blue = (unsigned)rgb & 255;
3995 }
3996 }
3997 else
3998#endif
3999 if (id != 0 && t_colors >= 16)
4000 {
Milly7b5f45b2021-10-15 22:25:43 +01004001 int cterm_fg = -1;
4002 int cterm_bg = -1;
4003 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01004004
4005 if (cterm_fg >= 0)
4006 cterm_color2vterm(cterm_fg, fg);
4007 if (cterm_bg >= 0)
4008 cterm_color2vterm(cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004009 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004010 else
4011 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004012#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004013 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004014#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004015
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004016 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004017 if (cterm_normal_fg_color > 0)
4018 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004019 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004020# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4021# ifdef VIMDLL
4022 if (!gui.in_use)
4023# endif
4024 {
4025 tmp = fg->red;
4026 fg->red = fg->blue;
4027 fg->blue = tmp;
4028 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004029# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004030 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004031# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004032 else
4033 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004034# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004035
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004036 if (cterm_normal_bg_color > 0)
4037 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004038 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004039# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4040# ifdef VIMDLL
4041 if (!gui.in_use)
4042# endif
4043 {
4044 tmp = fg->red;
4045 fg->red = fg->blue;
4046 fg->blue = tmp;
4047 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004048# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004049 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004050# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004051 else
4052 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004053# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004054 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004055}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004056
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004057#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4058/*
4059 * Set the 16 ANSI colors from array of RGB values
4060 */
4061 static void
4062set_vterm_palette(VTerm *vterm, long_u *rgb)
4063{
4064 int index = 0;
4065 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004066
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004067 for (; index < 16; index++)
4068 {
4069 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004070
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004071 color.red = (unsigned)(rgb[index] >> 16);
4072 color.green = (unsigned)(rgb[index] >> 8) & 255;
4073 color.blue = (unsigned)rgb[index] & 255;
4074 vterm_state_set_palette_color(state, index, &color);
4075 }
4076}
4077
4078/*
4079 * Set the ANSI color palette from a list of colors
4080 */
4081 static int
4082set_ansi_colors_list(VTerm *vterm, list_T *list)
4083{
4084 int n = 0;
4085 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004086 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004087
Bram Moolenaarb0992022020-01-30 14:55:42 +01004088 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004089 {
4090 char_u *color_name;
4091 guicolor_T guicolor;
4092
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004093 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004094 if (color_name == NULL)
4095 return FAIL;
4096
4097 guicolor = GUI_GET_COLOR(color_name);
4098 if (guicolor == INVALCOLOR)
4099 return FAIL;
4100
4101 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4102 }
4103
4104 if (n != 16 || li != NULL)
4105 return FAIL;
4106
4107 set_vterm_palette(vterm, rgb);
4108
4109 return OK;
4110}
4111
4112/*
4113 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4114 */
4115 static void
4116init_vterm_ansi_colors(VTerm *vterm)
4117{
4118 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4119
4120 if (var != NULL
4121 && (var->di_tv.v_type != VAR_LIST
4122 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004123 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004124 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004125 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004126}
4127#endif
4128
Bram Moolenaar52acb112018-03-18 19:20:22 +01004129/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004130 * Handles a "drop" command from the job in the terminal.
4131 * "item" is the file name, "item->li_next" may have options.
4132 */
4133 static void
4134handle_drop_command(listitem_T *item)
4135{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004136 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004137 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004138 int bufnr;
4139 win_T *wp;
4140 tabpage_T *tp;
4141 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004142 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004143
4144 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4145 FOR_ALL_TAB_WINDOWS(tp, wp)
4146 {
4147 if (wp->w_buffer->b_fnum == bufnr)
4148 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004149 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004150 goto_tabpage_win(tp, wp);
4151 return;
4152 }
4153 }
4154
Bram Moolenaara80faa82020-04-12 19:37:17 +02004155 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004156
4157 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4158 && opt_item->li_tv.vval.v_dict != NULL)
4159 {
4160 dict_T *dict = opt_item->li_tv.vval.v_dict;
4161 char_u *p;
4162
Bram Moolenaar8f667172018-12-14 15:38:31 +01004163 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004164 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004165 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004166 if (p != NULL)
4167 {
4168 if (check_ff_value(p) == FAIL)
4169 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4170 else
4171 ea.force_ff = *p;
4172 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004173 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004174 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004175 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004176 if (p != NULL)
4177 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004178 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004179 if (ea.cmd != NULL)
4180 {
4181 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4182 ea.force_enc = 11;
4183 tofree = ea.cmd;
4184 }
4185 }
4186
Bram Moolenaar8f667172018-12-14 15:38:31 +01004187 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004188 if (p != NULL)
4189 get_bad_opt(p, &ea);
4190
4191 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4192 ea.force_bin = FORCE_BIN;
4193 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4194 ea.force_bin = FORCE_BIN;
4195 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4196 ea.force_bin = FORCE_NOBIN;
4197 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4198 ea.force_bin = FORCE_NOBIN;
4199 }
4200
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004201 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004202 if (ea.cmd == NULL)
4203 ea.cmd = (char_u *)"split";
4204 ea.arg = fname;
4205 ea.cmdidx = CMD_split;
4206 ex_splitview(&ea);
4207
4208 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004209}
4210
4211/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004212 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4213 */
4214 static int
4215is_permitted_term_api(char_u *func, char_u *pat)
4216{
4217 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4218}
4219
4220/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004221 * Handles a function call from the job running in a terminal.
4222 * "item" is the function name, "item->li_next" has the arguments.
4223 */
4224 static void
4225handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4226{
4227 char_u *func;
4228 typval_T argvars[2];
4229 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004230 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004231
4232 if (item->li_next == NULL)
4233 {
4234 ch_log(channel, "Missing function arguments for call");
4235 return;
4236 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004237 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004238
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004239 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004240 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004241 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004242 return;
4243 }
4244
4245 argvars[0].v_type = VAR_NUMBER;
4246 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4247 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004248 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004249 funcexe.firstline = 1L;
4250 funcexe.lastline = 1L;
4251 funcexe.evaluate = TRUE;
4252 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004253 {
4254 clear_tv(&rettv);
4255 ch_log(channel, "Function %s called", func);
4256 }
4257 else
4258 ch_log(channel, "Calling function %s failed", func);
4259}
4260
4261/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004262 * URL decoding (also know as Percent-encoding).
4263 *
4264 * Note this function currently is only used for decoding shell's
4265 * OSC 7 escape sequence which we can assume all bytes are valid
4266 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4267 * encoding bytes like 0xfe, 0xff.
4268 */
4269 static size_t
4270url_decode(const char *src, const size_t len, char_u *dst)
4271{
4272 size_t i = 0, j = 0;
4273
4274 while (i < len)
4275 {
4276 if (src[i] == '%' && i + 2 < len)
4277 {
4278 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4279 j++;
4280 i += 3;
4281 }
4282 else
4283 {
4284 dst[j] = src[i];
4285 i++;
4286 j++;
4287 }
4288 }
4289 dst[j] = '\0';
4290 return j;
4291}
4292
4293/*
4294 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4295 *
4296 * The OSC 7 sequence has the format of
4297 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4298 * and what VTerm provides via VTermStringFragment is
4299 * "file://HOSTNAME/CURRENT/DIR"
4300 */
4301 static void
4302sync_shell_dir(VTermStringFragment *frag)
4303{
4304 int offset = 7; // len of "file://" is 7
4305 char *pos = (char *)frag->str + offset;
4306 char_u *new_dir;
4307
4308 // remove HOSTNAME to get PWD
Bram Moolenaar918b0892021-05-08 20:09:24 +02004309 while (*pos != '/' && offset < (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004310 {
4311 offset += 1;
4312 pos += 1;
4313 }
4314
Bram Moolenaar918b0892021-05-08 20:09:24 +02004315 if (offset >= (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004316 {
4317 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
4318 frag->str);
4319 return;
4320 }
4321
4322 new_dir = alloc(frag->len - offset + 1);
4323 url_decode(pos, frag->len-offset, new_dir);
4324 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4325 vim_free(new_dir);
4326}
4327
4328/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004329 * Called by libvterm when it cannot recognize an OSC sequence.
4330 * We recognize a terminal API command.
4331 */
4332 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004333parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004334{
4335 term_T *term = (term_T *)user;
4336 js_read_T reader;
4337 typval_T tv;
4338 channel_T *channel = term->tl_job == NULL ? NULL
4339 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004340 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004341
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004342 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
4343 if (p_asd && command == 7)
4344 {
4345 sync_shell_dir(&frag);
4346 return 1;
4347 }
4348
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004349 if (command != 51)
4350 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004351
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004352 // Concatenate what was received until the final piece is found.
4353 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4354 {
4355 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004356 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004357 }
4358 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004359 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004360 if (!frag.final)
4361 return 1;
4362
4363 ((char *)gap->ga_data)[gap->ga_len] = 0;
4364 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004365 reader.js_fill = NULL;
4366 reader.js_used = 0;
4367 if (json_decode(&reader, &tv, 0) == OK
4368 && tv.v_type == VAR_LIST
4369 && tv.vval.v_list != NULL)
4370 {
4371 listitem_T *item = tv.vval.v_list->lv_first;
4372
4373 if (item == NULL)
4374 ch_log(channel, "Missing command");
4375 else
4376 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004377 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004378
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004379 // Make sure an invoked command doesn't delete the buffer (and the
4380 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004381 ++term->tl_buffer->b_locked;
4382
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004383 item = item->li_next;
4384 if (item == NULL)
4385 ch_log(channel, "Missing argument for %s", cmd);
4386 else if (STRCMP(cmd, "drop") == 0)
4387 handle_drop_command(item);
4388 else if (STRCMP(cmd, "call") == 0)
4389 handle_call_command(term, channel, item);
4390 else
4391 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004392 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004393 }
4394 }
4395 else
4396 ch_log(channel, "Invalid JSON received");
4397
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004398 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004399 clear_tv(&tv);
4400 return 1;
4401}
4402
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004403/*
4404 * Called by libvterm when it cannot recognize a CSI sequence.
4405 * We recognize the window position report.
4406 */
4407 static int
4408parse_csi(
4409 const char *leader UNUSED,
4410 const long args[],
4411 int argcount,
4412 const char *intermed UNUSED,
4413 char command,
4414 void *user)
4415{
4416 term_T *term = (term_T *)user;
4417 char buf[100];
4418 int len;
4419 int x = 0;
4420 int y = 0;
4421 win_T *wp;
4422
4423 // We recognize only CSI 13 t
4424 if (command != 't' || argcount != 1 || args[0] != 13)
4425 return 0; // not handled
4426
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004427 // When getting the window position is not possible or it fails it results
4428 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004429#if defined(FEAT_GUI) \
4430 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4431 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004432 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004433#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004434
4435 FOR_ALL_WINDOWS(wp)
4436 if (wp->w_buffer == term->tl_buffer)
4437 break;
4438 if (wp != NULL)
4439 {
4440#ifdef FEAT_GUI
4441 if (gui.in_use)
4442 {
4443 x += wp->w_wincol * gui.char_width;
4444 y += W_WINROW(wp) * gui.char_height;
4445 }
4446 else
4447#endif
4448 {
4449 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004450 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004451 x += wp->w_wincol * 7;
4452 y += W_WINROW(wp) * 10;
4453 }
4454 }
4455
4456 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4457 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4458 (char_u *)buf, len, NULL);
4459 return 1;
4460}
4461
Bram Moolenaard8637282020-05-20 18:41:41 +02004462static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004463 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004464 parse_csi, // csi
4465 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004466 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004467};
4468
4469/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004470 * Use Vim's allocation functions for vterm so profiling works.
4471 */
4472 static void *
4473vterm_malloc(size_t size, void *data UNUSED)
4474{
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004475 return alloc_clear(size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004476}
4477
4478 static void
4479vterm_memfree(void *ptr, void *data UNUSED)
4480{
4481 vim_free(ptr);
4482}
4483
4484static VTermAllocatorFunctions vterm_allocator = {
4485 &vterm_malloc,
4486 &vterm_memfree
4487};
4488
4489/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004490 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004491 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004492 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004493 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004494create_vterm(term_T *term, int rows, int cols)
4495{
4496 VTerm *vterm;
4497 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004498 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004499 VTermValue value;
4500
Bram Moolenaar756ef112018-04-10 12:04:27 +02004501 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004502 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004503 if (vterm == NULL)
4504 return FAIL;
4505
4506 // Allocate screen and state here, so we can bail out if that fails.
4507 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004508 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004509 if (state == NULL || screen == NULL)
4510 {
4511 vterm_free(vterm);
4512 return FAIL;
4513 }
4514
Bram Moolenaar52acb112018-03-18 19:20:22 +01004515 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004516 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004517 vterm_set_utf8(vterm, 1);
4518
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004519 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004520
4521 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004522 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004523 &term->tl_default_color.fg,
4524 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004525
Bram Moolenaar9e587872019-05-13 20:27:23 +02004526 if (t_colors < 16)
4527 // Less than 16 colors: assume that bold means using a bright color for
4528 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004529 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4530
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004531 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004532 vterm_screen_reset(screen, 1 /* hard */);
4533
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004534 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004535 vterm_screen_enable_altscreen(screen, 1);
4536
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004537 // For unix do not use a blinking cursor. In an xterm this causes the
4538 // cursor to blink if it's blinking in the xterm.
4539 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004540#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004541 if (GetCaretBlinkTime() == INFINITE)
4542 value.boolean = 0;
4543 else
4544 value.boolean = 1;
4545#else
4546 value.boolean = 0;
4547#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004548 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004549 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004550
4551 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004552}
4553
4554/*
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004555 * Called when 'wincolor' was set.
4556 */
4557 void
Bram Moolenaarad431992021-05-03 20:40:38 +02004558term_update_colors(term_T *term)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004559{
Bram Moolenaarad431992021-05-03 20:40:38 +02004560 win_T *wp;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004561
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01004562 if (term->tl_vterm == NULL)
4563 return;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004564 init_default_colors(term, curwin);
4565 vterm_state_set_default_colors(
4566 vterm_obtain_state(term->tl_vterm),
4567 &term->tl_default_color.fg,
4568 &term->tl_default_color.bg);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01004569
Bram Moolenaarad431992021-05-03 20:40:38 +02004570 FOR_ALL_WINDOWS(wp)
4571 if (wp->w_buffer == term->tl_buffer)
4572 redraw_win_later(wp, NOT_VALID);
4573}
4574
4575/*
4576 * Called when 'background' was set.
4577 */
4578 void
4579term_update_colors_all(void)
4580{
4581 term_T *tp;
4582
4583 FOR_ALL_TERMS(tp)
4584 term_update_colors(tp);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004585}
4586
4587/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004588 * Return the text to show for the buffer name and status.
4589 */
4590 char_u *
4591term_get_status_text(term_T *term)
4592{
4593 if (term->tl_status_text == NULL)
4594 {
4595 char_u *txt;
4596 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004597 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004598
4599 if (term->tl_normal_mode)
4600 {
4601 if (term_job_running(term))
4602 txt = (char_u *)_("Terminal");
4603 else
4604 txt = (char_u *)_("Terminal-finished");
4605 }
4606 else if (term->tl_title != NULL)
4607 txt = term->tl_title;
4608 else if (term_none_open(term))
4609 txt = (char_u *)_("active");
4610 else if (term_job_running(term))
4611 txt = (char_u *)_("running");
4612 else
4613 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004614 fname = buf_get_fname(term->tl_buffer);
4615 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004616 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004617 if (term->tl_status_text != NULL)
4618 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004619 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004620 }
4621 return term->tl_status_text;
4622}
4623
4624/*
4625 * Mark references in jobs of terminals.
4626 */
4627 int
4628set_ref_in_term(int copyID)
4629{
4630 int abort = FALSE;
4631 term_T *term;
4632 typval_T tv;
4633
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004634 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004635 if (term->tl_job != NULL)
4636 {
4637 tv.v_type = VAR_JOB;
4638 tv.vval.v_job = term->tl_job;
4639 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4640 }
4641 return abort;
4642}
4643
4644/*
4645 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004646 * Returns NULL when the buffer is not for a terminal window and logs a message
4647 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004648 */
4649 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004650term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004651{
4652 buf_T *buf;
4653
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004654 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004655 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004656 --emsg_off;
4657 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004658 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004659 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004660 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004661 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004662 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004663 return buf;
4664}
4665
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004666 static void
4667clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004668{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004669 CLEAR_FIELD(*cell);
4670 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4671 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004672}
4673
4674 static void
4675dump_term_color(FILE *fd, VTermColor *color)
4676{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004677 int index;
4678
4679 if (VTERM_COLOR_IS_INDEXED(color))
4680 index = color->index + 1;
4681 else if (color->type == 0)
4682 // use RGB values
4683 index = 255;
4684 else
4685 // default color
4686 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004687 fprintf(fd, "%02x%02x%02x%d",
4688 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004689 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004690}
4691
4692/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004693 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004694 *
4695 * Each screen cell in full is:
4696 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4697 * {characters} is a space for an empty cell
4698 * For a double-width character "+" is changed to "*" and the next cell is
4699 * skipped.
4700 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4701 * when "&" use the same as the previous cell.
4702 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4703 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4704 * {color-idx} is a number from 0 to 255
4705 *
4706 * Screen cell with same width, attributes and color as the previous one:
4707 * |{characters}
4708 *
4709 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4710 *
4711 * Repeating the previous screen cell:
4712 * @{count}
4713 */
4714 void
4715f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4716{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004717 buf_T *buf;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004718 term_T *term;
4719 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004720 int max_height = 0;
4721 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004722 stat_T st;
4723 FILE *fd;
4724 VTermPos pos;
4725 VTermScreen *screen;
4726 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004727 VTermState *state;
4728 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004729
4730 if (check_restricted() || check_secure())
4731 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004732
4733 if (in_vim9script()
4734 && (check_for_buffer_arg(argvars, 0) == FAIL
4735 || check_for_string_arg(argvars, 1) == FAIL
4736 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4737 return;
4738
4739 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004740 if (buf == NULL)
4741 return;
4742 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004743 if (term->tl_vterm == NULL)
4744 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004745 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004746 return;
4747 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004748
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004749 if (argvars[2].v_type != VAR_UNKNOWN)
4750 {
4751 dict_T *d;
4752
4753 if (argvars[2].v_type != VAR_DICT)
4754 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004755 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004756 return;
4757 }
4758 d = argvars[2].vval.v_dict;
4759 if (d != NULL)
4760 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004761 max_height = dict_get_number(d, (char_u *)"rows");
4762 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004763 }
4764 }
4765
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004766 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004767 if (fname == NULL)
4768 return;
4769 if (mch_stat((char *)fname, &st) >= 0)
4770 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004771 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004772 return;
4773 }
4774
Bram Moolenaard96ff162018-02-18 22:13:29 +01004775 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4776 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004777 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004778 return;
4779 }
4780
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004781 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004782
4783 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004784 state = vterm_obtain_state(term->tl_vterm);
4785 vterm_state_get_cursorpos(state, &cursor_pos);
4786
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004787 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4788 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004789 {
4790 int repeat = 0;
4791
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004792 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4793 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004794 {
4795 VTermScreenCell cell;
4796 int same_attr;
4797 int same_chars = TRUE;
4798 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004799 int is_cursor_pos = (pos.col == cursor_pos.col
4800 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004801
4802 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004803 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004804
4805 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4806 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004807 int c = cell.chars[i];
4808 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004809 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004810
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004811 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004812 if (i == 0)
4813 {
4814 c = (c == NUL) ? ' ' : c;
4815 pc = (pc == NUL) ? ' ' : pc;
4816 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004817 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004818 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004819 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004820 break;
4821 }
4822 same_attr = vtermAttr2hl(cell.attrs)
4823 == vtermAttr2hl(prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004824 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4825 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004826 if (same_chars && cell.width == prev_cell.width && same_attr
4827 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004828 {
4829 ++repeat;
4830 }
4831 else
4832 {
4833 if (repeat > 0)
4834 {
4835 fprintf(fd, "@%d", repeat);
4836 repeat = 0;
4837 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004838 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004839
4840 if (cell.chars[0] == NUL)
4841 fputs(" ", fd);
4842 else
4843 {
4844 char_u charbuf[10];
4845 int len;
4846
4847 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4848 && cell.chars[i] != NUL; ++i)
4849 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004850 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004851 fwrite(charbuf, len, 1, fd);
4852 }
4853 }
4854
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004855 // When only the characters differ we don't write anything, the
4856 // following "|", "@" or NL will indicate using the same
4857 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004858 if (cell.width != prev_cell.width || !same_attr)
4859 {
4860 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004861 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004862 else
4863 fputs("+", fd);
4864
4865 if (same_attr)
4866 {
4867 fputs("&", fd);
4868 }
4869 else
4870 {
4871 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004872 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004873 fputs("&", fd);
4874 else
4875 {
4876 fputs("#", fd);
4877 dump_term_color(fd, &cell.fg);
4878 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004879 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004880 fputs("&", fd);
4881 else
4882 {
4883 fputs("#", fd);
4884 dump_term_color(fd, &cell.bg);
4885 }
4886 }
4887 }
4888
4889 prev_cell = cell;
4890 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004891
4892 if (cell.width == 2)
4893 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004894 }
4895 if (repeat > 0)
4896 fprintf(fd, "@%d", repeat);
4897 fputs("\n", fd);
4898 }
4899
4900 fclose(fd);
4901}
4902
4903/*
4904 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4905 */
4906 static void
4907dump_is_corrupt(garray_T *gap)
4908{
4909 ga_concat(gap, (char_u *)"CORRUPT");
4910}
4911
4912 static void
4913append_cell(garray_T *gap, cellattr_T *cell)
4914{
4915 if (ga_grow(gap, 1) == OK)
4916 {
4917 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4918 ++gap->ga_len;
4919 }
4920}
4921
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004922 static void
4923clear_cellattr(cellattr_T *cell)
4924{
4925 CLEAR_FIELD(*cell);
4926 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4927 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4928}
4929
Bram Moolenaard96ff162018-02-18 22:13:29 +01004930/*
4931 * Read the dump file from "fd" and append lines to the current buffer.
4932 * Return the cell width of the longest line.
4933 */
4934 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004935read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004936{
4937 int c;
4938 garray_T ga_text;
4939 garray_T ga_cell;
4940 char_u *prev_char = NULL;
4941 int attr = 0;
4942 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004943 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004944 term_T *term = curbuf->b_term;
4945 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004946 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004947
4948 ga_init2(&ga_text, 1, 90);
4949 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004950 clear_cellattr(&cell);
4951 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004952 cursor_pos->row = -1;
4953 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004954
4955 c = fgetc(fd);
4956 for (;;)
4957 {
4958 if (c == EOF)
4959 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004960 if (c == '\r')
4961 {
4962 // DOS line endings? Ignore.
4963 c = fgetc(fd);
4964 }
4965 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004966 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004967 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004968 if (ga_text.ga_data == NULL)
4969 dump_is_corrupt(&ga_text);
4970 if (ga_grow(&term->tl_scrollback, 1) == OK)
4971 {
4972 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
4973 + term->tl_scrollback.ga_len;
4974
4975 if (max_cells < ga_cell.ga_len)
4976 max_cells = ga_cell.ga_len;
4977 line->sb_cols = ga_cell.ga_len;
4978 line->sb_cells = ga_cell.ga_data;
4979 line->sb_fill_attr = term->tl_default_color;
4980 ++term->tl_scrollback.ga_len;
4981 ga_init(&ga_cell);
4982
4983 ga_append(&ga_text, NUL);
4984 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4985 ga_text.ga_len, FALSE);
4986 }
4987 else
4988 ga_clear(&ga_cell);
4989 ga_text.ga_len = 0;
4990
4991 c = fgetc(fd);
4992 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004993 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004994 {
4995 int prev_len = ga_text.ga_len;
4996
Bram Moolenaar9271d052018-02-25 21:39:46 +01004997 if (c == '>')
4998 {
4999 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005000 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005001 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5002 cursor_pos->col = ga_cell.ga_len;
5003 }
5004
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005005 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005006 c = fgetc(fd);
5007 if (c != EOF)
5008 ga_append(&ga_text, c);
5009 for (;;)
5010 {
5011 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005012 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005013 || c == EOF || c == '\n')
5014 break;
5015 ga_append(&ga_text, c);
5016 }
5017
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005018 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005019 vim_free(prev_char);
5020 if (ga_text.ga_data != NULL)
5021 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5022 ga_text.ga_len - prev_len);
5023
Bram Moolenaar9271d052018-02-25 21:39:46 +01005024 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005025 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005026 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005027 }
5028 else if (c == '+' || c == '*')
5029 {
5030 int is_bg;
5031
5032 cell.width = c == '+' ? 1 : 2;
5033
5034 c = fgetc(fd);
5035 if (c == '&')
5036 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005037 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005038 c = fgetc(fd);
5039 }
5040 else if (isdigit(c))
5041 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005042 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005043 attr = 0;
5044 while (isdigit(c))
5045 {
5046 attr = attr * 10 + (c - '0');
5047 c = fgetc(fd);
5048 }
5049 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005050
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005051 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005052 for (is_bg = 0; is_bg <= 1; ++is_bg)
5053 {
5054 if (c == '&')
5055 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005056 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005057 c = fgetc(fd);
5058 }
5059 else if (c == '#')
5060 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005061 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005062
5063 c = fgetc(fd);
5064 red = hex2nr(c);
5065 c = fgetc(fd);
5066 red = (red << 4) + hex2nr(c);
5067 c = fgetc(fd);
5068 green = hex2nr(c);
5069 c = fgetc(fd);
5070 green = (green << 4) + hex2nr(c);
5071 c = fgetc(fd);
5072 blue = hex2nr(c);
5073 c = fgetc(fd);
5074 blue = (blue << 4) + hex2nr(c);
5075 c = fgetc(fd);
5076 if (!isdigit(c))
5077 dump_is_corrupt(&ga_text);
5078 while (isdigit(c))
5079 {
5080 index = index * 10 + (c - '0');
5081 c = fgetc(fd);
5082 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005083 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005084 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005085 type = VTERM_COLOR_RGB;
5086 if (index == 0)
5087 {
5088 if (is_bg)
5089 type |= VTERM_COLOR_DEFAULT_BG;
5090 else
5091 type |= VTERM_COLOR_DEFAULT_FG;
5092 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005093 }
5094 else
5095 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005096 type = VTERM_COLOR_INDEXED;
5097 index -= 1;
5098 }
5099 if (is_bg)
5100 {
5101 cell.bg.type = type;
5102 cell.bg.red = red;
5103 cell.bg.green = green;
5104 cell.bg.blue = blue;
5105 cell.bg.index = index;
5106 }
5107 else
5108 {
5109 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005110 cell.fg.red = red;
5111 cell.fg.green = green;
5112 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005113 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005114 }
5115 }
5116 else
5117 dump_is_corrupt(&ga_text);
5118 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005119 }
5120 else
5121 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005122 }
5123 else
5124 dump_is_corrupt(&ga_text);
5125
5126 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005127 if (cell.width == 2)
5128 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005129 }
5130 else if (c == '@')
5131 {
5132 if (prev_char == NULL)
5133 dump_is_corrupt(&ga_text);
5134 else
5135 {
5136 int count = 0;
5137
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005138 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005139 for (;;)
5140 {
5141 c = fgetc(fd);
5142 if (!isdigit(c))
5143 break;
5144 count = count * 10 + (c - '0');
5145 }
5146
5147 while (count-- > 0)
5148 {
5149 ga_concat(&ga_text, prev_char);
5150 append_cell(&ga_cell, &cell);
5151 }
5152 }
5153 }
5154 else
5155 {
5156 dump_is_corrupt(&ga_text);
5157 c = fgetc(fd);
5158 }
5159 }
5160
5161 if (ga_text.ga_len > 0)
5162 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005163 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005164 dump_is_corrupt(&ga_text);
5165 ga_append(&ga_text, NUL);
5166 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5167 ga_text.ga_len, FALSE);
5168 }
5169
5170 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005171 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005172 vim_free(prev_char);
5173
5174 return max_cells;
5175}
5176
5177/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005178 * Return an allocated string with at least "text_width" "=" characters and
5179 * "fname" inserted in the middle.
5180 */
5181 static char_u *
5182get_separator(int text_width, char_u *fname)
5183{
5184 int width = MAX(text_width, curwin->w_width);
5185 char_u *textline;
5186 int fname_size;
5187 char_u *p = fname;
5188 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005189 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005190
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005191 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005192 if (textline == NULL)
5193 return NULL;
5194
5195 fname_size = vim_strsize(fname);
5196 if (fname_size < width - 8)
5197 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005198 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005199 width = MAX(text_width, fname_size + 8);
5200 }
5201 else if (fname_size > width - 8)
5202 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005203 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005204 p = gettail(fname);
5205 fname_size = vim_strsize(p);
5206 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005207 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005208 while (fname_size > width - 8)
5209 {
5210 p += (*mb_ptr2len)(p);
5211 fname_size = vim_strsize(p);
5212 }
5213
5214 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5215 textline[i] = '=';
5216 textline[i++] = ' ';
5217
5218 STRCPY(textline + i, p);
5219 off = STRLEN(textline);
5220 textline[off] = ' ';
5221 for (i = 1; i < (width - fname_size) / 2; ++i)
5222 textline[off + i] = '=';
5223 textline[off + i] = NUL;
5224
5225 return textline;
5226}
5227
5228/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005229 * Common for "term_dumpdiff()" and "term_dumpload()".
5230 */
5231 static void
5232term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5233{
5234 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005235 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005236 char_u buf1[NUMBUFLEN];
5237 char_u buf2[NUMBUFLEN];
5238 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005239 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005240 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005241 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005242 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005243 char_u *textline = NULL;
5244
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005245 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005246 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005247 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005248 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005249 if (fname1 == NULL || (do_diff && fname2 == NULL))
5250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005251 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005252 return;
5253 }
5254 fd1 = mch_fopen((char *)fname1, READBIN);
5255 if (fd1 == NULL)
5256 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005257 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005258 return;
5259 }
5260 if (do_diff)
5261 {
5262 fd2 = mch_fopen((char *)fname2, READBIN);
5263 if (fd2 == NULL)
5264 {
5265 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005266 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005267 return;
5268 }
5269 }
5270
5271 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005272 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5273 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5274 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5275 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5276 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005277
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005278 if (opt.jo_term_name == NULL)
5279 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005280 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005281
Bram Moolenaar51e14382019-05-25 20:21:28 +02005282 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005283 if (fname_tofree != NULL)
5284 {
5285 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5286 opt.jo_term_name = fname_tofree;
5287 }
5288 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005289
Bram Moolenaar87abab92019-06-03 21:14:59 +02005290 if (opt.jo_bufnr_buf != NULL)
5291 {
5292 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5293
5294 // With "bufnr" argument: enter the window with this buffer and make it
5295 // empty.
5296 if (wp == NULL)
5297 semsg(_(e_invarg2), "bufnr");
5298 else
5299 {
5300 buf = curbuf;
5301 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005302 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005303 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005304 redraw_later(NOT_VALID);
5305 }
5306 }
5307 else
5308 // Create a new terminal window.
5309 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5310
Bram Moolenaard96ff162018-02-18 22:13:29 +01005311 if (buf != NULL && buf->b_term != NULL)
5312 {
5313 int i;
5314 linenr_T bot_lnum;
5315 linenr_T lnum;
5316 term_T *term = buf->b_term;
5317 int width;
5318 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005319 VTermPos cursor_pos1;
5320 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005321
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005322 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005323
Bram Moolenaard96ff162018-02-18 22:13:29 +01005324 rettv->vval.v_number = buf->b_fnum;
5325
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005326 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005327 width = read_dump_file(fd1, &cursor_pos1);
5328
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005329 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005330 if (cursor_pos1.row >= 0)
5331 {
5332 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5333 coladvance(cursor_pos1.col);
5334 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005335
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005336 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005337 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005338
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005339 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005340 if (!do_diff)
5341 goto theend;
5342
5343 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5344
Bram Moolenaar4a696342018-04-05 18:45:26 +02005345 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005346 if (textline == NULL)
5347 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005348 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5349 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5350 vim_free(textline);
5351
5352 textline = get_separator(width, fname2);
5353 if (textline == NULL)
5354 goto theend;
5355 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5356 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005357 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005358
5359 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005360 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005361 if (width2 > width)
5362 {
5363 vim_free(textline);
5364 textline = alloc(width2 + 1);
5365 if (textline == NULL)
5366 goto theend;
5367 width = width2;
5368 textline[width] = NUL;
5369 }
5370 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5371
5372 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5373 {
5374 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5375 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005376 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005377 for (i = 0; i < width; ++i)
5378 textline[i] = '-';
5379 }
5380 else
5381 {
5382 char_u *line1;
5383 char_u *line2;
5384 char_u *p1;
5385 char_u *p2;
5386 int col;
5387 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5388 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5389 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5390 ->sb_cells;
5391
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005392 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005393 line1 = vim_strsave(ml_get(lnum));
5394 if (line1 == NULL)
5395 break;
5396 p1 = line1;
5397
5398 line2 = ml_get(lnum + bot_lnum);
5399 p2 = line2;
5400 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5401 {
5402 int len1 = utfc_ptr2len(p1);
5403 int len2 = utfc_ptr2len(p2);
5404
5405 textline[col] = ' ';
5406 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005407 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005408 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005409 else if (lnum == cursor_pos1.row + 1
5410 && col == cursor_pos1.col
5411 && (cursor_pos1.row != cursor_pos2.row
5412 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005413 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005414 textline[col] = '>';
5415 else if (lnum == cursor_pos2.row + 1
5416 && col == cursor_pos2.col
5417 && (cursor_pos1.row != cursor_pos2.row
5418 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005419 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005420 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005421 else if (cellattr1 != NULL && cellattr2 != NULL)
5422 {
5423 if ((cellattr1 + col)->width
5424 != (cellattr2 + col)->width)
5425 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005426 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005427 &(cellattr2 + col)->fg))
5428 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005429 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005430 &(cellattr2 + col)->bg))
5431 textline[col] = 'b';
5432 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5433 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5434 textline[col] = 'a';
5435 }
5436 p1 += len1;
5437 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005438 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005439 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005440
5441 while (col < width)
5442 {
5443 if (*p1 == NUL && *p2 == NUL)
5444 textline[col] = '?';
5445 else if (*p1 == NUL)
5446 {
5447 textline[col] = '+';
5448 p2 += utfc_ptr2len(p2);
5449 }
5450 else
5451 {
5452 textline[col] = '-';
5453 p1 += utfc_ptr2len(p1);
5454 }
5455 ++col;
5456 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005457
5458 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005459 }
5460 if (add_empty_scrollback(term, &term->tl_default_color,
5461 term->tl_top_diff_rows) == OK)
5462 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5463 ++bot_lnum;
5464 }
5465
5466 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5467 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005468 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005469 for (i = 0; i < width; ++i)
5470 textline[i] = '+';
5471 if (add_empty_scrollback(term, &term->tl_default_color,
5472 term->tl_top_diff_rows) == OK)
5473 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5474 ++lnum;
5475 ++bot_lnum;
5476 }
5477
5478 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005479
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005480 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005481 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005482 }
5483
5484theend:
5485 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005486 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005487 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005488 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005489 fclose(fd2);
5490}
5491
5492/*
5493 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5494 * bottom files.
5495 * Return FAIL when this is not possible.
5496 */
5497 int
5498term_swap_diff()
5499{
5500 term_T *term = curbuf->b_term;
5501 linenr_T line_count;
5502 linenr_T top_rows;
5503 linenr_T bot_rows;
5504 linenr_T bot_start;
5505 linenr_T lnum;
5506 char_u *p;
5507 sb_line_T *sb_line;
5508
5509 if (term == NULL
5510 || !term_is_finished(curbuf)
5511 || term->tl_top_diff_rows == 0
5512 || term->tl_scrollback.ga_len == 0)
5513 return FAIL;
5514
5515 line_count = curbuf->b_ml.ml_line_count;
5516 top_rows = term->tl_top_diff_rows;
5517 bot_rows = term->tl_bot_diff_rows;
5518 bot_start = line_count - bot_rows;
5519 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5520
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005521 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005522 for (lnum = 1; lnum <= top_rows; ++lnum)
5523 {
5524 p = vim_strsave(ml_get(1));
5525 if (p == NULL)
5526 return OK;
5527 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005528 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005529 vim_free(p);
5530 }
5531
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005532 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005533 for (lnum = 1; lnum <= bot_rows; ++lnum)
5534 {
5535 p = vim_strsave(ml_get(bot_start + lnum));
5536 if (p == NULL)
5537 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005538 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005539 ml_append(lnum - 1, p, 0, FALSE);
5540 vim_free(p);
5541 }
5542
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005543 // move top title to bottom
5544 p = vim_strsave(ml_get(bot_rows + 1));
5545 if (p == NULL)
5546 return OK;
5547 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005548 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005549 vim_free(p);
5550
5551 // move bottom title to top
5552 p = vim_strsave(ml_get(line_count - top_rows));
5553 if (p == NULL)
5554 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005555 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005556 ml_append(bot_rows, p, 0, FALSE);
5557 vim_free(p);
5558
Bram Moolenaard96ff162018-02-18 22:13:29 +01005559 if (top_rows == bot_rows)
5560 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005561 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005562 for (lnum = 0; lnum < top_rows; ++lnum)
5563 {
5564 sb_line_T temp;
5565
5566 temp = *(sb_line + lnum);
5567 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5568 *(sb_line + bot_start + lnum) = temp;
5569 }
5570 }
5571 else
5572 {
5573 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005574 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005575
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005576 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005577 if (temp != NULL)
5578 {
5579 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5580 mch_memmove(term->tl_scrollback.ga_data,
5581 temp + bot_start,
5582 sizeof(sb_line_T) * bot_rows);
5583 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5584 temp + top_rows,
5585 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5586 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5587 + line_count - top_rows,
5588 temp,
5589 sizeof(sb_line_T) * top_rows);
5590 vim_free(temp);
5591 }
5592 }
5593
5594 term->tl_top_diff_rows = bot_rows;
5595 term->tl_bot_diff_rows = top_rows;
5596
5597 update_screen(NOT_VALID);
5598 return OK;
5599}
5600
5601/*
5602 * "term_dumpdiff(filename, filename, options)" function
5603 */
5604 void
5605f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5606{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005607 if (in_vim9script()
5608 && (check_for_string_arg(argvars, 0) == FAIL
5609 || check_for_string_arg(argvars, 1) == FAIL
5610 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5611 return;
5612
Bram Moolenaard96ff162018-02-18 22:13:29 +01005613 term_load_dump(argvars, rettv, TRUE);
5614}
5615
5616/*
5617 * "term_dumpload(filename, options)" function
5618 */
5619 void
5620f_term_dumpload(typval_T *argvars, typval_T *rettv)
5621{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005622 if (in_vim9script()
5623 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005624 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005625 return;
5626
Bram Moolenaard96ff162018-02-18 22:13:29 +01005627 term_load_dump(argvars, rettv, FALSE);
5628}
5629
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005630/*
5631 * "term_getaltscreen(buf)" function
5632 */
5633 void
5634f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5635{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005636 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005637
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005638 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5639 return;
5640
5641 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005642 if (buf == NULL)
5643 return;
5644 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5645}
5646
5647/*
5648 * "term_getattr(attr, name)" function
5649 */
5650 void
5651f_term_getattr(typval_T *argvars, typval_T *rettv)
5652{
5653 int attr;
5654 size_t i;
5655 char_u *name;
5656
5657 static struct {
5658 char *name;
5659 int attr;
5660 } attrs[] = {
5661 {"bold", HL_BOLD},
5662 {"italic", HL_ITALIC},
5663 {"underline", HL_UNDERLINE},
5664 {"strike", HL_STRIKETHROUGH},
5665 {"reverse", HL_INVERSE},
5666 };
5667
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005668 if (in_vim9script()
5669 && (check_for_number_arg(argvars, 0) == FAIL
5670 || check_for_string_arg(argvars, 1) == FAIL))
5671 return;
5672
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005673 attr = tv_get_number(&argvars[0]);
5674 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005675 if (name == NULL)
5676 return;
5677
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005678 if (attr > HL_ALL)
5679 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02005680 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005681 if (STRCMP(name, attrs[i].name) == 0)
5682 {
5683 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5684 break;
5685 }
5686}
5687
5688/*
5689 * "term_getcursor(buf)" function
5690 */
5691 void
5692f_term_getcursor(typval_T *argvars, typval_T *rettv)
5693{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005694 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005695 term_T *term;
5696 list_T *l;
5697 dict_T *d;
5698
5699 if (rettv_list_alloc(rettv) == FAIL)
5700 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005701
5702 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5703 return;
5704
5705 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005706 if (buf == NULL)
5707 return;
5708 term = buf->b_term;
5709
5710 l = rettv->vval.v_list;
5711 list_append_number(l, term->tl_cursor_pos.row + 1);
5712 list_append_number(l, term->tl_cursor_pos.col + 1);
5713
5714 d = dict_alloc();
5715 if (d != NULL)
5716 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005717 dict_add_number(d, "visible", term->tl_cursor_visible);
5718 dict_add_number(d, "blink", blink_state_is_inverted()
5719 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5720 dict_add_number(d, "shape", term->tl_cursor_shape);
5721 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005722 list_append_dict(l, d);
5723 }
5724}
5725
5726/*
5727 * "term_getjob(buf)" function
5728 */
5729 void
5730f_term_getjob(typval_T *argvars, typval_T *rettv)
5731{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005732 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005733
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005734 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5735 return;
5736
5737 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005738 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005739 {
5740 rettv->v_type = VAR_SPECIAL;
5741 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005742 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005743 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005744
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005745 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005746 rettv->vval.v_job = buf->b_term->tl_job;
5747 if (rettv->vval.v_job != NULL)
5748 ++rettv->vval.v_job->jv_refcount;
5749}
5750
5751 static int
5752get_row_number(typval_T *tv, term_T *term)
5753{
5754 if (tv->v_type == VAR_STRING
5755 && tv->vval.v_string != NULL
5756 && STRCMP(tv->vval.v_string, ".") == 0)
5757 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005758 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005759}
5760
5761/*
5762 * "term_getline(buf, row)" function
5763 */
5764 void
5765f_term_getline(typval_T *argvars, typval_T *rettv)
5766{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005767 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005768 term_T *term;
5769 int row;
5770
5771 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005772
5773 if (in_vim9script()
5774 && (check_for_buffer_arg(argvars, 0) == FAIL
5775 || check_for_lnum_arg(argvars, 1) == FAIL))
5776 return;
5777
5778 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005779 if (buf == NULL)
5780 return;
5781 term = buf->b_term;
5782 row = get_row_number(&argvars[1], term);
5783
5784 if (term->tl_vterm == NULL)
5785 {
5786 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5787
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005788 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005789 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5790 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5791 }
5792 else
5793 {
5794 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5795 VTermRect rect;
5796 int len;
5797 char_u *p;
5798
5799 if (row < 0 || row >= term->tl_rows)
5800 return;
5801 len = term->tl_cols * MB_MAXBYTES + 1;
5802 p = alloc(len);
5803 if (p == NULL)
5804 return;
5805 rettv->vval.v_string = p;
5806
5807 rect.start_col = 0;
5808 rect.end_col = term->tl_cols;
5809 rect.start_row = row;
5810 rect.end_row = row + 1;
5811 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5812 }
5813}
5814
5815/*
5816 * "term_getscrolled(buf)" function
5817 */
5818 void
5819f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5820{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005821 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005822
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005823 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5824 return;
5825
5826 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005827 if (buf == NULL)
5828 return;
5829 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5830}
5831
5832/*
5833 * "term_getsize(buf)" function
5834 */
5835 void
5836f_term_getsize(typval_T *argvars, typval_T *rettv)
5837{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005838 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005839 list_T *l;
5840
5841 if (rettv_list_alloc(rettv) == FAIL)
5842 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005843
5844 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5845 return;
5846
5847 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005848 if (buf == NULL)
5849 return;
5850
5851 l = rettv->vval.v_list;
5852 list_append_number(l, buf->b_term->tl_rows);
5853 list_append_number(l, buf->b_term->tl_cols);
5854}
5855
5856/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005857 * "term_setsize(buf, rows, cols)" function
5858 */
5859 void
5860f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5861{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005862 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02005863 term_T *term;
5864 varnumber_T rows, cols;
5865
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005866 if (in_vim9script()
5867 && (check_for_buffer_arg(argvars, 0) == FAIL
5868 || check_for_number_arg(argvars, 1) == FAIL
5869 || check_for_number_arg(argvars, 2) == FAIL))
5870 return;
5871
5872 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005873 if (buf == NULL)
5874 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005875 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005876 return;
5877 }
5878 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005879 return;
5880 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005881 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005882 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005883 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005884 cols = cols <= 0 ? term->tl_cols : cols;
5885 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005886 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005887
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005888 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005889 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5890 term_report_winsize(term, term->tl_rows, term->tl_cols);
5891}
5892
5893/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005894 * "term_getstatus(buf)" function
5895 */
5896 void
5897f_term_getstatus(typval_T *argvars, typval_T *rettv)
5898{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005899 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005900 term_T *term;
5901 char_u val[100];
5902
5903 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005904
5905 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5906 return;
5907
5908 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005909 if (buf == NULL)
5910 return;
5911 term = buf->b_term;
5912
5913 if (term_job_running(term))
5914 STRCPY(val, "running");
5915 else
5916 STRCPY(val, "finished");
5917 if (term->tl_normal_mode)
5918 STRCAT(val, ",normal");
5919 rettv->vval.v_string = vim_strsave(val);
5920}
5921
5922/*
5923 * "term_gettitle(buf)" function
5924 */
5925 void
5926f_term_gettitle(typval_T *argvars, typval_T *rettv)
5927{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005928 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005929
5930 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005931
5932 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5933 return;
5934
5935 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005936 if (buf == NULL)
5937 return;
5938
5939 if (buf->b_term->tl_title != NULL)
5940 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5941}
5942
5943/*
5944 * "term_gettty(buf)" function
5945 */
5946 void
5947f_term_gettty(typval_T *argvars, typval_T *rettv)
5948{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005949 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005950 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005951 int num = 0;
5952
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005953 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005954 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005955 || check_for_opt_bool_arg(argvars, 1) == FAIL))
5956 return;
5957
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005958 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005959 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005960 if (buf == NULL)
5961 return;
5962 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02005963 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005964
5965 switch (num)
5966 {
5967 case 0:
5968 if (buf->b_term->tl_job != NULL)
5969 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005970 break;
5971 case 1:
5972 if (buf->b_term->tl_job != NULL)
5973 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005974 break;
5975 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005976 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005977 return;
5978 }
5979 if (p != NULL)
5980 rettv->vval.v_string = vim_strsave(p);
5981}
5982
5983/*
5984 * "term_list()" function
5985 */
5986 void
5987f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
5988{
5989 term_T *tp;
5990 list_T *l;
5991
5992 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
5993 return;
5994
5995 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005996 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02005997 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005998 if (list_append_number(l,
5999 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6000 return;
6001}
6002
6003/*
6004 * "term_scrape(buf, row)" function
6005 */
6006 void
6007f_term_scrape(typval_T *argvars, typval_T *rettv)
6008{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006009 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006010 VTermScreen *screen = NULL;
6011 VTermPos pos;
6012 list_T *l;
6013 term_T *term;
6014 char_u *p;
6015 sb_line_T *line;
6016
6017 if (rettv_list_alloc(rettv) == FAIL)
6018 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006019
6020 if (in_vim9script()
6021 && (check_for_buffer_arg(argvars, 0) == FAIL
6022 || check_for_lnum_arg(argvars, 1) == FAIL))
6023 return;
6024
6025 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006026 if (buf == NULL)
6027 return;
6028 term = buf->b_term;
6029
6030 l = rettv->vval.v_list;
6031 pos.row = get_row_number(&argvars[1], term);
6032
6033 if (term->tl_vterm != NULL)
6034 {
6035 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006036 if (screen == NULL) // can't really happen
6037 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006038 p = NULL;
6039 line = NULL;
6040 }
6041 else
6042 {
6043 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6044
6045 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6046 return;
6047 p = ml_get_buf(buf, lnum + 1, FALSE);
6048 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6049 }
6050
6051 for (pos.col = 0; pos.col < term->tl_cols; )
6052 {
6053 dict_T *dcell;
6054 int width;
6055 VTermScreenCellAttrs attrs;
6056 VTermColor fg, bg;
6057 char_u rgb[8];
6058 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6059 int off = 0;
6060 int i;
6061
6062 if (screen == NULL)
6063 {
6064 cellattr_T *cellattr;
6065 int len;
6066
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006067 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006068 if (pos.col >= line->sb_cols)
6069 break;
6070 cellattr = line->sb_cells + pos.col;
6071 width = cellattr->width;
6072 attrs = cellattr->attrs;
6073 fg = cellattr->fg;
6074 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006075 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006076 mch_memmove(mbs, p, len);
6077 mbs[len] = NUL;
6078 p += len;
6079 }
6080 else
6081 {
6082 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006083
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006084 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6085 break;
6086 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6087 {
6088 if (cell.chars[i] == 0)
6089 break;
6090 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6091 }
6092 mbs[off] = NUL;
6093 width = cell.width;
6094 attrs = cell.attrs;
6095 fg = cell.fg;
6096 bg = cell.bg;
6097 }
6098 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006099 if (dcell == NULL)
6100 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006101 list_append_dict(l, dcell);
6102
Bram Moolenaare0be1672018-07-08 16:50:37 +02006103 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006104
6105 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6106 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006107 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006108 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6109 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006110 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006111
Bram Moolenaar83d47902020-03-26 20:34:00 +01006112 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006113 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006114
6115 ++pos.col;
6116 if (width == 2)
6117 ++pos.col;
6118 }
6119}
6120
6121/*
6122 * "term_sendkeys(buf, keys)" function
6123 */
6124 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006125f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006126{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006127 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006128 char_u *msg;
6129 term_T *term;
6130
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006131 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006132 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006133 || check_for_string_arg(argvars, 1) == FAIL))
6134 return;
6135
6136 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006137 if (buf == NULL)
6138 return;
6139
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006140 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006141 if (msg == NULL)
6142 return;
6143 term = buf->b_term;
6144 if (term->tl_vterm == NULL)
6145 return;
6146
6147 while (*msg != NUL)
6148 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006149 int c;
6150
6151 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6152 {
6153 c = TO_SPECIAL(msg[1], msg[2]);
6154 msg += 3;
6155 }
6156 else
6157 {
6158 c = PTR2CHAR(msg);
6159 msg += MB_CPTR2LEN(msg);
6160 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006161 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006162 }
6163}
6164
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006165#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6166/*
6167 * "term_getansicolors(buf)" function
6168 */
6169 void
6170f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6171{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006172 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006173 term_T *term;
6174 VTermState *state;
6175 VTermColor color;
6176 char_u hexbuf[10];
6177 int index;
6178 list_T *list;
6179
6180 if (rettv_list_alloc(rettv) == FAIL)
6181 return;
6182
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006183 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6184 return;
6185
6186 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006187 if (buf == NULL)
6188 return;
6189 term = buf->b_term;
6190 if (term->tl_vterm == NULL)
6191 return;
6192
6193 list = rettv->vval.v_list;
6194 state = vterm_obtain_state(term->tl_vterm);
6195 for (index = 0; index < 16; index++)
6196 {
6197 vterm_state_get_palette_color(state, index, &color);
6198 sprintf((char *)hexbuf, "#%02x%02x%02x",
6199 color.red, color.green, color.blue);
6200 if (list_append_string(list, hexbuf, 7) == FAIL)
6201 return;
6202 }
6203}
6204
6205/*
6206 * "term_setansicolors(buf, list)" function
6207 */
6208 void
6209f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6210{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006211 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006212 term_T *term;
6213
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006214 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006215 && (check_for_buffer_arg(argvars, 0) == FAIL
6216 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006217 return;
6218
6219 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006220 if (buf == NULL)
6221 return;
6222 term = buf->b_term;
6223 if (term->tl_vterm == NULL)
6224 return;
6225
6226 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6227 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006228 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006229 return;
6230 }
6231
6232 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006233 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006234}
6235#endif
6236
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006237/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006238 * "term_setapi(buf, api)" function
6239 */
6240 void
6241f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6242{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006243 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006244 term_T *term;
6245 char_u *api;
6246
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006247 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006248 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006249 || check_for_string_arg(argvars, 1) == FAIL))
6250 return;
6251
6252 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006253 if (buf == NULL)
6254 return;
6255 term = buf->b_term;
6256 vim_free(term->tl_api);
6257 api = tv_get_string_chk(&argvars[1]);
6258 if (api != NULL)
6259 term->tl_api = vim_strsave(api);
6260 else
6261 term->tl_api = NULL;
6262}
6263
6264/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006265 * "term_setrestore(buf, command)" function
6266 */
6267 void
6268f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6269{
6270#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006271 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006272 term_T *term;
6273 char_u *cmd;
6274
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006275 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006276 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006277 || check_for_string_arg(argvars, 1) == FAIL))
6278 return;
6279
6280 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006281 if (buf == NULL)
6282 return;
6283 term = buf->b_term;
6284 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006285 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006286 if (cmd != NULL)
6287 term->tl_command = vim_strsave(cmd);
6288 else
6289 term->tl_command = NULL;
6290#endif
6291}
6292
6293/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006294 * "term_setkill(buf, how)" function
6295 */
6296 void
6297f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6298{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006299 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006300 term_T *term;
6301 char_u *how;
6302
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006303 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006304 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006305 || check_for_string_arg(argvars, 1) == FAIL))
6306 return;
6307
6308 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006309 if (buf == NULL)
6310 return;
6311 term = buf->b_term;
6312 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006313 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006314 if (how != NULL)
6315 term->tl_kill = vim_strsave(how);
6316 else
6317 term->tl_kill = NULL;
6318}
6319
6320/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006321 * "term_start(command, options)" function
6322 */
6323 void
6324f_term_start(typval_T *argvars, typval_T *rettv)
6325{
6326 jobopt_T opt;
6327 buf_T *buf;
6328
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006329 if (in_vim9script()
6330 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6331 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6332 return;
6333
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006334 init_job_options(&opt);
6335 if (argvars[1].v_type != VAR_UNKNOWN
6336 && get_job_options(&argvars[1], &opt,
6337 JO_TIMEOUT_ALL + JO_STOPONEXIT
6338 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6339 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6340 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6341 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006342 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006343 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006344 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006345 return;
6346
Bram Moolenaar13568252018-03-16 20:46:58 +01006347 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006348
6349 if (buf != NULL && buf->b_term != NULL)
6350 rettv->vval.v_number = buf->b_fnum;
6351}
6352
6353/*
6354 * "term_wait" function
6355 */
6356 void
6357f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6358{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006359 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006360
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006361 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006362 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006363 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006364 return;
6365
6366 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006367 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006368 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006369 if (buf->b_term->tl_job == NULL)
6370 {
6371 ch_log(NULL, "term_wait(): no job to wait for");
6372 return;
6373 }
6374 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006375 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006376 return;
6377
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006378 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006379 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006380 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6381 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006382 // The job is dead, keep reading channel I/O until the channel is
6383 // closed. buf->b_term may become NULL if the terminal was closed while
6384 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006385 ch_log(NULL, "term_wait(): waiting for channel to close");
6386 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6387 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006388 term_flush_messages();
6389
Bram Moolenaard45aa552018-05-21 22:50:29 +02006390 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006391 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006392 // If the terminal is closed when the channel is closed the
6393 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006394 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006395 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006396
6397 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006398 }
6399 else
6400 {
6401 long wait = 10L;
6402
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006403 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006404
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006405 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006406 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006407 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006408 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006409
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006410 // Flushing messages on channels is hopefully sufficient.
6411 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006412 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006413 }
6414}
6415
6416/*
6417 * Called when a channel has sent all the lines to a terminal.
6418 * Send a CTRL-D to mark the end of the text.
6419 */
6420 void
6421term_send_eof(channel_T *ch)
6422{
6423 term_T *term;
6424
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006425 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006426 if (term->tl_job == ch->ch_job)
6427 {
6428 if (term->tl_eof_chars != NULL)
6429 {
6430 channel_send(ch, PART_IN, term->tl_eof_chars,
6431 (int)STRLEN(term->tl_eof_chars), NULL);
6432 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6433 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006434# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006435 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006436 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006437 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6438# endif
6439 }
6440}
6441
Bram Moolenaar113e1072019-01-20 15:30:40 +01006442#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006443 job_T *
6444term_getjob(term_T *term)
6445{
6446 return term != NULL ? term->tl_job : NULL;
6447}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006448#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006449
Bram Moolenaar4f974752019-02-17 17:44:42 +01006450# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006451
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006452///////////////////////////////////////
6453// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006454#ifdef PROTO
6455typedef int COORD;
6456typedef int DWORD;
6457typedef int HANDLE;
6458typedef int *DWORD_PTR;
6459typedef int HPCON;
6460typedef int HRESULT;
6461typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006462typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006463typedef int PSIZE_T;
6464typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006465typedef int BOOL;
6466# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006467#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006468
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006469HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6470HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6471HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006472BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6473BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6474void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006475
6476 static int
6477dyn_conpty_init(int verbose)
6478{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006479 static HMODULE hKerneldll = NULL;
6480 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006481 static struct
6482 {
6483 char *name;
6484 FARPROC *ptr;
6485 } conpty_entry[] =
6486 {
6487 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6488 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6489 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6490 {"InitializeProcThreadAttributeList",
6491 (FARPROC*)&pInitializeProcThreadAttributeList},
6492 {"UpdateProcThreadAttribute",
6493 (FARPROC*)&pUpdateProcThreadAttribute},
6494 {"DeleteProcThreadAttributeList",
6495 (FARPROC*)&pDeleteProcThreadAttributeList},
6496 {NULL, NULL}
6497 };
6498
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006499 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006500 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006501 if (verbose)
6502 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006503 return FAIL;
6504 }
6505
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006506 // No need to initialize twice.
6507 if (hKerneldll)
6508 return OK;
6509
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006510 hKerneldll = vimLoadLib("kernel32.dll");
6511 for (i = 0; conpty_entry[i].name != NULL
6512 && conpty_entry[i].ptr != NULL; ++i)
6513 {
6514 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6515 conpty_entry[i].name)) == NULL)
6516 {
6517 if (verbose)
6518 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006519 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006520 return FAIL;
6521 }
6522 }
6523
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006524 return OK;
6525}
6526
6527 static int
6528conpty_term_and_job_init(
6529 term_T *term,
6530 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006531 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006532 jobopt_T *opt,
6533 jobopt_T *orig_opt)
6534{
6535 WCHAR *cmd_wchar = NULL;
6536 WCHAR *cmd_wchar_copy = NULL;
6537 WCHAR *cwd_wchar = NULL;
6538 WCHAR *env_wchar = NULL;
6539 channel_T *channel = NULL;
6540 job_T *job = NULL;
6541 HANDLE jo = NULL;
6542 garray_T ga_cmd, ga_env;
6543 char_u *cmd = NULL;
6544 HRESULT hr;
6545 COORD consize;
6546 SIZE_T breq;
6547 PROCESS_INFORMATION proc_info;
6548 HANDLE i_theirs = NULL;
6549 HANDLE o_theirs = NULL;
6550 HANDLE i_ours = NULL;
6551 HANDLE o_ours = NULL;
6552
6553 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6554 ga_init2(&ga_env, (int)sizeof(char*), 20);
6555
6556 if (argvar->v_type == VAR_STRING)
6557 {
6558 cmd = argvar->vval.v_string;
6559 }
6560 else if (argvar->v_type == VAR_LIST)
6561 {
6562 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6563 goto failed;
6564 cmd = ga_cmd.ga_data;
6565 }
6566 if (cmd == NULL || *cmd == NUL)
6567 {
6568 emsg(_(e_invarg));
6569 goto failed;
6570 }
6571
6572 term->tl_arg0_cmd = vim_strsave(cmd);
6573
6574 cmd_wchar = enc_to_utf16(cmd, NULL);
6575
6576 if (cmd_wchar != NULL)
6577 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006578 // Request by CreateProcessW
6579 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006580 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006581 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6582 }
6583
6584 ga_clear(&ga_cmd);
6585 if (cmd_wchar == NULL)
6586 goto failed;
6587 if (opt->jo_cwd != NULL)
6588 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6589
6590 win32_build_env(opt->jo_env, &ga_env, TRUE);
6591 env_wchar = ga_env.ga_data;
6592
6593 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6594 goto failed;
6595 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6596 goto failed;
6597
6598 consize.X = term->tl_cols;
6599 consize.Y = term->tl_rows;
6600 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6601 &term->tl_conpty);
6602 if (FAILED(hr))
6603 goto failed;
6604
6605 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6606
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006607 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006608 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006609 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006610 if (!term->tl_siex.lpAttributeList)
6611 goto failed;
6612 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6613 0, &breq))
6614 goto failed;
6615 if (!pUpdateProcThreadAttribute(
6616 term->tl_siex.lpAttributeList, 0,
6617 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6618 sizeof(HPCON), NULL, NULL))
6619 goto failed;
6620
6621 channel = add_channel();
6622 if (channel == NULL)
6623 goto failed;
6624
6625 job = job_alloc();
6626 if (job == NULL)
6627 goto failed;
6628 if (argvar->v_type == VAR_STRING)
6629 {
6630 int argc;
6631
6632 build_argv_from_string(cmd, &job->jv_argv, &argc);
6633 }
6634 else
6635 {
6636 int argc;
6637
6638 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6639 }
6640
6641 if (opt->jo_set & JO_IN_BUF)
6642 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6643
6644 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6645 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006646 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006647 env_wchar, cwd_wchar,
6648 &term->tl_siex.StartupInfo, &proc_info))
6649 goto failed;
6650
6651 CloseHandle(i_theirs);
6652 CloseHandle(o_theirs);
6653
6654 channel_set_pipes(channel,
6655 (sock_T)i_ours,
6656 (sock_T)o_ours,
6657 (sock_T)o_ours);
6658
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006659 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006660 channel->ch_write_text_mode = TRUE;
6661
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006662 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006663 channel->ch_anonymous_pipe = TRUE;
6664
6665 jo = CreateJobObject(NULL, NULL);
6666 if (jo == NULL)
6667 goto failed;
6668
6669 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6670 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006671 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006672 CloseHandle(jo);
6673 jo = NULL;
6674 }
6675
6676 ResumeThread(proc_info.hThread);
6677 CloseHandle(proc_info.hThread);
6678
6679 vim_free(cmd_wchar);
6680 vim_free(cmd_wchar_copy);
6681 vim_free(cwd_wchar);
6682 vim_free(env_wchar);
6683
6684 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6685 goto failed;
6686
6687#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6688 if (opt->jo_set2 & JO2_ANSI_COLORS)
6689 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6690 else
6691 init_vterm_ansi_colors(term->tl_vterm);
6692#endif
6693
6694 channel_set_job(channel, job, opt);
6695 job_set_options(job, opt);
6696
6697 job->jv_channel = channel;
6698 job->jv_proc_info = proc_info;
6699 job->jv_job_object = jo;
6700 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006701 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006702 ++job->jv_refcount;
6703 term->tl_job = job;
6704
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006705 // Redirecting stdout and stderr doesn't work at the job level. Instead
6706 // open the file here and handle it in. opt->jo_io was changed in
6707 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006708 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6709 {
6710 char_u *fname = opt->jo_io_name[PART_OUT];
6711
6712 ch_log(channel, "Opening output file %s", fname);
6713 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6714 if (term->tl_out_fd == NULL)
6715 semsg(_(e_notopen), fname);
6716 }
6717
6718 return OK;
6719
6720failed:
6721 ga_clear(&ga_cmd);
6722 ga_clear(&ga_env);
6723 vim_free(cmd_wchar);
6724 vim_free(cmd_wchar_copy);
6725 vim_free(cwd_wchar);
6726 if (channel != NULL)
6727 channel_clear(channel);
6728 if (job != NULL)
6729 {
6730 job->jv_channel = NULL;
6731 job_cleanup(job);
6732 }
6733 term->tl_job = NULL;
6734 if (jo != NULL)
6735 CloseHandle(jo);
6736
6737 if (term->tl_siex.lpAttributeList != NULL)
6738 {
6739 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6740 vim_free(term->tl_siex.lpAttributeList);
6741 }
6742 term->tl_siex.lpAttributeList = NULL;
6743 if (o_theirs != NULL)
6744 CloseHandle(o_theirs);
6745 if (o_ours != NULL)
6746 CloseHandle(o_ours);
6747 if (i_ours != NULL)
6748 CloseHandle(i_ours);
6749 if (i_theirs != NULL)
6750 CloseHandle(i_theirs);
6751 if (term->tl_conpty != NULL)
6752 pClosePseudoConsole(term->tl_conpty);
6753 term->tl_conpty = NULL;
6754 return FAIL;
6755}
6756
6757 static void
6758conpty_term_report_winsize(term_T *term, int rows, int cols)
6759{
6760 COORD consize;
6761
6762 consize.X = cols;
6763 consize.Y = rows;
6764 pResizePseudoConsole(term->tl_conpty, consize);
6765}
6766
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006767 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006768term_free_conpty(term_T *term)
6769{
6770 if (term->tl_siex.lpAttributeList != NULL)
6771 {
6772 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6773 vim_free(term->tl_siex.lpAttributeList);
6774 }
6775 term->tl_siex.lpAttributeList = NULL;
6776 if (term->tl_conpty != NULL)
6777 pClosePseudoConsole(term->tl_conpty);
6778 term->tl_conpty = NULL;
6779}
6780
6781 int
6782use_conpty(void)
6783{
6784 return has_conpty;
6785}
6786
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006787# ifndef PROTO
6788
6789#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6790#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006791#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006792
6793void* (*winpty_config_new)(UINT64, void*);
6794void* (*winpty_open)(void*, void*);
6795void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6796BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6797void (*winpty_config_set_mouse_mode)(void*, int);
6798void (*winpty_config_set_initial_size)(void*, int, int);
6799LPCWSTR (*winpty_conin_name)(void*);
6800LPCWSTR (*winpty_conout_name)(void*);
6801LPCWSTR (*winpty_conerr_name)(void*);
6802void (*winpty_free)(void*);
6803void (*winpty_config_free)(void*);
6804void (*winpty_spawn_config_free)(void*);
6805void (*winpty_error_free)(void*);
6806LPCWSTR (*winpty_error_msg)(void*);
6807BOOL (*winpty_set_size)(void*, int, int, void*);
6808HANDLE (*winpty_agent_process)(void*);
6809
6810#define WINPTY_DLL "winpty.dll"
6811
6812static HINSTANCE hWinPtyDLL = NULL;
6813# endif
6814
6815 static int
6816dyn_winpty_init(int verbose)
6817{
6818 int i;
6819 static struct
6820 {
6821 char *name;
6822 FARPROC *ptr;
6823 } winpty_entry[] =
6824 {
6825 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6826 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6827 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6828 {"winpty_config_set_mouse_mode",
6829 (FARPROC*)&winpty_config_set_mouse_mode},
6830 {"winpty_config_set_initial_size",
6831 (FARPROC*)&winpty_config_set_initial_size},
6832 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6833 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6834 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6835 {"winpty_free", (FARPROC*)&winpty_free},
6836 {"winpty_open", (FARPROC*)&winpty_open},
6837 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6838 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6839 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6840 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6841 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6842 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6843 {NULL, NULL}
6844 };
6845
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006846 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006847 if (hWinPtyDLL)
6848 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006849 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6850 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006851 if (*p_winptydll != NUL)
6852 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6853 if (!hWinPtyDLL)
6854 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6855 if (!hWinPtyDLL)
6856 {
6857 if (verbose)
Martin Tournoij1a3e5742021-07-24 13:57:29 +02006858 semsg(_(e_loadlib),
6859 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
6860 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006861 return FAIL;
6862 }
6863 for (i = 0; winpty_entry[i].name != NULL
6864 && winpty_entry[i].ptr != NULL; ++i)
6865 {
6866 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6867 winpty_entry[i].name)) == NULL)
6868 {
6869 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006870 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006871 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006872 return FAIL;
6873 }
6874 }
6875
6876 return OK;
6877}
6878
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006879 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006880winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006881 term_T *term,
6882 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006883 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006884 jobopt_T *opt,
6885 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006886{
6887 WCHAR *cmd_wchar = NULL;
6888 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006889 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006890 channel_T *channel = NULL;
6891 job_T *job = NULL;
6892 DWORD error;
6893 HANDLE jo = NULL;
6894 HANDLE child_process_handle;
6895 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006896 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006897 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006898 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006899 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006900
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006901 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6902 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006903
6904 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006905 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006906 cmd = argvar->vval.v_string;
6907 }
6908 else if (argvar->v_type == VAR_LIST)
6909 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006910 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006911 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006912 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006913 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006914 if (cmd == NULL || *cmd == NUL)
6915 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006916 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006917 goto failed;
6918 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006919
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006920 term->tl_arg0_cmd = vim_strsave(cmd);
6921
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006922 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006923 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006924 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006925 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006926 if (opt->jo_cwd != NULL)
6927 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006928
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006929 win32_build_env(opt->jo_env, &ga_env, TRUE);
6930 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006931
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006932 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6933 if (term->tl_winpty_config == NULL)
6934 goto failed;
6935
6936 winpty_config_set_mouse_mode(term->tl_winpty_config,
6937 WINPTY_MOUSE_MODE_FORCE);
6938 winpty_config_set_initial_size(term->tl_winpty_config,
6939 term->tl_cols, term->tl_rows);
6940 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6941 if (term->tl_winpty == NULL)
6942 goto failed;
6943
6944 spawn_config = winpty_spawn_config_new(
6945 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6946 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6947 NULL,
6948 cmd_wchar,
6949 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006950 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006951 &winpty_err);
6952 if (spawn_config == NULL)
6953 goto failed;
6954
6955 channel = add_channel();
6956 if (channel == NULL)
6957 goto failed;
6958
6959 job = job_alloc();
6960 if (job == NULL)
6961 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006962 if (argvar->v_type == VAR_STRING)
6963 {
6964 int argc;
6965
6966 build_argv_from_string(cmd, &job->jv_argv, &argc);
6967 }
6968 else
6969 {
6970 int argc;
6971
6972 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6973 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006974
6975 if (opt->jo_set & JO_IN_BUF)
6976 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6977
6978 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
6979 &child_thread_handle, &error, &winpty_err))
6980 goto failed;
6981
6982 channel_set_pipes(channel,
6983 (sock_T)CreateFileW(
6984 winpty_conin_name(term->tl_winpty),
6985 GENERIC_WRITE, 0, NULL,
6986 OPEN_EXISTING, 0, NULL),
6987 (sock_T)CreateFileW(
6988 winpty_conout_name(term->tl_winpty),
6989 GENERIC_READ, 0, NULL,
6990 OPEN_EXISTING, 0, NULL),
6991 (sock_T)CreateFileW(
6992 winpty_conerr_name(term->tl_winpty),
6993 GENERIC_READ, 0, NULL,
6994 OPEN_EXISTING, 0, NULL));
6995
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006996 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006997 channel->ch_write_text_mode = TRUE;
6998
6999 jo = CreateJobObject(NULL, NULL);
7000 if (jo == NULL)
7001 goto failed;
7002
7003 if (!AssignProcessToJobObject(jo, child_process_handle))
7004 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007005 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007006 CloseHandle(jo);
7007 jo = NULL;
7008 }
7009
7010 winpty_spawn_config_free(spawn_config);
7011 vim_free(cmd_wchar);
7012 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007013 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007014
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007015 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7016 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007017
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007018#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7019 if (opt->jo_set2 & JO2_ANSI_COLORS)
7020 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7021 else
7022 init_vterm_ansi_colors(term->tl_vterm);
7023#endif
7024
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007025 channel_set_job(channel, job, opt);
7026 job_set_options(job, opt);
7027
7028 job->jv_channel = channel;
7029 job->jv_proc_info.hProcess = child_process_handle;
7030 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7031 job->jv_job_object = jo;
7032 job->jv_status = JOB_STARTED;
7033 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007034 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007035 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007036 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007037 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007038 ++job->jv_refcount;
7039 term->tl_job = job;
7040
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007041 // Redirecting stdout and stderr doesn't work at the job level. Instead
7042 // open the file here and handle it in. opt->jo_io was changed in
7043 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007044 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7045 {
7046 char_u *fname = opt->jo_io_name[PART_OUT];
7047
7048 ch_log(channel, "Opening output file %s", fname);
7049 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7050 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007051 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007052 }
7053
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007054 return OK;
7055
7056failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007057 ga_clear(&ga_cmd);
7058 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007059 vim_free(cmd_wchar);
7060 vim_free(cwd_wchar);
7061 if (spawn_config != NULL)
7062 winpty_spawn_config_free(spawn_config);
7063 if (channel != NULL)
7064 channel_clear(channel);
7065 if (job != NULL)
7066 {
7067 job->jv_channel = NULL;
7068 job_cleanup(job);
7069 }
7070 term->tl_job = NULL;
7071 if (jo != NULL)
7072 CloseHandle(jo);
7073 if (term->tl_winpty != NULL)
7074 winpty_free(term->tl_winpty);
7075 term->tl_winpty = NULL;
7076 if (term->tl_winpty_config != NULL)
7077 winpty_config_free(term->tl_winpty_config);
7078 term->tl_winpty_config = NULL;
7079 if (winpty_err != NULL)
7080 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007081 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007082 (short_u *)winpty_error_msg(winpty_err), NULL);
7083
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007084 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007085 winpty_error_free(winpty_err);
7086 }
7087 return FAIL;
7088}
7089
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007090/*
7091 * Create a new terminal of "rows" by "cols" cells.
7092 * Store a reference in "term".
7093 * Return OK or FAIL.
7094 */
7095 static int
7096term_and_job_init(
7097 term_T *term,
7098 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007099 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007100 jobopt_T *opt,
7101 jobopt_T *orig_opt)
7102{
7103 int use_winpty = FALSE;
7104 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007105 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007106
7107 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7108 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7109
7110 if (!has_winpty && !has_conpty)
7111 // If neither is available give the errors for winpty, since when
7112 // conpty is not available it can't be installed either.
7113 return dyn_winpty_init(TRUE);
7114
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007115 if (opt->jo_tty_type != NUL)
7116 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007117
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007118 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007119 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007120 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007121 use_conpty = TRUE;
7122 else if (has_winpty)
7123 use_winpty = TRUE;
7124 // else: error
7125 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007126 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007127 {
7128 if (has_winpty)
7129 use_winpty = TRUE;
7130 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007131 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007132 {
7133 if (has_conpty)
7134 use_conpty = TRUE;
7135 else
7136 return dyn_conpty_init(TRUE);
7137 }
7138
7139 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007140 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007141
7142 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007143 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007144
7145 // error
7146 return dyn_winpty_init(TRUE);
7147}
7148
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007149 static int
7150create_pty_only(term_T *term, jobopt_T *options)
7151{
7152 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7153 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7154 char in_name[80], out_name[80];
7155 channel_T *channel = NULL;
7156
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007157 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7158 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007159
7160 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7161 GetCurrentProcessId(),
7162 curbuf->b_fnum);
7163 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7164 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7165 PIPE_UNLIMITED_INSTANCES,
7166 0, 0, NMPWAIT_NOWAIT, NULL);
7167 if (hPipeIn == INVALID_HANDLE_VALUE)
7168 goto failed;
7169
7170 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7171 GetCurrentProcessId(),
7172 curbuf->b_fnum);
7173 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7174 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7175 PIPE_UNLIMITED_INSTANCES,
7176 0, 0, 0, NULL);
7177 if (hPipeOut == INVALID_HANDLE_VALUE)
7178 goto failed;
7179
7180 ConnectNamedPipe(hPipeIn, NULL);
7181 ConnectNamedPipe(hPipeOut, NULL);
7182
7183 term->tl_job = job_alloc();
7184 if (term->tl_job == NULL)
7185 goto failed;
7186 ++term->tl_job->jv_refcount;
7187
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007188 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007189 term->tl_job->jv_status = JOB_FINISHED;
7190
7191 channel = add_channel();
7192 if (channel == NULL)
7193 goto failed;
7194 term->tl_job->jv_channel = channel;
7195 channel->ch_keep_open = TRUE;
7196 channel->ch_named_pipe = TRUE;
7197
7198 channel_set_pipes(channel,
7199 (sock_T)hPipeIn,
7200 (sock_T)hPipeOut,
7201 (sock_T)hPipeOut);
7202 channel_set_job(channel, term->tl_job, options);
7203 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7204 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7205
7206 return OK;
7207
7208failed:
7209 if (hPipeIn != NULL)
7210 CloseHandle(hPipeIn);
7211 if (hPipeOut != NULL)
7212 CloseHandle(hPipeOut);
7213 return FAIL;
7214}
7215
7216/*
7217 * Free the terminal emulator part of "term".
7218 */
7219 static void
7220term_free_vterm(term_T *term)
7221{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007222 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007223 if (term->tl_winpty != NULL)
7224 winpty_free(term->tl_winpty);
7225 term->tl_winpty = NULL;
7226 if (term->tl_winpty_config != NULL)
7227 winpty_config_free(term->tl_winpty_config);
7228 term->tl_winpty_config = NULL;
7229 if (term->tl_vterm != NULL)
7230 vterm_free(term->tl_vterm);
7231 term->tl_vterm = NULL;
7232}
7233
7234/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007235 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007236 */
7237 static void
7238term_report_winsize(term_T *term, int rows, int cols)
7239{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007240 if (term->tl_conpty)
7241 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007242 if (term->tl_winpty)
7243 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7244}
7245
7246 int
7247terminal_enabled(void)
7248{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007249 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007250}
7251
7252# else
7253
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007254///////////////////////////////////////
7255// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007256
7257/*
7258 * Create a new terminal of "rows" by "cols" cells.
7259 * Start job for "cmd".
7260 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007261 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007262 * Return OK or FAIL.
7263 */
7264 static int
7265term_and_job_init(
7266 term_T *term,
7267 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007268 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007269 jobopt_T *opt,
7270 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007271{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007272 term->tl_arg0_cmd = NULL;
7273
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007274 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7275 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007276
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007277#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7278 if (opt->jo_set2 & JO2_ANSI_COLORS)
7279 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7280 else
7281 init_vterm_ansi_colors(term->tl_vterm);
7282#endif
7283
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007284 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007285 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007286 if (term->tl_job != NULL)
7287 ++term->tl_job->jv_refcount;
7288
7289 return term->tl_job != NULL
7290 && term->tl_job->jv_channel != NULL
7291 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7292}
7293
7294 static int
7295create_pty_only(term_T *term, jobopt_T *opt)
7296{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007297 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7298 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007299
7300 term->tl_job = job_alloc();
7301 if (term->tl_job == NULL)
7302 return FAIL;
7303 ++term->tl_job->jv_refcount;
7304
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007305 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007306 term->tl_job->jv_status = JOB_FINISHED;
7307
7308 return mch_create_pty_channel(term->tl_job, opt);
7309}
7310
7311/*
7312 * Free the terminal emulator part of "term".
7313 */
7314 static void
7315term_free_vterm(term_T *term)
7316{
7317 if (term->tl_vterm != NULL)
7318 vterm_free(term->tl_vterm);
7319 term->tl_vterm = NULL;
7320}
7321
7322/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007323 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007324 */
7325 static void
7326term_report_winsize(term_T *term, int rows, int cols)
7327{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007328 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007329 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7330 {
7331 int fd = -1;
7332 int part;
7333
7334 for (part = PART_OUT; part < PART_COUNT; ++part)
7335 {
7336 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007337 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007338 break;
7339 }
7340 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7341 mch_signal_job(term->tl_job, (char_u *)"winch");
7342 }
7343}
7344
7345# endif
7346
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007347#endif // FEAT_TERMINAL