blob: c00302d2b590c3e5aa1a9a095dd0a3785f93d0a6 [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/*
2322 * Return the highight group name for the terminal; "Terminal" if not set.
2323 */
2324 static char_u *
2325term_get_highlight_name(term_T *term)
2326{
2327 if (term->tl_highlight_name == NULL)
2328 return (char_u *)"Terminal";
2329 return term->tl_highlight_name;
2330}
2331
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002332#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002333 cursorentry_T *
2334term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2335{
2336 term_T *term = in_terminal_loop;
2337 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002338 int id;
2339 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002340
Bram Moolenaara80faa82020-04-12 19:37:17 +02002341 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002342 entry.shape = entry.mshape =
2343 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2344 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2345 SHAPE_BLOCK;
2346 entry.percentage = 20;
2347 if (term->tl_cursor_blink)
2348 {
2349 entry.blinkwait = 700;
2350 entry.blinkon = 400;
2351 entry.blinkoff = 250;
2352 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002353
Bram Moolenaar83d47902020-03-26 20:34:00 +01002354 // The highlight group overrules the defaults.
2355 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002356 if (id != 0)
2357 {
2358 syn_id2colors(id, &term_fg, &term_bg);
2359 *fg = term_bg;
2360 }
2361 else
2362 *fg = gui.back_pixel;
2363
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002364 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002365 {
2366 if (id != 0)
2367 *bg = term_fg;
2368 else
2369 *bg = gui.norm_pixel;
2370 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002371 else
2372 *bg = color_name2handle(term->tl_cursor_color);
2373 entry.name = "n";
2374 entry.used_for = SHAPE_CURSOR;
2375
2376 return &entry;
2377}
2378#endif
2379
Bram Moolenaard317b382018-02-08 22:33:31 +01002380 static void
2381may_output_cursor_props(void)
2382{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002383 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002384 || last_set_cursor_shape != desired_cursor_shape
2385 || last_set_cursor_blink != desired_cursor_blink)
2386 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002387 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002388 last_set_cursor_shape = desired_cursor_shape;
2389 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002390 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002391 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002392 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002393 ui_cursor_shape_forced(TRUE);
2394 else
2395 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2396 }
2397}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002398
Bram Moolenaard317b382018-02-08 22:33:31 +01002399/*
2400 * Set the cursor color and shape, if not last set to these.
2401 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002402 static void
2403may_set_cursor_props(term_T *term)
2404{
2405#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002406 // For the GUI the cursor properties are obtained with
2407 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002408 if (gui.in_use)
2409 return;
2410#endif
2411 if (in_terminal_loop == term)
2412 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002413 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002414 desired_cursor_shape = term->tl_cursor_shape;
2415 desired_cursor_blink = term->tl_cursor_blink;
2416 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002417 }
2418}
2419
Bram Moolenaard317b382018-02-08 22:33:31 +01002420/*
2421 * Reset the desired cursor properties and restore them when needed.
2422 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002423 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002424prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002425{
2426#ifdef FEAT_GUI
2427 if (gui.in_use)
2428 return;
2429#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002430 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002431 desired_cursor_shape = -1;
2432 desired_cursor_blink = -1;
2433 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002434}
2435
2436/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002437 * Returns TRUE if the current window contains a terminal and we are sending
2438 * keys to the job.
2439 * If "check_job_status" is TRUE update the job status.
2440 */
2441 static int
2442term_use_loop_check(int check_job_status)
2443{
2444 term_T *term = curbuf->b_term;
2445
2446 return term != NULL
2447 && !term->tl_normal_mode
2448 && term->tl_vterm != NULL
2449 && term_job_running_check(term, check_job_status);
2450}
2451
2452/*
2453 * Returns TRUE if the current window contains a terminal and we are sending
2454 * keys to the job.
2455 */
2456 int
2457term_use_loop(void)
2458{
2459 return term_use_loop_check(FALSE);
2460}
2461
2462/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002463 * Called when entering a window with the mouse. If this is a terminal window
2464 * we may want to change state.
2465 */
2466 void
2467term_win_entered()
2468{
2469 term_T *term = curbuf->b_term;
2470
2471 if (term != NULL)
2472 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002473 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002474 {
2475 reset_VIsual_and_resel();
2476 if (State & INSERT)
2477 stop_insert_mode = TRUE;
2478 }
2479 mouse_was_outside = FALSE;
2480 enter_mouse_col = mouse_col;
2481 enter_mouse_row = mouse_row;
2482 }
2483}
2484
2485/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002486 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2487 * Return the Ctrl-key value in that case.
2488 */
2489 static int
2490raw_c_to_ctrl(int c)
2491{
2492 if ((mod_mask & MOD_MASK_CTRL)
2493 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2494 return c & 0x1f;
2495 return c;
2496}
2497
2498/*
2499 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2500 * May set "mod_mask".
2501 */
2502 static int
2503ctrl_to_raw_c(int c)
2504{
2505 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2506 {
2507 mod_mask |= MOD_MASK_CTRL;
2508 return c + '@';
2509 }
2510 return c;
2511}
2512
2513/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002514 * Wait for input and send it to the job.
2515 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2516 * when there is no more typahead.
2517 * Return when the start of a CTRL-W command is typed or anything else that
2518 * should be handled as a Normal mode command.
2519 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2520 * the terminal was closed.
2521 */
2522 int
2523terminal_loop(int blocking)
2524{
2525 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002526 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002527 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002528 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002529#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002530 int tty_fd = curbuf->b_term->tl_job->jv_channel
2531 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002532#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002533 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002534
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002535 // Remember the terminal we are sending keys to. However, the terminal
2536 // might be closed while waiting for a character, e.g. typing "exit" in a
2537 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2538 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002539 in_terminal_loop = curbuf->b_term;
2540
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002541 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002542 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002543 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002544 if (termwinkey == Ctrl_W)
2545 termwinkey = 0;
2546 }
Bram Moolenaarebec3e22020-11-28 20:22:06 +01002547 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002548 may_set_cursor_props(curbuf->b_term);
2549
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002550 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002551 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002552#ifdef FEAT_GUI
Bram Moolenaar02764712020-11-14 20:21:55 +01002553 if (curbuf->b_term != NULL && !curbuf->b_term->tl_system)
Bram Moolenaar13568252018-03-16 20:46:58 +01002554#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002555 // TODO: skip screen update when handling a sequence of keys.
2556 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002557 while (must_redraw != 0)
2558 if (update_screen(0) == FAIL)
2559 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002560 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002561 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002562 break;
2563
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002564 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002565 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002566
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002567 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002568 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002569 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002570 // Job finished while waiting for a character. Push back the
2571 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002572 if (raw_c != K_IGNORE)
2573 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002574 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002575 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002576 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002577 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002578 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002579
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002580#ifdef UNIX
2581 /*
2582 * The shell or another program may change the tty settings. Getting
2583 * them for every typed character is a bit of overhead, but it's needed
2584 * for the first character typed, e.g. when Vim starts in a shell.
2585 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002586 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002587 {
2588 ttyinfo_T info;
2589
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002590 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002591 if (get_tty_info(tty_fd, &info) == OK)
2592 term_backspace_char = info.backspace;
2593 }
2594#endif
2595
Bram Moolenaar4f974752019-02-17 17:44:42 +01002596#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002597 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2598 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002599 if (ctrl_break_was_pressed)
2600 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2601#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002602 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2603 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002604 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002605#ifdef FEAT_GUI
2606 && !curbuf->b_term->tl_system
2607#endif
2608 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002609 {
2610 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002611 int prev_raw_c = raw_c;
2612 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002613
2614#ifdef FEAT_CMDL_INFO
2615 if (add_to_showcmd(c))
2616 out_flush();
2617#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002618 raw_c = term_vgetc();
2619 c = raw_c_to_ctrl(raw_c);
2620
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002621#ifdef FEAT_CMDL_INFO
2622 clear_showcmd();
2623#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002624 if (!term_use_loop_check(TRUE)
2625 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002626 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002627 break;
2628
2629 if (prev_c == Ctrl_BSL)
2630 {
2631 if (c == Ctrl_N)
2632 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002633 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002634 term_enter_normal_mode();
2635 ret = FAIL;
2636 goto theend;
2637 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002638 // Send both keys to the terminal, first one here, second one
2639 // below.
2640 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2641 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002642 }
2643 else if (c == Ctrl_C)
2644 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002645 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002646 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2647 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002648 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002649 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002650 // "CTRL-W .": send CTRL-W to the job
2651 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002652 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002653 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002654 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002655 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002656 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002657 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002658 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002659 else if (c == 'N')
2660 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002661 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002662 term_enter_normal_mode();
2663 ret = FAIL;
2664 goto theend;
2665 }
2666 else if (c == '"')
2667 {
2668 term_paste_register(prev_c);
2669 continue;
2670 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002671 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002672 {
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002673 // space for CTRL-W, modifier, multi-byte char and NUL
2674 char_u buf[1 + 3 + MB_MAXBYTES + 1];
Bram Moolenaara4b26992019-08-15 20:58:54 +02002675
2676 // Put the command into the typeahead buffer, when using the
2677 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2678 buf[0] = Ctrl_W;
Bram Moolenaarf43e7ac2020-09-29 21:23:25 +02002679 buf[special_to_buf(c, mod_mask, FALSE, buf + 1) + 1] = NUL;
Bram Moolenaara4b26992019-08-15 20:58:54 +02002680 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002681 ret = OK;
2682 goto theend;
2683 }
2684 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002685# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002686 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002687 {
2688 WCHAR wc;
2689 char_u mb[3];
2690
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002691 mb[0] = (unsigned)raw_c >> 8;
2692 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002693 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002694 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002695 }
2696# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002697 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002698 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002699 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002700 // We are sure to come back here, don't reset the cursor color
2701 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002702 restore_cursor = FALSE;
2703
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002704 ret = OK;
2705 goto theend;
2706 }
2707 }
2708 ret = FAIL;
2709
2710theend:
2711 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002712 if (restore_cursor)
2713 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002714
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002715 // Move a snapshot of the screen contents to the buffer, so that completion
2716 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002717 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2718 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002719
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002720 return ret;
2721}
2722
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002723 static void
2724may_toggle_cursor(term_T *term)
2725{
2726 if (in_terminal_loop == term)
2727 {
2728 if (term->tl_cursor_visible)
2729 cursor_on();
2730 else
2731 cursor_off();
2732 }
2733}
2734
2735/*
2736 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002737 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002738 */
2739 static int
2740color2index(VTermColor *color, int fg, int *boldp)
2741{
2742 int red = color->red;
2743 int blue = color->blue;
2744 int green = color->green;
2745
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002746 if (VTERM_COLOR_IS_DEFAULT_FG(color)
2747 || VTERM_COLOR_IS_DEFAULT_BG(color))
2748 return 0;
2749 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002750 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002751 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002752 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002753 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002754 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002755 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2756 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2757 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002758 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002759 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2760 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2761 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2762 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2763 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2764 case 10: return lookup_color(20, fg, boldp) + 1; // red
2765 case 11: return lookup_color(16, fg, boldp) + 1; // green
2766 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2767 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2768 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2769 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2770 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002771 }
2772 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002773
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002774 if (t_colors >= 256)
2775 {
2776 if (red == blue && red == green)
2777 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002778 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002779 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002780 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2781 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2782 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002783 int i;
2784
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002785 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002786 return 17; // 00/00/00
2787 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002788 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002789 for (i = 0; i < 23; ++i)
2790 if (red < cutoff[i])
2791 return i + 233;
2792 return 256;
2793 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002794 {
2795 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2796 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002797
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002798 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002799 for (ri = 0; ri < 5; ++ri)
2800 if (red < cutoff[ri])
2801 break;
2802 for (gi = 0; gi < 5; ++gi)
2803 if (green < cutoff[gi])
2804 break;
2805 for (bi = 0; bi < 5; ++bi)
2806 if (blue < cutoff[bi])
2807 break;
2808 return 17 + ri * 36 + gi * 6 + bi;
2809 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002810 }
2811 return 0;
2812}
2813
2814/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002815 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002816 */
2817 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002818vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002819{
2820 int attr = 0;
2821
2822 if (cellattrs.bold)
2823 attr |= HL_BOLD;
2824 if (cellattrs.underline)
2825 attr |= HL_UNDERLINE;
2826 if (cellattrs.italic)
2827 attr |= HL_ITALIC;
2828 if (cellattrs.strike)
2829 attr |= HL_STRIKETHROUGH;
2830 if (cellattrs.reverse)
2831 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002832 return attr;
2833}
2834
2835/*
2836 * Store Vterm attributes in "cell" from highlight flags.
2837 */
2838 static void
2839hl2vtermAttr(int attr, cellattr_T *cell)
2840{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002841 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002842 if (attr & HL_BOLD)
2843 cell->attrs.bold = 1;
2844 if (attr & HL_UNDERLINE)
2845 cell->attrs.underline = 1;
2846 if (attr & HL_ITALIC)
2847 cell->attrs.italic = 1;
2848 if (attr & HL_STRIKETHROUGH)
2849 cell->attrs.strike = 1;
2850 if (attr & HL_INVERSE)
2851 cell->attrs.reverse = 1;
2852}
2853
2854/*
2855 * Convert the attributes of a vterm cell into an attribute index.
2856 */
2857 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002858cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002859 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002860 win_T *wp,
2861 VTermScreenCellAttrs cellattrs,
2862 VTermColor cellfg,
2863 VTermColor cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002864{
2865 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002866
2867#ifdef FEAT_GUI
2868 if (gui.in_use)
2869 {
2870 guicolor_T fg, bg;
2871
2872 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2873 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2874 return get_gui_attr_idx(attr, fg, bg);
2875 }
2876 else
2877#endif
2878#ifdef FEAT_TERMGUICOLORS
2879 if (p_tgc)
2880 {
Milly7b5f45b2021-10-15 22:25:43 +01002881 guicolor_T fg = INVALCOLOR;
2882 guicolor_T bg = INVALCOLOR;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002883
Milly7b5f45b2021-10-15 22:25:43 +01002884 // Use the 'wincolor' or "Terminal" highlighting for the default
2885 // colors.
2886 if (VTERM_COLOR_IS_DEFAULT_FG(&cellfg)
2887 || VTERM_COLOR_IS_DEFAULT_BG(&cellbg))
2888 {
2889 int id = 0;
2890
2891 if (wp != NULL && *wp->w_p_wcr != NUL)
2892 id = syn_name2id(wp->w_p_wcr);
2893 if (id == 0)
2894 id = syn_name2id(term_get_highlight_name(term));
2895 if (id > 0)
2896 syn_id2colors(id, &fg, &bg);
2897 if (!VTERM_COLOR_IS_DEFAULT_FG(&cellfg))
2898 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green,
2899 cellfg.blue);
2900 if (!VTERM_COLOR_IS_DEFAULT_BG(&cellbg))
2901 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green,
2902 cellbg.blue);
2903 }
2904 else
2905 {
2906 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2907 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2908 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002909
2910 return get_tgc_attr_idx(attr, fg, bg);
2911 }
2912 else
2913#endif
2914 {
2915 int bold = MAYBE;
2916 int fg = color2index(&cellfg, TRUE, &bold);
2917 int bg = color2index(&cellbg, FALSE, &bold);
2918
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002919 // Use the 'wincolor' or "Terminal" highlighting for the default
2920 // colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002921 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002922 {
Milly7b5f45b2021-10-15 22:25:43 +01002923 int cterm_fg = -1;
2924 int cterm_bg = -1;
2925 int id = 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002926
2927 if (wp != NULL && *wp->w_p_wcr != NUL)
Milly7b5f45b2021-10-15 22:25:43 +01002928 id = syn_name2id(wp->w_p_wcr);
2929 if (id == 0)
2930 id = syn_name2id(term_get_highlight_name(term));
2931 if (id > 0)
2932 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
2933 if (fg == 0 && cterm_fg >= 0)
2934 fg = cterm_fg + 1;
2935 if (bg == 0 && cterm_bg >= 0)
2936 bg = cterm_bg + 1;
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002937 }
2938
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002939 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002940 if (bold == TRUE)
2941 attr |= HL_BOLD;
2942 return get_cterm_attr_idx(attr, fg, bg);
2943 }
2944 return 0;
2945}
2946
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002947 static void
2948set_dirty_snapshot(term_T *term)
2949{
2950 term->tl_dirty_snapshot = TRUE;
2951#ifdef FEAT_TIMERS
2952 if (!term->tl_normal_mode)
2953 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002954 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002955 profile_setlimit(100L, &term->tl_timer_due);
2956 term->tl_timer_set = TRUE;
2957 }
2958#endif
2959}
2960
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002961 static int
2962handle_damage(VTermRect rect, void *user)
2963{
2964 term_T *term = (term_T *)user;
2965
2966 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2967 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002968 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002969 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002970 return 1;
2971}
2972
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002973 static void
2974term_scroll_up(term_T *term, int start_row, int count)
2975{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002976 win_T *wp = NULL;
2977 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002978 VTermColor fg, bg;
2979 VTermScreenCellAttrs attr;
2980 int clear_attr;
2981
Bram Moolenaara80faa82020-04-12 19:37:17 +02002982 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002983
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002984 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002985 {
2986 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002987 {
2988 // Set the color to clear lines with.
2989 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2990 &fg, &bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002991 clear_attr = cell2attr(term, wp, attr, fg, bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002992 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002993 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002994 }
2995}
2996
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002997 static int
2998handle_moverect(VTermRect dest, VTermRect src, void *user)
2999{
3000 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003001 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003002
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003003 // Scrolling up is done much more efficiently by deleting lines instead of
3004 // redrawing the text. But avoid doing this multiple times, postpone until
3005 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003006 if (dest.start_col == src.start_col
3007 && dest.end_col == src.end_col
3008 && dest.start_row < src.start_row)
3009 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003010 if (dest.start_row == 0)
3011 term->tl_postponed_scroll += count;
3012 else
3013 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003014 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003015
3016 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
3017 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02003018 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003019
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003020 // Note sure if the scrolling will work correctly, let's do a complete
3021 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003022 redraw_buf_later(term->tl_buffer, NOT_VALID);
3023 return 1;
3024}
3025
3026 static int
3027handle_movecursor(
3028 VTermPos pos,
3029 VTermPos oldpos UNUSED,
3030 int visible,
3031 void *user)
3032{
3033 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003034 win_T *wp = NULL;
3035 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003036
3037 term->tl_cursor_pos = pos;
3038 term->tl_cursor_visible = visible;
3039
Bram Moolenaare52e0c82020-02-28 22:20:10 +01003040 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003041 {
3042 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003043 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003044 }
3045 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003046 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003047
3048 return 1;
3049}
3050
3051 static int
3052handle_settermprop(
3053 VTermProp prop,
3054 VTermValue *value,
3055 void *user)
3056{
3057 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003058 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003059
3060 switch (prop)
3061 {
3062 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003063 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003064 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003065 if (strval == NULL)
3066 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003067 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003068 // a blank title isn't useful, make it empty, so that "running" is
3069 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003070 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003071 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003072 // Same as blank
3073 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003074 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003075 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3076 term->tl_title = NULL;
3077 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003078 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003079 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003080#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003081 else if (!enc_utf8 && enc_codepage > 0)
3082 {
3083 WCHAR *ret = NULL;
3084 int length = 0;
3085
3086 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003087 (char*)value->string.str,
3088 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003089 if (ret != NULL)
3090 {
3091 WideCharToMultiByte_alloc(enc_codepage, 0,
3092 ret, length, (char**)&term->tl_title,
3093 &length, 0, 0);
3094 vim_free(ret);
3095 }
3096 }
3097#endif
3098 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003099 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003100 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003101 strval = NULL;
3102 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003103 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003104 if (term == curbuf->b_term)
3105 maketitle();
3106 break;
3107
3108 case VTERM_PROP_CURSORVISIBLE:
3109 term->tl_cursor_visible = value->boolean;
3110 may_toggle_cursor(term);
3111 out_flush();
3112 break;
3113
3114 case VTERM_PROP_CURSORBLINK:
3115 term->tl_cursor_blink = value->boolean;
3116 may_set_cursor_props(term);
3117 break;
3118
3119 case VTERM_PROP_CURSORSHAPE:
3120 term->tl_cursor_shape = value->number;
3121 may_set_cursor_props(term);
3122 break;
3123
3124 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003125 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003126 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003127 if (strval == NULL)
3128 break;
3129 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003130 may_set_cursor_props(term);
3131 break;
3132
3133 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003134 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003135 term->tl_using_altscreen = value->boolean;
3136 break;
3137
3138 default:
3139 break;
3140 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003141 vim_free(strval);
3142
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003143 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003144 return 1;
3145}
3146
3147/*
3148 * The job running in the terminal resized the terminal.
3149 */
3150 static int
3151handle_resize(int rows, int cols, void *user)
3152{
3153 term_T *term = (term_T *)user;
3154 win_T *wp;
3155
3156 term->tl_rows = rows;
3157 term->tl_cols = cols;
3158 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003159 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003160 term->tl_vterm_size_changed = FALSE;
3161 else
3162 {
3163 FOR_ALL_WINDOWS(wp)
3164 {
3165 if (wp->w_buffer == term->tl_buffer)
3166 {
3167 win_setheight_win(rows, wp);
3168 win_setwidth_win(cols, wp);
3169 }
3170 }
3171 redraw_buf_later(term->tl_buffer, NOT_VALID);
3172 }
3173 return 1;
3174}
3175
3176/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003177 * If the number of lines that are stored goes over 'termscrollback' then
3178 * delete the first 10%.
3179 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3180 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003181 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003182 static void
3183limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003184{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003185 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003186 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003187 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003188 int i;
3189
3190 curbuf = term->tl_buffer;
3191 for (i = 0; i < todo; ++i)
3192 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003193 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3194 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003195 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003196 }
3197 curbuf = curwin->w_buffer;
3198
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003199 gap->ga_len -= todo;
3200 mch_memmove(gap->ga_data,
3201 (sb_line_T *)gap->ga_data + todo,
3202 sizeof(sb_line_T) * gap->ga_len);
3203 if (update_buffer)
3204 term->tl_scrollback_scrolled -= todo;
3205 }
3206}
3207
3208/*
3209 * Handle a line that is pushed off the top of the screen.
3210 */
3211 static int
3212handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3213{
3214 term_T *term = (term_T *)user;
3215 garray_T *gap;
3216 int update_buffer;
3217
3218 if (term->tl_normal_mode)
3219 {
3220 // In Terminal-Normal mode the user interacts with the buffer, thus we
3221 // must not change it. Postpone adding the scrollback lines.
3222 gap = &term->tl_scrollback_postponed;
3223 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003224 }
3225 else
3226 {
3227 // First remove the lines that were appended before, the pushed line
3228 // goes above it.
3229 cleanup_scrollback(term);
3230 gap = &term->tl_scrollback;
3231 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003232 }
3233
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003234 limit_scrollback(term, gap, update_buffer);
3235
3236 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003237 {
3238 cellattr_T *p = NULL;
3239 int len = 0;
3240 int i;
3241 int c;
3242 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003243 int text_len;
3244 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003245 sb_line_T *line;
3246 garray_T ga;
3247 cellattr_T fill_attr = term->tl_default_color;
3248
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003249 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003250 for (i = 0; i < cols; ++i)
3251 if (cells[i].chars[0] != 0)
3252 len = i + 1;
3253 else
3254 cell2cellattr(&cells[i], &fill_attr);
3255
3256 ga_init2(&ga, 1, 100);
3257 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003258 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003259 if (p != NULL)
3260 {
3261 for (col = 0; col < len; col += cells[col].width)
3262 {
3263 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3264 {
3265 ga.ga_len = 0;
3266 break;
3267 }
3268 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3269 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3270 (char_u *)ga.ga_data + ga.ga_len);
3271 cell2cellattr(&cells[col], &p[col]);
3272 }
3273 }
3274 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003275 {
3276 if (update_buffer)
3277 text = (char_u *)"";
3278 else
3279 text = vim_strsave((char_u *)"");
3280 text_len = 0;
3281 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003282 else
3283 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003284 text = ga.ga_data;
3285 text_len = ga.ga_len;
3286 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003287 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003288 if (update_buffer)
3289 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003290
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003291 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003292 line->sb_cols = len;
3293 line->sb_cells = p;
3294 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003295 if (update_buffer)
3296 {
3297 line->sb_text = NULL;
3298 ++term->tl_scrollback_scrolled;
3299 ga_clear(&ga); // free the text
3300 }
3301 else
3302 {
3303 line->sb_text = text;
3304 ga_init(&ga); // text is kept in tl_scrollback_postponed
3305 }
3306 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003307 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003308 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003309}
3310
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003311/*
3312 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3313 * received and stored in tl_scrollback_postponed.
3314 */
3315 static void
3316handle_postponed_scrollback(term_T *term)
3317{
3318 int i;
3319
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003320 if (term->tl_scrollback_postponed.ga_len == 0)
3321 return;
3322 ch_log(NULL, "Moving postponed scrollback to scrollback");
3323
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003324 // First remove the lines that were appended before, the pushed lines go
3325 // above it.
3326 cleanup_scrollback(term);
3327
3328 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3329 {
3330 char_u *text;
3331 sb_line_T *pp_line;
3332 sb_line_T *line;
3333
3334 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3335 break;
3336 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3337
3338 text = pp_line->sb_text;
3339 if (text == NULL)
3340 text = (char_u *)"";
3341 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3342 vim_free(pp_line->sb_text);
3343
3344 line = (sb_line_T *)term->tl_scrollback.ga_data
3345 + term->tl_scrollback.ga_len;
3346 line->sb_cols = pp_line->sb_cols;
3347 line->sb_cells = pp_line->sb_cells;
3348 line->sb_fill_attr = pp_line->sb_fill_attr;
3349 line->sb_text = NULL;
3350 ++term->tl_scrollback_scrolled;
3351 ++term->tl_scrollback.ga_len;
3352 }
3353
3354 ga_clear(&term->tl_scrollback_postponed);
3355 limit_scrollback(term, &term->tl_scrollback, TRUE);
3356}
3357
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003358static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003359 handle_damage, // damage
3360 handle_moverect, // moverect
3361 handle_movecursor, // movecursor
3362 handle_settermprop, // settermprop
3363 NULL, // bell
3364 handle_resize, // resize
3365 handle_pushline, // sb_pushline
3366 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003367};
3368
3369/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003370 * Do the work after the channel of a terminal was closed.
3371 * Must be called only when updating_screen is FALSE.
3372 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3373 */
3374 static int
3375term_after_channel_closed(term_T *term)
3376{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003377 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003378 if (!term->tl_normal_mode)
3379 {
3380 int fnum = term->tl_buffer->b_fnum;
3381
3382 cleanup_vterm(term);
3383
3384 if (term->tl_finish == TL_FINISH_CLOSE)
3385 {
3386 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003387 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003388#ifdef FEAT_PROP_POPUP
3389 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003390
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003391 // If this was a terminal in a popup window, go back to the
3392 // previous window.
3393 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3394 {
3395 pwin = curwin;
3396 if (win_valid(prevwin))
3397 win_enter(prevwin, FALSE);
3398 }
3399 else
3400#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003401 // If this is the last normal window: exit Vim.
3402 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3403 {
3404 exarg_T ea;
3405
Bram Moolenaara80faa82020-04-12 19:37:17 +02003406 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003407 ex_quit(&ea);
3408 return TRUE;
3409 }
3410
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003411 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003412 ch_log(NULL, "terminal job finished, closing window");
3413 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003414 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003415 if (curwin == aucmd_win)
3416 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003417 if (do_set_w_closing)
3418 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003419 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003420 if (do_set_w_closing)
3421 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003422 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003423#ifdef FEAT_PROP_POPUP
3424 if (pwin != NULL)
3425 popup_close_with_retval(pwin, 0);
3426#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003427 return TRUE;
3428 }
3429 if (term->tl_finish == TL_FINISH_OPEN
3430 && term->tl_buffer->b_nwindows == 0)
3431 {
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003432 char *cmd = term->tl_opencmd == NULL
3433 ? "botright sbuf %d"
3434 : (char *)term->tl_opencmd;
3435 size_t len = strlen(cmd) + 50;
3436 char *buf = alloc(len);
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003437
Bram Moolenaar47c5ea42020-11-12 15:12:15 +01003438 if (buf != NULL)
3439 {
3440 ch_log(NULL, "terminal job finished, opening window");
3441 vim_snprintf(buf, len, cmd, fnum);
3442 do_cmdline_cmd((char_u *)buf);
3443 vim_free(buf);
3444 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003445 }
3446 else
3447 ch_log(NULL, "terminal job finished");
3448 }
3449
3450 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3451 return FALSE;
3452}
3453
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003454#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3455/*
3456 * If the current window is a terminal in a popup window and the job has
3457 * finished, close the popup window and to back to the previous window.
3458 * Otherwise return FAIL.
3459 */
3460 int
3461may_close_term_popup(void)
3462{
3463 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3464 && !term_job_running(curbuf->b_term))
3465 {
3466 win_T *pwin = curwin;
3467
3468 if (win_valid(prevwin))
3469 win_enter(prevwin, FALSE);
3470 popup_close_with_retval(pwin, 0);
3471 return OK;
3472 }
3473 return FAIL;
3474}
3475#endif
3476
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003477/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003478 * Called when a channel has been closed.
3479 * If this was a channel for a terminal window then finish it up.
3480 */
3481 void
3482term_channel_closed(channel_T *ch)
3483{
3484 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003485 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003486 int did_one = FALSE;
3487
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003488 for (term = first_term; term != NULL; term = next_term)
3489 {
3490 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003491 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003492 {
3493 term->tl_channel_closed = TRUE;
3494 did_one = TRUE;
3495
Bram Moolenaard23a8232018-02-10 18:45:26 +01003496 VIM_CLEAR(term->tl_title);
3497 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003498#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003499 if (term->tl_out_fd != NULL)
3500 {
3501 fclose(term->tl_out_fd);
3502 term->tl_out_fd = NULL;
3503 }
3504#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003505
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003506 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003507 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003508 // Cannot open or close windows now. Can happen when
3509 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003510 term->tl_channel_recently_closed = TRUE;
3511 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003512 }
3513
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003514 if (term_after_channel_closed(term))
3515 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003516 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003517 }
3518
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003519 if (did_one)
3520 {
3521 redraw_statuslines();
3522
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003523 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003524 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003525 typebuf_was_filled = TRUE;
3526
3527 term = curbuf->b_term;
3528 if (term != NULL)
3529 {
3530 if (term->tl_job == ch->ch_job)
3531 maketitle();
3532 update_cursor(term, term->tl_cursor_visible);
3533 }
3534 }
3535}
3536
3537/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003538 * To be called after resetting updating_screen: handle any terminal where the
3539 * channel was closed.
3540 */
3541 void
3542term_check_channel_closed_recently()
3543{
3544 term_T *term;
3545 term_T *next_term;
3546
3547 for (term = first_term; term != NULL; term = next_term)
3548 {
3549 next_term = term->tl_next;
3550 if (term->tl_channel_recently_closed)
3551 {
3552 term->tl_channel_recently_closed = FALSE;
3553 if (term_after_channel_closed(term))
3554 // start over, the list may have changed
3555 next_term = first_term;
3556 }
3557 }
3558}
3559
3560/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003561 * Fill one screen line from a line of the terminal.
3562 * Advances "pos" to past the last column.
3563 */
3564 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003565term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003566 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003567 win_T *wp,
3568 VTermScreen *screen,
3569 VTermPos *pos,
3570 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003571{
3572 int off = screen_get_current_line_off();
3573
3574 for (pos->col = 0; pos->col < max_col; )
3575 {
3576 VTermScreenCell cell;
3577 int c;
3578
3579 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003580 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003581
3582 c = cell.chars[0];
3583 if (c == NUL)
3584 {
3585 ScreenLines[off] = ' ';
3586 if (enc_utf8)
3587 ScreenLinesUC[off] = NUL;
3588 }
3589 else
3590 {
3591 if (enc_utf8)
3592 {
3593 int i;
3594
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003595 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003596 for (i = 0; i < Screen_mco
3597 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3598 {
3599 ScreenLinesC[i][off] = cell.chars[i + 1];
3600 if (cell.chars[i + 1] == 0)
3601 break;
3602 }
3603 if (c >= 0x80 || (Screen_mco > 0
3604 && ScreenLinesC[0][off] != 0))
3605 {
3606 ScreenLines[off] = ' ';
3607 ScreenLinesUC[off] = c;
3608 }
3609 else
3610 {
3611 ScreenLines[off] = c;
3612 ScreenLinesUC[off] = NUL;
3613 }
3614 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003615#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003616 else if (has_mbyte && c >= 0x80)
3617 {
3618 char_u mb[MB_MAXBYTES+1];
3619 WCHAR wc = c;
3620
3621 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3622 (char*)mb, 2, 0, 0) > 1)
3623 {
3624 ScreenLines[off] = mb[0];
3625 ScreenLines[off + 1] = mb[1];
3626 cell.width = mb_ptr2cells(mb);
3627 }
3628 else
3629 ScreenLines[off] = c;
3630 }
3631#endif
3632 else
Bram Moolenaar927495b2020-11-06 17:58:35 +01003633 // This will only store the lower byte of "c".
Bram Moolenaar13568252018-03-16 20:46:58 +01003634 ScreenLines[off] = c;
3635 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003636 ScreenAttrs[off] = cell2attr(term, wp, cell.attrs, cell.fg, cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003637
3638 ++pos->col;
3639 ++off;
3640 if (cell.width == 2)
3641 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003642 // don't set the second byte to NUL for a DBCS encoding, it
3643 // has been set above
Bram Moolenaar927495b2020-11-06 17:58:35 +01003644 if (enc_utf8)
3645 {
3646 ScreenLinesUC[off] = NUL;
Bram Moolenaar13568252018-03-16 20:46:58 +01003647 ScreenLines[off] = NUL;
Bram Moolenaar927495b2020-11-06 17:58:35 +01003648 }
3649 else if (!has_mbyte)
3650 {
3651 // Can't show a double-width character with a single-byte
3652 // 'encoding', just use a space.
3653 ScreenLines[off] = ' ';
3654 ScreenAttrs[off] = ScreenAttrs[off - 1];
3655 }
Bram Moolenaar13568252018-03-16 20:46:58 +01003656
3657 ++pos->col;
3658 ++off;
3659 }
3660 }
3661}
3662
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003663#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003664 static void
3665update_system_term(term_T *term)
3666{
3667 VTermPos pos;
3668 VTermScreen *screen;
3669
3670 if (term->tl_vterm == NULL)
3671 return;
3672 screen = vterm_obtain_screen(term->tl_vterm);
3673
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003674 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003675 while (term->tl_toprow > 0
3676 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3677 {
3678 int save_p_more = p_more;
3679
3680 p_more = FALSE;
3681 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003682 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003683 p_more = save_p_more;
3684 --term->tl_toprow;
3685 }
3686
3687 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3688 && pos.row < Rows; ++pos.row)
3689 {
3690 if (pos.row < term->tl_rows)
3691 {
3692 int max_col = MIN(Columns, term->tl_cols);
3693
Bram Moolenaar83d47902020-03-26 20:34:00 +01003694 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003695 }
3696 else
3697 pos.col = 0;
3698
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003699 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003700 }
3701
3702 term->tl_dirty_row_start = MAX_ROW;
3703 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003704}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003705#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003706
3707/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003708 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3709 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003710 * Terminal-Normal mode.
3711 */
3712 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003713term_do_update_window(win_T *wp)
3714{
3715 term_T *term = wp->w_buffer->b_term;
3716
3717 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3718}
3719
3720/*
3721 * Called to update a window that contains an active terminal.
3722 */
3723 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003724term_update_window(win_T *wp)
3725{
3726 term_T *term = wp->w_buffer->b_term;
3727 VTerm *vterm;
3728 VTermScreen *screen;
3729 VTermState *state;
3730 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003731 int rows, cols;
3732 int newrows, newcols;
3733 int minsize;
3734 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003735
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003736 vterm = term->tl_vterm;
3737 screen = vterm_obtain_screen(vterm);
3738 state = vterm_obtain_state(vterm);
3739
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003740 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3741 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003742 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003743 {
3744 term->tl_dirty_row_start = 0;
3745 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003746
3747 if (term->tl_postponed_scroll > 0
3748 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003749 // Scrolling is usually faster than redrawing, when there are only
3750 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003751 term_scroll_up(term, 0, term->tl_postponed_scroll);
3752 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003753 }
3754
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003755 /*
3756 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003757 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003758 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003759 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003760
Bram Moolenaar498c2562018-04-15 23:45:15 +02003761 newrows = 99999;
3762 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003763 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003764 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003765 // Always use curwin, it may be a popup window.
3766 win_T *wwp = twp == NULL ? curwin : twp;
3767
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003768 // When more than one window shows the same terminal, use the
3769 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003770 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003771 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003772 newrows = MIN(newrows, wwp->w_height);
3773 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003774 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003775 if (twp == NULL)
3776 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003777 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003778 if (newrows == 99999 || newcols == 99999)
3779 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003780 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3781 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3782
Bram Moolenaareba13e42021-02-23 17:47:23 +01003783 // If no cell is visible there is no point in resizing. Also, vterm can't
3784 // handle a zero height.
3785 if (newrows == 0 || newcols == 0)
3786 return;
3787
Bram Moolenaar498c2562018-04-15 23:45:15 +02003788 if (term->tl_rows != newrows || term->tl_cols != newcols)
3789 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003790 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003791 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003792 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003793 newrows);
3794 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003795
3796 // Updating the terminal size will cause the snapshot to be cleared.
3797 // When not in terminal_loop() we need to restore it.
3798 if (term != in_terminal_loop)
3799 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003800 }
3801
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003802 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003803 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaarebec3e22020-11-28 20:22:06 +01003804 position_cursor(wp, &pos);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003805
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003806 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3807 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003808 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003809 if (pos.row < term->tl_rows)
3810 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003811 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003812
Bram Moolenaar83d47902020-03-26 20:34:00 +01003813 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003814 }
3815 else
3816 pos.col = 0;
3817
Bram Moolenaarf118d482018-03-13 13:14:00 +01003818 screen_line(wp->w_winrow + pos.row
3819#ifdef FEAT_MENU
3820 + winbar_height(wp)
3821#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003822 , wp->w_wincol, pos.col, wp->w_width,
3823#ifdef FEAT_PROP_POPUP
3824 popup_is_popup(wp) ? SLF_POPUP :
3825#endif
3826 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003827 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003828 term->tl_dirty_row_start = MAX_ROW;
3829 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003830}
3831
3832/*
3833 * Return TRUE if "wp" is a terminal window where the job has finished.
3834 */
3835 int
3836term_is_finished(buf_T *buf)
3837{
3838 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3839}
3840
3841/*
3842 * Return TRUE if "wp" is a terminal window where the job has finished or we
3843 * are in Terminal-Normal mode, thus we show the buffer contents.
3844 */
3845 int
3846term_show_buffer(buf_T *buf)
3847{
3848 term_T *term = buf->b_term;
3849
3850 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3851}
3852
3853/*
3854 * The current buffer is going to be changed. If there is terminal
3855 * highlighting remove it now.
3856 */
3857 void
3858term_change_in_curbuf(void)
3859{
3860 term_T *term = curbuf->b_term;
3861
3862 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3863 {
3864 free_scrollback(term);
3865 redraw_buf_later(term->tl_buffer, NOT_VALID);
3866
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003867 // The buffer is now like a normal buffer, it cannot be easily
3868 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003869 set_string_option_direct((char_u *)"buftype", -1,
3870 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3871 }
3872}
3873
3874/*
3875 * Get the screen attribute for a position in the buffer.
3876 * Use a negative "col" to get the filler background color.
3877 */
3878 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003879term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003880{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003881 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003882 term_T *term = buf->b_term;
3883 sb_line_T *line;
3884 cellattr_T *cellattr;
3885
3886 if (lnum > term->tl_scrollback.ga_len)
3887 cellattr = &term->tl_default_color;
3888 else
3889 {
3890 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3891 if (col < 0 || col >= line->sb_cols)
3892 cellattr = &line->sb_fill_attr;
3893 else
3894 cellattr = line->sb_cells + col;
3895 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003896 return cell2attr(term, wp, cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003897}
3898
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003899/*
3900 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003901 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003902 */
3903 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003904cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003905{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003906 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3907 if (rgb->index == 0)
3908 rgb->type = VTERM_COLOR_RGB;
3909 else
3910 {
3911 rgb->type = VTERM_COLOR_INDEXED;
3912 --rgb->index;
3913 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003914}
3915
3916/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003917 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003918 */
3919 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003920init_default_colors(term_T *term, win_T *wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003921{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003922 VTermColor *fg, *bg;
3923 int fgval, bgval;
3924 int id;
3925
Bram Moolenaara80faa82020-04-12 19:37:17 +02003926 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003927 term->tl_default_color.width = 1;
3928 fg = &term->tl_default_color.fg;
3929 bg = &term->tl_default_color.bg;
3930
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003931 // Vterm uses a default black background. Set it to white when
3932 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003933 if (*p_bg == 'l')
3934 {
3935 fgval = 0;
3936 bgval = 255;
3937 }
3938 else
3939 {
3940 fgval = 255;
3941 bgval = 0;
3942 }
3943 fg->red = fg->green = fg->blue = fgval;
3944 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003945 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3946 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003947
Bram Moolenaar83d47902020-03-26 20:34:00 +01003948 // The 'wincolor' or the highlight group overrules the defaults.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003949 if (wp != NULL && *wp->w_p_wcr != NUL)
3950 id = syn_name2id(wp->w_p_wcr);
3951 else
Bram Moolenaar83d47902020-03-26 20:34:00 +01003952 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003953
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003954 // Use the actual color for the GUI and when 'termguicolors' is set.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003955#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3956 if (0
3957# ifdef FEAT_GUI
3958 || gui.in_use
3959# endif
3960# ifdef FEAT_TERMGUICOLORS
3961 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003962# ifdef FEAT_VTP
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003963 // Finally get INVALCOLOR on this execution path
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003964 || (!p_tgc && t_colors >= 256)
3965# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003966# endif
3967 )
3968 {
3969 guicolor_T fg_rgb = INVALCOLOR;
3970 guicolor_T bg_rgb = INVALCOLOR;
3971
3972 if (id != 0)
3973 syn_id2colors(id, &fg_rgb, &bg_rgb);
3974
3975# ifdef FEAT_GUI
3976 if (gui.in_use)
3977 {
3978 if (fg_rgb == INVALCOLOR)
3979 fg_rgb = gui.norm_pixel;
3980 if (bg_rgb == INVALCOLOR)
3981 bg_rgb = gui.back_pixel;
3982 }
3983# ifdef FEAT_TERMGUICOLORS
3984 else
3985# endif
3986# endif
3987# ifdef FEAT_TERMGUICOLORS
3988 {
3989 if (fg_rgb == INVALCOLOR)
3990 fg_rgb = cterm_normal_fg_gui_color;
3991 if (bg_rgb == INVALCOLOR)
3992 bg_rgb = cterm_normal_bg_gui_color;
3993 }
3994# endif
3995 if (fg_rgb != INVALCOLOR)
3996 {
3997 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3998
3999 fg->red = (unsigned)(rgb >> 16);
4000 fg->green = (unsigned)(rgb >> 8) & 255;
4001 fg->blue = (unsigned)rgb & 255;
4002 }
4003 if (bg_rgb != INVALCOLOR)
4004 {
4005 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
4006
4007 bg->red = (unsigned)(rgb >> 16);
4008 bg->green = (unsigned)(rgb >> 8) & 255;
4009 bg->blue = (unsigned)rgb & 255;
4010 }
4011 }
4012 else
4013#endif
4014 if (id != 0 && t_colors >= 16)
4015 {
Milly7b5f45b2021-10-15 22:25:43 +01004016 int cterm_fg = -1;
4017 int cterm_bg = -1;
4018 syn_id2cterm_bg(id, &cterm_fg, &cterm_bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01004019
4020 if (cterm_fg >= 0)
4021 cterm_color2vterm(cterm_fg, fg);
4022 if (cterm_bg >= 0)
4023 cterm_color2vterm(cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004024 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004025 else
4026 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004027#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004028 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004029#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004030
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004031 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004032 if (cterm_normal_fg_color > 0)
4033 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004034 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004035# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4036# ifdef VIMDLL
4037 if (!gui.in_use)
4038# endif
4039 {
4040 tmp = fg->red;
4041 fg->red = fg->blue;
4042 fg->blue = tmp;
4043 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004044# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004045 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004046# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004047 else
4048 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004049# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004050
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004051 if (cterm_normal_bg_color > 0)
4052 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02004053 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004054# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4055# ifdef VIMDLL
4056 if (!gui.in_use)
4057# endif
4058 {
4059 tmp = fg->red;
4060 fg->red = fg->blue;
4061 fg->blue = tmp;
4062 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004063# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004064 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02004065# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004066 else
4067 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004068# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004069 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004070}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004071
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004072#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4073/*
4074 * Set the 16 ANSI colors from array of RGB values
4075 */
4076 static void
4077set_vterm_palette(VTerm *vterm, long_u *rgb)
4078{
4079 int index = 0;
4080 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004081
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004082 for (; index < 16; index++)
4083 {
4084 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004085
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004086 color.red = (unsigned)(rgb[index] >> 16);
4087 color.green = (unsigned)(rgb[index] >> 8) & 255;
4088 color.blue = (unsigned)rgb[index] & 255;
4089 vterm_state_set_palette_color(state, index, &color);
4090 }
4091}
4092
4093/*
4094 * Set the ANSI color palette from a list of colors
4095 */
4096 static int
4097set_ansi_colors_list(VTerm *vterm, list_T *list)
4098{
4099 int n = 0;
4100 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004101 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004102
Bram Moolenaarb0992022020-01-30 14:55:42 +01004103 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004104 {
4105 char_u *color_name;
4106 guicolor_T guicolor;
4107
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004108 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004109 if (color_name == NULL)
4110 return FAIL;
4111
4112 guicolor = GUI_GET_COLOR(color_name);
4113 if (guicolor == INVALCOLOR)
4114 return FAIL;
4115
4116 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4117 }
4118
4119 if (n != 16 || li != NULL)
4120 return FAIL;
4121
4122 set_vterm_palette(vterm, rgb);
4123
4124 return OK;
4125}
4126
4127/*
4128 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4129 */
4130 static void
4131init_vterm_ansi_colors(VTerm *vterm)
4132{
4133 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4134
4135 if (var != NULL
4136 && (var->di_tv.v_type != VAR_LIST
4137 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004138 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004139 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004140 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004141}
4142#endif
4143
Bram Moolenaar52acb112018-03-18 19:20:22 +01004144/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004145 * Handles a "drop" command from the job in the terminal.
4146 * "item" is the file name, "item->li_next" may have options.
4147 */
4148 static void
4149handle_drop_command(listitem_T *item)
4150{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004151 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004152 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004153 int bufnr;
4154 win_T *wp;
4155 tabpage_T *tp;
4156 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004157 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004158
4159 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4160 FOR_ALL_TAB_WINDOWS(tp, wp)
4161 {
4162 if (wp->w_buffer->b_fnum == bufnr)
4163 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004164 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004165 goto_tabpage_win(tp, wp);
4166 return;
4167 }
4168 }
4169
Bram Moolenaara80faa82020-04-12 19:37:17 +02004170 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004171
4172 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4173 && opt_item->li_tv.vval.v_dict != NULL)
4174 {
4175 dict_T *dict = opt_item->li_tv.vval.v_dict;
4176 char_u *p;
4177
Bram Moolenaar8f667172018-12-14 15:38:31 +01004178 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004179 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004180 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004181 if (p != NULL)
4182 {
4183 if (check_ff_value(p) == FAIL)
4184 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4185 else
4186 ea.force_ff = *p;
4187 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004188 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004189 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004190 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004191 if (p != NULL)
4192 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004193 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004194 if (ea.cmd != NULL)
4195 {
4196 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4197 ea.force_enc = 11;
4198 tofree = ea.cmd;
4199 }
4200 }
4201
Bram Moolenaar8f667172018-12-14 15:38:31 +01004202 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004203 if (p != NULL)
4204 get_bad_opt(p, &ea);
4205
4206 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4207 ea.force_bin = FORCE_BIN;
4208 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4209 ea.force_bin = FORCE_BIN;
4210 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4211 ea.force_bin = FORCE_NOBIN;
4212 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4213 ea.force_bin = FORCE_NOBIN;
4214 }
4215
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004216 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004217 if (ea.cmd == NULL)
4218 ea.cmd = (char_u *)"split";
4219 ea.arg = fname;
4220 ea.cmdidx = CMD_split;
4221 ex_splitview(&ea);
4222
4223 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004224}
4225
4226/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004227 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4228 */
4229 static int
4230is_permitted_term_api(char_u *func, char_u *pat)
4231{
4232 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4233}
4234
4235/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004236 * Handles a function call from the job running in a terminal.
4237 * "item" is the function name, "item->li_next" has the arguments.
4238 */
4239 static void
4240handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4241{
4242 char_u *func;
4243 typval_T argvars[2];
4244 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004245 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004246
4247 if (item->li_next == NULL)
4248 {
4249 ch_log(channel, "Missing function arguments for call");
4250 return;
4251 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004252 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004253
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004254 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004255 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004256 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004257 return;
4258 }
4259
4260 argvars[0].v_type = VAR_NUMBER;
4261 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4262 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004263 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004264 funcexe.firstline = 1L;
4265 funcexe.lastline = 1L;
4266 funcexe.evaluate = TRUE;
4267 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004268 {
4269 clear_tv(&rettv);
4270 ch_log(channel, "Function %s called", func);
4271 }
4272 else
4273 ch_log(channel, "Calling function %s failed", func);
4274}
4275
4276/*
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004277 * URL decoding (also know as Percent-encoding).
4278 *
4279 * Note this function currently is only used for decoding shell's
4280 * OSC 7 escape sequence which we can assume all bytes are valid
4281 * UTF-8 bytes. Thus we don't need to deal with invalid UTF-8
4282 * encoding bytes like 0xfe, 0xff.
4283 */
4284 static size_t
4285url_decode(const char *src, const size_t len, char_u *dst)
4286{
4287 size_t i = 0, j = 0;
4288
4289 while (i < len)
4290 {
4291 if (src[i] == '%' && i + 2 < len)
4292 {
4293 dst[j] = hexhex2nr((char_u *)&src[i + 1]);
4294 j++;
4295 i += 3;
4296 }
4297 else
4298 {
4299 dst[j] = src[i];
4300 i++;
4301 j++;
4302 }
4303 }
4304 dst[j] = '\0';
4305 return j;
4306}
4307
4308/*
4309 * Sync terminal buffer's cwd with shell's pwd with the help of OSC 7.
4310 *
4311 * The OSC 7 sequence has the format of
4312 * "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"
4313 * and what VTerm provides via VTermStringFragment is
4314 * "file://HOSTNAME/CURRENT/DIR"
4315 */
4316 static void
4317sync_shell_dir(VTermStringFragment *frag)
4318{
4319 int offset = 7; // len of "file://" is 7
4320 char *pos = (char *)frag->str + offset;
4321 char_u *new_dir;
4322
4323 // remove HOSTNAME to get PWD
Bram Moolenaar918b0892021-05-08 20:09:24 +02004324 while (*pos != '/' && offset < (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004325 {
4326 offset += 1;
4327 pos += 1;
4328 }
4329
Bram Moolenaar918b0892021-05-08 20:09:24 +02004330 if (offset >= (int)frag->len)
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004331 {
4332 semsg(_(e_failed_to_extract_pwd_from_str_check_your_shell_config),
4333 frag->str);
4334 return;
4335 }
4336
4337 new_dir = alloc(frag->len - offset + 1);
4338 url_decode(pos, frag->len-offset, new_dir);
4339 changedir_func(new_dir, TRUE, CDSCOPE_WINDOW);
4340 vim_free(new_dir);
4341}
4342
4343/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004344 * Called by libvterm when it cannot recognize an OSC sequence.
4345 * We recognize a terminal API command.
4346 */
4347 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004348parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004349{
4350 term_T *term = (term_T *)user;
4351 js_read_T reader;
4352 typval_T tv;
4353 channel_T *channel = term->tl_job == NULL ? NULL
4354 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004355 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004356
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +02004357 // We recognize only OSC 5 1 ; {command} and OSC 7 ; {command}
4358 if (p_asd && command == 7)
4359 {
4360 sync_shell_dir(&frag);
4361 return 1;
4362 }
4363
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004364 if (command != 51)
4365 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004366
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004367 // Concatenate what was received until the final piece is found.
4368 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4369 {
4370 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004371 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004372 }
4373 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004374 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004375 if (!frag.final)
4376 return 1;
4377
4378 ((char *)gap->ga_data)[gap->ga_len] = 0;
4379 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004380 reader.js_fill = NULL;
4381 reader.js_used = 0;
4382 if (json_decode(&reader, &tv, 0) == OK
4383 && tv.v_type == VAR_LIST
4384 && tv.vval.v_list != NULL)
4385 {
4386 listitem_T *item = tv.vval.v_list->lv_first;
4387
4388 if (item == NULL)
4389 ch_log(channel, "Missing command");
4390 else
4391 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004392 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004393
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004394 // Make sure an invoked command doesn't delete the buffer (and the
4395 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004396 ++term->tl_buffer->b_locked;
4397
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004398 item = item->li_next;
4399 if (item == NULL)
4400 ch_log(channel, "Missing argument for %s", cmd);
4401 else if (STRCMP(cmd, "drop") == 0)
4402 handle_drop_command(item);
4403 else if (STRCMP(cmd, "call") == 0)
4404 handle_call_command(term, channel, item);
4405 else
4406 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004407 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004408 }
4409 }
4410 else
4411 ch_log(channel, "Invalid JSON received");
4412
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004413 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004414 clear_tv(&tv);
4415 return 1;
4416}
4417
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004418/*
4419 * Called by libvterm when it cannot recognize a CSI sequence.
4420 * We recognize the window position report.
4421 */
4422 static int
4423parse_csi(
4424 const char *leader UNUSED,
4425 const long args[],
4426 int argcount,
4427 const char *intermed UNUSED,
4428 char command,
4429 void *user)
4430{
4431 term_T *term = (term_T *)user;
4432 char buf[100];
4433 int len;
4434 int x = 0;
4435 int y = 0;
4436 win_T *wp;
4437
4438 // We recognize only CSI 13 t
4439 if (command != 't' || argcount != 1 || args[0] != 13)
4440 return 0; // not handled
4441
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004442 // When getting the window position is not possible or it fails it results
4443 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004444#if defined(FEAT_GUI) \
4445 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4446 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004447 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004448#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004449
4450 FOR_ALL_WINDOWS(wp)
4451 if (wp->w_buffer == term->tl_buffer)
4452 break;
4453 if (wp != NULL)
4454 {
4455#ifdef FEAT_GUI
4456 if (gui.in_use)
4457 {
4458 x += wp->w_wincol * gui.char_width;
4459 y += W_WINROW(wp) * gui.char_height;
4460 }
4461 else
4462#endif
4463 {
4464 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004465 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004466 x += wp->w_wincol * 7;
4467 y += W_WINROW(wp) * 10;
4468 }
4469 }
4470
4471 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4472 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4473 (char_u *)buf, len, NULL);
4474 return 1;
4475}
4476
Bram Moolenaard8637282020-05-20 18:41:41 +02004477static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004478 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004479 parse_csi, // csi
4480 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004481 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004482};
4483
4484/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004485 * Use Vim's allocation functions for vterm so profiling works.
4486 */
4487 static void *
4488vterm_malloc(size_t size, void *data UNUSED)
4489{
Bram Moolenaar88137392021-11-12 16:01:15 +00004490 // make sure that the length is not zero
4491 return alloc_clear(size == 0 ? 1L : size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004492}
4493
4494 static void
4495vterm_memfree(void *ptr, void *data UNUSED)
4496{
4497 vim_free(ptr);
4498}
4499
4500static VTermAllocatorFunctions vterm_allocator = {
4501 &vterm_malloc,
4502 &vterm_memfree
4503};
4504
4505/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004506 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004507 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004508 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004509 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004510create_vterm(term_T *term, int rows, int cols)
4511{
4512 VTerm *vterm;
4513 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004514 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004515 VTermValue value;
4516
Bram Moolenaar756ef112018-04-10 12:04:27 +02004517 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004518 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004519 if (vterm == NULL)
4520 return FAIL;
4521
4522 // Allocate screen and state here, so we can bail out if that fails.
4523 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004524 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004525 if (state == NULL || screen == NULL)
4526 {
4527 vterm_free(vterm);
4528 return FAIL;
4529 }
4530
Bram Moolenaar52acb112018-03-18 19:20:22 +01004531 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004532 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004533 vterm_set_utf8(vterm, 1);
4534
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004535 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004536
4537 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004538 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004539 &term->tl_default_color.fg,
4540 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004541
Bram Moolenaar9e587872019-05-13 20:27:23 +02004542 if (t_colors < 16)
4543 // Less than 16 colors: assume that bold means using a bright color for
4544 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004545 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4546
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004547 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004548 vterm_screen_reset(screen, 1 /* hard */);
4549
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004550 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004551 vterm_screen_enable_altscreen(screen, 1);
4552
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004553 // For unix do not use a blinking cursor. In an xterm this causes the
4554 // cursor to blink if it's blinking in the xterm.
4555 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004556#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004557 if (GetCaretBlinkTime() == INFINITE)
4558 value.boolean = 0;
4559 else
4560 value.boolean = 1;
4561#else
4562 value.boolean = 0;
4563#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004564 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004565 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004566
4567 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004568}
4569
4570/*
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004571 * Called when 'wincolor' was set.
4572 */
4573 void
Bram Moolenaarad431992021-05-03 20:40:38 +02004574term_update_colors(term_T *term)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004575{
Bram Moolenaarad431992021-05-03 20:40:38 +02004576 win_T *wp;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004577
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01004578 if (term->tl_vterm == NULL)
4579 return;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004580 init_default_colors(term, curwin);
4581 vterm_state_set_default_colors(
4582 vterm_obtain_state(term->tl_vterm),
4583 &term->tl_default_color.fg,
4584 &term->tl_default_color.bg);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01004585
Bram Moolenaarad431992021-05-03 20:40:38 +02004586 FOR_ALL_WINDOWS(wp)
4587 if (wp->w_buffer == term->tl_buffer)
4588 redraw_win_later(wp, NOT_VALID);
4589}
4590
4591/*
4592 * Called when 'background' was set.
4593 */
4594 void
4595term_update_colors_all(void)
4596{
4597 term_T *tp;
4598
4599 FOR_ALL_TERMS(tp)
4600 term_update_colors(tp);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004601}
4602
4603/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004604 * Return the text to show for the buffer name and status.
4605 */
4606 char_u *
4607term_get_status_text(term_T *term)
4608{
4609 if (term->tl_status_text == NULL)
4610 {
4611 char_u *txt;
4612 size_t len;
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004613 char_u *fname;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004614
4615 if (term->tl_normal_mode)
4616 {
4617 if (term_job_running(term))
4618 txt = (char_u *)_("Terminal");
4619 else
4620 txt = (char_u *)_("Terminal-finished");
4621 }
4622 else if (term->tl_title != NULL)
4623 txt = term->tl_title;
4624 else if (term_none_open(term))
4625 txt = (char_u *)_("active");
4626 else if (term_job_running(term))
4627 txt = (char_u *)_("running");
4628 else
4629 txt = (char_u *)_("finished");
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004630 fname = buf_get_fname(term->tl_buffer);
4631 len = 9 + STRLEN(fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004632 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004633 if (term->tl_status_text != NULL)
4634 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
Bram Moolenaar00806bc2020-11-05 19:36:38 +01004635 fname, txt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004636 }
4637 return term->tl_status_text;
4638}
4639
4640/*
Bram Moolenaar3ad69532021-11-19 17:01:08 +00004641 * Clear the cached value of the status text.
4642 */
4643 void
4644term_clear_status_text(term_T *term)
4645{
4646 VIM_CLEAR(term->tl_status_text);
4647}
4648
4649/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004650 * Mark references in jobs of terminals.
4651 */
4652 int
4653set_ref_in_term(int copyID)
4654{
4655 int abort = FALSE;
4656 term_T *term;
4657 typval_T tv;
4658
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004659 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004660 if (term->tl_job != NULL)
4661 {
4662 tv.v_type = VAR_JOB;
4663 tv.vval.v_job = term->tl_job;
4664 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4665 }
4666 return abort;
4667}
4668
4669/*
4670 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004671 * Returns NULL when the buffer is not for a terminal window and logs a message
4672 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004673 */
4674 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004675term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004676{
4677 buf_T *buf;
4678
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004679 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004680 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004681 --emsg_off;
4682 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004683 {
Bram Moolenaar4d05af02020-11-27 20:55:00 +01004684 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004685 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004686 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004687 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004688 return buf;
4689}
4690
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004691 static void
4692clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004693{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004694 CLEAR_FIELD(*cell);
4695 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4696 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004697}
4698
4699 static void
4700dump_term_color(FILE *fd, VTermColor *color)
4701{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004702 int index;
4703
4704 if (VTERM_COLOR_IS_INDEXED(color))
4705 index = color->index + 1;
4706 else if (color->type == 0)
4707 // use RGB values
4708 index = 255;
4709 else
4710 // default color
4711 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004712 fprintf(fd, "%02x%02x%02x%d",
4713 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004714 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004715}
4716
4717/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004718 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004719 *
4720 * Each screen cell in full is:
4721 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4722 * {characters} is a space for an empty cell
4723 * For a double-width character "+" is changed to "*" and the next cell is
4724 * skipped.
4725 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4726 * when "&" use the same as the previous cell.
4727 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4728 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4729 * {color-idx} is a number from 0 to 255
4730 *
4731 * Screen cell with same width, attributes and color as the previous one:
4732 * |{characters}
4733 *
4734 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4735 *
4736 * Repeating the previous screen cell:
4737 * @{count}
4738 */
4739 void
4740f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4741{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004742 buf_T *buf;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004743 term_T *term;
4744 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004745 int max_height = 0;
4746 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004747 stat_T st;
4748 FILE *fd;
4749 VTermPos pos;
4750 VTermScreen *screen;
4751 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004752 VTermState *state;
4753 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004754
4755 if (check_restricted() || check_secure())
4756 return;
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02004757
4758 if (in_vim9script()
4759 && (check_for_buffer_arg(argvars, 0) == FAIL
4760 || check_for_string_arg(argvars, 1) == FAIL
4761 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4762 return;
4763
4764 buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004765 if (buf == NULL)
4766 return;
4767 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004768 if (term->tl_vterm == NULL)
4769 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004770 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004771 return;
4772 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004773
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004774 if (argvars[2].v_type != VAR_UNKNOWN)
4775 {
4776 dict_T *d;
4777
4778 if (argvars[2].v_type != VAR_DICT)
4779 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004780 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004781 return;
4782 }
4783 d = argvars[2].vval.v_dict;
4784 if (d != NULL)
4785 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004786 max_height = dict_get_number(d, (char_u *)"rows");
4787 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004788 }
4789 }
4790
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004791 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004792 if (fname == NULL)
4793 return;
4794 if (mch_stat((char *)fname, &st) >= 0)
4795 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004796 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004797 return;
4798 }
4799
Bram Moolenaard96ff162018-02-18 22:13:29 +01004800 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4801 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004802 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004803 return;
4804 }
4805
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004806 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004807
4808 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004809 state = vterm_obtain_state(term->tl_vterm);
4810 vterm_state_get_cursorpos(state, &cursor_pos);
4811
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004812 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4813 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004814 {
4815 int repeat = 0;
4816
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004817 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4818 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004819 {
4820 VTermScreenCell cell;
4821 int same_attr;
4822 int same_chars = TRUE;
4823 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004824 int is_cursor_pos = (pos.col == cursor_pos.col
4825 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004826
4827 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004828 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004829
4830 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4831 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004832 int c = cell.chars[i];
4833 int pc = prev_cell.chars[i];
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004834 int should_break = c == NUL || pc == NUL;
Bram Moolenaar47015b82018-03-23 22:10:34 +01004835
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004836 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004837 if (i == 0)
4838 {
4839 c = (c == NUL) ? ' ' : c;
4840 pc = (pc == NUL) ? ' ' : pc;
4841 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004842 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004843 same_chars = FALSE;
Bram Moolenaar9c24cd12020-10-23 15:40:39 +02004844 if (should_break)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004845 break;
4846 }
4847 same_attr = vtermAttr2hl(cell.attrs)
4848 == vtermAttr2hl(prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004849 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4850 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004851 if (same_chars && cell.width == prev_cell.width && same_attr
4852 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004853 {
4854 ++repeat;
4855 }
4856 else
4857 {
4858 if (repeat > 0)
4859 {
4860 fprintf(fd, "@%d", repeat);
4861 repeat = 0;
4862 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004863 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004864
4865 if (cell.chars[0] == NUL)
4866 fputs(" ", fd);
4867 else
4868 {
4869 char_u charbuf[10];
4870 int len;
4871
4872 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4873 && cell.chars[i] != NUL; ++i)
4874 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004875 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004876 fwrite(charbuf, len, 1, fd);
4877 }
4878 }
4879
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004880 // When only the characters differ we don't write anything, the
4881 // following "|", "@" or NL will indicate using the same
4882 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004883 if (cell.width != prev_cell.width || !same_attr)
4884 {
4885 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004886 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004887 else
4888 fputs("+", fd);
4889
4890 if (same_attr)
4891 {
4892 fputs("&", fd);
4893 }
4894 else
4895 {
4896 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004897 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004898 fputs("&", fd);
4899 else
4900 {
4901 fputs("#", fd);
4902 dump_term_color(fd, &cell.fg);
4903 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004904 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004905 fputs("&", fd);
4906 else
4907 {
4908 fputs("#", fd);
4909 dump_term_color(fd, &cell.bg);
4910 }
4911 }
4912 }
4913
4914 prev_cell = cell;
4915 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004916
4917 if (cell.width == 2)
4918 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004919 }
4920 if (repeat > 0)
4921 fprintf(fd, "@%d", repeat);
4922 fputs("\n", fd);
4923 }
4924
4925 fclose(fd);
4926}
4927
4928/*
4929 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4930 */
4931 static void
4932dump_is_corrupt(garray_T *gap)
4933{
4934 ga_concat(gap, (char_u *)"CORRUPT");
4935}
4936
4937 static void
4938append_cell(garray_T *gap, cellattr_T *cell)
4939{
4940 if (ga_grow(gap, 1) == OK)
4941 {
4942 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4943 ++gap->ga_len;
4944 }
4945}
4946
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004947 static void
4948clear_cellattr(cellattr_T *cell)
4949{
4950 CLEAR_FIELD(*cell);
4951 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4952 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4953}
4954
Bram Moolenaard96ff162018-02-18 22:13:29 +01004955/*
4956 * Read the dump file from "fd" and append lines to the current buffer.
4957 * Return the cell width of the longest line.
4958 */
4959 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004960read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004961{
4962 int c;
4963 garray_T ga_text;
4964 garray_T ga_cell;
4965 char_u *prev_char = NULL;
4966 int attr = 0;
4967 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004968 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004969 term_T *term = curbuf->b_term;
4970 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004971 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004972
4973 ga_init2(&ga_text, 1, 90);
4974 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004975 clear_cellattr(&cell);
4976 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004977 cursor_pos->row = -1;
4978 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004979
4980 c = fgetc(fd);
4981 for (;;)
4982 {
4983 if (c == EOF)
4984 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004985 if (c == '\r')
4986 {
4987 // DOS line endings? Ignore.
4988 c = fgetc(fd);
4989 }
4990 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004991 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004992 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004993 if (ga_text.ga_data == NULL)
4994 dump_is_corrupt(&ga_text);
4995 if (ga_grow(&term->tl_scrollback, 1) == OK)
4996 {
4997 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
4998 + term->tl_scrollback.ga_len;
4999
5000 if (max_cells < ga_cell.ga_len)
5001 max_cells = ga_cell.ga_len;
5002 line->sb_cols = ga_cell.ga_len;
5003 line->sb_cells = ga_cell.ga_data;
5004 line->sb_fill_attr = term->tl_default_color;
5005 ++term->tl_scrollback.ga_len;
5006 ga_init(&ga_cell);
5007
5008 ga_append(&ga_text, NUL);
5009 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5010 ga_text.ga_len, FALSE);
5011 }
5012 else
5013 ga_clear(&ga_cell);
5014 ga_text.ga_len = 0;
5015
5016 c = fgetc(fd);
5017 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01005018 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005019 {
5020 int prev_len = ga_text.ga_len;
5021
Bram Moolenaar9271d052018-02-25 21:39:46 +01005022 if (c == '>')
5023 {
5024 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005025 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005026 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
5027 cursor_pos->col = ga_cell.ga_len;
5028 }
5029
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005030 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005031 c = fgetc(fd);
5032 if (c != EOF)
5033 ga_append(&ga_text, c);
5034 for (;;)
5035 {
5036 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01005037 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01005038 || c == EOF || c == '\n')
5039 break;
5040 ga_append(&ga_text, c);
5041 }
5042
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005043 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01005044 vim_free(prev_char);
5045 if (ga_text.ga_data != NULL)
5046 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
5047 ga_text.ga_len - prev_len);
5048
Bram Moolenaar9271d052018-02-25 21:39:46 +01005049 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01005050 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005051 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005052 }
5053 else if (c == '+' || c == '*')
5054 {
5055 int is_bg;
5056
5057 cell.width = c == '+' ? 1 : 2;
5058
5059 c = fgetc(fd);
5060 if (c == '&')
5061 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005062 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01005063 c = fgetc(fd);
5064 }
5065 else if (isdigit(c))
5066 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005067 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01005068 attr = 0;
5069 while (isdigit(c))
5070 {
5071 attr = attr * 10 + (c - '0');
5072 c = fgetc(fd);
5073 }
5074 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005075
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005076 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005077 for (is_bg = 0; is_bg <= 1; ++is_bg)
5078 {
5079 if (c == '&')
5080 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005081 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005082 c = fgetc(fd);
5083 }
5084 else if (c == '#')
5085 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005086 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005087
5088 c = fgetc(fd);
5089 red = hex2nr(c);
5090 c = fgetc(fd);
5091 red = (red << 4) + hex2nr(c);
5092 c = fgetc(fd);
5093 green = hex2nr(c);
5094 c = fgetc(fd);
5095 green = (green << 4) + hex2nr(c);
5096 c = fgetc(fd);
5097 blue = hex2nr(c);
5098 c = fgetc(fd);
5099 blue = (blue << 4) + hex2nr(c);
5100 c = fgetc(fd);
5101 if (!isdigit(c))
5102 dump_is_corrupt(&ga_text);
5103 while (isdigit(c))
5104 {
5105 index = index * 10 + (c - '0');
5106 c = fgetc(fd);
5107 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005108 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005109 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005110 type = VTERM_COLOR_RGB;
5111 if (index == 0)
5112 {
5113 if (is_bg)
5114 type |= VTERM_COLOR_DEFAULT_BG;
5115 else
5116 type |= VTERM_COLOR_DEFAULT_FG;
5117 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005118 }
5119 else
5120 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005121 type = VTERM_COLOR_INDEXED;
5122 index -= 1;
5123 }
5124 if (is_bg)
5125 {
5126 cell.bg.type = type;
5127 cell.bg.red = red;
5128 cell.bg.green = green;
5129 cell.bg.blue = blue;
5130 cell.bg.index = index;
5131 }
5132 else
5133 {
5134 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005135 cell.fg.red = red;
5136 cell.fg.green = green;
5137 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005138 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005139 }
5140 }
5141 else
5142 dump_is_corrupt(&ga_text);
5143 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005144 }
5145 else
5146 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005147 }
5148 else
5149 dump_is_corrupt(&ga_text);
5150
5151 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01005152 if (cell.width == 2)
5153 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005154 }
5155 else if (c == '@')
5156 {
5157 if (prev_char == NULL)
5158 dump_is_corrupt(&ga_text);
5159 else
5160 {
5161 int count = 0;
5162
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005163 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01005164 for (;;)
5165 {
5166 c = fgetc(fd);
5167 if (!isdigit(c))
5168 break;
5169 count = count * 10 + (c - '0');
5170 }
5171
5172 while (count-- > 0)
5173 {
5174 ga_concat(&ga_text, prev_char);
5175 append_cell(&ga_cell, &cell);
5176 }
5177 }
5178 }
5179 else
5180 {
5181 dump_is_corrupt(&ga_text);
5182 c = fgetc(fd);
5183 }
5184 }
5185
5186 if (ga_text.ga_len > 0)
5187 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005188 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005189 dump_is_corrupt(&ga_text);
5190 ga_append(&ga_text, NUL);
5191 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5192 ga_text.ga_len, FALSE);
5193 }
5194
5195 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005196 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005197 vim_free(prev_char);
5198
5199 return max_cells;
5200}
5201
5202/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005203 * Return an allocated string with at least "text_width" "=" characters and
5204 * "fname" inserted in the middle.
5205 */
5206 static char_u *
5207get_separator(int text_width, char_u *fname)
5208{
5209 int width = MAX(text_width, curwin->w_width);
5210 char_u *textline;
5211 int fname_size;
5212 char_u *p = fname;
5213 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005214 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005215
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005216 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005217 if (textline == NULL)
5218 return NULL;
5219
5220 fname_size = vim_strsize(fname);
5221 if (fname_size < width - 8)
5222 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005223 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005224 width = MAX(text_width, fname_size + 8);
5225 }
5226 else if (fname_size > width - 8)
5227 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005228 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005229 p = gettail(fname);
5230 fname_size = vim_strsize(p);
5231 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005232 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005233 while (fname_size > width - 8)
5234 {
5235 p += (*mb_ptr2len)(p);
5236 fname_size = vim_strsize(p);
5237 }
5238
5239 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5240 textline[i] = '=';
5241 textline[i++] = ' ';
5242
5243 STRCPY(textline + i, p);
5244 off = STRLEN(textline);
5245 textline[off] = ' ';
5246 for (i = 1; i < (width - fname_size) / 2; ++i)
5247 textline[off + i] = '=';
5248 textline[off + i] = NUL;
5249
5250 return textline;
5251}
5252
5253/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005254 * Common for "term_dumpdiff()" and "term_dumpload()".
5255 */
5256 static void
5257term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5258{
5259 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005260 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005261 char_u buf1[NUMBUFLEN];
5262 char_u buf2[NUMBUFLEN];
5263 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005264 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005265 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005266 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005267 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005268 char_u *textline = NULL;
5269
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005270 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005271 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005272 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005273 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005274 if (fname1 == NULL || (do_diff && fname2 == NULL))
5275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005276 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005277 return;
5278 }
5279 fd1 = mch_fopen((char *)fname1, READBIN);
5280 if (fd1 == NULL)
5281 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005282 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005283 return;
5284 }
5285 if (do_diff)
5286 {
5287 fd2 = mch_fopen((char *)fname2, READBIN);
5288 if (fd2 == NULL)
5289 {
5290 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005291 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005292 return;
5293 }
5294 }
5295
5296 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005297 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5298 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5299 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5300 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5301 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005302
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005303 if (opt.jo_term_name == NULL)
5304 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005305 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005306
Bram Moolenaar51e14382019-05-25 20:21:28 +02005307 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005308 if (fname_tofree != NULL)
5309 {
5310 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5311 opt.jo_term_name = fname_tofree;
5312 }
5313 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005314
Bram Moolenaar87abab92019-06-03 21:14:59 +02005315 if (opt.jo_bufnr_buf != NULL)
5316 {
5317 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5318
5319 // With "bufnr" argument: enter the window with this buffer and make it
5320 // empty.
5321 if (wp == NULL)
5322 semsg(_(e_invarg2), "bufnr");
5323 else
5324 {
5325 buf = curbuf;
5326 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005327 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005328 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005329 redraw_later(NOT_VALID);
5330 }
5331 }
5332 else
5333 // Create a new terminal window.
5334 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5335
Bram Moolenaard96ff162018-02-18 22:13:29 +01005336 if (buf != NULL && buf->b_term != NULL)
5337 {
5338 int i;
5339 linenr_T bot_lnum;
5340 linenr_T lnum;
5341 term_T *term = buf->b_term;
5342 int width;
5343 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005344 VTermPos cursor_pos1;
5345 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005346
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005347 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005348
Bram Moolenaard96ff162018-02-18 22:13:29 +01005349 rettv->vval.v_number = buf->b_fnum;
5350
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005351 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005352 width = read_dump_file(fd1, &cursor_pos1);
5353
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005354 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005355 if (cursor_pos1.row >= 0)
5356 {
5357 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5358 coladvance(cursor_pos1.col);
5359 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005360
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005361 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005362 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005363
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005364 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005365 if (!do_diff)
5366 goto theend;
5367
5368 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5369
Bram Moolenaar4a696342018-04-05 18:45:26 +02005370 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005371 if (textline == NULL)
5372 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005373 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5374 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5375 vim_free(textline);
5376
5377 textline = get_separator(width, fname2);
5378 if (textline == NULL)
5379 goto theend;
5380 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5381 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005382 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005383
5384 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005385 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005386 if (width2 > width)
5387 {
5388 vim_free(textline);
5389 textline = alloc(width2 + 1);
5390 if (textline == NULL)
5391 goto theend;
5392 width = width2;
5393 textline[width] = NUL;
5394 }
5395 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5396
5397 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5398 {
5399 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5400 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005401 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005402 for (i = 0; i < width; ++i)
5403 textline[i] = '-';
5404 }
5405 else
5406 {
5407 char_u *line1;
5408 char_u *line2;
5409 char_u *p1;
5410 char_u *p2;
5411 int col;
5412 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5413 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5414 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5415 ->sb_cells;
5416
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005417 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005418 line1 = vim_strsave(ml_get(lnum));
5419 if (line1 == NULL)
5420 break;
5421 p1 = line1;
5422
5423 line2 = ml_get(lnum + bot_lnum);
5424 p2 = line2;
5425 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5426 {
5427 int len1 = utfc_ptr2len(p1);
5428 int len2 = utfc_ptr2len(p2);
5429
5430 textline[col] = ' ';
5431 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005432 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005433 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005434 else if (lnum == cursor_pos1.row + 1
5435 && col == cursor_pos1.col
5436 && (cursor_pos1.row != cursor_pos2.row
5437 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005438 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005439 textline[col] = '>';
5440 else if (lnum == cursor_pos2.row + 1
5441 && col == cursor_pos2.col
5442 && (cursor_pos1.row != cursor_pos2.row
5443 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005444 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005445 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005446 else if (cellattr1 != NULL && cellattr2 != NULL)
5447 {
5448 if ((cellattr1 + col)->width
5449 != (cellattr2 + col)->width)
5450 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005451 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005452 &(cellattr2 + col)->fg))
5453 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005454 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005455 &(cellattr2 + col)->bg))
5456 textline[col] = 'b';
5457 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5458 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5459 textline[col] = 'a';
5460 }
5461 p1 += len1;
5462 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005463 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005464 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005465
5466 while (col < width)
5467 {
5468 if (*p1 == NUL && *p2 == NUL)
5469 textline[col] = '?';
5470 else if (*p1 == NUL)
5471 {
5472 textline[col] = '+';
5473 p2 += utfc_ptr2len(p2);
5474 }
5475 else
5476 {
5477 textline[col] = '-';
5478 p1 += utfc_ptr2len(p1);
5479 }
5480 ++col;
5481 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005482
5483 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005484 }
5485 if (add_empty_scrollback(term, &term->tl_default_color,
5486 term->tl_top_diff_rows) == OK)
5487 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5488 ++bot_lnum;
5489 }
5490
5491 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5492 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005493 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005494 for (i = 0; i < width; ++i)
5495 textline[i] = '+';
5496 if (add_empty_scrollback(term, &term->tl_default_color,
5497 term->tl_top_diff_rows) == OK)
5498 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5499 ++lnum;
5500 ++bot_lnum;
5501 }
5502
5503 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005504
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005505 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005506 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005507 }
5508
5509theend:
5510 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005511 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005512 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005513 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005514 fclose(fd2);
5515}
5516
5517/*
5518 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5519 * bottom files.
5520 * Return FAIL when this is not possible.
5521 */
5522 int
5523term_swap_diff()
5524{
5525 term_T *term = curbuf->b_term;
5526 linenr_T line_count;
5527 linenr_T top_rows;
5528 linenr_T bot_rows;
5529 linenr_T bot_start;
5530 linenr_T lnum;
5531 char_u *p;
5532 sb_line_T *sb_line;
5533
5534 if (term == NULL
5535 || !term_is_finished(curbuf)
5536 || term->tl_top_diff_rows == 0
5537 || term->tl_scrollback.ga_len == 0)
5538 return FAIL;
5539
5540 line_count = curbuf->b_ml.ml_line_count;
5541 top_rows = term->tl_top_diff_rows;
5542 bot_rows = term->tl_bot_diff_rows;
5543 bot_start = line_count - bot_rows;
5544 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5545
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005546 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005547 for (lnum = 1; lnum <= top_rows; ++lnum)
5548 {
5549 p = vim_strsave(ml_get(1));
5550 if (p == NULL)
5551 return OK;
5552 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005553 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005554 vim_free(p);
5555 }
5556
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005557 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005558 for (lnum = 1; lnum <= bot_rows; ++lnum)
5559 {
5560 p = vim_strsave(ml_get(bot_start + lnum));
5561 if (p == NULL)
5562 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005563 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005564 ml_append(lnum - 1, p, 0, FALSE);
5565 vim_free(p);
5566 }
5567
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005568 // move top title to bottom
5569 p = vim_strsave(ml_get(bot_rows + 1));
5570 if (p == NULL)
5571 return OK;
5572 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005573 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005574 vim_free(p);
5575
5576 // move bottom title to top
5577 p = vim_strsave(ml_get(line_count - top_rows));
5578 if (p == NULL)
5579 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005580 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005581 ml_append(bot_rows, p, 0, FALSE);
5582 vim_free(p);
5583
Bram Moolenaard96ff162018-02-18 22:13:29 +01005584 if (top_rows == bot_rows)
5585 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005586 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005587 for (lnum = 0; lnum < top_rows; ++lnum)
5588 {
5589 sb_line_T temp;
5590
5591 temp = *(sb_line + lnum);
5592 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5593 *(sb_line + bot_start + lnum) = temp;
5594 }
5595 }
5596 else
5597 {
5598 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005599 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005600
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005601 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005602 if (temp != NULL)
5603 {
5604 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5605 mch_memmove(term->tl_scrollback.ga_data,
5606 temp + bot_start,
5607 sizeof(sb_line_T) * bot_rows);
5608 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5609 temp + top_rows,
5610 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5611 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5612 + line_count - top_rows,
5613 temp,
5614 sizeof(sb_line_T) * top_rows);
5615 vim_free(temp);
5616 }
5617 }
5618
5619 term->tl_top_diff_rows = bot_rows;
5620 term->tl_bot_diff_rows = top_rows;
5621
5622 update_screen(NOT_VALID);
5623 return OK;
5624}
5625
5626/*
5627 * "term_dumpdiff(filename, filename, options)" function
5628 */
5629 void
5630f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5631{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02005632 if (in_vim9script()
5633 && (check_for_string_arg(argvars, 0) == FAIL
5634 || check_for_string_arg(argvars, 1) == FAIL
5635 || check_for_opt_dict_arg(argvars, 2) == FAIL))
5636 return;
5637
Bram Moolenaard96ff162018-02-18 22:13:29 +01005638 term_load_dump(argvars, rettv, TRUE);
5639}
5640
5641/*
5642 * "term_dumpload(filename, options)" function
5643 */
5644 void
5645f_term_dumpload(typval_T *argvars, typval_T *rettv)
5646{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005647 if (in_vim9script()
5648 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmananfc3b7752021-09-08 14:57:42 +02005649 || check_for_opt_dict_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005650 return;
5651
Bram Moolenaard96ff162018-02-18 22:13:29 +01005652 term_load_dump(argvars, rettv, FALSE);
5653}
5654
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005655/*
5656 * "term_getaltscreen(buf)" function
5657 */
5658 void
5659f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5660{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005661 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005662
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005663 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5664 return;
5665
5666 buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005667 if (buf == NULL)
5668 return;
5669 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5670}
5671
5672/*
5673 * "term_getattr(attr, name)" function
5674 */
5675 void
5676f_term_getattr(typval_T *argvars, typval_T *rettv)
5677{
5678 int attr;
5679 size_t i;
5680 char_u *name;
5681
5682 static struct {
5683 char *name;
5684 int attr;
5685 } attrs[] = {
5686 {"bold", HL_BOLD},
5687 {"italic", HL_ITALIC},
5688 {"underline", HL_UNDERLINE},
5689 {"strike", HL_STRIKETHROUGH},
5690 {"reverse", HL_INVERSE},
5691 };
5692
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +02005693 if (in_vim9script()
5694 && (check_for_number_arg(argvars, 0) == FAIL
5695 || check_for_string_arg(argvars, 1) == FAIL))
5696 return;
5697
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005698 attr = tv_get_number(&argvars[0]);
5699 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005700 if (name == NULL)
5701 return;
5702
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005703 if (attr > HL_ALL)
5704 attr = syn_attr2attr(attr);
K.Takataeeec2542021-06-02 13:28:16 +02005705 for (i = 0; i < ARRAY_LENGTH(attrs); ++i)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005706 if (STRCMP(name, attrs[i].name) == 0)
5707 {
5708 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5709 break;
5710 }
5711}
5712
5713/*
5714 * "term_getcursor(buf)" function
5715 */
5716 void
5717f_term_getcursor(typval_T *argvars, typval_T *rettv)
5718{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005719 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005720 term_T *term;
5721 list_T *l;
5722 dict_T *d;
5723
5724 if (rettv_list_alloc(rettv) == FAIL)
5725 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005726
5727 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5728 return;
5729
5730 buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005731 if (buf == NULL)
5732 return;
5733 term = buf->b_term;
5734
5735 l = rettv->vval.v_list;
5736 list_append_number(l, term->tl_cursor_pos.row + 1);
5737 list_append_number(l, term->tl_cursor_pos.col + 1);
5738
5739 d = dict_alloc();
5740 if (d != NULL)
5741 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005742 dict_add_number(d, "visible", term->tl_cursor_visible);
5743 dict_add_number(d, "blink", blink_state_is_inverted()
5744 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5745 dict_add_number(d, "shape", term->tl_cursor_shape);
5746 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005747 list_append_dict(l, d);
5748 }
5749}
5750
5751/*
5752 * "term_getjob(buf)" function
5753 */
5754 void
5755f_term_getjob(typval_T *argvars, typval_T *rettv)
5756{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005757 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005758
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005759 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5760 return;
5761
5762 buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005763 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005764 {
5765 rettv->v_type = VAR_SPECIAL;
5766 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005767 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005768 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005769
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005770 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005771 rettv->vval.v_job = buf->b_term->tl_job;
5772 if (rettv->vval.v_job != NULL)
5773 ++rettv->vval.v_job->jv_refcount;
5774}
5775
5776 static int
5777get_row_number(typval_T *tv, term_T *term)
5778{
5779 if (tv->v_type == VAR_STRING
5780 && tv->vval.v_string != NULL
5781 && STRCMP(tv->vval.v_string, ".") == 0)
5782 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005783 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005784}
5785
5786/*
5787 * "term_getline(buf, row)" function
5788 */
5789 void
5790f_term_getline(typval_T *argvars, typval_T *rettv)
5791{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005792 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005793 term_T *term;
5794 int row;
5795
5796 rettv->v_type = VAR_STRING;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005797
5798 if (in_vim9script()
5799 && (check_for_buffer_arg(argvars, 0) == FAIL
5800 || check_for_lnum_arg(argvars, 1) == FAIL))
5801 return;
5802
5803 buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005804 if (buf == NULL)
5805 return;
5806 term = buf->b_term;
5807 row = get_row_number(&argvars[1], term);
5808
5809 if (term->tl_vterm == NULL)
5810 {
5811 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5812
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005813 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005814 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5815 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5816 }
5817 else
5818 {
5819 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5820 VTermRect rect;
5821 int len;
5822 char_u *p;
5823
5824 if (row < 0 || row >= term->tl_rows)
5825 return;
5826 len = term->tl_cols * MB_MAXBYTES + 1;
5827 p = alloc(len);
5828 if (p == NULL)
5829 return;
5830 rettv->vval.v_string = p;
5831
5832 rect.start_col = 0;
5833 rect.end_col = term->tl_cols;
5834 rect.start_row = row;
5835 rect.end_row = row + 1;
5836 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5837 }
5838}
5839
5840/*
5841 * "term_getscrolled(buf)" function
5842 */
5843 void
5844f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5845{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005846 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005847
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005848 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5849 return;
5850
5851 buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005852 if (buf == NULL)
5853 return;
5854 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5855}
5856
5857/*
5858 * "term_getsize(buf)" function
5859 */
5860 void
5861f_term_getsize(typval_T *argvars, typval_T *rettv)
5862{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005863 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005864 list_T *l;
5865
5866 if (rettv_list_alloc(rettv) == FAIL)
5867 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005868
5869 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5870 return;
5871
5872 buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005873 if (buf == NULL)
5874 return;
5875
5876 l = rettv->vval.v_list;
5877 list_append_number(l, buf->b_term->tl_rows);
5878 list_append_number(l, buf->b_term->tl_cols);
5879}
5880
5881/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005882 * "term_setsize(buf, rows, cols)" function
5883 */
5884 void
5885f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5886{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005887 buf_T *buf;
Bram Moolenaara42d3632018-04-14 17:05:38 +02005888 term_T *term;
5889 varnumber_T rows, cols;
5890
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005891 if (in_vim9script()
5892 && (check_for_buffer_arg(argvars, 0) == FAIL
5893 || check_for_number_arg(argvars, 1) == FAIL
5894 || check_for_number_arg(argvars, 2) == FAIL))
5895 return;
5896
5897 buf = term_get_buf(argvars, "term_setsize()");
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005898 if (buf == NULL)
5899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005900 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005901 return;
5902 }
5903 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005904 return;
5905 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005906 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005907 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005908 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005909 cols = cols <= 0 ? term->tl_cols : cols;
5910 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005911 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005912
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005913 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005914 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5915 term_report_winsize(term, term->tl_rows, term->tl_cols);
5916}
5917
5918/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005919 * "term_getstatus(buf)" function
5920 */
5921 void
5922f_term_getstatus(typval_T *argvars, typval_T *rettv)
5923{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005924 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005925 term_T *term;
5926 char_u val[100];
5927
5928 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005929
5930 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5931 return;
5932
5933 buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005934 if (buf == NULL)
5935 return;
5936 term = buf->b_term;
5937
5938 if (term_job_running(term))
5939 STRCPY(val, "running");
5940 else
5941 STRCPY(val, "finished");
5942 if (term->tl_normal_mode)
5943 STRCAT(val, ",normal");
5944 rettv->vval.v_string = vim_strsave(val);
5945}
5946
5947/*
5948 * "term_gettitle(buf)" function
5949 */
5950 void
5951f_term_gettitle(typval_T *argvars, typval_T *rettv)
5952{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005953 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005954
5955 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02005956
5957 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
5958 return;
5959
5960 buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005961 if (buf == NULL)
5962 return;
5963
5964 if (buf->b_term->tl_title != NULL)
5965 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5966}
5967
5968/*
5969 * "term_gettty(buf)" function
5970 */
5971 void
5972f_term_gettty(typval_T *argvars, typval_T *rettv)
5973{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005974 buf_T *buf;
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005975 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005976 int num = 0;
5977
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005978 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02005979 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005980 || check_for_opt_bool_arg(argvars, 1) == FAIL))
5981 return;
5982
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005983 rettv->v_type = VAR_STRING;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005984 buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005985 if (buf == NULL)
5986 return;
5987 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02005988 num = tv_get_bool(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005989
5990 switch (num)
5991 {
5992 case 0:
5993 if (buf->b_term->tl_job != NULL)
5994 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005995 break;
5996 case 1:
5997 if (buf->b_term->tl_job != NULL)
5998 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005999 break;
6000 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006001 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006002 return;
6003 }
6004 if (p != NULL)
6005 rettv->vval.v_string = vim_strsave(p);
6006}
6007
6008/*
6009 * "term_list()" function
6010 */
6011 void
6012f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
6013{
6014 term_T *tp;
6015 list_T *l;
6016
6017 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
6018 return;
6019
6020 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006021 FOR_ALL_TERMS(tp)
Bram Moolenaarad431992021-05-03 20:40:38 +02006022 if (tp->tl_buffer != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006023 if (list_append_number(l,
6024 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
6025 return;
6026}
6027
6028/*
6029 * "term_scrape(buf, row)" function
6030 */
6031 void
6032f_term_scrape(typval_T *argvars, typval_T *rettv)
6033{
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006034 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006035 VTermScreen *screen = NULL;
6036 VTermPos pos;
6037 list_T *l;
6038 term_T *term;
6039 char_u *p;
6040 sb_line_T *line;
6041
6042 if (rettv_list_alloc(rettv) == FAIL)
6043 return;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006044
6045 if (in_vim9script()
6046 && (check_for_buffer_arg(argvars, 0) == FAIL
6047 || check_for_lnum_arg(argvars, 1) == FAIL))
6048 return;
6049
6050 buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006051 if (buf == NULL)
6052 return;
6053 term = buf->b_term;
6054
6055 l = rettv->vval.v_list;
6056 pos.row = get_row_number(&argvars[1], term);
6057
6058 if (term->tl_vterm != NULL)
6059 {
6060 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01006061 if (screen == NULL) // can't really happen
6062 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006063 p = NULL;
6064 line = NULL;
6065 }
6066 else
6067 {
6068 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
6069
6070 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
6071 return;
6072 p = ml_get_buf(buf, lnum + 1, FALSE);
6073 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
6074 }
6075
6076 for (pos.col = 0; pos.col < term->tl_cols; )
6077 {
6078 dict_T *dcell;
6079 int width;
6080 VTermScreenCellAttrs attrs;
6081 VTermColor fg, bg;
6082 char_u rgb[8];
6083 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
6084 int off = 0;
6085 int i;
6086
6087 if (screen == NULL)
6088 {
6089 cellattr_T *cellattr;
6090 int len;
6091
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006092 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006093 if (pos.col >= line->sb_cols)
6094 break;
6095 cellattr = line->sb_cells + pos.col;
6096 width = cellattr->width;
6097 attrs = cellattr->attrs;
6098 fg = cellattr->fg;
6099 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02006100 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006101 mch_memmove(mbs, p, len);
6102 mbs[len] = NUL;
6103 p += len;
6104 }
6105 else
6106 {
6107 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02006108
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006109 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
6110 break;
6111 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
6112 {
6113 if (cell.chars[i] == 0)
6114 break;
6115 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
6116 }
6117 mbs[off] = NUL;
6118 width = cell.width;
6119 attrs = cell.attrs;
6120 fg = cell.fg;
6121 bg = cell.bg;
6122 }
6123 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01006124 if (dcell == NULL)
6125 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006126 list_append_dict(l, dcell);
6127
Bram Moolenaare0be1672018-07-08 16:50:37 +02006128 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006129
6130 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6131 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006132 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006133 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
6134 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006135 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006136
Bram Moolenaar83d47902020-03-26 20:34:00 +01006137 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02006138 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006139
6140 ++pos.col;
6141 if (width == 2)
6142 ++pos.col;
6143 }
6144}
6145
6146/*
6147 * "term_sendkeys(buf, keys)" function
6148 */
6149 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01006150f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006151{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006152 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006153 char_u *msg;
6154 term_T *term;
6155
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006156 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006157 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006158 || check_for_string_arg(argvars, 1) == FAIL))
6159 return;
6160
6161 buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006162 if (buf == NULL)
6163 return;
6164
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006165 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006166 if (msg == NULL)
6167 return;
6168 term = buf->b_term;
6169 if (term->tl_vterm == NULL)
6170 return;
6171
6172 while (*msg != NUL)
6173 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02006174 int c;
6175
6176 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
6177 {
6178 c = TO_SPECIAL(msg[1], msg[2]);
6179 msg += 3;
6180 }
6181 else
6182 {
6183 c = PTR2CHAR(msg);
6184 msg += MB_CPTR2LEN(msg);
6185 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006186 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006187 }
6188}
6189
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006190#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
6191/*
6192 * "term_getansicolors(buf)" function
6193 */
6194 void
6195f_term_getansicolors(typval_T *argvars, typval_T *rettv)
6196{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006197 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006198 term_T *term;
6199 VTermState *state;
6200 VTermColor color;
6201 char_u hexbuf[10];
6202 int index;
6203 list_T *list;
6204
6205 if (rettv_list_alloc(rettv) == FAIL)
6206 return;
6207
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006208 if (in_vim9script() && check_for_buffer_arg(argvars, 0) == FAIL)
6209 return;
6210
6211 buf = term_get_buf(argvars, "term_getansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006212 if (buf == NULL)
6213 return;
6214 term = buf->b_term;
6215 if (term->tl_vterm == NULL)
6216 return;
6217
6218 list = rettv->vval.v_list;
6219 state = vterm_obtain_state(term->tl_vterm);
6220 for (index = 0; index < 16; index++)
6221 {
6222 vterm_state_get_palette_color(state, index, &color);
6223 sprintf((char *)hexbuf, "#%02x%02x%02x",
6224 color.red, color.green, color.blue);
6225 if (list_append_string(list, hexbuf, 7) == FAIL)
6226 return;
6227 }
6228}
6229
6230/*
6231 * "term_setansicolors(buf, list)" function
6232 */
6233 void
6234f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
6235{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006236 buf_T *buf;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006237 term_T *term;
6238
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006239 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02006240 && (check_for_buffer_arg(argvars, 0) == FAIL
6241 || check_for_list_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006242 return;
6243
6244 buf = term_get_buf(argvars, "term_setansicolors()");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006245 if (buf == NULL)
6246 return;
6247 term = buf->b_term;
6248 if (term->tl_vterm == NULL)
6249 return;
6250
6251 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
6252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006253 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006254 return;
6255 }
6256
6257 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006258 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006259}
6260#endif
6261
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006262/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006263 * "term_setapi(buf, api)" function
6264 */
6265 void
6266f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6267{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006268 buf_T *buf;
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006269 term_T *term;
6270 char_u *api;
6271
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006272 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006273 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006274 || check_for_string_arg(argvars, 1) == FAIL))
6275 return;
6276
6277 buf = term_get_buf(argvars, "term_setapi()");
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006278 if (buf == NULL)
6279 return;
6280 term = buf->b_term;
6281 vim_free(term->tl_api);
6282 api = tv_get_string_chk(&argvars[1]);
6283 if (api != NULL)
6284 term->tl_api = vim_strsave(api);
6285 else
6286 term->tl_api = NULL;
6287}
6288
6289/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006290 * "term_setrestore(buf, command)" function
6291 */
6292 void
6293f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6294{
6295#if defined(FEAT_SESSION)
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006296 buf_T *buf;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006297 term_T *term;
6298 char_u *cmd;
6299
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006300 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006301 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006302 || check_for_string_arg(argvars, 1) == FAIL))
6303 return;
6304
6305 buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006306 if (buf == NULL)
6307 return;
6308 term = buf->b_term;
6309 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006310 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006311 if (cmd != NULL)
6312 term->tl_command = vim_strsave(cmd);
6313 else
6314 term->tl_command = NULL;
6315#endif
6316}
6317
6318/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006319 * "term_setkill(buf, how)" function
6320 */
6321 void
6322f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6323{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006324 buf_T *buf;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006325 term_T *term;
6326 char_u *how;
6327
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006328 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006329 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006330 || check_for_string_arg(argvars, 1) == FAIL))
6331 return;
6332
6333 buf = term_get_buf(argvars, "term_setkill()");
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006334 if (buf == NULL)
6335 return;
6336 term = buf->b_term;
6337 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006338 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006339 if (how != NULL)
6340 term->tl_kill = vim_strsave(how);
6341 else
6342 term->tl_kill = NULL;
6343}
6344
6345/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006346 * "term_start(command, options)" function
6347 */
6348 void
6349f_term_start(typval_T *argvars, typval_T *rettv)
6350{
6351 jobopt_T opt;
6352 buf_T *buf;
6353
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006354 if (in_vim9script()
6355 && (check_for_string_or_list_arg(argvars, 0) == FAIL
6356 || check_for_opt_dict_arg(argvars, 1) == FAIL))
6357 return;
6358
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006359 init_job_options(&opt);
6360 if (argvars[1].v_type != VAR_UNKNOWN
6361 && get_job_options(&argvars[1], &opt,
6362 JO_TIMEOUT_ALL + JO_STOPONEXIT
6363 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6364 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6365 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6366 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006367 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006368 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006369 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006370 return;
6371
Bram Moolenaar13568252018-03-16 20:46:58 +01006372 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006373
6374 if (buf != NULL && buf->b_term != NULL)
6375 rettv->vval.v_number = buf->b_fnum;
6376}
6377
6378/*
6379 * "term_wait" function
6380 */
6381 void
6382f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6383{
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006384 buf_T *buf;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006385
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006386 if (in_vim9script()
Yegappan Lakshmanancd917202021-07-21 19:09:09 +02006387 && (check_for_buffer_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02006388 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +02006389 return;
6390
6391 buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006392 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006393 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006394 if (buf->b_term->tl_job == NULL)
6395 {
6396 ch_log(NULL, "term_wait(): no job to wait for");
6397 return;
6398 }
6399 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006400 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006401 return;
6402
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006403 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006404 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006405 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6406 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006407 // The job is dead, keep reading channel I/O until the channel is
6408 // closed. buf->b_term may become NULL if the terminal was closed while
6409 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006410 ch_log(NULL, "term_wait(): waiting for channel to close");
6411 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6412 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006413 term_flush_messages();
6414
Bram Moolenaard45aa552018-05-21 22:50:29 +02006415 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006416 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006417 // If the terminal is closed when the channel is closed the
6418 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006419 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006420 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006421
6422 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006423 }
6424 else
6425 {
6426 long wait = 10L;
6427
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006428 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006429
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006430 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006431 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006432 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006433 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006434
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006435 // Flushing messages on channels is hopefully sufficient.
6436 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006437 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006438 }
6439}
6440
6441/*
6442 * Called when a channel has sent all the lines to a terminal.
6443 * Send a CTRL-D to mark the end of the text.
6444 */
6445 void
6446term_send_eof(channel_T *ch)
6447{
6448 term_T *term;
6449
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006450 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006451 if (term->tl_job == ch->ch_job)
6452 {
6453 if (term->tl_eof_chars != NULL)
6454 {
6455 channel_send(ch, PART_IN, term->tl_eof_chars,
6456 (int)STRLEN(term->tl_eof_chars), NULL);
6457 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6458 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006459# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006460 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006461 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006462 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6463# endif
6464 }
6465}
6466
Bram Moolenaar113e1072019-01-20 15:30:40 +01006467#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006468 job_T *
6469term_getjob(term_T *term)
6470{
6471 return term != NULL ? term->tl_job : NULL;
6472}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006473#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006474
Bram Moolenaar4f974752019-02-17 17:44:42 +01006475# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006476
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006477///////////////////////////////////////
6478// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006479#ifdef PROTO
6480typedef int COORD;
6481typedef int DWORD;
6482typedef int HANDLE;
6483typedef int *DWORD_PTR;
6484typedef int HPCON;
6485typedef int HRESULT;
6486typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006487typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006488typedef int PSIZE_T;
6489typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006490typedef int BOOL;
6491# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006492#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006493
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006494HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6495HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6496HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006497BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6498BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6499void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006500
6501 static int
6502dyn_conpty_init(int verbose)
6503{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006504 static HMODULE hKerneldll = NULL;
6505 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006506 static struct
6507 {
6508 char *name;
6509 FARPROC *ptr;
6510 } conpty_entry[] =
6511 {
6512 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6513 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6514 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6515 {"InitializeProcThreadAttributeList",
6516 (FARPROC*)&pInitializeProcThreadAttributeList},
6517 {"UpdateProcThreadAttribute",
6518 (FARPROC*)&pUpdateProcThreadAttribute},
6519 {"DeleteProcThreadAttributeList",
6520 (FARPROC*)&pDeleteProcThreadAttributeList},
6521 {NULL, NULL}
6522 };
6523
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006524 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006525 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006526 if (verbose)
6527 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006528 return FAIL;
6529 }
6530
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006531 // No need to initialize twice.
6532 if (hKerneldll)
6533 return OK;
6534
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006535 hKerneldll = vimLoadLib("kernel32.dll");
6536 for (i = 0; conpty_entry[i].name != NULL
6537 && conpty_entry[i].ptr != NULL; ++i)
6538 {
6539 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6540 conpty_entry[i].name)) == NULL)
6541 {
6542 if (verbose)
6543 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006544 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006545 return FAIL;
6546 }
6547 }
6548
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006549 return OK;
6550}
6551
6552 static int
6553conpty_term_and_job_init(
6554 term_T *term,
6555 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006556 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006557 jobopt_T *opt,
6558 jobopt_T *orig_opt)
6559{
6560 WCHAR *cmd_wchar = NULL;
6561 WCHAR *cmd_wchar_copy = NULL;
6562 WCHAR *cwd_wchar = NULL;
6563 WCHAR *env_wchar = NULL;
6564 channel_T *channel = NULL;
6565 job_T *job = NULL;
6566 HANDLE jo = NULL;
6567 garray_T ga_cmd, ga_env;
6568 char_u *cmd = NULL;
6569 HRESULT hr;
6570 COORD consize;
6571 SIZE_T breq;
6572 PROCESS_INFORMATION proc_info;
6573 HANDLE i_theirs = NULL;
6574 HANDLE o_theirs = NULL;
6575 HANDLE i_ours = NULL;
6576 HANDLE o_ours = NULL;
6577
6578 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6579 ga_init2(&ga_env, (int)sizeof(char*), 20);
6580
6581 if (argvar->v_type == VAR_STRING)
6582 {
6583 cmd = argvar->vval.v_string;
6584 }
6585 else if (argvar->v_type == VAR_LIST)
6586 {
6587 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6588 goto failed;
6589 cmd = ga_cmd.ga_data;
6590 }
6591 if (cmd == NULL || *cmd == NUL)
6592 {
6593 emsg(_(e_invarg));
6594 goto failed;
6595 }
6596
6597 term->tl_arg0_cmd = vim_strsave(cmd);
6598
6599 cmd_wchar = enc_to_utf16(cmd, NULL);
6600
6601 if (cmd_wchar != NULL)
6602 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006603 // Request by CreateProcessW
6604 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006605 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006606 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6607 }
6608
6609 ga_clear(&ga_cmd);
6610 if (cmd_wchar == NULL)
6611 goto failed;
6612 if (opt->jo_cwd != NULL)
6613 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6614
6615 win32_build_env(opt->jo_env, &ga_env, TRUE);
6616 env_wchar = ga_env.ga_data;
6617
6618 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6619 goto failed;
6620 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6621 goto failed;
6622
6623 consize.X = term->tl_cols;
6624 consize.Y = term->tl_rows;
6625 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6626 &term->tl_conpty);
6627 if (FAILED(hr))
6628 goto failed;
6629
6630 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6631
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006632 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006633 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006634 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006635 if (!term->tl_siex.lpAttributeList)
6636 goto failed;
6637 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6638 0, &breq))
6639 goto failed;
6640 if (!pUpdateProcThreadAttribute(
6641 term->tl_siex.lpAttributeList, 0,
6642 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6643 sizeof(HPCON), NULL, NULL))
6644 goto failed;
6645
6646 channel = add_channel();
6647 if (channel == NULL)
6648 goto failed;
6649
6650 job = job_alloc();
6651 if (job == NULL)
6652 goto failed;
6653 if (argvar->v_type == VAR_STRING)
6654 {
6655 int argc;
6656
6657 build_argv_from_string(cmd, &job->jv_argv, &argc);
6658 }
6659 else
6660 {
6661 int argc;
6662
6663 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6664 }
6665
6666 if (opt->jo_set & JO_IN_BUF)
6667 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6668
6669 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6670 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006671 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006672 env_wchar, cwd_wchar,
6673 &term->tl_siex.StartupInfo, &proc_info))
6674 goto failed;
6675
6676 CloseHandle(i_theirs);
6677 CloseHandle(o_theirs);
6678
6679 channel_set_pipes(channel,
6680 (sock_T)i_ours,
6681 (sock_T)o_ours,
6682 (sock_T)o_ours);
6683
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006684 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006685 channel->ch_write_text_mode = TRUE;
6686
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006687 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006688 channel->ch_anonymous_pipe = TRUE;
6689
6690 jo = CreateJobObject(NULL, NULL);
6691 if (jo == NULL)
6692 goto failed;
6693
6694 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6695 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006696 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006697 CloseHandle(jo);
6698 jo = NULL;
6699 }
6700
6701 ResumeThread(proc_info.hThread);
6702 CloseHandle(proc_info.hThread);
6703
6704 vim_free(cmd_wchar);
6705 vim_free(cmd_wchar_copy);
6706 vim_free(cwd_wchar);
6707 vim_free(env_wchar);
6708
6709 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6710 goto failed;
6711
6712#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6713 if (opt->jo_set2 & JO2_ANSI_COLORS)
6714 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6715 else
6716 init_vterm_ansi_colors(term->tl_vterm);
6717#endif
6718
6719 channel_set_job(channel, job, opt);
6720 job_set_options(job, opt);
6721
6722 job->jv_channel = channel;
6723 job->jv_proc_info = proc_info;
6724 job->jv_job_object = jo;
6725 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006726 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006727 ++job->jv_refcount;
6728 term->tl_job = job;
6729
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006730 // Redirecting stdout and stderr doesn't work at the job level. Instead
6731 // open the file here and handle it in. opt->jo_io was changed in
6732 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006733 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6734 {
6735 char_u *fname = opt->jo_io_name[PART_OUT];
6736
6737 ch_log(channel, "Opening output file %s", fname);
6738 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6739 if (term->tl_out_fd == NULL)
6740 semsg(_(e_notopen), fname);
6741 }
6742
6743 return OK;
6744
6745failed:
6746 ga_clear(&ga_cmd);
6747 ga_clear(&ga_env);
6748 vim_free(cmd_wchar);
6749 vim_free(cmd_wchar_copy);
6750 vim_free(cwd_wchar);
6751 if (channel != NULL)
6752 channel_clear(channel);
6753 if (job != NULL)
6754 {
6755 job->jv_channel = NULL;
6756 job_cleanup(job);
6757 }
6758 term->tl_job = NULL;
6759 if (jo != NULL)
6760 CloseHandle(jo);
6761
6762 if (term->tl_siex.lpAttributeList != NULL)
6763 {
6764 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6765 vim_free(term->tl_siex.lpAttributeList);
6766 }
6767 term->tl_siex.lpAttributeList = NULL;
6768 if (o_theirs != NULL)
6769 CloseHandle(o_theirs);
6770 if (o_ours != NULL)
6771 CloseHandle(o_ours);
6772 if (i_ours != NULL)
6773 CloseHandle(i_ours);
6774 if (i_theirs != NULL)
6775 CloseHandle(i_theirs);
6776 if (term->tl_conpty != NULL)
6777 pClosePseudoConsole(term->tl_conpty);
6778 term->tl_conpty = NULL;
6779 return FAIL;
6780}
6781
6782 static void
6783conpty_term_report_winsize(term_T *term, int rows, int cols)
6784{
6785 COORD consize;
6786
6787 consize.X = cols;
6788 consize.Y = rows;
6789 pResizePseudoConsole(term->tl_conpty, consize);
6790}
6791
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006792 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006793term_free_conpty(term_T *term)
6794{
6795 if (term->tl_siex.lpAttributeList != NULL)
6796 {
6797 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6798 vim_free(term->tl_siex.lpAttributeList);
6799 }
6800 term->tl_siex.lpAttributeList = NULL;
6801 if (term->tl_conpty != NULL)
6802 pClosePseudoConsole(term->tl_conpty);
6803 term->tl_conpty = NULL;
6804}
6805
6806 int
6807use_conpty(void)
6808{
6809 return has_conpty;
6810}
6811
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006812# ifndef PROTO
6813
6814#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6815#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006816#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006817
6818void* (*winpty_config_new)(UINT64, void*);
6819void* (*winpty_open)(void*, void*);
6820void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6821BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6822void (*winpty_config_set_mouse_mode)(void*, int);
6823void (*winpty_config_set_initial_size)(void*, int, int);
6824LPCWSTR (*winpty_conin_name)(void*);
6825LPCWSTR (*winpty_conout_name)(void*);
6826LPCWSTR (*winpty_conerr_name)(void*);
6827void (*winpty_free)(void*);
6828void (*winpty_config_free)(void*);
6829void (*winpty_spawn_config_free)(void*);
6830void (*winpty_error_free)(void*);
6831LPCWSTR (*winpty_error_msg)(void*);
6832BOOL (*winpty_set_size)(void*, int, int, void*);
6833HANDLE (*winpty_agent_process)(void*);
6834
6835#define WINPTY_DLL "winpty.dll"
6836
6837static HINSTANCE hWinPtyDLL = NULL;
6838# endif
6839
6840 static int
6841dyn_winpty_init(int verbose)
6842{
6843 int i;
6844 static struct
6845 {
6846 char *name;
6847 FARPROC *ptr;
6848 } winpty_entry[] =
6849 {
6850 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6851 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6852 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6853 {"winpty_config_set_mouse_mode",
6854 (FARPROC*)&winpty_config_set_mouse_mode},
6855 {"winpty_config_set_initial_size",
6856 (FARPROC*)&winpty_config_set_initial_size},
6857 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6858 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6859 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6860 {"winpty_free", (FARPROC*)&winpty_free},
6861 {"winpty_open", (FARPROC*)&winpty_open},
6862 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6863 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6864 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6865 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6866 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6867 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6868 {NULL, NULL}
6869 };
6870
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006871 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006872 if (hWinPtyDLL)
6873 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006874 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6875 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006876 if (*p_winptydll != NUL)
6877 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6878 if (!hWinPtyDLL)
6879 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6880 if (!hWinPtyDLL)
6881 {
6882 if (verbose)
Martin Tournoij1a3e5742021-07-24 13:57:29 +02006883 semsg(_(e_loadlib),
6884 (*p_winptydll != NUL ? p_winptydll : (char_u *)WINPTY_DLL),
6885 GetWin32Error());
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006886 return FAIL;
6887 }
6888 for (i = 0; winpty_entry[i].name != NULL
6889 && winpty_entry[i].ptr != NULL; ++i)
6890 {
6891 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6892 winpty_entry[i].name)) == NULL)
6893 {
6894 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006895 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006896 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006897 return FAIL;
6898 }
6899 }
6900
6901 return OK;
6902}
6903
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006904 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006905winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006906 term_T *term,
6907 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006908 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006909 jobopt_T *opt,
6910 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006911{
6912 WCHAR *cmd_wchar = NULL;
6913 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006914 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006915 channel_T *channel = NULL;
6916 job_T *job = NULL;
6917 DWORD error;
6918 HANDLE jo = NULL;
6919 HANDLE child_process_handle;
6920 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006921 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006922 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006923 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006924 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006925
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006926 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6927 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006928
6929 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006930 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006931 cmd = argvar->vval.v_string;
6932 }
6933 else if (argvar->v_type == VAR_LIST)
6934 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006935 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006936 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006937 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006938 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006939 if (cmd == NULL || *cmd == NUL)
6940 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006941 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006942 goto failed;
6943 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006944
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006945 term->tl_arg0_cmd = vim_strsave(cmd);
6946
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006947 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006948 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006949 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006950 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006951 if (opt->jo_cwd != NULL)
6952 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006953
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006954 win32_build_env(opt->jo_env, &ga_env, TRUE);
6955 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006956
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006957 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6958 if (term->tl_winpty_config == NULL)
6959 goto failed;
6960
6961 winpty_config_set_mouse_mode(term->tl_winpty_config,
6962 WINPTY_MOUSE_MODE_FORCE);
6963 winpty_config_set_initial_size(term->tl_winpty_config,
6964 term->tl_cols, term->tl_rows);
6965 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6966 if (term->tl_winpty == NULL)
6967 goto failed;
6968
6969 spawn_config = winpty_spawn_config_new(
6970 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6971 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6972 NULL,
6973 cmd_wchar,
6974 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006975 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006976 &winpty_err);
6977 if (spawn_config == NULL)
6978 goto failed;
6979
6980 channel = add_channel();
6981 if (channel == NULL)
6982 goto failed;
6983
6984 job = job_alloc();
6985 if (job == NULL)
6986 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006987 if (argvar->v_type == VAR_STRING)
6988 {
6989 int argc;
6990
6991 build_argv_from_string(cmd, &job->jv_argv, &argc);
6992 }
6993 else
6994 {
6995 int argc;
6996
6997 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6998 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006999
7000 if (opt->jo_set & JO_IN_BUF)
7001 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
7002
7003 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
7004 &child_thread_handle, &error, &winpty_err))
7005 goto failed;
7006
7007 channel_set_pipes(channel,
7008 (sock_T)CreateFileW(
7009 winpty_conin_name(term->tl_winpty),
7010 GENERIC_WRITE, 0, NULL,
7011 OPEN_EXISTING, 0, NULL),
7012 (sock_T)CreateFileW(
7013 winpty_conout_name(term->tl_winpty),
7014 GENERIC_READ, 0, NULL,
7015 OPEN_EXISTING, 0, NULL),
7016 (sock_T)CreateFileW(
7017 winpty_conerr_name(term->tl_winpty),
7018 GENERIC_READ, 0, NULL,
7019 OPEN_EXISTING, 0, NULL));
7020
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007021 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007022 channel->ch_write_text_mode = TRUE;
7023
7024 jo = CreateJobObject(NULL, NULL);
7025 if (jo == NULL)
7026 goto failed;
7027
7028 if (!AssignProcessToJobObject(jo, child_process_handle))
7029 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007030 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007031 CloseHandle(jo);
7032 jo = NULL;
7033 }
7034
7035 winpty_spawn_config_free(spawn_config);
7036 vim_free(cmd_wchar);
7037 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007038 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007039
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007040 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7041 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007042
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007043#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7044 if (opt->jo_set2 & JO2_ANSI_COLORS)
7045 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7046 else
7047 init_vterm_ansi_colors(term->tl_vterm);
7048#endif
7049
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007050 channel_set_job(channel, job, opt);
7051 job_set_options(job, opt);
7052
7053 job->jv_channel = channel;
7054 job->jv_proc_info.hProcess = child_process_handle;
7055 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
7056 job->jv_job_object = jo;
7057 job->jv_status = JOB_STARTED;
7058 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007059 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007060 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007061 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01007062 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007063 ++job->jv_refcount;
7064 term->tl_job = job;
7065
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007066 // Redirecting stdout and stderr doesn't work at the job level. Instead
7067 // open the file here and handle it in. opt->jo_io was changed in
7068 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007069 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
7070 {
7071 char_u *fname = opt->jo_io_name[PART_OUT];
7072
7073 ch_log(channel, "Opening output file %s", fname);
7074 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
7075 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007076 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007077 }
7078
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007079 return OK;
7080
7081failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01007082 ga_clear(&ga_cmd);
7083 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007084 vim_free(cmd_wchar);
7085 vim_free(cwd_wchar);
7086 if (spawn_config != NULL)
7087 winpty_spawn_config_free(spawn_config);
7088 if (channel != NULL)
7089 channel_clear(channel);
7090 if (job != NULL)
7091 {
7092 job->jv_channel = NULL;
7093 job_cleanup(job);
7094 }
7095 term->tl_job = NULL;
7096 if (jo != NULL)
7097 CloseHandle(jo);
7098 if (term->tl_winpty != NULL)
7099 winpty_free(term->tl_winpty);
7100 term->tl_winpty = NULL;
7101 if (term->tl_winpty_config != NULL)
7102 winpty_config_free(term->tl_winpty_config);
7103 term->tl_winpty_config = NULL;
7104 if (winpty_err != NULL)
7105 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007106 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007107 (short_u *)winpty_error_msg(winpty_err), NULL);
7108
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007109 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007110 winpty_error_free(winpty_err);
7111 }
7112 return FAIL;
7113}
7114
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007115/*
7116 * Create a new terminal of "rows" by "cols" cells.
7117 * Store a reference in "term".
7118 * Return OK or FAIL.
7119 */
7120 static int
7121term_and_job_init(
7122 term_T *term,
7123 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01007124 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007125 jobopt_T *opt,
7126 jobopt_T *orig_opt)
7127{
7128 int use_winpty = FALSE;
7129 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007130 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007131
7132 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
7133 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
7134
7135 if (!has_winpty && !has_conpty)
7136 // If neither is available give the errors for winpty, since when
7137 // conpty is not available it can't be installed either.
7138 return dyn_winpty_init(TRUE);
7139
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007140 if (opt->jo_tty_type != NUL)
7141 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007142
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007143 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007144 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01007145 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007146 use_conpty = TRUE;
7147 else if (has_winpty)
7148 use_winpty = TRUE;
7149 // else: error
7150 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007151 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007152 {
7153 if (has_winpty)
7154 use_winpty = TRUE;
7155 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01007156 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007157 {
7158 if (has_conpty)
7159 use_conpty = TRUE;
7160 else
7161 return dyn_conpty_init(TRUE);
7162 }
7163
7164 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007165 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007166
7167 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007168 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007169
7170 // error
7171 return dyn_winpty_init(TRUE);
7172}
7173
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007174 static int
7175create_pty_only(term_T *term, jobopt_T *options)
7176{
7177 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
7178 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
7179 char in_name[80], out_name[80];
7180 channel_T *channel = NULL;
7181
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007182 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7183 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007184
7185 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
7186 GetCurrentProcessId(),
7187 curbuf->b_fnum);
7188 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
7189 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7190 PIPE_UNLIMITED_INSTANCES,
7191 0, 0, NMPWAIT_NOWAIT, NULL);
7192 if (hPipeIn == INVALID_HANDLE_VALUE)
7193 goto failed;
7194
7195 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
7196 GetCurrentProcessId(),
7197 curbuf->b_fnum);
7198 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
7199 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
7200 PIPE_UNLIMITED_INSTANCES,
7201 0, 0, 0, NULL);
7202 if (hPipeOut == INVALID_HANDLE_VALUE)
7203 goto failed;
7204
7205 ConnectNamedPipe(hPipeIn, NULL);
7206 ConnectNamedPipe(hPipeOut, NULL);
7207
7208 term->tl_job = job_alloc();
7209 if (term->tl_job == NULL)
7210 goto failed;
7211 ++term->tl_job->jv_refcount;
7212
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007213 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007214 term->tl_job->jv_status = JOB_FINISHED;
7215
7216 channel = add_channel();
7217 if (channel == NULL)
7218 goto failed;
7219 term->tl_job->jv_channel = channel;
7220 channel->ch_keep_open = TRUE;
7221 channel->ch_named_pipe = TRUE;
7222
7223 channel_set_pipes(channel,
7224 (sock_T)hPipeIn,
7225 (sock_T)hPipeOut,
7226 (sock_T)hPipeOut);
7227 channel_set_job(channel, term->tl_job, options);
7228 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
7229 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
7230
7231 return OK;
7232
7233failed:
7234 if (hPipeIn != NULL)
7235 CloseHandle(hPipeIn);
7236 if (hPipeOut != NULL)
7237 CloseHandle(hPipeOut);
7238 return FAIL;
7239}
7240
7241/*
7242 * Free the terminal emulator part of "term".
7243 */
7244 static void
7245term_free_vterm(term_T *term)
7246{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007247 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007248 if (term->tl_winpty != NULL)
7249 winpty_free(term->tl_winpty);
7250 term->tl_winpty = NULL;
7251 if (term->tl_winpty_config != NULL)
7252 winpty_config_free(term->tl_winpty_config);
7253 term->tl_winpty_config = NULL;
7254 if (term->tl_vterm != NULL)
7255 vterm_free(term->tl_vterm);
7256 term->tl_vterm = NULL;
7257}
7258
7259/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007260 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007261 */
7262 static void
7263term_report_winsize(term_T *term, int rows, int cols)
7264{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007265 if (term->tl_conpty)
7266 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007267 if (term->tl_winpty)
7268 winpty_set_size(term->tl_winpty, cols, rows, NULL);
7269}
7270
7271 int
7272terminal_enabled(void)
7273{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007274 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007275}
7276
7277# else
7278
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007279///////////////////////////////////////
7280// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007281
7282/*
7283 * Create a new terminal of "rows" by "cols" cells.
7284 * Start job for "cmd".
7285 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01007286 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007287 * Return OK or FAIL.
7288 */
7289 static int
7290term_and_job_init(
7291 term_T *term,
7292 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01007293 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02007294 jobopt_T *opt,
7295 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007296{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007297 term->tl_arg0_cmd = NULL;
7298
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007299 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7300 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007301
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007302#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7303 if (opt->jo_set2 & JO2_ANSI_COLORS)
7304 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7305 else
7306 init_vterm_ansi_colors(term->tl_vterm);
7307#endif
7308
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007309 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007310 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007311 if (term->tl_job != NULL)
7312 ++term->tl_job->jv_refcount;
7313
7314 return term->tl_job != NULL
7315 && term->tl_job->jv_channel != NULL
7316 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7317}
7318
7319 static int
7320create_pty_only(term_T *term, jobopt_T *opt)
7321{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007322 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7323 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007324
7325 term->tl_job = job_alloc();
7326 if (term->tl_job == NULL)
7327 return FAIL;
7328 ++term->tl_job->jv_refcount;
7329
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007330 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007331 term->tl_job->jv_status = JOB_FINISHED;
7332
7333 return mch_create_pty_channel(term->tl_job, opt);
7334}
7335
7336/*
7337 * Free the terminal emulator part of "term".
7338 */
7339 static void
7340term_free_vterm(term_T *term)
7341{
7342 if (term->tl_vterm != NULL)
7343 vterm_free(term->tl_vterm);
7344 term->tl_vterm = NULL;
7345}
7346
7347/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007348 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007349 */
7350 static void
7351term_report_winsize(term_T *term, int rows, int cols)
7352{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007353 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007354 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7355 {
7356 int fd = -1;
7357 int part;
7358
7359 for (part = PART_OUT; part < PART_COUNT; ++part)
7360 {
7361 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007362 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007363 break;
7364 }
7365 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7366 mch_signal_job(term->tl_job, (char_u *)"winch");
7367 }
7368}
7369
7370# endif
7371
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007372#endif // FEAT_TERMINAL