blob: 0a5ccbc597117d9d657204edb3bb0d76f67dc4bf [file] [log] [blame]
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Terminal window support, see ":help :terminal".
12 *
13 * There are three parts:
14 * 1. Generic code for all systems.
15 * Uses libvterm for the terminal emulator.
16 * 2. The MS-Windows implementation.
17 * Uses winpty.
18 * 3. The Unix-like implementation.
19 * Uses pseudo-tty's (pty's).
20 *
21 * For each terminal one VTerm is constructed. This uses libvterm. A copy of
22 * this library is in the libvterm directory.
23 *
24 * When a terminal window is opened, a job is started that will be connected to
25 * the terminal emulator.
26 *
27 * If the terminal window has keyboard focus, typed keys are converted to the
28 * terminal encoding and writing to the job over a channel.
29 *
30 * If the job produces output, it is written to the terminal emulator. The
31 * terminal emulator invokes callbacks when its screen content changes. The
32 * line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
33 * while, if the terminal window is visible, the screen contents is drawn.
34 *
35 * When the job ends the text is put in a buffer. Redrawing then happens from
36 * that buffer, attributes come from the scrollback buffer tl_scrollback.
37 * When the buffer is changed it is turned into a normal buffer, the attributes
38 * in tl_scrollback are no longer used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020039 */
40
41#include "vim.h"
42
43#if defined(FEAT_TERMINAL) || defined(PROTO)
44
45#ifndef MIN
46# define MIN(x,y) ((x) < (y) ? (x) : (y))
47#endif
48#ifndef MAX
49# define MAX(x,y) ((x) > (y) ? (x) : (y))
50#endif
51
52#include "libvterm/include/vterm.h"
53
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010054// This is VTermScreenCell without the characters, thus much smaller.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020055typedef struct {
56 VTermScreenCellAttrs attrs;
57 char width;
Bram Moolenaard96ff162018-02-18 22:13:29 +010058 VTermColor fg;
59 VTermColor bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020060} cellattr_T;
61
62typedef struct sb_line_S {
Bram Moolenaar29ae2232019-02-14 21:22:01 +010063 int sb_cols; // can differ per line
64 cellattr_T *sb_cells; // allocated
65 cellattr_T sb_fill_attr; // for short line
66 char_u *sb_text; // for tl_scrollback_postponed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020067} sb_line_T;
68
Bram Moolenaar4f974752019-02-17 17:44:42 +010069#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010070# ifndef HPCON
71# define HPCON VOID*
72# endif
73# ifndef EXTENDED_STARTUPINFO_PRESENT
74# define EXTENDED_STARTUPINFO_PRESENT 0x00080000
75# endif
76# ifndef PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE
77# define PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE 0x00020016
78# endif
79typedef struct _DYN_STARTUPINFOEXW
80{
81 STARTUPINFOW StartupInfo;
82 LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList;
83} DYN_STARTUPINFOEXW, *PDYN_STARTUPINFOEXW;
84#endif
85
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010086// typedef term_T in structs.h
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020087struct terminal_S {
88 term_T *tl_next;
89
90 VTerm *tl_vterm;
91 job_T *tl_job;
92 buf_T *tl_buffer;
Bram Moolenaar13568252018-03-16 20:46:58 +010093#if defined(FEAT_GUI)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010094 int tl_system; // when non-zero used for :!cmd output
95 int tl_toprow; // row with first line of system terminal
Bram Moolenaar13568252018-03-16 20:46:58 +010096#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020097
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +010098 // Set when setting the size of a vterm, reset after redrawing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020099 int tl_vterm_size_changed;
100
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100101 int tl_normal_mode; // TRUE: Terminal-Normal mode
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200102 int tl_channel_closed;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200103 int tl_channel_recently_closed; // still need to handle tl_finish
104
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100105 int tl_finish;
106#define TL_FINISH_UNSET NUL
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100107#define TL_FINISH_CLOSE 'c' // ++close or :terminal without argument
108#define TL_FINISH_NOCLOSE 'n' // ++noclose
109#define TL_FINISH_OPEN 'o' // ++open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200110 char_u *tl_opencmd;
111 char_u *tl_eof_chars;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200112 char_u *tl_api; // prefix for terminal API function
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200113
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100114 char_u *tl_arg0_cmd; // To format the status bar
115
Bram Moolenaar4f974752019-02-17 17:44:42 +0100116#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200117 void *tl_winpty_config;
118 void *tl_winpty;
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200119
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100120 HPCON tl_conpty;
121 DYN_STARTUPINFOEXW tl_siex; // Structure that always needs to be hold
122
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200123 FILE *tl_out_fd;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200124#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100125#if defined(FEAT_SESSION)
126 char_u *tl_command;
127#endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100128 char_u *tl_kill;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200129
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100130 // last known vterm size
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200131 int tl_rows;
132 int tl_cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200133
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100134 char_u *tl_title; // NULL or allocated
135 char_u *tl_status_text; // NULL or allocated
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200136
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100137 // Range of screen rows to update. Zero based.
138 int tl_dirty_row_start; // MAX_ROW if nothing dirty
139 int tl_dirty_row_end; // row below last one to update
140 int tl_dirty_snapshot; // text updated after making snapshot
Bram Moolenaar56bc8e22018-05-10 18:05:56 +0200141#ifdef FEAT_TIMERS
142 int tl_timer_set;
143 proftime_T tl_timer_due;
144#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100145 int tl_postponed_scroll; // to be scrolled up
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200146
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200147 garray_T tl_scrollback;
148 int tl_scrollback_scrolled;
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100149 garray_T tl_scrollback_postponed;
150
Bram Moolenaar83d47902020-03-26 20:34:00 +0100151 char_u *tl_highlight_name; // replaces "Terminal"; allocated
152
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200153 cellattr_T tl_default_color;
154
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100155 linenr_T tl_top_diff_rows; // rows of top diff file or zero
156 linenr_T tl_bot_diff_rows; // rows of bottom diff file
Bram Moolenaard96ff162018-02-18 22:13:29 +0100157
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200158 VTermPos tl_cursor_pos;
159 int tl_cursor_visible;
160 int tl_cursor_blink;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100161 int tl_cursor_shape; // 1: block, 2: underline, 3: bar
162 char_u *tl_cursor_color; // NULL or allocated
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200163
164 int tl_using_altscreen;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200165 garray_T tl_osc_buf; // incomplete OSC string
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200166};
167
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100168#define TMODE_ONCE 1 // CTRL-\ CTRL-N used
169#define TMODE_LOOP 2 // CTRL-W N used
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200170
171/*
172 * List of all active terminals.
173 */
174static term_T *first_term = NULL;
175
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100176// Terminal active in terminal_loop().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200177static term_T *in_terminal_loop = NULL;
178
Bram Moolenaar4f974752019-02-17 17:44:42 +0100179#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100180static BOOL has_winpty = FALSE;
181static BOOL has_conpty = FALSE;
182#endif
183
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100184#define MAX_ROW 999999 // used for tl_dirty_row_end to update all rows
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200185#define KEY_BUF_LEN 200
186
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200187#define FOR_ALL_TERMS(term) \
188 for ((term) = first_term; (term) != NULL; (term) = (term)->tl_next)
189
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200190/*
191 * Functions with separate implementation for MS-Windows and Unix-like systems.
192 */
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200193static int term_and_job_init(term_T *term, typval_T *argvar, char **argv, jobopt_T *opt, jobopt_T *orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200194static int create_pty_only(term_T *term, jobopt_T *opt);
195static void term_report_winsize(term_T *term, int rows, int cols);
196static void term_free_vterm(term_T *term);
Bram Moolenaar13568252018-03-16 20:46:58 +0100197#ifdef FEAT_GUI
198static void update_system_term(term_T *term);
199#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200200
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100201static void handle_postponed_scrollback(term_T *term);
202
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100203// The character that we know (or assume) that the terminal expects for the
204// backspace key.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200205static int term_backspace_char = BS;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200206
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100207// "Terminal" highlight group colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +0100208static int term_default_cterm_fg = -1;
209static int term_default_cterm_bg = -1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200210
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100211// Store the last set and the desired cursor properties, so that we only update
212// them when needed. Doing it unnecessary may result in flicker.
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200213static char_u *last_set_cursor_color = NULL;
214static char_u *desired_cursor_color = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +0100215static int last_set_cursor_shape = -1;
216static int desired_cursor_shape = -1;
217static int last_set_cursor_blink = -1;
218static int desired_cursor_blink = -1;
219
220
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100221///////////////////////////////////////
222// 1. Generic code for all systems.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200223
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200224 static int
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200225cursor_color_equal(char_u *lhs_color, char_u *rhs_color)
226{
227 if (lhs_color != NULL && rhs_color != NULL)
228 return STRCMP(lhs_color, rhs_color) == 0;
229 return lhs_color == NULL && rhs_color == NULL;
230}
231
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200232 static void
233cursor_color_copy(char_u **to_color, char_u *from_color)
234{
235 // Avoid a free & alloc if the value is already right.
236 if (cursor_color_equal(*to_color, from_color))
237 return;
238 vim_free(*to_color);
239 *to_color = (from_color == NULL) ? NULL : vim_strsave(from_color);
240}
241
242 static char_u *
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200243cursor_color_get(char_u *color)
244{
245 return (color == NULL) ? (char_u *)"" : color;
246}
247
248
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200249/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200250 * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
Bram Moolenaar498c2562018-04-15 23:45:15 +0200251 * current window.
252 * Sets "rows" and/or "cols" to zero when it should follow the window size.
253 * Return TRUE if the size is the minimum size: "24*80".
254 */
255 static int
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200256parse_termwinsize(win_T *wp, int *rows, int *cols)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200257{
258 int minsize = FALSE;
259
260 *rows = 0;
261 *cols = 0;
262
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200263 if (*wp->w_p_tws != NUL)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200264 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200265 char_u *p = vim_strchr(wp->w_p_tws, 'x');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200266
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100267 // Syntax of value was already checked when it's set.
Bram Moolenaar498c2562018-04-15 23:45:15 +0200268 if (p == NULL)
269 {
270 minsize = TRUE;
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200271 p = vim_strchr(wp->w_p_tws, '*');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200272 }
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200273 *rows = atoi((char *)wp->w_p_tws);
Bram Moolenaar498c2562018-04-15 23:45:15 +0200274 *cols = atoi((char *)p + 1);
275 }
276 return minsize;
277}
278
279/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200280 * Determine the terminal size from 'termwinsize' and the current window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200281 */
282 static void
283set_term_and_win_size(term_T *term)
284{
Bram Moolenaar13568252018-03-16 20:46:58 +0100285#ifdef FEAT_GUI
286 if (term->tl_system)
287 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100288 // Use the whole screen for the system command. However, it will start
289 // at the command line and scroll up as needed, using tl_toprow.
Bram Moolenaar13568252018-03-16 20:46:58 +0100290 term->tl_rows = Rows;
291 term->tl_cols = Columns;
Bram Moolenaar07b46af2018-04-10 14:56:18 +0200292 return;
Bram Moolenaar13568252018-03-16 20:46:58 +0100293 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100294#endif
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200295 if (parse_termwinsize(curwin, &term->tl_rows, &term->tl_cols))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200296 {
Bram Moolenaar498c2562018-04-15 23:45:15 +0200297 if (term->tl_rows != 0)
298 term->tl_rows = MAX(term->tl_rows, curwin->w_height);
299 if (term->tl_cols != 0)
300 term->tl_cols = MAX(term->tl_cols, curwin->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200301 }
302 if (term->tl_rows == 0)
303 term->tl_rows = curwin->w_height;
304 else
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200305 win_setheight_win(term->tl_rows, curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200306 if (term->tl_cols == 0)
307 term->tl_cols = curwin->w_width;
308 else
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200309 win_setwidth_win(term->tl_cols, curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200310}
311
312/*
313 * Initialize job options for a terminal job.
314 * Caller may overrule some of them.
315 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100316 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200317init_job_options(jobopt_T *opt)
318{
319 clear_job_options(opt);
320
321 opt->jo_mode = MODE_RAW;
322 opt->jo_out_mode = MODE_RAW;
323 opt->jo_err_mode = MODE_RAW;
324 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
325}
326
327/*
328 * Set job options mandatory for a terminal job.
329 */
330 static void
331setup_job_options(jobopt_T *opt, int rows, int cols)
332{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100333#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100334 // Win32: Redirecting the job output won't work, thus always connect stdout
335 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200336 if (!(opt->jo_set & JO_OUT_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200337#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200338 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100339 // Connect stdout to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200340 opt->jo_io[PART_OUT] = JIO_BUFFER;
341 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
342 opt->jo_modifiable[PART_OUT] = 0;
343 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
344 }
345
Bram Moolenaar4f974752019-02-17 17:44:42 +0100346#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100347 // Win32: Redirecting the job output won't work, thus always connect stderr
348 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200349 if (!(opt->jo_set & JO_ERR_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200350#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200351 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100352 // Connect stderr to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200353 opt->jo_io[PART_ERR] = JIO_BUFFER;
354 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
355 opt->jo_modifiable[PART_ERR] = 0;
356 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
357 }
358
359 opt->jo_pty = TRUE;
360 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
361 opt->jo_term_rows = rows;
362 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
363 opt->jo_term_cols = cols;
364}
365
366/*
Bram Moolenaar5c381eb2019-06-25 06:50:31 +0200367 * Flush messages on channels.
368 */
369 static void
370term_flush_messages()
371{
372 mch_check_messages();
373 parse_queued_messages();
374}
375
376/*
Bram Moolenaard96ff162018-02-18 22:13:29 +0100377 * Close a terminal buffer (and its window). Used when creating the terminal
378 * fails.
379 */
380 static void
381term_close_buffer(buf_T *buf, buf_T *old_curbuf)
382{
383 free_terminal(buf);
384 if (old_curbuf != NULL)
385 {
386 --curbuf->b_nwindows;
387 curbuf = old_curbuf;
388 curwin->w_buffer = curbuf;
389 ++curbuf->b_nwindows;
390 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100391 CHECK_CURBUF;
Bram Moolenaard96ff162018-02-18 22:13:29 +0100392
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100393 // Wiping out the buffer will also close the window and call
394 // free_terminal().
Bram Moolenaard96ff162018-02-18 22:13:29 +0100395 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
396}
397
398/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200399 * Start a terminal window and return its buffer.
Bram Moolenaar13568252018-03-16 20:46:58 +0100400 * Use either "argvar" or "argv", the other must be NULL.
401 * When "flags" has TERM_START_NOJOB only create the buffer, b_term and open
402 * the window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200403 * Returns NULL when failed.
404 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100405 buf_T *
406term_start(
407 typval_T *argvar,
408 char **argv,
409 jobopt_T *opt,
410 int flags)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200411{
412 exarg_T split_ea;
413 win_T *old_curwin = curwin;
414 term_T *term;
415 buf_T *old_curbuf = NULL;
416 int res;
417 buf_T *newbuf;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100418 int vertical = opt->jo_vertical || (cmdmod.split & WSP_VERT);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200419 jobopt_T orig_opt; // only partly filled
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200420
421 if (check_restricted() || check_secure())
422 return NULL;
423
424 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
425 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
426 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
Bram Moolenaarb0992022020-01-30 14:55:42 +0100427 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))
428 || (argvar != NULL
429 && argvar->v_type == VAR_LIST
430 && argvar->vval.v_list != NULL
431 && argvar->vval.v_list->lv_first == &range_list_item))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200432 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100433 emsg(_(e_invarg));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200434 return NULL;
435 }
436
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200437 term = ALLOC_CLEAR_ONE(term_T);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200438 if (term == NULL)
439 return NULL;
440 term->tl_dirty_row_end = MAX_ROW;
441 term->tl_cursor_visible = TRUE;
442 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
443 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100444#ifdef FEAT_GUI
445 term->tl_system = (flags & TERM_START_SYSTEM);
446#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200447 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100448 ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +0200449 ga_init2(&term->tl_osc_buf, sizeof(char), 300);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200450
Bram Moolenaara80faa82020-04-12 19:37:17 +0200451 CLEAR_FIELD(split_ea);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200452 if (opt->jo_curwin)
453 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100454 // Create a new buffer in the current window.
Bram Moolenaar13568252018-03-16 20:46:58 +0100455 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200456 {
457 no_write_message();
458 vim_free(term);
459 return NULL;
460 }
461 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaar13568252018-03-16 20:46:58 +0100462 ECMD_HIDE
463 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
464 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200465 {
466 vim_free(term);
467 return NULL;
468 }
469 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100470 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200471 {
472 buf_T *buf;
473
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100474 // Create a new buffer without a window. Make it the current buffer for
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100475 // a moment to be able to do the initializations.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200476 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
477 BLN_NEW | BLN_LISTED);
478 if (buf == NULL || ml_open(buf) == FAIL)
479 {
480 vim_free(term);
481 return NULL;
482 }
483 old_curbuf = curbuf;
484 --curbuf->b_nwindows;
485 curbuf = buf;
486 curwin->w_buffer = buf;
487 ++curbuf->b_nwindows;
488 }
489 else
490 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100491 // Open a new window or tab.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200492 split_ea.cmdidx = CMD_new;
493 split_ea.cmd = (char_u *)"new";
494 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100495 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200496 {
497 split_ea.line2 = opt->jo_term_rows;
498 split_ea.addr_count = 1;
499 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100500 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200501 {
502 split_ea.line2 = opt->jo_term_cols;
503 split_ea.addr_count = 1;
504 }
505
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100506 if (vertical)
507 cmdmod.split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200508 ex_splitview(&split_ea);
509 if (curwin == old_curwin)
510 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100511 // split failed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200512 vim_free(term);
513 return NULL;
514 }
515 }
516 term->tl_buffer = curbuf;
517 curbuf->b_term = term;
518
519 if (!opt->jo_hidden)
520 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100521 // Only one size was taken care of with :new, do the other one. With
522 // "curwin" both need to be done.
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100523 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200524 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100525 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200526 win_setwidth(opt->jo_term_cols);
527 }
528
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100529 // Link the new terminal in the list of active terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200530 term->tl_next = first_term;
531 first_term = term;
532
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100533 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
534
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200535 if (opt->jo_term_name != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100536 {
537 vim_free(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200538 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100539 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100540 else if (argv != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100541 {
542 vim_free(curbuf->b_ffname);
Bram Moolenaar13568252018-03-16 20:46:58 +0100543 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100544 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200545 else
546 {
547 int i;
548 size_t len;
549 char_u *cmd, *p;
550
551 if (argvar->v_type == VAR_STRING)
552 {
553 cmd = argvar->vval.v_string;
554 if (cmd == NULL)
555 cmd = (char_u *)"";
556 else if (STRCMP(cmd, "NONE") == 0)
557 cmd = (char_u *)"pty";
558 }
559 else if (argvar->v_type != VAR_LIST
560 || argvar->vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +0100561 || argvar->vval.v_list->lv_len == 0
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100562 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200563 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
564 cmd = (char_u*)"";
565
566 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200567 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200568
569 for (i = 0; p != NULL; ++i)
570 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100571 // Prepend a ! to the command name to avoid the buffer name equals
572 // the executable, otherwise ":w!" would overwrite it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200573 if (i == 0)
574 vim_snprintf((char *)p, len, "!%s", cmd);
575 else
576 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
577 if (buflist_findname(p) == NULL)
578 {
579 vim_free(curbuf->b_ffname);
580 curbuf->b_ffname = p;
581 break;
582 }
583 }
584 }
Bram Moolenaare010c722020-02-24 21:37:54 +0100585 vim_free(curbuf->b_sfname);
586 curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200587 curbuf->b_fname = curbuf->b_ffname;
588
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100589 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
590
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200591 if (opt->jo_term_opencmd != NULL)
592 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
593
594 if (opt->jo_eof_chars != NULL)
595 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
596
597 set_string_option_direct((char_u *)"buftype", -1,
598 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200599 // Avoid that 'buftype' is reset when this buffer is entered.
600 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200601
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100602 // Mark the buffer as not modifiable. It can only be made modifiable after
603 // the job finished.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200604 curbuf->b_p_ma = FALSE;
605
606 set_term_and_win_size(term);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100607#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200608 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
609#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200610 setup_job_options(opt, term->tl_rows, term->tl_cols);
611
Bram Moolenaar13568252018-03-16 20:46:58 +0100612 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100613 return curbuf;
614
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100615#if defined(FEAT_SESSION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100616 // Remember the command for the session file.
Bram Moolenaar13568252018-03-16 20:46:58 +0100617 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100618 term->tl_command = vim_strsave((char_u *)"NONE");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100619 else if (argvar->v_type == VAR_STRING)
620 {
621 char_u *cmd = argvar->vval.v_string;
622
623 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
624 term->tl_command = vim_strsave(cmd);
625 }
626 else if (argvar->v_type == VAR_LIST
627 && argvar->vval.v_list != NULL
628 && argvar->vval.v_list->lv_len > 0)
629 {
630 garray_T ga;
631 listitem_T *item;
632
633 ga_init2(&ga, 1, 100);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200634 FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100635 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100636 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100637 char_u *p;
638
639 if (s == NULL)
640 break;
641 p = vim_strsave_fnameescape(s, FALSE);
642 if (p == NULL)
643 break;
644 ga_concat(&ga, p);
645 vim_free(p);
646 ga_append(&ga, ' ');
647 }
648 if (item == NULL)
649 {
650 ga_append(&ga, NUL);
651 term->tl_command = ga.ga_data;
652 }
653 else
654 ga_clear(&ga);
655 }
656#endif
657
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100658 if (opt->jo_term_kill != NULL)
659 {
660 char_u *p = skiptowhite(opt->jo_term_kill);
661
662 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
663 }
664
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200665 if (opt->jo_term_api != NULL)
Bram Moolenaar21109272020-01-30 16:27:20 +0100666 {
667 char_u *p = skiptowhite(opt->jo_term_api);
668
669 term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
670 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200671 else
672 term->tl_api = vim_strsave((char_u *)"Tapi_");
673
Bram Moolenaar83d47902020-03-26 20:34:00 +0100674 if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
675 term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
676
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100677 // System dependent: setup the vterm and maybe start the job in it.
Bram Moolenaar13568252018-03-16 20:46:58 +0100678 if (argv == NULL
679 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200680 && argvar->vval.v_string != NULL
681 && STRCMP(argvar->vval.v_string, "NONE") == 0)
682 res = create_pty_only(term, opt);
683 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200684 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200685
686 newbuf = curbuf;
687 if (res == OK)
688 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100689 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200690 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
691 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100692#ifdef FEAT_GUI
693 if (term->tl_system)
694 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100695 // display first line below typed command
Bram Moolenaar13568252018-03-16 20:46:58 +0100696 term->tl_toprow = msg_row + 1;
697 term->tl_dirty_row_end = 0;
698 }
699#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200700
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100701 // Make sure we don't get stuck on sending keys to the job, it leads to
702 // a deadlock if the job is waiting for Vim to read.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200703 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
704
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200705 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200706 {
707 --curbuf->b_nwindows;
708 curbuf = old_curbuf;
709 curwin->w_buffer = curbuf;
710 ++curbuf->b_nwindows;
711 }
712 }
713 else
714 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100715 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200716 return NULL;
717 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100718
Bram Moolenaar13568252018-03-16 20:46:58 +0100719 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar28ed4df2019-10-26 16:21:40 +0200720 if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
721 apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200722 return newbuf;
723}
724
725/*
726 * ":terminal": open a terminal window and execute a job in it.
727 */
728 void
729ex_terminal(exarg_T *eap)
730{
731 typval_T argvar[2];
732 jobopt_T opt;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100733 int opt_shell = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200734 char_u *cmd;
735 char_u *tofree = NULL;
736
737 init_job_options(&opt);
738
739 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100740 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200741 {
742 char_u *p, *ep;
743
744 cmd += 2;
745 p = skiptowhite(cmd);
746 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200747 if (ep != NULL)
748 {
749 if (ep < p)
750 p = ep;
751 else
752 ep = NULL;
753 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200754
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200755# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
756 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
757 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200758 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200759 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100760 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200761 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200762 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200763 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200764 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200765 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200766 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200767 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100768 opt.jo_term_norestore = 1;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100769 else if (OPTARG_HAS("shell"))
770 opt_shell = TRUE;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200771 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100772 {
773 opt.jo_set2 |= JO2_TERM_KILL;
774 opt.jo_term_kill = ep + 1;
775 p = skiptowhite(cmd);
776 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200777 else if (OPTARG_HAS("api"))
778 {
779 opt.jo_set2 |= JO2_TERM_API;
780 if (ep != NULL)
781 {
782 opt.jo_term_api = ep + 1;
783 p = skiptowhite(cmd);
784 }
785 else
786 opt.jo_term_api = NULL;
787 }
788 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200789 {
790 opt.jo_set2 |= JO2_TERM_ROWS;
791 opt.jo_term_rows = atoi((char *)ep + 1);
792 p = skiptowhite(cmd);
793 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200794 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200795 {
796 opt.jo_set2 |= JO2_TERM_COLS;
797 opt.jo_term_cols = atoi((char *)ep + 1);
798 p = skiptowhite(cmd);
799 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200800 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200801 {
802 char_u *buf = NULL;
803 char_u *keys;
804
Bram Moolenaar21109272020-01-30 16:27:20 +0100805 vim_free(opt.jo_eof_chars);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200806 p = skiptowhite(cmd);
807 *p = NUL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200808 keys = replace_termcodes(ep + 1, &buf,
809 REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200810 opt.jo_set2 |= JO2_EOF_CHARS;
811 opt.jo_eof_chars = vim_strsave(keys);
812 vim_free(buf);
813 *p = ' ';
814 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100815#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100816 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
817 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100818 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100819 int tty_type = NUL;
820
821 p = skiptowhite(cmd);
822 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
823 tty_type = 'w';
824 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
825 tty_type = 'c';
826 else
827 {
828 semsg(e_invargval, "type");
829 goto theend;
830 }
831 opt.jo_set2 |= JO2_TTY_TYPE;
832 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100833 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100834#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200835 else
836 {
837 if (*p)
838 *p = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100839 semsg(_("E181: Invalid attribute: %s"), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100840 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200841 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200842# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200843 cmd = skipwhite(p);
844 }
845 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100846 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100847 // Make a copy of 'shell', an autocommand may change the option.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200848 tofree = cmd = vim_strsave(p_sh);
849
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100850 // default to close when the shell exits
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100851 if (opt.jo_term_finish == NUL)
Bram Moolenaare2978022020-04-26 14:47:44 +0200852 opt.jo_term_finish = TL_FINISH_CLOSE;
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100853 }
854
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200855 if (eap->addr_count > 0)
856 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100857 // Write lines from current buffer to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200858 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
859 opt.jo_io[PART_IN] = JIO_BUFFER;
860 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
861 opt.jo_in_top = eap->line1;
862 opt.jo_in_bot = eap->line2;
863 }
864
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100865 if (opt_shell && tofree == NULL)
866 {
867#ifdef UNIX
868 char **argv = NULL;
869 char_u *tofree1 = NULL;
870 char_u *tofree2 = NULL;
871
872 // :term ++shell command
873 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
874 term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaaradf4aa22019-11-10 22:36:44 +0100875 vim_free(argv);
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100876 vim_free(tofree1);
877 vim_free(tofree2);
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100878 goto theend;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100879#else
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100880# ifdef MSWIN
881 long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
882 char_u *newcmd;
883
884 newcmd = alloc(cmdlen);
885 if (newcmd == NULL)
886 goto theend;
887 tofree = newcmd;
888 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
889 cmd = newcmd;
890# else
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100891 emsg(_("E279: Sorry, ++shell is not supported on this system"));
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100892 goto theend;
893# endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100894#endif
895 }
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100896 argvar[0].v_type = VAR_STRING;
897 argvar[0].vval.v_string = cmd;
898 argvar[1].v_type = VAR_UNKNOWN;
899 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100900
901theend:
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100902 vim_free(tofree);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200903 vim_free(opt.jo_eof_chars);
904}
905
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100906#if defined(FEAT_SESSION) || defined(PROTO)
907/*
908 * Write a :terminal command to the session file to restore the terminal in
909 * window "wp".
910 * Return FAIL if writing fails.
911 */
912 int
913term_write_session(FILE *fd, win_T *wp)
914{
915 term_T *term = wp->w_buffer->b_term;
916
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100917 // Create the terminal and run the command. This is not without
918 // risk, but let's assume the user only creates a session when this
919 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100920 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
921 term->tl_cols, term->tl_rows) < 0)
922 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100923#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100924 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
925 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100926#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100927 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
928 return FAIL;
929
930 return put_eol(fd);
931}
932
933/*
934 * Return TRUE if "buf" has a terminal that should be restored.
935 */
936 int
937term_should_restore(buf_T *buf)
938{
939 term_T *term = buf->b_term;
940
941 return term != NULL && (term->tl_command == NULL
942 || STRCMP(term->tl_command, "NONE") != 0);
943}
944#endif
945
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200946/*
947 * Free the scrollback buffer for "term".
948 */
949 static void
950free_scrollback(term_T *term)
951{
952 int i;
953
954 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
955 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
956 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100957 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
958 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
959 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200960}
961
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100962
963// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +0200964static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100965
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200966/*
967 * Free a terminal and everything it refers to.
968 * Kills the job if there is one.
969 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100970 * The actual terminal structure is freed later in free_unused_terminals(),
971 * because callbacks may wipe out a buffer while the terminal is still
972 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200973 */
974 void
975free_terminal(buf_T *buf)
976{
977 term_T *term = buf->b_term;
978 term_T *tp;
979
980 if (term == NULL)
981 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100982
983 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200984 if (first_term == term)
985 first_term = term->tl_next;
986 else
987 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
988 if (tp->tl_next == term)
989 {
990 tp->tl_next = term->tl_next;
991 break;
992 }
993
994 if (term->tl_job != NULL)
995 {
996 if (term->tl_job->jv_status != JOB_ENDED
997 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +0100998 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200999 job_stop(term->tl_job, NULL, "kill");
1000 job_unref(term->tl_job);
1001 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001002 term->tl_next = terminals_to_free;
1003 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001004
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001005 buf->b_term = NULL;
1006 if (in_terminal_loop == term)
1007 in_terminal_loop = NULL;
1008}
1009
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001010 void
1011free_unused_terminals()
1012{
1013 while (terminals_to_free != NULL)
1014 {
1015 term_T *term = terminals_to_free;
1016
1017 terminals_to_free = term->tl_next;
1018
1019 free_scrollback(term);
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02001020 ga_clear(&term->tl_osc_buf);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001021
1022 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001023 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001024 vim_free(term->tl_title);
1025#ifdef FEAT_SESSION
1026 vim_free(term->tl_command);
1027#endif
1028 vim_free(term->tl_kill);
1029 vim_free(term->tl_status_text);
1030 vim_free(term->tl_opencmd);
1031 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001032 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001033#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001034 if (term->tl_out_fd != NULL)
1035 fclose(term->tl_out_fd);
1036#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001037 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001038 vim_free(term->tl_cursor_color);
1039 vim_free(term);
1040 }
1041}
1042
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001043/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001044 * Get the part that is connected to the tty. Normally this is PART_IN, but
1045 * when writing buffer lines to the job it can be another. This makes it
1046 * possible to do "1,5term vim -".
1047 */
1048 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001049get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001050{
1051#ifdef UNIX
1052 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1053 int i;
1054
1055 for (i = 0; i < 3; ++i)
1056 {
1057 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1058
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001059 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001060 return parts[i];
1061 }
1062#endif
1063 return PART_IN;
1064}
1065
1066/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001067 * Write job output "msg[len]" to the vterm.
1068 */
1069 static void
1070term_write_job_output(term_T *term, char_u *msg, size_t len)
1071{
1072 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001073 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001074
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001075 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001076
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001077 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001078 if (prevlen != vterm_output_get_buffer_current(vterm))
1079 {
1080 char buf[KEY_BUF_LEN];
1081 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1082
1083 if (curlen > 0)
1084 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1085 (char_u *)buf, (int)curlen, NULL);
1086 }
1087
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001088 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001089 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1090}
1091
1092 static void
1093update_cursor(term_T *term, int redraw)
1094{
1095 if (term->tl_normal_mode)
1096 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001097#ifdef FEAT_GUI
1098 if (term->tl_system)
1099 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1100 term->tl_cursor_pos.col);
1101 else
1102#endif
1103 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001104 if (redraw)
1105 {
1106 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1107 cursor_on();
1108 out_flush();
1109#ifdef FEAT_GUI
1110 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001111 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001112 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001113 gui_mch_flush();
1114 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001115#endif
1116 }
1117}
1118
1119/*
1120 * Invoked when "msg" output from a job was received. Write it to the terminal
1121 * of "buffer".
1122 */
1123 void
1124write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1125{
1126 size_t len = STRLEN(msg);
1127 term_T *term = buffer->b_term;
1128
Bram Moolenaar4f974752019-02-17 17:44:42 +01001129#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001130 // Win32: Cannot redirect output of the job, intercept it here and write to
1131 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001132 if (term->tl_out_fd != NULL)
1133 {
1134 ch_log(channel, "Writing %d bytes to output file", (int)len);
1135 fwrite(msg, len, 1, term->tl_out_fd);
1136 return;
1137 }
1138#endif
1139
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001140 if (term->tl_vterm == NULL)
1141 {
1142 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1143 return;
1144 }
1145 ch_log(channel, "writing %d bytes to terminal", (int)len);
1146 term_write_job_output(term, msg, len);
1147
Bram Moolenaar13568252018-03-16 20:46:58 +01001148#ifdef FEAT_GUI
1149 if (term->tl_system)
1150 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001151 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001152 update_system_term(term);
1153 update_cursor(term, TRUE);
1154 }
1155 else
1156#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001157 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1158 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001159 if (!term->tl_normal_mode)
1160 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001161 // Don't use update_screen() when editing the command line, it gets
1162 // cleared.
1163 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001164 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001165 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001166 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001167 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001168 // update_screen() can be slow, check the terminal wasn't closed
1169 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001170 if (buffer == curbuf && curbuf->b_term != NULL)
1171 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001172 }
1173 else
1174 redraw_after_callback(TRUE);
1175 }
1176}
1177
1178/*
1179 * Send a mouse position and click to the vterm
1180 */
1181 static int
1182term_send_mouse(VTerm *vterm, int button, int pressed)
1183{
1184 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001185 int row = mouse_row - W_WINROW(curwin);
1186 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001187
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001188#ifdef FEAT_PROP_POPUP
1189 if (popup_is_popup(curwin))
1190 {
1191 row -= popup_top_extra(curwin);
1192 col -= popup_left_extra(curwin);
1193 }
1194#endif
1195 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001196 if (button != 0)
1197 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001198 return TRUE;
1199}
1200
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001201static int enter_mouse_col = -1;
1202static int enter_mouse_row = -1;
1203
1204/*
1205 * Handle a mouse click, drag or release.
1206 * Return TRUE when a mouse event is sent to the terminal.
1207 */
1208 static int
1209term_mouse_click(VTerm *vterm, int key)
1210{
1211#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001212 // For modeless selection mouse drag and release events are ignored, unless
1213 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001214 static int ignore_drag_release = TRUE;
1215 VTermMouseState mouse_state;
1216
1217 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1218 if (mouse_state.flags == 0)
1219 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001220 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001221 switch (key)
1222 {
1223 case K_LEFTDRAG:
1224 case K_LEFTRELEASE:
1225 case K_RIGHTDRAG:
1226 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001227 // Ignore drag and release events when the button-down wasn't
1228 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001229 if (ignore_drag_release)
1230 {
1231 int save_mouse_col, save_mouse_row;
1232
1233 if (enter_mouse_col < 0)
1234 break;
1235
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001236 // mouse click in the window gave us focus, handle that
1237 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001238 save_mouse_col = mouse_col;
1239 save_mouse_row = mouse_row;
1240 mouse_col = enter_mouse_col;
1241 mouse_row = enter_mouse_row;
1242 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1243 mouse_col = save_mouse_col;
1244 mouse_row = save_mouse_row;
1245 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001246 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001247 case K_LEFTMOUSE:
1248 case K_RIGHTMOUSE:
1249 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1250 ignore_drag_release = TRUE;
1251 else
1252 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001253 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001254 if (clip_star.available)
1255 {
1256 int button, is_click, is_drag;
1257
1258 button = get_mouse_button(KEY2TERMCAP1(key),
1259 &is_click, &is_drag);
1260 if (mouse_model_popup() && button == MOUSE_LEFT
1261 && (mod_mask & MOD_MASK_SHIFT))
1262 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001263 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001264 button = MOUSE_RIGHT;
1265 mod_mask &= ~MOD_MASK_SHIFT;
1266 }
1267 clip_modeless(button, is_click, is_drag);
1268 }
1269 break;
1270
1271 case K_MIDDLEMOUSE:
1272 if (clip_star.available)
1273 insert_reg('*', TRUE);
1274 break;
1275 }
1276 enter_mouse_col = -1;
1277 return FALSE;
1278 }
1279#endif
1280 enter_mouse_col = -1;
1281
1282 switch (key)
1283 {
1284 case K_LEFTMOUSE:
1285 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1286 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1287 case K_LEFTRELEASE:
1288 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1289 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1290 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1291 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1292 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1293 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1294 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1295 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1296 }
1297 return TRUE;
1298}
1299
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001300/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001301 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1302 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001303 * Return the number of bytes in "buf".
1304 */
1305 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001306term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001307{
1308 VTerm *vterm = term->tl_vterm;
1309 VTermKey key = VTERM_KEY_NONE;
1310 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001311 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001312
1313 switch (c)
1314 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001315 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001316
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001317 // don't use VTERM_KEY_BACKSPACE, it always
1318 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001319 case K_BS: c = term_backspace_char; break;
1320
1321 case ESC: key = VTERM_KEY_ESCAPE; break;
1322 case K_DEL: key = VTERM_KEY_DEL; break;
1323 case K_DOWN: key = VTERM_KEY_DOWN; break;
1324 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1325 key = VTERM_KEY_DOWN; break;
1326 case K_END: key = VTERM_KEY_END; break;
1327 case K_S_END: mod = VTERM_MOD_SHIFT;
1328 key = VTERM_KEY_END; break;
1329 case K_C_END: mod = VTERM_MOD_CTRL;
1330 key = VTERM_KEY_END; break;
1331 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1332 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1333 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1334 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1335 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1336 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1337 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1338 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1339 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1340 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1341 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1342 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1343 case K_HOME: key = VTERM_KEY_HOME; break;
1344 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1345 key = VTERM_KEY_HOME; break;
1346 case K_C_HOME: mod = VTERM_MOD_CTRL;
1347 key = VTERM_KEY_HOME; break;
1348 case K_INS: key = VTERM_KEY_INS; break;
1349 case K_K0: key = VTERM_KEY_KP_0; break;
1350 case K_K1: key = VTERM_KEY_KP_1; break;
1351 case K_K2: key = VTERM_KEY_KP_2; break;
1352 case K_K3: key = VTERM_KEY_KP_3; break;
1353 case K_K4: key = VTERM_KEY_KP_4; break;
1354 case K_K5: key = VTERM_KEY_KP_5; break;
1355 case K_K6: key = VTERM_KEY_KP_6; break;
1356 case K_K7: key = VTERM_KEY_KP_7; break;
1357 case K_K8: key = VTERM_KEY_KP_8; break;
1358 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001359 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001360 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001361 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001362 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001363 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1364 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001365 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1366 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001367 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1368 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001369 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1370 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1371 case K_LEFT: key = VTERM_KEY_LEFT; break;
1372 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1373 key = VTERM_KEY_LEFT; break;
1374 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1375 key = VTERM_KEY_LEFT; break;
1376 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1377 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1378 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1379 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1380 key = VTERM_KEY_RIGHT; break;
1381 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1382 key = VTERM_KEY_RIGHT; break;
1383 case K_UP: key = VTERM_KEY_UP; break;
1384 case K_S_UP: mod = VTERM_MOD_SHIFT;
1385 key = VTERM_KEY_UP; break;
1386 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001387 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1388 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001389
Bram Moolenaara42ad572017-11-16 13:08:04 +01001390 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1391 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001392 case K_MOUSELEFT: /* TODO */ return 0;
1393 case K_MOUSERIGHT: /* TODO */ return 0;
1394
1395 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001396 case K_LEFTMOUSE_NM:
1397 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001398 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001399 case K_LEFTRELEASE_NM:
1400 case K_MOUSEMOVE:
1401 case K_MIDDLEMOUSE:
1402 case K_MIDDLEDRAG:
1403 case K_MIDDLERELEASE:
1404 case K_RIGHTMOUSE:
1405 case K_RIGHTDRAG:
1406 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1407 return 0;
1408 other = TRUE;
1409 break;
1410
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001411 case K_X1MOUSE: /* TODO */ return 0;
1412 case K_X1DRAG: /* TODO */ return 0;
1413 case K_X1RELEASE: /* TODO */ return 0;
1414 case K_X2MOUSE: /* TODO */ return 0;
1415 case K_X2DRAG: /* TODO */ return 0;
1416 case K_X2RELEASE: /* TODO */ return 0;
1417
1418 case K_IGNORE: return 0;
1419 case K_NOP: return 0;
1420 case K_UNDO: return 0;
1421 case K_HELP: return 0;
1422 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1423 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1424 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1425 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1426 case K_SELECT: return 0;
1427#ifdef FEAT_GUI
1428 case K_VER_SCROLLBAR: return 0;
1429 case K_HOR_SCROLLBAR: return 0;
1430#endif
1431#ifdef FEAT_GUI_TABLINE
1432 case K_TABLINE: return 0;
1433 case K_TABMENU: return 0;
1434#endif
1435#ifdef FEAT_NETBEANS_INTG
1436 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1437#endif
1438#ifdef FEAT_DND
1439 case K_DROP: return 0;
1440#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001441 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001442 case K_PS: vterm_keyboard_start_paste(vterm);
1443 other = TRUE;
1444 break;
1445 case K_PE: vterm_keyboard_end_paste(vterm);
1446 other = TRUE;
1447 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001448 }
1449
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001450 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001451 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001452 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001453 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001454 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001455 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001456 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001457
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001458 /*
1459 * Convert special keys to vterm keys:
1460 * - Write keys to vterm: vterm_keyboard_key()
1461 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001462 */
1463 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001464 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001465 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001466 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001467 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001468 vterm_keyboard_unichar(vterm, c, mod);
1469
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001470 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001471 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1472}
1473
1474/*
1475 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001476 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001477 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001478 */
1479 static int
1480term_job_running_check(term_T *term, int check_job_status)
1481{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001482 // Also consider the job finished when the channel is closed, to avoid a
1483 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001484 if (term != NULL
1485 && term->tl_job != NULL
1486 && channel_is_open(term->tl_job->jv_channel))
1487 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001488 job_T *job = term->tl_job;
1489
1490 // Careful: Checking the job status may invoked callbacks, which close
1491 // the buffer and terminate "term". However, "job" will not be freed
1492 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001493 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001494 job_status(job);
1495 return (job->jv_status == JOB_STARTED
1496 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001497 }
1498 return FALSE;
1499}
1500
1501/*
1502 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001503 */
1504 int
1505term_job_running(term_T *term)
1506{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001507 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001508}
1509
1510/*
1511 * Return TRUE if "term" has an active channel and used ":term NONE".
1512 */
1513 int
1514term_none_open(term_T *term)
1515{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001516 // Also consider the job finished when the channel is closed, to avoid a
1517 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001518 return term != NULL
1519 && term->tl_job != NULL
1520 && channel_is_open(term->tl_job->jv_channel)
1521 && term->tl_job->jv_channel->ch_keep_open;
1522}
1523
1524/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001525 * Used when exiting: kill the job in "buf" if so desired.
1526 * Return OK when the job finished.
1527 * Return FAIL when the job is still running.
1528 */
1529 int
1530term_try_stop_job(buf_T *buf)
1531{
1532 int count;
1533 char *how = (char *)buf->b_term->tl_kill;
1534
1535#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1536 if ((how == NULL || *how == NUL) && (p_confirm || cmdmod.confirm))
1537 {
1538 char_u buff[DIALOG_MSG_SIZE];
1539 int ret;
1540
1541 dialog_msg(buff, _("Kill job in \"%s\"?"), buf->b_fname);
1542 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1543 if (ret == VIM_YES)
1544 how = "kill";
1545 else if (ret == VIM_CANCEL)
1546 return FAIL;
1547 }
1548#endif
1549 if (how == NULL || *how == NUL)
1550 return FAIL;
1551
1552 job_stop(buf->b_term->tl_job, NULL, how);
1553
Bram Moolenaar9172d232019-01-29 23:06:54 +01001554 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001555 for (count = 0; count < 100; ++count)
1556 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001557 job_T *job;
1558
1559 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001560 if (!buf_valid(buf)
1561 || buf->b_term == NULL
1562 || buf->b_term->tl_job == NULL)
1563 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001564 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001565
Bram Moolenaar9172d232019-01-29 23:06:54 +01001566 // Call job_status() to update jv_status. It may cause the job to be
1567 // cleaned up but it won't be freed.
1568 job_status(job);
1569 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001570 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001571
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001572 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001573 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001574 }
1575 return FAIL;
1576}
1577
1578/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001579 * Add the last line of the scrollback buffer to the buffer in the window.
1580 */
1581 static void
1582add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1583{
1584 buf_T *buf = term->tl_buffer;
1585 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1586 linenr_T lnum = buf->b_ml.ml_line_count;
1587
Bram Moolenaar4f974752019-02-17 17:44:42 +01001588#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001589 if (!enc_utf8 && enc_codepage > 0)
1590 {
1591 WCHAR *ret = NULL;
1592 int length = 0;
1593
1594 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1595 &ret, &length);
1596 if (ret != NULL)
1597 {
1598 WideCharToMultiByte_alloc(enc_codepage, 0,
1599 ret, length, (char **)&text, &len, 0, 0);
1600 vim_free(ret);
1601 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1602 vim_free(text);
1603 }
1604 }
1605 else
1606#endif
1607 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1608 if (empty)
1609 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001610 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001611 curbuf = buf;
1612 ml_delete(1, FALSE);
1613 curbuf = curwin->w_buffer;
1614 }
1615}
1616
1617 static void
1618cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1619{
1620 attr->width = cell->width;
1621 attr->attrs = cell->attrs;
1622 attr->fg = cell->fg;
1623 attr->bg = cell->bg;
1624}
1625
1626 static int
1627equal_celattr(cellattr_T *a, cellattr_T *b)
1628{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02001629 // We only compare the RGB colors, ignoring the ANSI index and type.
1630 // Thus black set explicitly is equal the background black.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001631 return a->fg.red == b->fg.red
1632 && a->fg.green == b->fg.green
1633 && a->fg.blue == b->fg.blue
1634 && a->bg.red == b->bg.red
1635 && a->bg.green == b->bg.green
1636 && a->bg.blue == b->bg.blue;
1637}
1638
Bram Moolenaard96ff162018-02-18 22:13:29 +01001639/*
1640 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1641 * line at this position. Otherwise at the end.
1642 */
1643 static int
1644add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1645{
1646 if (ga_grow(&term->tl_scrollback, 1) == OK)
1647 {
1648 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1649 + term->tl_scrollback.ga_len;
1650
1651 if (lnum > 0)
1652 {
1653 int i;
1654
1655 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1656 {
1657 *line = *(line - 1);
1658 --line;
1659 }
1660 }
1661 line->sb_cols = 0;
1662 line->sb_cells = NULL;
1663 line->sb_fill_attr = *fill_attr;
1664 ++term->tl_scrollback.ga_len;
1665 return OK;
1666 }
1667 return FALSE;
1668}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001669
1670/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001671 * Remove the terminal contents from the scrollback and the buffer.
1672 * Used before adding a new scrollback line or updating the buffer for lines
1673 * displayed in the terminal.
1674 */
1675 static void
1676cleanup_scrollback(term_T *term)
1677{
1678 sb_line_T *line;
1679 garray_T *gap;
1680
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001681 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001682 gap = &term->tl_scrollback;
1683 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1684 && gap->ga_len > 0)
1685 {
1686 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1687 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1688 vim_free(line->sb_cells);
1689 --gap->ga_len;
1690 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001691 curbuf = curwin->w_buffer;
1692 if (curbuf == term->tl_buffer)
1693 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001694}
1695
1696/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001697 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001698 */
1699 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001700update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001701{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001702 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001703 int len;
1704 int lines_skipped = 0;
1705 VTermPos pos;
1706 VTermScreenCell cell;
1707 cellattr_T fill_attr, new_fill_attr;
1708 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001709
1710 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1711 "Adding terminal window snapshot to buffer");
1712
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001713 // First remove the lines that were appended before, they might be
1714 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001715 cleanup_scrollback(term);
1716
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001717 screen = vterm_obtain_screen(term->tl_vterm);
1718 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001719 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1720 {
1721 len = 0;
1722 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1723 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1724 && cell.chars[0] != NUL)
1725 {
1726 len = pos.col + 1;
1727 new_fill_attr = term->tl_default_color;
1728 }
1729 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001730 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001731 cell2cellattr(&cell, &new_fill_attr);
1732
1733 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1734 ++lines_skipped;
1735 else
1736 {
1737 while (lines_skipped > 0)
1738 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001739 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001740 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001741 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001742 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001743 }
1744
1745 if (len == 0)
1746 p = NULL;
1747 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001748 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001749 if ((p != NULL || len == 0)
1750 && ga_grow(&term->tl_scrollback, 1) == OK)
1751 {
1752 garray_T ga;
1753 int width;
1754 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1755 + term->tl_scrollback.ga_len;
1756
1757 ga_init2(&ga, 1, 100);
1758 for (pos.col = 0; pos.col < len; pos.col += width)
1759 {
1760 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1761 {
1762 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001763 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001764 if (ga_grow(&ga, 1) == OK)
1765 ga.ga_len += utf_char2bytes(' ',
1766 (char_u *)ga.ga_data + ga.ga_len);
1767 }
1768 else
1769 {
1770 width = cell.width;
1771
1772 cell2cellattr(&cell, &p[pos.col]);
1773
Bram Moolenaara79fd562018-12-20 20:47:32 +01001774 // Each character can be up to 6 bytes.
1775 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001776 {
1777 int i;
1778 int c;
1779
1780 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1781 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1782 (char_u *)ga.ga_data + ga.ga_len);
1783 }
1784 }
1785 }
1786 line->sb_cols = len;
1787 line->sb_cells = p;
1788 line->sb_fill_attr = new_fill_attr;
1789 fill_attr = new_fill_attr;
1790 ++term->tl_scrollback.ga_len;
1791
1792 if (ga_grow(&ga, 1) == FAIL)
1793 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1794 else
1795 {
1796 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1797 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1798 }
1799 ga_clear(&ga);
1800 }
1801 else
1802 vim_free(p);
1803 }
1804 }
1805
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001806 // Add trailing empty lines.
1807 for (pos.row = term->tl_scrollback.ga_len;
1808 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1809 ++pos.row)
1810 {
1811 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1812 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1813 }
1814
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001815 term->tl_dirty_snapshot = FALSE;
1816#ifdef FEAT_TIMERS
1817 term->tl_timer_set = FALSE;
1818#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001819}
1820
1821/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001822 * Loop over all windows in the current tab, and also curwin, which is not
1823 * encountered when using a terminal in a popup window.
1824 * Return TRUE if "*wp" was set to the next window.
1825 */
1826 static int
1827for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1828{
1829 if (*wp == NULL)
1830 *wp = firstwin;
1831 else if ((*wp)->w_next != NULL)
1832 *wp = (*wp)->w_next;
1833 else if (!*did_curwin)
1834 *wp = curwin;
1835 else
1836 return FALSE;
1837 if (*wp == curwin)
1838 *did_curwin = TRUE;
1839 return TRUE;
1840}
1841
1842/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001843 * If needed, add the current lines of the terminal to scrollback and to the
1844 * buffer. Called after the job has ended and when switching to
1845 * Terminal-Normal mode.
1846 * When "redraw" is TRUE redraw the windows that show the terminal.
1847 */
1848 static void
1849may_move_terminal_to_buffer(term_T *term, int redraw)
1850{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001851 if (term->tl_vterm == NULL)
1852 return;
1853
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001854 // Update the snapshot only if something changes or the buffer does not
1855 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001856 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1857 <= term->tl_scrollback_scrolled)
1858 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001859
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001860 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001861 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1862 &term->tl_default_color.fg, &term->tl_default_color.bg);
1863
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001864 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001865 {
1866 win_T *wp = NULL;
1867 int did_curwin = FALSE;
1868
1869 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001870 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001871 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001872 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001873 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1874 wp->w_cursor.col = 0;
1875 wp->w_valid = 0;
1876 if (wp->w_cursor.lnum >= wp->w_height)
1877 {
1878 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001879
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001880 if (wp->w_topline < min_topline)
1881 wp->w_topline = min_topline;
1882 }
1883 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001884 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001885 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001886 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001887}
1888
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001889#if defined(FEAT_TIMERS) || defined(PROTO)
1890/*
1891 * Check if any terminal timer expired. If so, copy text from the terminal to
1892 * the buffer.
1893 * Return the time until the next timer will expire.
1894 */
1895 int
1896term_check_timers(int next_due_arg, proftime_T *now)
1897{
1898 term_T *term;
1899 int next_due = next_due_arg;
1900
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001901 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001902 {
1903 if (term->tl_timer_set && !term->tl_normal_mode)
1904 {
1905 long this_due = proftime_time_left(&term->tl_timer_due, now);
1906
1907 if (this_due <= 1)
1908 {
1909 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001910 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001911 }
1912 else if (next_due == -1 || next_due > this_due)
1913 next_due = this_due;
1914 }
1915 }
1916
1917 return next_due;
1918}
1919#endif
1920
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001921/*
1922 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
1923 * otherwise end it.
1924 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001925 static void
1926set_terminal_mode(term_T *term, int normal_mode)
1927{
1928 term->tl_normal_mode = normal_mode;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001929 if (!normal_mode)
1930 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001931 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001932 if (term->tl_buffer == curbuf)
1933 maketitle();
1934}
1935
1936/*
Bram Moolenaare2978022020-04-26 14:47:44 +02001937 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001938 * Move the vterm contents into the scrollback buffer and free the vterm.
1939 */
1940 static void
1941cleanup_vterm(term_T *term)
1942{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001943 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01001944 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001945 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001946 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001947}
1948
1949/*
1950 * Switch from Terminal-Job mode to Terminal-Normal mode.
1951 * Suspends updating the terminal window.
1952 */
1953 static void
1954term_enter_normal_mode(void)
1955{
1956 term_T *term = curbuf->b_term;
1957
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001958 set_terminal_mode(term, TRUE);
1959
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001960 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001961 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001962
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001963 // Move the window cursor to the position of the cursor in the
1964 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001965 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1966 + term->tl_cursor_pos.row + 1;
1967 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02001968 if (coladvance(term->tl_cursor_pos.col) == FAIL)
1969 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001970 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001971
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001972 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001973 curwin->w_topline = term->tl_scrollback_scrolled + 1;
1974}
1975
1976/*
1977 * Returns TRUE if the current window contains a terminal and we are in
1978 * Terminal-Normal mode.
1979 */
1980 int
1981term_in_normal_mode(void)
1982{
1983 term_T *term = curbuf->b_term;
1984
1985 return term != NULL && term->tl_normal_mode;
1986}
1987
1988/*
1989 * Switch from Terminal-Normal mode to Terminal-Job mode.
1990 * Restores updating the terminal window.
1991 */
1992 void
1993term_enter_job_mode()
1994{
1995 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001996
1997 set_terminal_mode(term, FALSE);
1998
1999 if (term->tl_channel_closed)
2000 cleanup_vterm(term);
2001 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002002#ifdef FEAT_PROP_POPUP
2003 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002004 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002005#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002006}
2007
2008/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002009 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002010 * Note: while waiting a terminal may be closed and freed if the channel is
2011 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002012 */
2013 static int
2014term_vgetc()
2015{
2016 int c;
2017 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002018 int modify_other_keys =
2019 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002020
2021 State = TERMINAL;
2022 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002023#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002024 ctrl_break_was_pressed = FALSE;
2025#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002026 if (modify_other_keys)
2027 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002028 c = vgetc();
2029 got_int = FALSE;
2030 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002031 if (modify_other_keys)
2032 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002033 return c;
2034}
2035
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002036static int mouse_was_outside = FALSE;
2037
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002038/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002039 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002040 * Return FAIL when the key needs to be handled in Normal mode.
2041 * Return OK when the key was dropped or sent to the terminal.
2042 */
2043 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002044send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002045{
2046 char msg[KEY_BUF_LEN];
2047 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002048 int dragging_outside = FALSE;
2049
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002050 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002051 switch (c)
2052 {
2053 case NUL:
2054 case K_ZERO:
2055 if (typed)
2056 stuffcharReadbuff(c);
2057 return FAIL;
2058
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002059 case K_TABLINE:
2060 stuffcharReadbuff(c);
2061 return FAIL;
2062
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002063 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002064 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002065 return FAIL;
2066
2067 case K_LEFTDRAG:
2068 case K_MIDDLEDRAG:
2069 case K_RIGHTDRAG:
2070 case K_X1DRAG:
2071 case K_X2DRAG:
2072 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002073 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002074 case K_LEFTMOUSE:
2075 case K_LEFTMOUSE_NM:
2076 case K_LEFTRELEASE:
2077 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002078 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002079 case K_MIDDLEMOUSE:
2080 case K_MIDDLERELEASE:
2081 case K_RIGHTMOUSE:
2082 case K_RIGHTRELEASE:
2083 case K_X1MOUSE:
2084 case K_X1RELEASE:
2085 case K_X2MOUSE:
2086 case K_X2RELEASE:
2087
2088 case K_MOUSEUP:
2089 case K_MOUSEDOWN:
2090 case K_MOUSELEFT:
2091 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002092 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002093 int row = mouse_row;
2094 int col = mouse_col;
2095
2096#ifdef FEAT_PROP_POPUP
2097 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002098 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002099 row -= popup_top_extra(curwin);
2100 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002101 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002102#endif
2103 if (row < W_WINROW(curwin)
2104 || row >= (W_WINROW(curwin) + curwin->w_height)
2105 || col < curwin->w_wincol
2106 || col >= W_ENDCOL(curwin)
2107 || dragging_outside)
2108 {
2109 // click or scroll outside the current window or on status
2110 // line or vertical separator
2111 if (typed)
2112 {
2113 stuffcharReadbuff(c);
2114 mouse_was_outside = TRUE;
2115 }
2116 return FAIL;
2117 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002118 }
2119 }
2120 if (typed)
2121 mouse_was_outside = FALSE;
2122
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002123 // Convert the typed key to a sequence of bytes for the job.
2124 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002125 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002126 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002127 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2128 (char_u *)msg, (int)len, NULL);
2129
2130 return OK;
2131}
2132
2133 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002134position_cursor(win_T *wp, VTermPos *pos, int add_off UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002135{
2136 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2137 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002138#ifdef FEAT_PROP_POPUP
2139 if (add_off && popup_is_popup(curwin))
2140 {
2141 wp->w_wrow += popup_top_extra(curwin);
2142 wp->w_wcol += popup_left_extra(curwin);
2143 }
2144#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002145 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2146}
2147
2148/*
2149 * Handle CTRL-W "": send register contents to the job.
2150 */
2151 static void
2152term_paste_register(int prev_c UNUSED)
2153{
2154 int c;
2155 list_T *l;
2156 listitem_T *item;
2157 long reglen = 0;
2158 int type;
2159
2160#ifdef FEAT_CMDL_INFO
2161 if (add_to_showcmd(prev_c))
2162 if (add_to_showcmd('"'))
2163 out_flush();
2164#endif
2165 c = term_vgetc();
2166#ifdef FEAT_CMDL_INFO
2167 clear_showcmd();
2168#endif
2169 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002170 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002171 return;
2172
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002173 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002174 if (c == '=' && get_expr_register() != '=')
2175 return;
2176 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002177 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002178 return;
2179
2180 l = (list_T *)get_reg_contents(c, GREG_LIST);
2181 if (l != NULL)
2182 {
2183 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002184 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002185 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002186 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002187#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002188 char_u *tmp = s;
2189
2190 if (!enc_utf8 && enc_codepage > 0)
2191 {
2192 WCHAR *ret = NULL;
2193 int length = 0;
2194
2195 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2196 (int)STRLEN(s), &ret, &length);
2197 if (ret != NULL)
2198 {
2199 WideCharToMultiByte_alloc(CP_UTF8, 0,
2200 ret, length, (char **)&s, &length, 0, 0);
2201 vim_free(ret);
2202 }
2203 }
2204#endif
2205 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2206 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002207#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002208 if (tmp != s)
2209 vim_free(s);
2210#endif
2211
2212 if (item->li_next != NULL || type == MLINE)
2213 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2214 (char_u *)"\r", 1, NULL);
2215 }
2216 list_free(l);
2217 }
2218}
2219
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002220/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002221 * Return TRUE when waiting for a character in the terminal, the cursor of the
2222 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002223 */
2224 int
2225terminal_is_active()
2226{
2227 return in_terminal_loop != NULL;
2228}
2229
Bram Moolenaar83d47902020-03-26 20:34:00 +01002230/*
2231 * Return the highight group name for the terminal; "Terminal" if not set.
2232 */
2233 static char_u *
2234term_get_highlight_name(term_T *term)
2235{
2236 if (term->tl_highlight_name == NULL)
2237 return (char_u *)"Terminal";
2238 return term->tl_highlight_name;
2239}
2240
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002241#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002242 cursorentry_T *
2243term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2244{
2245 term_T *term = in_terminal_loop;
2246 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002247 int id;
2248 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002249
Bram Moolenaara80faa82020-04-12 19:37:17 +02002250 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002251 entry.shape = entry.mshape =
2252 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2253 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2254 SHAPE_BLOCK;
2255 entry.percentage = 20;
2256 if (term->tl_cursor_blink)
2257 {
2258 entry.blinkwait = 700;
2259 entry.blinkon = 400;
2260 entry.blinkoff = 250;
2261 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002262
Bram Moolenaar83d47902020-03-26 20:34:00 +01002263 // The highlight group overrules the defaults.
2264 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002265 if (id != 0)
2266 {
2267 syn_id2colors(id, &term_fg, &term_bg);
2268 *fg = term_bg;
2269 }
2270 else
2271 *fg = gui.back_pixel;
2272
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002273 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002274 {
2275 if (id != 0)
2276 *bg = term_fg;
2277 else
2278 *bg = gui.norm_pixel;
2279 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002280 else
2281 *bg = color_name2handle(term->tl_cursor_color);
2282 entry.name = "n";
2283 entry.used_for = SHAPE_CURSOR;
2284
2285 return &entry;
2286}
2287#endif
2288
Bram Moolenaard317b382018-02-08 22:33:31 +01002289 static void
2290may_output_cursor_props(void)
2291{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002292 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002293 || last_set_cursor_shape != desired_cursor_shape
2294 || last_set_cursor_blink != desired_cursor_blink)
2295 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002296 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002297 last_set_cursor_shape = desired_cursor_shape;
2298 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002299 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002300 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002301 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002302 ui_cursor_shape_forced(TRUE);
2303 else
2304 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2305 }
2306}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002307
Bram Moolenaard317b382018-02-08 22:33:31 +01002308/*
2309 * Set the cursor color and shape, if not last set to these.
2310 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002311 static void
2312may_set_cursor_props(term_T *term)
2313{
2314#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002315 // For the GUI the cursor properties are obtained with
2316 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002317 if (gui.in_use)
2318 return;
2319#endif
2320 if (in_terminal_loop == term)
2321 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002322 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002323 desired_cursor_shape = term->tl_cursor_shape;
2324 desired_cursor_blink = term->tl_cursor_blink;
2325 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002326 }
2327}
2328
Bram Moolenaard317b382018-02-08 22:33:31 +01002329/*
2330 * Reset the desired cursor properties and restore them when needed.
2331 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002332 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002333prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002334{
2335#ifdef FEAT_GUI
2336 if (gui.in_use)
2337 return;
2338#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002339 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002340 desired_cursor_shape = -1;
2341 desired_cursor_blink = -1;
2342 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002343}
2344
2345/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002346 * Returns TRUE if the current window contains a terminal and we are sending
2347 * keys to the job.
2348 * If "check_job_status" is TRUE update the job status.
2349 */
2350 static int
2351term_use_loop_check(int check_job_status)
2352{
2353 term_T *term = curbuf->b_term;
2354
2355 return term != NULL
2356 && !term->tl_normal_mode
2357 && term->tl_vterm != NULL
2358 && term_job_running_check(term, check_job_status);
2359}
2360
2361/*
2362 * Returns TRUE if the current window contains a terminal and we are sending
2363 * keys to the job.
2364 */
2365 int
2366term_use_loop(void)
2367{
2368 return term_use_loop_check(FALSE);
2369}
2370
2371/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002372 * Called when entering a window with the mouse. If this is a terminal window
2373 * we may want to change state.
2374 */
2375 void
2376term_win_entered()
2377{
2378 term_T *term = curbuf->b_term;
2379
2380 if (term != NULL)
2381 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002382 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002383 {
2384 reset_VIsual_and_resel();
2385 if (State & INSERT)
2386 stop_insert_mode = TRUE;
2387 }
2388 mouse_was_outside = FALSE;
2389 enter_mouse_col = mouse_col;
2390 enter_mouse_row = mouse_row;
2391 }
2392}
2393
2394/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002395 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2396 * Return the Ctrl-key value in that case.
2397 */
2398 static int
2399raw_c_to_ctrl(int c)
2400{
2401 if ((mod_mask & MOD_MASK_CTRL)
2402 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2403 return c & 0x1f;
2404 return c;
2405}
2406
2407/*
2408 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2409 * May set "mod_mask".
2410 */
2411 static int
2412ctrl_to_raw_c(int c)
2413{
2414 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2415 {
2416 mod_mask |= MOD_MASK_CTRL;
2417 return c + '@';
2418 }
2419 return c;
2420}
2421
2422/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002423 * Wait for input and send it to the job.
2424 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2425 * when there is no more typahead.
2426 * Return when the start of a CTRL-W command is typed or anything else that
2427 * should be handled as a Normal mode command.
2428 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2429 * the terminal was closed.
2430 */
2431 int
2432terminal_loop(int blocking)
2433{
2434 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002435 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002436 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002437 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002438#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002439 int tty_fd = curbuf->b_term->tl_job->jv_channel
2440 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002441#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002442 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002443
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002444 // Remember the terminal we are sending keys to. However, the terminal
2445 // might be closed while waiting for a character, e.g. typing "exit" in a
2446 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2447 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002448 in_terminal_loop = curbuf->b_term;
2449
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002450 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002451 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002452 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002453 if (termwinkey == Ctrl_W)
2454 termwinkey = 0;
2455 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002456 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002457 may_set_cursor_props(curbuf->b_term);
2458
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002459 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002460 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002461#ifdef FEAT_GUI
2462 if (!curbuf->b_term->tl_system)
2463#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002464 // TODO: skip screen update when handling a sequence of keys.
2465 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002466 while (must_redraw != 0)
2467 if (update_screen(0) == FAIL)
2468 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002469 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002470 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002471 break;
2472
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002473 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002474 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002475
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002476 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002477 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002478 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002479 // Job finished while waiting for a character. Push back the
2480 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002481 if (raw_c != K_IGNORE)
2482 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002483 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002484 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002485 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002486 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002487 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002488
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002489#ifdef UNIX
2490 /*
2491 * The shell or another program may change the tty settings. Getting
2492 * them for every typed character is a bit of overhead, but it's needed
2493 * for the first character typed, e.g. when Vim starts in a shell.
2494 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002495 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002496 {
2497 ttyinfo_T info;
2498
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002499 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002500 if (get_tty_info(tty_fd, &info) == OK)
2501 term_backspace_char = info.backspace;
2502 }
2503#endif
2504
Bram Moolenaar4f974752019-02-17 17:44:42 +01002505#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002506 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2507 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002508 if (ctrl_break_was_pressed)
2509 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2510#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002511 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2512 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002513 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002514#ifdef FEAT_GUI
2515 && !curbuf->b_term->tl_system
2516#endif
2517 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002518 {
2519 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002520 int prev_raw_c = raw_c;
2521 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002522
2523#ifdef FEAT_CMDL_INFO
2524 if (add_to_showcmd(c))
2525 out_flush();
2526#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002527 raw_c = term_vgetc();
2528 c = raw_c_to_ctrl(raw_c);
2529
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002530#ifdef FEAT_CMDL_INFO
2531 clear_showcmd();
2532#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002533 if (!term_use_loop_check(TRUE)
2534 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002535 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002536 break;
2537
2538 if (prev_c == Ctrl_BSL)
2539 {
2540 if (c == Ctrl_N)
2541 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002542 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002543 term_enter_normal_mode();
2544 ret = FAIL;
2545 goto theend;
2546 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002547 // Send both keys to the terminal, first one here, second one
2548 // below.
2549 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2550 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002551 }
2552 else if (c == Ctrl_C)
2553 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002554 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002555 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2556 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002557 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002558 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002559 // "CTRL-W .": send CTRL-W to the job
2560 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002561 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002562 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002563 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002564 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002565 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002566 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002567 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002568 else if (c == 'N')
2569 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002570 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002571 term_enter_normal_mode();
2572 ret = FAIL;
2573 goto theend;
2574 }
2575 else if (c == '"')
2576 {
2577 term_paste_register(prev_c);
2578 continue;
2579 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002580 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002581 {
Bram Moolenaara4b26992019-08-15 20:58:54 +02002582 char_u buf[MB_MAXBYTES + 2];
2583
2584 // Put the command into the typeahead buffer, when using the
2585 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2586 buf[0] = Ctrl_W;
2587 buf[(*mb_char2bytes)(c, buf + 1) + 1] = NUL;
2588 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002589 ret = OK;
2590 goto theend;
2591 }
2592 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002593# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002594 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002595 {
2596 WCHAR wc;
2597 char_u mb[3];
2598
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002599 mb[0] = (unsigned)raw_c >> 8;
2600 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002601 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002602 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002603 }
2604# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002605 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002606 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002607 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002608 // We are sure to come back here, don't reset the cursor color
2609 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002610 restore_cursor = FALSE;
2611
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002612 ret = OK;
2613 goto theend;
2614 }
2615 }
2616 ret = FAIL;
2617
2618theend:
2619 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002620 if (restore_cursor)
2621 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002622
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002623 // Move a snapshot of the screen contents to the buffer, so that completion
2624 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002625 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2626 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002627
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002628 return ret;
2629}
2630
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002631 static void
2632may_toggle_cursor(term_T *term)
2633{
2634 if (in_terminal_loop == term)
2635 {
2636 if (term->tl_cursor_visible)
2637 cursor_on();
2638 else
2639 cursor_off();
2640 }
2641}
2642
2643/*
Bram Moolenaar83d47902020-03-26 20:34:00 +01002644 * Cache "Terminal" highlight group colors.
2645 */
2646 void
2647set_terminal_default_colors(int cterm_fg, int cterm_bg)
2648{
2649 term_default_cterm_fg = cterm_fg - 1;
2650 term_default_cterm_bg = cterm_bg - 1;
2651}
2652
2653 static int
2654get_default_cterm_fg(term_T *term)
2655{
2656 if (term->tl_highlight_name != NULL)
2657 {
2658 int id = syn_name2id(term->tl_highlight_name);
2659 int fg = -1;
2660 int bg = -1;
2661
2662 if (id > 0)
2663 syn_id2cterm_bg(id, &fg, &bg);
2664 return fg;
2665 }
2666 return term_default_cterm_fg;
2667}
2668
2669 static int
2670get_default_cterm_bg(term_T *term)
2671{
2672 if (term->tl_highlight_name != NULL)
2673 {
2674 int id = syn_name2id(term->tl_highlight_name);
2675 int fg = -1;
2676 int bg = -1;
2677
2678 if (id > 0)
2679 syn_id2cterm_bg(id, &fg, &bg);
2680 return bg;
2681 }
2682 return term_default_cterm_bg;
2683}
2684
2685/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002686 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002687 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002688 */
2689 static int
2690color2index(VTermColor *color, int fg, int *boldp)
2691{
2692 int red = color->red;
2693 int blue = color->blue;
2694 int green = color->green;
2695
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002696 if (VTERM_COLOR_IS_DEFAULT_FG(color)
2697 || VTERM_COLOR_IS_DEFAULT_BG(color))
2698 return 0;
2699 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002700 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002701 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002702 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002703 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002704 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002705 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2706 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2707 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002708 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002709 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2710 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2711 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2712 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2713 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2714 case 10: return lookup_color(20, fg, boldp) + 1; // red
2715 case 11: return lookup_color(16, fg, boldp) + 1; // green
2716 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2717 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2718 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2719 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2720 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002721 }
2722 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002723
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002724 if (t_colors >= 256)
2725 {
2726 if (red == blue && red == green)
2727 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002728 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002729 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002730 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2731 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2732 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002733 int i;
2734
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002735 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002736 return 17; // 00/00/00
2737 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002738 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002739 for (i = 0; i < 23; ++i)
2740 if (red < cutoff[i])
2741 return i + 233;
2742 return 256;
2743 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002744 {
2745 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2746 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002747
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002748 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002749 for (ri = 0; ri < 5; ++ri)
2750 if (red < cutoff[ri])
2751 break;
2752 for (gi = 0; gi < 5; ++gi)
2753 if (green < cutoff[gi])
2754 break;
2755 for (bi = 0; bi < 5; ++bi)
2756 if (blue < cutoff[bi])
2757 break;
2758 return 17 + ri * 36 + gi * 6 + bi;
2759 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002760 }
2761 return 0;
2762}
2763
2764/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002765 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002766 */
2767 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002768vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002769{
2770 int attr = 0;
2771
2772 if (cellattrs.bold)
2773 attr |= HL_BOLD;
2774 if (cellattrs.underline)
2775 attr |= HL_UNDERLINE;
2776 if (cellattrs.italic)
2777 attr |= HL_ITALIC;
2778 if (cellattrs.strike)
2779 attr |= HL_STRIKETHROUGH;
2780 if (cellattrs.reverse)
2781 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002782 return attr;
2783}
2784
2785/*
2786 * Store Vterm attributes in "cell" from highlight flags.
2787 */
2788 static void
2789hl2vtermAttr(int attr, cellattr_T *cell)
2790{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002791 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002792 if (attr & HL_BOLD)
2793 cell->attrs.bold = 1;
2794 if (attr & HL_UNDERLINE)
2795 cell->attrs.underline = 1;
2796 if (attr & HL_ITALIC)
2797 cell->attrs.italic = 1;
2798 if (attr & HL_STRIKETHROUGH)
2799 cell->attrs.strike = 1;
2800 if (attr & HL_INVERSE)
2801 cell->attrs.reverse = 1;
2802}
2803
2804/*
2805 * Convert the attributes of a vterm cell into an attribute index.
2806 */
2807 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002808cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002809 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002810 win_T *wp,
2811 VTermScreenCellAttrs cellattrs,
2812 VTermColor cellfg,
2813 VTermColor cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002814{
2815 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002816
2817#ifdef FEAT_GUI
2818 if (gui.in_use)
2819 {
2820 guicolor_T fg, bg;
2821
2822 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2823 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2824 return get_gui_attr_idx(attr, fg, bg);
2825 }
2826 else
2827#endif
2828#ifdef FEAT_TERMGUICOLORS
2829 if (p_tgc)
2830 {
2831 guicolor_T fg, bg;
2832
2833 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2834 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2835
2836 return get_tgc_attr_idx(attr, fg, bg);
2837 }
2838 else
2839#endif
2840 {
2841 int bold = MAYBE;
2842 int fg = color2index(&cellfg, TRUE, &bold);
2843 int bg = color2index(&cellbg, FALSE, &bold);
2844
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002845 // Use the 'wincolor' or "Terminal" highlighting for the default
2846 // colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002847 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002848 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002849 int wincolor_fg = -1;
2850 int wincolor_bg = -1;
2851
2852 if (wp != NULL && *wp->w_p_wcr != NUL)
2853 {
2854 int id = syn_name2id(curwin->w_p_wcr);
2855
2856 // Get the 'wincolor' group colors.
2857 if (id > 0)
2858 syn_id2cterm_bg(id, &wincolor_fg, &wincolor_bg);
2859 }
2860 if (fg == 0)
2861 {
2862 if (wincolor_fg >= 0)
2863 fg = wincolor_fg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002864 else
2865 {
2866 int cterm_fg = get_default_cterm_fg(term);
2867
2868 if (cterm_fg >= 0)
2869 fg = cterm_fg + 1;
2870 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002871 }
2872 if (bg == 0)
2873 {
2874 if (wincolor_bg >= 0)
2875 bg = wincolor_bg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002876 else
2877 {
2878 int cterm_bg = get_default_cterm_bg(term);
2879
2880 if (cterm_bg >= 0)
2881 bg = cterm_bg + 1;
2882 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002883 }
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002884 }
2885
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002886 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002887 if (bold == TRUE)
2888 attr |= HL_BOLD;
2889 return get_cterm_attr_idx(attr, fg, bg);
2890 }
2891 return 0;
2892}
2893
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002894 static void
2895set_dirty_snapshot(term_T *term)
2896{
2897 term->tl_dirty_snapshot = TRUE;
2898#ifdef FEAT_TIMERS
2899 if (!term->tl_normal_mode)
2900 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002901 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002902 profile_setlimit(100L, &term->tl_timer_due);
2903 term->tl_timer_set = TRUE;
2904 }
2905#endif
2906}
2907
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002908 static int
2909handle_damage(VTermRect rect, void *user)
2910{
2911 term_T *term = (term_T *)user;
2912
2913 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2914 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002915 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002916 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002917 return 1;
2918}
2919
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002920 static void
2921term_scroll_up(term_T *term, int start_row, int count)
2922{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002923 win_T *wp = NULL;
2924 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002925 VTermColor fg, bg;
2926 VTermScreenCellAttrs attr;
2927 int clear_attr;
2928
Bram Moolenaara80faa82020-04-12 19:37:17 +02002929 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002930
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002931 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002932 {
2933 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002934 {
2935 // Set the color to clear lines with.
2936 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2937 &fg, &bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002938 clear_attr = cell2attr(term, wp, attr, fg, bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002939 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002940 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002941 }
2942}
2943
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002944 static int
2945handle_moverect(VTermRect dest, VTermRect src, void *user)
2946{
2947 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002948 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002949
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002950 // Scrolling up is done much more efficiently by deleting lines instead of
2951 // redrawing the text. But avoid doing this multiple times, postpone until
2952 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002953 if (dest.start_col == src.start_col
2954 && dest.end_col == src.end_col
2955 && dest.start_row < src.start_row)
2956 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002957 if (dest.start_row == 0)
2958 term->tl_postponed_scroll += count;
2959 else
2960 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002961 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02002962
2963 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
2964 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002965 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02002966
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002967 // Note sure if the scrolling will work correctly, let's do a complete
2968 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002969 redraw_buf_later(term->tl_buffer, NOT_VALID);
2970 return 1;
2971}
2972
2973 static int
2974handle_movecursor(
2975 VTermPos pos,
2976 VTermPos oldpos UNUSED,
2977 int visible,
2978 void *user)
2979{
2980 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002981 win_T *wp = NULL;
2982 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002983
2984 term->tl_cursor_pos = pos;
2985 term->tl_cursor_visible = visible;
2986
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002987 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002988 {
2989 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002990 position_cursor(wp, &pos, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002991 }
2992 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
2993 {
2994 may_toggle_cursor(term);
2995 update_cursor(term, term->tl_cursor_visible);
2996 }
2997
2998 return 1;
2999}
3000
3001 static int
3002handle_settermprop(
3003 VTermProp prop,
3004 VTermValue *value,
3005 void *user)
3006{
3007 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003008 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003009
3010 switch (prop)
3011 {
3012 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003013 strval = vim_strnsave((char_u *)value->string.str,
3014 (int)value->string.len);
3015 if (strval == NULL)
3016 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003017 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003018 // a blank title isn't useful, make it empty, so that "running" is
3019 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003020 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003021 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003022 // Same as blank
3023 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003024 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003025 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3026 term->tl_title = NULL;
3027 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003028 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003029 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003030#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003031 else if (!enc_utf8 && enc_codepage > 0)
3032 {
3033 WCHAR *ret = NULL;
3034 int length = 0;
3035
3036 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003037 (char*)value->string.str,
3038 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003039 if (ret != NULL)
3040 {
3041 WideCharToMultiByte_alloc(enc_codepage, 0,
3042 ret, length, (char**)&term->tl_title,
3043 &length, 0, 0);
3044 vim_free(ret);
3045 }
3046 }
3047#endif
3048 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003049 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003050 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003051 strval = NULL;
3052 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003053 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003054 if (term == curbuf->b_term)
3055 maketitle();
3056 break;
3057
3058 case VTERM_PROP_CURSORVISIBLE:
3059 term->tl_cursor_visible = value->boolean;
3060 may_toggle_cursor(term);
3061 out_flush();
3062 break;
3063
3064 case VTERM_PROP_CURSORBLINK:
3065 term->tl_cursor_blink = value->boolean;
3066 may_set_cursor_props(term);
3067 break;
3068
3069 case VTERM_PROP_CURSORSHAPE:
3070 term->tl_cursor_shape = value->number;
3071 may_set_cursor_props(term);
3072 break;
3073
3074 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003075 strval = vim_strnsave((char_u *)value->string.str,
3076 (int)value->string.len);
3077 if (strval == NULL)
3078 break;
3079 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003080 may_set_cursor_props(term);
3081 break;
3082
3083 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003084 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003085 term->tl_using_altscreen = value->boolean;
3086 break;
3087
3088 default:
3089 break;
3090 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003091 vim_free(strval);
3092
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003093 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003094 return 1;
3095}
3096
3097/*
3098 * The job running in the terminal resized the terminal.
3099 */
3100 static int
3101handle_resize(int rows, int cols, void *user)
3102{
3103 term_T *term = (term_T *)user;
3104 win_T *wp;
3105
3106 term->tl_rows = rows;
3107 term->tl_cols = cols;
3108 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003109 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003110 term->tl_vterm_size_changed = FALSE;
3111 else
3112 {
3113 FOR_ALL_WINDOWS(wp)
3114 {
3115 if (wp->w_buffer == term->tl_buffer)
3116 {
3117 win_setheight_win(rows, wp);
3118 win_setwidth_win(cols, wp);
3119 }
3120 }
3121 redraw_buf_later(term->tl_buffer, NOT_VALID);
3122 }
3123 return 1;
3124}
3125
3126/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003127 * If the number of lines that are stored goes over 'termscrollback' then
3128 * delete the first 10%.
3129 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3130 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003131 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003132 static void
3133limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003134{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003135 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003136 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003137 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003138 int i;
3139
3140 curbuf = term->tl_buffer;
3141 for (i = 0; i < todo; ++i)
3142 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003143 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3144 if (update_buffer)
3145 ml_delete(1, FALSE);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003146 }
3147 curbuf = curwin->w_buffer;
3148
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003149 gap->ga_len -= todo;
3150 mch_memmove(gap->ga_data,
3151 (sb_line_T *)gap->ga_data + todo,
3152 sizeof(sb_line_T) * gap->ga_len);
3153 if (update_buffer)
3154 term->tl_scrollback_scrolled -= todo;
3155 }
3156}
3157
3158/*
3159 * Handle a line that is pushed off the top of the screen.
3160 */
3161 static int
3162handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3163{
3164 term_T *term = (term_T *)user;
3165 garray_T *gap;
3166 int update_buffer;
3167
3168 if (term->tl_normal_mode)
3169 {
3170 // In Terminal-Normal mode the user interacts with the buffer, thus we
3171 // must not change it. Postpone adding the scrollback lines.
3172 gap = &term->tl_scrollback_postponed;
3173 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003174 }
3175 else
3176 {
3177 // First remove the lines that were appended before, the pushed line
3178 // goes above it.
3179 cleanup_scrollback(term);
3180 gap = &term->tl_scrollback;
3181 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003182 }
3183
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003184 limit_scrollback(term, gap, update_buffer);
3185
3186 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003187 {
3188 cellattr_T *p = NULL;
3189 int len = 0;
3190 int i;
3191 int c;
3192 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003193 int text_len;
3194 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003195 sb_line_T *line;
3196 garray_T ga;
3197 cellattr_T fill_attr = term->tl_default_color;
3198
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003199 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003200 for (i = 0; i < cols; ++i)
3201 if (cells[i].chars[0] != 0)
3202 len = i + 1;
3203 else
3204 cell2cellattr(&cells[i], &fill_attr);
3205
3206 ga_init2(&ga, 1, 100);
3207 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003208 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003209 if (p != NULL)
3210 {
3211 for (col = 0; col < len; col += cells[col].width)
3212 {
3213 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3214 {
3215 ga.ga_len = 0;
3216 break;
3217 }
3218 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3219 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3220 (char_u *)ga.ga_data + ga.ga_len);
3221 cell2cellattr(&cells[col], &p[col]);
3222 }
3223 }
3224 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003225 {
3226 if (update_buffer)
3227 text = (char_u *)"";
3228 else
3229 text = vim_strsave((char_u *)"");
3230 text_len = 0;
3231 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003232 else
3233 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003234 text = ga.ga_data;
3235 text_len = ga.ga_len;
3236 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003237 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003238 if (update_buffer)
3239 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003240
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003241 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003242 line->sb_cols = len;
3243 line->sb_cells = p;
3244 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003245 if (update_buffer)
3246 {
3247 line->sb_text = NULL;
3248 ++term->tl_scrollback_scrolled;
3249 ga_clear(&ga); // free the text
3250 }
3251 else
3252 {
3253 line->sb_text = text;
3254 ga_init(&ga); // text is kept in tl_scrollback_postponed
3255 }
3256 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003257 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003258 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003259}
3260
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003261/*
3262 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3263 * received and stored in tl_scrollback_postponed.
3264 */
3265 static void
3266handle_postponed_scrollback(term_T *term)
3267{
3268 int i;
3269
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003270 if (term->tl_scrollback_postponed.ga_len == 0)
3271 return;
3272 ch_log(NULL, "Moving postponed scrollback to scrollback");
3273
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003274 // First remove the lines that were appended before, the pushed lines go
3275 // above it.
3276 cleanup_scrollback(term);
3277
3278 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3279 {
3280 char_u *text;
3281 sb_line_T *pp_line;
3282 sb_line_T *line;
3283
3284 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3285 break;
3286 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3287
3288 text = pp_line->sb_text;
3289 if (text == NULL)
3290 text = (char_u *)"";
3291 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3292 vim_free(pp_line->sb_text);
3293
3294 line = (sb_line_T *)term->tl_scrollback.ga_data
3295 + term->tl_scrollback.ga_len;
3296 line->sb_cols = pp_line->sb_cols;
3297 line->sb_cells = pp_line->sb_cells;
3298 line->sb_fill_attr = pp_line->sb_fill_attr;
3299 line->sb_text = NULL;
3300 ++term->tl_scrollback_scrolled;
3301 ++term->tl_scrollback.ga_len;
3302 }
3303
3304 ga_clear(&term->tl_scrollback_postponed);
3305 limit_scrollback(term, &term->tl_scrollback, TRUE);
3306}
3307
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003308static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003309 handle_damage, // damage
3310 handle_moverect, // moverect
3311 handle_movecursor, // movecursor
3312 handle_settermprop, // settermprop
3313 NULL, // bell
3314 handle_resize, // resize
3315 handle_pushline, // sb_pushline
3316 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003317};
3318
3319/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003320 * Do the work after the channel of a terminal was closed.
3321 * Must be called only when updating_screen is FALSE.
3322 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3323 */
3324 static int
3325term_after_channel_closed(term_T *term)
3326{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003327 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003328 if (!term->tl_normal_mode)
3329 {
3330 int fnum = term->tl_buffer->b_fnum;
3331
3332 cleanup_vterm(term);
3333
3334 if (term->tl_finish == TL_FINISH_CLOSE)
3335 {
3336 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003337 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003338#ifdef FEAT_PROP_POPUP
3339 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003340
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003341 // If this was a terminal in a popup window, go back to the
3342 // previous window.
3343 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3344 {
3345 pwin = curwin;
3346 if (win_valid(prevwin))
3347 win_enter(prevwin, FALSE);
3348 }
3349 else
3350#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003351 // If this is the last normal window: exit Vim.
3352 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3353 {
3354 exarg_T ea;
3355
Bram Moolenaara80faa82020-04-12 19:37:17 +02003356 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003357 ex_quit(&ea);
3358 return TRUE;
3359 }
3360
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003361 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003362 ch_log(NULL, "terminal job finished, closing window");
3363 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003364 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003365 if (curwin == aucmd_win)
3366 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003367 if (do_set_w_closing)
3368 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003369 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003370 if (do_set_w_closing)
3371 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003372 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003373#ifdef FEAT_PROP_POPUP
3374 if (pwin != NULL)
3375 popup_close_with_retval(pwin, 0);
3376#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003377 return TRUE;
3378 }
3379 if (term->tl_finish == TL_FINISH_OPEN
3380 && term->tl_buffer->b_nwindows == 0)
3381 {
3382 char buf[50];
3383
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003384 // TODO: use term_opencmd
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003385 ch_log(NULL, "terminal job finished, opening window");
3386 vim_snprintf(buf, sizeof(buf),
3387 term->tl_opencmd == NULL
3388 ? "botright sbuf %d"
3389 : (char *)term->tl_opencmd, fnum);
3390 do_cmdline_cmd((char_u *)buf);
3391 }
3392 else
3393 ch_log(NULL, "terminal job finished");
3394 }
3395
3396 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3397 return FALSE;
3398}
3399
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003400#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3401/*
3402 * If the current window is a terminal in a popup window and the job has
3403 * finished, close the popup window and to back to the previous window.
3404 * Otherwise return FAIL.
3405 */
3406 int
3407may_close_term_popup(void)
3408{
3409 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3410 && !term_job_running(curbuf->b_term))
3411 {
3412 win_T *pwin = curwin;
3413
3414 if (win_valid(prevwin))
3415 win_enter(prevwin, FALSE);
3416 popup_close_with_retval(pwin, 0);
3417 return OK;
3418 }
3419 return FAIL;
3420}
3421#endif
3422
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003423/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003424 * Called when a channel has been closed.
3425 * If this was a channel for a terminal window then finish it up.
3426 */
3427 void
3428term_channel_closed(channel_T *ch)
3429{
3430 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003431 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003432 int did_one = FALSE;
3433
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003434 for (term = first_term; term != NULL; term = next_term)
3435 {
3436 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003437 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003438 {
3439 term->tl_channel_closed = TRUE;
3440 did_one = TRUE;
3441
Bram Moolenaard23a8232018-02-10 18:45:26 +01003442 VIM_CLEAR(term->tl_title);
3443 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003444#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003445 if (term->tl_out_fd != NULL)
3446 {
3447 fclose(term->tl_out_fd);
3448 term->tl_out_fd = NULL;
3449 }
3450#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003451
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003452 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003453 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003454 // Cannot open or close windows now. Can happen when
3455 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003456 term->tl_channel_recently_closed = TRUE;
3457 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003458 }
3459
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003460 if (term_after_channel_closed(term))
3461 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003462 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003463 }
3464
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003465 if (did_one)
3466 {
3467 redraw_statuslines();
3468
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003469 // Need to break out of vgetc().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003470 ins_char_typebuf(K_IGNORE);
3471 typebuf_was_filled = TRUE;
3472
3473 term = curbuf->b_term;
3474 if (term != NULL)
3475 {
3476 if (term->tl_job == ch->ch_job)
3477 maketitle();
3478 update_cursor(term, term->tl_cursor_visible);
3479 }
3480 }
3481}
3482
3483/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003484 * To be called after resetting updating_screen: handle any terminal where the
3485 * channel was closed.
3486 */
3487 void
3488term_check_channel_closed_recently()
3489{
3490 term_T *term;
3491 term_T *next_term;
3492
3493 for (term = first_term; term != NULL; term = next_term)
3494 {
3495 next_term = term->tl_next;
3496 if (term->tl_channel_recently_closed)
3497 {
3498 term->tl_channel_recently_closed = FALSE;
3499 if (term_after_channel_closed(term))
3500 // start over, the list may have changed
3501 next_term = first_term;
3502 }
3503 }
3504}
3505
3506/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003507 * Fill one screen line from a line of the terminal.
3508 * Advances "pos" to past the last column.
3509 */
3510 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003511term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003512 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003513 win_T *wp,
3514 VTermScreen *screen,
3515 VTermPos *pos,
3516 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003517{
3518 int off = screen_get_current_line_off();
3519
3520 for (pos->col = 0; pos->col < max_col; )
3521 {
3522 VTermScreenCell cell;
3523 int c;
3524
3525 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003526 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003527
3528 c = cell.chars[0];
3529 if (c == NUL)
3530 {
3531 ScreenLines[off] = ' ';
3532 if (enc_utf8)
3533 ScreenLinesUC[off] = NUL;
3534 }
3535 else
3536 {
3537 if (enc_utf8)
3538 {
3539 int i;
3540
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003541 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003542 for (i = 0; i < Screen_mco
3543 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3544 {
3545 ScreenLinesC[i][off] = cell.chars[i + 1];
3546 if (cell.chars[i + 1] == 0)
3547 break;
3548 }
3549 if (c >= 0x80 || (Screen_mco > 0
3550 && ScreenLinesC[0][off] != 0))
3551 {
3552 ScreenLines[off] = ' ';
3553 ScreenLinesUC[off] = c;
3554 }
3555 else
3556 {
3557 ScreenLines[off] = c;
3558 ScreenLinesUC[off] = NUL;
3559 }
3560 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003561#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003562 else if (has_mbyte && c >= 0x80)
3563 {
3564 char_u mb[MB_MAXBYTES+1];
3565 WCHAR wc = c;
3566
3567 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3568 (char*)mb, 2, 0, 0) > 1)
3569 {
3570 ScreenLines[off] = mb[0];
3571 ScreenLines[off + 1] = mb[1];
3572 cell.width = mb_ptr2cells(mb);
3573 }
3574 else
3575 ScreenLines[off] = c;
3576 }
3577#endif
3578 else
3579 ScreenLines[off] = c;
3580 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003581 ScreenAttrs[off] = cell2attr(term, wp, cell.attrs, cell.fg, cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003582
3583 ++pos->col;
3584 ++off;
3585 if (cell.width == 2)
3586 {
3587 if (enc_utf8)
3588 ScreenLinesUC[off] = NUL;
3589
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003590 // don't set the second byte to NUL for a DBCS encoding, it
3591 // has been set above
Bram Moolenaar13568252018-03-16 20:46:58 +01003592 if (enc_utf8 || !has_mbyte)
3593 ScreenLines[off] = NUL;
3594
3595 ++pos->col;
3596 ++off;
3597 }
3598 }
3599}
3600
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003601#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003602 static void
3603update_system_term(term_T *term)
3604{
3605 VTermPos pos;
3606 VTermScreen *screen;
3607
3608 if (term->tl_vterm == NULL)
3609 return;
3610 screen = vterm_obtain_screen(term->tl_vterm);
3611
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003612 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003613 while (term->tl_toprow > 0
3614 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3615 {
3616 int save_p_more = p_more;
3617
3618 p_more = FALSE;
3619 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003620 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003621 p_more = save_p_more;
3622 --term->tl_toprow;
3623 }
3624
3625 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3626 && pos.row < Rows; ++pos.row)
3627 {
3628 if (pos.row < term->tl_rows)
3629 {
3630 int max_col = MIN(Columns, term->tl_cols);
3631
Bram Moolenaar83d47902020-03-26 20:34:00 +01003632 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003633 }
3634 else
3635 pos.col = 0;
3636
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003637 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003638 }
3639
3640 term->tl_dirty_row_start = MAX_ROW;
3641 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003642}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003643#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003644
3645/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003646 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3647 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003648 * Terminal-Normal mode.
3649 */
3650 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003651term_do_update_window(win_T *wp)
3652{
3653 term_T *term = wp->w_buffer->b_term;
3654
3655 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3656}
3657
3658/*
3659 * Called to update a window that contains an active terminal.
3660 */
3661 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003662term_update_window(win_T *wp)
3663{
3664 term_T *term = wp->w_buffer->b_term;
3665 VTerm *vterm;
3666 VTermScreen *screen;
3667 VTermState *state;
3668 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003669 int rows, cols;
3670 int newrows, newcols;
3671 int minsize;
3672 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003673
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003674 vterm = term->tl_vterm;
3675 screen = vterm_obtain_screen(vterm);
3676 state = vterm_obtain_state(vterm);
3677
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003678 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3679 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003680 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003681 {
3682 term->tl_dirty_row_start = 0;
3683 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003684
3685 if (term->tl_postponed_scroll > 0
3686 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003687 // Scrolling is usually faster than redrawing, when there are only
3688 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003689 term_scroll_up(term, 0, term->tl_postponed_scroll);
3690 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003691 }
3692
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003693 /*
3694 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003695 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003696 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003697 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003698
Bram Moolenaar498c2562018-04-15 23:45:15 +02003699 newrows = 99999;
3700 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003701 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003702 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003703 // Always use curwin, it may be a popup window.
3704 win_T *wwp = twp == NULL ? curwin : twp;
3705
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003706 // When more than one window shows the same terminal, use the
3707 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003708 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003709 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003710 newrows = MIN(newrows, wwp->w_height);
3711 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003712 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003713 if (twp == NULL)
3714 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003715 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003716 if (newrows == 99999 || newcols == 99999)
3717 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003718 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3719 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3720
3721 if (term->tl_rows != newrows || term->tl_cols != newcols)
3722 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003723 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003724 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003725 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003726 newrows);
3727 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003728
3729 // Updating the terminal size will cause the snapshot to be cleared.
3730 // When not in terminal_loop() we need to restore it.
3731 if (term != in_terminal_loop)
3732 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003733 }
3734
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003735 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003736 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003737 position_cursor(wp, &pos, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003738
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003739 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3740 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003741 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003742 if (pos.row < term->tl_rows)
3743 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003744 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003745
Bram Moolenaar83d47902020-03-26 20:34:00 +01003746 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003747 }
3748 else
3749 pos.col = 0;
3750
Bram Moolenaarf118d482018-03-13 13:14:00 +01003751 screen_line(wp->w_winrow + pos.row
3752#ifdef FEAT_MENU
3753 + winbar_height(wp)
3754#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003755 , wp->w_wincol, pos.col, wp->w_width,
3756#ifdef FEAT_PROP_POPUP
3757 popup_is_popup(wp) ? SLF_POPUP :
3758#endif
3759 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003760 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003761 term->tl_dirty_row_start = MAX_ROW;
3762 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003763}
3764
3765/*
3766 * Return TRUE if "wp" is a terminal window where the job has finished.
3767 */
3768 int
3769term_is_finished(buf_T *buf)
3770{
3771 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3772}
3773
3774/*
3775 * Return TRUE if "wp" is a terminal window where the job has finished or we
3776 * are in Terminal-Normal mode, thus we show the buffer contents.
3777 */
3778 int
3779term_show_buffer(buf_T *buf)
3780{
3781 term_T *term = buf->b_term;
3782
3783 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3784}
3785
3786/*
3787 * The current buffer is going to be changed. If there is terminal
3788 * highlighting remove it now.
3789 */
3790 void
3791term_change_in_curbuf(void)
3792{
3793 term_T *term = curbuf->b_term;
3794
3795 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3796 {
3797 free_scrollback(term);
3798 redraw_buf_later(term->tl_buffer, NOT_VALID);
3799
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003800 // The buffer is now like a normal buffer, it cannot be easily
3801 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003802 set_string_option_direct((char_u *)"buftype", -1,
3803 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3804 }
3805}
3806
3807/*
3808 * Get the screen attribute for a position in the buffer.
3809 * Use a negative "col" to get the filler background color.
3810 */
3811 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003812term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003813{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003814 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003815 term_T *term = buf->b_term;
3816 sb_line_T *line;
3817 cellattr_T *cellattr;
3818
3819 if (lnum > term->tl_scrollback.ga_len)
3820 cellattr = &term->tl_default_color;
3821 else
3822 {
3823 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3824 if (col < 0 || col >= line->sb_cols)
3825 cellattr = &line->sb_fill_attr;
3826 else
3827 cellattr = line->sb_cells + col;
3828 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003829 return cell2attr(term, wp, cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003830}
3831
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003832/*
3833 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003834 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003835 */
3836 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003837cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003838{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003839 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3840 if (rgb->index == 0)
3841 rgb->type = VTERM_COLOR_RGB;
3842 else
3843 {
3844 rgb->type = VTERM_COLOR_INDEXED;
3845 --rgb->index;
3846 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003847}
3848
3849/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003850 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003851 */
3852 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003853init_default_colors(term_T *term, win_T *wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003854{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003855 VTermColor *fg, *bg;
3856 int fgval, bgval;
3857 int id;
3858
Bram Moolenaara80faa82020-04-12 19:37:17 +02003859 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003860 term->tl_default_color.width = 1;
3861 fg = &term->tl_default_color.fg;
3862 bg = &term->tl_default_color.bg;
3863
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003864 // Vterm uses a default black background. Set it to white when
3865 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003866 if (*p_bg == 'l')
3867 {
3868 fgval = 0;
3869 bgval = 255;
3870 }
3871 else
3872 {
3873 fgval = 255;
3874 bgval = 0;
3875 }
3876 fg->red = fg->green = fg->blue = fgval;
3877 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003878 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3879 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003880
Bram Moolenaar83d47902020-03-26 20:34:00 +01003881 // The 'wincolor' or the highlight group overrules the defaults.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003882 if (wp != NULL && *wp->w_p_wcr != NUL)
3883 id = syn_name2id(wp->w_p_wcr);
3884 else
Bram Moolenaar83d47902020-03-26 20:34:00 +01003885 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003886
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003887 // Use the actual color for the GUI and when 'termguicolors' is set.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003888#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3889 if (0
3890# ifdef FEAT_GUI
3891 || gui.in_use
3892# endif
3893# ifdef FEAT_TERMGUICOLORS
3894 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003895# ifdef FEAT_VTP
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003896 // Finally get INVALCOLOR on this execution path
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003897 || (!p_tgc && t_colors >= 256)
3898# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003899# endif
3900 )
3901 {
3902 guicolor_T fg_rgb = INVALCOLOR;
3903 guicolor_T bg_rgb = INVALCOLOR;
3904
3905 if (id != 0)
3906 syn_id2colors(id, &fg_rgb, &bg_rgb);
3907
3908# ifdef FEAT_GUI
3909 if (gui.in_use)
3910 {
3911 if (fg_rgb == INVALCOLOR)
3912 fg_rgb = gui.norm_pixel;
3913 if (bg_rgb == INVALCOLOR)
3914 bg_rgb = gui.back_pixel;
3915 }
3916# ifdef FEAT_TERMGUICOLORS
3917 else
3918# endif
3919# endif
3920# ifdef FEAT_TERMGUICOLORS
3921 {
3922 if (fg_rgb == INVALCOLOR)
3923 fg_rgb = cterm_normal_fg_gui_color;
3924 if (bg_rgb == INVALCOLOR)
3925 bg_rgb = cterm_normal_bg_gui_color;
3926 }
3927# endif
3928 if (fg_rgb != INVALCOLOR)
3929 {
3930 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3931
3932 fg->red = (unsigned)(rgb >> 16);
3933 fg->green = (unsigned)(rgb >> 8) & 255;
3934 fg->blue = (unsigned)rgb & 255;
3935 }
3936 if (bg_rgb != INVALCOLOR)
3937 {
3938 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
3939
3940 bg->red = (unsigned)(rgb >> 16);
3941 bg->green = (unsigned)(rgb >> 8) & 255;
3942 bg->blue = (unsigned)rgb & 255;
3943 }
3944 }
3945 else
3946#endif
3947 if (id != 0 && t_colors >= 16)
3948 {
Bram Moolenaar83d47902020-03-26 20:34:00 +01003949 int cterm_fg = get_default_cterm_fg(term);
3950 int cterm_bg = get_default_cterm_bg(term);
3951
3952 if (cterm_fg >= 0)
3953 cterm_color2vterm(cterm_fg, fg);
3954 if (cterm_bg >= 0)
3955 cterm_color2vterm(cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003956 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003957 else
3958 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003959#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003960 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003961#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003962
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003963 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003964 if (cterm_normal_fg_color > 0)
3965 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003966 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003967# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3968# ifdef VIMDLL
3969 if (!gui.in_use)
3970# endif
3971 {
3972 tmp = fg->red;
3973 fg->red = fg->blue;
3974 fg->blue = tmp;
3975 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003976# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003977 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02003978# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003979 else
3980 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02003981# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003982
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003983 if (cterm_normal_bg_color > 0)
3984 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003985 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003986# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3987# ifdef VIMDLL
3988 if (!gui.in_use)
3989# endif
3990 {
3991 tmp = fg->red;
3992 fg->red = fg->blue;
3993 fg->blue = tmp;
3994 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003995# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003996 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02003997# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003998 else
3999 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004000# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004001 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004002}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004003
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004004#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4005/*
4006 * Set the 16 ANSI colors from array of RGB values
4007 */
4008 static void
4009set_vterm_palette(VTerm *vterm, long_u *rgb)
4010{
4011 int index = 0;
4012 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004013
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004014 for (; index < 16; index++)
4015 {
4016 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004017
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004018 color.red = (unsigned)(rgb[index] >> 16);
4019 color.green = (unsigned)(rgb[index] >> 8) & 255;
4020 color.blue = (unsigned)rgb[index] & 255;
4021 vterm_state_set_palette_color(state, index, &color);
4022 }
4023}
4024
4025/*
4026 * Set the ANSI color palette from a list of colors
4027 */
4028 static int
4029set_ansi_colors_list(VTerm *vterm, list_T *list)
4030{
4031 int n = 0;
4032 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004033 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004034
Bram Moolenaarb0992022020-01-30 14:55:42 +01004035 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004036 {
4037 char_u *color_name;
4038 guicolor_T guicolor;
4039
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004040 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004041 if (color_name == NULL)
4042 return FAIL;
4043
4044 guicolor = GUI_GET_COLOR(color_name);
4045 if (guicolor == INVALCOLOR)
4046 return FAIL;
4047
4048 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4049 }
4050
4051 if (n != 16 || li != NULL)
4052 return FAIL;
4053
4054 set_vterm_palette(vterm, rgb);
4055
4056 return OK;
4057}
4058
4059/*
4060 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4061 */
4062 static void
4063init_vterm_ansi_colors(VTerm *vterm)
4064{
4065 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4066
4067 if (var != NULL
4068 && (var->di_tv.v_type != VAR_LIST
4069 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004070 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004071 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004072 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004073}
4074#endif
4075
Bram Moolenaar52acb112018-03-18 19:20:22 +01004076/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004077 * Handles a "drop" command from the job in the terminal.
4078 * "item" is the file name, "item->li_next" may have options.
4079 */
4080 static void
4081handle_drop_command(listitem_T *item)
4082{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004083 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004084 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004085 int bufnr;
4086 win_T *wp;
4087 tabpage_T *tp;
4088 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004089 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004090
4091 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4092 FOR_ALL_TAB_WINDOWS(tp, wp)
4093 {
4094 if (wp->w_buffer->b_fnum == bufnr)
4095 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004096 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004097 goto_tabpage_win(tp, wp);
4098 return;
4099 }
4100 }
4101
Bram Moolenaara80faa82020-04-12 19:37:17 +02004102 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004103
4104 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4105 && opt_item->li_tv.vval.v_dict != NULL)
4106 {
4107 dict_T *dict = opt_item->li_tv.vval.v_dict;
4108 char_u *p;
4109
Bram Moolenaar8f667172018-12-14 15:38:31 +01004110 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004111 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004112 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004113 if (p != NULL)
4114 {
4115 if (check_ff_value(p) == FAIL)
4116 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4117 else
4118 ea.force_ff = *p;
4119 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004120 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004121 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004122 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004123 if (p != NULL)
4124 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004125 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004126 if (ea.cmd != NULL)
4127 {
4128 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4129 ea.force_enc = 11;
4130 tofree = ea.cmd;
4131 }
4132 }
4133
Bram Moolenaar8f667172018-12-14 15:38:31 +01004134 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004135 if (p != NULL)
4136 get_bad_opt(p, &ea);
4137
4138 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4139 ea.force_bin = FORCE_BIN;
4140 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4141 ea.force_bin = FORCE_BIN;
4142 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4143 ea.force_bin = FORCE_NOBIN;
4144 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4145 ea.force_bin = FORCE_NOBIN;
4146 }
4147
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004148 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004149 if (ea.cmd == NULL)
4150 ea.cmd = (char_u *)"split";
4151 ea.arg = fname;
4152 ea.cmdidx = CMD_split;
4153 ex_splitview(&ea);
4154
4155 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004156}
4157
4158/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004159 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4160 */
4161 static int
4162is_permitted_term_api(char_u *func, char_u *pat)
4163{
4164 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4165}
4166
4167/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004168 * Handles a function call from the job running in a terminal.
4169 * "item" is the function name, "item->li_next" has the arguments.
4170 */
4171 static void
4172handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4173{
4174 char_u *func;
4175 typval_T argvars[2];
4176 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004177 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004178
4179 if (item->li_next == NULL)
4180 {
4181 ch_log(channel, "Missing function arguments for call");
4182 return;
4183 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004184 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004185
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004186 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004187 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004188 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004189 return;
4190 }
4191
4192 argvars[0].v_type = VAR_NUMBER;
4193 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4194 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004195 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004196 funcexe.firstline = 1L;
4197 funcexe.lastline = 1L;
4198 funcexe.evaluate = TRUE;
4199 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004200 {
4201 clear_tv(&rettv);
4202 ch_log(channel, "Function %s called", func);
4203 }
4204 else
4205 ch_log(channel, "Calling function %s failed", func);
4206}
4207
4208/*
4209 * Called by libvterm when it cannot recognize an OSC sequence.
4210 * We recognize a terminal API command.
4211 */
4212 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004213parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004214{
4215 term_T *term = (term_T *)user;
4216 js_read_T reader;
4217 typval_T tv;
4218 channel_T *channel = term->tl_job == NULL ? NULL
4219 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004220 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004221
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004222 // We recognize only OSC 5 1 ; {command}
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004223 if (command != 51)
4224 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004225
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004226 // Concatenate what was received until the final piece is found.
4227 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4228 {
4229 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004230 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004231 }
4232 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
4233 gap->ga_len += frag.len;
4234 if (!frag.final)
4235 return 1;
4236
4237 ((char *)gap->ga_data)[gap->ga_len] = 0;
4238 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004239 reader.js_fill = NULL;
4240 reader.js_used = 0;
4241 if (json_decode(&reader, &tv, 0) == OK
4242 && tv.v_type == VAR_LIST
4243 && tv.vval.v_list != NULL)
4244 {
4245 listitem_T *item = tv.vval.v_list->lv_first;
4246
4247 if (item == NULL)
4248 ch_log(channel, "Missing command");
4249 else
4250 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004251 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004252
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004253 // Make sure an invoked command doesn't delete the buffer (and the
4254 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004255 ++term->tl_buffer->b_locked;
4256
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004257 item = item->li_next;
4258 if (item == NULL)
4259 ch_log(channel, "Missing argument for %s", cmd);
4260 else if (STRCMP(cmd, "drop") == 0)
4261 handle_drop_command(item);
4262 else if (STRCMP(cmd, "call") == 0)
4263 handle_call_command(term, channel, item);
4264 else
4265 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004266 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004267 }
4268 }
4269 else
4270 ch_log(channel, "Invalid JSON received");
4271
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004272 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004273 clear_tv(&tv);
4274 return 1;
4275}
4276
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004277/*
4278 * Called by libvterm when it cannot recognize a CSI sequence.
4279 * We recognize the window position report.
4280 */
4281 static int
4282parse_csi(
4283 const char *leader UNUSED,
4284 const long args[],
4285 int argcount,
4286 const char *intermed UNUSED,
4287 char command,
4288 void *user)
4289{
4290 term_T *term = (term_T *)user;
4291 char buf[100];
4292 int len;
4293 int x = 0;
4294 int y = 0;
4295 win_T *wp;
4296
4297 // We recognize only CSI 13 t
4298 if (command != 't' || argcount != 1 || args[0] != 13)
4299 return 0; // not handled
4300
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004301 // When getting the window position is not possible or it fails it results
4302 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004303#if defined(FEAT_GUI) \
4304 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4305 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004306 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004307#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004308
4309 FOR_ALL_WINDOWS(wp)
4310 if (wp->w_buffer == term->tl_buffer)
4311 break;
4312 if (wp != NULL)
4313 {
4314#ifdef FEAT_GUI
4315 if (gui.in_use)
4316 {
4317 x += wp->w_wincol * gui.char_width;
4318 y += W_WINROW(wp) * gui.char_height;
4319 }
4320 else
4321#endif
4322 {
4323 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004324 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004325 x += wp->w_wincol * 7;
4326 y += W_WINROW(wp) * 10;
4327 }
4328 }
4329
4330 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4331 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4332 (char_u *)buf, len, NULL);
4333 return 1;
4334}
4335
Bram Moolenaard8637282020-05-20 18:41:41 +02004336static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004337 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004338 parse_csi, // csi
4339 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004340 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004341};
4342
4343/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004344 * Use Vim's allocation functions for vterm so profiling works.
4345 */
4346 static void *
4347vterm_malloc(size_t size, void *data UNUSED)
4348{
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004349 return alloc_clear(size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004350}
4351
4352 static void
4353vterm_memfree(void *ptr, void *data UNUSED)
4354{
4355 vim_free(ptr);
4356}
4357
4358static VTermAllocatorFunctions vterm_allocator = {
4359 &vterm_malloc,
4360 &vterm_memfree
4361};
4362
4363/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004364 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004365 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004366 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004367 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004368create_vterm(term_T *term, int rows, int cols)
4369{
4370 VTerm *vterm;
4371 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004372 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004373 VTermValue value;
4374
Bram Moolenaar756ef112018-04-10 12:04:27 +02004375 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004376 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004377 if (vterm == NULL)
4378 return FAIL;
4379
4380 // Allocate screen and state here, so we can bail out if that fails.
4381 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004382 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004383 if (state == NULL || screen == NULL)
4384 {
4385 vterm_free(vterm);
4386 return FAIL;
4387 }
4388
Bram Moolenaar52acb112018-03-18 19:20:22 +01004389 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004390 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004391 vterm_set_utf8(vterm, 1);
4392
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004393 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004394
4395 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004396 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004397 &term->tl_default_color.fg,
4398 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004399
Bram Moolenaar9e587872019-05-13 20:27:23 +02004400 if (t_colors < 16)
4401 // Less than 16 colors: assume that bold means using a bright color for
4402 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004403 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4404
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004405 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004406 vterm_screen_reset(screen, 1 /* hard */);
4407
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004408 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004409 vterm_screen_enable_altscreen(screen, 1);
4410
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004411 // For unix do not use a blinking cursor. In an xterm this causes the
4412 // cursor to blink if it's blinking in the xterm.
4413 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004414#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004415 if (GetCaretBlinkTime() == INFINITE)
4416 value.boolean = 0;
4417 else
4418 value.boolean = 1;
4419#else
4420 value.boolean = 0;
4421#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004422 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004423 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004424
4425 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004426}
4427
4428/*
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004429 * Called when 'wincolor' was set.
4430 */
4431 void
4432term_update_colors(void)
4433{
4434 term_T *term = curwin->w_buffer->b_term;
4435
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01004436 if (term->tl_vterm == NULL)
4437 return;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004438 init_default_colors(term, curwin);
4439 vterm_state_set_default_colors(
4440 vterm_obtain_state(term->tl_vterm),
4441 &term->tl_default_color.fg,
4442 &term->tl_default_color.bg);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01004443
4444 redraw_later(NOT_VALID);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004445}
4446
4447/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004448 * Return the text to show for the buffer name and status.
4449 */
4450 char_u *
4451term_get_status_text(term_T *term)
4452{
4453 if (term->tl_status_text == NULL)
4454 {
4455 char_u *txt;
4456 size_t len;
4457
4458 if (term->tl_normal_mode)
4459 {
4460 if (term_job_running(term))
4461 txt = (char_u *)_("Terminal");
4462 else
4463 txt = (char_u *)_("Terminal-finished");
4464 }
4465 else if (term->tl_title != NULL)
4466 txt = term->tl_title;
4467 else if (term_none_open(term))
4468 txt = (char_u *)_("active");
4469 else if (term_job_running(term))
4470 txt = (char_u *)_("running");
4471 else
4472 txt = (char_u *)_("finished");
4473 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004474 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004475 if (term->tl_status_text != NULL)
4476 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
4477 term->tl_buffer->b_fname, txt);
4478 }
4479 return term->tl_status_text;
4480}
4481
4482/*
4483 * Mark references in jobs of terminals.
4484 */
4485 int
4486set_ref_in_term(int copyID)
4487{
4488 int abort = FALSE;
4489 term_T *term;
4490 typval_T tv;
4491
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004492 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004493 if (term->tl_job != NULL)
4494 {
4495 tv.v_type = VAR_JOB;
4496 tv.vval.v_job = term->tl_job;
4497 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4498 }
4499 return abort;
4500}
4501
4502/*
4503 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004504 * Returns NULL when the buffer is not for a terminal window and logs a message
4505 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004506 */
4507 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004508term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004509{
4510 buf_T *buf;
4511
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004512 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004513 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004514 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004515 --emsg_off;
4516 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004517 {
4518 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004519 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004520 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004521 return buf;
4522}
4523
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004524 static void
4525clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004526{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004527 CLEAR_FIELD(*cell);
4528 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4529 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004530}
4531
4532 static void
4533dump_term_color(FILE *fd, VTermColor *color)
4534{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004535 int index;
4536
4537 if (VTERM_COLOR_IS_INDEXED(color))
4538 index = color->index + 1;
4539 else if (color->type == 0)
4540 // use RGB values
4541 index = 255;
4542 else
4543 // default color
4544 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004545 fprintf(fd, "%02x%02x%02x%d",
4546 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004547 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004548}
4549
4550/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004551 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004552 *
4553 * Each screen cell in full is:
4554 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4555 * {characters} is a space for an empty cell
4556 * For a double-width character "+" is changed to "*" and the next cell is
4557 * skipped.
4558 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4559 * when "&" use the same as the previous cell.
4560 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4561 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4562 * {color-idx} is a number from 0 to 255
4563 *
4564 * Screen cell with same width, attributes and color as the previous one:
4565 * |{characters}
4566 *
4567 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4568 *
4569 * Repeating the previous screen cell:
4570 * @{count}
4571 */
4572 void
4573f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4574{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004575 buf_T *buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004576 term_T *term;
4577 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004578 int max_height = 0;
4579 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004580 stat_T st;
4581 FILE *fd;
4582 VTermPos pos;
4583 VTermScreen *screen;
4584 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004585 VTermState *state;
4586 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004587
4588 if (check_restricted() || check_secure())
4589 return;
4590 if (buf == NULL)
4591 return;
4592 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004593 if (term->tl_vterm == NULL)
4594 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004595 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004596 return;
4597 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004598
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004599 if (argvars[2].v_type != VAR_UNKNOWN)
4600 {
4601 dict_T *d;
4602
4603 if (argvars[2].v_type != VAR_DICT)
4604 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004605 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004606 return;
4607 }
4608 d = argvars[2].vval.v_dict;
4609 if (d != NULL)
4610 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004611 max_height = dict_get_number(d, (char_u *)"rows");
4612 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004613 }
4614 }
4615
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004616 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004617 if (fname == NULL)
4618 return;
4619 if (mch_stat((char *)fname, &st) >= 0)
4620 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004621 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004622 return;
4623 }
4624
Bram Moolenaard96ff162018-02-18 22:13:29 +01004625 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004627 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004628 return;
4629 }
4630
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004631 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004632
4633 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004634 state = vterm_obtain_state(term->tl_vterm);
4635 vterm_state_get_cursorpos(state, &cursor_pos);
4636
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004637 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4638 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004639 {
4640 int repeat = 0;
4641
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004642 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4643 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004644 {
4645 VTermScreenCell cell;
4646 int same_attr;
4647 int same_chars = TRUE;
4648 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004649 int is_cursor_pos = (pos.col == cursor_pos.col
4650 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004651
4652 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004653 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004654
4655 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4656 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004657 int c = cell.chars[i];
4658 int pc = prev_cell.chars[i];
4659
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004660 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004661 if (i == 0)
4662 {
4663 c = (c == NUL) ? ' ' : c;
4664 pc = (pc == NUL) ? ' ' : pc;
4665 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004666 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004667 same_chars = FALSE;
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004668 if (c == NUL || pc == NUL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004669 break;
4670 }
4671 same_attr = vtermAttr2hl(cell.attrs)
4672 == vtermAttr2hl(prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004673 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4674 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004675 if (same_chars && cell.width == prev_cell.width && same_attr
4676 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004677 {
4678 ++repeat;
4679 }
4680 else
4681 {
4682 if (repeat > 0)
4683 {
4684 fprintf(fd, "@%d", repeat);
4685 repeat = 0;
4686 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004687 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004688
4689 if (cell.chars[0] == NUL)
4690 fputs(" ", fd);
4691 else
4692 {
4693 char_u charbuf[10];
4694 int len;
4695
4696 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4697 && cell.chars[i] != NUL; ++i)
4698 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004699 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004700 fwrite(charbuf, len, 1, fd);
4701 }
4702 }
4703
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004704 // When only the characters differ we don't write anything, the
4705 // following "|", "@" or NL will indicate using the same
4706 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004707 if (cell.width != prev_cell.width || !same_attr)
4708 {
4709 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004710 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004711 else
4712 fputs("+", fd);
4713
4714 if (same_attr)
4715 {
4716 fputs("&", fd);
4717 }
4718 else
4719 {
4720 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004721 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004722 fputs("&", fd);
4723 else
4724 {
4725 fputs("#", fd);
4726 dump_term_color(fd, &cell.fg);
4727 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004728 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004729 fputs("&", fd);
4730 else
4731 {
4732 fputs("#", fd);
4733 dump_term_color(fd, &cell.bg);
4734 }
4735 }
4736 }
4737
4738 prev_cell = cell;
4739 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004740
4741 if (cell.width == 2)
4742 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004743 }
4744 if (repeat > 0)
4745 fprintf(fd, "@%d", repeat);
4746 fputs("\n", fd);
4747 }
4748
4749 fclose(fd);
4750}
4751
4752/*
4753 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4754 */
4755 static void
4756dump_is_corrupt(garray_T *gap)
4757{
4758 ga_concat(gap, (char_u *)"CORRUPT");
4759}
4760
4761 static void
4762append_cell(garray_T *gap, cellattr_T *cell)
4763{
4764 if (ga_grow(gap, 1) == OK)
4765 {
4766 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4767 ++gap->ga_len;
4768 }
4769}
4770
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004771 static void
4772clear_cellattr(cellattr_T *cell)
4773{
4774 CLEAR_FIELD(*cell);
4775 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4776 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4777}
4778
Bram Moolenaard96ff162018-02-18 22:13:29 +01004779/*
4780 * Read the dump file from "fd" and append lines to the current buffer.
4781 * Return the cell width of the longest line.
4782 */
4783 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004784read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004785{
4786 int c;
4787 garray_T ga_text;
4788 garray_T ga_cell;
4789 char_u *prev_char = NULL;
4790 int attr = 0;
4791 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004792 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004793 term_T *term = curbuf->b_term;
4794 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004795 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004796
4797 ga_init2(&ga_text, 1, 90);
4798 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004799 clear_cellattr(&cell);
4800 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004801 cursor_pos->row = -1;
4802 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004803
4804 c = fgetc(fd);
4805 for (;;)
4806 {
4807 if (c == EOF)
4808 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004809 if (c == '\r')
4810 {
4811 // DOS line endings? Ignore.
4812 c = fgetc(fd);
4813 }
4814 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004815 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004816 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004817 if (ga_text.ga_data == NULL)
4818 dump_is_corrupt(&ga_text);
4819 if (ga_grow(&term->tl_scrollback, 1) == OK)
4820 {
4821 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
4822 + term->tl_scrollback.ga_len;
4823
4824 if (max_cells < ga_cell.ga_len)
4825 max_cells = ga_cell.ga_len;
4826 line->sb_cols = ga_cell.ga_len;
4827 line->sb_cells = ga_cell.ga_data;
4828 line->sb_fill_attr = term->tl_default_color;
4829 ++term->tl_scrollback.ga_len;
4830 ga_init(&ga_cell);
4831
4832 ga_append(&ga_text, NUL);
4833 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4834 ga_text.ga_len, FALSE);
4835 }
4836 else
4837 ga_clear(&ga_cell);
4838 ga_text.ga_len = 0;
4839
4840 c = fgetc(fd);
4841 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004842 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004843 {
4844 int prev_len = ga_text.ga_len;
4845
Bram Moolenaar9271d052018-02-25 21:39:46 +01004846 if (c == '>')
4847 {
4848 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004849 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01004850 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
4851 cursor_pos->col = ga_cell.ga_len;
4852 }
4853
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004854 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01004855 c = fgetc(fd);
4856 if (c != EOF)
4857 ga_append(&ga_text, c);
4858 for (;;)
4859 {
4860 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004861 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01004862 || c == EOF || c == '\n')
4863 break;
4864 ga_append(&ga_text, c);
4865 }
4866
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004867 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01004868 vim_free(prev_char);
4869 if (ga_text.ga_data != NULL)
4870 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
4871 ga_text.ga_len - prev_len);
4872
Bram Moolenaar9271d052018-02-25 21:39:46 +01004873 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004874 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004875 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01004876 }
4877 else if (c == '+' || c == '*')
4878 {
4879 int is_bg;
4880
4881 cell.width = c == '+' ? 1 : 2;
4882
4883 c = fgetc(fd);
4884 if (c == '&')
4885 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004886 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01004887 c = fgetc(fd);
4888 }
4889 else if (isdigit(c))
4890 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004891 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01004892 attr = 0;
4893 while (isdigit(c))
4894 {
4895 attr = attr * 10 + (c - '0');
4896 c = fgetc(fd);
4897 }
4898 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004899
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004900 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004901 for (is_bg = 0; is_bg <= 1; ++is_bg)
4902 {
4903 if (c == '&')
4904 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004905 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004906 c = fgetc(fd);
4907 }
4908 else if (c == '#')
4909 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004910 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004911
4912 c = fgetc(fd);
4913 red = hex2nr(c);
4914 c = fgetc(fd);
4915 red = (red << 4) + hex2nr(c);
4916 c = fgetc(fd);
4917 green = hex2nr(c);
4918 c = fgetc(fd);
4919 green = (green << 4) + hex2nr(c);
4920 c = fgetc(fd);
4921 blue = hex2nr(c);
4922 c = fgetc(fd);
4923 blue = (blue << 4) + hex2nr(c);
4924 c = fgetc(fd);
4925 if (!isdigit(c))
4926 dump_is_corrupt(&ga_text);
4927 while (isdigit(c))
4928 {
4929 index = index * 10 + (c - '0');
4930 c = fgetc(fd);
4931 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004932 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004933 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004934 type = VTERM_COLOR_RGB;
4935 if (index == 0)
4936 {
4937 if (is_bg)
4938 type |= VTERM_COLOR_DEFAULT_BG;
4939 else
4940 type |= VTERM_COLOR_DEFAULT_FG;
4941 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004942 }
4943 else
4944 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004945 type = VTERM_COLOR_INDEXED;
4946 index -= 1;
4947 }
4948 if (is_bg)
4949 {
4950 cell.bg.type = type;
4951 cell.bg.red = red;
4952 cell.bg.green = green;
4953 cell.bg.blue = blue;
4954 cell.bg.index = index;
4955 }
4956 else
4957 {
4958 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004959 cell.fg.red = red;
4960 cell.fg.green = green;
4961 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004962 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004963 }
4964 }
4965 else
4966 dump_is_corrupt(&ga_text);
4967 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004968 }
4969 else
4970 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004971 }
4972 else
4973 dump_is_corrupt(&ga_text);
4974
4975 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004976 if (cell.width == 2)
4977 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004978 }
4979 else if (c == '@')
4980 {
4981 if (prev_char == NULL)
4982 dump_is_corrupt(&ga_text);
4983 else
4984 {
4985 int count = 0;
4986
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004987 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01004988 for (;;)
4989 {
4990 c = fgetc(fd);
4991 if (!isdigit(c))
4992 break;
4993 count = count * 10 + (c - '0');
4994 }
4995
4996 while (count-- > 0)
4997 {
4998 ga_concat(&ga_text, prev_char);
4999 append_cell(&ga_cell, &cell);
5000 }
5001 }
5002 }
5003 else
5004 {
5005 dump_is_corrupt(&ga_text);
5006 c = fgetc(fd);
5007 }
5008 }
5009
5010 if (ga_text.ga_len > 0)
5011 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005012 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005013 dump_is_corrupt(&ga_text);
5014 ga_append(&ga_text, NUL);
5015 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5016 ga_text.ga_len, FALSE);
5017 }
5018
5019 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005020 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005021 vim_free(prev_char);
5022
5023 return max_cells;
5024}
5025
5026/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005027 * Return an allocated string with at least "text_width" "=" characters and
5028 * "fname" inserted in the middle.
5029 */
5030 static char_u *
5031get_separator(int text_width, char_u *fname)
5032{
5033 int width = MAX(text_width, curwin->w_width);
5034 char_u *textline;
5035 int fname_size;
5036 char_u *p = fname;
5037 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005038 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005039
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005040 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005041 if (textline == NULL)
5042 return NULL;
5043
5044 fname_size = vim_strsize(fname);
5045 if (fname_size < width - 8)
5046 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005047 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005048 width = MAX(text_width, fname_size + 8);
5049 }
5050 else if (fname_size > width - 8)
5051 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005052 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005053 p = gettail(fname);
5054 fname_size = vim_strsize(p);
5055 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005056 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005057 while (fname_size > width - 8)
5058 {
5059 p += (*mb_ptr2len)(p);
5060 fname_size = vim_strsize(p);
5061 }
5062
5063 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5064 textline[i] = '=';
5065 textline[i++] = ' ';
5066
5067 STRCPY(textline + i, p);
5068 off = STRLEN(textline);
5069 textline[off] = ' ';
5070 for (i = 1; i < (width - fname_size) / 2; ++i)
5071 textline[off + i] = '=';
5072 textline[off + i] = NUL;
5073
5074 return textline;
5075}
5076
5077/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005078 * Common for "term_dumpdiff()" and "term_dumpload()".
5079 */
5080 static void
5081term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5082{
5083 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005084 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005085 char_u buf1[NUMBUFLEN];
5086 char_u buf2[NUMBUFLEN];
5087 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005088 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005089 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005090 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005091 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005092 char_u *textline = NULL;
5093
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005094 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005095 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005096 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005097 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005098 if (fname1 == NULL || (do_diff && fname2 == NULL))
5099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005100 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005101 return;
5102 }
5103 fd1 = mch_fopen((char *)fname1, READBIN);
5104 if (fd1 == NULL)
5105 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005106 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005107 return;
5108 }
5109 if (do_diff)
5110 {
5111 fd2 = mch_fopen((char *)fname2, READBIN);
5112 if (fd2 == NULL)
5113 {
5114 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005115 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005116 return;
5117 }
5118 }
5119
5120 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005121 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5122 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5123 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5124 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5125 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005126
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005127 if (opt.jo_term_name == NULL)
5128 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005129 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005130
Bram Moolenaar51e14382019-05-25 20:21:28 +02005131 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005132 if (fname_tofree != NULL)
5133 {
5134 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5135 opt.jo_term_name = fname_tofree;
5136 }
5137 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005138
Bram Moolenaar87abab92019-06-03 21:14:59 +02005139 if (opt.jo_bufnr_buf != NULL)
5140 {
5141 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5142
5143 // With "bufnr" argument: enter the window with this buffer and make it
5144 // empty.
5145 if (wp == NULL)
5146 semsg(_(e_invarg2), "bufnr");
5147 else
5148 {
5149 buf = curbuf;
5150 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
5151 ml_delete((linenr_T)1, FALSE);
Bram Moolenaar86173482019-10-01 17:02:16 +02005152 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005153 redraw_later(NOT_VALID);
5154 }
5155 }
5156 else
5157 // Create a new terminal window.
5158 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5159
Bram Moolenaard96ff162018-02-18 22:13:29 +01005160 if (buf != NULL && buf->b_term != NULL)
5161 {
5162 int i;
5163 linenr_T bot_lnum;
5164 linenr_T lnum;
5165 term_T *term = buf->b_term;
5166 int width;
5167 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005168 VTermPos cursor_pos1;
5169 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005170
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005171 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005172
Bram Moolenaard96ff162018-02-18 22:13:29 +01005173 rettv->vval.v_number = buf->b_fnum;
5174
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005175 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005176 width = read_dump_file(fd1, &cursor_pos1);
5177
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005178 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005179 if (cursor_pos1.row >= 0)
5180 {
5181 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5182 coladvance(cursor_pos1.col);
5183 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005184
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005185 // Delete the empty line that was in the empty buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005186 ml_delete(1, FALSE);
5187
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005188 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005189 if (!do_diff)
5190 goto theend;
5191
5192 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5193
Bram Moolenaar4a696342018-04-05 18:45:26 +02005194 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005195 if (textline == NULL)
5196 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005197 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5198 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5199 vim_free(textline);
5200
5201 textline = get_separator(width, fname2);
5202 if (textline == NULL)
5203 goto theend;
5204 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5205 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005206 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005207
5208 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005209 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005210 if (width2 > width)
5211 {
5212 vim_free(textline);
5213 textline = alloc(width2 + 1);
5214 if (textline == NULL)
5215 goto theend;
5216 width = width2;
5217 textline[width] = NUL;
5218 }
5219 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5220
5221 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5222 {
5223 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5224 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005225 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005226 for (i = 0; i < width; ++i)
5227 textline[i] = '-';
5228 }
5229 else
5230 {
5231 char_u *line1;
5232 char_u *line2;
5233 char_u *p1;
5234 char_u *p2;
5235 int col;
5236 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5237 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5238 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5239 ->sb_cells;
5240
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005241 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005242 line1 = vim_strsave(ml_get(lnum));
5243 if (line1 == NULL)
5244 break;
5245 p1 = line1;
5246
5247 line2 = ml_get(lnum + bot_lnum);
5248 p2 = line2;
5249 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5250 {
5251 int len1 = utfc_ptr2len(p1);
5252 int len2 = utfc_ptr2len(p2);
5253
5254 textline[col] = ' ';
5255 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005256 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005257 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005258 else if (lnum == cursor_pos1.row + 1
5259 && col == cursor_pos1.col
5260 && (cursor_pos1.row != cursor_pos2.row
5261 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005262 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005263 textline[col] = '>';
5264 else if (lnum == cursor_pos2.row + 1
5265 && col == cursor_pos2.col
5266 && (cursor_pos1.row != cursor_pos2.row
5267 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005268 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005269 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005270 else if (cellattr1 != NULL && cellattr2 != NULL)
5271 {
5272 if ((cellattr1 + col)->width
5273 != (cellattr2 + col)->width)
5274 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005275 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005276 &(cellattr2 + col)->fg))
5277 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005278 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005279 &(cellattr2 + col)->bg))
5280 textline[col] = 'b';
5281 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5282 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5283 textline[col] = 'a';
5284 }
5285 p1 += len1;
5286 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005287 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005288 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005289
5290 while (col < width)
5291 {
5292 if (*p1 == NUL && *p2 == NUL)
5293 textline[col] = '?';
5294 else if (*p1 == NUL)
5295 {
5296 textline[col] = '+';
5297 p2 += utfc_ptr2len(p2);
5298 }
5299 else
5300 {
5301 textline[col] = '-';
5302 p1 += utfc_ptr2len(p1);
5303 }
5304 ++col;
5305 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005306
5307 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005308 }
5309 if (add_empty_scrollback(term, &term->tl_default_color,
5310 term->tl_top_diff_rows) == OK)
5311 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5312 ++bot_lnum;
5313 }
5314
5315 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5316 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005317 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005318 for (i = 0; i < width; ++i)
5319 textline[i] = '+';
5320 if (add_empty_scrollback(term, &term->tl_default_color,
5321 term->tl_top_diff_rows) == OK)
5322 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5323 ++lnum;
5324 ++bot_lnum;
5325 }
5326
5327 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005328
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005329 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005330 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005331 }
5332
5333theend:
5334 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005335 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005336 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005337 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005338 fclose(fd2);
5339}
5340
5341/*
5342 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5343 * bottom files.
5344 * Return FAIL when this is not possible.
5345 */
5346 int
5347term_swap_diff()
5348{
5349 term_T *term = curbuf->b_term;
5350 linenr_T line_count;
5351 linenr_T top_rows;
5352 linenr_T bot_rows;
5353 linenr_T bot_start;
5354 linenr_T lnum;
5355 char_u *p;
5356 sb_line_T *sb_line;
5357
5358 if (term == NULL
5359 || !term_is_finished(curbuf)
5360 || term->tl_top_diff_rows == 0
5361 || term->tl_scrollback.ga_len == 0)
5362 return FAIL;
5363
5364 line_count = curbuf->b_ml.ml_line_count;
5365 top_rows = term->tl_top_diff_rows;
5366 bot_rows = term->tl_bot_diff_rows;
5367 bot_start = line_count - bot_rows;
5368 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5369
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005370 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005371 for (lnum = 1; lnum <= top_rows; ++lnum)
5372 {
5373 p = vim_strsave(ml_get(1));
5374 if (p == NULL)
5375 return OK;
5376 ml_append(bot_start, p, 0, FALSE);
5377 ml_delete(1, FALSE);
5378 vim_free(p);
5379 }
5380
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005381 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005382 for (lnum = 1; lnum <= bot_rows; ++lnum)
5383 {
5384 p = vim_strsave(ml_get(bot_start + lnum));
5385 if (p == NULL)
5386 return OK;
5387 ml_delete(bot_start + lnum, FALSE);
5388 ml_append(lnum - 1, p, 0, FALSE);
5389 vim_free(p);
5390 }
5391
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005392 // move top title to bottom
5393 p = vim_strsave(ml_get(bot_rows + 1));
5394 if (p == NULL)
5395 return OK;
5396 ml_append(line_count - top_rows - 1, p, 0, FALSE);
5397 ml_delete(bot_rows + 1, FALSE);
5398 vim_free(p);
5399
5400 // move bottom title to top
5401 p = vim_strsave(ml_get(line_count - top_rows));
5402 if (p == NULL)
5403 return OK;
5404 ml_delete(line_count - top_rows, FALSE);
5405 ml_append(bot_rows, p, 0, FALSE);
5406 vim_free(p);
5407
Bram Moolenaard96ff162018-02-18 22:13:29 +01005408 if (top_rows == bot_rows)
5409 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005410 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005411 for (lnum = 0; lnum < top_rows; ++lnum)
5412 {
5413 sb_line_T temp;
5414
5415 temp = *(sb_line + lnum);
5416 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5417 *(sb_line + bot_start + lnum) = temp;
5418 }
5419 }
5420 else
5421 {
5422 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005423 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005424
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005425 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005426 if (temp != NULL)
5427 {
5428 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5429 mch_memmove(term->tl_scrollback.ga_data,
5430 temp + bot_start,
5431 sizeof(sb_line_T) * bot_rows);
5432 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5433 temp + top_rows,
5434 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5435 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5436 + line_count - top_rows,
5437 temp,
5438 sizeof(sb_line_T) * top_rows);
5439 vim_free(temp);
5440 }
5441 }
5442
5443 term->tl_top_diff_rows = bot_rows;
5444 term->tl_bot_diff_rows = top_rows;
5445
5446 update_screen(NOT_VALID);
5447 return OK;
5448}
5449
5450/*
5451 * "term_dumpdiff(filename, filename, options)" function
5452 */
5453 void
5454f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5455{
5456 term_load_dump(argvars, rettv, TRUE);
5457}
5458
5459/*
5460 * "term_dumpload(filename, options)" function
5461 */
5462 void
5463f_term_dumpload(typval_T *argvars, typval_T *rettv)
5464{
5465 term_load_dump(argvars, rettv, FALSE);
5466}
5467
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005468/*
5469 * "term_getaltscreen(buf)" function
5470 */
5471 void
5472f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5473{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005474 buf_T *buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005475
5476 if (buf == NULL)
5477 return;
5478 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5479}
5480
5481/*
5482 * "term_getattr(attr, name)" function
5483 */
5484 void
5485f_term_getattr(typval_T *argvars, typval_T *rettv)
5486{
5487 int attr;
5488 size_t i;
5489 char_u *name;
5490
5491 static struct {
5492 char *name;
5493 int attr;
5494 } attrs[] = {
5495 {"bold", HL_BOLD},
5496 {"italic", HL_ITALIC},
5497 {"underline", HL_UNDERLINE},
5498 {"strike", HL_STRIKETHROUGH},
5499 {"reverse", HL_INVERSE},
5500 };
5501
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005502 attr = tv_get_number(&argvars[0]);
5503 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005504 if (name == NULL)
5505 return;
5506
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005507 if (attr > HL_ALL)
5508 attr = syn_attr2attr(attr);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005509 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
5510 if (STRCMP(name, attrs[i].name) == 0)
5511 {
5512 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5513 break;
5514 }
5515}
5516
5517/*
5518 * "term_getcursor(buf)" function
5519 */
5520 void
5521f_term_getcursor(typval_T *argvars, typval_T *rettv)
5522{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005523 buf_T *buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005524 term_T *term;
5525 list_T *l;
5526 dict_T *d;
5527
5528 if (rettv_list_alloc(rettv) == FAIL)
5529 return;
5530 if (buf == NULL)
5531 return;
5532 term = buf->b_term;
5533
5534 l = rettv->vval.v_list;
5535 list_append_number(l, term->tl_cursor_pos.row + 1);
5536 list_append_number(l, term->tl_cursor_pos.col + 1);
5537
5538 d = dict_alloc();
5539 if (d != NULL)
5540 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005541 dict_add_number(d, "visible", term->tl_cursor_visible);
5542 dict_add_number(d, "blink", blink_state_is_inverted()
5543 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5544 dict_add_number(d, "shape", term->tl_cursor_shape);
5545 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005546 list_append_dict(l, d);
5547 }
5548}
5549
5550/*
5551 * "term_getjob(buf)" function
5552 */
5553 void
5554f_term_getjob(typval_T *argvars, typval_T *rettv)
5555{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005556 buf_T *buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005557
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005558 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005559 {
5560 rettv->v_type = VAR_SPECIAL;
5561 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005562 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005563 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005564
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005565 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005566 rettv->vval.v_job = buf->b_term->tl_job;
5567 if (rettv->vval.v_job != NULL)
5568 ++rettv->vval.v_job->jv_refcount;
5569}
5570
5571 static int
5572get_row_number(typval_T *tv, term_T *term)
5573{
5574 if (tv->v_type == VAR_STRING
5575 && tv->vval.v_string != NULL
5576 && STRCMP(tv->vval.v_string, ".") == 0)
5577 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005578 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005579}
5580
5581/*
5582 * "term_getline(buf, row)" function
5583 */
5584 void
5585f_term_getline(typval_T *argvars, typval_T *rettv)
5586{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005587 buf_T *buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005588 term_T *term;
5589 int row;
5590
5591 rettv->v_type = VAR_STRING;
5592 if (buf == NULL)
5593 return;
5594 term = buf->b_term;
5595 row = get_row_number(&argvars[1], term);
5596
5597 if (term->tl_vterm == NULL)
5598 {
5599 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5600
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005601 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005602 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5603 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5604 }
5605 else
5606 {
5607 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5608 VTermRect rect;
5609 int len;
5610 char_u *p;
5611
5612 if (row < 0 || row >= term->tl_rows)
5613 return;
5614 len = term->tl_cols * MB_MAXBYTES + 1;
5615 p = alloc(len);
5616 if (p == NULL)
5617 return;
5618 rettv->vval.v_string = p;
5619
5620 rect.start_col = 0;
5621 rect.end_col = term->tl_cols;
5622 rect.start_row = row;
5623 rect.end_row = row + 1;
5624 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5625 }
5626}
5627
5628/*
5629 * "term_getscrolled(buf)" function
5630 */
5631 void
5632f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5633{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005634 buf_T *buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005635
5636 if (buf == NULL)
5637 return;
5638 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5639}
5640
5641/*
5642 * "term_getsize(buf)" function
5643 */
5644 void
5645f_term_getsize(typval_T *argvars, typval_T *rettv)
5646{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005647 buf_T *buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005648 list_T *l;
5649
5650 if (rettv_list_alloc(rettv) == FAIL)
5651 return;
5652 if (buf == NULL)
5653 return;
5654
5655 l = rettv->vval.v_list;
5656 list_append_number(l, buf->b_term->tl_rows);
5657 list_append_number(l, buf->b_term->tl_cols);
5658}
5659
5660/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005661 * "term_setsize(buf, rows, cols)" function
5662 */
5663 void
5664f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5665{
5666 buf_T *buf = term_get_buf(argvars, "term_setsize()");
5667 term_T *term;
5668 varnumber_T rows, cols;
5669
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005670 if (buf == NULL)
5671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005672 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005673 return;
5674 }
5675 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005676 return;
5677 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005678 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005679 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005680 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005681 cols = cols <= 0 ? term->tl_cols : cols;
5682 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005683 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005684
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005685 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005686 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5687 term_report_winsize(term, term->tl_rows, term->tl_cols);
5688}
5689
5690/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005691 * "term_getstatus(buf)" function
5692 */
5693 void
5694f_term_getstatus(typval_T *argvars, typval_T *rettv)
5695{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005696 buf_T *buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005697 term_T *term;
5698 char_u val[100];
5699
5700 rettv->v_type = VAR_STRING;
5701 if (buf == NULL)
5702 return;
5703 term = buf->b_term;
5704
5705 if (term_job_running(term))
5706 STRCPY(val, "running");
5707 else
5708 STRCPY(val, "finished");
5709 if (term->tl_normal_mode)
5710 STRCAT(val, ",normal");
5711 rettv->vval.v_string = vim_strsave(val);
5712}
5713
5714/*
5715 * "term_gettitle(buf)" function
5716 */
5717 void
5718f_term_gettitle(typval_T *argvars, typval_T *rettv)
5719{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005720 buf_T *buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005721
5722 rettv->v_type = VAR_STRING;
5723 if (buf == NULL)
5724 return;
5725
5726 if (buf->b_term->tl_title != NULL)
5727 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5728}
5729
5730/*
5731 * "term_gettty(buf)" function
5732 */
5733 void
5734f_term_gettty(typval_T *argvars, typval_T *rettv)
5735{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005736 buf_T *buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005737 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005738 int num = 0;
5739
5740 rettv->v_type = VAR_STRING;
5741 if (buf == NULL)
5742 return;
5743 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005744 num = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005745
5746 switch (num)
5747 {
5748 case 0:
5749 if (buf->b_term->tl_job != NULL)
5750 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005751 break;
5752 case 1:
5753 if (buf->b_term->tl_job != NULL)
5754 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005755 break;
5756 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005757 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005758 return;
5759 }
5760 if (p != NULL)
5761 rettv->vval.v_string = vim_strsave(p);
5762}
5763
5764/*
5765 * "term_list()" function
5766 */
5767 void
5768f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
5769{
5770 term_T *tp;
5771 list_T *l;
5772
5773 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
5774 return;
5775
5776 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005777 FOR_ALL_TERMS(tp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005778 if (tp != NULL && tp->tl_buffer != NULL)
5779 if (list_append_number(l,
5780 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
5781 return;
5782}
5783
5784/*
5785 * "term_scrape(buf, row)" function
5786 */
5787 void
5788f_term_scrape(typval_T *argvars, typval_T *rettv)
5789{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005790 buf_T *buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005791 VTermScreen *screen = NULL;
5792 VTermPos pos;
5793 list_T *l;
5794 term_T *term;
5795 char_u *p;
5796 sb_line_T *line;
5797
5798 if (rettv_list_alloc(rettv) == FAIL)
5799 return;
5800 if (buf == NULL)
5801 return;
5802 term = buf->b_term;
5803
5804 l = rettv->vval.v_list;
5805 pos.row = get_row_number(&argvars[1], term);
5806
5807 if (term->tl_vterm != NULL)
5808 {
5809 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01005810 if (screen == NULL) // can't really happen
5811 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005812 p = NULL;
5813 line = NULL;
5814 }
5815 else
5816 {
5817 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
5818
5819 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
5820 return;
5821 p = ml_get_buf(buf, lnum + 1, FALSE);
5822 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
5823 }
5824
5825 for (pos.col = 0; pos.col < term->tl_cols; )
5826 {
5827 dict_T *dcell;
5828 int width;
5829 VTermScreenCellAttrs attrs;
5830 VTermColor fg, bg;
5831 char_u rgb[8];
5832 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
5833 int off = 0;
5834 int i;
5835
5836 if (screen == NULL)
5837 {
5838 cellattr_T *cellattr;
5839 int len;
5840
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005841 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005842 if (pos.col >= line->sb_cols)
5843 break;
5844 cellattr = line->sb_cells + pos.col;
5845 width = cellattr->width;
5846 attrs = cellattr->attrs;
5847 fg = cellattr->fg;
5848 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02005849 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005850 mch_memmove(mbs, p, len);
5851 mbs[len] = NUL;
5852 p += len;
5853 }
5854 else
5855 {
5856 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005857
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005858 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
5859 break;
5860 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
5861 {
5862 if (cell.chars[i] == 0)
5863 break;
5864 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
5865 }
5866 mbs[off] = NUL;
5867 width = cell.width;
5868 attrs = cell.attrs;
5869 fg = cell.fg;
5870 bg = cell.bg;
5871 }
5872 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01005873 if (dcell == NULL)
5874 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005875 list_append_dict(l, dcell);
5876
Bram Moolenaare0be1672018-07-08 16:50:37 +02005877 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005878
5879 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5880 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005881 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005882 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5883 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005884 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005885
Bram Moolenaar83d47902020-03-26 20:34:00 +01005886 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02005887 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005888
5889 ++pos.col;
5890 if (width == 2)
5891 ++pos.col;
5892 }
5893}
5894
5895/*
5896 * "term_sendkeys(buf, keys)" function
5897 */
5898 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01005899f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005900{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005901 buf_T *buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005902 char_u *msg;
5903 term_T *term;
5904
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005905 if (buf == NULL)
5906 return;
5907
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005908 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005909 if (msg == NULL)
5910 return;
5911 term = buf->b_term;
5912 if (term->tl_vterm == NULL)
5913 return;
5914
5915 while (*msg != NUL)
5916 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02005917 int c;
5918
5919 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
5920 {
5921 c = TO_SPECIAL(msg[1], msg[2]);
5922 msg += 3;
5923 }
5924 else
5925 {
5926 c = PTR2CHAR(msg);
5927 msg += MB_CPTR2LEN(msg);
5928 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01005929 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005930 }
5931}
5932
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005933#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
5934/*
5935 * "term_getansicolors(buf)" function
5936 */
5937 void
5938f_term_getansicolors(typval_T *argvars, typval_T *rettv)
5939{
5940 buf_T *buf = term_get_buf(argvars, "term_getansicolors()");
5941 term_T *term;
5942 VTermState *state;
5943 VTermColor color;
5944 char_u hexbuf[10];
5945 int index;
5946 list_T *list;
5947
5948 if (rettv_list_alloc(rettv) == FAIL)
5949 return;
5950
5951 if (buf == NULL)
5952 return;
5953 term = buf->b_term;
5954 if (term->tl_vterm == NULL)
5955 return;
5956
5957 list = rettv->vval.v_list;
5958 state = vterm_obtain_state(term->tl_vterm);
5959 for (index = 0; index < 16; index++)
5960 {
5961 vterm_state_get_palette_color(state, index, &color);
5962 sprintf((char *)hexbuf, "#%02x%02x%02x",
5963 color.red, color.green, color.blue);
5964 if (list_append_string(list, hexbuf, 7) == FAIL)
5965 return;
5966 }
5967}
5968
5969/*
5970 * "term_setansicolors(buf, list)" function
5971 */
5972 void
5973f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
5974{
5975 buf_T *buf = term_get_buf(argvars, "term_setansicolors()");
5976 term_T *term;
5977
5978 if (buf == NULL)
5979 return;
5980 term = buf->b_term;
5981 if (term->tl_vterm == NULL)
5982 return;
5983
5984 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
5985 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005986 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005987 return;
5988 }
5989
5990 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005991 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005992}
5993#endif
5994
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005995/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02005996 * "term_setapi(buf, api)" function
5997 */
5998 void
5999f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6000{
6001 buf_T *buf = term_get_buf(argvars, "term_setapi()");
6002 term_T *term;
6003 char_u *api;
6004
6005 if (buf == NULL)
6006 return;
6007 term = buf->b_term;
6008 vim_free(term->tl_api);
6009 api = tv_get_string_chk(&argvars[1]);
6010 if (api != NULL)
6011 term->tl_api = vim_strsave(api);
6012 else
6013 term->tl_api = NULL;
6014}
6015
6016/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006017 * "term_setrestore(buf, command)" function
6018 */
6019 void
6020f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6021{
6022#if defined(FEAT_SESSION)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006023 buf_T *buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006024 term_T *term;
6025 char_u *cmd;
6026
6027 if (buf == NULL)
6028 return;
6029 term = buf->b_term;
6030 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006031 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006032 if (cmd != NULL)
6033 term->tl_command = vim_strsave(cmd);
6034 else
6035 term->tl_command = NULL;
6036#endif
6037}
6038
6039/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006040 * "term_setkill(buf, how)" function
6041 */
6042 void
6043f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6044{
6045 buf_T *buf = term_get_buf(argvars, "term_setkill()");
6046 term_T *term;
6047 char_u *how;
6048
6049 if (buf == NULL)
6050 return;
6051 term = buf->b_term;
6052 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006053 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006054 if (how != NULL)
6055 term->tl_kill = vim_strsave(how);
6056 else
6057 term->tl_kill = NULL;
6058}
6059
6060/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006061 * "term_start(command, options)" function
6062 */
6063 void
6064f_term_start(typval_T *argvars, typval_T *rettv)
6065{
6066 jobopt_T opt;
6067 buf_T *buf;
6068
6069 init_job_options(&opt);
6070 if (argvars[1].v_type != VAR_UNKNOWN
6071 && get_job_options(&argvars[1], &opt,
6072 JO_TIMEOUT_ALL + JO_STOPONEXIT
6073 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6074 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6075 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6076 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006077 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006078 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006079 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006080 return;
6081
Bram Moolenaar13568252018-03-16 20:46:58 +01006082 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006083
6084 if (buf != NULL && buf->b_term != NULL)
6085 rettv->vval.v_number = buf->b_fnum;
6086}
6087
6088/*
6089 * "term_wait" function
6090 */
6091 void
6092f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6093{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006094 buf_T *buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006095
6096 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006097 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006098 if (buf->b_term->tl_job == NULL)
6099 {
6100 ch_log(NULL, "term_wait(): no job to wait for");
6101 return;
6102 }
6103 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006104 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006105 return;
6106
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006107 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006108 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006109 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6110 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006111 // The job is dead, keep reading channel I/O until the channel is
6112 // closed. buf->b_term may become NULL if the terminal was closed while
6113 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006114 ch_log(NULL, "term_wait(): waiting for channel to close");
6115 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6116 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006117 term_flush_messages();
6118
Bram Moolenaard45aa552018-05-21 22:50:29 +02006119 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006120 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006121 // If the terminal is closed when the channel is closed the
6122 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006123 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006124 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006125
6126 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006127 }
6128 else
6129 {
6130 long wait = 10L;
6131
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006132 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006133
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006134 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006135 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006136 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006137 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006138
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006139 // Flushing messages on channels is hopefully sufficient.
6140 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006141 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006142 }
6143}
6144
6145/*
6146 * Called when a channel has sent all the lines to a terminal.
6147 * Send a CTRL-D to mark the end of the text.
6148 */
6149 void
6150term_send_eof(channel_T *ch)
6151{
6152 term_T *term;
6153
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006154 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006155 if (term->tl_job == ch->ch_job)
6156 {
6157 if (term->tl_eof_chars != NULL)
6158 {
6159 channel_send(ch, PART_IN, term->tl_eof_chars,
6160 (int)STRLEN(term->tl_eof_chars), NULL);
6161 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6162 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006163# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006164 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006165 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006166 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6167# endif
6168 }
6169}
6170
Bram Moolenaar113e1072019-01-20 15:30:40 +01006171#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006172 job_T *
6173term_getjob(term_T *term)
6174{
6175 return term != NULL ? term->tl_job : NULL;
6176}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006177#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006178
Bram Moolenaar4f974752019-02-17 17:44:42 +01006179# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006180
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006181///////////////////////////////////////
6182// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006183#ifdef PROTO
6184typedef int COORD;
6185typedef int DWORD;
6186typedef int HANDLE;
6187typedef int *DWORD_PTR;
6188typedef int HPCON;
6189typedef int HRESULT;
6190typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006191typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006192typedef int PSIZE_T;
6193typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006194typedef int BOOL;
6195# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006196#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006197
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006198HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6199HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6200HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006201BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6202BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6203void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006204
6205 static int
6206dyn_conpty_init(int verbose)
6207{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006208 static HMODULE hKerneldll = NULL;
6209 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006210 static struct
6211 {
6212 char *name;
6213 FARPROC *ptr;
6214 } conpty_entry[] =
6215 {
6216 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6217 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6218 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6219 {"InitializeProcThreadAttributeList",
6220 (FARPROC*)&pInitializeProcThreadAttributeList},
6221 {"UpdateProcThreadAttribute",
6222 (FARPROC*)&pUpdateProcThreadAttribute},
6223 {"DeleteProcThreadAttributeList",
6224 (FARPROC*)&pDeleteProcThreadAttributeList},
6225 {NULL, NULL}
6226 };
6227
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006228 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006229 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006230 if (verbose)
6231 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006232 return FAIL;
6233 }
6234
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006235 // No need to initialize twice.
6236 if (hKerneldll)
6237 return OK;
6238
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006239 hKerneldll = vimLoadLib("kernel32.dll");
6240 for (i = 0; conpty_entry[i].name != NULL
6241 && conpty_entry[i].ptr != NULL; ++i)
6242 {
6243 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6244 conpty_entry[i].name)) == NULL)
6245 {
6246 if (verbose)
6247 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006248 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006249 return FAIL;
6250 }
6251 }
6252
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006253 return OK;
6254}
6255
6256 static int
6257conpty_term_and_job_init(
6258 term_T *term,
6259 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006260 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006261 jobopt_T *opt,
6262 jobopt_T *orig_opt)
6263{
6264 WCHAR *cmd_wchar = NULL;
6265 WCHAR *cmd_wchar_copy = NULL;
6266 WCHAR *cwd_wchar = NULL;
6267 WCHAR *env_wchar = NULL;
6268 channel_T *channel = NULL;
6269 job_T *job = NULL;
6270 HANDLE jo = NULL;
6271 garray_T ga_cmd, ga_env;
6272 char_u *cmd = NULL;
6273 HRESULT hr;
6274 COORD consize;
6275 SIZE_T breq;
6276 PROCESS_INFORMATION proc_info;
6277 HANDLE i_theirs = NULL;
6278 HANDLE o_theirs = NULL;
6279 HANDLE i_ours = NULL;
6280 HANDLE o_ours = NULL;
6281
6282 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6283 ga_init2(&ga_env, (int)sizeof(char*), 20);
6284
6285 if (argvar->v_type == VAR_STRING)
6286 {
6287 cmd = argvar->vval.v_string;
6288 }
6289 else if (argvar->v_type == VAR_LIST)
6290 {
6291 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6292 goto failed;
6293 cmd = ga_cmd.ga_data;
6294 }
6295 if (cmd == NULL || *cmd == NUL)
6296 {
6297 emsg(_(e_invarg));
6298 goto failed;
6299 }
6300
6301 term->tl_arg0_cmd = vim_strsave(cmd);
6302
6303 cmd_wchar = enc_to_utf16(cmd, NULL);
6304
6305 if (cmd_wchar != NULL)
6306 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006307 // Request by CreateProcessW
6308 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006309 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006310 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6311 }
6312
6313 ga_clear(&ga_cmd);
6314 if (cmd_wchar == NULL)
6315 goto failed;
6316 if (opt->jo_cwd != NULL)
6317 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6318
6319 win32_build_env(opt->jo_env, &ga_env, TRUE);
6320 env_wchar = ga_env.ga_data;
6321
6322 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6323 goto failed;
6324 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6325 goto failed;
6326
6327 consize.X = term->tl_cols;
6328 consize.Y = term->tl_rows;
6329 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6330 &term->tl_conpty);
6331 if (FAILED(hr))
6332 goto failed;
6333
6334 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6335
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006336 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006337 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006338 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006339 if (!term->tl_siex.lpAttributeList)
6340 goto failed;
6341 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6342 0, &breq))
6343 goto failed;
6344 if (!pUpdateProcThreadAttribute(
6345 term->tl_siex.lpAttributeList, 0,
6346 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6347 sizeof(HPCON), NULL, NULL))
6348 goto failed;
6349
6350 channel = add_channel();
6351 if (channel == NULL)
6352 goto failed;
6353
6354 job = job_alloc();
6355 if (job == NULL)
6356 goto failed;
6357 if (argvar->v_type == VAR_STRING)
6358 {
6359 int argc;
6360
6361 build_argv_from_string(cmd, &job->jv_argv, &argc);
6362 }
6363 else
6364 {
6365 int argc;
6366
6367 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6368 }
6369
6370 if (opt->jo_set & JO_IN_BUF)
6371 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6372
6373 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6374 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006375 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006376 env_wchar, cwd_wchar,
6377 &term->tl_siex.StartupInfo, &proc_info))
6378 goto failed;
6379
6380 CloseHandle(i_theirs);
6381 CloseHandle(o_theirs);
6382
6383 channel_set_pipes(channel,
6384 (sock_T)i_ours,
6385 (sock_T)o_ours,
6386 (sock_T)o_ours);
6387
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006388 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006389 channel->ch_write_text_mode = TRUE;
6390
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006391 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006392 channel->ch_anonymous_pipe = TRUE;
6393
6394 jo = CreateJobObject(NULL, NULL);
6395 if (jo == NULL)
6396 goto failed;
6397
6398 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6399 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006400 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006401 CloseHandle(jo);
6402 jo = NULL;
6403 }
6404
6405 ResumeThread(proc_info.hThread);
6406 CloseHandle(proc_info.hThread);
6407
6408 vim_free(cmd_wchar);
6409 vim_free(cmd_wchar_copy);
6410 vim_free(cwd_wchar);
6411 vim_free(env_wchar);
6412
6413 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6414 goto failed;
6415
6416#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6417 if (opt->jo_set2 & JO2_ANSI_COLORS)
6418 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6419 else
6420 init_vterm_ansi_colors(term->tl_vterm);
6421#endif
6422
6423 channel_set_job(channel, job, opt);
6424 job_set_options(job, opt);
6425
6426 job->jv_channel = channel;
6427 job->jv_proc_info = proc_info;
6428 job->jv_job_object = jo;
6429 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006430 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006431 ++job->jv_refcount;
6432 term->tl_job = job;
6433
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006434 // Redirecting stdout and stderr doesn't work at the job level. Instead
6435 // open the file here and handle it in. opt->jo_io was changed in
6436 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006437 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6438 {
6439 char_u *fname = opt->jo_io_name[PART_OUT];
6440
6441 ch_log(channel, "Opening output file %s", fname);
6442 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6443 if (term->tl_out_fd == NULL)
6444 semsg(_(e_notopen), fname);
6445 }
6446
6447 return OK;
6448
6449failed:
6450 ga_clear(&ga_cmd);
6451 ga_clear(&ga_env);
6452 vim_free(cmd_wchar);
6453 vim_free(cmd_wchar_copy);
6454 vim_free(cwd_wchar);
6455 if (channel != NULL)
6456 channel_clear(channel);
6457 if (job != NULL)
6458 {
6459 job->jv_channel = NULL;
6460 job_cleanup(job);
6461 }
6462 term->tl_job = NULL;
6463 if (jo != NULL)
6464 CloseHandle(jo);
6465
6466 if (term->tl_siex.lpAttributeList != NULL)
6467 {
6468 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6469 vim_free(term->tl_siex.lpAttributeList);
6470 }
6471 term->tl_siex.lpAttributeList = NULL;
6472 if (o_theirs != NULL)
6473 CloseHandle(o_theirs);
6474 if (o_ours != NULL)
6475 CloseHandle(o_ours);
6476 if (i_ours != NULL)
6477 CloseHandle(i_ours);
6478 if (i_theirs != NULL)
6479 CloseHandle(i_theirs);
6480 if (term->tl_conpty != NULL)
6481 pClosePseudoConsole(term->tl_conpty);
6482 term->tl_conpty = NULL;
6483 return FAIL;
6484}
6485
6486 static void
6487conpty_term_report_winsize(term_T *term, int rows, int cols)
6488{
6489 COORD consize;
6490
6491 consize.X = cols;
6492 consize.Y = rows;
6493 pResizePseudoConsole(term->tl_conpty, consize);
6494}
6495
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006496 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006497term_free_conpty(term_T *term)
6498{
6499 if (term->tl_siex.lpAttributeList != NULL)
6500 {
6501 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6502 vim_free(term->tl_siex.lpAttributeList);
6503 }
6504 term->tl_siex.lpAttributeList = NULL;
6505 if (term->tl_conpty != NULL)
6506 pClosePseudoConsole(term->tl_conpty);
6507 term->tl_conpty = NULL;
6508}
6509
6510 int
6511use_conpty(void)
6512{
6513 return has_conpty;
6514}
6515
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006516# ifndef PROTO
6517
6518#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6519#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006520#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006521
6522void* (*winpty_config_new)(UINT64, void*);
6523void* (*winpty_open)(void*, void*);
6524void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6525BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6526void (*winpty_config_set_mouse_mode)(void*, int);
6527void (*winpty_config_set_initial_size)(void*, int, int);
6528LPCWSTR (*winpty_conin_name)(void*);
6529LPCWSTR (*winpty_conout_name)(void*);
6530LPCWSTR (*winpty_conerr_name)(void*);
6531void (*winpty_free)(void*);
6532void (*winpty_config_free)(void*);
6533void (*winpty_spawn_config_free)(void*);
6534void (*winpty_error_free)(void*);
6535LPCWSTR (*winpty_error_msg)(void*);
6536BOOL (*winpty_set_size)(void*, int, int, void*);
6537HANDLE (*winpty_agent_process)(void*);
6538
6539#define WINPTY_DLL "winpty.dll"
6540
6541static HINSTANCE hWinPtyDLL = NULL;
6542# endif
6543
6544 static int
6545dyn_winpty_init(int verbose)
6546{
6547 int i;
6548 static struct
6549 {
6550 char *name;
6551 FARPROC *ptr;
6552 } winpty_entry[] =
6553 {
6554 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6555 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6556 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6557 {"winpty_config_set_mouse_mode",
6558 (FARPROC*)&winpty_config_set_mouse_mode},
6559 {"winpty_config_set_initial_size",
6560 (FARPROC*)&winpty_config_set_initial_size},
6561 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6562 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6563 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6564 {"winpty_free", (FARPROC*)&winpty_free},
6565 {"winpty_open", (FARPROC*)&winpty_open},
6566 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6567 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6568 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6569 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6570 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6571 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6572 {NULL, NULL}
6573 };
6574
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006575 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006576 if (hWinPtyDLL)
6577 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006578 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6579 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006580 if (*p_winptydll != NUL)
6581 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6582 if (!hWinPtyDLL)
6583 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6584 if (!hWinPtyDLL)
6585 {
6586 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006587 semsg(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006588 : (char_u *)WINPTY_DLL);
6589 return FAIL;
6590 }
6591 for (i = 0; winpty_entry[i].name != NULL
6592 && winpty_entry[i].ptr != NULL; ++i)
6593 {
6594 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6595 winpty_entry[i].name)) == NULL)
6596 {
6597 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006598 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006599 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006600 return FAIL;
6601 }
6602 }
6603
6604 return OK;
6605}
6606
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006607 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006608winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006609 term_T *term,
6610 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006611 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006612 jobopt_T *opt,
6613 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006614{
6615 WCHAR *cmd_wchar = NULL;
6616 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006617 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006618 channel_T *channel = NULL;
6619 job_T *job = NULL;
6620 DWORD error;
6621 HANDLE jo = NULL;
6622 HANDLE child_process_handle;
6623 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006624 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006625 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006626 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006627 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006628
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006629 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6630 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006631
6632 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006633 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006634 cmd = argvar->vval.v_string;
6635 }
6636 else if (argvar->v_type == VAR_LIST)
6637 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006638 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006639 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006640 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006641 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006642 if (cmd == NULL || *cmd == NUL)
6643 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006644 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006645 goto failed;
6646 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006647
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006648 term->tl_arg0_cmd = vim_strsave(cmd);
6649
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006650 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006651 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006652 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006653 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006654 if (opt->jo_cwd != NULL)
6655 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006656
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006657 win32_build_env(opt->jo_env, &ga_env, TRUE);
6658 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006659
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006660 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6661 if (term->tl_winpty_config == NULL)
6662 goto failed;
6663
6664 winpty_config_set_mouse_mode(term->tl_winpty_config,
6665 WINPTY_MOUSE_MODE_FORCE);
6666 winpty_config_set_initial_size(term->tl_winpty_config,
6667 term->tl_cols, term->tl_rows);
6668 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6669 if (term->tl_winpty == NULL)
6670 goto failed;
6671
6672 spawn_config = winpty_spawn_config_new(
6673 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6674 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6675 NULL,
6676 cmd_wchar,
6677 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006678 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006679 &winpty_err);
6680 if (spawn_config == NULL)
6681 goto failed;
6682
6683 channel = add_channel();
6684 if (channel == NULL)
6685 goto failed;
6686
6687 job = job_alloc();
6688 if (job == NULL)
6689 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006690 if (argvar->v_type == VAR_STRING)
6691 {
6692 int argc;
6693
6694 build_argv_from_string(cmd, &job->jv_argv, &argc);
6695 }
6696 else
6697 {
6698 int argc;
6699
6700 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6701 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006702
6703 if (opt->jo_set & JO_IN_BUF)
6704 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6705
6706 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
6707 &child_thread_handle, &error, &winpty_err))
6708 goto failed;
6709
6710 channel_set_pipes(channel,
6711 (sock_T)CreateFileW(
6712 winpty_conin_name(term->tl_winpty),
6713 GENERIC_WRITE, 0, NULL,
6714 OPEN_EXISTING, 0, NULL),
6715 (sock_T)CreateFileW(
6716 winpty_conout_name(term->tl_winpty),
6717 GENERIC_READ, 0, NULL,
6718 OPEN_EXISTING, 0, NULL),
6719 (sock_T)CreateFileW(
6720 winpty_conerr_name(term->tl_winpty),
6721 GENERIC_READ, 0, NULL,
6722 OPEN_EXISTING, 0, NULL));
6723
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006724 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006725 channel->ch_write_text_mode = TRUE;
6726
6727 jo = CreateJobObject(NULL, NULL);
6728 if (jo == NULL)
6729 goto failed;
6730
6731 if (!AssignProcessToJobObject(jo, child_process_handle))
6732 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006733 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006734 CloseHandle(jo);
6735 jo = NULL;
6736 }
6737
6738 winpty_spawn_config_free(spawn_config);
6739 vim_free(cmd_wchar);
6740 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006741 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006742
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006743 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6744 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006745
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006746#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6747 if (opt->jo_set2 & JO2_ANSI_COLORS)
6748 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6749 else
6750 init_vterm_ansi_colors(term->tl_vterm);
6751#endif
6752
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006753 channel_set_job(channel, job, opt);
6754 job_set_options(job, opt);
6755
6756 job->jv_channel = channel;
6757 job->jv_proc_info.hProcess = child_process_handle;
6758 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
6759 job->jv_job_object = jo;
6760 job->jv_status = JOB_STARTED;
6761 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006762 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006763 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006764 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006765 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006766 ++job->jv_refcount;
6767 term->tl_job = job;
6768
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006769 // Redirecting stdout and stderr doesn't work at the job level. Instead
6770 // open the file here and handle it in. opt->jo_io was changed in
6771 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006772 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6773 {
6774 char_u *fname = opt->jo_io_name[PART_OUT];
6775
6776 ch_log(channel, "Opening output file %s", fname);
6777 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6778 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006779 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006780 }
6781
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006782 return OK;
6783
6784failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006785 ga_clear(&ga_cmd);
6786 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006787 vim_free(cmd_wchar);
6788 vim_free(cwd_wchar);
6789 if (spawn_config != NULL)
6790 winpty_spawn_config_free(spawn_config);
6791 if (channel != NULL)
6792 channel_clear(channel);
6793 if (job != NULL)
6794 {
6795 job->jv_channel = NULL;
6796 job_cleanup(job);
6797 }
6798 term->tl_job = NULL;
6799 if (jo != NULL)
6800 CloseHandle(jo);
6801 if (term->tl_winpty != NULL)
6802 winpty_free(term->tl_winpty);
6803 term->tl_winpty = NULL;
6804 if (term->tl_winpty_config != NULL)
6805 winpty_config_free(term->tl_winpty_config);
6806 term->tl_winpty_config = NULL;
6807 if (winpty_err != NULL)
6808 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006809 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006810 (short_u *)winpty_error_msg(winpty_err), NULL);
6811
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006812 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006813 winpty_error_free(winpty_err);
6814 }
6815 return FAIL;
6816}
6817
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006818/*
6819 * Create a new terminal of "rows" by "cols" cells.
6820 * Store a reference in "term".
6821 * Return OK or FAIL.
6822 */
6823 static int
6824term_and_job_init(
6825 term_T *term,
6826 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01006827 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006828 jobopt_T *opt,
6829 jobopt_T *orig_opt)
6830{
6831 int use_winpty = FALSE;
6832 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006833 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006834
6835 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
6836 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
6837
6838 if (!has_winpty && !has_conpty)
6839 // If neither is available give the errors for winpty, since when
6840 // conpty is not available it can't be installed either.
6841 return dyn_winpty_init(TRUE);
6842
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006843 if (opt->jo_tty_type != NUL)
6844 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006845
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006846 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006847 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006848 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006849 use_conpty = TRUE;
6850 else if (has_winpty)
6851 use_winpty = TRUE;
6852 // else: error
6853 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006854 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006855 {
6856 if (has_winpty)
6857 use_winpty = TRUE;
6858 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006859 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006860 {
6861 if (has_conpty)
6862 use_conpty = TRUE;
6863 else
6864 return dyn_conpty_init(TRUE);
6865 }
6866
6867 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006868 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006869
6870 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006871 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006872
6873 // error
6874 return dyn_winpty_init(TRUE);
6875}
6876
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006877 static int
6878create_pty_only(term_T *term, jobopt_T *options)
6879{
6880 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
6881 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
6882 char in_name[80], out_name[80];
6883 channel_T *channel = NULL;
6884
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006885 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6886 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006887
6888 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
6889 GetCurrentProcessId(),
6890 curbuf->b_fnum);
6891 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
6892 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6893 PIPE_UNLIMITED_INSTANCES,
6894 0, 0, NMPWAIT_NOWAIT, NULL);
6895 if (hPipeIn == INVALID_HANDLE_VALUE)
6896 goto failed;
6897
6898 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
6899 GetCurrentProcessId(),
6900 curbuf->b_fnum);
6901 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
6902 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6903 PIPE_UNLIMITED_INSTANCES,
6904 0, 0, 0, NULL);
6905 if (hPipeOut == INVALID_HANDLE_VALUE)
6906 goto failed;
6907
6908 ConnectNamedPipe(hPipeIn, NULL);
6909 ConnectNamedPipe(hPipeOut, NULL);
6910
6911 term->tl_job = job_alloc();
6912 if (term->tl_job == NULL)
6913 goto failed;
6914 ++term->tl_job->jv_refcount;
6915
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006916 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006917 term->tl_job->jv_status = JOB_FINISHED;
6918
6919 channel = add_channel();
6920 if (channel == NULL)
6921 goto failed;
6922 term->tl_job->jv_channel = channel;
6923 channel->ch_keep_open = TRUE;
6924 channel->ch_named_pipe = TRUE;
6925
6926 channel_set_pipes(channel,
6927 (sock_T)hPipeIn,
6928 (sock_T)hPipeOut,
6929 (sock_T)hPipeOut);
6930 channel_set_job(channel, term->tl_job, options);
6931 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
6932 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
6933
6934 return OK;
6935
6936failed:
6937 if (hPipeIn != NULL)
6938 CloseHandle(hPipeIn);
6939 if (hPipeOut != NULL)
6940 CloseHandle(hPipeOut);
6941 return FAIL;
6942}
6943
6944/*
6945 * Free the terminal emulator part of "term".
6946 */
6947 static void
6948term_free_vterm(term_T *term)
6949{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006950 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006951 if (term->tl_winpty != NULL)
6952 winpty_free(term->tl_winpty);
6953 term->tl_winpty = NULL;
6954 if (term->tl_winpty_config != NULL)
6955 winpty_config_free(term->tl_winpty_config);
6956 term->tl_winpty_config = NULL;
6957 if (term->tl_vterm != NULL)
6958 vterm_free(term->tl_vterm);
6959 term->tl_vterm = NULL;
6960}
6961
6962/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02006963 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006964 */
6965 static void
6966term_report_winsize(term_T *term, int rows, int cols)
6967{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006968 if (term->tl_conpty)
6969 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006970 if (term->tl_winpty)
6971 winpty_set_size(term->tl_winpty, cols, rows, NULL);
6972}
6973
6974 int
6975terminal_enabled(void)
6976{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006977 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006978}
6979
6980# else
6981
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006982///////////////////////////////////////
6983// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006984
6985/*
6986 * Create a new terminal of "rows" by "cols" cells.
6987 * Start job for "cmd".
6988 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01006989 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006990 * Return OK or FAIL.
6991 */
6992 static int
6993term_and_job_init(
6994 term_T *term,
6995 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01006996 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006997 jobopt_T *opt,
6998 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006999{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007000 term->tl_arg0_cmd = NULL;
7001
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007002 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7003 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007004
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007005#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7006 if (opt->jo_set2 & JO2_ANSI_COLORS)
7007 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7008 else
7009 init_vterm_ansi_colors(term->tl_vterm);
7010#endif
7011
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007012 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007013 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007014 if (term->tl_job != NULL)
7015 ++term->tl_job->jv_refcount;
7016
7017 return term->tl_job != NULL
7018 && term->tl_job->jv_channel != NULL
7019 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7020}
7021
7022 static int
7023create_pty_only(term_T *term, jobopt_T *opt)
7024{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007025 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7026 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007027
7028 term->tl_job = job_alloc();
7029 if (term->tl_job == NULL)
7030 return FAIL;
7031 ++term->tl_job->jv_refcount;
7032
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007033 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007034 term->tl_job->jv_status = JOB_FINISHED;
7035
7036 return mch_create_pty_channel(term->tl_job, opt);
7037}
7038
7039/*
7040 * Free the terminal emulator part of "term".
7041 */
7042 static void
7043term_free_vterm(term_T *term)
7044{
7045 if (term->tl_vterm != NULL)
7046 vterm_free(term->tl_vterm);
7047 term->tl_vterm = NULL;
7048}
7049
7050/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007051 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007052 */
7053 static void
7054term_report_winsize(term_T *term, int rows, int cols)
7055{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007056 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007057 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7058 {
7059 int fd = -1;
7060 int part;
7061
7062 for (part = PART_OUT; part < PART_COUNT; ++part)
7063 {
7064 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007065 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007066 break;
7067 }
7068 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7069 mch_signal_job(term->tl_job, (char_u *)"winch");
7070 }
7071}
7072
7073# endif
7074
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007075#endif // FEAT_TERMINAL