blob: bb3035bdfd15740031ae15f0a6ea7c8c6110d5c8 [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;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +0100670 p = vim_strsave_fnameescape(s, VSE_NONE);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100671 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;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001998 trigger_modechanged();
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001999 if (!normal_mode)
2000 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002001 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002002 if (term->tl_buffer == curbuf)
2003 maketitle();
2004}
2005
2006/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002007 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002008 * Move the vterm contents into the scrollback buffer and free the vterm.
2009 */
2010 static void
2011cleanup_vterm(term_T *term)
2012{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002013 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002014 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002015 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002016 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002017}
2018
2019/*
2020 * Switch from Terminal-Job mode to Terminal-Normal mode.
2021 * Suspends updating the terminal window.
2022 */
2023 static void
2024term_enter_normal_mode(void)
2025{
2026 term_T *term = curbuf->b_term;
2027
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002028 set_terminal_mode(term, TRUE);
2029
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002030 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002031 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002032
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002033 // Move the window cursor to the position of the cursor in the
2034 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002035 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2036 + term->tl_cursor_pos.row + 1;
2037 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002038 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2039 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002040 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002041
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002042 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002043 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2044}
2045
2046/*
2047 * Returns TRUE if the current window contains a terminal and we are in
2048 * Terminal-Normal mode.
2049 */
2050 int
2051term_in_normal_mode(void)
2052{
2053 term_T *term = curbuf->b_term;
2054
2055 return term != NULL && term->tl_normal_mode;
2056}
2057
2058/*
2059 * Switch from Terminal-Normal mode to Terminal-Job mode.
2060 * Restores updating the terminal window.
2061 */
2062 void
2063term_enter_job_mode()
2064{
2065 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002066
2067 set_terminal_mode(term, FALSE);
2068
2069 if (term->tl_channel_closed)
2070 cleanup_vterm(term);
2071 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002072#ifdef FEAT_PROP_POPUP
2073 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002074 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002075#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002076}
2077
2078/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002079 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002080 * Note: while waiting a terminal may be closed and freed if the channel is
2081 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002082 */
2083 static int
2084term_vgetc()
2085{
2086 int c;
2087 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002088 int modify_other_keys =
2089 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002090
2091 State = TERMINAL;
2092 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002093#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002094 ctrl_break_was_pressed = FALSE;
2095#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002096 if (modify_other_keys)
2097 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002098 c = vgetc();
2099 got_int = FALSE;
2100 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002101 if (modify_other_keys)
2102 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002103 return c;
2104}
2105
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002106static int mouse_was_outside = FALSE;
2107
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002108/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002109 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002110 * Return FAIL when the key needs to be handled in Normal mode.
2111 * Return OK when the key was dropped or sent to the terminal.
2112 */
2113 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002114send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002115{
2116 char msg[KEY_BUF_LEN];
2117 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002118 int dragging_outside = FALSE;
2119
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002120 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002121 switch (c)
2122 {
2123 case NUL:
2124 case K_ZERO:
2125 if (typed)
2126 stuffcharReadbuff(c);
2127 return FAIL;
2128
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002129 case K_TABLINE:
2130 stuffcharReadbuff(c);
2131 return FAIL;
2132
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002133 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002134 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002135 return FAIL;
2136
2137 case K_LEFTDRAG:
2138 case K_MIDDLEDRAG:
2139 case K_RIGHTDRAG:
2140 case K_X1DRAG:
2141 case K_X2DRAG:
2142 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002143 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002144 case K_LEFTMOUSE:
2145 case K_LEFTMOUSE_NM:
2146 case K_LEFTRELEASE:
2147 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002148 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002149 case K_MIDDLEMOUSE:
2150 case K_MIDDLERELEASE:
2151 case K_RIGHTMOUSE:
2152 case K_RIGHTRELEASE:
2153 case K_X1MOUSE:
2154 case K_X1RELEASE:
2155 case K_X2MOUSE:
2156 case K_X2RELEASE:
2157
2158 case K_MOUSEUP:
2159 case K_MOUSEDOWN:
2160 case K_MOUSELEFT:
2161 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002162 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002163 int row = mouse_row;
2164 int col = mouse_col;
2165
2166#ifdef FEAT_PROP_POPUP
2167 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002168 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002169 row -= popup_top_extra(curwin);
2170 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002171 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002172#endif
2173 if (row < W_WINROW(curwin)
2174 || row >= (W_WINROW(curwin) + curwin->w_height)
2175 || col < curwin->w_wincol
2176 || col >= W_ENDCOL(curwin)
2177 || dragging_outside)
2178 {
2179 // click or scroll outside the current window or on status
2180 // line or vertical separator
2181 if (typed)
2182 {
2183 stuffcharReadbuff(c);
2184 mouse_was_outside = TRUE;
2185 }
2186 return FAIL;
2187 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002188 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002189 break;
2190
2191 case K_COMMAND:
2192 return do_cmdline(NULL, getcmdkeycmd, NULL, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002193 }
2194 if (typed)
2195 mouse_was_outside = FALSE;
2196
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002197 // Convert the typed key to a sequence of bytes for the job.
2198 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002199 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002200 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002201 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2202 (char_u *)msg, (int)len, NULL);
2203
2204 return OK;
2205}
2206
2207 static void
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002208position_cursor(win_T *wp, VTermPos *pos)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002209{
2210 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2211 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002212#ifdef FEAT_PROP_POPUP
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002213 if (popup_is_popup(wp))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002214 {
Bram Moolenaarf5452692020-11-28 21:56:06 +01002215 wp->w_wrow += popup_top_extra(wp);
2216 wp->w_wcol += popup_left_extra(wp);
Bram Moolenaar6a076442020-11-15 20:32:58 +01002217 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002218 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01002219 else
2220 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002221#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002222 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2223}
2224
2225/*
2226 * Handle CTRL-W "": send register contents to the job.
2227 */
2228 static void
2229term_paste_register(int prev_c UNUSED)
2230{
2231 int c;
2232 list_T *l;
2233 listitem_T *item;
2234 long reglen = 0;
2235 int type;
2236
2237#ifdef FEAT_CMDL_INFO
2238 if (add_to_showcmd(prev_c))
2239 if (add_to_showcmd('"'))
2240 out_flush();
2241#endif
2242 c = term_vgetc();
2243#ifdef FEAT_CMDL_INFO
2244 clear_showcmd();
2245#endif
2246 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002247 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002248 return;
2249
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002250 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002251 if (c == '=' && get_expr_register() != '=')
2252 return;
2253 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002254 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002255 return;
2256
2257 l = (list_T *)get_reg_contents(c, GREG_LIST);
2258 if (l != NULL)
2259 {
2260 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002261 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002262 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002263 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002264#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002265 char_u *tmp = s;
2266
2267 if (!enc_utf8 && enc_codepage > 0)
2268 {
2269 WCHAR *ret = NULL;
2270 int length = 0;
2271
2272 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2273 (int)STRLEN(s), &ret, &length);
2274 if (ret != NULL)
2275 {
2276 WideCharToMultiByte_alloc(CP_UTF8, 0,
2277 ret, length, (char **)&s, &length, 0, 0);
2278 vim_free(ret);
2279 }
2280 }
2281#endif
2282 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2283 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002284#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002285 if (tmp != s)
2286 vim_free(s);
2287#endif
2288
2289 if (item->li_next != NULL || type == MLINE)
2290 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2291 (char_u *)"\r", 1, NULL);
2292 }
2293 list_free(l);
2294 }
2295}
2296
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002297/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002298 * Return TRUE when waiting for a character in the terminal, the cursor of the
2299 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002300 */
2301 int
2302terminal_is_active()
2303{
2304 return in_terminal_loop != NULL;
2305}
2306
Bram Moolenaar83d47902020-03-26 20:34:00 +01002307/*
2308 * Return the highight group name for the terminal; "Terminal" if not set.
2309 */
2310 static char_u *
2311term_get_highlight_name(term_T *term)
2312{
2313 if (term->tl_highlight_name == NULL)
2314 return (char_u *)"Terminal";
2315 return term->tl_highlight_name;
2316}
2317
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002318#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002319 cursorentry_T *
2320term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2321{
2322 term_T *term = in_terminal_loop;
2323 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002324 int id;
2325 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002326
Bram Moolenaara80faa82020-04-12 19:37:17 +02002327 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002328 entry.shape = entry.mshape =
2329 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2330 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2331 SHAPE_BLOCK;
2332 entry.percentage = 20;
2333 if (term->tl_cursor_blink)
2334 {
2335 entry.blinkwait = 700;
2336 entry.blinkon = 400;
2337 entry.blinkoff = 250;
2338 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002339
Bram Moolenaar83d47902020-03-26 20:34:00 +01002340 // The highlight group overrules the defaults.
2341 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002342 if (id != 0)
2343 {
2344 syn_id2colors(id, &term_fg, &term_bg);
2345 *fg = term_bg;
2346 }
2347 else
2348 *fg = gui.back_pixel;
2349
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002350 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002351 {
2352 if (id != 0)
2353 *bg = term_fg;
2354 else
2355 *bg = gui.norm_pixel;
2356 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002357 else
2358 *bg = color_name2handle(term->tl_cursor_color);
2359 entry.name = "n";
2360 entry.used_for = SHAPE_CURSOR;
2361
2362 return &entry;
2363}
2364#endif
2365
Bram Moolenaard317b382018-02-08 22:33:31 +01002366 static void
2367may_output_cursor_props(void)
2368{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002369 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002370 || last_set_cursor_shape != desired_cursor_shape
2371 || last_set_cursor_blink != desired_cursor_blink)
2372 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002373 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002374 last_set_cursor_shape = desired_cursor_shape;
2375 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002376 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002377 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002378 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002379 ui_cursor_shape_forced(TRUE);
2380 else
2381 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2382 }
2383}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002384
Bram Moolenaard317b382018-02-08 22:33:31 +01002385/*
2386 * Set the cursor color and shape, if not last set to these.
2387 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002388 static void
2389may_set_cursor_props(term_T *term)
2390{
2391#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002392 // For the GUI the cursor properties are obtained with
2393 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002394 if (gui.in_use)
2395 return;
2396#endif
2397 if (in_terminal_loop == term)
2398 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002399 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002400 desired_cursor_shape = term->tl_cursor_shape;
2401 desired_cursor_blink = term->tl_cursor_blink;
2402 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002403 }
2404}
2405
Bram Moolenaard317b382018-02-08 22:33:31 +01002406/*
2407 * Reset the desired cursor properties and restore them when needed.
2408 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002409 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002410prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002411{
2412#ifdef FEAT_GUI
2413 if (gui.in_use)
2414 return;
2415#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002416 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002417 desired_cursor_shape = -1;
2418 desired_cursor_blink = -1;
2419 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002420}
2421
2422/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002423 * Returns TRUE if the current window contains a terminal and we are sending
2424 * keys to the job.
2425 * If "check_job_status" is TRUE update the job status.
2426 */
2427 static int
2428term_use_loop_check(int check_job_status)
2429{
2430 term_T *term = curbuf->b_term;
2431
2432 return term != NULL
2433 && !term->tl_normal_mode
2434 && term->tl_vterm != NULL
2435 && term_job_running_check(term, check_job_status);
2436}
2437
2438/*
2439 * Returns TRUE if the current window contains a terminal and we are sending
2440 * keys to the job.
2441 */
2442 int
2443term_use_loop(void)
2444{
2445 return term_use_loop_check(FALSE);
2446}
2447
2448/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002449 * Called when entering a window with the mouse. If this is a terminal window
2450 * we may want to change state.
2451 */
2452 void
2453term_win_entered()
2454{
2455 term_T *term = curbuf->b_term;
2456
2457 if (term != NULL)
2458 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002459 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002460 {
2461 reset_VIsual_and_resel();
2462 if (State & INSERT)
2463 stop_insert_mode = TRUE;
2464 }
2465 mouse_was_outside = FALSE;
2466 enter_mouse_col = mouse_col;
2467 enter_mouse_row = mouse_row;
2468 }
2469}
2470
2471/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002472 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2473 * Return the Ctrl-key value in that case.
2474 */
2475 static int
2476raw_c_to_ctrl(int c)
2477{
2478 if ((mod_mask & MOD_MASK_CTRL)
2479 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2480 return c & 0x1f;
2481 return c;
2482}
2483
2484/*
2485 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2486 * May set "mod_mask".
2487 */
2488 static int
2489ctrl_to_raw_c(int c)
2490{
2491 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2492 {
2493 mod_mask |= MOD_MASK_CTRL;
2494 return c + '@';
2495 }
2496 return c;
2497}
2498
2499/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002500 * Wait for input and send it to the job.
2501 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2502 * when there is no more typahead.
2503 * Return when the start of a CTRL-W command is typed or anything else that
2504 * should be handled as a Normal mode command.
2505 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2506 * the terminal was closed.
2507 */
2508 int
2509terminal_loop(int blocking)
2510{
2511 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002512 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002513 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002514 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002515#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002516 int tty_fd = curbuf->b_term->tl_job->jv_channel
2517 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002518#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002519 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002520
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002521 // Remember the terminal we are sending keys to. However, the terminal
2522 // might be closed while waiting for a character, e.g. typing "exit" in a
2523 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2524 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002525 in_terminal_loop = curbuf->b_term;
2526
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002527 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002528 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002529 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002530 if (termwinkey == Ctrl_W)
2531 termwinkey = 0;
2532 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002533 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002534 may_set_cursor_props(curbuf->b_term);
2535
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002536 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002537 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002538#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002539 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002540#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002541 // TODO: skip screen update when handling a sequence of keys.
2542 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002543 while (must_redraw != 0)
2544 if (update_screen(0) == FAIL)
2545 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002546 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002547 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002548 break;
2549
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002550 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002551 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002552
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002553 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002554 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002555 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002556 // Job finished while waiting for a character. Push back the
2557 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002558 if (raw_c != K_IGNORE)
2559 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002560 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002561 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002562 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002563 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002564 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002565
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002566#ifdef UNIX
2567 /*
2568 * The shell or another program may change the tty settings. Getting
2569 * them for every typed character is a bit of overhead, but it's needed
2570 * for the first character typed, e.g. when Vim starts in a shell.
2571 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002572 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002573 {
2574 ttyinfo_T info;
2575
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002576 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002577 if (get_tty_info(tty_fd, &info) == OK)
2578 term_backspace_char = info.backspace;
2579 }
2580#endif
2581
Bram Moolenaar4f974752019-02-17 17:44:42 +01002582#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002583 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2584 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002585 if (ctrl_break_was_pressed)
2586 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2587#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002588 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2589 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002590 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002591#ifdef FEAT_GUI
2592 && !curbuf->b_term->tl_system
2593#endif
2594 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002595 {
2596 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002597 int prev_raw_c = raw_c;
2598 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002599
2600#ifdef FEAT_CMDL_INFO
2601 if (add_to_showcmd(c))
2602 out_flush();
2603#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002604 raw_c = term_vgetc();
2605 c = raw_c_to_ctrl(raw_c);
2606
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002607#ifdef FEAT_CMDL_INFO
2608 clear_showcmd();
2609#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002610 if (!term_use_loop_check(TRUE)
2611 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002612 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002613 break;
2614
2615 if (prev_c == Ctrl_BSL)
2616 {
2617 if (c == Ctrl_N)
2618 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002619 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002620 term_enter_normal_mode();
2621 ret = FAIL;
2622 goto theend;
2623 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002624 // Send both keys to the terminal, first one here, second one
2625 // below.
2626 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2627 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002628 }
2629 else if (c == Ctrl_C)
2630 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002631 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002632 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2633 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002634 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002635 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002636 // "CTRL-W .": send CTRL-W to the job
2637 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002638 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002639 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002640 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002641 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002642 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002643 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002644 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002645 else if (c == 'N')
2646 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002647 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002648 term_enter_normal_mode();
2649 ret = FAIL;
2650 goto theend;
2651 }
2652 else if (c == '"')
2653 {
2654 term_paste_register(prev_c);
2655 continue;
2656 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002657 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002658 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002659 // space for CTRL-W, modifier, multi-byte char and NUL
2660 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002661
2662 // Put the command into the typeahead buffer, when using the
2663 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2664 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002665 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002666 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002667 ret = OK;
2668 goto theend;
2669 }
2670 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002671# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002672 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002673 {
2674 WCHAR wc;
2675 char_u mb[3];
2676
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002677 mb[0] = (unsigned)raw_c >> 8;
2678 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002679 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002680 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002681 }
2682# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002683 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002684 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002685 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002686 // We are sure to come back here, don't reset the cursor color
2687 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002688 restore_cursor = FALSE;
2689
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002690 ret = OK;
2691 goto theend;
2692 }
2693 }
2694 ret = FAIL;
2695
2696theend:
2697 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002698 if (restore_cursor)
2699 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002700
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002701 // Move a snapshot of the screen contents to the buffer, so that completion
2702 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002703 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2704 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002705
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002706 return ret;
2707}
2708
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002709 static void
2710may_toggle_cursor(term_T *term)
2711{
2712 if (in_terminal_loop == term)
2713 {
2714 if (term->tl_cursor_visible)
2715 cursor_on();
2716 else
2717 cursor_off();
2718 }
2719}
2720
2721/*
2722 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002723 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002724 */
2725 static int
2726color2index(VTermColor *color, int fg, int *boldp)
2727{
2728 int red = color->red;
2729 int blue = color->blue;
2730 int green = color->green;
2731
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002732 if (VTERM_COLOR_IS_DEFAULT_FG(color)
2733 || VTERM_COLOR_IS_DEFAULT_BG(color))
2734 return 0;
2735 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002736 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002737 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002738 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002739 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002740 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002741 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2742 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2743 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002744 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002745 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2746 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2747 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2748 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2749 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2750 case 10: return lookup_color(20, fg, boldp) + 1; // red
2751 case 11: return lookup_color(16, fg, boldp) + 1; // green
2752 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2753 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2754 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2755 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2756 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002757 }
2758 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002759
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002760 if (t_colors >= 256)
2761 {
2762 if (red == blue && red == green)
2763 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002764 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002765 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002766 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2767 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2768 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002769 int i;
2770
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002771 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002772 return 17; // 00/00/00
2773 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002774 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002775 for (i = 0; i < 23; ++i)
2776 if (red < cutoff[i])
2777 return i + 233;
2778 return 256;
2779 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002780 {
2781 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2782 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002783
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002784 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002785 for (ri = 0; ri < 5; ++ri)
2786 if (red < cutoff[ri])
2787 break;
2788 for (gi = 0; gi < 5; ++gi)
2789 if (green < cutoff[gi])
2790 break;
2791 for (bi = 0; bi < 5; ++bi)
2792 if (blue < cutoff[bi])
2793 break;
2794 return 17 + ri * 36 + gi * 6 + bi;
2795 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002796 }
2797 return 0;
2798}
2799
2800/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002801 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002802 */
2803 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002804vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002805{
2806 int attr = 0;
2807
2808 if (cellattrs.bold)
2809 attr |= HL_BOLD;
2810 if (cellattrs.underline)
2811 attr |= HL_UNDERLINE;
2812 if (cellattrs.italic)
2813 attr |= HL_ITALIC;
2814 if (cellattrs.strike)
2815 attr |= HL_STRIKETHROUGH;
2816 if (cellattrs.reverse)
2817 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002818 return attr;
2819}
2820
2821/*
2822 * Store Vterm attributes in "cell" from highlight flags.
2823 */
2824 static void
2825hl2vtermAttr(int attr, cellattr_T *cell)
2826{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002827 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002828 if (attr & HL_BOLD)
2829 cell->attrs.bold = 1;
2830 if (attr & HL_UNDERLINE)
2831 cell->attrs.underline = 1;
2832 if (attr & HL_ITALIC)
2833 cell->attrs.italic = 1;
2834 if (attr & HL_STRIKETHROUGH)
2835 cell->attrs.strike = 1;
2836 if (attr & HL_INVERSE)
2837 cell->attrs.reverse = 1;
2838}
2839
2840/*
2841 * Convert the attributes of a vterm cell into an attribute index.
2842 */
2843 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002844cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002845 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002846 win_T *wp,
2847 VTermScreenCellAttrs cellattrs,
2848 VTermColor cellfg,
2849 VTermColor cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002850{
2851 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002852
2853#ifdef FEAT_GUI
2854 if (gui.in_use)
2855 {
2856 guicolor_T fg, bg;
2857
2858 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2859 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2860 return get_gui_attr_idx(attr, fg, bg);
2861 }
2862 else
2863#endif
2864#ifdef FEAT_TERMGUICOLORS
2865 if (p_tgc)
2866 {
Milly7b5f45b2021-10-15 22:25:43 +01002867 guicolor_T fg = INVALCOLOR;
2868 guicolor_T bg = INVALCOLOR;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002869
Milly7b5f45b2021-10-15 22:25:43 +01002870 // Use the 'wincolor' or "Terminal" highlighting for the default
2871 // colors.
2872 if (VTERM_COLOR_IS_DEFAULT_FG(&cellfg)
2873 || VTERM_COLOR_IS_DEFAULT_BG(&cellbg))
2874 {
2875 int id = 0;
2876
2877 if (wp != NULL && *wp->w_p_wcr != NUL)
2878 id = syn_name2id(wp->w_p_wcr);
2879 if (id == 0)
2880 id = syn_name2id(term_get_highlight_name(term));
2881 if (id > 0)
2882 syn_id2colors(id, &fg, &bg);
2883 if (!VTERM_COLOR_IS_DEFAULT_FG(&cellfg))
2884 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green,
2885 cellfg.blue);
2886 if (!VTERM_COLOR_IS_DEFAULT_BG(&cellbg))
2887 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green,
2888 cellbg.blue);
2889 }
2890 else
2891 {
2892 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2893 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2894 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002895
2896 return get_tgc_attr_idx(attr, fg, bg);
2897 }
2898 else
2899#endif
2900 {
2901 int bold = MAYBE;
2902 int fg = color2index(&cellfg, TRUE, &bold);
2903 int bg = color2index(&cellbg, FALSE, &bold);
2904
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002905 // Use the 'wincolor' or "Terminal" highlighting for the default
2906 // colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002907 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002908 {
Milly7b5f45b2021-10-15 22:25:43 +01002909 int cterm_fg = -1;
2910 int cterm_bg = -1;
2911 int id = 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002912
2913 if (wp != NULL && *wp->w_p_wcr != NUL)
Milly7b5f45b2021-10-15 22:25:43 +01002914 id = syn_name2id(wp->w_p_wcr);
2915 if (id == 0)
2916 id = syn_name2id(term_get_highlight_name(term));
2917 if (id > 0)
2918 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
2919 if (fg == 0 && cterm_fg >= 0)
2920 fg = cterm_fg + 1;
2921 if (bg == 0 && cterm_bg >= 0)
2922 bg = cterm_bg + 1;
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002923 }
2924
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002925 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002926 if (bold == TRUE)
2927 attr |= HL_BOLD;
2928 return get_cterm_attr_idx(attr, fg, bg);
2929 }
2930 return 0;
2931}
2932
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002933 static void
2934set_dirty_snapshot(term_T *term)
2935{
2936 term->tl_dirty_snapshot = TRUE;
2937#ifdef FEAT_TIMERS
2938 if (!term->tl_normal_mode)
2939 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002940 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002941 profile_setlimit(100L, &term->tl_timer_due);
2942 term->tl_timer_set = TRUE;
2943 }
2944#endif
2945}
2946
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002947 static int
2948handle_damage(VTermRect rect, void *user)
2949{
2950 term_T *term = (term_T *)user;
2951
2952 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2953 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002954 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002955 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002956 return 1;
2957}
2958
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002959 static void
2960term_scroll_up(term_T *term, int start_row, int count)
2961{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002962 win_T *wp = NULL;
2963 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002964 VTermColor fg, bg;
2965 VTermScreenCellAttrs attr;
2966 int clear_attr;
2967
Bram Moolenaara80faa82020-04-12 19:37:17 +02002968 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002969
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002970 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002971 {
2972 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002973 {
2974 // Set the color to clear lines with.
2975 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2976 &fg, &bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002977 clear_attr = cell2attr(term, wp, attr, fg, bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002978 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002979 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002980 }
2981}
2982
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002983 static int
2984handle_moverect(VTermRect dest, VTermRect src, void *user)
2985{
2986 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002987 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002988
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002989 // Scrolling up is done much more efficiently by deleting lines instead of
2990 // redrawing the text. But avoid doing this multiple times, postpone until
2991 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002992 if (dest.start_col == src.start_col
2993 && dest.end_col == src.end_col
2994 && dest.start_row < src.start_row)
2995 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002996 if (dest.start_row == 0)
2997 term->tl_postponed_scroll += count;
2998 else
2999 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003000 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003001
3002 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3003 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003004 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003005
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003006 // Note sure if the scrolling will work correctly, let's do a complete
3007 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003008 redraw_buf_later(term->tl_buffer, NOT_VALID);
3009 return 1;
3010}
3011
3012 static int
3013handle_movecursor(
3014 VTermPos pos,
3015 VTermPos oldpos UNUSED,
3016 int visible,
3017 void *user)
3018{
3019 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003020 win_T *wp = NULL;
3021 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003022
3023 term->tl_cursor_pos = pos;
3024 term->tl_cursor_visible = visible;
3025
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003026 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003027 {
3028 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003029 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003030 }
3031 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003032 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003033
3034 return 1;
3035}
3036
3037 static int
3038handle_settermprop(
3039 VTermProp prop,
3040 VTermValue *value,
3041 void *user)
3042{
3043 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003044 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003045
3046 switch (prop)
3047 {
3048 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003049 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003050 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003051 if (strval == NULL)
3052 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003053 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003054 // a blank title isn't useful, make it empty, so that "running" is
3055 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003056 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003057 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003058 // Same as blank
3059 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003060 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003061 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3062 term->tl_title = NULL;
3063 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003064 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003065 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003066#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003067 else if (!enc_utf8 && enc_codepage > 0)
3068 {
3069 WCHAR *ret = NULL;
3070 int length = 0;
3071
3072 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003073 (char*)value->string.str,
3074 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003075 if (ret != NULL)
3076 {
3077 WideCharToMultiByte_alloc(enc_codepage, 0,
3078 ret, length, (char**)&term->tl_title,
3079 &length, 0, 0);
3080 vim_free(ret);
3081 }
3082 }
3083#endif
3084 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003085 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003086 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003087 strval = NULL;
3088 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003089 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003090 if (term == curbuf->b_term)
3091 maketitle();
3092 break;
3093
3094 case VTERM_PROP_CURSORVISIBLE:
3095 term->tl_cursor_visible = value->boolean;
3096 may_toggle_cursor(term);
3097 out_flush();
3098 break;
3099
3100 case VTERM_PROP_CURSORBLINK:
3101 term->tl_cursor_blink = value->boolean;
3102 may_set_cursor_props(term);
3103 break;
3104
3105 case VTERM_PROP_CURSORSHAPE:
3106 term->tl_cursor_shape = value->number;
3107 may_set_cursor_props(term);
3108 break;
3109
3110 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003111 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003112 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003113 if (strval == NULL)
3114 break;
3115 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003116 may_set_cursor_props(term);
3117 break;
3118
3119 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003120 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003121 term->tl_using_altscreen = value->boolean;
3122 break;
3123
3124 default:
3125 break;
3126 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003127 vim_free(strval);
3128
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003129 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003130 return 1;
3131}
3132
3133/*
3134 * The job running in the terminal resized the terminal.
3135 */
3136 static int
3137handle_resize(int rows, int cols, void *user)
3138{
3139 term_T *term = (term_T *)user;
3140 win_T *wp;
3141
3142 term->tl_rows = rows;
3143 term->tl_cols = cols;
3144 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003145 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003146 term->tl_vterm_size_changed = FALSE;
3147 else
3148 {
3149 FOR_ALL_WINDOWS(wp)
3150 {
3151 if (wp->w_buffer == term->tl_buffer)
3152 {
3153 win_setheight_win(rows, wp);
3154 win_setwidth_win(cols, wp);
3155 }
3156 }
3157 redraw_buf_later(term->tl_buffer, NOT_VALID);
3158 }
3159 return 1;
3160}
3161
3162/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003163 * If the number of lines that are stored goes over 'termscrollback' then
3164 * delete the first 10%.
3165 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3166 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003167 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003168 static void
3169limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003170{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003171 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003172 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003173 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003174 int i;
3175
3176 curbuf = term->tl_buffer;
3177 for (i = 0; i < todo; ++i)
3178 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003179 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3180 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003181 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003182 }
3183 curbuf = curwin->w_buffer;
3184
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003185 gap->ga_len -= todo;
3186 mch_memmove(gap->ga_data,
3187 (sb_line_T *)gap->ga_data + todo,
3188 sizeof(sb_line_T) * gap->ga_len);
3189 if (update_buffer)
3190 term->tl_scrollback_scrolled -= todo;
3191 }
3192}
3193
3194/*
3195 * Handle a line that is pushed off the top of the screen.
3196 */
3197 static int
3198handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3199{
3200 term_T *term = (term_T *)user;
3201 garray_T *gap;
3202 int update_buffer;
3203
3204 if (term->tl_normal_mode)
3205 {
3206 // In Terminal-Normal mode the user interacts with the buffer, thus we
3207 // must not change it. Postpone adding the scrollback lines.
3208 gap = &term->tl_scrollback_postponed;
3209 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003210 }
3211 else
3212 {
3213 // First remove the lines that were appended before, the pushed line
3214 // goes above it.
3215 cleanup_scrollback(term);
3216 gap = &term->tl_scrollback;
3217 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003218 }
3219
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003220 limit_scrollback(term, gap, update_buffer);
3221
3222 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003223 {
3224 cellattr_T *p = NULL;
3225 int len = 0;
3226 int i;
3227 int c;
3228 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003229 int text_len;
3230 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003231 sb_line_T *line;
3232 garray_T ga;
3233 cellattr_T fill_attr = term->tl_default_color;
3234
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003235 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003236 for (i = 0; i < cols; ++i)
3237 if (cells[i].chars[0] != 0)
3238 len = i + 1;
3239 else
3240 cell2cellattr(&cells[i], &fill_attr);
3241
3242 ga_init2(&ga, 1, 100);
3243 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003244 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003245 if (p != NULL)
3246 {
3247 for (col = 0; col < len; col += cells[col].width)
3248 {
3249 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3250 {
3251 ga.ga_len = 0;
3252 break;
3253 }
3254 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3255 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3256 (char_u *)ga.ga_data + ga.ga_len);
3257 cell2cellattr(&cells[col], &p[col]);
3258 }
3259 }
3260 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003261 {
3262 if (update_buffer)
3263 text = (char_u *)"";
3264 else
3265 text = vim_strsave((char_u *)"");
3266 text_len = 0;
3267 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003268 else
3269 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003270 text = ga.ga_data;
3271 text_len = ga.ga_len;
3272 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003273 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003274 if (update_buffer)
3275 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003276
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003277 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003278 line->sb_cols = len;
3279 line->sb_cells = p;
3280 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003281 if (update_buffer)
3282 {
3283 line->sb_text = NULL;
3284 ++term->tl_scrollback_scrolled;
3285 ga_clear(&ga); // free the text
3286 }
3287 else
3288 {
3289 line->sb_text = text;
3290 ga_init(&ga); // text is kept in tl_scrollback_postponed
3291 }
3292 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003293 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003294 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003295}
3296
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003297/*
3298 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3299 * received and stored in tl_scrollback_postponed.
3300 */
3301 static void
3302handle_postponed_scrollback(term_T *term)
3303{
3304 int i;
3305
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003306 if (term->tl_scrollback_postponed.ga_len == 0)
3307 return;
3308 ch_log(NULL, "Moving postponed scrollback to scrollback");
3309
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003310 // First remove the lines that were appended before, the pushed lines go
3311 // above it.
3312 cleanup_scrollback(term);
3313
3314 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3315 {
3316 char_u *text;
3317 sb_line_T *pp_line;
3318 sb_line_T *line;
3319
3320 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3321 break;
3322 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3323
3324 text = pp_line->sb_text;
3325 if (text == NULL)
3326 text = (char_u *)"";
3327 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3328 vim_free(pp_line->sb_text);
3329
3330 line = (sb_line_T *)term->tl_scrollback.ga_data
3331 + term->tl_scrollback.ga_len;
3332 line->sb_cols = pp_line->sb_cols;
3333 line->sb_cells = pp_line->sb_cells;
3334 line->sb_fill_attr = pp_line->sb_fill_attr;
3335 line->sb_text = NULL;
3336 ++term->tl_scrollback_scrolled;
3337 ++term->tl_scrollback.ga_len;
3338 }
3339
3340 ga_clear(&term->tl_scrollback_postponed);
3341 limit_scrollback(term, &term->tl_scrollback, TRUE);
3342}
3343
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003344static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003345 handle_damage, // damage
3346 handle_moverect, // moverect
3347 handle_movecursor, // movecursor
3348 handle_settermprop, // settermprop
3349 NULL, // bell
3350 handle_resize, // resize
3351 handle_pushline, // sb_pushline
3352 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003353};
3354
3355/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003356 * Do the work after the channel of a terminal was closed.
3357 * Must be called only when updating_screen is FALSE.
3358 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3359 */
3360 static int
3361term_after_channel_closed(term_T *term)
3362{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003363 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003364 if (!term->tl_normal_mode)
3365 {
3366 int fnum = term->tl_buffer->b_fnum;
3367
3368 cleanup_vterm(term);
3369
3370 if (term->tl_finish == TL_FINISH_CLOSE)
3371 {
3372 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003373 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003374#ifdef FEAT_PROP_POPUP
3375 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003376
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003377 // If this was a terminal in a popup window, go back to the
3378 // previous window.
3379 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3380 {
3381 pwin = curwin;
3382 if (win_valid(prevwin))
3383 win_enter(prevwin, FALSE);
3384 }
3385 else
3386#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003387 // If this is the last normal window: exit Vim.
3388 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3389 {
3390 exarg_T ea;
3391
Bram Moolenaara80faa82020-04-12 19:37:17 +02003392 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003393 ex_quit(&ea);
3394 return TRUE;
3395 }
3396
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003397 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003398 ch_log(NULL, "terminal job finished, closing window");
3399 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003400 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003401 if (curwin == aucmd_win)
3402 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003403 if (do_set_w_closing)
3404 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003405 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003406 if (do_set_w_closing)
3407 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003408 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003409#ifdef FEAT_PROP_POPUP
3410 if (pwin != NULL)
3411 popup_close_with_retval(pwin, 0);
3412#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003413 return TRUE;
3414 }
3415 if (term->tl_finish == TL_FINISH_OPEN
3416 && term->tl_buffer->b_nwindows == 0)
3417 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003418 char *cmd = term->tl_opencmd == NULL
3419 ? "botright sbuf %d"
3420 : (char *)term->tl_opencmd;
3421 size_t len = strlen(cmd) + 50;
3422 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003423
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003424 if (buf != NULL)
3425 {
3426 ch_log(NULL, "terminal job finished, opening window");
3427 vim_snprintf(buf, len, cmd, fnum);
3428 do_cmdline_cmd((char_u *)buf);
3429 vim_free(buf);
3430 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003431 }
3432 else
3433 ch_log(NULL, "terminal job finished");
3434 }
3435
3436 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3437 return FALSE;
3438}
3439
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003440#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3441/*
3442 * If the current window is a terminal in a popup window and the job has
3443 * finished, close the popup window and to back to the previous window.
3444 * Otherwise return FAIL.
3445 */
3446 int
3447may_close_term_popup(void)
3448{
3449 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3450 && !term_job_running(curbuf->b_term))
3451 {
3452 win_T *pwin = curwin;
3453
3454 if (win_valid(prevwin))
3455 win_enter(prevwin, FALSE);
3456 popup_close_with_retval(pwin, 0);
3457 return OK;
3458 }
3459 return FAIL;
3460}
3461#endif
3462
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003463/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003464 * Called when a channel has been closed.
3465 * If this was a channel for a terminal window then finish it up.
3466 */
3467 void
3468term_channel_closed(channel_T *ch)
3469{
3470 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003471 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003472 int did_one = FALSE;
3473
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003474 for (term = first_term; term != NULL; term = next_term)
3475 {
3476 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003477 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003478 {
3479 term->tl_channel_closed = TRUE;
3480 did_one = TRUE;
3481
Bram Moolenaard23a8232018-02-10 18:45:26 +01003482 VIM_CLEAR(term->tl_title);
3483 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003484#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003485 if (term->tl_out_fd != NULL)
3486 {
3487 fclose(term->tl_out_fd);
3488 term->tl_out_fd = NULL;
3489 }
3490#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003491
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003492 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003493 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003494 // Cannot open or close windows now. Can happen when
3495 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003496 term->tl_channel_recently_closed = TRUE;
3497 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003498 }
3499
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003500 if (term_after_channel_closed(term))
3501 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003502 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003503 }
3504
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003505 if (did_one)
3506 {
3507 redraw_statuslines();
3508
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003509 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003510 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003511 typebuf_was_filled = TRUE;
3512
3513 term = curbuf->b_term;
3514 if (term != NULL)
3515 {
3516 if (term->tl_job == ch->ch_job)
3517 maketitle();
3518 update_cursor(term, term->tl_cursor_visible);
3519 }
3520 }
3521}
3522
3523/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003524 * To be called after resetting updating_screen: handle any terminal where the
3525 * channel was closed.
3526 */
3527 void
3528term_check_channel_closed_recently()
3529{
3530 term_T *term;
3531 term_T *next_term;
3532
3533 for (term = first_term; term != NULL; term = next_term)
3534 {
3535 next_term = term->tl_next;
3536 if (term->tl_channel_recently_closed)
3537 {
3538 term->tl_channel_recently_closed = FALSE;
3539 if (term_after_channel_closed(term))
3540 // start over, the list may have changed
3541 next_term = first_term;
3542 }
3543 }
3544}
3545
3546/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003547 * Fill one screen line from a line of the terminal.
3548 * Advances "pos" to past the last column.
3549 */
3550 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003551term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003552 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003553 win_T *wp,
3554 VTermScreen *screen,
3555 VTermPos *pos,
3556 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003557{
3558 int off = screen_get_current_line_off();
3559
3560 for (pos->col = 0; pos->col < max_col; )
3561 {
3562 VTermScreenCell cell;
3563 int c;
3564
3565 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003566 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003567
3568 c = cell.chars[0];
3569 if (c == NUL)
3570 {
3571 ScreenLines[off] = ' ';
3572 if (enc_utf8)
3573 ScreenLinesUC[off] = NUL;
3574 }
3575 else
3576 {
3577 if (enc_utf8)
3578 {
3579 int i;
3580
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003581 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003582 for (i = 0; i < Screen_mco
3583 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3584 {
3585 ScreenLinesC[i][off] = cell.chars[i + 1];
3586 if (cell.chars[i + 1] == 0)
3587 break;
3588 }
3589 if (c >= 0x80 || (Screen_mco > 0
3590 && ScreenLinesC[0][off] != 0))
3591 {
3592 ScreenLines[off] = ' ';
3593 ScreenLinesUC[off] = c;
3594 }
3595 else
3596 {
3597 ScreenLines[off] = c;
3598 ScreenLinesUC[off] = NUL;
3599 }
3600 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003601#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003602 else if (has_mbyte && c >= 0x80)
3603 {
3604 char_u mb[MB_MAXBYTES+1];
3605 WCHAR wc = c;
3606
3607 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3608 (char*)mb, 2, 0, 0) > 1)
3609 {
3610 ScreenLines[off] = mb[0];
3611 ScreenLines[off + 1] = mb[1];
3612 cell.width = mb_ptr2cells(mb);
3613 }
3614 else
3615 ScreenLines[off] = c;
3616 }
3617#endif
3618 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003619 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003620 ScreenLines[off] = c;
3621 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003622 ScreenAttrs[off] = cell2attr(term, wp, cell.attrs, cell.fg, cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003623
3624 ++pos->col;
3625 ++off;
3626 if (cell.width == 2)
3627 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003628 // don't set the second byte to NUL for a DBCS encoding, it
3629 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003630 if (enc_utf8)
3631 {
3632 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003633 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003634 }
3635 else if (!has_mbyte)
3636 {
3637 // Can't show a double-width character with a single-byte
3638 // 'encoding', just use a space.
3639 ScreenLines[off] = ' ';
3640 ScreenAttrs[off] = ScreenAttrs[off - 1];
3641 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003642
3643 ++pos->col;
3644 ++off;
3645 }
3646 }
3647}
3648
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003649#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003650 static void
3651update_system_term(term_T *term)
3652{
3653 VTermPos pos;
3654 VTermScreen *screen;
3655
3656 if (term->tl_vterm == NULL)
3657 return;
3658 screen = vterm_obtain_screen(term->tl_vterm);
3659
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003660 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003661 while (term->tl_toprow > 0
3662 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3663 {
3664 int save_p_more = p_more;
3665
3666 p_more = FALSE;
3667 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003668 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003669 p_more = save_p_more;
3670 --term->tl_toprow;
3671 }
3672
3673 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3674 && pos.row < Rows; ++pos.row)
3675 {
3676 if (pos.row < term->tl_rows)
3677 {
3678 int max_col = MIN(Columns, term->tl_cols);
3679
Bram Moolenaar83d47902020-03-26 20:34:00 +01003680 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003681 }
3682 else
3683 pos.col = 0;
3684
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003685 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003686 }
3687
3688 term->tl_dirty_row_start = MAX_ROW;
3689 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003690}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003691#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003692
3693/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003694 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3695 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003696 * Terminal-Normal mode.
3697 */
3698 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003699term_do_update_window(win_T *wp)
3700{
3701 term_T *term = wp->w_buffer->b_term;
3702
3703 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3704}
3705
3706/*
3707 * Called to update a window that contains an active terminal.
3708 */
3709 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003710term_update_window(win_T *wp)
3711{
3712 term_T *term = wp->w_buffer->b_term;
3713 VTerm *vterm;
3714 VTermScreen *screen;
3715 VTermState *state;
3716 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003717 int rows, cols;
3718 int newrows, newcols;
3719 int minsize;
3720 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003721
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003722 vterm = term->tl_vterm;
3723 screen = vterm_obtain_screen(vterm);
3724 state = vterm_obtain_state(vterm);
3725
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003726 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3727 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003728 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003729 {
3730 term->tl_dirty_row_start = 0;
3731 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003732
3733 if (term->tl_postponed_scroll > 0
3734 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003735 // Scrolling is usually faster than redrawing, when there are only
3736 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003737 term_scroll_up(term, 0, term->tl_postponed_scroll);
3738 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003739 }
3740
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003741 /*
3742 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003743 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003744 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003745 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003746
Bram Moolenaar498c2562018-04-15 23:45:15 +02003747 newrows = 99999;
3748 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003749 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003750 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003751 // Always use curwin, it may be a popup window.
3752 win_T *wwp = twp == NULL ? curwin : twp;
3753
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003754 // When more than one window shows the same terminal, use the
3755 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003756 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003757 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003758 newrows = MIN(newrows, wwp->w_height);
3759 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003760 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003761 if (twp == NULL)
3762 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003763 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003764 if (newrows == 99999 || newcols == 99999)
3765 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003766 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3767 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3768
Bram Moolenaareba13e42021-02-23 17:47:23 +01003769 // If no cell is visible there is no point in resizing. Also, vterm can't
3770 // handle a zero height.
3771 if (newrows == 0 || newcols == 0)
3772 return;
3773
Bram Moolenaar498c2562018-04-15 23:45:15 +02003774 if (term->tl_rows != newrows || term->tl_cols != newcols)
3775 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003776 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003777 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003778 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003779 newrows);
3780 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003781
3782 // Updating the terminal size will cause the snapshot to be cleared.
3783 // When not in terminal_loop() we need to restore it.
3784 if (term != in_terminal_loop)
3785 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003786 }
3787
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003788 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003789 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003790 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003791
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003792 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3793 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003794 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003795 if (pos.row < term->tl_rows)
3796 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003797 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003798
Bram Moolenaar83d47902020-03-26 20:34:00 +01003799 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003800 }
3801 else
3802 pos.col = 0;
3803
Bram Moolenaarf118d482018-03-13 13:14:00 +01003804 screen_line(wp->w_winrow + pos.row
3805#ifdef FEAT_MENU
3806 + winbar_height(wp)
3807#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003808 , wp->w_wincol, pos.col, wp->w_width,
3809#ifdef FEAT_PROP_POPUP
3810 popup_is_popup(wp) ? SLF_POPUP :
3811#endif
3812 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003813 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003814 term->tl_dirty_row_start = MAX_ROW;
3815 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003816}
3817
3818/*
3819 * Return TRUE if "wp" is a terminal window where the job has finished.
3820 */
3821 int
3822term_is_finished(buf_T *buf)
3823{
3824 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3825}
3826
3827/*
3828 * Return TRUE if "wp" is a terminal window where the job has finished or we
3829 * are in Terminal-Normal mode, thus we show the buffer contents.
3830 */
3831 int
3832term_show_buffer(buf_T *buf)
3833{
3834 term_T *term = buf->b_term;
3835
3836 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3837}
3838
3839/*
3840 * The current buffer is going to be changed. If there is terminal
3841 * highlighting remove it now.
3842 */
3843 void
3844term_change_in_curbuf(void)
3845{
3846 term_T *term = curbuf->b_term;
3847
3848 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3849 {
3850 free_scrollback(term);
3851 redraw_buf_later(term->tl_buffer, NOT_VALID);
3852
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003853 // The buffer is now like a normal buffer, it cannot be easily
3854 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003855 set_string_option_direct((char_u *)"buftype", -1,
3856 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3857 }
3858}
3859
3860/*
3861 * Get the screen attribute for a position in the buffer.
3862 * Use a negative "col" to get the filler background color.
3863 */
3864 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003865term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003866{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003867 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003868 term_T *term = buf->b_term;
3869 sb_line_T *line;
3870 cellattr_T *cellattr;
3871
3872 if (lnum > term->tl_scrollback.ga_len)
3873 cellattr = &term->tl_default_color;
3874 else
3875 {
3876 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3877 if (col < 0 || col >= line->sb_cols)
3878 cellattr = &line->sb_fill_attr;
3879 else
3880 cellattr = line->sb_cells + col;
3881 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003882 return cell2attr(term, wp, cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003883}
3884
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003885/*
3886 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003887 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003888 */
3889 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003890cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003891{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003892 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3893 if (rgb->index == 0)
3894 rgb->type = VTERM_COLOR_RGB;
3895 else
3896 {
3897 rgb->type = VTERM_COLOR_INDEXED;
3898 --rgb->index;
3899 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003900}
3901
3902/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003903 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003904 */
3905 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003906init_default_colors(term_T *term, win_T *wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003907{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003908 VTermColor *fg, *bg;
3909 int fgval, bgval;
3910 int id;
3911
Bram Moolenaara80faa82020-04-12 19:37:17 +02003912 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003913 term->tl_default_color.width = 1;
3914 fg = &term->tl_default_color.fg;
3915 bg = &term->tl_default_color.bg;
3916
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003917 // Vterm uses a default black background. Set it to white when
3918 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003919 if (*p_bg == 'l')
3920 {
3921 fgval = 0;
3922 bgval = 255;
3923 }
3924 else
3925 {
3926 fgval = 255;
3927 bgval = 0;
3928 }
3929 fg->red = fg->green = fg->blue = fgval;
3930 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003931 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3932 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003933
Bram Moolenaar83d47902020-03-26 20:34:00 +01003934 // The 'wincolor' or the highlight group overrules the defaults.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003935 if (wp != NULL && *wp->w_p_wcr != NUL)
3936 id = syn_name2id(wp->w_p_wcr);
3937 else
Bram Moolenaar83d47902020-03-26 20:34:00 +01003938 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003939
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003940 // Use the actual color for the GUI and when 'termguicolors' is set.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003941#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3942 if (0
3943# ifdef FEAT_GUI
3944 || gui.in_use
3945# endif
3946# ifdef FEAT_TERMGUICOLORS
3947 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003948# ifdef FEAT_VTP
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003949 // Finally get INVALCOLOR on this execution path
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003950 || (!p_tgc && t_colors >= 256)
3951# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003952# endif
3953 )
3954 {
3955 guicolor_T fg_rgb = INVALCOLOR;
3956 guicolor_T bg_rgb = INVALCOLOR;
3957
3958 if (id != 0)
3959 syn_id2colors(id, &fg_rgb, &bg_rgb);
3960
3961# ifdef FEAT_GUI
3962 if (gui.in_use)
3963 {
3964 if (fg_rgb == INVALCOLOR)
3965 fg_rgb = gui.norm_pixel;
3966 if (bg_rgb == INVALCOLOR)
3967 bg_rgb = gui.back_pixel;
3968 }
3969# ifdef FEAT_TERMGUICOLORS
3970 else
3971# endif
3972# endif
3973# ifdef FEAT_TERMGUICOLORS
3974 {
3975 if (fg_rgb == INVALCOLOR)
3976 fg_rgb = cterm_normal_fg_gui_color;
3977 if (bg_rgb == INVALCOLOR)
3978 bg_rgb = cterm_normal_bg_gui_color;
3979 }
3980# endif
3981 if (fg_rgb != INVALCOLOR)
3982 {
3983 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3984
3985 fg->red = (unsigned)(rgb >> 16);
3986 fg->green = (unsigned)(rgb >> 8) & 255;
3987 fg->blue = (unsigned)rgb & 255;
3988 }
3989 if (bg_rgb != INVALCOLOR)
3990 {
3991 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
3992
3993 bg->red = (unsigned)(rgb >> 16);
3994 bg->green = (unsigned)(rgb >> 8) & 255;
3995 bg->blue = (unsigned)rgb & 255;
3996 }
3997 }
3998 else
3999#endif
4000 if (id != 0 && t_colors >= 16)
4001 {
Milly7b5f45b2021-10-15 22:25:43 +01004002 int cterm_fg = -1;
4003 int cterm_bg = -1;
4004 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01004005
4006 if (cterm_fg >= 0)
4007 cterm_color2vterm(cterm_fg, fg);
4008 if (cterm_bg >= 0)
4009 cterm_color2vterm(cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004010 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004011 else
4012 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004013#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004014 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004015#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004016
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004017 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004018 if (cterm_normal_fg_color > 0)
4019 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004020 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004021# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4022# ifdef VIMDLL
4023 if (!gui.in_use)
4024# endif
4025 {
4026 tmp = fg->red;
4027 fg->red = fg->blue;
4028 fg->blue = tmp;
4029 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004030# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004031 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004032# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004033 else
4034 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004035# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004036
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004037 if (cterm_normal_bg_color > 0)
4038 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004039 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004040# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4041# ifdef VIMDLL
4042 if (!gui.in_use)
4043# endif
4044 {
4045 tmp = fg->red;
4046 fg->red = fg->blue;
4047 fg->blue = tmp;
4048 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004049# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004050 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004051# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004052 else
4053 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004054# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004055 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004056}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004057
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004058#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4059/*
4060 * Set the 16 ANSI colors from array of RGB values
4061 */
4062 static void
4063set_vterm_palette(VTerm *vterm, long_u *rgb)
4064{
4065 int index = 0;
4066 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004067
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004068 for (; index < 16; index++)
4069 {
4070 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004071
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004072 color.red = (unsigned)(rgb[index] >> 16);
4073 color.green = (unsigned)(rgb[index] >> 8) & 255;
4074 color.blue = (unsigned)rgb[index] & 255;
4075 vterm_state_set_palette_color(state, index, &color);
4076 }
4077}
4078
4079/*
4080 * Set the ANSI color palette from a list of colors
4081 */
4082 static int
4083set_ansi_colors_list(VTerm *vterm, list_T *list)
4084{
4085 int n = 0;
4086 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004087 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004088
Bram Moolenaarb0992022020-01-30 14:55:42 +01004089 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004090 {
4091 char_u *color_name;
4092 guicolor_T guicolor;
4093
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004094 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004095 if (color_name == NULL)
4096 return FAIL;
4097
4098 guicolor = GUI_GET_COLOR(color_name);
4099 if (guicolor == INVALCOLOR)
4100 return FAIL;
4101
4102 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4103 }
4104
4105 if (n != 16 || li != NULL)
4106 return FAIL;
4107
4108 set_vterm_palette(vterm, rgb);
4109
4110 return OK;
4111}
4112
4113/*
4114 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4115 */
4116 static void
4117init_vterm_ansi_colors(VTerm *vterm)
4118{
4119 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4120
4121 if (var != NULL
4122 && (var->di_tv.v_type != VAR_LIST
4123 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004124 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004125 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004126 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004127}
4128#endif
4129
Bram Moolenaar52acb112018-03-18 19:20:22 +01004130/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004131 * Handles a "drop" command from the job in the terminal.
4132 * "item" is the file name, "item->li_next" may have options.
4133 */
4134 static void
4135handle_drop_command(listitem_T *item)
4136{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004137 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004138 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004139 int bufnr;
4140 win_T *wp;
4141 tabpage_T *tp;
4142 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004143 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004144
4145 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4146 FOR_ALL_TAB_WINDOWS(tp, wp)
4147 {
4148 if (wp->w_buffer->b_fnum == bufnr)
4149 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004150 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004151 goto_tabpage_win(tp, wp);
4152 return;
4153 }
4154 }
4155
Bram Moolenaara80faa82020-04-12 19:37:17 +02004156 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004157
4158 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4159 && opt_item->li_tv.vval.v_dict != NULL)
4160 {
4161 dict_T *dict = opt_item->li_tv.vval.v_dict;
4162 char_u *p;
4163
Bram Moolenaar8f667172018-12-14 15:38:31 +01004164 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004165 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004166 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004167 if (p != NULL)
4168 {
4169 if (check_ff_value(p) == FAIL)
4170 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4171 else
4172 ea.force_ff = *p;
4173 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004174 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004175 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004176 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004177 if (p != NULL)
4178 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004179 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004180 if (ea.cmd != NULL)
4181 {
4182 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4183 ea.force_enc = 11;
4184 tofree = ea.cmd;
4185 }
4186 }
4187
Bram Moolenaar8f667172018-12-14 15:38:31 +01004188 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004189 if (p != NULL)
4190 get_bad_opt(p, &ea);
4191
4192 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4193 ea.force_bin = FORCE_BIN;
4194 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4195 ea.force_bin = FORCE_BIN;
4196 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4197 ea.force_bin = FORCE_NOBIN;
4198 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4199 ea.force_bin = FORCE_NOBIN;
4200 }
4201
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004202 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004203 if (ea.cmd == NULL)
4204 ea.cmd = (char_u *)"split";
4205 ea.arg = fname;
4206 ea.cmdidx = CMD_split;
4207 ex_splitview(&ea);
4208
4209 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004210}
4211
4212/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004213 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4214 */
4215 static int
4216is_permitted_term_api(char_u *func, char_u *pat)
4217{
4218 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4219}
4220
4221/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004222 * Handles a function call from the job running in a terminal.
4223 * "item" is the function name, "item->li_next" has the arguments.
4224 */
4225 static void
4226handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4227{
4228 char_u *func;
4229 typval_T argvars[2];
4230 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004231 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004232
4233 if (item->li_next == NULL)
4234 {
4235 ch_log(channel, "Missing function arguments for call");
4236 return;
4237 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004238 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004239
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004240 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004241 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004242 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004243 return;
4244 }
4245
4246 argvars[0].v_type = VAR_NUMBER;
4247 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4248 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004249 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004250 funcexe.firstline = 1L;
4251 funcexe.lastline = 1L;
4252 funcexe.evaluate = TRUE;
4253 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004254 {
4255 clear_tv(&rettv);
4256 ch_log(channel, "Function %s called", func);
4257 }
4258 else
4259 ch_log(channel, "Calling function %s failed", func);
4260}
4261
4262/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004263 * URL decoding (also know as Percent-encoding).
4264 *
4265 * Note this function currently is only used for decoding shell's
4266 * OSC 7 escape sequence which we can assume all bytes are valid
4267 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4268 * encoding bytes like 0xfe, 0xff.
4269 */
4270 static size_t
4271url_decode(const char *src, const size_t len, char_u *dst)
4272{
4273 size_t i = 0, j = 0;
4274
4275 while (i < len)
4276 {
4277 if (src[i] == '%' && i + 2 < len)
4278 {
4279 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4280 j++;
4281 i += 3;
4282 }
4283 else
4284 {
4285 dst[j] = src[i];
4286 i++;
4287 j++;
4288 }
4289 }
4290 dst[j] = '\0';
4291 return j;
4292}
4293
4294/*
4295 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4296 *
4297 * The OSC 7 sequence has the format of
4298 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4299 * and what VTerm provides via VTermStringFragment is
4300 * "file://HOSTNAME/CURRENT/DIR"
4301 */
4302 static void
4303sync_shell_dir(VTermStringFragment *frag)
4304{
4305 int offset = 7; // len of "file://" is 7
4306 char *pos = (char *)frag->str + offset;
4307 char_u *new_dir;
4308
4309 // remove HOSTNAME to get PWD
Bram Moolenaar918b0892021-05-08 20:09:24 +02004310 while (*pos != '/' && offset < (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004311 {
4312 offset += 1;
4313 pos += 1;
4314 }
4315
Bram Moolenaar918b0892021-05-08 20:09:24 +02004316 if (offset >= (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004317 {
4318 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
4319 frag->str);
4320 return;
4321 }
4322
4323 new_dir = alloc(frag->len - offset + 1);
4324 url_decode(pos, frag->len-offset, new_dir);
4325 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4326 vim_free(new_dir);
4327}
4328
4329/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004330 * Called by libvterm when it cannot recognize an OSC sequence.
4331 * We recognize a terminal API command.
4332 */
4333 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004334parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004335{
4336 term_T *term = (term_T *)user;
4337 js_read_T reader;
4338 typval_T tv;
4339 channel_T *channel = term->tl_job == NULL ? NULL
4340 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004341 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004342
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004343 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
4344 if (p_asd && command == 7)
4345 {
4346 sync_shell_dir(&frag);
4347 return 1;
4348 }
4349
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004350 if (command != 51)
4351 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004352
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004353 // Concatenate what was received until the final piece is found.
4354 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4355 {
4356 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004357 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004358 }
4359 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004360 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004361 if (!frag.final)
4362 return 1;
4363
4364 ((char *)gap->ga_data)[gap->ga_len] = 0;
4365 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004366 reader.js_fill = NULL;
4367 reader.js_used = 0;
4368 if (json_decode(&reader, &tv, 0) == OK
4369 && tv.v_type == VAR_LIST
4370 && tv.vval.v_list != NULL)
4371 {
4372 listitem_T *item = tv.vval.v_list->lv_first;
4373
4374 if (item == NULL)
4375 ch_log(channel, "Missing command");
4376 else
4377 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004378 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004379
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004380 // Make sure an invoked command doesn't delete the buffer (and the
4381 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004382 ++term->tl_buffer->b_locked;
4383
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004384 item = item->li_next;
4385 if (item == NULL)
4386 ch_log(channel, "Missing argument for %s", cmd);
4387 else if (STRCMP(cmd, "drop") == 0)
4388 handle_drop_command(item);
4389 else if (STRCMP(cmd, "call") == 0)
4390 handle_call_command(term, channel, item);
4391 else
4392 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004393 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004394 }
4395 }
4396 else
4397 ch_log(channel, "Invalid JSON received");
4398
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004399 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004400 clear_tv(&tv);
4401 return 1;
4402}
4403
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004404/*
4405 * Called by libvterm when it cannot recognize a CSI sequence.
4406 * We recognize the window position report.
4407 */
4408 static int
4409parse_csi(
4410 const char *leader UNUSED,
4411 const long args[],
4412 int argcount,
4413 const char *intermed UNUSED,
4414 char command,
4415 void *user)
4416{
4417 term_T *term = (term_T *)user;
4418 char buf[100];
4419 int len;
4420 int x = 0;
4421 int y = 0;
4422 win_T *wp;
4423
4424 // We recognize only CSI 13 t
4425 if (command != 't' || argcount != 1 || args[0] != 13)
4426 return 0; // not handled
4427
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004428 // When getting the window position is not possible or it fails it results
4429 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004430#if defined(FEAT_GUI) \
4431 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4432 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004433 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004434#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004435
4436 FOR_ALL_WINDOWS(wp)
4437 if (wp->w_buffer == term->tl_buffer)
4438 break;
4439 if (wp != NULL)
4440 {
4441#ifdef FEAT_GUI
4442 if (gui.in_use)
4443 {
4444 x += wp->w_wincol * gui.char_width;
4445 y += W_WINROW(wp) * gui.char_height;
4446 }
4447 else
4448#endif
4449 {
4450 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004451 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004452 x += wp->w_wincol * 7;
4453 y += W_WINROW(wp) * 10;
4454 }
4455 }
4456
4457 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4458 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4459 (char_u *)buf, len, NULL);
4460 return 1;
4461}
4462
Bram Moolenaard8637282020-05-20 18:41:41 +02004463static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004464 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004465 parse_csi, // csi
4466 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004467 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004468};
4469
4470/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004471 * Use Vim's allocation functions for vterm so profiling works.
4472 */
4473 static void *
4474vterm_malloc(size_t size, void *data UNUSED)
4475{
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004476 return alloc_clear(size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004477}
4478
4479 static void
4480vterm_memfree(void *ptr, void *data UNUSED)
4481{
4482 vim_free(ptr);
4483}
4484
4485static VTermAllocatorFunctions vterm_allocator = {
4486 &vterm_malloc,
4487 &vterm_memfree
4488};
4489
4490/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004491 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004492 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004493 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004494 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004495create_vterm(term_T *term, int rows, int cols)
4496{
4497 VTerm *vterm;
4498 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004499 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004500 VTermValue value;
4501
Bram Moolenaar756ef112018-04-10 12:04:27 +02004502 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004503 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004504 if (vterm == NULL)
4505 return FAIL;
4506
4507 // Allocate screen and state here, so we can bail out if that fails.
4508 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004509 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004510 if (state == NULL || screen == NULL)
4511 {
4512 vterm_free(vterm);
4513 return FAIL;
4514 }
4515
Bram Moolenaar52acb112018-03-18 19:20:22 +01004516 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004517 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004518 vterm_set_utf8(vterm, 1);
4519
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004520 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004521
4522 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004523 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004524 &term->tl_default_color.fg,
4525 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004526
Bram Moolenaar9e587872019-05-13 20:27:23 +02004527 if (t_colors < 16)
4528 // Less than 16 colors: assume that bold means using a bright color for
4529 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004530 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4531
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004532 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004533 vterm_screen_reset(screen, 1 /* hard */);
4534
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004535 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004536 vterm_screen_enable_altscreen(screen, 1);
4537
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004538 // For unix do not use a blinking cursor. In an xterm this causes the
4539 // cursor to blink if it's blinking in the xterm.
4540 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004541#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004542 if (GetCaretBlinkTime() == INFINITE)
4543 value.boolean = 0;
4544 else
4545 value.boolean = 1;
4546#else
4547 value.boolean = 0;
4548#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004549 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004550 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004551
4552 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004553}
4554
4555/*
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004556 * Called when 'wincolor' was set.
4557 */
4558 void
Bram Moolenaarad431992021-05-03 20:40:38 +02004559term_update_colors(term_T *term)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004560{
Bram Moolenaarad431992021-05-03 20:40:38 +02004561 win_T *wp;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004562
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01004563 if (term->tl_vterm == NULL)
4564 return;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004565 init_default_colors(term, curwin);
4566 vterm_state_set_default_colors(
4567 vterm_obtain_state(term->tl_vterm),
4568 &term->tl_default_color.fg,
4569 &term->tl_default_color.bg);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01004570
Bram Moolenaarad431992021-05-03 20:40:38 +02004571 FOR_ALL_WINDOWS(wp)
4572 if (wp->w_buffer == term->tl_buffer)
4573 redraw_win_later(wp, NOT_VALID);
4574}
4575
4576/*
4577 * Called when 'background' was set.
4578 */
4579 void
4580term_update_colors_all(void)
4581{
4582 term_T *tp;
4583
4584 FOR_ALL_TERMS(tp)
4585 term_update_colors(tp);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004586}
4587
4588/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004589 * Return the text to show for the buffer name and status.
4590 */
4591 char_u *
4592term_get_status_text(term_T *term)
4593{
4594 if (term->tl_status_text == NULL)
4595 {
4596 char_u *txt;
4597 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004598 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004599
4600 if (term->tl_normal_mode)
4601 {
4602 if (term_job_running(term))
4603 txt = (char_u *)_("Terminal");
4604 else
4605 txt = (char_u *)_("Terminal-finished");
4606 }
4607 else if (term->tl_title != NULL)
4608 txt = term->tl_title;
4609 else if (term_none_open(term))
4610 txt = (char_u *)_("active");
4611 else if (term_job_running(term))
4612 txt = (char_u *)_("running");
4613 else
4614 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004615 fname = buf_get_fname(term->tl_buffer);
4616 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004617 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004618 if (term->tl_status_text != NULL)
4619 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004620 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004621 }
4622 return term->tl_status_text;
4623}
4624
4625/*
4626 * Mark references in jobs of terminals.
4627 */
4628 int
4629set_ref_in_term(int copyID)
4630{
4631 int abort = FALSE;
4632 term_T *term;
4633 typval_T tv;
4634
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004635 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004636 if (term->tl_job != NULL)
4637 {
4638 tv.v_type = VAR_JOB;
4639 tv.vval.v_job = term->tl_job;
4640 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4641 }
4642 return abort;
4643}
4644
4645/*
4646 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004647 * Returns NULL when the buffer is not for a terminal window and logs a message
4648 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004649 */
4650 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004651term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004652{
4653 buf_T *buf;
4654
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004655 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004656 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004657 --emsg_off;
4658 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004659 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004660 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004661 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004662 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004663 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004664 return buf;
4665}
4666
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004667 static void
4668clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004669{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004670 CLEAR_FIELD(*cell);
4671 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4672 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004673}
4674
4675 static void
4676dump_term_color(FILE *fd, VTermColor *color)
4677{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004678 int index;
4679
4680 if (VTERM_COLOR_IS_INDEXED(color))
4681 index = color->index + 1;
4682 else if (color->type == 0)
4683 // use RGB values
4684 index = 255;
4685 else
4686 // default color
4687 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004688 fprintf(fd, "%02x%02x%02x%d",
4689 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004690 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004691}
4692
4693/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004694 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004695 *
4696 * Each screen cell in full is:
4697 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4698 * {characters} is a space for an empty cell
4699 * For a double-width character "+" is changed to "*" and the next cell is
4700 * skipped.
4701 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4702 * when "&" use the same as the previous cell.
4703 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4704 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4705 * {color-idx} is a number from 0 to 255
4706 *
4707 * Screen cell with same width, attributes and color as the previous one:
4708 * |{characters}
4709 *
4710 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4711 *
4712 * Repeating the previous screen cell:
4713 * @{count}
4714 */
4715 void
4716f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4717{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004718 buf_T *buf;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004719 term_T *term;
4720 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004721 int max_height = 0;
4722 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004723 stat_T st;
4724 FILE *fd;
4725 VTermPos pos;
4726 VTermScreen *screen;
4727 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004728 VTermState *state;
4729 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004730
4731 if (check_restricted() || check_secure())
4732 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004733
4734 if (in_vim9script()
4735 && (check_for_buffer_arg(argvars, 0) == FAIL
4736 || check_for_string_arg(argvars, 1) == FAIL
4737 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4738 return;
4739
4740 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004741 if (buf == NULL)
4742 return;
4743 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004744 if (term->tl_vterm == NULL)
4745 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004746 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004747 return;
4748 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004749
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004750 if (argvars[2].v_type != VAR_UNKNOWN)
4751 {
4752 dict_T *d;
4753
4754 if (argvars[2].v_type != VAR_DICT)
4755 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004756 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004757 return;
4758 }
4759 d = argvars[2].vval.v_dict;
4760 if (d != NULL)
4761 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004762 max_height = dict_get_number(d, (char_u *)"rows");
4763 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004764 }
4765 }
4766
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004767 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004768 if (fname == NULL)
4769 return;
4770 if (mch_stat((char *)fname, &st) >= 0)
4771 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004772 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004773 return;
4774 }
4775
Bram Moolenaard96ff162018-02-18 22:13:29 +01004776 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4777 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004778 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004779 return;
4780 }
4781
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004782 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004783
4784 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004785 state = vterm_obtain_state(term->tl_vterm);
4786 vterm_state_get_cursorpos(state, &cursor_pos);
4787
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004788 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4789 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004790 {
4791 int repeat = 0;
4792
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004793 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4794 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004795 {
4796 VTermScreenCell cell;
4797 int same_attr;
4798 int same_chars = TRUE;
4799 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004800 int is_cursor_pos = (pos.col == cursor_pos.col
4801 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004802
4803 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004804 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004805
4806 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4807 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004808 int c = cell.chars[i];
4809 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004810 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004811
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004812 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004813 if (i == 0)
4814 {
4815 c = (c == NUL) ? ' ' : c;
4816 pc = (pc == NUL) ? ' ' : pc;
4817 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004818 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004819 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004820 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004821 break;
4822 }
4823 same_attr = vtermAttr2hl(cell.attrs)
4824 == vtermAttr2hl(prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004825 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4826 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004827 if (same_chars && cell.width == prev_cell.width && same_attr
4828 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004829 {
4830 ++repeat;
4831 }
4832 else
4833 {
4834 if (repeat > 0)
4835 {
4836 fprintf(fd, "@%d", repeat);
4837 repeat = 0;
4838 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004839 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004840
4841 if (cell.chars[0] == NUL)
4842 fputs(" ", fd);
4843 else
4844 {
4845 char_u charbuf[10];
4846 int len;
4847
4848 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4849 && cell.chars[i] != NUL; ++i)
4850 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004851 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004852 fwrite(charbuf, len, 1, fd);
4853 }
4854 }
4855
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004856 // When only the characters differ we don't write anything, the
4857 // following "|", "@" or NL will indicate using the same
4858 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004859 if (cell.width != prev_cell.width || !same_attr)
4860 {
4861 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004862 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004863 else
4864 fputs("+", fd);
4865
4866 if (same_attr)
4867 {
4868 fputs("&", fd);
4869 }
4870 else
4871 {
4872 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004873 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004874 fputs("&", fd);
4875 else
4876 {
4877 fputs("#", fd);
4878 dump_term_color(fd, &cell.fg);
4879 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004880 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004881 fputs("&", fd);
4882 else
4883 {
4884 fputs("#", fd);
4885 dump_term_color(fd, &cell.bg);
4886 }
4887 }
4888 }
4889
4890 prev_cell = cell;
4891 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004892
4893 if (cell.width == 2)
4894 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004895 }
4896 if (repeat > 0)
4897 fprintf(fd, "@%d", repeat);
4898 fputs("\n", fd);
4899 }
4900
4901 fclose(fd);
4902}
4903
4904/*
4905 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4906 */
4907 static void
4908dump_is_corrupt(garray_T *gap)
4909{
4910 ga_concat(gap, (char_u *)"CORRUPT");
4911}
4912
4913 static void
4914append_cell(garray_T *gap, cellattr_T *cell)
4915{
4916 if (ga_grow(gap, 1) == OK)
4917 {
4918 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4919 ++gap->ga_len;
4920 }
4921}
4922
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004923 static void
4924clear_cellattr(cellattr_T *cell)
4925{
4926 CLEAR_FIELD(*cell);
4927 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4928 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4929}
4930
Bram Moolenaard96ff162018-02-18 22:13:29 +01004931/*
4932 * Read the dump file from "fd" and append lines to the current buffer.
4933 * Return the cell width of the longest line.
4934 */
4935 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004936read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004937{
4938 int c;
4939 garray_T ga_text;
4940 garray_T ga_cell;
4941 char_u *prev_char = NULL;
4942 int attr = 0;
4943 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004944 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004945 term_T *term = curbuf->b_term;
4946 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004947 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004948
4949 ga_init2(&ga_text, 1, 90);
4950 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004951 clear_cellattr(&cell);
4952 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004953 cursor_pos->row = -1;
4954 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004955
4956 c = fgetc(fd);
4957 for (;;)
4958 {
4959 if (c == EOF)
4960 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004961 if (c == '\r')
4962 {
4963 // DOS line endings? Ignore.
4964 c = fgetc(fd);
4965 }
4966 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004967 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004968 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004969 if (ga_text.ga_data == NULL)
4970 dump_is_corrupt(&ga_text);
4971 if (ga_grow(&term->tl_scrollback, 1) == OK)
4972 {
4973 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
4974 + term->tl_scrollback.ga_len;
4975
4976 if (max_cells < ga_cell.ga_len)
4977 max_cells = ga_cell.ga_len;
4978 line->sb_cols = ga_cell.ga_len;
4979 line->sb_cells = ga_cell.ga_data;
4980 line->sb_fill_attr = term->tl_default_color;
4981 ++term->tl_scrollback.ga_len;
4982 ga_init(&ga_cell);
4983
4984 ga_append(&ga_text, NUL);
4985 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4986 ga_text.ga_len, FALSE);
4987 }
4988 else
4989 ga_clear(&ga_cell);
4990 ga_text.ga_len = 0;
4991
4992 c = fgetc(fd);
4993 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004994 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004995 {
4996 int prev_len = ga_text.ga_len;
4997
Bram Moolenaar9271d052018-02-25 21:39:46 +01004998 if (c == '>')
4999 {
5000 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005001 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005002 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5003 cursor_pos->col = ga_cell.ga_len;
5004 }
5005
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005006 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005007 c = fgetc(fd);
5008 if (c != EOF)
5009 ga_append(&ga_text, c);
5010 for (;;)
5011 {
5012 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005013 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005014 || c == EOF || c == '\n')
5015 break;
5016 ga_append(&ga_text, c);
5017 }
5018
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005019 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005020 vim_free(prev_char);
5021 if (ga_text.ga_data != NULL)
5022 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5023 ga_text.ga_len - prev_len);
5024
Bram Moolenaar9271d052018-02-25 21:39:46 +01005025 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005026 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005027 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005028 }
5029 else if (c == '+' || c == '*')
5030 {
5031 int is_bg;
5032
5033 cell.width = c == '+' ? 1 : 2;
5034
5035 c = fgetc(fd);
5036 if (c == '&')
5037 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005038 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005039 c = fgetc(fd);
5040 }
5041 else if (isdigit(c))
5042 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005043 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005044 attr = 0;
5045 while (isdigit(c))
5046 {
5047 attr = attr * 10 + (c - '0');
5048 c = fgetc(fd);
5049 }
5050 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005051
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005052 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005053 for (is_bg = 0; is_bg <= 1; ++is_bg)
5054 {
5055 if (c == '&')
5056 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005057 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005058 c = fgetc(fd);
5059 }
5060 else if (c == '#')
5061 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005062 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005063
5064 c = fgetc(fd);
5065 red = hex2nr(c);
5066 c = fgetc(fd);
5067 red = (red << 4) + hex2nr(c);
5068 c = fgetc(fd);
5069 green = hex2nr(c);
5070 c = fgetc(fd);
5071 green = (green << 4) + hex2nr(c);
5072 c = fgetc(fd);
5073 blue = hex2nr(c);
5074 c = fgetc(fd);
5075 blue = (blue << 4) + hex2nr(c);
5076 c = fgetc(fd);
5077 if (!isdigit(c))
5078 dump_is_corrupt(&ga_text);
5079 while (isdigit(c))
5080 {
5081 index = index * 10 + (c - '0');
5082 c = fgetc(fd);
5083 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005084 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005085 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005086 type = VTERM_COLOR_RGB;
5087 if (index == 0)
5088 {
5089 if (is_bg)
5090 type |= VTERM_COLOR_DEFAULT_BG;
5091 else
5092 type |= VTERM_COLOR_DEFAULT_FG;
5093 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005094 }
5095 else
5096 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005097 type = VTERM_COLOR_INDEXED;
5098 index -= 1;
5099 }
5100 if (is_bg)
5101 {
5102 cell.bg.type = type;
5103 cell.bg.red = red;
5104 cell.bg.green = green;
5105 cell.bg.blue = blue;
5106 cell.bg.index = index;
5107 }
5108 else
5109 {
5110 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005111 cell.fg.red = red;
5112 cell.fg.green = green;
5113 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005114 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005115 }
5116 }
5117 else
5118 dump_is_corrupt(&ga_text);
5119 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005120 }
5121 else
5122 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005123 }
5124 else
5125 dump_is_corrupt(&ga_text);
5126
5127 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005128 if (cell.width == 2)
5129 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005130 }
5131 else if (c == '@')
5132 {
5133 if (prev_char == NULL)
5134 dump_is_corrupt(&ga_text);
5135 else
5136 {
5137 int count = 0;
5138
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005139 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005140 for (;;)
5141 {
5142 c = fgetc(fd);
5143 if (!isdigit(c))
5144 break;
5145 count = count * 10 + (c - '0');
5146 }
5147
5148 while (count-- > 0)
5149 {
5150 ga_concat(&ga_text, prev_char);
5151 append_cell(&ga_cell, &cell);
5152 }
5153 }
5154 }
5155 else
5156 {
5157 dump_is_corrupt(&ga_text);
5158 c = fgetc(fd);
5159 }
5160 }
5161
5162 if (ga_text.ga_len > 0)
5163 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005164 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005165 dump_is_corrupt(&ga_text);
5166 ga_append(&ga_text, NUL);
5167 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5168 ga_text.ga_len, FALSE);
5169 }
5170
5171 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005172 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005173 vim_free(prev_char);
5174
5175 return max_cells;
5176}
5177
5178/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005179 * Return an allocated string with at least "text_width" "=" characters and
5180 * "fname" inserted in the middle.
5181 */
5182 static char_u *
5183get_separator(int text_width, char_u *fname)
5184{
5185 int width = MAX(text_width, curwin->w_width);
5186 char_u *textline;
5187 int fname_size;
5188 char_u *p = fname;
5189 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005190 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005191
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005192 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005193 if (textline == NULL)
5194 return NULL;
5195
5196 fname_size = vim_strsize(fname);
5197 if (fname_size < width - 8)
5198 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005199 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005200 width = MAX(text_width, fname_size + 8);
5201 }
5202 else if (fname_size > width - 8)
5203 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005204 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005205 p = gettail(fname);
5206 fname_size = vim_strsize(p);
5207 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005208 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005209 while (fname_size > width - 8)
5210 {
5211 p += (*mb_ptr2len)(p);
5212 fname_size = vim_strsize(p);
5213 }
5214
5215 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5216 textline[i] = '=';
5217 textline[i++] = ' ';
5218
5219 STRCPY(textline + i, p);
5220 off = STRLEN(textline);
5221 textline[off] = ' ';
5222 for (i = 1; i < (width - fname_size) / 2; ++i)
5223 textline[off + i] = '=';
5224 textline[off + i] = NUL;
5225
5226 return textline;
5227}
5228
5229/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005230 * Common for "term_dumpdiff()" and "term_dumpload()".
5231 */
5232 static void
5233term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5234{
5235 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005236 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005237 char_u buf1[NUMBUFLEN];
5238 char_u buf2[NUMBUFLEN];
5239 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005240 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005241 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005242 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005243 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005244 char_u *textline = NULL;
5245
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005246 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005247 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005248 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005249 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005250 if (fname1 == NULL || (do_diff && fname2 == NULL))
5251 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005252 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005253 return;
5254 }
5255 fd1 = mch_fopen((char *)fname1, READBIN);
5256 if (fd1 == NULL)
5257 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005258 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005259 return;
5260 }
5261 if (do_diff)
5262 {
5263 fd2 = mch_fopen((char *)fname2, READBIN);
5264 if (fd2 == NULL)
5265 {
5266 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005267 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005268 return;
5269 }
5270 }
5271
5272 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005273 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5274 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5275 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5276 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5277 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005278
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005279 if (opt.jo_term_name == NULL)
5280 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005281 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005282
Bram Moolenaar51e14382019-05-25 20:21:28 +02005283 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005284 if (fname_tofree != NULL)
5285 {
5286 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5287 opt.jo_term_name = fname_tofree;
5288 }
5289 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005290
Bram Moolenaar87abab92019-06-03 21:14:59 +02005291 if (opt.jo_bufnr_buf != NULL)
5292 {
5293 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5294
5295 // With "bufnr" argument: enter the window with this buffer and make it
5296 // empty.
5297 if (wp == NULL)
5298 semsg(_(e_invarg2), "bufnr");
5299 else
5300 {
5301 buf = curbuf;
5302 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005303 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005304 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005305 redraw_later(NOT_VALID);
5306 }
5307 }
5308 else
5309 // Create a new terminal window.
5310 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5311
Bram Moolenaard96ff162018-02-18 22:13:29 +01005312 if (buf != NULL && buf->b_term != NULL)
5313 {
5314 int i;
5315 linenr_T bot_lnum;
5316 linenr_T lnum;
5317 term_T *term = buf->b_term;
5318 int width;
5319 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005320 VTermPos cursor_pos1;
5321 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005322
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005323 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005324
Bram Moolenaard96ff162018-02-18 22:13:29 +01005325 rettv->vval.v_number = buf->b_fnum;
5326
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005327 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005328 width = read_dump_file(fd1, &cursor_pos1);
5329
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005330 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005331 if (cursor_pos1.row >= 0)
5332 {
5333 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5334 coladvance(cursor_pos1.col);
5335 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005336
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005337 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005338 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005339
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005340 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005341 if (!do_diff)
5342 goto theend;
5343
5344 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5345
Bram Moolenaar4a696342018-04-05 18:45:26 +02005346 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005347 if (textline == NULL)
5348 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005349 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5350 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5351 vim_free(textline);
5352
5353 textline = get_separator(width, fname2);
5354 if (textline == NULL)
5355 goto theend;
5356 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5357 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005358 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005359
5360 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005361 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005362 if (width2 > width)
5363 {
5364 vim_free(textline);
5365 textline = alloc(width2 + 1);
5366 if (textline == NULL)
5367 goto theend;
5368 width = width2;
5369 textline[width] = NUL;
5370 }
5371 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5372
5373 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5374 {
5375 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5376 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005377 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005378 for (i = 0; i < width; ++i)
5379 textline[i] = '-';
5380 }
5381 else
5382 {
5383 char_u *line1;
5384 char_u *line2;
5385 char_u *p1;
5386 char_u *p2;
5387 int col;
5388 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5389 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5390 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5391 ->sb_cells;
5392
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005393 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005394 line1 = vim_strsave(ml_get(lnum));
5395 if (line1 == NULL)
5396 break;
5397 p1 = line1;
5398
5399 line2 = ml_get(lnum + bot_lnum);
5400 p2 = line2;
5401 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5402 {
5403 int len1 = utfc_ptr2len(p1);
5404 int len2 = utfc_ptr2len(p2);
5405
5406 textline[col] = ' ';
5407 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005408 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005409 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005410 else if (lnum == cursor_pos1.row + 1
5411 && col == cursor_pos1.col
5412 && (cursor_pos1.row != cursor_pos2.row
5413 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005414 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005415 textline[col] = '>';
5416 else if (lnum == cursor_pos2.row + 1
5417 && col == cursor_pos2.col
5418 && (cursor_pos1.row != cursor_pos2.row
5419 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005420 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005421 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005422 else if (cellattr1 != NULL && cellattr2 != NULL)
5423 {
5424 if ((cellattr1 + col)->width
5425 != (cellattr2 + col)->width)
5426 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005427 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005428 &(cellattr2 + col)->fg))
5429 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005430 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005431 &(cellattr2 + col)->bg))
5432 textline[col] = 'b';
5433 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5434 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5435 textline[col] = 'a';
5436 }
5437 p1 += len1;
5438 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005439 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005440 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005441
5442 while (col < width)
5443 {
5444 if (*p1 == NUL && *p2 == NUL)
5445 textline[col] = '?';
5446 else if (*p1 == NUL)
5447 {
5448 textline[col] = '+';
5449 p2 += utfc_ptr2len(p2);
5450 }
5451 else
5452 {
5453 textline[col] = '-';
5454 p1 += utfc_ptr2len(p1);
5455 }
5456 ++col;
5457 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005458
5459 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005460 }
5461 if (add_empty_scrollback(term, &term->tl_default_color,
5462 term->tl_top_diff_rows) == OK)
5463 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5464 ++bot_lnum;
5465 }
5466
5467 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5468 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005469 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005470 for (i = 0; i < width; ++i)
5471 textline[i] = '+';
5472 if (add_empty_scrollback(term, &term->tl_default_color,
5473 term->tl_top_diff_rows) == OK)
5474 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5475 ++lnum;
5476 ++bot_lnum;
5477 }
5478
5479 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005480
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005481 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005482 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005483 }
5484
5485theend:
5486 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005487 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005488 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005489 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005490 fclose(fd2);
5491}
5492
5493/*
5494 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5495 * bottom files.
5496 * Return FAIL when this is not possible.
5497 */
5498 int
5499term_swap_diff()
5500{
5501 term_T *term = curbuf->b_term;
5502 linenr_T line_count;
5503 linenr_T top_rows;
5504 linenr_T bot_rows;
5505 linenr_T bot_start;
5506 linenr_T lnum;
5507 char_u *p;
5508 sb_line_T *sb_line;
5509
5510 if (term == NULL
5511 || !term_is_finished(curbuf)
5512 || term->tl_top_diff_rows == 0
5513 || term->tl_scrollback.ga_len == 0)
5514 return FAIL;
5515
5516 line_count = curbuf->b_ml.ml_line_count;
5517 top_rows = term->tl_top_diff_rows;
5518 bot_rows = term->tl_bot_diff_rows;
5519 bot_start = line_count - bot_rows;
5520 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5521
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005522 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005523 for (lnum = 1; lnum <= top_rows; ++lnum)
5524 {
5525 p = vim_strsave(ml_get(1));
5526 if (p == NULL)
5527 return OK;
5528 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005529 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005530 vim_free(p);
5531 }
5532
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005533 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005534 for (lnum = 1; lnum <= bot_rows; ++lnum)
5535 {
5536 p = vim_strsave(ml_get(bot_start + lnum));
5537 if (p == NULL)
5538 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005539 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005540 ml_append(lnum - 1, p, 0, FALSE);
5541 vim_free(p);
5542 }
5543
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005544 // move top title to bottom
5545 p = vim_strsave(ml_get(bot_rows + 1));
5546 if (p == NULL)
5547 return OK;
5548 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005549 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005550 vim_free(p);
5551
5552 // move bottom title to top
5553 p = vim_strsave(ml_get(line_count - top_rows));
5554 if (p == NULL)
5555 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005556 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005557 ml_append(bot_rows, p, 0, FALSE);
5558 vim_free(p);
5559
Bram Moolenaard96ff162018-02-18 22:13:29 +01005560 if (top_rows == bot_rows)
5561 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005562 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005563 for (lnum = 0; lnum < top_rows; ++lnum)
5564 {
5565 sb_line_T temp;
5566
5567 temp = *(sb_line + lnum);
5568 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5569 *(sb_line + bot_start + lnum) = temp;
5570 }
5571 }
5572 else
5573 {
5574 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005575 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005576
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005577 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005578 if (temp != NULL)
5579 {
5580 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5581 mch_memmove(term->tl_scrollback.ga_data,
5582 temp + bot_start,
5583 sizeof(sb_line_T) * bot_rows);
5584 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5585 temp + top_rows,
5586 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5587 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5588 + line_count - top_rows,
5589 temp,
5590 sizeof(sb_line_T) * top_rows);
5591 vim_free(temp);
5592 }
5593 }
5594
5595 term->tl_top_diff_rows = bot_rows;
5596 term->tl_bot_diff_rows = top_rows;
5597
5598 update_screen(NOT_VALID);
5599 return OK;
5600}
5601
5602/*
5603 * "term_dumpdiff(filename, filename, options)" function
5604 */
5605 void
5606f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5607{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005608 if (in_vim9script()
5609 && (check_for_string_arg(argvars, 0) == FAIL
5610 || check_for_string_arg(argvars, 1) == FAIL
5611 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5612 return;
5613
Bram Moolenaard96ff162018-02-18 22:13:29 +01005614 term_load_dump(argvars, rettv, TRUE);
5615}
5616
5617/*
5618 * "term_dumpload(filename, options)" function
5619 */
5620 void
5621f_term_dumpload(typval_T *argvars, typval_T *rettv)
5622{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005623 if (in_vim9script()
5624 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005625 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005626 return;
5627
Bram Moolenaard96ff162018-02-18 22:13:29 +01005628 term_load_dump(argvars, rettv, FALSE);
5629}
5630
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005631/*
5632 * "term_getaltscreen(buf)" function
5633 */
5634 void
5635f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5636{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005637 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005638
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005639 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5640 return;
5641
5642 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005643 if (buf == NULL)
5644 return;
5645 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5646}
5647
5648/*
5649 * "term_getattr(attr, name)" function
5650 */
5651 void
5652f_term_getattr(typval_T *argvars, typval_T *rettv)
5653{
5654 int attr;
5655 size_t i;
5656 char_u *name;
5657
5658 static struct {
5659 char *name;
5660 int attr;
5661 } attrs[] = {
5662 {"bold", HL_BOLD},
5663 {"italic", HL_ITALIC},
5664 {"underline", HL_UNDERLINE},
5665 {"strike", HL_STRIKETHROUGH},
5666 {"reverse", HL_INVERSE},
5667 };
5668
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005669 if (in_vim9script()
5670 && (check_for_number_arg(argvars, 0) == FAIL
5671 || check_for_string_arg(argvars, 1) == FAIL))
5672 return;
5673
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005674 attr = tv_get_number(&argvars[0]);
5675 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005676 if (name == NULL)
5677 return;
5678
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005679 if (attr > HL_ALL)
5680 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02005681 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005682 if (STRCMP(name, attrs[i].name) == 0)
5683 {
5684 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5685 break;
5686 }
5687}
5688
5689/*
5690 * "term_getcursor(buf)" function
5691 */
5692 void
5693f_term_getcursor(typval_T *argvars, typval_T *rettv)
5694{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005695 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005696 term_T *term;
5697 list_T *l;
5698 dict_T *d;
5699
5700 if (rettv_list_alloc(rettv) == FAIL)
5701 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005702
5703 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5704 return;
5705
5706 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005707 if (buf == NULL)
5708 return;
5709 term = buf->b_term;
5710
5711 l = rettv->vval.v_list;
5712 list_append_number(l, term->tl_cursor_pos.row + 1);
5713 list_append_number(l, term->tl_cursor_pos.col + 1);
5714
5715 d = dict_alloc();
5716 if (d != NULL)
5717 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005718 dict_add_number(d, "visible", term->tl_cursor_visible);
5719 dict_add_number(d, "blink", blink_state_is_inverted()
5720 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5721 dict_add_number(d, "shape", term->tl_cursor_shape);
5722 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005723 list_append_dict(l, d);
5724 }
5725}
5726
5727/*
5728 * "term_getjob(buf)" function
5729 */
5730 void
5731f_term_getjob(typval_T *argvars, typval_T *rettv)
5732{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005733 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005734
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005735 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5736 return;
5737
5738 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005739 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005740 {
5741 rettv->v_type = VAR_SPECIAL;
5742 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005743 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005744 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005745
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005746 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005747 rettv->vval.v_job = buf->b_term->tl_job;
5748 if (rettv->vval.v_job != NULL)
5749 ++rettv->vval.v_job->jv_refcount;
5750}
5751
5752 static int
5753get_row_number(typval_T *tv, term_T *term)
5754{
5755 if (tv->v_type == VAR_STRING
5756 && tv->vval.v_string != NULL
5757 && STRCMP(tv->vval.v_string, ".") == 0)
5758 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005759 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005760}
5761
5762/*
5763 * "term_getline(buf, row)" function
5764 */
5765 void
5766f_term_getline(typval_T *argvars, typval_T *rettv)
5767{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005768 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005769 term_T *term;
5770 int row;
5771
5772 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005773
5774 if (in_vim9script()
5775 && (check_for_buffer_arg(argvars, 0) == FAIL
5776 || check_for_lnum_arg(argvars, 1) == FAIL))
5777 return;
5778
5779 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005780 if (buf == NULL)
5781 return;
5782 term = buf->b_term;
5783 row = get_row_number(&argvars[1], term);
5784
5785 if (term->tl_vterm == NULL)
5786 {
5787 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5788
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005789 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005790 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5791 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5792 }
5793 else
5794 {
5795 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5796 VTermRect rect;
5797 int len;
5798 char_u *p;
5799
5800 if (row < 0 || row >= term->tl_rows)
5801 return;
5802 len = term->tl_cols * MB_MAXBYTES + 1;
5803 p = alloc(len);
5804 if (p == NULL)
5805 return;
5806 rettv->vval.v_string = p;
5807
5808 rect.start_col = 0;
5809 rect.end_col = term->tl_cols;
5810 rect.start_row = row;
5811 rect.end_row = row + 1;
5812 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5813 }
5814}
5815
5816/*
5817 * "term_getscrolled(buf)" function
5818 */
5819 void
5820f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5821{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005822 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005823
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005824 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5825 return;
5826
5827 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005828 if (buf == NULL)
5829 return;
5830 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5831}
5832
5833/*
5834 * "term_getsize(buf)" function
5835 */
5836 void
5837f_term_getsize(typval_T *argvars, typval_T *rettv)
5838{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005839 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005840 list_T *l;
5841
5842 if (rettv_list_alloc(rettv) == FAIL)
5843 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005844
5845 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5846 return;
5847
5848 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005849 if (buf == NULL)
5850 return;
5851
5852 l = rettv->vval.v_list;
5853 list_append_number(l, buf->b_term->tl_rows);
5854 list_append_number(l, buf->b_term->tl_cols);
5855}
5856
5857/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005858 * "term_setsize(buf, rows, cols)" function
5859 */
5860 void
5861f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5862{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005863 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02005864 term_T *term;
5865 varnumber_T rows, cols;
5866
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005867 if (in_vim9script()
5868 && (check_for_buffer_arg(argvars, 0) == FAIL
5869 || check_for_number_arg(argvars, 1) == FAIL
5870 || check_for_number_arg(argvars, 2) == FAIL))
5871 return;
5872
5873 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005874 if (buf == NULL)
5875 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005876 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005877 return;
5878 }
5879 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005880 return;
5881 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005882 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005883 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005884 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005885 cols = cols <= 0 ? term->tl_cols : cols;
5886 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005887 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005888
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005889 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005890 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5891 term_report_winsize(term, term->tl_rows, term->tl_cols);
5892}
5893
5894/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005895 * "term_getstatus(buf)" function
5896 */
5897 void
5898f_term_getstatus(typval_T *argvars, typval_T *rettv)
5899{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005900 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005901 term_T *term;
5902 char_u val[100];
5903
5904 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005905
5906 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5907 return;
5908
5909 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005910 if (buf == NULL)
5911 return;
5912 term = buf->b_term;
5913
5914 if (term_job_running(term))
5915 STRCPY(val, "running");
5916 else
5917 STRCPY(val, "finished");
5918 if (term->tl_normal_mode)
5919 STRCAT(val, ",normal");
5920 rettv->vval.v_string = vim_strsave(val);
5921}
5922
5923/*
5924 * "term_gettitle(buf)" function
5925 */
5926 void
5927f_term_gettitle(typval_T *argvars, typval_T *rettv)
5928{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005929 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005930
5931 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005932
5933 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5934 return;
5935
5936 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005937 if (buf == NULL)
5938 return;
5939
5940 if (buf->b_term->tl_title != NULL)
5941 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5942}
5943
5944/*
5945 * "term_gettty(buf)" function
5946 */
5947 void
5948f_term_gettty(typval_T *argvars, typval_T *rettv)
5949{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005950 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005951 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005952 int num = 0;
5953
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005954 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005955 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005956 || check_for_opt_bool_arg(argvars, 1) == FAIL))
5957 return;
5958
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005959 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005960 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005961 if (buf == NULL)
5962 return;
5963 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02005964 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005965
5966 switch (num)
5967 {
5968 case 0:
5969 if (buf->b_term->tl_job != NULL)
5970 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005971 break;
5972 case 1:
5973 if (buf->b_term->tl_job != NULL)
5974 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005975 break;
5976 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005977 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005978 return;
5979 }
5980 if (p != NULL)
5981 rettv->vval.v_string = vim_strsave(p);
5982}
5983
5984/*
5985 * "term_list()" function
5986 */
5987 void
5988f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
5989{
5990 term_T *tp;
5991 list_T *l;
5992
5993 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
5994 return;
5995
5996 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005997 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02005998 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005999 if (list_append_number(l,
6000 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6001 return;
6002}
6003
6004/*
6005 * "term_scrape(buf, row)" function
6006 */
6007 void
6008f_term_scrape(typval_T *argvars, typval_T *rettv)
6009{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006010 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006011 VTermScreen *screen = NULL;
6012 VTermPos pos;
6013 list_T *l;
6014 term_T *term;
6015 char_u *p;
6016 sb_line_T *line;
6017
6018 if (rettv_list_alloc(rettv) == FAIL)
6019 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006020
6021 if (in_vim9script()
6022 && (check_for_buffer_arg(argvars, 0) == FAIL
6023 || check_for_lnum_arg(argvars, 1) == FAIL))
6024 return;
6025
6026 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006027 if (buf == NULL)
6028 return;
6029 term = buf->b_term;
6030
6031 l = rettv->vval.v_list;
6032 pos.row = get_row_number(&argvars[1], term);
6033
6034 if (term->tl_vterm != NULL)
6035 {
6036 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006037 if (screen == NULL) // can't really happen
6038 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006039 p = NULL;
6040 line = NULL;
6041 }
6042 else
6043 {
6044 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6045
6046 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6047 return;
6048 p = ml_get_buf(buf, lnum + 1, FALSE);
6049 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6050 }
6051
6052 for (pos.col = 0; pos.col < term->tl_cols; )
6053 {
6054 dict_T *dcell;
6055 int width;
6056 VTermScreenCellAttrs attrs;
6057 VTermColor fg, bg;
6058 char_u rgb[8];
6059 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6060 int off = 0;
6061 int i;
6062
6063 if (screen == NULL)
6064 {
6065 cellattr_T *cellattr;
6066 int len;
6067
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006068 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006069 if (pos.col >= line->sb_cols)
6070 break;
6071 cellattr = line->sb_cells + pos.col;
6072 width = cellattr->width;
6073 attrs = cellattr->attrs;
6074 fg = cellattr->fg;
6075 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006076 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006077 mch_memmove(mbs, p, len);
6078 mbs[len] = NUL;
6079 p += len;
6080 }
6081 else
6082 {
6083 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006084
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006085 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6086 break;
6087 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6088 {
6089 if (cell.chars[i] == 0)
6090 break;
6091 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6092 }
6093 mbs[off] = NUL;
6094 width = cell.width;
6095 attrs = cell.attrs;
6096 fg = cell.fg;
6097 bg = cell.bg;
6098 }
6099 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006100 if (dcell == NULL)
6101 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006102 list_append_dict(l, dcell);
6103
Bram Moolenaare0be1672018-07-08 16:50:37 +02006104 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006105
6106 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6107 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006108 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006109 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6110 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006111 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006112
Bram Moolenaar83d47902020-03-26 20:34:00 +01006113 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006114 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006115
6116 ++pos.col;
6117 if (width == 2)
6118 ++pos.col;
6119 }
6120}
6121
6122/*
6123 * "term_sendkeys(buf, keys)" function
6124 */
6125 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006126f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006127{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006128 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006129 char_u *msg;
6130 term_T *term;
6131
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006132 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006133 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006134 || check_for_string_arg(argvars, 1) == FAIL))
6135 return;
6136
6137 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006138 if (buf == NULL)
6139 return;
6140
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006141 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006142 if (msg == NULL)
6143 return;
6144 term = buf->b_term;
6145 if (term->tl_vterm == NULL)
6146 return;
6147
6148 while (*msg != NUL)
6149 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006150 int c;
6151
6152 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6153 {
6154 c = TO_SPECIAL(msg[1], msg[2]);
6155 msg += 3;
6156 }
6157 else
6158 {
6159 c = PTR2CHAR(msg);
6160 msg += MB_CPTR2LEN(msg);
6161 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006162 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006163 }
6164}
6165
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006166#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6167/*
6168 * "term_getansicolors(buf)" function
6169 */
6170 void
6171f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6172{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006173 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006174 term_T *term;
6175 VTermState *state;
6176 VTermColor color;
6177 char_u hexbuf[10];
6178 int index;
6179 list_T *list;
6180
6181 if (rettv_list_alloc(rettv) == FAIL)
6182 return;
6183
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006184 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6185 return;
6186
6187 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006188 if (buf == NULL)
6189 return;
6190 term = buf->b_term;
6191 if (term->tl_vterm == NULL)
6192 return;
6193
6194 list = rettv->vval.v_list;
6195 state = vterm_obtain_state(term->tl_vterm);
6196 for (index = 0; index < 16; index++)
6197 {
6198 vterm_state_get_palette_color(state, index, &color);
6199 sprintf((char *)hexbuf, "#%02x%02x%02x",
6200 color.red, color.green, color.blue);
6201 if (list_append_string(list, hexbuf, 7) == FAIL)
6202 return;
6203 }
6204}
6205
6206/*
6207 * "term_setansicolors(buf, list)" function
6208 */
6209 void
6210f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6211{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006212 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006213 term_T *term;
6214
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006215 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006216 && (check_for_buffer_arg(argvars, 0) == FAIL
6217 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006218 return;
6219
6220 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006221 if (buf == NULL)
6222 return;
6223 term = buf->b_term;
6224 if (term->tl_vterm == NULL)
6225 return;
6226
6227 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006229 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006230 return;
6231 }
6232
6233 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006234 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006235}
6236#endif
6237
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006238/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006239 * "term_setapi(buf, api)" function
6240 */
6241 void
6242f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6243{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006244 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006245 term_T *term;
6246 char_u *api;
6247
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006248 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006249 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006250 || check_for_string_arg(argvars, 1) == FAIL))
6251 return;
6252
6253 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006254 if (buf == NULL)
6255 return;
6256 term = buf->b_term;
6257 vim_free(term->tl_api);
6258 api = tv_get_string_chk(&argvars[1]);
6259 if (api != NULL)
6260 term->tl_api = vim_strsave(api);
6261 else
6262 term->tl_api = NULL;
6263}
6264
6265/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006266 * "term_setrestore(buf, command)" function
6267 */
6268 void
6269f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6270{
6271#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006272 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006273 term_T *term;
6274 char_u *cmd;
6275
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006276 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006277 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006278 || check_for_string_arg(argvars, 1) == FAIL))
6279 return;
6280
6281 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006282 if (buf == NULL)
6283 return;
6284 term = buf->b_term;
6285 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006286 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006287 if (cmd != NULL)
6288 term->tl_command = vim_strsave(cmd);
6289 else
6290 term->tl_command = NULL;
6291#endif
6292}
6293
6294/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006295 * "term_setkill(buf, how)" function
6296 */
6297 void
6298f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6299{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006300 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006301 term_T *term;
6302 char_u *how;
6303
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006304 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006305 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006306 || check_for_string_arg(argvars, 1) == FAIL))
6307 return;
6308
6309 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006310 if (buf == NULL)
6311 return;
6312 term = buf->b_term;
6313 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006314 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006315 if (how != NULL)
6316 term->tl_kill = vim_strsave(how);
6317 else
6318 term->tl_kill = NULL;
6319}
6320
6321/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006322 * "term_start(command, options)" function
6323 */
6324 void
6325f_term_start(typval_T *argvars, typval_T *rettv)
6326{
6327 jobopt_T opt;
6328 buf_T *buf;
6329
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006330 if (in_vim9script()
6331 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6332 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6333 return;
6334
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006335 init_job_options(&opt);
6336 if (argvars[1].v_type != VAR_UNKNOWN
6337 && get_job_options(&argvars[1], &opt,
6338 JO_TIMEOUT_ALL + JO_STOPONEXIT
6339 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6340 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6341 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6342 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006343 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006344 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006345 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006346 return;
6347
Bram Moolenaar13568252018-03-16 20:46:58 +01006348 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006349
6350 if (buf != NULL && buf->b_term != NULL)
6351 rettv->vval.v_number = buf->b_fnum;
6352}
6353
6354/*
6355 * "term_wait" function
6356 */
6357 void
6358f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6359{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006360 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006361
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006362 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006363 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006364 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006365 return;
6366
6367 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006368 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006369 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006370 if (buf->b_term->tl_job == NULL)
6371 {
6372 ch_log(NULL, "term_wait(): no job to wait for");
6373 return;
6374 }
6375 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006376 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006377 return;
6378
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006379 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006380 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006381 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6382 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006383 // The job is dead, keep reading channel I/O until the channel is
6384 // closed. buf->b_term may become NULL if the terminal was closed while
6385 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006386 ch_log(NULL, "term_wait(): waiting for channel to close");
6387 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6388 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006389 term_flush_messages();
6390
Bram Moolenaard45aa552018-05-21 22:50:29 +02006391 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006392 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006393 // If the terminal is closed when the channel is closed the
6394 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006395 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006396 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006397
6398 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006399 }
6400 else
6401 {
6402 long wait = 10L;
6403
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006404 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006405
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006406 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006407 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006408 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006409 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006410
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006411 // Flushing messages on channels is hopefully sufficient.
6412 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006413 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006414 }
6415}
6416
6417/*
6418 * Called when a channel has sent all the lines to a terminal.
6419 * Send a CTRL-D to mark the end of the text.
6420 */
6421 void
6422term_send_eof(channel_T *ch)
6423{
6424 term_T *term;
6425
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006426 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006427 if (term->tl_job == ch->ch_job)
6428 {
6429 if (term->tl_eof_chars != NULL)
6430 {
6431 channel_send(ch, PART_IN, term->tl_eof_chars,
6432 (int)STRLEN(term->tl_eof_chars), NULL);
6433 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6434 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006435# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006436 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006437 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006438 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6439# endif
6440 }
6441}
6442
Bram Moolenaar113e1072019-01-20 15:30:40 +01006443#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006444 job_T *
6445term_getjob(term_T *term)
6446{
6447 return term != NULL ? term->tl_job : NULL;
6448}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006449#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006450
Bram Moolenaar4f974752019-02-17 17:44:42 +01006451# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006452
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006453///////////////////////////////////////
6454// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006455#ifdef PROTO
6456typedef int COORD;
6457typedef int DWORD;
6458typedef int HANDLE;
6459typedef int *DWORD_PTR;
6460typedef int HPCON;
6461typedef int HRESULT;
6462typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006463typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006464typedef int PSIZE_T;
6465typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006466typedef int BOOL;
6467# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006468#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006469
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006470HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6471HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6472HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006473BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6474BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6475void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006476
6477 static int
6478dyn_conpty_init(int verbose)
6479{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006480 static HMODULE hKerneldll = NULL;
6481 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006482 static struct
6483 {
6484 char *name;
6485 FARPROC *ptr;
6486 } conpty_entry[] =
6487 {
6488 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6489 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6490 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6491 {"InitializeProcThreadAttributeList",
6492 (FARPROC*)&pInitializeProcThreadAttributeList},
6493 {"UpdateProcThreadAttribute",
6494 (FARPROC*)&pUpdateProcThreadAttribute},
6495 {"DeleteProcThreadAttributeList",
6496 (FARPROC*)&pDeleteProcThreadAttributeList},
6497 {NULL, NULL}
6498 };
6499
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006500 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006501 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006502 if (verbose)
6503 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006504 return FAIL;
6505 }
6506
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006507 // No need to initialize twice.
6508 if (hKerneldll)
6509 return OK;
6510
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006511 hKerneldll = vimLoadLib("kernel32.dll");
6512 for (i = 0; conpty_entry[i].name != NULL
6513 && conpty_entry[i].ptr != NULL; ++i)
6514 {
6515 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6516 conpty_entry[i].name)) == NULL)
6517 {
6518 if (verbose)
6519 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006520 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006521 return FAIL;
6522 }
6523 }
6524
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006525 return OK;
6526}
6527
6528 static int
6529conpty_term_and_job_init(
6530 term_T *term,
6531 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006532 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006533 jobopt_T *opt,
6534 jobopt_T *orig_opt)
6535{
6536 WCHAR *cmd_wchar = NULL;
6537 WCHAR *cmd_wchar_copy = NULL;
6538 WCHAR *cwd_wchar = NULL;
6539 WCHAR *env_wchar = NULL;
6540 channel_T *channel = NULL;
6541 job_T *job = NULL;
6542 HANDLE jo = NULL;
6543 garray_T ga_cmd, ga_env;
6544 char_u *cmd = NULL;
6545 HRESULT hr;
6546 COORD consize;
6547 SIZE_T breq;
6548 PROCESS_INFORMATION proc_info;
6549 HANDLE i_theirs = NULL;
6550 HANDLE o_theirs = NULL;
6551 HANDLE i_ours = NULL;
6552 HANDLE o_ours = NULL;
6553
6554 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6555 ga_init2(&ga_env, (int)sizeof(char*), 20);
6556
6557 if (argvar->v_type == VAR_STRING)
6558 {
6559 cmd = argvar->vval.v_string;
6560 }
6561 else if (argvar->v_type == VAR_LIST)
6562 {
6563 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6564 goto failed;
6565 cmd = ga_cmd.ga_data;
6566 }
6567 if (cmd == NULL || *cmd == NUL)
6568 {
6569 emsg(_(e_invarg));
6570 goto failed;
6571 }
6572
6573 term->tl_arg0_cmd = vim_strsave(cmd);
6574
6575 cmd_wchar = enc_to_utf16(cmd, NULL);
6576
6577 if (cmd_wchar != NULL)
6578 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006579 // Request by CreateProcessW
6580 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006581 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006582 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6583 }
6584
6585 ga_clear(&ga_cmd);
6586 if (cmd_wchar == NULL)
6587 goto failed;
6588 if (opt->jo_cwd != NULL)
6589 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6590
6591 win32_build_env(opt->jo_env, &ga_env, TRUE);
6592 env_wchar = ga_env.ga_data;
6593
6594 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6595 goto failed;
6596 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6597 goto failed;
6598
6599 consize.X = term->tl_cols;
6600 consize.Y = term->tl_rows;
6601 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6602 &term->tl_conpty);
6603 if (FAILED(hr))
6604 goto failed;
6605
6606 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6607
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006608 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006609 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006610 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006611 if (!term->tl_siex.lpAttributeList)
6612 goto failed;
6613 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6614 0, &breq))
6615 goto failed;
6616 if (!pUpdateProcThreadAttribute(
6617 term->tl_siex.lpAttributeList, 0,
6618 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6619 sizeof(HPCON), NULL, NULL))
6620 goto failed;
6621
6622 channel = add_channel();
6623 if (channel == NULL)
6624 goto failed;
6625
6626 job = job_alloc();
6627 if (job == NULL)
6628 goto failed;
6629 if (argvar->v_type == VAR_STRING)
6630 {
6631 int argc;
6632
6633 build_argv_from_string(cmd, &job->jv_argv, &argc);
6634 }
6635 else
6636 {
6637 int argc;
6638
6639 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6640 }
6641
6642 if (opt->jo_set & JO_IN_BUF)
6643 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6644
6645 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6646 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006647 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006648 env_wchar, cwd_wchar,
6649 &term->tl_siex.StartupInfo, &proc_info))
6650 goto failed;
6651
6652 CloseHandle(i_theirs);
6653 CloseHandle(o_theirs);
6654
6655 channel_set_pipes(channel,
6656 (sock_T)i_ours,
6657 (sock_T)o_ours,
6658 (sock_T)o_ours);
6659
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006660 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006661 channel->ch_write_text_mode = TRUE;
6662
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006663 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006664 channel->ch_anonymous_pipe = TRUE;
6665
6666 jo = CreateJobObject(NULL, NULL);
6667 if (jo == NULL)
6668 goto failed;
6669
6670 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6671 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006672 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006673 CloseHandle(jo);
6674 jo = NULL;
6675 }
6676
6677 ResumeThread(proc_info.hThread);
6678 CloseHandle(proc_info.hThread);
6679
6680 vim_free(cmd_wchar);
6681 vim_free(cmd_wchar_copy);
6682 vim_free(cwd_wchar);
6683 vim_free(env_wchar);
6684
6685 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6686 goto failed;
6687
6688#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6689 if (opt->jo_set2 & JO2_ANSI_COLORS)
6690 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6691 else
6692 init_vterm_ansi_colors(term->tl_vterm);
6693#endif
6694
6695 channel_set_job(channel, job, opt);
6696 job_set_options(job, opt);
6697
6698 job->jv_channel = channel;
6699 job->jv_proc_info = proc_info;
6700 job->jv_job_object = jo;
6701 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006702 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006703 ++job->jv_refcount;
6704 term->tl_job = job;
6705
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006706 // Redirecting stdout and stderr doesn't work at the job level. Instead
6707 // open the file here and handle it in. opt->jo_io was changed in
6708 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006709 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6710 {
6711 char_u *fname = opt->jo_io_name[PART_OUT];
6712
6713 ch_log(channel, "Opening output file %s", fname);
6714 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6715 if (term->tl_out_fd == NULL)
6716 semsg(_(e_notopen), fname);
6717 }
6718
6719 return OK;
6720
6721failed:
6722 ga_clear(&ga_cmd);
6723 ga_clear(&ga_env);
6724 vim_free(cmd_wchar);
6725 vim_free(cmd_wchar_copy);
6726 vim_free(cwd_wchar);
6727 if (channel != NULL)
6728 channel_clear(channel);
6729 if (job != NULL)
6730 {
6731 job->jv_channel = NULL;
6732 job_cleanup(job);
6733 }
6734 term->tl_job = NULL;
6735 if (jo != NULL)
6736 CloseHandle(jo);
6737
6738 if (term->tl_siex.lpAttributeList != NULL)
6739 {
6740 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6741 vim_free(term->tl_siex.lpAttributeList);
6742 }
6743 term->tl_siex.lpAttributeList = NULL;
6744 if (o_theirs != NULL)
6745 CloseHandle(o_theirs);
6746 if (o_ours != NULL)
6747 CloseHandle(o_ours);
6748 if (i_ours != NULL)
6749 CloseHandle(i_ours);
6750 if (i_theirs != NULL)
6751 CloseHandle(i_theirs);
6752 if (term->tl_conpty != NULL)
6753 pClosePseudoConsole(term->tl_conpty);
6754 term->tl_conpty = NULL;
6755 return FAIL;
6756}
6757
6758 static void
6759conpty_term_report_winsize(term_T *term, int rows, int cols)
6760{
6761 COORD consize;
6762
6763 consize.X = cols;
6764 consize.Y = rows;
6765 pResizePseudoConsole(term->tl_conpty, consize);
6766}
6767
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006768 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006769term_free_conpty(term_T *term)
6770{
6771 if (term->tl_siex.lpAttributeList != NULL)
6772 {
6773 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6774 vim_free(term->tl_siex.lpAttributeList);
6775 }
6776 term->tl_siex.lpAttributeList = NULL;
6777 if (term->tl_conpty != NULL)
6778 pClosePseudoConsole(term->tl_conpty);
6779 term->tl_conpty = NULL;
6780}
6781
6782 int
6783use_conpty(void)
6784{
6785 return has_conpty;
6786}
6787
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006788# ifndef PROTO
6789
6790#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6791#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006792#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006793
6794void* (*winpty_config_new)(UINT64, void*);
6795void* (*winpty_open)(void*, void*);
6796void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6797BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6798void (*winpty_config_set_mouse_mode)(void*, int);
6799void (*winpty_config_set_initial_size)(void*, int, int);
6800LPCWSTR (*winpty_conin_name)(void*);
6801LPCWSTR (*winpty_conout_name)(void*);
6802LPCWSTR (*winpty_conerr_name)(void*);
6803void (*winpty_free)(void*);
6804void (*winpty_config_free)(void*);
6805void (*winpty_spawn_config_free)(void*);
6806void (*winpty_error_free)(void*);
6807LPCWSTR (*winpty_error_msg)(void*);
6808BOOL (*winpty_set_size)(void*, int, int, void*);
6809HANDLE (*winpty_agent_process)(void*);
6810
6811#define WINPTY_DLL "winpty.dll"
6812
6813static HINSTANCE hWinPtyDLL = NULL;
6814# endif
6815
6816 static int
6817dyn_winpty_init(int verbose)
6818{
6819 int i;
6820 static struct
6821 {
6822 char *name;
6823 FARPROC *ptr;
6824 } winpty_entry[] =
6825 {
6826 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6827 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6828 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6829 {"winpty_config_set_mouse_mode",
6830 (FARPROC*)&winpty_config_set_mouse_mode},
6831 {"winpty_config_set_initial_size",
6832 (FARPROC*)&winpty_config_set_initial_size},
6833 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6834 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6835 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6836 {"winpty_free", (FARPROC*)&winpty_free},
6837 {"winpty_open", (FARPROC*)&winpty_open},
6838 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6839 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6840 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6841 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6842 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6843 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6844 {NULL, NULL}
6845 };
6846
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006847 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006848 if (hWinPtyDLL)
6849 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006850 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6851 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006852 if (*p_winptydll != NUL)
6853 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6854 if (!hWinPtyDLL)
6855 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6856 if (!hWinPtyDLL)
6857 {
6858 if (verbose)
Martin Tournoij1a3e5742021-07-24 13:57:29 +02006859 semsg(_(e_loadlib),
6860 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
6861 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006862 return FAIL;
6863 }
6864 for (i = 0; winpty_entry[i].name != NULL
6865 && winpty_entry[i].ptr != NULL; ++i)
6866 {
6867 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6868 winpty_entry[i].name)) == NULL)
6869 {
6870 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006871 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006872 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006873 return FAIL;
6874 }
6875 }
6876
6877 return OK;
6878}
6879
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006880 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006881winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006882 term_T *term,
6883 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006884 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006885 jobopt_T *opt,
6886 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006887{
6888 WCHAR *cmd_wchar = NULL;
6889 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006890 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006891 channel_T *channel = NULL;
6892 job_T *job = NULL;
6893 DWORD error;
6894 HANDLE jo = NULL;
6895 HANDLE child_process_handle;
6896 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006897 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006898 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006899 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006900 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006901
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006902 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6903 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006904
6905 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006906 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006907 cmd = argvar->vval.v_string;
6908 }
6909 else if (argvar->v_type == VAR_LIST)
6910 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006911 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006912 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006913 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006914 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006915 if (cmd == NULL || *cmd == NUL)
6916 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006917 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006918 goto failed;
6919 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006920
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006921 term->tl_arg0_cmd = vim_strsave(cmd);
6922
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006923 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006924 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006925 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006926 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006927 if (opt->jo_cwd != NULL)
6928 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006929
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006930 win32_build_env(opt->jo_env, &ga_env, TRUE);
6931 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006932
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006933 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6934 if (term->tl_winpty_config == NULL)
6935 goto failed;
6936
6937 winpty_config_set_mouse_mode(term->tl_winpty_config,
6938 WINPTY_MOUSE_MODE_FORCE);
6939 winpty_config_set_initial_size(term->tl_winpty_config,
6940 term->tl_cols, term->tl_rows);
6941 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6942 if (term->tl_winpty == NULL)
6943 goto failed;
6944
6945 spawn_config = winpty_spawn_config_new(
6946 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6947 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6948 NULL,
6949 cmd_wchar,
6950 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006951 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006952 &winpty_err);
6953 if (spawn_config == NULL)
6954 goto failed;
6955
6956 channel = add_channel();
6957 if (channel == NULL)
6958 goto failed;
6959
6960 job = job_alloc();
6961 if (job == NULL)
6962 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006963 if (argvar->v_type == VAR_STRING)
6964 {
6965 int argc;
6966
6967 build_argv_from_string(cmd, &job->jv_argv, &argc);
6968 }
6969 else
6970 {
6971 int argc;
6972
6973 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6974 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006975
6976 if (opt->jo_set & JO_IN_BUF)
6977 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6978
6979 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
6980 &child_thread_handle, &error, &winpty_err))
6981 goto failed;
6982
6983 channel_set_pipes(channel,
6984 (sock_T)CreateFileW(
6985 winpty_conin_name(term->tl_winpty),
6986 GENERIC_WRITE, 0, NULL,
6987 OPEN_EXISTING, 0, NULL),
6988 (sock_T)CreateFileW(
6989 winpty_conout_name(term->tl_winpty),
6990 GENERIC_READ, 0, NULL,
6991 OPEN_EXISTING, 0, NULL),
6992 (sock_T)CreateFileW(
6993 winpty_conerr_name(term->tl_winpty),
6994 GENERIC_READ, 0, NULL,
6995 OPEN_EXISTING, 0, NULL));
6996
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006997 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006998 channel->ch_write_text_mode = TRUE;
6999
7000 jo = CreateJobObject(NULL, NULL);
7001 if (jo == NULL)
7002 goto failed;
7003
7004 if (!AssignProcessToJobObject(jo, child_process_handle))
7005 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007006 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007007 CloseHandle(jo);
7008 jo = NULL;
7009 }
7010
7011 winpty_spawn_config_free(spawn_config);
7012 vim_free(cmd_wchar);
7013 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007014 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007015
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007016 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7017 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007018
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007019#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7020 if (opt->jo_set2 & JO2_ANSI_COLORS)
7021 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7022 else
7023 init_vterm_ansi_colors(term->tl_vterm);
7024#endif
7025
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007026 channel_set_job(channel, job, opt);
7027 job_set_options(job, opt);
7028
7029 job->jv_channel = channel;
7030 job->jv_proc_info.hProcess = child_process_handle;
7031 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7032 job->jv_job_object = jo;
7033 job->jv_status = JOB_STARTED;
7034 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007035 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007036 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007037 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007038 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007039 ++job->jv_refcount;
7040 term->tl_job = job;
7041
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007042 // Redirecting stdout and stderr doesn't work at the job level. Instead
7043 // open the file here and handle it in. opt->jo_io was changed in
7044 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007045 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7046 {
7047 char_u *fname = opt->jo_io_name[PART_OUT];
7048
7049 ch_log(channel, "Opening output file %s", fname);
7050 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7051 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007052 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007053 }
7054
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007055 return OK;
7056
7057failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007058 ga_clear(&ga_cmd);
7059 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007060 vim_free(cmd_wchar);
7061 vim_free(cwd_wchar);
7062 if (spawn_config != NULL)
7063 winpty_spawn_config_free(spawn_config);
7064 if (channel != NULL)
7065 channel_clear(channel);
7066 if (job != NULL)
7067 {
7068 job->jv_channel = NULL;
7069 job_cleanup(job);
7070 }
7071 term->tl_job = NULL;
7072 if (jo != NULL)
7073 CloseHandle(jo);
7074 if (term->tl_winpty != NULL)
7075 winpty_free(term->tl_winpty);
7076 term->tl_winpty = NULL;
7077 if (term->tl_winpty_config != NULL)
7078 winpty_config_free(term->tl_winpty_config);
7079 term->tl_winpty_config = NULL;
7080 if (winpty_err != NULL)
7081 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007082 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007083 (short_u *)winpty_error_msg(winpty_err), NULL);
7084
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007085 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007086 winpty_error_free(winpty_err);
7087 }
7088 return FAIL;
7089}
7090
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007091/*
7092 * Create a new terminal of "rows" by "cols" cells.
7093 * Store a reference in "term".
7094 * Return OK or FAIL.
7095 */
7096 static int
7097term_and_job_init(
7098 term_T *term,
7099 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007100 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007101 jobopt_T *opt,
7102 jobopt_T *orig_opt)
7103{
7104 int use_winpty = FALSE;
7105 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007106 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007107
7108 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7109 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7110
7111 if (!has_winpty && !has_conpty)
7112 // If neither is available give the errors for winpty, since when
7113 // conpty is not available it can't be installed either.
7114 return dyn_winpty_init(TRUE);
7115
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007116 if (opt->jo_tty_type != NUL)
7117 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007118
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007119 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007120 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007121 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007122 use_conpty = TRUE;
7123 else if (has_winpty)
7124 use_winpty = TRUE;
7125 // else: error
7126 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007127 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007128 {
7129 if (has_winpty)
7130 use_winpty = TRUE;
7131 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007132 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007133 {
7134 if (has_conpty)
7135 use_conpty = TRUE;
7136 else
7137 return dyn_conpty_init(TRUE);
7138 }
7139
7140 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007141 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007142
7143 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007144 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007145
7146 // error
7147 return dyn_winpty_init(TRUE);
7148}
7149
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007150 static int
7151create_pty_only(term_T *term, jobopt_T *options)
7152{
7153 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7154 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7155 char in_name[80], out_name[80];
7156 channel_T *channel = NULL;
7157
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007158 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7159 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007160
7161 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7162 GetCurrentProcessId(),
7163 curbuf->b_fnum);
7164 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7165 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7166 PIPE_UNLIMITED_INSTANCES,
7167 0, 0, NMPWAIT_NOWAIT, NULL);
7168 if (hPipeIn == INVALID_HANDLE_VALUE)
7169 goto failed;
7170
7171 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7172 GetCurrentProcessId(),
7173 curbuf->b_fnum);
7174 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7175 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7176 PIPE_UNLIMITED_INSTANCES,
7177 0, 0, 0, NULL);
7178 if (hPipeOut == INVALID_HANDLE_VALUE)
7179 goto failed;
7180
7181 ConnectNamedPipe(hPipeIn, NULL);
7182 ConnectNamedPipe(hPipeOut, NULL);
7183
7184 term->tl_job = job_alloc();
7185 if (term->tl_job == NULL)
7186 goto failed;
7187 ++term->tl_job->jv_refcount;
7188
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007189 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007190 term->tl_job->jv_status = JOB_FINISHED;
7191
7192 channel = add_channel();
7193 if (channel == NULL)
7194 goto failed;
7195 term->tl_job->jv_channel = channel;
7196 channel->ch_keep_open = TRUE;
7197 channel->ch_named_pipe = TRUE;
7198
7199 channel_set_pipes(channel,
7200 (sock_T)hPipeIn,
7201 (sock_T)hPipeOut,
7202 (sock_T)hPipeOut);
7203 channel_set_job(channel, term->tl_job, options);
7204 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7205 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7206
7207 return OK;
7208
7209failed:
7210 if (hPipeIn != NULL)
7211 CloseHandle(hPipeIn);
7212 if (hPipeOut != NULL)
7213 CloseHandle(hPipeOut);
7214 return FAIL;
7215}
7216
7217/*
7218 * Free the terminal emulator part of "term".
7219 */
7220 static void
7221term_free_vterm(term_T *term)
7222{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007223 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007224 if (term->tl_winpty != NULL)
7225 winpty_free(term->tl_winpty);
7226 term->tl_winpty = NULL;
7227 if (term->tl_winpty_config != NULL)
7228 winpty_config_free(term->tl_winpty_config);
7229 term->tl_winpty_config = NULL;
7230 if (term->tl_vterm != NULL)
7231 vterm_free(term->tl_vterm);
7232 term->tl_vterm = NULL;
7233}
7234
7235/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007236 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007237 */
7238 static void
7239term_report_winsize(term_T *term, int rows, int cols)
7240{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007241 if (term->tl_conpty)
7242 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007243 if (term->tl_winpty)
7244 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7245}
7246
7247 int
7248terminal_enabled(void)
7249{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007250 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007251}
7252
7253# else
7254
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007255///////////////////////////////////////
7256// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007257
7258/*
7259 * Create a new terminal of "rows" by "cols" cells.
7260 * Start job for "cmd".
7261 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007262 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007263 * Return OK or FAIL.
7264 */
7265 static int
7266term_and_job_init(
7267 term_T *term,
7268 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007269 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007270 jobopt_T *opt,
7271 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007272{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007273 term->tl_arg0_cmd = NULL;
7274
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007275 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7276 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007277
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007278#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7279 if (opt->jo_set2 & JO2_ANSI_COLORS)
7280 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7281 else
7282 init_vterm_ansi_colors(term->tl_vterm);
7283#endif
7284
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007285 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007286 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007287 if (term->tl_job != NULL)
7288 ++term->tl_job->jv_refcount;
7289
7290 return term->tl_job != NULL
7291 && term->tl_job->jv_channel != NULL
7292 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7293}
7294
7295 static int
7296create_pty_only(term_T *term, jobopt_T *opt)
7297{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007298 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7299 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007300
7301 term->tl_job = job_alloc();
7302 if (term->tl_job == NULL)
7303 return FAIL;
7304 ++term->tl_job->jv_refcount;
7305
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007306 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007307 term->tl_job->jv_status = JOB_FINISHED;
7308
7309 return mch_create_pty_channel(term->tl_job, opt);
7310}
7311
7312/*
7313 * Free the terminal emulator part of "term".
7314 */
7315 static void
7316term_free_vterm(term_T *term)
7317{
7318 if (term->tl_vterm != NULL)
7319 vterm_free(term->tl_vterm);
7320 term->tl_vterm = NULL;
7321}
7322
7323/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007324 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007325 */
7326 static void
7327term_report_winsize(term_T *term, int rows, int cols)
7328{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007329 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007330 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7331 {
7332 int fd = -1;
7333 int part;
7334
7335 for (part = PART_OUT; part < PART_COUNT; ++part)
7336 {
7337 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007338 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007339 break;
7340 }
7341 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7342 mch_signal_job(term->tl_job, (char_u *)"winch");
7343 }
7344}
7345
7346# endif
7347
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007348#endif // FEAT_TERMINAL