blob: 24779d9c2d43f73602dfc898f8ef9a483ccc5144 [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
Bram Moolenaar36968af2021-11-15 17:13:11 +00001133term_write_job_output(term_T *term, char_u *msg_arg, size_t len_arg)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001134{
Bram Moolenaar36968af2021-11-15 17:13:11 +00001135 char_u *msg = msg_arg;
1136 size_t len = len_arg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001137 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001138 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar36968af2021-11-15 17:13:11 +00001139 size_t limit = term->tl_buffer->b_p_twsl * term->tl_cols * 3;
1140
1141 // Limit the length to 'termwinscroll' * cols * 3 bytes. Keep the text at
1142 // the end.
1143 if (len > limit)
1144 {
1145 char_u *p = msg + len - limit;
1146
1147 p -= (*mb_head_off)(msg, p);
1148 len -= p - msg;
1149 msg = p;
1150 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001151
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001152 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001153
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001154 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001155 if (prevlen != vterm_output_get_buffer_current(vterm))
1156 {
1157 char buf[KEY_BUF_LEN];
1158 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1159
1160 if (curlen > 0)
1161 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1162 (char_u *)buf, (int)curlen, NULL);
1163 }
1164
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001165 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001166 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1167}
1168
1169 static void
1170update_cursor(term_T *term, int redraw)
1171{
1172 if (term->tl_normal_mode)
1173 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001174#ifdef FEAT_GUI
1175 if (term->tl_system)
1176 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1177 term->tl_cursor_pos.col);
1178 else
1179#endif
1180 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001181 if (redraw)
1182 {
1183 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1184 cursor_on();
1185 out_flush();
1186#ifdef FEAT_GUI
1187 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001188 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001189 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001190 gui_mch_flush();
1191 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001192#endif
1193 }
1194}
1195
1196/*
1197 * Invoked when "msg" output from a job was received. Write it to the terminal
1198 * of "buffer".
1199 */
1200 void
1201write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1202{
1203 size_t len = STRLEN(msg);
1204 term_T *term = buffer->b_term;
1205
Bram Moolenaar4f974752019-02-17 17:44:42 +01001206#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001207 // Win32: Cannot redirect output of the job, intercept it here and write to
1208 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001209 if (term->tl_out_fd != NULL)
1210 {
1211 ch_log(channel, "Writing %d bytes to output file", (int)len);
1212 fwrite(msg, len, 1, term->tl_out_fd);
1213 return;
1214 }
1215#endif
1216
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001217 if (term->tl_vterm == NULL)
1218 {
1219 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1220 return;
1221 }
1222 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01001223 cursor_off();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001224 term_write_job_output(term, msg, len);
1225
Bram Moolenaar13568252018-03-16 20:46:58 +01001226#ifdef FEAT_GUI
1227 if (term->tl_system)
1228 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001229 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001230 update_system_term(term);
1231 update_cursor(term, TRUE);
1232 }
1233 else
1234#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001235 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1236 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001237 if (!term->tl_normal_mode)
1238 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001239 // Don't use update_screen() when editing the command line, it gets
1240 // cleared.
1241 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001242 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001243 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001244 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001245 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001246 // update_screen() can be slow, check the terminal wasn't closed
1247 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001248 if (buffer == curbuf && curbuf->b_term != NULL)
1249 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001250 }
1251 else
1252 redraw_after_callback(TRUE);
1253 }
1254}
1255
1256/*
1257 * Send a mouse position and click to the vterm
1258 */
1259 static int
1260term_send_mouse(VTerm *vterm, int button, int pressed)
1261{
1262 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001263 int row = mouse_row - W_WINROW(curwin);
1264 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001265
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001266#ifdef FEAT_PROP_POPUP
1267 if (popup_is_popup(curwin))
1268 {
1269 row -= popup_top_extra(curwin);
1270 col -= popup_left_extra(curwin);
1271 }
1272#endif
1273 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001274 if (button != 0)
1275 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001276 return TRUE;
1277}
1278
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001279static int enter_mouse_col = -1;
1280static int enter_mouse_row = -1;
1281
1282/*
1283 * Handle a mouse click, drag or release.
1284 * Return TRUE when a mouse event is sent to the terminal.
1285 */
1286 static int
1287term_mouse_click(VTerm *vterm, int key)
1288{
1289#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001290 // For modeless selection mouse drag and release events are ignored, unless
1291 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001292 static int ignore_drag_release = TRUE;
1293 VTermMouseState mouse_state;
1294
1295 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1296 if (mouse_state.flags == 0)
1297 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001298 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001299 switch (key)
1300 {
1301 case K_LEFTDRAG:
1302 case K_LEFTRELEASE:
1303 case K_RIGHTDRAG:
1304 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001305 // Ignore drag and release events when the button-down wasn't
1306 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001307 if (ignore_drag_release)
1308 {
1309 int save_mouse_col, save_mouse_row;
1310
1311 if (enter_mouse_col < 0)
1312 break;
1313
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001314 // mouse click in the window gave us focus, handle that
1315 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001316 save_mouse_col = mouse_col;
1317 save_mouse_row = mouse_row;
1318 mouse_col = enter_mouse_col;
1319 mouse_row = enter_mouse_row;
1320 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1321 mouse_col = save_mouse_col;
1322 mouse_row = save_mouse_row;
1323 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001324 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001325 case K_LEFTMOUSE:
1326 case K_RIGHTMOUSE:
1327 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1328 ignore_drag_release = TRUE;
1329 else
1330 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001331 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001332 if (clip_star.available)
1333 {
1334 int button, is_click, is_drag;
1335
1336 button = get_mouse_button(KEY2TERMCAP1(key),
1337 &is_click, &is_drag);
1338 if (mouse_model_popup() && button == MOUSE_LEFT
1339 && (mod_mask & MOD_MASK_SHIFT))
1340 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001341 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001342 button = MOUSE_RIGHT;
1343 mod_mask &= ~MOD_MASK_SHIFT;
1344 }
1345 clip_modeless(button, is_click, is_drag);
1346 }
1347 break;
1348
1349 case K_MIDDLEMOUSE:
1350 if (clip_star.available)
1351 insert_reg('*', TRUE);
1352 break;
1353 }
1354 enter_mouse_col = -1;
1355 return FALSE;
1356 }
1357#endif
1358 enter_mouse_col = -1;
1359
1360 switch (key)
1361 {
1362 case K_LEFTMOUSE:
1363 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1364 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1365 case K_LEFTRELEASE:
1366 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1367 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1368 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1369 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1370 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1371 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1372 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1373 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1374 }
1375 return TRUE;
1376}
1377
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001378/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001379 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1380 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001381 * Return the number of bytes in "buf".
1382 */
1383 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001384term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001385{
1386 VTerm *vterm = term->tl_vterm;
1387 VTermKey key = VTERM_KEY_NONE;
1388 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001389 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001390
1391 switch (c)
1392 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001393 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001394
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001395 // don't use VTERM_KEY_BACKSPACE, it always
1396 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001397 case K_BS: c = term_backspace_char; break;
1398
1399 case ESC: key = VTERM_KEY_ESCAPE; break;
1400 case K_DEL: key = VTERM_KEY_DEL; break;
1401 case K_DOWN: key = VTERM_KEY_DOWN; break;
1402 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1403 key = VTERM_KEY_DOWN; break;
1404 case K_END: key = VTERM_KEY_END; break;
1405 case K_S_END: mod = VTERM_MOD_SHIFT;
1406 key = VTERM_KEY_END; break;
1407 case K_C_END: mod = VTERM_MOD_CTRL;
1408 key = VTERM_KEY_END; break;
1409 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1410 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1411 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1412 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1413 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1414 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1415 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1416 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1417 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1418 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1419 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1420 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1421 case K_HOME: key = VTERM_KEY_HOME; break;
1422 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1423 key = VTERM_KEY_HOME; break;
1424 case K_C_HOME: mod = VTERM_MOD_CTRL;
1425 key = VTERM_KEY_HOME; break;
1426 case K_INS: key = VTERM_KEY_INS; break;
1427 case K_K0: key = VTERM_KEY_KP_0; break;
1428 case K_K1: key = VTERM_KEY_KP_1; break;
1429 case K_K2: key = VTERM_KEY_KP_2; break;
1430 case K_K3: key = VTERM_KEY_KP_3; break;
1431 case K_K4: key = VTERM_KEY_KP_4; break;
1432 case K_K5: key = VTERM_KEY_KP_5; break;
1433 case K_K6: key = VTERM_KEY_KP_6; break;
1434 case K_K7: key = VTERM_KEY_KP_7; break;
1435 case K_K8: key = VTERM_KEY_KP_8; break;
1436 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001437 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001438 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001439 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001440 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001441 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1442 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001443 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1444 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001445 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1446 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001447 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1448 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1449 case K_LEFT: key = VTERM_KEY_LEFT; break;
1450 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1451 key = VTERM_KEY_LEFT; break;
1452 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1453 key = VTERM_KEY_LEFT; break;
1454 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1455 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1456 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1457 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1458 key = VTERM_KEY_RIGHT; break;
1459 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1460 key = VTERM_KEY_RIGHT; break;
1461 case K_UP: key = VTERM_KEY_UP; break;
1462 case K_S_UP: mod = VTERM_MOD_SHIFT;
1463 key = VTERM_KEY_UP; break;
1464 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001465 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1466 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001467
Bram Moolenaara42ad572017-11-16 13:08:04 +01001468 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1469 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaard58d4f92020-07-01 15:49:29 +02001470 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1471 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001472
1473 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001474 case K_LEFTMOUSE_NM:
1475 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001476 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001477 case K_LEFTRELEASE_NM:
1478 case K_MOUSEMOVE:
1479 case K_MIDDLEMOUSE:
1480 case K_MIDDLEDRAG:
1481 case K_MIDDLERELEASE:
1482 case K_RIGHTMOUSE:
1483 case K_RIGHTDRAG:
1484 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1485 return 0;
1486 other = TRUE;
1487 break;
1488
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001489 case K_X1MOUSE: /* TODO */ return 0;
1490 case K_X1DRAG: /* TODO */ return 0;
1491 case K_X1RELEASE: /* TODO */ return 0;
1492 case K_X2MOUSE: /* TODO */ return 0;
1493 case K_X2DRAG: /* TODO */ return 0;
1494 case K_X2RELEASE: /* TODO */ return 0;
1495
1496 case K_IGNORE: return 0;
1497 case K_NOP: return 0;
1498 case K_UNDO: return 0;
1499 case K_HELP: return 0;
1500 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1501 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1502 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1503 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1504 case K_SELECT: return 0;
1505#ifdef FEAT_GUI
1506 case K_VER_SCROLLBAR: return 0;
1507 case K_HOR_SCROLLBAR: return 0;
1508#endif
1509#ifdef FEAT_GUI_TABLINE
1510 case K_TABLINE: return 0;
1511 case K_TABMENU: return 0;
1512#endif
1513#ifdef FEAT_NETBEANS_INTG
1514 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1515#endif
1516#ifdef FEAT_DND
1517 case K_DROP: return 0;
1518#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001519 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001520 case K_PS: vterm_keyboard_start_paste(vterm);
1521 other = TRUE;
1522 break;
1523 case K_PE: vterm_keyboard_end_paste(vterm);
1524 other = TRUE;
1525 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001526 }
1527
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001528 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001529 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001530 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001531 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001532 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001533 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001534 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001535
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001536 /*
1537 * Convert special keys to vterm keys:
1538 * - Write keys to vterm: vterm_keyboard_key()
1539 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001540 */
1541 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001542 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001543 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001544 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001545 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001546 vterm_keyboard_unichar(vterm, c, mod);
1547
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001548 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001549 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1550}
1551
1552/*
1553 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001554 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001555 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001556 */
1557 static int
1558term_job_running_check(term_T *term, int check_job_status)
1559{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001560 // Also consider the job finished when the channel is closed, to avoid a
1561 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001562 if (term != NULL
1563 && term->tl_job != NULL
1564 && channel_is_open(term->tl_job->jv_channel))
1565 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001566 job_T *job = term->tl_job;
1567
1568 // Careful: Checking the job status may invoked callbacks, which close
1569 // the buffer and terminate "term". However, "job" will not be freed
1570 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001571 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001572 job_status(job);
1573 return (job->jv_status == JOB_STARTED
1574 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001575 }
1576 return FALSE;
1577}
1578
1579/*
1580 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001581 */
1582 int
1583term_job_running(term_T *term)
1584{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001585 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001586}
1587
1588/*
1589 * Return TRUE if "term" has an active channel and used ":term NONE".
1590 */
1591 int
1592term_none_open(term_T *term)
1593{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001594 // Also consider the job finished when the channel is closed, to avoid a
1595 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001596 return term != NULL
1597 && term->tl_job != NULL
1598 && channel_is_open(term->tl_job->jv_channel)
1599 && term->tl_job->jv_channel->ch_keep_open;
1600}
1601
1602/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001603 * Used when exiting: kill the job in "buf" if so desired.
1604 * Return OK when the job finished.
1605 * Return FAIL when the job is still running.
1606 */
1607 int
1608term_try_stop_job(buf_T *buf)
1609{
1610 int count;
1611 char *how = (char *)buf->b_term->tl_kill;
1612
1613#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001614 if ((how == NULL || *how == NUL)
1615 && (p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)))
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001616 {
1617 char_u buff[DIALOG_MSG_SIZE];
1618 int ret;
1619
Bram Moolenaar00806bc2020-11-05 19:36:38 +01001620 dialog_msg(buff, _("Kill job in \"%s\"?"), buf_get_fname(buf));
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001621 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1622 if (ret == VIM_YES)
1623 how = "kill";
1624 else if (ret == VIM_CANCEL)
1625 return FAIL;
1626 }
1627#endif
1628 if (how == NULL || *how == NUL)
1629 return FAIL;
1630
1631 job_stop(buf->b_term->tl_job, NULL, how);
1632
Bram Moolenaar9172d232019-01-29 23:06:54 +01001633 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001634 for (count = 0; count < 100; ++count)
1635 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001636 job_T *job;
1637
1638 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001639 if (!buf_valid(buf)
1640 || buf->b_term == NULL
1641 || buf->b_term->tl_job == NULL)
1642 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001643 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001644
Bram Moolenaar9172d232019-01-29 23:06:54 +01001645 // Call job_status() to update jv_status. It may cause the job to be
1646 // cleaned up but it won't be freed.
1647 job_status(job);
1648 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001649 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001650
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001651 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001652 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001653 }
1654 return FAIL;
1655}
1656
1657/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001658 * Add the last line of the scrollback buffer to the buffer in the window.
1659 */
1660 static void
1661add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1662{
1663 buf_T *buf = term->tl_buffer;
1664 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1665 linenr_T lnum = buf->b_ml.ml_line_count;
1666
Bram Moolenaar4f974752019-02-17 17:44:42 +01001667#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001668 if (!enc_utf8 && enc_codepage > 0)
1669 {
1670 WCHAR *ret = NULL;
1671 int length = 0;
1672
1673 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1674 &ret, &length);
1675 if (ret != NULL)
1676 {
1677 WideCharToMultiByte_alloc(enc_codepage, 0,
1678 ret, length, (char **)&text, &len, 0, 0);
1679 vim_free(ret);
1680 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1681 vim_free(text);
1682 }
1683 }
1684 else
1685#endif
1686 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1687 if (empty)
1688 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001689 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001690 curbuf = buf;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001691 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001692 curbuf = curwin->w_buffer;
1693 }
1694}
1695
1696 static void
1697cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1698{
1699 attr->width = cell->width;
1700 attr->attrs = cell->attrs;
1701 attr->fg = cell->fg;
1702 attr->bg = cell->bg;
1703}
1704
1705 static int
1706equal_celattr(cellattr_T *a, cellattr_T *b)
1707{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001708 // We only compare the RGB colors, ignoring the ANSI index and type.
1709 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001710 return a->fg.red == b->fg.red
1711 && a->fg.green == b->fg.green
1712 && a->fg.blue == b->fg.blue
1713 && a->bg.red == b->bg.red
1714 && a->bg.green == b->bg.green
1715 && a->bg.blue == b->bg.blue;
1716}
1717
Bram Moolenaard96ff162018-02-18 22:13:29 +01001718/*
1719 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1720 * line at this position. Otherwise at the end.
1721 */
1722 static int
1723add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1724{
1725 if (ga_grow(&term->tl_scrollback, 1) == OK)
1726 {
1727 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1728 + term->tl_scrollback.ga_len;
1729
1730 if (lnum > 0)
1731 {
1732 int i;
1733
1734 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1735 {
1736 *line = *(line - 1);
1737 --line;
1738 }
1739 }
1740 line->sb_cols = 0;
1741 line->sb_cells = NULL;
1742 line->sb_fill_attr = *fill_attr;
1743 ++term->tl_scrollback.ga_len;
1744 return OK;
1745 }
1746 return FALSE;
1747}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001748
1749/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001750 * Remove the terminal contents from the scrollback and the buffer.
1751 * Used before adding a new scrollback line or updating the buffer for lines
1752 * displayed in the terminal.
1753 */
1754 static void
1755cleanup_scrollback(term_T *term)
1756{
1757 sb_line_T *line;
1758 garray_T *gap;
1759
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001760 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001761 gap = &term->tl_scrollback;
1762 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1763 && gap->ga_len > 0)
1764 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001765 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001766 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1767 vim_free(line->sb_cells);
1768 --gap->ga_len;
1769 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001770 curbuf = curwin->w_buffer;
1771 if (curbuf == term->tl_buffer)
1772 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001773}
1774
1775/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001776 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001777 */
1778 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001779update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001780{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001781 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001782 int len;
1783 int lines_skipped = 0;
1784 VTermPos pos;
1785 VTermScreenCell cell;
1786 cellattr_T fill_attr, new_fill_attr;
1787 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001788
1789 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1790 "Adding terminal window snapshot to buffer");
1791
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001792 // First remove the lines that were appended before, they might be
1793 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001794 cleanup_scrollback(term);
1795
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001796 screen = vterm_obtain_screen(term->tl_vterm);
1797 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001798 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1799 {
1800 len = 0;
1801 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1802 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1803 && cell.chars[0] != NUL)
1804 {
1805 len = pos.col + 1;
1806 new_fill_attr = term->tl_default_color;
1807 }
1808 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001809 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001810 cell2cellattr(&cell, &new_fill_attr);
1811
1812 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1813 ++lines_skipped;
1814 else
1815 {
1816 while (lines_skipped > 0)
1817 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001818 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001819 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001820 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001821 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001822 }
1823
1824 if (len == 0)
1825 p = NULL;
1826 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001827 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001828 if ((p != NULL || len == 0)
1829 && ga_grow(&term->tl_scrollback, 1) == OK)
1830 {
1831 garray_T ga;
1832 int width;
1833 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1834 + term->tl_scrollback.ga_len;
1835
1836 ga_init2(&ga, 1, 100);
1837 for (pos.col = 0; pos.col < len; pos.col += width)
1838 {
1839 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1840 {
1841 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001842 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001843 if (ga_grow(&ga, 1) == OK)
1844 ga.ga_len += utf_char2bytes(' ',
1845 (char_u *)ga.ga_data + ga.ga_len);
1846 }
1847 else
1848 {
1849 width = cell.width;
1850
1851 cell2cellattr(&cell, &p[pos.col]);
Bram Moolenaar927495b2020-11-06 17:58:35 +01001852 if (width == 2)
1853 // second cell of double-width character has the
1854 // same attributes.
1855 p[pos.col + 1] = p[pos.col];
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001856
Bram Moolenaara79fd562018-12-20 20:47:32 +01001857 // Each character can be up to 6 bytes.
1858 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001859 {
1860 int i;
1861 int c;
1862
1863 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1864 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1865 (char_u *)ga.ga_data + ga.ga_len);
1866 }
1867 }
1868 }
1869 line->sb_cols = len;
1870 line->sb_cells = p;
1871 line->sb_fill_attr = new_fill_attr;
1872 fill_attr = new_fill_attr;
1873 ++term->tl_scrollback.ga_len;
1874
1875 if (ga_grow(&ga, 1) == FAIL)
1876 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1877 else
1878 {
1879 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1880 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1881 }
1882 ga_clear(&ga);
1883 }
1884 else
1885 vim_free(p);
1886 }
1887 }
1888
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001889 // Add trailing empty lines.
1890 for (pos.row = term->tl_scrollback.ga_len;
1891 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1892 ++pos.row)
1893 {
1894 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1895 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1896 }
1897
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001898 term->tl_dirty_snapshot = FALSE;
1899#ifdef FEAT_TIMERS
1900 term->tl_timer_set = FALSE;
1901#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001902}
1903
1904/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001905 * Loop over all windows in the current tab, and also curwin, which is not
1906 * encountered when using a terminal in a popup window.
1907 * Return TRUE if "*wp" was set to the next window.
1908 */
1909 static int
1910for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1911{
1912 if (*wp == NULL)
1913 *wp = firstwin;
1914 else if ((*wp)->w_next != NULL)
1915 *wp = (*wp)->w_next;
1916 else if (!*did_curwin)
1917 *wp = curwin;
1918 else
1919 return FALSE;
1920 if (*wp == curwin)
1921 *did_curwin = TRUE;
1922 return TRUE;
1923}
1924
1925/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001926 * If needed, add the current lines of the terminal to scrollback and to the
1927 * buffer. Called after the job has ended and when switching to
1928 * Terminal-Normal mode.
1929 * When "redraw" is TRUE redraw the windows that show the terminal.
1930 */
1931 static void
1932may_move_terminal_to_buffer(term_T *term, int redraw)
1933{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001934 if (term->tl_vterm == NULL)
1935 return;
1936
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001937 // Update the snapshot only if something changes or the buffer does not
1938 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001939 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1940 <= term->tl_scrollback_scrolled)
1941 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001942
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001943 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001944 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1945 &term->tl_default_color.fg, &term->tl_default_color.bg);
1946
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001947 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001948 {
1949 win_T *wp = NULL;
1950 int did_curwin = FALSE;
1951
1952 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001953 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001954 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001955 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001956 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1957 wp->w_cursor.col = 0;
1958 wp->w_valid = 0;
1959 if (wp->w_cursor.lnum >= wp->w_height)
1960 {
1961 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001962
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001963 if (wp->w_topline < min_topline)
1964 wp->w_topline = min_topline;
1965 }
1966 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001967 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001968 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001969 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001970}
1971
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001972#if defined(FEAT_TIMERS) || defined(PROTO)
1973/*
1974 * Check if any terminal timer expired. If so, copy text from the terminal to
1975 * the buffer.
1976 * Return the time until the next timer will expire.
1977 */
1978 int
1979term_check_timers(int next_due_arg, proftime_T *now)
1980{
1981 term_T *term;
1982 int next_due = next_due_arg;
1983
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001984 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001985 {
1986 if (term->tl_timer_set && !term->tl_normal_mode)
1987 {
1988 long this_due = proftime_time_left(&term->tl_timer_due, now);
1989
1990 if (this_due <= 1)
1991 {
1992 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001993 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001994 }
1995 else if (next_due == -1 || next_due > this_due)
1996 next_due = this_due;
1997 }
1998 }
1999
2000 return next_due;
2001}
2002#endif
2003
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002004/*
2005 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
2006 * otherwise end it.
2007 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002008 static void
2009set_terminal_mode(term_T *term, int normal_mode)
2010{
2011 term->tl_normal_mode = normal_mode;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01002012 trigger_modechanged();
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002013 if (!normal_mode)
2014 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002015 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002016 if (term->tl_buffer == curbuf)
2017 maketitle();
2018}
2019
2020/*
Bram Moolenaare2978022020-04-26 14:47:44 +02002021 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002022 * Move the vterm contents into the scrollback buffer and free the vterm.
2023 */
2024 static void
2025cleanup_vterm(term_T *term)
2026{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002027 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01002028 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002029 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002030 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002031}
2032
2033/*
2034 * Switch from Terminal-Job mode to Terminal-Normal mode.
2035 * Suspends updating the terminal window.
2036 */
2037 static void
2038term_enter_normal_mode(void)
2039{
2040 term_T *term = curbuf->b_term;
2041
Bram Moolenaar2bc79952018-05-12 20:36:24 +02002042 set_terminal_mode(term, TRUE);
2043
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002044 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02002045 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002046
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002047 // Move the window cursor to the position of the cursor in the
2048 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002049 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
2050 + term->tl_cursor_pos.row + 1;
2051 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02002052 if (coladvance(term->tl_cursor_pos.col) == FAIL)
2053 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002054 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002055
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002056 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002057 curwin->w_topline = term->tl_scrollback_scrolled + 1;
2058}
2059
2060/*
2061 * Returns TRUE if the current window contains a terminal and we are in
2062 * Terminal-Normal mode.
2063 */
2064 int
2065term_in_normal_mode(void)
2066{
2067 term_T *term = curbuf->b_term;
2068
2069 return term != NULL && term->tl_normal_mode;
2070}
2071
2072/*
2073 * Switch from Terminal-Normal mode to Terminal-Job mode.
2074 * Restores updating the terminal window.
2075 */
2076 void
2077term_enter_job_mode()
2078{
2079 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002080
2081 set_terminal_mode(term, FALSE);
2082
2083 if (term->tl_channel_closed)
2084 cleanup_vterm(term);
2085 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002086#ifdef FEAT_PROP_POPUP
2087 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002088 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002089#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002090}
2091
2092/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002093 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002094 * Note: while waiting a terminal may be closed and freed if the channel is
2095 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002096 */
2097 static int
2098term_vgetc()
2099{
2100 int c;
2101 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002102 int modify_other_keys =
2103 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002104
2105 State = TERMINAL;
2106 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002107#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002108 ctrl_break_was_pressed = FALSE;
2109#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002110 if (modify_other_keys)
2111 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002112 c = vgetc();
2113 got_int = FALSE;
2114 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002115 if (modify_other_keys)
2116 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002117 return c;
2118}
2119
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002120static int mouse_was_outside = FALSE;
2121
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002122/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002123 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002124 * Return FAIL when the key needs to be handled in Normal mode.
2125 * Return OK when the key was dropped or sent to the terminal.
2126 */
2127 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002128send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002129{
2130 char msg[KEY_BUF_LEN];
2131 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002132 int dragging_outside = FALSE;
2133
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002134 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002135 switch (c)
2136 {
2137 case NUL:
2138 case K_ZERO:
2139 if (typed)
2140 stuffcharReadbuff(c);
2141 return FAIL;
2142
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002143 case K_TABLINE:
2144 stuffcharReadbuff(c);
2145 return FAIL;
2146
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002147 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002148 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002149 return FAIL;
2150
2151 case K_LEFTDRAG:
2152 case K_MIDDLEDRAG:
2153 case K_RIGHTDRAG:
2154 case K_X1DRAG:
2155 case K_X2DRAG:
2156 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002157 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002158 case K_LEFTMOUSE:
2159 case K_LEFTMOUSE_NM:
2160 case K_LEFTRELEASE:
2161 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002162 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002163 case K_MIDDLEMOUSE:
2164 case K_MIDDLERELEASE:
2165 case K_RIGHTMOUSE:
2166 case K_RIGHTRELEASE:
2167 case K_X1MOUSE:
2168 case K_X1RELEASE:
2169 case K_X2MOUSE:
2170 case K_X2RELEASE:
2171
2172 case K_MOUSEUP:
2173 case K_MOUSEDOWN:
2174 case K_MOUSELEFT:
2175 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002176 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002177 int row = mouse_row;
2178 int col = mouse_col;
2179
2180#ifdef FEAT_PROP_POPUP
2181 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002182 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002183 row -= popup_top_extra(curwin);
2184 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002185 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002186#endif
2187 if (row < W_WINROW(curwin)
2188 || row >= (W_WINROW(curwin) + curwin->w_height)
2189 || col < curwin->w_wincol
2190 || col >= W_ENDCOL(curwin)
2191 || dragging_outside)
2192 {
2193 // click or scroll outside the current window or on status
2194 // line or vertical separator
2195 if (typed)
2196 {
2197 stuffcharReadbuff(c);
2198 mouse_was_outside = TRUE;
2199 }
2200 return FAIL;
2201 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002202 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01002203 break;
2204
2205 case K_COMMAND:
2206 return do_cmdline(NULL, getcmdkeycmd, NULL, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002207 }
2208 if (typed)
2209 mouse_was_outside = FALSE;
2210
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002211 // Convert the typed key to a sequence of bytes for the job.
2212 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002213 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002214 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002215 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2216 (char_u *)msg, (int)len, NULL);
2217
2218 return OK;
2219}
2220
2221 static void
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002222position_cursor(win_T *wp, VTermPos *pos)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002223{
2224 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2225 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002226#ifdef FEAT_PROP_POPUP
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002227 if (popup_is_popup(wp))
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002228 {
Bram Moolenaarf5452692020-11-28 21:56:06 +01002229 wp->w_wrow += popup_top_extra(wp);
2230 wp->w_wcol += popup_left_extra(wp);
Bram Moolenaar6a076442020-11-15 20:32:58 +01002231 wp->w_flags |= WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002232 }
Bram Moolenaar6a076442020-11-15 20:32:58 +01002233 else
2234 wp->w_flags &= ~(WFLAG_WCOL_OFF_ADDED | WFLAG_WROW_OFF_ADDED);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002235#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002236 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2237}
2238
2239/*
2240 * Handle CTRL-W "": send register contents to the job.
2241 */
2242 static void
2243term_paste_register(int prev_c UNUSED)
2244{
2245 int c;
2246 list_T *l;
2247 listitem_T *item;
2248 long reglen = 0;
2249 int type;
2250
2251#ifdef FEAT_CMDL_INFO
2252 if (add_to_showcmd(prev_c))
2253 if (add_to_showcmd('"'))
2254 out_flush();
2255#endif
2256 c = term_vgetc();
2257#ifdef FEAT_CMDL_INFO
2258 clear_showcmd();
2259#endif
2260 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002261 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002262 return;
2263
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002264 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002265 if (c == '=' && get_expr_register() != '=')
2266 return;
2267 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002268 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002269 return;
2270
2271 l = (list_T *)get_reg_contents(c, GREG_LIST);
2272 if (l != NULL)
2273 {
2274 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002275 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002276 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002277 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002278#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002279 char_u *tmp = s;
2280
2281 if (!enc_utf8 && enc_codepage > 0)
2282 {
2283 WCHAR *ret = NULL;
2284 int length = 0;
2285
2286 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2287 (int)STRLEN(s), &ret, &length);
2288 if (ret != NULL)
2289 {
2290 WideCharToMultiByte_alloc(CP_UTF8, 0,
2291 ret, length, (char **)&s, &length, 0, 0);
2292 vim_free(ret);
2293 }
2294 }
2295#endif
2296 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2297 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002298#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002299 if (tmp != s)
2300 vim_free(s);
2301#endif
2302
2303 if (item->li_next != NULL || type == MLINE)
2304 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2305 (char_u *)"\r", 1, NULL);
2306 }
2307 list_free(l);
2308 }
2309}
2310
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002311/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002312 * Return TRUE when waiting for a character in the terminal, the cursor of the
2313 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002314 */
2315 int
2316terminal_is_active()
2317{
2318 return in_terminal_loop != NULL;
2319}
2320
Bram Moolenaar83d47902020-03-26 20:34:00 +01002321/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002322 * Return the highight group ID for the terminal and the window.
Bram Moolenaar83d47902020-03-26 20:34:00 +01002323 */
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002324 static int
2325term_get_highlight_id(term_T *term, win_T *wp)
Bram Moolenaar83d47902020-03-26 20:34:00 +01002326{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002327 char_u *name;
2328
2329 if (wp != NULL && *wp->w_p_wcr != NUL)
2330 name = wp->w_p_wcr;
2331 else if (term->tl_highlight_name != NULL)
2332 name = term->tl_highlight_name;
2333 else
2334 name = (char_u*)"Terminal";
2335
2336 return syn_name2id(name);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002337}
2338
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002339#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002340 cursorentry_T *
2341term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2342{
2343 term_T *term = in_terminal_loop;
2344 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002345 int id;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002346 guicolor_T term_fg = INVALCOLOR;
2347 guicolor_T term_bg = INVALCOLOR;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002348
Bram Moolenaara80faa82020-04-12 19:37:17 +02002349 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002350 entry.shape = entry.mshape =
2351 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2352 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2353 SHAPE_BLOCK;
2354 entry.percentage = 20;
2355 if (term->tl_cursor_blink)
2356 {
2357 entry.blinkwait = 700;
2358 entry.blinkon = 400;
2359 entry.blinkoff = 250;
2360 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002361
Bram Moolenaar83d47902020-03-26 20:34:00 +01002362 // The highlight group overrules the defaults.
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002363 id = term_get_highlight_id(term, curwin);
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002364 if (id != 0)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002365 syn_id2colors(id, &term_fg, &term_bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002366 if (term_bg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002367 *fg = term_bg;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002368 else
2369 *fg = gui.back_pixel;
2370
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002371 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002372 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002373 if (term_fg != INVALCOLOR)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002374 *bg = term_fg;
2375 else
2376 *bg = gui.norm_pixel;
2377 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002378 else
2379 *bg = color_name2handle(term->tl_cursor_color);
2380 entry.name = "n";
2381 entry.used_for = SHAPE_CURSOR;
2382
2383 return &entry;
2384}
2385#endif
2386
Bram Moolenaard317b382018-02-08 22:33:31 +01002387 static void
2388may_output_cursor_props(void)
2389{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002390 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002391 || last_set_cursor_shape != desired_cursor_shape
2392 || last_set_cursor_blink != desired_cursor_blink)
2393 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002394 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002395 last_set_cursor_shape = desired_cursor_shape;
2396 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002397 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002398 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002399 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002400 ui_cursor_shape_forced(TRUE);
2401 else
2402 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2403 }
2404}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002405
Bram Moolenaard317b382018-02-08 22:33:31 +01002406/*
2407 * Set the cursor color and shape, if not last set to these.
2408 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002409 static void
2410may_set_cursor_props(term_T *term)
2411{
2412#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002413 // For the GUI the cursor properties are obtained with
2414 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002415 if (gui.in_use)
2416 return;
2417#endif
2418 if (in_terminal_loop == term)
2419 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002420 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002421 desired_cursor_shape = term->tl_cursor_shape;
2422 desired_cursor_blink = term->tl_cursor_blink;
2423 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002424 }
2425}
2426
Bram Moolenaard317b382018-02-08 22:33:31 +01002427/*
2428 * Reset the desired cursor properties and restore them when needed.
2429 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002430 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002431prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002432{
2433#ifdef FEAT_GUI
2434 if (gui.in_use)
2435 return;
2436#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002437 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002438 desired_cursor_shape = -1;
2439 desired_cursor_blink = -1;
2440 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002441}
2442
2443/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002444 * Returns TRUE if the current window contains a terminal and we are sending
2445 * keys to the job.
2446 * If "check_job_status" is TRUE update the job status.
2447 */
2448 static int
2449term_use_loop_check(int check_job_status)
2450{
2451 term_T *term = curbuf->b_term;
2452
2453 return term != NULL
2454 && !term->tl_normal_mode
2455 && term->tl_vterm != NULL
2456 && term_job_running_check(term, check_job_status);
2457}
2458
2459/*
2460 * Returns TRUE if the current window contains a terminal and we are sending
2461 * keys to the job.
2462 */
2463 int
2464term_use_loop(void)
2465{
2466 return term_use_loop_check(FALSE);
2467}
2468
2469/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002470 * Called when entering a window with the mouse. If this is a terminal window
2471 * we may want to change state.
2472 */
2473 void
2474term_win_entered()
2475{
2476 term_T *term = curbuf->b_term;
2477
2478 if (term != NULL)
2479 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002480 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002481 {
2482 reset_VIsual_and_resel();
2483 if (State & INSERT)
2484 stop_insert_mode = TRUE;
2485 }
2486 mouse_was_outside = FALSE;
2487 enter_mouse_col = mouse_col;
2488 enter_mouse_row = mouse_row;
2489 }
2490}
2491
2492/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002493 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2494 * Return the Ctrl-key value in that case.
2495 */
2496 static int
2497raw_c_to_ctrl(int c)
2498{
2499 if ((mod_mask & MOD_MASK_CTRL)
2500 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2501 return c & 0x1f;
2502 return c;
2503}
2504
2505/*
2506 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2507 * May set "mod_mask".
2508 */
2509 static int
2510ctrl_to_raw_c(int c)
2511{
2512 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2513 {
2514 mod_mask |= MOD_MASK_CTRL;
2515 return c + '@';
2516 }
2517 return c;
2518}
2519
2520/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002521 * Wait for input and send it to the job.
2522 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2523 * when there is no more typahead.
2524 * Return when the start of a CTRL-W command is typed or anything else that
2525 * should be handled as a Normal mode command.
2526 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2527 * the terminal was closed.
2528 */
2529 int
2530terminal_loop(int blocking)
2531{
2532 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002533 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002534 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002535 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002536#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002537 int tty_fd = curbuf->b_term->tl_job->jv_channel
2538 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002539#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002540 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002541
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002542 // Remember the terminal we are sending keys to. However, the terminal
2543 // might be closed while waiting for a character, e.g. typing "exit" in a
2544 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2545 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002546 in_terminal_loop = curbuf->b_term;
2547
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002548 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002549 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002550 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002551 if (termwinkey == Ctrl_W)
2552 termwinkey = 0;
2553 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002554 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002555 may_set_cursor_props(curbuf->b_term);
2556
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002557 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002558 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002559#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002560 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002561#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002562 // TODO: skip screen update when handling a sequence of keys.
2563 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002564 while (must_redraw != 0)
2565 if (update_screen(0) == FAIL)
2566 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002567 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002568 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002569 break;
2570
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002571 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002572 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002573
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002574 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002575 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002576 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002577 // Job finished while waiting for a character. Push back the
2578 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002579 if (raw_c != K_IGNORE)
2580 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002581 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002582 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002583 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002584 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002585 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002586
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002587#ifdef UNIX
2588 /*
2589 * The shell or another program may change the tty settings. Getting
2590 * them for every typed character is a bit of overhead, but it's needed
2591 * for the first character typed, e.g. when Vim starts in a shell.
2592 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002593 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002594 {
2595 ttyinfo_T info;
2596
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002597 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002598 if (get_tty_info(tty_fd, &info) == OK)
2599 term_backspace_char = info.backspace;
2600 }
2601#endif
2602
Bram Moolenaar4f974752019-02-17 17:44:42 +01002603#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002604 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2605 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002606 if (ctrl_break_was_pressed)
2607 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2608#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002609 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2610 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002611 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002612#ifdef FEAT_GUI
2613 && !curbuf->b_term->tl_system
2614#endif
2615 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002616 {
2617 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002618 int prev_raw_c = raw_c;
2619 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002620
2621#ifdef FEAT_CMDL_INFO
2622 if (add_to_showcmd(c))
2623 out_flush();
2624#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002625 raw_c = term_vgetc();
2626 c = raw_c_to_ctrl(raw_c);
2627
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002628#ifdef FEAT_CMDL_INFO
2629 clear_showcmd();
2630#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002631 if (!term_use_loop_check(TRUE)
2632 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002633 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002634 break;
2635
2636 if (prev_c == Ctrl_BSL)
2637 {
2638 if (c == Ctrl_N)
2639 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002640 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002641 term_enter_normal_mode();
2642 ret = FAIL;
2643 goto theend;
2644 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002645 // Send both keys to the terminal, first one here, second one
2646 // below.
2647 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2648 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002649 }
2650 else if (c == Ctrl_C)
2651 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002652 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002653 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2654 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002655 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002656 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002657 // "CTRL-W .": send CTRL-W to the job
2658 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002659 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002660 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002661 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002662 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002663 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002664 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002665 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002666 else if (c == 'N')
2667 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002668 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002669 term_enter_normal_mode();
2670 ret = FAIL;
2671 goto theend;
2672 }
2673 else if (c == '"')
2674 {
2675 term_paste_register(prev_c);
2676 continue;
2677 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002678 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002679 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002680 // space for CTRL-W, modifier, multi-byte char and NUL
2681 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002682
2683 // Put the command into the typeahead buffer, when using the
2684 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2685 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002686 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002687 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002688 ret = OK;
2689 goto theend;
2690 }
2691 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002692# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002693 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002694 {
2695 WCHAR wc;
2696 char_u mb[3];
2697
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002698 mb[0] = (unsigned)raw_c >> 8;
2699 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002700 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002701 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002702 }
2703# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002704 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002705 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002706 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002707 // We are sure to come back here, don't reset the cursor color
2708 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002709 restore_cursor = FALSE;
2710
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002711 ret = OK;
2712 goto theend;
2713 }
2714 }
2715 ret = FAIL;
2716
2717theend:
2718 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002719 if (restore_cursor)
2720 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002721
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002722 // Move a snapshot of the screen contents to the buffer, so that completion
2723 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002724 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2725 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002726
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002727 return ret;
2728}
2729
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002730 static void
2731may_toggle_cursor(term_T *term)
2732{
2733 if (in_terminal_loop == term)
2734 {
2735 if (term->tl_cursor_visible)
2736 cursor_on();
2737 else
2738 cursor_off();
2739 }
2740}
2741
2742/*
2743 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002744 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002745 */
2746 static int
2747color2index(VTermColor *color, int fg, int *boldp)
2748{
2749 int red = color->red;
2750 int blue = color->blue;
2751 int green = color->green;
2752
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002753 if (VTERM_COLOR_IS_INVALID(color))
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002754 return 0;
2755 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002756 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002757 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002758 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002759 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002760 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002761 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2762 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2763 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002764 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002765 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2766 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2767 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2768 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2769 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2770 case 10: return lookup_color(20, fg, boldp) + 1; // red
2771 case 11: return lookup_color(16, fg, boldp) + 1; // green
2772 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2773 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2774 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2775 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2776 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002777 }
2778 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002779
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002780 if (t_colors >= 256)
2781 {
2782 if (red == blue && red == green)
2783 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002784 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002785 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002786 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2787 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2788 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002789 int i;
2790
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002791 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002792 return 17; // 00/00/00
2793 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002794 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002795 for (i = 0; i < 23; ++i)
2796 if (red < cutoff[i])
2797 return i + 233;
2798 return 256;
2799 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002800 {
2801 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2802 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002803
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002804 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002805 for (ri = 0; ri < 5; ++ri)
2806 if (red < cutoff[ri])
2807 break;
2808 for (gi = 0; gi < 5; ++gi)
2809 if (green < cutoff[gi])
2810 break;
2811 for (bi = 0; bi < 5; ++bi)
2812 if (blue < cutoff[bi])
2813 break;
2814 return 17 + ri * 36 + gi * 6 + bi;
2815 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002816 }
2817 return 0;
2818}
2819
2820/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002821 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002822 */
2823 static int
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002824vtermAttr2hl(VTermScreenCellAttrs *cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002825{
2826 int attr = 0;
2827
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002828 if (cellattrs->bold)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002829 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002830 if (cellattrs->underline)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002831 attr |= HL_UNDERLINE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002832 if (cellattrs->italic)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002833 attr |= HL_ITALIC;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002834 if (cellattrs->strike)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002835 attr |= HL_STRIKETHROUGH;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002836 if (cellattrs->reverse)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002837 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002838 return attr;
2839}
2840
2841/*
2842 * Store Vterm attributes in "cell" from highlight flags.
2843 */
2844 static void
2845hl2vtermAttr(int attr, cellattr_T *cell)
2846{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002847 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002848 if (attr & HL_BOLD)
2849 cell->attrs.bold = 1;
2850 if (attr & HL_UNDERLINE)
2851 cell->attrs.underline = 1;
2852 if (attr & HL_ITALIC)
2853 cell->attrs.italic = 1;
2854 if (attr & HL_STRIKETHROUGH)
2855 cell->attrs.strike = 1;
2856 if (attr & HL_INVERSE)
2857 cell->attrs.reverse = 1;
2858}
2859
2860/*
2861 * Convert the attributes of a vterm cell into an attribute index.
2862 */
2863 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002864cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002865 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002866 win_T *wp,
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002867 VTermScreenCellAttrs *cellattrs,
2868 VTermColor *cellfg,
2869 VTermColor *cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002870{
2871 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002872 VTermColor *fg = cellfg;
2873 VTermColor *bg = cellbg;
2874 int is_default_fg = VTERM_COLOR_IS_DEFAULT_FG(fg);
2875 int is_default_bg = VTERM_COLOR_IS_DEFAULT_BG(bg);
2876
2877 if (is_default_fg || is_default_bg)
2878 {
2879 if (wp != NULL && *wp->w_p_wcr != NUL)
2880 {
2881 if (is_default_fg)
2882 fg = &wp->w_term_wincolor.fg;
2883 if (is_default_bg)
2884 bg = &wp->w_term_wincolor.bg;
2885 }
2886 else
2887 {
2888 if (is_default_fg)
2889 fg = &term->tl_default_color.fg;
2890 if (is_default_bg)
2891 bg = &term->tl_default_color.bg;
2892 }
2893 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002894
2895#ifdef FEAT_GUI
2896 if (gui.in_use)
2897 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002898 guicolor_T guifg = gui_mch_get_rgb_color(fg->red, fg->green, fg->blue);
2899 guicolor_T guibg = gui_mch_get_rgb_color(bg->red, bg->green, bg->blue);
2900 return get_gui_attr_idx(attr, guifg, guibg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002901 }
2902 else
2903#endif
2904#ifdef FEAT_TERMGUICOLORS
2905 if (p_tgc)
2906 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002907 guicolor_T tgcfg = VTERM_COLOR_IS_INVALID(fg)
2908 ? INVALCOLOR
2909 : gui_get_rgb_color_cmn(fg->red, fg->green, fg->blue);
2910 guicolor_T tgcbg = VTERM_COLOR_IS_INVALID(bg)
2911 ? INVALCOLOR
2912 : gui_get_rgb_color_cmn(bg->red, bg->green, bg->blue);
2913 return get_tgc_attr_idx(attr, tgcfg, tgcbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002914 }
2915 else
2916#endif
2917 {
2918 int bold = MAYBE;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002919 int ctermfg = color2index(fg, TRUE, &bold);
2920 int ctermbg = color2index(bg, FALSE, &bold);
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002921
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002922 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002923 if (bold == TRUE)
2924 attr |= HL_BOLD;
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002925
2926 return get_cterm_attr_idx(attr, ctermfg, ctermbg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002927 }
2928 return 0;
2929}
2930
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002931 static void
2932set_dirty_snapshot(term_T *term)
2933{
2934 term->tl_dirty_snapshot = TRUE;
2935#ifdef FEAT_TIMERS
2936 if (!term->tl_normal_mode)
2937 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002938 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002939 profile_setlimit(100L, &term->tl_timer_due);
2940 term->tl_timer_set = TRUE;
2941 }
2942#endif
2943}
2944
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002945 static int
2946handle_damage(VTermRect rect, void *user)
2947{
2948 term_T *term = (term_T *)user;
2949
2950 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2951 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002952 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002953 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002954 return 1;
2955}
2956
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002957 static void
2958term_scroll_up(term_T *term, int start_row, int count)
2959{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002960 win_T *wp = NULL;
2961 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002962 VTermColor fg, bg;
2963 VTermScreenCellAttrs attr;
2964 int clear_attr;
2965
Bram Moolenaara80faa82020-04-12 19:37:17 +02002966 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002967
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002968 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002969 {
2970 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002971 {
2972 // Set the color to clear lines with.
2973 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2974 &fg, &bg);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00002975 clear_attr = cell2attr(term, wp, &attr, &fg, &bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002976 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002977 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002978 }
2979}
2980
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002981 static int
2982handle_moverect(VTermRect dest, VTermRect src, void *user)
2983{
2984 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002985 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002986
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002987 // Scrolling up is done much more efficiently by deleting lines instead of
2988 // redrawing the text. But avoid doing this multiple times, postpone until
2989 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002990 if (dest.start_col == src.start_col
2991 && dest.end_col == src.end_col
2992 && dest.start_row < src.start_row)
2993 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002994 if (dest.start_row == 0)
2995 term->tl_postponed_scroll += count;
2996 else
2997 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002998 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02002999
3000 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3001 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003002 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003003
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003004 // Note sure if the scrolling will work correctly, let's do a complete
3005 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003006 redraw_buf_later(term->tl_buffer, NOT_VALID);
3007 return 1;
3008}
3009
3010 static int
3011handle_movecursor(
3012 VTermPos pos,
3013 VTermPos oldpos UNUSED,
3014 int visible,
3015 void *user)
3016{
3017 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003018 win_T *wp = NULL;
3019 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003020
3021 term->tl_cursor_pos = pos;
3022 term->tl_cursor_visible = visible;
3023
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003024 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003025 {
3026 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003027 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003028 }
3029 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003030 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003031
3032 return 1;
3033}
3034
3035 static int
3036handle_settermprop(
3037 VTermProp prop,
3038 VTermValue *value,
3039 void *user)
3040{
3041 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003042 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003043
3044 switch (prop)
3045 {
3046 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003047 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003048 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003049 if (strval == NULL)
3050 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003051 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003052 // a blank title isn't useful, make it empty, so that "running" is
3053 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003054 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003055 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003056 // Same as blank
3057 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003058 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003059 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3060 term->tl_title = NULL;
3061 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003062 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003063 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003064#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003065 else if (!enc_utf8 && enc_codepage > 0)
3066 {
3067 WCHAR *ret = NULL;
3068 int length = 0;
3069
3070 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003071 (char*)value->string.str,
3072 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003073 if (ret != NULL)
3074 {
3075 WideCharToMultiByte_alloc(enc_codepage, 0,
3076 ret, length, (char**)&term->tl_title,
3077 &length, 0, 0);
3078 vim_free(ret);
3079 }
3080 }
3081#endif
3082 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003083 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003084 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003085 strval = NULL;
3086 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003087 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003088 if (term == curbuf->b_term)
3089 maketitle();
3090 break;
3091
3092 case VTERM_PROP_CURSORVISIBLE:
3093 term->tl_cursor_visible = value->boolean;
3094 may_toggle_cursor(term);
3095 out_flush();
3096 break;
3097
3098 case VTERM_PROP_CURSORBLINK:
3099 term->tl_cursor_blink = value->boolean;
3100 may_set_cursor_props(term);
3101 break;
3102
3103 case VTERM_PROP_CURSORSHAPE:
3104 term->tl_cursor_shape = value->number;
3105 may_set_cursor_props(term);
3106 break;
3107
3108 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003109 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003110 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003111 if (strval == NULL)
3112 break;
3113 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003114 may_set_cursor_props(term);
3115 break;
3116
3117 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003118 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003119 term->tl_using_altscreen = value->boolean;
3120 break;
3121
3122 default:
3123 break;
3124 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003125 vim_free(strval);
3126
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003127 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003128 return 1;
3129}
3130
3131/*
3132 * The job running in the terminal resized the terminal.
3133 */
3134 static int
3135handle_resize(int rows, int cols, void *user)
3136{
3137 term_T *term = (term_T *)user;
3138 win_T *wp;
3139
3140 term->tl_rows = rows;
3141 term->tl_cols = cols;
3142 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003143 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003144 term->tl_vterm_size_changed = FALSE;
3145 else
3146 {
3147 FOR_ALL_WINDOWS(wp)
3148 {
3149 if (wp->w_buffer == term->tl_buffer)
3150 {
3151 win_setheight_win(rows, wp);
3152 win_setwidth_win(cols, wp);
3153 }
3154 }
3155 redraw_buf_later(term->tl_buffer, NOT_VALID);
3156 }
3157 return 1;
3158}
3159
3160/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003161 * If the number of lines that are stored goes over 'termscrollback' then
3162 * delete the first 10%.
3163 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3164 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003165 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003166 static void
3167limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003168{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003169 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003170 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003171 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003172 int i;
3173
3174 curbuf = term->tl_buffer;
3175 for (i = 0; i < todo; ++i)
3176 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003177 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3178 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003179 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003180 }
3181 curbuf = curwin->w_buffer;
3182
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003183 gap->ga_len -= todo;
3184 mch_memmove(gap->ga_data,
3185 (sb_line_T *)gap->ga_data + todo,
3186 sizeof(sb_line_T) * gap->ga_len);
3187 if (update_buffer)
3188 term->tl_scrollback_scrolled -= todo;
3189 }
3190}
3191
3192/*
3193 * Handle a line that is pushed off the top of the screen.
3194 */
3195 static int
3196handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3197{
3198 term_T *term = (term_T *)user;
3199 garray_T *gap;
3200 int update_buffer;
3201
3202 if (term->tl_normal_mode)
3203 {
3204 // In Terminal-Normal mode the user interacts with the buffer, thus we
3205 // must not change it. Postpone adding the scrollback lines.
3206 gap = &term->tl_scrollback_postponed;
3207 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003208 }
3209 else
3210 {
3211 // First remove the lines that were appended before, the pushed line
3212 // goes above it.
3213 cleanup_scrollback(term);
3214 gap = &term->tl_scrollback;
3215 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003216 }
3217
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003218 limit_scrollback(term, gap, update_buffer);
3219
3220 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003221 {
3222 cellattr_T *p = NULL;
3223 int len = 0;
3224 int i;
3225 int c;
3226 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003227 int text_len;
3228 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003229 sb_line_T *line;
3230 garray_T ga;
3231 cellattr_T fill_attr = term->tl_default_color;
3232
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003233 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003234 for (i = 0; i < cols; ++i)
3235 if (cells[i].chars[0] != 0)
3236 len = i + 1;
3237 else
3238 cell2cellattr(&cells[i], &fill_attr);
3239
3240 ga_init2(&ga, 1, 100);
3241 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003242 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003243 if (p != NULL)
3244 {
3245 for (col = 0; col < len; col += cells[col].width)
3246 {
3247 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3248 {
3249 ga.ga_len = 0;
3250 break;
3251 }
3252 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3253 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3254 (char_u *)ga.ga_data + ga.ga_len);
3255 cell2cellattr(&cells[col], &p[col]);
3256 }
3257 }
3258 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003259 {
3260 if (update_buffer)
3261 text = (char_u *)"";
3262 else
3263 text = vim_strsave((char_u *)"");
3264 text_len = 0;
3265 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003266 else
3267 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003268 text = ga.ga_data;
3269 text_len = ga.ga_len;
3270 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003271 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003272 if (update_buffer)
3273 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003274
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003275 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003276 line->sb_cols = len;
3277 line->sb_cells = p;
3278 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003279 if (update_buffer)
3280 {
3281 line->sb_text = NULL;
3282 ++term->tl_scrollback_scrolled;
3283 ga_clear(&ga); // free the text
3284 }
3285 else
3286 {
3287 line->sb_text = text;
3288 ga_init(&ga); // text is kept in tl_scrollback_postponed
3289 }
3290 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003291 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003292 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003293}
3294
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003295/*
3296 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3297 * received and stored in tl_scrollback_postponed.
3298 */
3299 static void
3300handle_postponed_scrollback(term_T *term)
3301{
3302 int i;
3303
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003304 if (term->tl_scrollback_postponed.ga_len == 0)
3305 return;
3306 ch_log(NULL, "Moving postponed scrollback to scrollback");
3307
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003308 // First remove the lines that were appended before, the pushed lines go
3309 // above it.
3310 cleanup_scrollback(term);
3311
3312 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3313 {
3314 char_u *text;
3315 sb_line_T *pp_line;
3316 sb_line_T *line;
3317
3318 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3319 break;
3320 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3321
3322 text = pp_line->sb_text;
3323 if (text == NULL)
3324 text = (char_u *)"";
3325 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3326 vim_free(pp_line->sb_text);
3327
3328 line = (sb_line_T *)term->tl_scrollback.ga_data
3329 + term->tl_scrollback.ga_len;
3330 line->sb_cols = pp_line->sb_cols;
3331 line->sb_cells = pp_line->sb_cells;
3332 line->sb_fill_attr = pp_line->sb_fill_attr;
3333 line->sb_text = NULL;
3334 ++term->tl_scrollback_scrolled;
3335 ++term->tl_scrollback.ga_len;
3336 }
3337
3338 ga_clear(&term->tl_scrollback_postponed);
3339 limit_scrollback(term, &term->tl_scrollback, TRUE);
3340}
3341
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003342static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003343 handle_damage, // damage
3344 handle_moverect, // moverect
3345 handle_movecursor, // movecursor
3346 handle_settermprop, // settermprop
3347 NULL, // bell
3348 handle_resize, // resize
3349 handle_pushline, // sb_pushline
3350 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003351};
3352
3353/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003354 * Do the work after the channel of a terminal was closed.
3355 * Must be called only when updating_screen is FALSE.
3356 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3357 */
3358 static int
3359term_after_channel_closed(term_T *term)
3360{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003361 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003362 if (!term->tl_normal_mode)
3363 {
3364 int fnum = term->tl_buffer->b_fnum;
3365
3366 cleanup_vterm(term);
3367
3368 if (term->tl_finish == TL_FINISH_CLOSE)
3369 {
3370 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003371 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003372#ifdef FEAT_PROP_POPUP
3373 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003374
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003375 // If this was a terminal in a popup window, go back to the
3376 // previous window.
3377 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3378 {
3379 pwin = curwin;
3380 if (win_valid(prevwin))
3381 win_enter(prevwin, FALSE);
3382 }
3383 else
3384#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003385 // If this is the last normal window: exit Vim.
3386 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3387 {
3388 exarg_T ea;
3389
Bram Moolenaara80faa82020-04-12 19:37:17 +02003390 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003391 ex_quit(&ea);
3392 return TRUE;
3393 }
3394
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003395 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003396 ch_log(NULL, "terminal job finished, closing window");
3397 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003398 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003399 if (curwin == aucmd_win)
3400 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003401 if (do_set_w_closing)
3402 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003403 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003404 if (do_set_w_closing)
3405 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003406 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003407#ifdef FEAT_PROP_POPUP
3408 if (pwin != NULL)
3409 popup_close_with_retval(pwin, 0);
3410#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003411 return TRUE;
3412 }
3413 if (term->tl_finish == TL_FINISH_OPEN
3414 && term->tl_buffer->b_nwindows == 0)
3415 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003416 char *cmd = term->tl_opencmd == NULL
3417 ? "botright sbuf %d"
3418 : (char *)term->tl_opencmd;
3419 size_t len = strlen(cmd) + 50;
3420 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003421
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003422 if (buf != NULL)
3423 {
3424 ch_log(NULL, "terminal job finished, opening window");
3425 vim_snprintf(buf, len, cmd, fnum);
3426 do_cmdline_cmd((char_u *)buf);
3427 vim_free(buf);
3428 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003429 }
3430 else
3431 ch_log(NULL, "terminal job finished");
3432 }
3433
3434 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3435 return FALSE;
3436}
3437
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003438#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3439/*
3440 * If the current window is a terminal in a popup window and the job has
3441 * finished, close the popup window and to back to the previous window.
3442 * Otherwise return FAIL.
3443 */
3444 int
3445may_close_term_popup(void)
3446{
3447 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3448 && !term_job_running(curbuf->b_term))
3449 {
3450 win_T *pwin = curwin;
3451
3452 if (win_valid(prevwin))
3453 win_enter(prevwin, FALSE);
3454 popup_close_with_retval(pwin, 0);
3455 return OK;
3456 }
3457 return FAIL;
3458}
3459#endif
3460
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003461/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003462 * Called when a channel has been closed.
3463 * If this was a channel for a terminal window then finish it up.
3464 */
3465 void
3466term_channel_closed(channel_T *ch)
3467{
3468 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003469 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003470 int did_one = FALSE;
3471
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003472 for (term = first_term; term != NULL; term = next_term)
3473 {
3474 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003475 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003476 {
3477 term->tl_channel_closed = TRUE;
3478 did_one = TRUE;
3479
Bram Moolenaard23a8232018-02-10 18:45:26 +01003480 VIM_CLEAR(term->tl_title);
3481 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003482#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003483 if (term->tl_out_fd != NULL)
3484 {
3485 fclose(term->tl_out_fd);
3486 term->tl_out_fd = NULL;
3487 }
3488#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003489
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003490 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003491 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003492 // Cannot open or close windows now. Can happen when
3493 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003494 term->tl_channel_recently_closed = TRUE;
3495 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003496 }
3497
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003498 if (term_after_channel_closed(term))
3499 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003500 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003501 }
3502
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003503 if (did_one)
3504 {
3505 redraw_statuslines();
3506
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003507 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003508 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003509 typebuf_was_filled = TRUE;
3510
3511 term = curbuf->b_term;
3512 if (term != NULL)
3513 {
3514 if (term->tl_job == ch->ch_job)
3515 maketitle();
3516 update_cursor(term, term->tl_cursor_visible);
3517 }
3518 }
3519}
3520
3521/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003522 * To be called after resetting updating_screen: handle any terminal where the
3523 * channel was closed.
3524 */
3525 void
3526term_check_channel_closed_recently()
3527{
3528 term_T *term;
3529 term_T *next_term;
3530
3531 for (term = first_term; term != NULL; term = next_term)
3532 {
3533 next_term = term->tl_next;
3534 if (term->tl_channel_recently_closed)
3535 {
3536 term->tl_channel_recently_closed = FALSE;
3537 if (term_after_channel_closed(term))
3538 // start over, the list may have changed
3539 next_term = first_term;
3540 }
3541 }
3542}
3543
3544/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003545 * Fill one screen line from a line of the terminal.
3546 * Advances "pos" to past the last column.
3547 */
3548 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003549term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003550 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003551 win_T *wp,
3552 VTermScreen *screen,
3553 VTermPos *pos,
3554 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003555{
3556 int off = screen_get_current_line_off();
3557
3558 for (pos->col = 0; pos->col < max_col; )
3559 {
3560 VTermScreenCell cell;
3561 int c;
3562
3563 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003564 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003565
3566 c = cell.chars[0];
3567 if (c == NUL)
3568 {
3569 ScreenLines[off] = ' ';
3570 if (enc_utf8)
3571 ScreenLinesUC[off] = NUL;
3572 }
3573 else
3574 {
3575 if (enc_utf8)
3576 {
3577 int i;
3578
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003579 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003580 for (i = 0; i < Screen_mco
3581 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3582 {
3583 ScreenLinesC[i][off] = cell.chars[i + 1];
3584 if (cell.chars[i + 1] == 0)
3585 break;
3586 }
3587 if (c >= 0x80 || (Screen_mco > 0
3588 && ScreenLinesC[0][off] != 0))
3589 {
3590 ScreenLines[off] = ' ';
3591 ScreenLinesUC[off] = c;
3592 }
3593 else
3594 {
3595 ScreenLines[off] = c;
3596 ScreenLinesUC[off] = NUL;
3597 }
3598 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003599#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003600 else if (has_mbyte && c >= 0x80)
3601 {
3602 char_u mb[MB_MAXBYTES+1];
3603 WCHAR wc = c;
3604
3605 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3606 (char*)mb, 2, 0, 0) > 1)
3607 {
3608 ScreenLines[off] = mb[0];
3609 ScreenLines[off + 1] = mb[1];
3610 cell.width = mb_ptr2cells(mb);
3611 }
3612 else
3613 ScreenLines[off] = c;
3614 }
3615#endif
3616 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003617 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003618 ScreenLines[off] = c;
3619 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003620 ScreenAttrs[off] = cell2attr(term, wp, &cell.attrs, &cell.fg,
3621 &cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003622
3623 ++pos->col;
3624 ++off;
3625 if (cell.width == 2)
3626 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003627 // don't set the second byte to NUL for a DBCS encoding, it
3628 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003629 if (enc_utf8)
3630 {
3631 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003632 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003633 }
3634 else if (!has_mbyte)
3635 {
3636 // Can't show a double-width character with a single-byte
3637 // 'encoding', just use a space.
3638 ScreenLines[off] = ' ';
3639 ScreenAttrs[off] = ScreenAttrs[off - 1];
3640 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003641
3642 ++pos->col;
3643 ++off;
3644 }
3645 }
3646}
3647
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003648#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003649 static void
3650update_system_term(term_T *term)
3651{
3652 VTermPos pos;
3653 VTermScreen *screen;
3654
3655 if (term->tl_vterm == NULL)
3656 return;
3657 screen = vterm_obtain_screen(term->tl_vterm);
3658
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003659 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003660 while (term->tl_toprow > 0
3661 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3662 {
3663 int save_p_more = p_more;
3664
3665 p_more = FALSE;
3666 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003667 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003668 p_more = save_p_more;
3669 --term->tl_toprow;
3670 }
3671
3672 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3673 && pos.row < Rows; ++pos.row)
3674 {
3675 if (pos.row < term->tl_rows)
3676 {
3677 int max_col = MIN(Columns, term->tl_cols);
3678
Bram Moolenaar83d47902020-03-26 20:34:00 +01003679 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003680 }
3681 else
3682 pos.col = 0;
3683
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003684 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003685 }
3686
3687 term->tl_dirty_row_start = MAX_ROW;
3688 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003689}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003690#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003691
3692/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003693 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3694 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003695 * Terminal-Normal mode.
3696 */
3697 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003698term_do_update_window(win_T *wp)
3699{
3700 term_T *term = wp->w_buffer->b_term;
3701
3702 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3703}
3704
3705/*
3706 * Called to update a window that contains an active terminal.
3707 */
3708 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003709term_update_window(win_T *wp)
3710{
3711 term_T *term = wp->w_buffer->b_term;
3712 VTerm *vterm;
3713 VTermScreen *screen;
3714 VTermState *state;
3715 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003716 int rows, cols;
3717 int newrows, newcols;
3718 int minsize;
3719 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003720
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003721 vterm = term->tl_vterm;
3722 screen = vterm_obtain_screen(vterm);
3723 state = vterm_obtain_state(vterm);
3724
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003725 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3726 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003727 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003728 {
3729 term->tl_dirty_row_start = 0;
3730 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003731
3732 if (term->tl_postponed_scroll > 0
3733 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003734 // Scrolling is usually faster than redrawing, when there are only
3735 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003736 term_scroll_up(term, 0, term->tl_postponed_scroll);
3737 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003738 }
3739
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003740 /*
3741 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003742 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003743 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003744 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003745
Bram Moolenaar498c2562018-04-15 23:45:15 +02003746 newrows = 99999;
3747 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003748 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003749 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003750 // Always use curwin, it may be a popup window.
3751 win_T *wwp = twp == NULL ? curwin : twp;
3752
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003753 // When more than one window shows the same terminal, use the
3754 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003755 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003756 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003757 newrows = MIN(newrows, wwp->w_height);
3758 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003759 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003760 if (twp == NULL)
3761 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003762 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003763 if (newrows == 99999 || newcols == 99999)
3764 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003765 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3766 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3767
Bram Moolenaareba13e42021-02-23 17:47:23 +01003768 // If no cell is visible there is no point in resizing. Also, vterm can't
3769 // handle a zero height.
3770 if (newrows == 0 || newcols == 0)
3771 return;
3772
Bram Moolenaar498c2562018-04-15 23:45:15 +02003773 if (term->tl_rows != newrows || term->tl_cols != newcols)
3774 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003775 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003776 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003777 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003778 newrows);
3779 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003780
3781 // Updating the terminal size will cause the snapshot to be cleared.
3782 // When not in terminal_loop() we need to restore it.
3783 if (term != in_terminal_loop)
3784 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003785 }
3786
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003787 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003788 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003789 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003790
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003791 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3792 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003793 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003794 if (pos.row < term->tl_rows)
3795 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003796 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003797
Bram Moolenaar83d47902020-03-26 20:34:00 +01003798 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003799 }
3800 else
3801 pos.col = 0;
3802
Bram Moolenaarf118d482018-03-13 13:14:00 +01003803 screen_line(wp->w_winrow + pos.row
3804#ifdef FEAT_MENU
3805 + winbar_height(wp)
3806#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003807 , wp->w_wincol, pos.col, wp->w_width,
3808#ifdef FEAT_PROP_POPUP
3809 popup_is_popup(wp) ? SLF_POPUP :
3810#endif
3811 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003812 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003813 term->tl_dirty_row_start = MAX_ROW;
3814 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003815}
3816
3817/*
3818 * Return TRUE if "wp" is a terminal window where the job has finished.
3819 */
3820 int
3821term_is_finished(buf_T *buf)
3822{
3823 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3824}
3825
3826/*
3827 * Return TRUE if "wp" is a terminal window where the job has finished or we
3828 * are in Terminal-Normal mode, thus we show the buffer contents.
3829 */
3830 int
3831term_show_buffer(buf_T *buf)
3832{
3833 term_T *term = buf->b_term;
3834
3835 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3836}
3837
3838/*
3839 * The current buffer is going to be changed. If there is terminal
3840 * highlighting remove it now.
3841 */
3842 void
3843term_change_in_curbuf(void)
3844{
3845 term_T *term = curbuf->b_term;
3846
3847 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3848 {
3849 free_scrollback(term);
3850 redraw_buf_later(term->tl_buffer, NOT_VALID);
3851
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003852 // The buffer is now like a normal buffer, it cannot be easily
3853 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003854 set_string_option_direct((char_u *)"buftype", -1,
3855 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3856 }
3857}
3858
3859/*
3860 * Get the screen attribute for a position in the buffer.
3861 * Use a negative "col" to get the filler background color.
3862 */
3863 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003864term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003865{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003866 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003867 term_T *term = buf->b_term;
3868 sb_line_T *line;
3869 cellattr_T *cellattr;
3870
3871 if (lnum > term->tl_scrollback.ga_len)
3872 cellattr = &term->tl_default_color;
3873 else
3874 {
3875 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3876 if (col < 0 || col >= line->sb_cols)
3877 cellattr = &line->sb_fill_attr;
3878 else
3879 cellattr = line->sb_cells + col;
3880 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003881 return cell2attr(term, wp, &cellattr->attrs, &cellattr->fg, &cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003882}
3883
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003884/*
3885 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003886 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003887 */
3888 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003889cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003890{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003891 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3892 if (rgb->index == 0)
3893 rgb->type = VTERM_COLOR_RGB;
3894 else
3895 {
3896 rgb->type = VTERM_COLOR_INDEXED;
3897 --rgb->index;
3898 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003899}
3900
3901/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00003902 * Initialize vterm color from the synID.
3903 * Returns TRUE if color is set to "fg" and "bg".
3904 * Otherwise returns FALSE.
3905 */
3906 static int
3907get_vterm_color_from_synid(int id, VTermColor *fg, VTermColor *bg)
3908{
3909#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3910 // Use the actual color for the GUI and when 'termguicolors' is set.
3911 if (0
3912# ifdef FEAT_GUI
3913 || gui.in_use
3914# endif
3915# ifdef FEAT_TERMGUICOLORS
3916 || p_tgc
3917# ifdef FEAT_VTP
3918 // Finally get INVALCOLOR on this execution path
3919 || (!p_tgc && t_colors >= 256)
3920# endif
3921# endif
3922 )
3923 {
3924 guicolor_T fg_rgb = INVALCOLOR;
3925 guicolor_T bg_rgb = INVALCOLOR;
3926
3927 if (id > 0)
3928 syn_id2colors(id, &fg_rgb, &bg_rgb);
3929
3930 if (fg_rgb != INVALCOLOR)
3931 {
3932 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3933 fg->red = (unsigned)(rgb >> 16);
3934 fg->green = (unsigned)(rgb >> 8) & 255;
3935 fg->blue = (unsigned)rgb & 255;
3936 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3937 }
3938 else
3939 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
3940
3941 if (bg_rgb != INVALCOLOR)
3942 {
3943 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
3944 bg->red = (unsigned)(rgb >> 16);
3945 bg->green = (unsigned)(rgb >> 8) & 255;
3946 bg->blue = (unsigned)rgb & 255;
3947 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
3948 }
3949 else
3950 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
3951
3952 return TRUE;
3953 }
3954 else
3955#endif
3956 if (t_colors >= 16)
3957 {
3958 int cterm_fg = -1;
3959 int cterm_bg = -1;
3960
3961 if (id > 0)
3962 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
3963
3964 if (cterm_fg >= 0)
3965 {
3966 cterm_color2vterm(cterm_fg, fg);
3967 fg->type |= VTERM_COLOR_DEFAULT_FG;
3968 }
3969 else
3970 fg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
3971
3972 if (cterm_bg >= 0)
3973 {
3974 cterm_color2vterm(cterm_bg, bg);
3975 bg->type |= VTERM_COLOR_DEFAULT_BG;
3976 }
3977 else
3978 bg->type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
3979
3980 return TRUE;
3981 }
3982
3983 return FALSE;
3984}
3985
3986 void
3987term_reset_wincolor(win_T *wp)
3988{
3989 wp->w_term_wincolor.fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
3990 wp->w_term_wincolor.bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
3991}
3992
3993/*
3994 * Cache the color of 'wincolor'.
3995 */
3996 void
3997term_update_wincolor(win_T *wp)
3998{
3999 int id = 0;
4000
4001 if (*wp->w_p_wcr != NUL)
4002 id = syn_name2id(wp->w_p_wcr);
4003 if (id == 0 || !get_vterm_color_from_synid(id, &wp->w_term_wincolor.fg,
4004 &wp->w_term_wincolor.bg))
4005 term_reset_wincolor(wp);
4006}
4007
4008/*
4009 * Called when option 'termguicolors' was set,
4010 * or when any highlight is changed.
4011 */
4012 void
4013term_update_wincolor_all()
4014{
4015 win_T *wp = NULL;
4016 int did_curwin = FALSE;
4017
4018 while (for_all_windows_and_curwin(&wp, &did_curwin))
4019 term_update_wincolor(wp);
4020}
4021
4022/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004023 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004024 */
4025 static void
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004026init_default_colors(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004027{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004028 VTermColor *fg, *bg;
4029 int fgval, bgval;
4030 int id;
4031
Bram Moolenaara80faa82020-04-12 19:37:17 +02004032 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004033 term->tl_default_color.width = 1;
4034 fg = &term->tl_default_color.fg;
4035 bg = &term->tl_default_color.bg;
4036
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004037 // Vterm uses a default black background. Set it to white when
4038 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004039 if (*p_bg == 'l')
4040 {
4041 fgval = 0;
4042 bgval = 255;
4043 }
4044 else
4045 {
4046 fgval = 255;
4047 bgval = 0;
4048 }
4049 fg->red = fg->green = fg->blue = fgval;
4050 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004051 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
4052 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004053
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004054 // The highlight group overrules the defaults.
4055 id = term_get_highlight_id(term, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004056
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004057 if (!get_vterm_color_from_synid(id, fg, bg))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004058 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004059#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004060 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004061#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004062
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004063 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004064 if (cterm_normal_fg_color > 0)
4065 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004066 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004067# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4068# ifdef VIMDLL
4069 if (!gui.in_use)
4070# endif
4071 {
4072 tmp = fg->red;
4073 fg->red = fg->blue;
4074 fg->blue = tmp;
4075 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004076# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004077 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004078# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004079 else
4080 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004081# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004082
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004083 if (cterm_normal_bg_color > 0)
4084 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004085 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004086# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4087# ifdef VIMDLL
4088 if (!gui.in_use)
4089# endif
4090 {
4091 tmp = fg->red;
4092 fg->red = fg->blue;
4093 fg->blue = tmp;
4094 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004095# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004096 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004097# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004098 else
4099 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004100# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004101 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004102}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004103
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004104#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4105/*
4106 * Set the 16 ANSI colors from array of RGB values
4107 */
4108 static void
4109set_vterm_palette(VTerm *vterm, long_u *rgb)
4110{
4111 int index = 0;
4112 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004113
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004114 for (; index < 16; index++)
4115 {
4116 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004117
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004118 color.red = (unsigned)(rgb[index] >> 16);
4119 color.green = (unsigned)(rgb[index] >> 8) & 255;
4120 color.blue = (unsigned)rgb[index] & 255;
4121 vterm_state_set_palette_color(state, index, &color);
4122 }
4123}
4124
4125/*
4126 * Set the ANSI color palette from a list of colors
4127 */
4128 static int
4129set_ansi_colors_list(VTerm *vterm, list_T *list)
4130{
4131 int n = 0;
4132 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004133 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004134
Bram Moolenaarb0992022020-01-30 14:55:42 +01004135 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004136 {
4137 char_u *color_name;
4138 guicolor_T guicolor;
4139
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004140 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004141 if (color_name == NULL)
4142 return FAIL;
4143
4144 guicolor = GUI_GET_COLOR(color_name);
4145 if (guicolor == INVALCOLOR)
4146 return FAIL;
4147
4148 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4149 }
4150
4151 if (n != 16 || li != NULL)
4152 return FAIL;
4153
4154 set_vterm_palette(vterm, rgb);
4155
4156 return OK;
4157}
4158
4159/*
4160 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4161 */
4162 static void
4163init_vterm_ansi_colors(VTerm *vterm)
4164{
4165 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4166
4167 if (var != NULL
4168 && (var->di_tv.v_type != VAR_LIST
4169 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004170 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004171 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004172 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004173}
4174#endif
4175
Bram Moolenaar52acb112018-03-18 19:20:22 +01004176/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004177 * Handles a "drop" command from the job in the terminal.
4178 * "item" is the file name, "item->li_next" may have options.
4179 */
4180 static void
4181handle_drop_command(listitem_T *item)
4182{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004183 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004184 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004185 int bufnr;
4186 win_T *wp;
4187 tabpage_T *tp;
4188 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004189 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004190
4191 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4192 FOR_ALL_TAB_WINDOWS(tp, wp)
4193 {
4194 if (wp->w_buffer->b_fnum == bufnr)
4195 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004196 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004197 goto_tabpage_win(tp, wp);
4198 return;
4199 }
4200 }
4201
Bram Moolenaara80faa82020-04-12 19:37:17 +02004202 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004203
4204 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4205 && opt_item->li_tv.vval.v_dict != NULL)
4206 {
4207 dict_T *dict = opt_item->li_tv.vval.v_dict;
4208 char_u *p;
4209
Bram Moolenaar8f667172018-12-14 15:38:31 +01004210 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004211 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004212 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004213 if (p != NULL)
4214 {
4215 if (check_ff_value(p) == FAIL)
4216 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4217 else
4218 ea.force_ff = *p;
4219 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004220 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004221 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004222 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004223 if (p != NULL)
4224 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004225 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004226 if (ea.cmd != NULL)
4227 {
4228 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4229 ea.force_enc = 11;
4230 tofree = ea.cmd;
4231 }
4232 }
4233
Bram Moolenaar8f667172018-12-14 15:38:31 +01004234 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004235 if (p != NULL)
4236 get_bad_opt(p, &ea);
4237
4238 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4239 ea.force_bin = FORCE_BIN;
4240 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4241 ea.force_bin = FORCE_BIN;
4242 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4243 ea.force_bin = FORCE_NOBIN;
4244 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4245 ea.force_bin = FORCE_NOBIN;
4246 }
4247
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004248 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004249 if (ea.cmd == NULL)
4250 ea.cmd = (char_u *)"split";
4251 ea.arg = fname;
4252 ea.cmdidx = CMD_split;
4253 ex_splitview(&ea);
4254
4255 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004256}
4257
4258/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004259 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4260 */
4261 static int
4262is_permitted_term_api(char_u *func, char_u *pat)
4263{
4264 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4265}
4266
4267/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004268 * Handles a function call from the job running in a terminal.
4269 * "item" is the function name, "item->li_next" has the arguments.
4270 */
4271 static void
4272handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4273{
4274 char_u *func;
4275 typval_T argvars[2];
4276 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004277 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004278
4279 if (item->li_next == NULL)
4280 {
4281 ch_log(channel, "Missing function arguments for call");
4282 return;
4283 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004284 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004285
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004286 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004287 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004288 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004289 return;
4290 }
4291
4292 argvars[0].v_type = VAR_NUMBER;
4293 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4294 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004295 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004296 funcexe.firstline = 1L;
4297 funcexe.lastline = 1L;
4298 funcexe.evaluate = TRUE;
4299 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004300 {
4301 clear_tv(&rettv);
4302 ch_log(channel, "Function %s called", func);
4303 }
4304 else
4305 ch_log(channel, "Calling function %s failed", func);
4306}
4307
4308/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004309 * URL decoding (also know as Percent-encoding).
4310 *
4311 * Note this function currently is only used for decoding shell's
4312 * OSC 7 escape sequence which we can assume all bytes are valid
4313 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4314 * encoding bytes like 0xfe, 0xff.
4315 */
4316 static size_t
4317url_decode(const char *src, const size_t len, char_u *dst)
4318{
4319 size_t i = 0, j = 0;
4320
4321 while (i < len)
4322 {
4323 if (src[i] == '%' && i + 2 < len)
4324 {
4325 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4326 j++;
4327 i += 3;
4328 }
4329 else
4330 {
4331 dst[j] = src[i];
4332 i++;
4333 j++;
4334 }
4335 }
4336 dst[j] = '\0';
4337 return j;
4338}
4339
4340/*
4341 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4342 *
4343 * The OSC 7 sequence has the format of
4344 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4345 * and what VTerm provides via VTermStringFragment is
4346 * "file://HOSTNAME/CURRENT/DIR"
4347 */
4348 static void
4349sync_shell_dir(VTermStringFragment *frag)
4350{
4351 int offset = 7; // len of "file://" is 7
4352 char *pos = (char *)frag->str + offset;
4353 char_u *new_dir;
4354
4355 // remove HOSTNAME to get PWD
Bram Moolenaar918b0892021-05-08 20:09:24 +02004356 while (*pos != '/' && offset < (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004357 {
4358 offset += 1;
4359 pos += 1;
4360 }
4361
Bram Moolenaar918b0892021-05-08 20:09:24 +02004362 if (offset >= (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004363 {
4364 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
4365 frag->str);
4366 return;
4367 }
4368
4369 new_dir = alloc(frag->len - offset + 1);
4370 url_decode(pos, frag->len-offset, new_dir);
4371 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4372 vim_free(new_dir);
4373}
4374
4375/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004376 * Called by libvterm when it cannot recognize an OSC sequence.
4377 * We recognize a terminal API command.
4378 */
4379 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004380parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004381{
4382 term_T *term = (term_T *)user;
4383 js_read_T reader;
4384 typval_T tv;
4385 channel_T *channel = term->tl_job == NULL ? NULL
4386 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004387 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004388
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004389 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
4390 if (p_asd && command == 7)
4391 {
4392 sync_shell_dir(&frag);
4393 return 1;
4394 }
4395
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004396 if (command != 51)
4397 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004398
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004399 // Concatenate what was received until the final piece is found.
4400 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4401 {
4402 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004403 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004404 }
4405 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004406 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004407 if (!frag.final)
4408 return 1;
4409
4410 ((char *)gap->ga_data)[gap->ga_len] = 0;
4411 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004412 reader.js_fill = NULL;
4413 reader.js_used = 0;
4414 if (json_decode(&reader, &tv, 0) == OK
4415 && tv.v_type == VAR_LIST
4416 && tv.vval.v_list != NULL)
4417 {
4418 listitem_T *item = tv.vval.v_list->lv_first;
4419
4420 if (item == NULL)
4421 ch_log(channel, "Missing command");
4422 else
4423 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004424 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004425
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004426 // Make sure an invoked command doesn't delete the buffer (and the
4427 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004428 ++term->tl_buffer->b_locked;
4429
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004430 item = item->li_next;
4431 if (item == NULL)
4432 ch_log(channel, "Missing argument for %s", cmd);
4433 else if (STRCMP(cmd, "drop") == 0)
4434 handle_drop_command(item);
4435 else if (STRCMP(cmd, "call") == 0)
4436 handle_call_command(term, channel, item);
4437 else
4438 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004439 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004440 }
4441 }
4442 else
4443 ch_log(channel, "Invalid JSON received");
4444
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004445 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004446 clear_tv(&tv);
4447 return 1;
4448}
4449
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004450/*
4451 * Called by libvterm when it cannot recognize a CSI sequence.
4452 * We recognize the window position report.
4453 */
4454 static int
4455parse_csi(
4456 const char *leader UNUSED,
4457 const long args[],
4458 int argcount,
4459 const char *intermed UNUSED,
4460 char command,
4461 void *user)
4462{
4463 term_T *term = (term_T *)user;
4464 char buf[100];
4465 int len;
4466 int x = 0;
4467 int y = 0;
4468 win_T *wp;
4469
4470 // We recognize only CSI 13 t
4471 if (command != 't' || argcount != 1 || args[0] != 13)
4472 return 0; // not handled
4473
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004474 // When getting the window position is not possible or it fails it results
4475 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004476#if defined(FEAT_GUI) \
4477 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4478 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004479 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004480#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004481
4482 FOR_ALL_WINDOWS(wp)
4483 if (wp->w_buffer == term->tl_buffer)
4484 break;
4485 if (wp != NULL)
4486 {
4487#ifdef FEAT_GUI
4488 if (gui.in_use)
4489 {
4490 x += wp->w_wincol * gui.char_width;
4491 y += W_WINROW(wp) * gui.char_height;
4492 }
4493 else
4494#endif
4495 {
4496 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004497 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004498 x += wp->w_wincol * 7;
4499 y += W_WINROW(wp) * 10;
4500 }
4501 }
4502
4503 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4504 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4505 (char_u *)buf, len, NULL);
4506 return 1;
4507}
4508
Bram Moolenaard8637282020-05-20 18:41:41 +02004509static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004510 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004511 parse_csi, // csi
4512 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004513 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004514};
4515
4516/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004517 * Use Vim's allocation functions for vterm so profiling works.
4518 */
4519 static void *
4520vterm_malloc(size_t size, void *data UNUSED)
4521{
Bram Moolenaar88137392021-11-12 16:01:15 +00004522 // make sure that the length is not zero
4523 return alloc_clear(size == 0 ? 1L : size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004524}
4525
4526 static void
4527vterm_memfree(void *ptr, void *data UNUSED)
4528{
4529 vim_free(ptr);
4530}
4531
4532static VTermAllocatorFunctions vterm_allocator = {
4533 &vterm_malloc,
4534 &vterm_memfree
4535};
4536
4537/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004538 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004539 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004540 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004541 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004542create_vterm(term_T *term, int rows, int cols)
4543{
4544 VTerm *vterm;
4545 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004546 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004547 VTermValue value;
4548
Bram Moolenaar756ef112018-04-10 12:04:27 +02004549 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004550 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004551 if (vterm == NULL)
4552 return FAIL;
4553
4554 // Allocate screen and state here, so we can bail out if that fails.
4555 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004556 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004557 if (state == NULL || screen == NULL)
4558 {
4559 vterm_free(vterm);
4560 return FAIL;
4561 }
4562
Bram Moolenaar52acb112018-03-18 19:20:22 +01004563 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004564 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004565 vterm_set_utf8(vterm, 1);
4566
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004567 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004568
4569 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004570 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004571 &term->tl_default_color.fg,
4572 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004573
Bram Moolenaar9e587872019-05-13 20:27:23 +02004574 if (t_colors < 16)
4575 // Less than 16 colors: assume that bold means using a bright color for
4576 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004577 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4578
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004579 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004580 vterm_screen_reset(screen, 1 /* hard */);
4581
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004582 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004583 vterm_screen_enable_altscreen(screen, 1);
4584
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004585 // For unix do not use a blinking cursor. In an xterm this causes the
4586 // cursor to blink if it's blinking in the xterm.
4587 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004588#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004589 if (GetCaretBlinkTime() == INFINITE)
4590 value.boolean = 0;
4591 else
4592 value.boolean = 1;
4593#else
4594 value.boolean = 0;
4595#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004596 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004597 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004598
4599 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004600}
4601
4602/*
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004603 * Called when option 'background' or 'termguicolors' was set,
4604 * or when any highlight is changed.
Bram Moolenaarad431992021-05-03 20:40:38 +02004605 */
4606 void
4607term_update_colors_all(void)
4608{
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004609 term_T *term;
Bram Moolenaarad431992021-05-03 20:40:38 +02004610
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004611 FOR_ALL_TERMS(term)
4612 {
4613 if (term->tl_vterm == NULL)
4614 continue;
4615 init_default_colors(term);
4616 vterm_state_set_default_colors(
4617 vterm_obtain_state(term->tl_vterm),
4618 &term->tl_default_color.fg,
4619 &term->tl_default_color.bg);
4620 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004621}
4622
4623/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004624 * Return the text to show for the buffer name and status.
4625 */
4626 char_u *
4627term_get_status_text(term_T *term)
4628{
4629 if (term->tl_status_text == NULL)
4630 {
4631 char_u *txt;
4632 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004633 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004634
4635 if (term->tl_normal_mode)
4636 {
4637 if (term_job_running(term))
4638 txt = (char_u *)_("Terminal");
4639 else
4640 txt = (char_u *)_("Terminal-finished");
4641 }
4642 else if (term->tl_title != NULL)
4643 txt = term->tl_title;
4644 else if (term_none_open(term))
4645 txt = (char_u *)_("active");
4646 else if (term_job_running(term))
4647 txt = (char_u *)_("running");
4648 else
4649 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004650 fname = buf_get_fname(term->tl_buffer);
4651 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004652 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004653 if (term->tl_status_text != NULL)
4654 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004655 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004656 }
4657 return term->tl_status_text;
4658}
4659
4660/*
Bram Moolenaar3ad69532021-11-19 17:01:08 +00004661 * Clear the cached value of the status text.
4662 */
4663 void
4664term_clear_status_text(term_T *term)
4665{
4666 VIM_CLEAR(term->tl_status_text);
4667}
4668
4669/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004670 * Mark references in jobs of terminals.
4671 */
4672 int
4673set_ref_in_term(int copyID)
4674{
4675 int abort = FALSE;
4676 term_T *term;
4677 typval_T tv;
4678
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004679 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004680 if (term->tl_job != NULL)
4681 {
4682 tv.v_type = VAR_JOB;
4683 tv.vval.v_job = term->tl_job;
4684 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4685 }
4686 return abort;
4687}
4688
4689/*
4690 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004691 * Returns NULL when the buffer is not for a terminal window and logs a message
4692 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004693 */
4694 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004695term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004696{
4697 buf_T *buf;
4698
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004699 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004700 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004701 --emsg_off;
4702 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004703 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004704 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004705 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004706 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004707 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004708 return buf;
4709}
4710
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004711 static void
4712clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004713{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004714 CLEAR_FIELD(*cell);
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004715 cell->fg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_FG;
4716 cell->bg.type = VTERM_COLOR_INVALID | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004717}
4718
4719 static void
4720dump_term_color(FILE *fd, VTermColor *color)
4721{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004722 int index;
4723
4724 if (VTERM_COLOR_IS_INDEXED(color))
4725 index = color->index + 1;
4726 else if (color->type == 0)
4727 // use RGB values
4728 index = 255;
4729 else
4730 // default color
4731 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004732 fprintf(fd, "%02x%02x%02x%d",
4733 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004734 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004735}
4736
4737/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004738 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004739 *
4740 * Each screen cell in full is:
4741 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4742 * {characters} is a space for an empty cell
4743 * For a double-width character "+" is changed to "*" and the next cell is
4744 * skipped.
4745 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4746 * when "&" use the same as the previous cell.
4747 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4748 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4749 * {color-idx} is a number from 0 to 255
4750 *
4751 * Screen cell with same width, attributes and color as the previous one:
4752 * |{characters}
4753 *
4754 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4755 *
4756 * Repeating the previous screen cell:
4757 * @{count}
4758 */
4759 void
4760f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4761{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004762 buf_T *buf;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004763 term_T *term;
4764 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004765 int max_height = 0;
4766 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004767 stat_T st;
4768 FILE *fd;
4769 VTermPos pos;
4770 VTermScreen *screen;
4771 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004772 VTermState *state;
4773 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004774
4775 if (check_restricted() || check_secure())
4776 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004777
4778 if (in_vim9script()
4779 && (check_for_buffer_arg(argvars, 0) == FAIL
4780 || check_for_string_arg(argvars, 1) == FAIL
4781 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4782 return;
4783
4784 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004785 if (buf == NULL)
4786 return;
4787 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004788 if (term->tl_vterm == NULL)
4789 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004790 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004791 return;
4792 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004793
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004794 if (argvars[2].v_type != VAR_UNKNOWN)
4795 {
4796 dict_T *d;
4797
4798 if (argvars[2].v_type != VAR_DICT)
4799 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004800 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004801 return;
4802 }
4803 d = argvars[2].vval.v_dict;
4804 if (d != NULL)
4805 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004806 max_height = dict_get_number(d, (char_u *)"rows");
4807 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004808 }
4809 }
4810
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004811 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004812 if (fname == NULL)
4813 return;
4814 if (mch_stat((char *)fname, &st) >= 0)
4815 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004816 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004817 return;
4818 }
4819
Bram Moolenaard96ff162018-02-18 22:13:29 +01004820 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4821 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004822 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004823 return;
4824 }
4825
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004826 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004827
4828 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004829 state = vterm_obtain_state(term->tl_vterm);
4830 vterm_state_get_cursorpos(state, &cursor_pos);
4831
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004832 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4833 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004834 {
4835 int repeat = 0;
4836
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004837 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4838 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004839 {
4840 VTermScreenCell cell;
4841 int same_attr;
4842 int same_chars = TRUE;
4843 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004844 int is_cursor_pos = (pos.col == cursor_pos.col
4845 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004846
4847 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004848 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004849
4850 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4851 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004852 int c = cell.chars[i];
4853 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004854 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004855
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004856 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004857 if (i == 0)
4858 {
4859 c = (c == NUL) ? ' ' : c;
4860 pc = (pc == NUL) ? ' ' : pc;
4861 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004862 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004863 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004864 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004865 break;
4866 }
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004867 same_attr = vtermAttr2hl(&cell.attrs)
4868 == vtermAttr2hl(&prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004869 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4870 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004871 if (same_chars && cell.width == prev_cell.width && same_attr
4872 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004873 {
4874 ++repeat;
4875 }
4876 else
4877 {
4878 if (repeat > 0)
4879 {
4880 fprintf(fd, "@%d", repeat);
4881 repeat = 0;
4882 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004883 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004884
4885 if (cell.chars[0] == NUL)
4886 fputs(" ", fd);
4887 else
4888 {
4889 char_u charbuf[10];
4890 int len;
4891
4892 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4893 && cell.chars[i] != NUL; ++i)
4894 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004895 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004896 fwrite(charbuf, len, 1, fd);
4897 }
4898 }
4899
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004900 // When only the characters differ we don't write anything, the
4901 // following "|", "@" or NL will indicate using the same
4902 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004903 if (cell.width != prev_cell.width || !same_attr)
4904 {
4905 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004906 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004907 else
4908 fputs("+", fd);
4909
4910 if (same_attr)
4911 {
4912 fputs("&", fd);
4913 }
4914 else
4915 {
Bram Moolenaar87fd0922021-11-20 13:47:45 +00004916 fprintf(fd, "%d", vtermAttr2hl(&cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004917 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004918 fputs("&", fd);
4919 else
4920 {
4921 fputs("#", fd);
4922 dump_term_color(fd, &cell.fg);
4923 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004924 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004925 fputs("&", fd);
4926 else
4927 {
4928 fputs("#", fd);
4929 dump_term_color(fd, &cell.bg);
4930 }
4931 }
4932 }
4933
4934 prev_cell = cell;
4935 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004936
4937 if (cell.width == 2)
4938 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004939 }
4940 if (repeat > 0)
4941 fprintf(fd, "@%d", repeat);
4942 fputs("\n", fd);
4943 }
4944
4945 fclose(fd);
4946}
4947
4948/*
4949 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4950 */
4951 static void
4952dump_is_corrupt(garray_T *gap)
4953{
4954 ga_concat(gap, (char_u *)"CORRUPT");
4955}
4956
4957 static void
4958append_cell(garray_T *gap, cellattr_T *cell)
4959{
4960 if (ga_grow(gap, 1) == OK)
4961 {
4962 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4963 ++gap->ga_len;
4964 }
4965}
4966
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004967 static void
4968clear_cellattr(cellattr_T *cell)
4969{
4970 CLEAR_FIELD(*cell);
4971 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4972 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4973}
4974
Bram Moolenaard96ff162018-02-18 22:13:29 +01004975/*
4976 * Read the dump file from "fd" and append lines to the current buffer.
4977 * Return the cell width of the longest line.
4978 */
4979 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004980read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004981{
4982 int c;
4983 garray_T ga_text;
4984 garray_T ga_cell;
4985 char_u *prev_char = NULL;
4986 int attr = 0;
4987 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004988 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004989 term_T *term = curbuf->b_term;
4990 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004991 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004992
4993 ga_init2(&ga_text, 1, 90);
4994 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004995 clear_cellattr(&cell);
4996 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004997 cursor_pos->row = -1;
4998 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004999
5000 c = fgetc(fd);
5001 for (;;)
5002 {
5003 if (c == EOF)
5004 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02005005 if (c == '\r')
5006 {
5007 // DOS line endings? Ignore.
5008 c = fgetc(fd);
5009 }
5010 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005011 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005012 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005013 if (ga_text.ga_data == NULL)
5014 dump_is_corrupt(&ga_text);
5015 if (ga_grow(&term->tl_scrollback, 1) == OK)
5016 {
5017 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
5018 + term->tl_scrollback.ga_len;
5019
5020 if (max_cells < ga_cell.ga_len)
5021 max_cells = ga_cell.ga_len;
5022 line->sb_cols = ga_cell.ga_len;
5023 line->sb_cells = ga_cell.ga_data;
5024 line->sb_fill_attr = term->tl_default_color;
5025 ++term->tl_scrollback.ga_len;
5026 ga_init(&ga_cell);
5027
5028 ga_append(&ga_text, NUL);
5029 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5030 ga_text.ga_len, FALSE);
5031 }
5032 else
5033 ga_clear(&ga_cell);
5034 ga_text.ga_len = 0;
5035
5036 c = fgetc(fd);
5037 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005038 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005039 {
5040 int prev_len = ga_text.ga_len;
5041
Bram Moolenaar9271d052018-02-25 21:39:46 +01005042 if (c == '>')
5043 {
5044 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005045 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005046 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5047 cursor_pos->col = ga_cell.ga_len;
5048 }
5049
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005050 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005051 c = fgetc(fd);
5052 if (c != EOF)
5053 ga_append(&ga_text, c);
5054 for (;;)
5055 {
5056 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005057 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005058 || c == EOF || c == '\n')
5059 break;
5060 ga_append(&ga_text, c);
5061 }
5062
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005063 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005064 vim_free(prev_char);
5065 if (ga_text.ga_data != NULL)
5066 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5067 ga_text.ga_len - prev_len);
5068
Bram Moolenaar9271d052018-02-25 21:39:46 +01005069 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005070 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005071 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005072 }
5073 else if (c == '+' || c == '*')
5074 {
5075 int is_bg;
5076
5077 cell.width = c == '+' ? 1 : 2;
5078
5079 c = fgetc(fd);
5080 if (c == '&')
5081 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005082 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005083 c = fgetc(fd);
5084 }
5085 else if (isdigit(c))
5086 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005087 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005088 attr = 0;
5089 while (isdigit(c))
5090 {
5091 attr = attr * 10 + (c - '0');
5092 c = fgetc(fd);
5093 }
5094 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005095
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005096 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005097 for (is_bg = 0; is_bg <= 1; ++is_bg)
5098 {
5099 if (c == '&')
5100 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005101 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005102 c = fgetc(fd);
5103 }
5104 else if (c == '#')
5105 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005106 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005107
5108 c = fgetc(fd);
5109 red = hex2nr(c);
5110 c = fgetc(fd);
5111 red = (red << 4) + hex2nr(c);
5112 c = fgetc(fd);
5113 green = hex2nr(c);
5114 c = fgetc(fd);
5115 green = (green << 4) + hex2nr(c);
5116 c = fgetc(fd);
5117 blue = hex2nr(c);
5118 c = fgetc(fd);
5119 blue = (blue << 4) + hex2nr(c);
5120 c = fgetc(fd);
5121 if (!isdigit(c))
5122 dump_is_corrupt(&ga_text);
5123 while (isdigit(c))
5124 {
5125 index = index * 10 + (c - '0');
5126 c = fgetc(fd);
5127 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005128 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005129 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005130 type = VTERM_COLOR_RGB;
5131 if (index == 0)
5132 {
5133 if (is_bg)
5134 type |= VTERM_COLOR_DEFAULT_BG;
5135 else
5136 type |= VTERM_COLOR_DEFAULT_FG;
5137 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005138 }
5139 else
5140 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005141 type = VTERM_COLOR_INDEXED;
5142 index -= 1;
5143 }
5144 if (is_bg)
5145 {
5146 cell.bg.type = type;
5147 cell.bg.red = red;
5148 cell.bg.green = green;
5149 cell.bg.blue = blue;
5150 cell.bg.index = index;
5151 }
5152 else
5153 {
5154 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005155 cell.fg.red = red;
5156 cell.fg.green = green;
5157 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005158 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005159 }
5160 }
5161 else
5162 dump_is_corrupt(&ga_text);
5163 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005164 }
5165 else
5166 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005167 }
5168 else
5169 dump_is_corrupt(&ga_text);
5170
5171 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005172 if (cell.width == 2)
5173 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005174 }
5175 else if (c == '@')
5176 {
5177 if (prev_char == NULL)
5178 dump_is_corrupt(&ga_text);
5179 else
5180 {
5181 int count = 0;
5182
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005183 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005184 for (;;)
5185 {
5186 c = fgetc(fd);
5187 if (!isdigit(c))
5188 break;
5189 count = count * 10 + (c - '0');
5190 }
5191
5192 while (count-- > 0)
5193 {
5194 ga_concat(&ga_text, prev_char);
5195 append_cell(&ga_cell, &cell);
5196 }
5197 }
5198 }
5199 else
5200 {
5201 dump_is_corrupt(&ga_text);
5202 c = fgetc(fd);
5203 }
5204 }
5205
5206 if (ga_text.ga_len > 0)
5207 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005208 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005209 dump_is_corrupt(&ga_text);
5210 ga_append(&ga_text, NUL);
5211 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5212 ga_text.ga_len, FALSE);
5213 }
5214
5215 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005216 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005217 vim_free(prev_char);
5218
5219 return max_cells;
5220}
5221
5222/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005223 * Return an allocated string with at least "text_width" "=" characters and
5224 * "fname" inserted in the middle.
5225 */
5226 static char_u *
5227get_separator(int text_width, char_u *fname)
5228{
5229 int width = MAX(text_width, curwin->w_width);
5230 char_u *textline;
5231 int fname_size;
5232 char_u *p = fname;
5233 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005234 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005235
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005236 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005237 if (textline == NULL)
5238 return NULL;
5239
5240 fname_size = vim_strsize(fname);
5241 if (fname_size < width - 8)
5242 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005243 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005244 width = MAX(text_width, fname_size + 8);
5245 }
5246 else if (fname_size > width - 8)
5247 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005248 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005249 p = gettail(fname);
5250 fname_size = vim_strsize(p);
5251 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005252 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005253 while (fname_size > width - 8)
5254 {
5255 p += (*mb_ptr2len)(p);
5256 fname_size = vim_strsize(p);
5257 }
5258
5259 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5260 textline[i] = '=';
5261 textline[i++] = ' ';
5262
5263 STRCPY(textline + i, p);
5264 off = STRLEN(textline);
5265 textline[off] = ' ';
5266 for (i = 1; i < (width - fname_size) / 2; ++i)
5267 textline[off + i] = '=';
5268 textline[off + i] = NUL;
5269
5270 return textline;
5271}
5272
5273/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005274 * Common for "term_dumpdiff()" and "term_dumpload()".
5275 */
5276 static void
5277term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5278{
5279 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005280 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005281 char_u buf1[NUMBUFLEN];
5282 char_u buf2[NUMBUFLEN];
5283 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005284 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005285 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005286 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005287 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005288 char_u *textline = NULL;
5289
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005290 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005291 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005292 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005293 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005294 if (fname1 == NULL || (do_diff && fname2 == NULL))
5295 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005296 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005297 return;
5298 }
5299 fd1 = mch_fopen((char *)fname1, READBIN);
5300 if (fd1 == NULL)
5301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005302 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005303 return;
5304 }
5305 if (do_diff)
5306 {
5307 fd2 = mch_fopen((char *)fname2, READBIN);
5308 if (fd2 == NULL)
5309 {
5310 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005311 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005312 return;
5313 }
5314 }
5315
5316 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005317 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5318 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5319 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5320 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5321 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005322
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005323 if (opt.jo_term_name == NULL)
5324 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005325 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005326
Bram Moolenaar51e14382019-05-25 20:21:28 +02005327 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005328 if (fname_tofree != NULL)
5329 {
5330 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5331 opt.jo_term_name = fname_tofree;
5332 }
5333 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005334
Bram Moolenaar87abab92019-06-03 21:14:59 +02005335 if (opt.jo_bufnr_buf != NULL)
5336 {
5337 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5338
5339 // With "bufnr" argument: enter the window with this buffer and make it
5340 // empty.
5341 if (wp == NULL)
5342 semsg(_(e_invarg2), "bufnr");
5343 else
5344 {
5345 buf = curbuf;
5346 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005347 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005348 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005349 redraw_later(NOT_VALID);
5350 }
5351 }
5352 else
5353 // Create a new terminal window.
5354 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5355
Bram Moolenaard96ff162018-02-18 22:13:29 +01005356 if (buf != NULL && buf->b_term != NULL)
5357 {
5358 int i;
5359 linenr_T bot_lnum;
5360 linenr_T lnum;
5361 term_T *term = buf->b_term;
5362 int width;
5363 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005364 VTermPos cursor_pos1;
5365 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005366
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005367 init_default_colors(term);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005368
Bram Moolenaard96ff162018-02-18 22:13:29 +01005369 rettv->vval.v_number = buf->b_fnum;
5370
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005371 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005372 width = read_dump_file(fd1, &cursor_pos1);
5373
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005374 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005375 if (cursor_pos1.row >= 0)
5376 {
5377 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5378 coladvance(cursor_pos1.col);
5379 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005380
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005381 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005382 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005383
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005384 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005385 if (!do_diff)
5386 goto theend;
5387
5388 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5389
Bram Moolenaar4a696342018-04-05 18:45:26 +02005390 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005391 if (textline == NULL)
5392 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005393 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5394 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5395 vim_free(textline);
5396
5397 textline = get_separator(width, fname2);
5398 if (textline == NULL)
5399 goto theend;
5400 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5401 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005402 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005403
5404 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005405 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005406 if (width2 > width)
5407 {
5408 vim_free(textline);
5409 textline = alloc(width2 + 1);
5410 if (textline == NULL)
5411 goto theend;
5412 width = width2;
5413 textline[width] = NUL;
5414 }
5415 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5416
5417 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5418 {
5419 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5420 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005421 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005422 for (i = 0; i < width; ++i)
5423 textline[i] = '-';
5424 }
5425 else
5426 {
5427 char_u *line1;
5428 char_u *line2;
5429 char_u *p1;
5430 char_u *p2;
5431 int col;
5432 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5433 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5434 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5435 ->sb_cells;
5436
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005437 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005438 line1 = vim_strsave(ml_get(lnum));
5439 if (line1 == NULL)
5440 break;
5441 p1 = line1;
5442
5443 line2 = ml_get(lnum + bot_lnum);
5444 p2 = line2;
5445 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5446 {
5447 int len1 = utfc_ptr2len(p1);
5448 int len2 = utfc_ptr2len(p2);
5449
5450 textline[col] = ' ';
5451 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005452 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005453 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005454 else if (lnum == cursor_pos1.row + 1
5455 && col == cursor_pos1.col
5456 && (cursor_pos1.row != cursor_pos2.row
5457 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005458 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005459 textline[col] = '>';
5460 else if (lnum == cursor_pos2.row + 1
5461 && col == cursor_pos2.col
5462 && (cursor_pos1.row != cursor_pos2.row
5463 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005464 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005465 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005466 else if (cellattr1 != NULL && cellattr2 != NULL)
5467 {
5468 if ((cellattr1 + col)->width
5469 != (cellattr2 + col)->width)
5470 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005471 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005472 &(cellattr2 + col)->fg))
5473 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005474 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005475 &(cellattr2 + col)->bg))
5476 textline[col] = 'b';
Bram Moolenaar87fd0922021-11-20 13:47:45 +00005477 else if (vtermAttr2hl(&(cellattr1 + col)->attrs)
5478 != vtermAttr2hl(&((cellattr2 + col)->attrs)))
Bram Moolenaard96ff162018-02-18 22:13:29 +01005479 textline[col] = 'a';
5480 }
5481 p1 += len1;
5482 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005483 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005484 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005485
5486 while (col < width)
5487 {
5488 if (*p1 == NUL && *p2 == NUL)
5489 textline[col] = '?';
5490 else if (*p1 == NUL)
5491 {
5492 textline[col] = '+';
5493 p2 += utfc_ptr2len(p2);
5494 }
5495 else
5496 {
5497 textline[col] = '-';
5498 p1 += utfc_ptr2len(p1);
5499 }
5500 ++col;
5501 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005502
5503 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005504 }
5505 if (add_empty_scrollback(term, &term->tl_default_color,
5506 term->tl_top_diff_rows) == OK)
5507 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5508 ++bot_lnum;
5509 }
5510
5511 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5512 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005513 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005514 for (i = 0; i < width; ++i)
5515 textline[i] = '+';
5516 if (add_empty_scrollback(term, &term->tl_default_color,
5517 term->tl_top_diff_rows) == OK)
5518 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5519 ++lnum;
5520 ++bot_lnum;
5521 }
5522
5523 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005524
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005525 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005526 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005527 }
5528
5529theend:
5530 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005531 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005532 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005533 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005534 fclose(fd2);
5535}
5536
5537/*
5538 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5539 * bottom files.
5540 * Return FAIL when this is not possible.
5541 */
5542 int
5543term_swap_diff()
5544{
5545 term_T *term = curbuf->b_term;
5546 linenr_T line_count;
5547 linenr_T top_rows;
5548 linenr_T bot_rows;
5549 linenr_T bot_start;
5550 linenr_T lnum;
5551 char_u *p;
5552 sb_line_T *sb_line;
5553
5554 if (term == NULL
5555 || !term_is_finished(curbuf)
5556 || term->tl_top_diff_rows == 0
5557 || term->tl_scrollback.ga_len == 0)
5558 return FAIL;
5559
5560 line_count = curbuf->b_ml.ml_line_count;
5561 top_rows = term->tl_top_diff_rows;
5562 bot_rows = term->tl_bot_diff_rows;
5563 bot_start = line_count - bot_rows;
5564 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5565
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005566 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005567 for (lnum = 1; lnum <= top_rows; ++lnum)
5568 {
5569 p = vim_strsave(ml_get(1));
5570 if (p == NULL)
5571 return OK;
5572 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005573 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005574 vim_free(p);
5575 }
5576
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005577 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005578 for (lnum = 1; lnum <= bot_rows; ++lnum)
5579 {
5580 p = vim_strsave(ml_get(bot_start + lnum));
5581 if (p == NULL)
5582 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005583 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005584 ml_append(lnum - 1, p, 0, FALSE);
5585 vim_free(p);
5586 }
5587
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005588 // move top title to bottom
5589 p = vim_strsave(ml_get(bot_rows + 1));
5590 if (p == NULL)
5591 return OK;
5592 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005593 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005594 vim_free(p);
5595
5596 // move bottom title to top
5597 p = vim_strsave(ml_get(line_count - top_rows));
5598 if (p == NULL)
5599 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005600 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005601 ml_append(bot_rows, p, 0, FALSE);
5602 vim_free(p);
5603
Bram Moolenaard96ff162018-02-18 22:13:29 +01005604 if (top_rows == bot_rows)
5605 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005606 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005607 for (lnum = 0; lnum < top_rows; ++lnum)
5608 {
5609 sb_line_T temp;
5610
5611 temp = *(sb_line + lnum);
5612 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5613 *(sb_line + bot_start + lnum) = temp;
5614 }
5615 }
5616 else
5617 {
5618 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005619 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005620
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005621 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005622 if (temp != NULL)
5623 {
5624 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5625 mch_memmove(term->tl_scrollback.ga_data,
5626 temp + bot_start,
5627 sizeof(sb_line_T) * bot_rows);
5628 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5629 temp + top_rows,
5630 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5631 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5632 + line_count - top_rows,
5633 temp,
5634 sizeof(sb_line_T) * top_rows);
5635 vim_free(temp);
5636 }
5637 }
5638
5639 term->tl_top_diff_rows = bot_rows;
5640 term->tl_bot_diff_rows = top_rows;
5641
5642 update_screen(NOT_VALID);
5643 return OK;
5644}
5645
5646/*
5647 * "term_dumpdiff(filename, filename, options)" function
5648 */
5649 void
5650f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5651{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005652 if (in_vim9script()
5653 && (check_for_string_arg(argvars, 0) == FAIL
5654 || check_for_string_arg(argvars, 1) == FAIL
5655 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5656 return;
5657
Bram Moolenaard96ff162018-02-18 22:13:29 +01005658 term_load_dump(argvars, rettv, TRUE);
5659}
5660
5661/*
5662 * "term_dumpload(filename, options)" function
5663 */
5664 void
5665f_term_dumpload(typval_T *argvars, typval_T *rettv)
5666{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005667 if (in_vim9script()
5668 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005669 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005670 return;
5671
Bram Moolenaard96ff162018-02-18 22:13:29 +01005672 term_load_dump(argvars, rettv, FALSE);
5673}
5674
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005675/*
5676 * "term_getaltscreen(buf)" function
5677 */
5678 void
5679f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5680{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005681 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005682
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005683 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5684 return;
5685
5686 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005687 if (buf == NULL)
5688 return;
5689 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5690}
5691
5692/*
5693 * "term_getattr(attr, name)" function
5694 */
5695 void
5696f_term_getattr(typval_T *argvars, typval_T *rettv)
5697{
5698 int attr;
5699 size_t i;
5700 char_u *name;
5701
5702 static struct {
5703 char *name;
5704 int attr;
5705 } attrs[] = {
5706 {"bold", HL_BOLD},
5707 {"italic", HL_ITALIC},
5708 {"underline", HL_UNDERLINE},
5709 {"strike", HL_STRIKETHROUGH},
5710 {"reverse", HL_INVERSE},
5711 };
5712
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005713 if (in_vim9script()
5714 && (check_for_number_arg(argvars, 0) == FAIL
5715 || check_for_string_arg(argvars, 1) == FAIL))
5716 return;
5717
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005718 attr = tv_get_number(&argvars[0]);
5719 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005720 if (name == NULL)
5721 return;
5722
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005723 if (attr > HL_ALL)
5724 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02005725 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005726 if (STRCMP(name, attrs[i].name) == 0)
5727 {
5728 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5729 break;
5730 }
5731}
5732
5733/*
5734 * "term_getcursor(buf)" function
5735 */
5736 void
5737f_term_getcursor(typval_T *argvars, typval_T *rettv)
5738{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005739 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005740 term_T *term;
5741 list_T *l;
5742 dict_T *d;
5743
5744 if (rettv_list_alloc(rettv) == FAIL)
5745 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005746
5747 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5748 return;
5749
5750 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005751 if (buf == NULL)
5752 return;
5753 term = buf->b_term;
5754
5755 l = rettv->vval.v_list;
5756 list_append_number(l, term->tl_cursor_pos.row + 1);
5757 list_append_number(l, term->tl_cursor_pos.col + 1);
5758
5759 d = dict_alloc();
5760 if (d != NULL)
5761 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005762 dict_add_number(d, "visible", term->tl_cursor_visible);
5763 dict_add_number(d, "blink", blink_state_is_inverted()
5764 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5765 dict_add_number(d, "shape", term->tl_cursor_shape);
5766 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005767 list_append_dict(l, d);
5768 }
5769}
5770
5771/*
5772 * "term_getjob(buf)" function
5773 */
5774 void
5775f_term_getjob(typval_T *argvars, typval_T *rettv)
5776{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005777 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005778
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005779 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5780 return;
5781
5782 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005783 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005784 {
5785 rettv->v_type = VAR_SPECIAL;
5786 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005787 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005788 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005789
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005790 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005791 rettv->vval.v_job = buf->b_term->tl_job;
5792 if (rettv->vval.v_job != NULL)
5793 ++rettv->vval.v_job->jv_refcount;
5794}
5795
5796 static int
5797get_row_number(typval_T *tv, term_T *term)
5798{
5799 if (tv->v_type == VAR_STRING
5800 && tv->vval.v_string != NULL
5801 && STRCMP(tv->vval.v_string, ".") == 0)
5802 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005803 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005804}
5805
5806/*
5807 * "term_getline(buf, row)" function
5808 */
5809 void
5810f_term_getline(typval_T *argvars, typval_T *rettv)
5811{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005812 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005813 term_T *term;
5814 int row;
5815
5816 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005817
5818 if (in_vim9script()
5819 && (check_for_buffer_arg(argvars, 0) == FAIL
5820 || check_for_lnum_arg(argvars, 1) == FAIL))
5821 return;
5822
5823 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005824 if (buf == NULL)
5825 return;
5826 term = buf->b_term;
5827 row = get_row_number(&argvars[1], term);
5828
5829 if (term->tl_vterm == NULL)
5830 {
5831 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5832
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005833 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005834 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5835 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5836 }
5837 else
5838 {
5839 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5840 VTermRect rect;
5841 int len;
5842 char_u *p;
5843
5844 if (row < 0 || row >= term->tl_rows)
5845 return;
5846 len = term->tl_cols * MB_MAXBYTES + 1;
5847 p = alloc(len);
5848 if (p == NULL)
5849 return;
5850 rettv->vval.v_string = p;
5851
5852 rect.start_col = 0;
5853 rect.end_col = term->tl_cols;
5854 rect.start_row = row;
5855 rect.end_row = row + 1;
5856 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5857 }
5858}
5859
5860/*
5861 * "term_getscrolled(buf)" function
5862 */
5863 void
5864f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5865{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005866 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005867
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005868 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5869 return;
5870
5871 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005872 if (buf == NULL)
5873 return;
5874 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5875}
5876
5877/*
5878 * "term_getsize(buf)" function
5879 */
5880 void
5881f_term_getsize(typval_T *argvars, typval_T *rettv)
5882{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005883 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005884 list_T *l;
5885
5886 if (rettv_list_alloc(rettv) == FAIL)
5887 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005888
5889 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5890 return;
5891
5892 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005893 if (buf == NULL)
5894 return;
5895
5896 l = rettv->vval.v_list;
5897 list_append_number(l, buf->b_term->tl_rows);
5898 list_append_number(l, buf->b_term->tl_cols);
5899}
5900
5901/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005902 * "term_setsize(buf, rows, cols)" function
5903 */
5904 void
5905f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5906{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005907 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02005908 term_T *term;
5909 varnumber_T rows, cols;
5910
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005911 if (in_vim9script()
5912 && (check_for_buffer_arg(argvars, 0) == FAIL
5913 || check_for_number_arg(argvars, 1) == FAIL
5914 || check_for_number_arg(argvars, 2) == FAIL))
5915 return;
5916
5917 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005918 if (buf == NULL)
5919 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005920 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005921 return;
5922 }
5923 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005924 return;
5925 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005926 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005927 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005928 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005929 cols = cols <= 0 ? term->tl_cols : cols;
5930 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005931 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005932
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005933 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005934 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5935 term_report_winsize(term, term->tl_rows, term->tl_cols);
5936}
5937
5938/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005939 * "term_getstatus(buf)" function
5940 */
5941 void
5942f_term_getstatus(typval_T *argvars, typval_T *rettv)
5943{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005944 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005945 term_T *term;
5946 char_u val[100];
5947
5948 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005949
5950 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5951 return;
5952
5953 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005954 if (buf == NULL)
5955 return;
5956 term = buf->b_term;
5957
5958 if (term_job_running(term))
5959 STRCPY(val, "running");
5960 else
5961 STRCPY(val, "finished");
5962 if (term->tl_normal_mode)
5963 STRCAT(val, ",normal");
5964 rettv->vval.v_string = vim_strsave(val);
5965}
5966
5967/*
5968 * "term_gettitle(buf)" function
5969 */
5970 void
5971f_term_gettitle(typval_T *argvars, typval_T *rettv)
5972{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005973 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005974
5975 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005976
5977 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5978 return;
5979
5980 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005981 if (buf == NULL)
5982 return;
5983
5984 if (buf->b_term->tl_title != NULL)
5985 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5986}
5987
5988/*
5989 * "term_gettty(buf)" function
5990 */
5991 void
5992f_term_gettty(typval_T *argvars, typval_T *rettv)
5993{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005994 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005995 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005996 int num = 0;
5997
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005998 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005999 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006000 || check_for_opt_bool_arg(argvars, 1) == FAIL))
6001 return;
6002
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006003 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006004 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006005 if (buf == NULL)
6006 return;
6007 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02006008 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006009
6010 switch (num)
6011 {
6012 case 0:
6013 if (buf->b_term->tl_job != NULL)
6014 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006015 break;
6016 case 1:
6017 if (buf->b_term->tl_job != NULL)
6018 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006019 break;
6020 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006021 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006022 return;
6023 }
6024 if (p != NULL)
6025 rettv->vval.v_string = vim_strsave(p);
6026}
6027
6028/*
6029 * "term_list()" function
6030 */
6031 void
6032f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
6033{
6034 term_T *tp;
6035 list_T *l;
6036
6037 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
6038 return;
6039
6040 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006041 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02006042 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006043 if (list_append_number(l,
6044 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6045 return;
6046}
6047
6048/*
6049 * "term_scrape(buf, row)" function
6050 */
6051 void
6052f_term_scrape(typval_T *argvars, typval_T *rettv)
6053{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006054 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006055 VTermScreen *screen = NULL;
6056 VTermPos pos;
6057 list_T *l;
6058 term_T *term;
6059 char_u *p;
6060 sb_line_T *line;
6061
6062 if (rettv_list_alloc(rettv) == FAIL)
6063 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006064
6065 if (in_vim9script()
6066 && (check_for_buffer_arg(argvars, 0) == FAIL
6067 || check_for_lnum_arg(argvars, 1) == FAIL))
6068 return;
6069
6070 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006071 if (buf == NULL)
6072 return;
6073 term = buf->b_term;
6074
6075 l = rettv->vval.v_list;
6076 pos.row = get_row_number(&argvars[1], term);
6077
6078 if (term->tl_vterm != NULL)
6079 {
6080 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006081 if (screen == NULL) // can't really happen
6082 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006083 p = NULL;
6084 line = NULL;
6085 }
6086 else
6087 {
6088 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6089
6090 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6091 return;
6092 p = ml_get_buf(buf, lnum + 1, FALSE);
6093 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6094 }
6095
6096 for (pos.col = 0; pos.col < term->tl_cols; )
6097 {
6098 dict_T *dcell;
6099 int width;
6100 VTermScreenCellAttrs attrs;
6101 VTermColor fg, bg;
6102 char_u rgb[8];
6103 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6104 int off = 0;
6105 int i;
6106
6107 if (screen == NULL)
6108 {
6109 cellattr_T *cellattr;
6110 int len;
6111
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006112 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006113 if (pos.col >= line->sb_cols)
6114 break;
6115 cellattr = line->sb_cells + pos.col;
6116 width = cellattr->width;
6117 attrs = cellattr->attrs;
6118 fg = cellattr->fg;
6119 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006120 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006121 mch_memmove(mbs, p, len);
6122 mbs[len] = NUL;
6123 p += len;
6124 }
6125 else
6126 {
6127 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006128
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006129 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6130 break;
6131 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6132 {
6133 if (cell.chars[i] == 0)
6134 break;
6135 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6136 }
6137 mbs[off] = NUL;
6138 width = cell.width;
6139 attrs = cell.attrs;
6140 fg = cell.fg;
6141 bg = cell.bg;
6142 }
6143 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006144 if (dcell == NULL)
6145 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006146 list_append_dict(l, dcell);
6147
Bram Moolenaare0be1672018-07-08 16:50:37 +02006148 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006149
6150 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6151 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006152 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006153 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6154 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006155 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006156
Bram Moolenaar87fd0922021-11-20 13:47:45 +00006157 dict_add_number(dcell, "attr",
6158 cell2attr(term, NULL, &attrs, &fg, &bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006159 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006160
6161 ++pos.col;
6162 if (width == 2)
6163 ++pos.col;
6164 }
6165}
6166
6167/*
6168 * "term_sendkeys(buf, keys)" function
6169 */
6170 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006171f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006172{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006173 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006174 char_u *msg;
6175 term_T *term;
6176
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006177 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006178 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006179 || check_for_string_arg(argvars, 1) == FAIL))
6180 return;
6181
6182 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006183 if (buf == NULL)
6184 return;
6185
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006186 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006187 if (msg == NULL)
6188 return;
6189 term = buf->b_term;
6190 if (term->tl_vterm == NULL)
6191 return;
6192
6193 while (*msg != NUL)
6194 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006195 int c;
6196
6197 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6198 {
6199 c = TO_SPECIAL(msg[1], msg[2]);
6200 msg += 3;
6201 }
6202 else
6203 {
6204 c = PTR2CHAR(msg);
6205 msg += MB_CPTR2LEN(msg);
6206 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006207 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006208 }
6209}
6210
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006211#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6212/*
6213 * "term_getansicolors(buf)" function
6214 */
6215 void
6216f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6217{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006218 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006219 term_T *term;
6220 VTermState *state;
6221 VTermColor color;
6222 char_u hexbuf[10];
6223 int index;
6224 list_T *list;
6225
6226 if (rettv_list_alloc(rettv) == FAIL)
6227 return;
6228
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006229 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6230 return;
6231
6232 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006233 if (buf == NULL)
6234 return;
6235 term = buf->b_term;
6236 if (term->tl_vterm == NULL)
6237 return;
6238
6239 list = rettv->vval.v_list;
6240 state = vterm_obtain_state(term->tl_vterm);
6241 for (index = 0; index < 16; index++)
6242 {
6243 vterm_state_get_palette_color(state, index, &color);
6244 sprintf((char *)hexbuf, "#%02x%02x%02x",
6245 color.red, color.green, color.blue);
6246 if (list_append_string(list, hexbuf, 7) == FAIL)
6247 return;
6248 }
6249}
6250
6251/*
6252 * "term_setansicolors(buf, list)" function
6253 */
6254 void
6255f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6256{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006257 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006258 term_T *term;
6259
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006260 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006261 && (check_for_buffer_arg(argvars, 0) == FAIL
6262 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006263 return;
6264
6265 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006266 if (buf == NULL)
6267 return;
6268 term = buf->b_term;
6269 if (term->tl_vterm == NULL)
6270 return;
6271
6272 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6273 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006274 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006275 return;
6276 }
6277
6278 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006279 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006280}
6281#endif
6282
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006283/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006284 * "term_setapi(buf, api)" function
6285 */
6286 void
6287f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6288{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006289 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006290 term_T *term;
6291 char_u *api;
6292
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006293 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006294 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006295 || check_for_string_arg(argvars, 1) == FAIL))
6296 return;
6297
6298 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006299 if (buf == NULL)
6300 return;
6301 term = buf->b_term;
6302 vim_free(term->tl_api);
6303 api = tv_get_string_chk(&argvars[1]);
6304 if (api != NULL)
6305 term->tl_api = vim_strsave(api);
6306 else
6307 term->tl_api = NULL;
6308}
6309
6310/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006311 * "term_setrestore(buf, command)" function
6312 */
6313 void
6314f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6315{
6316#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006317 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006318 term_T *term;
6319 char_u *cmd;
6320
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006321 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006322 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006323 || check_for_string_arg(argvars, 1) == FAIL))
6324 return;
6325
6326 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006327 if (buf == NULL)
6328 return;
6329 term = buf->b_term;
6330 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006331 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006332 if (cmd != NULL)
6333 term->tl_command = vim_strsave(cmd);
6334 else
6335 term->tl_command = NULL;
6336#endif
6337}
6338
6339/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006340 * "term_setkill(buf, how)" function
6341 */
6342 void
6343f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6344{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006345 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006346 term_T *term;
6347 char_u *how;
6348
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006349 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006350 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006351 || check_for_string_arg(argvars, 1) == FAIL))
6352 return;
6353
6354 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006355 if (buf == NULL)
6356 return;
6357 term = buf->b_term;
6358 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006359 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006360 if (how != NULL)
6361 term->tl_kill = vim_strsave(how);
6362 else
6363 term->tl_kill = NULL;
6364}
6365
6366/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006367 * "term_start(command, options)" function
6368 */
6369 void
6370f_term_start(typval_T *argvars, typval_T *rettv)
6371{
6372 jobopt_T opt;
6373 buf_T *buf;
6374
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006375 if (in_vim9script()
6376 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6377 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6378 return;
6379
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006380 init_job_options(&opt);
6381 if (argvars[1].v_type != VAR_UNKNOWN
6382 && get_job_options(&argvars[1], &opt,
6383 JO_TIMEOUT_ALL + JO_STOPONEXIT
6384 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6385 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6386 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6387 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006388 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006389 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006390 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006391 return;
6392
Bram Moolenaar13568252018-03-16 20:46:58 +01006393 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006394
6395 if (buf != NULL && buf->b_term != NULL)
6396 rettv->vval.v_number = buf->b_fnum;
6397}
6398
6399/*
6400 * "term_wait" function
6401 */
6402 void
6403f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6404{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006405 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006406
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006407 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006408 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006409 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006410 return;
6411
6412 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006413 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006414 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006415 if (buf->b_term->tl_job == NULL)
6416 {
6417 ch_log(NULL, "term_wait(): no job to wait for");
6418 return;
6419 }
6420 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006421 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006422 return;
6423
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006424 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006425 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006426 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6427 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006428 // The job is dead, keep reading channel I/O until the channel is
6429 // closed. buf->b_term may become NULL if the terminal was closed while
6430 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006431 ch_log(NULL, "term_wait(): waiting for channel to close");
6432 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6433 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006434 term_flush_messages();
6435
Bram Moolenaard45aa552018-05-21 22:50:29 +02006436 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006437 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006438 // If the terminal is closed when the channel is closed the
6439 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006440 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006441 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006442
6443 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006444 }
6445 else
6446 {
6447 long wait = 10L;
6448
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006449 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006450
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006451 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006452 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006453 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006454 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006455
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006456 // Flushing messages on channels is hopefully sufficient.
6457 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006458 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006459 }
6460}
6461
6462/*
6463 * Called when a channel has sent all the lines to a terminal.
6464 * Send a CTRL-D to mark the end of the text.
6465 */
6466 void
6467term_send_eof(channel_T *ch)
6468{
6469 term_T *term;
6470
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006471 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006472 if (term->tl_job == ch->ch_job)
6473 {
6474 if (term->tl_eof_chars != NULL)
6475 {
6476 channel_send(ch, PART_IN, term->tl_eof_chars,
6477 (int)STRLEN(term->tl_eof_chars), NULL);
6478 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6479 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006480# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006481 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006482 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006483 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6484# endif
6485 }
6486}
6487
Bram Moolenaar113e1072019-01-20 15:30:40 +01006488#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006489 job_T *
6490term_getjob(term_T *term)
6491{
6492 return term != NULL ? term->tl_job : NULL;
6493}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006494#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006495
Bram Moolenaar4f974752019-02-17 17:44:42 +01006496# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006497
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006498///////////////////////////////////////
6499// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006500#ifdef PROTO
6501typedef int COORD;
6502typedef int DWORD;
6503typedef int HANDLE;
6504typedef int *DWORD_PTR;
6505typedef int HPCON;
6506typedef int HRESULT;
6507typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006508typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006509typedef int PSIZE_T;
6510typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006511typedef int BOOL;
6512# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006513#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006514
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006515HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6516HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6517HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006518BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6519BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6520void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006521
6522 static int
6523dyn_conpty_init(int verbose)
6524{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006525 static HMODULE hKerneldll = NULL;
6526 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006527 static struct
6528 {
6529 char *name;
6530 FARPROC *ptr;
6531 } conpty_entry[] =
6532 {
6533 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6534 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6535 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6536 {"InitializeProcThreadAttributeList",
6537 (FARPROC*)&pInitializeProcThreadAttributeList},
6538 {"UpdateProcThreadAttribute",
6539 (FARPROC*)&pUpdateProcThreadAttribute},
6540 {"DeleteProcThreadAttributeList",
6541 (FARPROC*)&pDeleteProcThreadAttributeList},
6542 {NULL, NULL}
6543 };
6544
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006545 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006546 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006547 if (verbose)
6548 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006549 return FAIL;
6550 }
6551
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006552 // No need to initialize twice.
6553 if (hKerneldll)
6554 return OK;
6555
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006556 hKerneldll = vimLoadLib("kernel32.dll");
6557 for (i = 0; conpty_entry[i].name != NULL
6558 && conpty_entry[i].ptr != NULL; ++i)
6559 {
6560 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6561 conpty_entry[i].name)) == NULL)
6562 {
6563 if (verbose)
6564 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006565 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006566 return FAIL;
6567 }
6568 }
6569
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006570 return OK;
6571}
6572
6573 static int
6574conpty_term_and_job_init(
6575 term_T *term,
6576 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006577 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006578 jobopt_T *opt,
6579 jobopt_T *orig_opt)
6580{
6581 WCHAR *cmd_wchar = NULL;
6582 WCHAR *cmd_wchar_copy = NULL;
6583 WCHAR *cwd_wchar = NULL;
6584 WCHAR *env_wchar = NULL;
6585 channel_T *channel = NULL;
6586 job_T *job = NULL;
6587 HANDLE jo = NULL;
6588 garray_T ga_cmd, ga_env;
6589 char_u *cmd = NULL;
6590 HRESULT hr;
6591 COORD consize;
6592 SIZE_T breq;
6593 PROCESS_INFORMATION proc_info;
6594 HANDLE i_theirs = NULL;
6595 HANDLE o_theirs = NULL;
6596 HANDLE i_ours = NULL;
6597 HANDLE o_ours = NULL;
6598
6599 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6600 ga_init2(&ga_env, (int)sizeof(char*), 20);
6601
6602 if (argvar->v_type == VAR_STRING)
6603 {
6604 cmd = argvar->vval.v_string;
6605 }
6606 else if (argvar->v_type == VAR_LIST)
6607 {
6608 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6609 goto failed;
6610 cmd = ga_cmd.ga_data;
6611 }
6612 if (cmd == NULL || *cmd == NUL)
6613 {
6614 emsg(_(e_invarg));
6615 goto failed;
6616 }
6617
6618 term->tl_arg0_cmd = vim_strsave(cmd);
6619
6620 cmd_wchar = enc_to_utf16(cmd, NULL);
6621
6622 if (cmd_wchar != NULL)
6623 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006624 // Request by CreateProcessW
6625 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006626 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006627 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6628 }
6629
6630 ga_clear(&ga_cmd);
6631 if (cmd_wchar == NULL)
6632 goto failed;
6633 if (opt->jo_cwd != NULL)
6634 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6635
6636 win32_build_env(opt->jo_env, &ga_env, TRUE);
6637 env_wchar = ga_env.ga_data;
6638
6639 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6640 goto failed;
6641 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6642 goto failed;
6643
6644 consize.X = term->tl_cols;
6645 consize.Y = term->tl_rows;
6646 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6647 &term->tl_conpty);
6648 if (FAILED(hr))
6649 goto failed;
6650
6651 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6652
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006653 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006654 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006655 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006656 if (!term->tl_siex.lpAttributeList)
6657 goto failed;
6658 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6659 0, &breq))
6660 goto failed;
6661 if (!pUpdateProcThreadAttribute(
6662 term->tl_siex.lpAttributeList, 0,
6663 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6664 sizeof(HPCON), NULL, NULL))
6665 goto failed;
6666
6667 channel = add_channel();
6668 if (channel == NULL)
6669 goto failed;
6670
6671 job = job_alloc();
6672 if (job == NULL)
6673 goto failed;
6674 if (argvar->v_type == VAR_STRING)
6675 {
6676 int argc;
6677
6678 build_argv_from_string(cmd, &job->jv_argv, &argc);
6679 }
6680 else
6681 {
6682 int argc;
6683
6684 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6685 }
6686
6687 if (opt->jo_set & JO_IN_BUF)
6688 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6689
6690 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6691 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006692 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006693 env_wchar, cwd_wchar,
6694 &term->tl_siex.StartupInfo, &proc_info))
6695 goto failed;
6696
6697 CloseHandle(i_theirs);
6698 CloseHandle(o_theirs);
6699
6700 channel_set_pipes(channel,
6701 (sock_T)i_ours,
6702 (sock_T)o_ours,
6703 (sock_T)o_ours);
6704
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006705 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006706 channel->ch_write_text_mode = TRUE;
6707
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006708 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006709 channel->ch_anonymous_pipe = TRUE;
6710
6711 jo = CreateJobObject(NULL, NULL);
6712 if (jo == NULL)
6713 goto failed;
6714
6715 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6716 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006717 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006718 CloseHandle(jo);
6719 jo = NULL;
6720 }
6721
6722 ResumeThread(proc_info.hThread);
6723 CloseHandle(proc_info.hThread);
6724
6725 vim_free(cmd_wchar);
6726 vim_free(cmd_wchar_copy);
6727 vim_free(cwd_wchar);
6728 vim_free(env_wchar);
6729
6730 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6731 goto failed;
6732
6733#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6734 if (opt->jo_set2 & JO2_ANSI_COLORS)
6735 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6736 else
6737 init_vterm_ansi_colors(term->tl_vterm);
6738#endif
6739
6740 channel_set_job(channel, job, opt);
6741 job_set_options(job, opt);
6742
6743 job->jv_channel = channel;
6744 job->jv_proc_info = proc_info;
6745 job->jv_job_object = jo;
6746 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006747 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006748 ++job->jv_refcount;
6749 term->tl_job = job;
6750
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006751 // Redirecting stdout and stderr doesn't work at the job level. Instead
6752 // open the file here and handle it in. opt->jo_io was changed in
6753 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006754 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6755 {
6756 char_u *fname = opt->jo_io_name[PART_OUT];
6757
6758 ch_log(channel, "Opening output file %s", fname);
6759 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6760 if (term->tl_out_fd == NULL)
6761 semsg(_(e_notopen), fname);
6762 }
6763
6764 return OK;
6765
6766failed:
6767 ga_clear(&ga_cmd);
6768 ga_clear(&ga_env);
6769 vim_free(cmd_wchar);
6770 vim_free(cmd_wchar_copy);
6771 vim_free(cwd_wchar);
6772 if (channel != NULL)
6773 channel_clear(channel);
6774 if (job != NULL)
6775 {
6776 job->jv_channel = NULL;
6777 job_cleanup(job);
6778 }
6779 term->tl_job = NULL;
6780 if (jo != NULL)
6781 CloseHandle(jo);
6782
6783 if (term->tl_siex.lpAttributeList != NULL)
6784 {
6785 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6786 vim_free(term->tl_siex.lpAttributeList);
6787 }
6788 term->tl_siex.lpAttributeList = NULL;
6789 if (o_theirs != NULL)
6790 CloseHandle(o_theirs);
6791 if (o_ours != NULL)
6792 CloseHandle(o_ours);
6793 if (i_ours != NULL)
6794 CloseHandle(i_ours);
6795 if (i_theirs != NULL)
6796 CloseHandle(i_theirs);
6797 if (term->tl_conpty != NULL)
6798 pClosePseudoConsole(term->tl_conpty);
6799 term->tl_conpty = NULL;
6800 return FAIL;
6801}
6802
6803 static void
6804conpty_term_report_winsize(term_T *term, int rows, int cols)
6805{
6806 COORD consize;
6807
6808 consize.X = cols;
6809 consize.Y = rows;
6810 pResizePseudoConsole(term->tl_conpty, consize);
6811}
6812
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006813 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006814term_free_conpty(term_T *term)
6815{
6816 if (term->tl_siex.lpAttributeList != NULL)
6817 {
6818 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6819 vim_free(term->tl_siex.lpAttributeList);
6820 }
6821 term->tl_siex.lpAttributeList = NULL;
6822 if (term->tl_conpty != NULL)
6823 pClosePseudoConsole(term->tl_conpty);
6824 term->tl_conpty = NULL;
6825}
6826
6827 int
6828use_conpty(void)
6829{
6830 return has_conpty;
6831}
6832
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006833# ifndef PROTO
6834
6835#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6836#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006837#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006838
6839void* (*winpty_config_new)(UINT64, void*);
6840void* (*winpty_open)(void*, void*);
6841void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6842BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6843void (*winpty_config_set_mouse_mode)(void*, int);
6844void (*winpty_config_set_initial_size)(void*, int, int);
6845LPCWSTR (*winpty_conin_name)(void*);
6846LPCWSTR (*winpty_conout_name)(void*);
6847LPCWSTR (*winpty_conerr_name)(void*);
6848void (*winpty_free)(void*);
6849void (*winpty_config_free)(void*);
6850void (*winpty_spawn_config_free)(void*);
6851void (*winpty_error_free)(void*);
6852LPCWSTR (*winpty_error_msg)(void*);
6853BOOL (*winpty_set_size)(void*, int, int, void*);
6854HANDLE (*winpty_agent_process)(void*);
6855
6856#define WINPTY_DLL "winpty.dll"
6857
6858static HINSTANCE hWinPtyDLL = NULL;
6859# endif
6860
6861 static int
6862dyn_winpty_init(int verbose)
6863{
6864 int i;
6865 static struct
6866 {
6867 char *name;
6868 FARPROC *ptr;
6869 } winpty_entry[] =
6870 {
6871 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6872 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6873 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6874 {"winpty_config_set_mouse_mode",
6875 (FARPROC*)&winpty_config_set_mouse_mode},
6876 {"winpty_config_set_initial_size",
6877 (FARPROC*)&winpty_config_set_initial_size},
6878 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6879 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6880 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6881 {"winpty_free", (FARPROC*)&winpty_free},
6882 {"winpty_open", (FARPROC*)&winpty_open},
6883 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6884 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6885 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6886 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6887 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6888 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6889 {NULL, NULL}
6890 };
6891
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006892 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006893 if (hWinPtyDLL)
6894 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006895 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6896 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006897 if (*p_winptydll != NUL)
6898 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6899 if (!hWinPtyDLL)
6900 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6901 if (!hWinPtyDLL)
6902 {
6903 if (verbose)
Martin Tournoij1a3e5742021-07-24 13:57:29 +02006904 semsg(_(e_loadlib),
6905 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
6906 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006907 return FAIL;
6908 }
6909 for (i = 0; winpty_entry[i].name != NULL
6910 && winpty_entry[i].ptr != NULL; ++i)
6911 {
6912 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6913 winpty_entry[i].name)) == NULL)
6914 {
6915 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006916 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006917 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006918 return FAIL;
6919 }
6920 }
6921
6922 return OK;
6923}
6924
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006925 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006926winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006927 term_T *term,
6928 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006929 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006930 jobopt_T *opt,
6931 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006932{
6933 WCHAR *cmd_wchar = NULL;
6934 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006935 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006936 channel_T *channel = NULL;
6937 job_T *job = NULL;
6938 DWORD error;
6939 HANDLE jo = NULL;
6940 HANDLE child_process_handle;
6941 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006942 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006943 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006944 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006945 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006946
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006947 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6948 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006949
6950 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006951 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006952 cmd = argvar->vval.v_string;
6953 }
6954 else if (argvar->v_type == VAR_LIST)
6955 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006956 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006957 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006958 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006959 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006960 if (cmd == NULL || *cmd == NUL)
6961 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006962 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006963 goto failed;
6964 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006965
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006966 term->tl_arg0_cmd = vim_strsave(cmd);
6967
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006968 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006969 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006970 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006971 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006972 if (opt->jo_cwd != NULL)
6973 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006974
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006975 win32_build_env(opt->jo_env, &ga_env, TRUE);
6976 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006977
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006978 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6979 if (term->tl_winpty_config == NULL)
6980 goto failed;
6981
6982 winpty_config_set_mouse_mode(term->tl_winpty_config,
6983 WINPTY_MOUSE_MODE_FORCE);
6984 winpty_config_set_initial_size(term->tl_winpty_config,
6985 term->tl_cols, term->tl_rows);
6986 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6987 if (term->tl_winpty == NULL)
6988 goto failed;
6989
6990 spawn_config = winpty_spawn_config_new(
6991 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6992 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6993 NULL,
6994 cmd_wchar,
6995 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006996 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006997 &winpty_err);
6998 if (spawn_config == NULL)
6999 goto failed;
7000
7001 channel = add_channel();
7002 if (channel == NULL)
7003 goto failed;
7004
7005 job = job_alloc();
7006 if (job == NULL)
7007 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02007008 if (argvar->v_type == VAR_STRING)
7009 {
7010 int argc;
7011
7012 build_argv_from_string(cmd, &job->jv_argv, &argc);
7013 }
7014 else
7015 {
7016 int argc;
7017
7018 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
7019 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007020
7021 if (opt->jo_set & JO_IN_BUF)
7022 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
7023
7024 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
7025 &child_thread_handle, &error, &winpty_err))
7026 goto failed;
7027
7028 channel_set_pipes(channel,
7029 (sock_T)CreateFileW(
7030 winpty_conin_name(term->tl_winpty),
7031 GENERIC_WRITE, 0, NULL,
7032 OPEN_EXISTING, 0, NULL),
7033 (sock_T)CreateFileW(
7034 winpty_conout_name(term->tl_winpty),
7035 GENERIC_READ, 0, NULL,
7036 OPEN_EXISTING, 0, NULL),
7037 (sock_T)CreateFileW(
7038 winpty_conerr_name(term->tl_winpty),
7039 GENERIC_READ, 0, NULL,
7040 OPEN_EXISTING, 0, NULL));
7041
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007042 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007043 channel->ch_write_text_mode = TRUE;
7044
7045 jo = CreateJobObject(NULL, NULL);
7046 if (jo == NULL)
7047 goto failed;
7048
7049 if (!AssignProcessToJobObject(jo, child_process_handle))
7050 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007051 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007052 CloseHandle(jo);
7053 jo = NULL;
7054 }
7055
7056 winpty_spawn_config_free(spawn_config);
7057 vim_free(cmd_wchar);
7058 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007059 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007060
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007061 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7062 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007063
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007064#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7065 if (opt->jo_set2 & JO2_ANSI_COLORS)
7066 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7067 else
7068 init_vterm_ansi_colors(term->tl_vterm);
7069#endif
7070
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007071 channel_set_job(channel, job, opt);
7072 job_set_options(job, opt);
7073
7074 job->jv_channel = channel;
7075 job->jv_proc_info.hProcess = child_process_handle;
7076 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7077 job->jv_job_object = jo;
7078 job->jv_status = JOB_STARTED;
7079 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007080 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007081 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007082 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007083 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007084 ++job->jv_refcount;
7085 term->tl_job = job;
7086
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007087 // Redirecting stdout and stderr doesn't work at the job level. Instead
7088 // open the file here and handle it in. opt->jo_io was changed in
7089 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007090 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7091 {
7092 char_u *fname = opt->jo_io_name[PART_OUT];
7093
7094 ch_log(channel, "Opening output file %s", fname);
7095 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7096 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007097 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007098 }
7099
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007100 return OK;
7101
7102failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007103 ga_clear(&ga_cmd);
7104 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007105 vim_free(cmd_wchar);
7106 vim_free(cwd_wchar);
7107 if (spawn_config != NULL)
7108 winpty_spawn_config_free(spawn_config);
7109 if (channel != NULL)
7110 channel_clear(channel);
7111 if (job != NULL)
7112 {
7113 job->jv_channel = NULL;
7114 job_cleanup(job);
7115 }
7116 term->tl_job = NULL;
7117 if (jo != NULL)
7118 CloseHandle(jo);
7119 if (term->tl_winpty != NULL)
7120 winpty_free(term->tl_winpty);
7121 term->tl_winpty = NULL;
7122 if (term->tl_winpty_config != NULL)
7123 winpty_config_free(term->tl_winpty_config);
7124 term->tl_winpty_config = NULL;
7125 if (winpty_err != NULL)
7126 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007127 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007128 (short_u *)winpty_error_msg(winpty_err), NULL);
7129
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007130 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007131 winpty_error_free(winpty_err);
7132 }
7133 return FAIL;
7134}
7135
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007136/*
7137 * Create a new terminal of "rows" by "cols" cells.
7138 * Store a reference in "term".
7139 * Return OK or FAIL.
7140 */
7141 static int
7142term_and_job_init(
7143 term_T *term,
7144 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007145 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007146 jobopt_T *opt,
7147 jobopt_T *orig_opt)
7148{
7149 int use_winpty = FALSE;
7150 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007151 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007152
7153 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7154 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7155
7156 if (!has_winpty && !has_conpty)
7157 // If neither is available give the errors for winpty, since when
7158 // conpty is not available it can't be installed either.
7159 return dyn_winpty_init(TRUE);
7160
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007161 if (opt->jo_tty_type != NUL)
7162 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007163
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007164 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007165 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007166 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007167 use_conpty = TRUE;
7168 else if (has_winpty)
7169 use_winpty = TRUE;
7170 // else: error
7171 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007172 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007173 {
7174 if (has_winpty)
7175 use_winpty = TRUE;
7176 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007177 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007178 {
7179 if (has_conpty)
7180 use_conpty = TRUE;
7181 else
7182 return dyn_conpty_init(TRUE);
7183 }
7184
7185 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007186 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007187
7188 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007189 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007190
7191 // error
7192 return dyn_winpty_init(TRUE);
7193}
7194
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007195 static int
7196create_pty_only(term_T *term, jobopt_T *options)
7197{
7198 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7199 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7200 char in_name[80], out_name[80];
7201 channel_T *channel = NULL;
7202
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007203 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7204 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007205
7206 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7207 GetCurrentProcessId(),
7208 curbuf->b_fnum);
7209 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7210 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7211 PIPE_UNLIMITED_INSTANCES,
7212 0, 0, NMPWAIT_NOWAIT, NULL);
7213 if (hPipeIn == INVALID_HANDLE_VALUE)
7214 goto failed;
7215
7216 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7217 GetCurrentProcessId(),
7218 curbuf->b_fnum);
7219 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7220 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7221 PIPE_UNLIMITED_INSTANCES,
7222 0, 0, 0, NULL);
7223 if (hPipeOut == INVALID_HANDLE_VALUE)
7224 goto failed;
7225
7226 ConnectNamedPipe(hPipeIn, NULL);
7227 ConnectNamedPipe(hPipeOut, NULL);
7228
7229 term->tl_job = job_alloc();
7230 if (term->tl_job == NULL)
7231 goto failed;
7232 ++term->tl_job->jv_refcount;
7233
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007234 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007235 term->tl_job->jv_status = JOB_FINISHED;
7236
7237 channel = add_channel();
7238 if (channel == NULL)
7239 goto failed;
7240 term->tl_job->jv_channel = channel;
7241 channel->ch_keep_open = TRUE;
7242 channel->ch_named_pipe = TRUE;
7243
7244 channel_set_pipes(channel,
7245 (sock_T)hPipeIn,
7246 (sock_T)hPipeOut,
7247 (sock_T)hPipeOut);
7248 channel_set_job(channel, term->tl_job, options);
7249 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7250 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7251
7252 return OK;
7253
7254failed:
7255 if (hPipeIn != NULL)
7256 CloseHandle(hPipeIn);
7257 if (hPipeOut != NULL)
7258 CloseHandle(hPipeOut);
7259 return FAIL;
7260}
7261
7262/*
7263 * Free the terminal emulator part of "term".
7264 */
7265 static void
7266term_free_vterm(term_T *term)
7267{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007268 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007269 if (term->tl_winpty != NULL)
7270 winpty_free(term->tl_winpty);
7271 term->tl_winpty = NULL;
7272 if (term->tl_winpty_config != NULL)
7273 winpty_config_free(term->tl_winpty_config);
7274 term->tl_winpty_config = NULL;
7275 if (term->tl_vterm != NULL)
7276 vterm_free(term->tl_vterm);
7277 term->tl_vterm = NULL;
7278}
7279
7280/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007281 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007282 */
7283 static void
7284term_report_winsize(term_T *term, int rows, int cols)
7285{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007286 if (term->tl_conpty)
7287 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007288 if (term->tl_winpty)
7289 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7290}
7291
7292 int
7293terminal_enabled(void)
7294{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007295 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007296}
7297
7298# else
7299
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007300///////////////////////////////////////
7301// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007302
7303/*
7304 * Create a new terminal of "rows" by "cols" cells.
7305 * Start job for "cmd".
7306 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007307 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007308 * Return OK or FAIL.
7309 */
7310 static int
7311term_and_job_init(
7312 term_T *term,
7313 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007314 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007315 jobopt_T *opt,
7316 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007317{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007318 term->tl_arg0_cmd = NULL;
7319
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007320 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7321 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007322
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007323#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7324 if (opt->jo_set2 & JO2_ANSI_COLORS)
7325 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7326 else
7327 init_vterm_ansi_colors(term->tl_vterm);
7328#endif
7329
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007330 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007331 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007332 if (term->tl_job != NULL)
7333 ++term->tl_job->jv_refcount;
7334
7335 return term->tl_job != NULL
7336 && term->tl_job->jv_channel != NULL
7337 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7338}
7339
7340 static int
7341create_pty_only(term_T *term, jobopt_T *opt)
7342{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007343 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7344 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007345
7346 term->tl_job = job_alloc();
7347 if (term->tl_job == NULL)
7348 return FAIL;
7349 ++term->tl_job->jv_refcount;
7350
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007351 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007352 term->tl_job->jv_status = JOB_FINISHED;
7353
7354 return mch_create_pty_channel(term->tl_job, opt);
7355}
7356
7357/*
7358 * Free the terminal emulator part of "term".
7359 */
7360 static void
7361term_free_vterm(term_T *term)
7362{
7363 if (term->tl_vterm != NULL)
7364 vterm_free(term->tl_vterm);
7365 term->tl_vterm = NULL;
7366}
7367
7368/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007369 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007370 */
7371 static void
7372term_report_winsize(term_T *term, int rows, int cols)
7373{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007374 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007375 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7376 {
7377 int fd = -1;
7378 int part;
7379
7380 for (part = PART_OUT; part < PART_COUNT; ++part)
7381 {
7382 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007383 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007384 break;
7385 }
7386 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7387 mch_signal_job(term->tl_job, (char_u *)"winch");
7388 }
7389}
7390
7391# endif
7392
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007393#endif // FEAT_TERMINAL