blob: bd5fd41ee73b5af7ad0d6e9f265363a9ffe7e09f [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 Moolenaarb1009092020-05-31 16:04:42 +0200462 (buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
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 Moolenaard58d4f92020-07-01 15:49:29 +02001392 case K_MOUSELEFT: other = term_send_mouse(vterm, 7, 1); break;
1393 case K_MOUSERIGHT: other = term_send_mouse(vterm, 6, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001394
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;
Bram Moolenaarca70c072020-05-30 20:30:46 +02001612 ml_delete(1);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001613 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 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02001686 ml_delete(curbuf->b_ml.ml_line_count);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001687 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 Moolenaard58d4f92020-07-01 15:49:29 +02002477if (raw_c > 0)
2478 ch_log(NULL, "terminal_loop() got %d", raw_c);
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002479 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002480 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002481 // Job finished while waiting for a character. Push back the
2482 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002483 if (raw_c != K_IGNORE)
2484 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002485 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002486 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002487 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002488 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002489 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002490
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002491#ifdef UNIX
2492 /*
2493 * The shell or another program may change the tty settings. Getting
2494 * them for every typed character is a bit of overhead, but it's needed
2495 * for the first character typed, e.g. when Vim starts in a shell.
2496 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002497 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002498 {
2499 ttyinfo_T info;
2500
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002501 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002502 if (get_tty_info(tty_fd, &info) == OK)
2503 term_backspace_char = info.backspace;
2504 }
2505#endif
2506
Bram Moolenaar4f974752019-02-17 17:44:42 +01002507#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002508 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2509 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002510 if (ctrl_break_was_pressed)
2511 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2512#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002513 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2514 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002515 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002516#ifdef FEAT_GUI
2517 && !curbuf->b_term->tl_system
2518#endif
2519 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002520 {
2521 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002522 int prev_raw_c = raw_c;
2523 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002524
2525#ifdef FEAT_CMDL_INFO
2526 if (add_to_showcmd(c))
2527 out_flush();
2528#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002529 raw_c = term_vgetc();
2530 c = raw_c_to_ctrl(raw_c);
2531
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002532#ifdef FEAT_CMDL_INFO
2533 clear_showcmd();
2534#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002535 if (!term_use_loop_check(TRUE)
2536 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002537 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002538 break;
2539
2540 if (prev_c == Ctrl_BSL)
2541 {
2542 if (c == Ctrl_N)
2543 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002544 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002545 term_enter_normal_mode();
2546 ret = FAIL;
2547 goto theend;
2548 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002549 // Send both keys to the terminal, first one here, second one
2550 // below.
2551 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2552 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002553 }
2554 else if (c == Ctrl_C)
2555 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002556 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002557 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2558 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002559 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002560 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002561 // "CTRL-W .": send CTRL-W to the job
2562 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002563 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002564 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002565 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002566 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002567 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002568 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002569 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002570 else if (c == 'N')
2571 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002572 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002573 term_enter_normal_mode();
2574 ret = FAIL;
2575 goto theend;
2576 }
2577 else if (c == '"')
2578 {
2579 term_paste_register(prev_c);
2580 continue;
2581 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002582 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002583 {
Bram Moolenaara4b26992019-08-15 20:58:54 +02002584 char_u buf[MB_MAXBYTES + 2];
2585
2586 // Put the command into the typeahead buffer, when using the
2587 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2588 buf[0] = Ctrl_W;
2589 buf[(*mb_char2bytes)(c, buf + 1) + 1] = NUL;
2590 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002591 ret = OK;
2592 goto theend;
2593 }
2594 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002595# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002596 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002597 {
2598 WCHAR wc;
2599 char_u mb[3];
2600
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002601 mb[0] = (unsigned)raw_c >> 8;
2602 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002603 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002604 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002605 }
2606# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002607 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002608 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002609 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002610 // We are sure to come back here, don't reset the cursor color
2611 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002612 restore_cursor = FALSE;
2613
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002614 ret = OK;
2615 goto theend;
2616 }
2617 }
2618 ret = FAIL;
2619
2620theend:
2621 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002622 if (restore_cursor)
2623 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002624
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002625 // Move a snapshot of the screen contents to the buffer, so that completion
2626 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002627 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2628 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002629
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002630 return ret;
2631}
2632
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002633 static void
2634may_toggle_cursor(term_T *term)
2635{
2636 if (in_terminal_loop == term)
2637 {
2638 if (term->tl_cursor_visible)
2639 cursor_on();
2640 else
2641 cursor_off();
2642 }
2643}
2644
2645/*
Bram Moolenaar83d47902020-03-26 20:34:00 +01002646 * Cache "Terminal" highlight group colors.
2647 */
2648 void
2649set_terminal_default_colors(int cterm_fg, int cterm_bg)
2650{
2651 term_default_cterm_fg = cterm_fg - 1;
2652 term_default_cterm_bg = cterm_bg - 1;
2653}
2654
2655 static int
2656get_default_cterm_fg(term_T *term)
2657{
2658 if (term->tl_highlight_name != NULL)
2659 {
2660 int id = syn_name2id(term->tl_highlight_name);
2661 int fg = -1;
2662 int bg = -1;
2663
2664 if (id > 0)
2665 syn_id2cterm_bg(id, &fg, &bg);
2666 return fg;
2667 }
2668 return term_default_cterm_fg;
2669}
2670
2671 static int
2672get_default_cterm_bg(term_T *term)
2673{
2674 if (term->tl_highlight_name != NULL)
2675 {
2676 int id = syn_name2id(term->tl_highlight_name);
2677 int fg = -1;
2678 int bg = -1;
2679
2680 if (id > 0)
2681 syn_id2cterm_bg(id, &fg, &bg);
2682 return bg;
2683 }
2684 return term_default_cterm_bg;
2685}
2686
2687/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002688 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002689 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002690 */
2691 static int
2692color2index(VTermColor *color, int fg, int *boldp)
2693{
2694 int red = color->red;
2695 int blue = color->blue;
2696 int green = color->green;
2697
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002698 if (VTERM_COLOR_IS_DEFAULT_FG(color)
2699 || VTERM_COLOR_IS_DEFAULT_BG(color))
2700 return 0;
2701 if (VTERM_COLOR_IS_INDEXED(color))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002702 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002703 // The first 16 colors and default: use the ANSI index.
Bram Moolenaare5886cc2020-05-21 20:10:04 +02002704 switch (color->index + 1)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002705 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002706 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002707 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2708 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2709 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002710 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002711 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2712 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2713 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2714 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2715 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2716 case 10: return lookup_color(20, fg, boldp) + 1; // red
2717 case 11: return lookup_color(16, fg, boldp) + 1; // green
2718 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2719 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2720 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2721 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2722 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002723 }
2724 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002725
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002726 if (t_colors >= 256)
2727 {
2728 if (red == blue && red == green)
2729 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002730 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002731 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002732 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2733 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2734 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002735 int i;
2736
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002737 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002738 return 17; // 00/00/00
2739 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002740 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002741 for (i = 0; i < 23; ++i)
2742 if (red < cutoff[i])
2743 return i + 233;
2744 return 256;
2745 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002746 {
2747 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2748 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002749
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002750 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002751 for (ri = 0; ri < 5; ++ri)
2752 if (red < cutoff[ri])
2753 break;
2754 for (gi = 0; gi < 5; ++gi)
2755 if (green < cutoff[gi])
2756 break;
2757 for (bi = 0; bi < 5; ++bi)
2758 if (blue < cutoff[bi])
2759 break;
2760 return 17 + ri * 36 + gi * 6 + bi;
2761 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002762 }
2763 return 0;
2764}
2765
2766/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002767 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002768 */
2769 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002770vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002771{
2772 int attr = 0;
2773
2774 if (cellattrs.bold)
2775 attr |= HL_BOLD;
2776 if (cellattrs.underline)
2777 attr |= HL_UNDERLINE;
2778 if (cellattrs.italic)
2779 attr |= HL_ITALIC;
2780 if (cellattrs.strike)
2781 attr |= HL_STRIKETHROUGH;
2782 if (cellattrs.reverse)
2783 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002784 return attr;
2785}
2786
2787/*
2788 * Store Vterm attributes in "cell" from highlight flags.
2789 */
2790 static void
2791hl2vtermAttr(int attr, cellattr_T *cell)
2792{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002793 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002794 if (attr & HL_BOLD)
2795 cell->attrs.bold = 1;
2796 if (attr & HL_UNDERLINE)
2797 cell->attrs.underline = 1;
2798 if (attr & HL_ITALIC)
2799 cell->attrs.italic = 1;
2800 if (attr & HL_STRIKETHROUGH)
2801 cell->attrs.strike = 1;
2802 if (attr & HL_INVERSE)
2803 cell->attrs.reverse = 1;
2804}
2805
2806/*
2807 * Convert the attributes of a vterm cell into an attribute index.
2808 */
2809 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002810cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002811 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002812 win_T *wp,
2813 VTermScreenCellAttrs cellattrs,
2814 VTermColor cellfg,
2815 VTermColor cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002816{
2817 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002818
2819#ifdef FEAT_GUI
2820 if (gui.in_use)
2821 {
2822 guicolor_T fg, bg;
2823
2824 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2825 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2826 return get_gui_attr_idx(attr, fg, bg);
2827 }
2828 else
2829#endif
2830#ifdef FEAT_TERMGUICOLORS
2831 if (p_tgc)
2832 {
2833 guicolor_T fg, bg;
2834
2835 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2836 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2837
2838 return get_tgc_attr_idx(attr, fg, bg);
2839 }
2840 else
2841#endif
2842 {
2843 int bold = MAYBE;
2844 int fg = color2index(&cellfg, TRUE, &bold);
2845 int bg = color2index(&cellbg, FALSE, &bold);
2846
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002847 // Use the 'wincolor' or "Terminal" highlighting for the default
2848 // colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002849 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002850 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002851 int wincolor_fg = -1;
2852 int wincolor_bg = -1;
2853
2854 if (wp != NULL && *wp->w_p_wcr != NUL)
2855 {
2856 int id = syn_name2id(curwin->w_p_wcr);
2857
2858 // Get the 'wincolor' group colors.
2859 if (id > 0)
2860 syn_id2cterm_bg(id, &wincolor_fg, &wincolor_bg);
2861 }
2862 if (fg == 0)
2863 {
2864 if (wincolor_fg >= 0)
2865 fg = wincolor_fg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002866 else
2867 {
2868 int cterm_fg = get_default_cterm_fg(term);
2869
2870 if (cterm_fg >= 0)
2871 fg = cterm_fg + 1;
2872 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002873 }
2874 if (bg == 0)
2875 {
2876 if (wincolor_bg >= 0)
2877 bg = wincolor_bg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002878 else
2879 {
2880 int cterm_bg = get_default_cterm_bg(term);
2881
2882 if (cterm_bg >= 0)
2883 bg = cterm_bg + 1;
2884 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002885 }
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002886 }
2887
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002888 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002889 if (bold == TRUE)
2890 attr |= HL_BOLD;
2891 return get_cterm_attr_idx(attr, fg, bg);
2892 }
2893 return 0;
2894}
2895
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002896 static void
2897set_dirty_snapshot(term_T *term)
2898{
2899 term->tl_dirty_snapshot = TRUE;
2900#ifdef FEAT_TIMERS
2901 if (!term->tl_normal_mode)
2902 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002903 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002904 profile_setlimit(100L, &term->tl_timer_due);
2905 term->tl_timer_set = TRUE;
2906 }
2907#endif
2908}
2909
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002910 static int
2911handle_damage(VTermRect rect, void *user)
2912{
2913 term_T *term = (term_T *)user;
2914
2915 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2916 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002917 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002918 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002919 return 1;
2920}
2921
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002922 static void
2923term_scroll_up(term_T *term, int start_row, int count)
2924{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002925 win_T *wp = NULL;
2926 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002927 VTermColor fg, bg;
2928 VTermScreenCellAttrs attr;
2929 int clear_attr;
2930
Bram Moolenaara80faa82020-04-12 19:37:17 +02002931 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002932
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002933 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002934 {
2935 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002936 {
2937 // Set the color to clear lines with.
2938 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2939 &fg, &bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002940 clear_attr = cell2attr(term, wp, attr, fg, bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002941 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002942 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002943 }
2944}
2945
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002946 static int
2947handle_moverect(VTermRect dest, VTermRect src, void *user)
2948{
2949 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002950 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002951
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002952 // Scrolling up is done much more efficiently by deleting lines instead of
2953 // redrawing the text. But avoid doing this multiple times, postpone until
2954 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002955 if (dest.start_col == src.start_col
2956 && dest.end_col == src.end_col
2957 && dest.start_row < src.start_row)
2958 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002959 if (dest.start_row == 0)
2960 term->tl_postponed_scroll += count;
2961 else
2962 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002963 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02002964
2965 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
2966 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002967 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02002968
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002969 // Note sure if the scrolling will work correctly, let's do a complete
2970 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002971 redraw_buf_later(term->tl_buffer, NOT_VALID);
2972 return 1;
2973}
2974
2975 static int
2976handle_movecursor(
2977 VTermPos pos,
2978 VTermPos oldpos UNUSED,
2979 int visible,
2980 void *user)
2981{
2982 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002983 win_T *wp = NULL;
2984 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002985
2986 term->tl_cursor_pos = pos;
2987 term->tl_cursor_visible = visible;
2988
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002989 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002990 {
2991 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002992 position_cursor(wp, &pos, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002993 }
2994 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
2995 {
2996 may_toggle_cursor(term);
2997 update_cursor(term, term->tl_cursor_visible);
2998 }
2999
3000 return 1;
3001}
3002
3003 static int
3004handle_settermprop(
3005 VTermProp prop,
3006 VTermValue *value,
3007 void *user)
3008{
3009 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003010 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003011
3012 switch (prop)
3013 {
3014 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003015 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003016 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003017 if (strval == NULL)
3018 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003019 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003020 // a blank title isn't useful, make it empty, so that "running" is
3021 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003022 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003023 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003024 // Same as blank
3025 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003026 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003027 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3028 term->tl_title = NULL;
3029 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003030 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003031 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003032#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003033 else if (!enc_utf8 && enc_codepage > 0)
3034 {
3035 WCHAR *ret = NULL;
3036 int length = 0;
3037
3038 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003039 (char*)value->string.str,
3040 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003041 if (ret != NULL)
3042 {
3043 WideCharToMultiByte_alloc(enc_codepage, 0,
3044 ret, length, (char**)&term->tl_title,
3045 &length, 0, 0);
3046 vim_free(ret);
3047 }
3048 }
3049#endif
3050 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003051 {
Bram Moolenaar98f16712020-05-22 13:34:01 +02003052 term->tl_title = strval;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003053 strval = NULL;
3054 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003055 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003056 if (term == curbuf->b_term)
3057 maketitle();
3058 break;
3059
3060 case VTERM_PROP_CURSORVISIBLE:
3061 term->tl_cursor_visible = value->boolean;
3062 may_toggle_cursor(term);
3063 out_flush();
3064 break;
3065
3066 case VTERM_PROP_CURSORBLINK:
3067 term->tl_cursor_blink = value->boolean;
3068 may_set_cursor_props(term);
3069 break;
3070
3071 case VTERM_PROP_CURSORSHAPE:
3072 term->tl_cursor_shape = value->number;
3073 may_set_cursor_props(term);
3074 break;
3075
3076 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003077 strval = vim_strnsave((char_u *)value->string.str,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003078 value->string.len);
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003079 if (strval == NULL)
3080 break;
3081 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003082 may_set_cursor_props(term);
3083 break;
3084
3085 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003086 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003087 term->tl_using_altscreen = value->boolean;
3088 break;
3089
3090 default:
3091 break;
3092 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003093 vim_free(strval);
3094
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003095 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003096 return 1;
3097}
3098
3099/*
3100 * The job running in the terminal resized the terminal.
3101 */
3102 static int
3103handle_resize(int rows, int cols, void *user)
3104{
3105 term_T *term = (term_T *)user;
3106 win_T *wp;
3107
3108 term->tl_rows = rows;
3109 term->tl_cols = cols;
3110 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003111 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003112 term->tl_vterm_size_changed = FALSE;
3113 else
3114 {
3115 FOR_ALL_WINDOWS(wp)
3116 {
3117 if (wp->w_buffer == term->tl_buffer)
3118 {
3119 win_setheight_win(rows, wp);
3120 win_setwidth_win(cols, wp);
3121 }
3122 }
3123 redraw_buf_later(term->tl_buffer, NOT_VALID);
3124 }
3125 return 1;
3126}
3127
3128/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003129 * If the number of lines that are stored goes over 'termscrollback' then
3130 * delete the first 10%.
3131 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3132 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003133 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003134 static void
3135limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003136{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003137 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003138 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003139 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003140 int i;
3141
3142 curbuf = term->tl_buffer;
3143 for (i = 0; i < todo; ++i)
3144 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003145 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3146 if (update_buffer)
Bram Moolenaarca70c072020-05-30 20:30:46 +02003147 ml_delete(1);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003148 }
3149 curbuf = curwin->w_buffer;
3150
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003151 gap->ga_len -= todo;
3152 mch_memmove(gap->ga_data,
3153 (sb_line_T *)gap->ga_data + todo,
3154 sizeof(sb_line_T) * gap->ga_len);
3155 if (update_buffer)
3156 term->tl_scrollback_scrolled -= todo;
3157 }
3158}
3159
3160/*
3161 * Handle a line that is pushed off the top of the screen.
3162 */
3163 static int
3164handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3165{
3166 term_T *term = (term_T *)user;
3167 garray_T *gap;
3168 int update_buffer;
3169
3170 if (term->tl_normal_mode)
3171 {
3172 // In Terminal-Normal mode the user interacts with the buffer, thus we
3173 // must not change it. Postpone adding the scrollback lines.
3174 gap = &term->tl_scrollback_postponed;
3175 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003176 }
3177 else
3178 {
3179 // First remove the lines that were appended before, the pushed line
3180 // goes above it.
3181 cleanup_scrollback(term);
3182 gap = &term->tl_scrollback;
3183 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003184 }
3185
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003186 limit_scrollback(term, gap, update_buffer);
3187
3188 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003189 {
3190 cellattr_T *p = NULL;
3191 int len = 0;
3192 int i;
3193 int c;
3194 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003195 int text_len;
3196 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003197 sb_line_T *line;
3198 garray_T ga;
3199 cellattr_T fill_attr = term->tl_default_color;
3200
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003201 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003202 for (i = 0; i < cols; ++i)
3203 if (cells[i].chars[0] != 0)
3204 len = i + 1;
3205 else
3206 cell2cellattr(&cells[i], &fill_attr);
3207
3208 ga_init2(&ga, 1, 100);
3209 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003210 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003211 if (p != NULL)
3212 {
3213 for (col = 0; col < len; col += cells[col].width)
3214 {
3215 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3216 {
3217 ga.ga_len = 0;
3218 break;
3219 }
3220 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3221 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3222 (char_u *)ga.ga_data + ga.ga_len);
3223 cell2cellattr(&cells[col], &p[col]);
3224 }
3225 }
3226 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003227 {
3228 if (update_buffer)
3229 text = (char_u *)"";
3230 else
3231 text = vim_strsave((char_u *)"");
3232 text_len = 0;
3233 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003234 else
3235 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003236 text = ga.ga_data;
3237 text_len = ga.ga_len;
3238 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003239 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003240 if (update_buffer)
3241 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003242
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003243 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003244 line->sb_cols = len;
3245 line->sb_cells = p;
3246 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003247 if (update_buffer)
3248 {
3249 line->sb_text = NULL;
3250 ++term->tl_scrollback_scrolled;
3251 ga_clear(&ga); // free the text
3252 }
3253 else
3254 {
3255 line->sb_text = text;
3256 ga_init(&ga); // text is kept in tl_scrollback_postponed
3257 }
3258 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003259 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003260 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003261}
3262
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003263/*
3264 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3265 * received and stored in tl_scrollback_postponed.
3266 */
3267 static void
3268handle_postponed_scrollback(term_T *term)
3269{
3270 int i;
3271
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003272 if (term->tl_scrollback_postponed.ga_len == 0)
3273 return;
3274 ch_log(NULL, "Moving postponed scrollback to scrollback");
3275
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003276 // First remove the lines that were appended before, the pushed lines go
3277 // above it.
3278 cleanup_scrollback(term);
3279
3280 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3281 {
3282 char_u *text;
3283 sb_line_T *pp_line;
3284 sb_line_T *line;
3285
3286 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3287 break;
3288 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3289
3290 text = pp_line->sb_text;
3291 if (text == NULL)
3292 text = (char_u *)"";
3293 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3294 vim_free(pp_line->sb_text);
3295
3296 line = (sb_line_T *)term->tl_scrollback.ga_data
3297 + term->tl_scrollback.ga_len;
3298 line->sb_cols = pp_line->sb_cols;
3299 line->sb_cells = pp_line->sb_cells;
3300 line->sb_fill_attr = pp_line->sb_fill_attr;
3301 line->sb_text = NULL;
3302 ++term->tl_scrollback_scrolled;
3303 ++term->tl_scrollback.ga_len;
3304 }
3305
3306 ga_clear(&term->tl_scrollback_postponed);
3307 limit_scrollback(term, &term->tl_scrollback, TRUE);
3308}
3309
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003310static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003311 handle_damage, // damage
3312 handle_moverect, // moverect
3313 handle_movecursor, // movecursor
3314 handle_settermprop, // settermprop
3315 NULL, // bell
3316 handle_resize, // resize
3317 handle_pushline, // sb_pushline
3318 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003319};
3320
3321/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003322 * Do the work after the channel of a terminal was closed.
3323 * Must be called only when updating_screen is FALSE.
3324 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3325 */
3326 static int
3327term_after_channel_closed(term_T *term)
3328{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003329 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003330 if (!term->tl_normal_mode)
3331 {
3332 int fnum = term->tl_buffer->b_fnum;
3333
3334 cleanup_vterm(term);
3335
3336 if (term->tl_finish == TL_FINISH_CLOSE)
3337 {
3338 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003339 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003340#ifdef FEAT_PROP_POPUP
3341 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003342
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003343 // If this was a terminal in a popup window, go back to the
3344 // previous window.
3345 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3346 {
3347 pwin = curwin;
3348 if (win_valid(prevwin))
3349 win_enter(prevwin, FALSE);
3350 }
3351 else
3352#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003353 // If this is the last normal window: exit Vim.
3354 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3355 {
3356 exarg_T ea;
3357
Bram Moolenaara80faa82020-04-12 19:37:17 +02003358 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003359 ex_quit(&ea);
3360 return TRUE;
3361 }
3362
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003363 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003364 ch_log(NULL, "terminal job finished, closing window");
3365 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003366 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003367 if (curwin == aucmd_win)
3368 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003369 if (do_set_w_closing)
3370 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003371 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003372 if (do_set_w_closing)
3373 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003374 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003375#ifdef FEAT_PROP_POPUP
3376 if (pwin != NULL)
3377 popup_close_with_retval(pwin, 0);
3378#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003379 return TRUE;
3380 }
3381 if (term->tl_finish == TL_FINISH_OPEN
3382 && term->tl_buffer->b_nwindows == 0)
3383 {
3384 char buf[50];
3385
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003386 // TODO: use term_opencmd
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003387 ch_log(NULL, "terminal job finished, opening window");
3388 vim_snprintf(buf, sizeof(buf),
3389 term->tl_opencmd == NULL
3390 ? "botright sbuf %d"
3391 : (char *)term->tl_opencmd, fnum);
3392 do_cmdline_cmd((char_u *)buf);
3393 }
3394 else
3395 ch_log(NULL, "terminal job finished");
3396 }
3397
3398 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3399 return FALSE;
3400}
3401
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003402#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3403/*
3404 * If the current window is a terminal in a popup window and the job has
3405 * finished, close the popup window and to back to the previous window.
3406 * Otherwise return FAIL.
3407 */
3408 int
3409may_close_term_popup(void)
3410{
3411 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3412 && !term_job_running(curbuf->b_term))
3413 {
3414 win_T *pwin = curwin;
3415
3416 if (win_valid(prevwin))
3417 win_enter(prevwin, FALSE);
3418 popup_close_with_retval(pwin, 0);
3419 return OK;
3420 }
3421 return FAIL;
3422}
3423#endif
3424
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003425/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003426 * Called when a channel has been closed.
3427 * If this was a channel for a terminal window then finish it up.
3428 */
3429 void
3430term_channel_closed(channel_T *ch)
3431{
3432 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003433 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003434 int did_one = FALSE;
3435
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003436 for (term = first_term; term != NULL; term = next_term)
3437 {
3438 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003439 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003440 {
3441 term->tl_channel_closed = TRUE;
3442 did_one = TRUE;
3443
Bram Moolenaard23a8232018-02-10 18:45:26 +01003444 VIM_CLEAR(term->tl_title);
3445 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003446#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003447 if (term->tl_out_fd != NULL)
3448 {
3449 fclose(term->tl_out_fd);
3450 term->tl_out_fd = NULL;
3451 }
3452#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003453
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003454 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003455 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003456 // Cannot open or close windows now. Can happen when
3457 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003458 term->tl_channel_recently_closed = TRUE;
3459 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003460 }
3461
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003462 if (term_after_channel_closed(term))
3463 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003464 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003465 }
3466
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003467 if (did_one)
3468 {
3469 redraw_statuslines();
3470
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003471 // Need to break out of vgetc().
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02003472 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003473 typebuf_was_filled = TRUE;
3474
3475 term = curbuf->b_term;
3476 if (term != NULL)
3477 {
3478 if (term->tl_job == ch->ch_job)
3479 maketitle();
3480 update_cursor(term, term->tl_cursor_visible);
3481 }
3482 }
3483}
3484
3485/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003486 * To be called after resetting updating_screen: handle any terminal where the
3487 * channel was closed.
3488 */
3489 void
3490term_check_channel_closed_recently()
3491{
3492 term_T *term;
3493 term_T *next_term;
3494
3495 for (term = first_term; term != NULL; term = next_term)
3496 {
3497 next_term = term->tl_next;
3498 if (term->tl_channel_recently_closed)
3499 {
3500 term->tl_channel_recently_closed = FALSE;
3501 if (term_after_channel_closed(term))
3502 // start over, the list may have changed
3503 next_term = first_term;
3504 }
3505 }
3506}
3507
3508/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003509 * Fill one screen line from a line of the terminal.
3510 * Advances "pos" to past the last column.
3511 */
3512 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003513term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003514 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003515 win_T *wp,
3516 VTermScreen *screen,
3517 VTermPos *pos,
3518 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003519{
3520 int off = screen_get_current_line_off();
3521
3522 for (pos->col = 0; pos->col < max_col; )
3523 {
3524 VTermScreenCell cell;
3525 int c;
3526
3527 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003528 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003529
3530 c = cell.chars[0];
3531 if (c == NUL)
3532 {
3533 ScreenLines[off] = ' ';
3534 if (enc_utf8)
3535 ScreenLinesUC[off] = NUL;
3536 }
3537 else
3538 {
3539 if (enc_utf8)
3540 {
3541 int i;
3542
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003543 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003544 for (i = 0; i < Screen_mco
3545 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3546 {
3547 ScreenLinesC[i][off] = cell.chars[i + 1];
3548 if (cell.chars[i + 1] == 0)
3549 break;
3550 }
3551 if (c >= 0x80 || (Screen_mco > 0
3552 && ScreenLinesC[0][off] != 0))
3553 {
3554 ScreenLines[off] = ' ';
3555 ScreenLinesUC[off] = c;
3556 }
3557 else
3558 {
3559 ScreenLines[off] = c;
3560 ScreenLinesUC[off] = NUL;
3561 }
3562 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003563#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003564 else if (has_mbyte && c >= 0x80)
3565 {
3566 char_u mb[MB_MAXBYTES+1];
3567 WCHAR wc = c;
3568
3569 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3570 (char*)mb, 2, 0, 0) > 1)
3571 {
3572 ScreenLines[off] = mb[0];
3573 ScreenLines[off + 1] = mb[1];
3574 cell.width = mb_ptr2cells(mb);
3575 }
3576 else
3577 ScreenLines[off] = c;
3578 }
3579#endif
3580 else
3581 ScreenLines[off] = c;
3582 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003583 ScreenAttrs[off] = cell2attr(term, wp, cell.attrs, cell.fg, cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003584
3585 ++pos->col;
3586 ++off;
3587 if (cell.width == 2)
3588 {
3589 if (enc_utf8)
3590 ScreenLinesUC[off] = NUL;
3591
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003592 // don't set the second byte to NUL for a DBCS encoding, it
3593 // has been set above
Bram Moolenaar13568252018-03-16 20:46:58 +01003594 if (enc_utf8 || !has_mbyte)
3595 ScreenLines[off] = NUL;
3596
3597 ++pos->col;
3598 ++off;
3599 }
3600 }
3601}
3602
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003603#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003604 static void
3605update_system_term(term_T *term)
3606{
3607 VTermPos pos;
3608 VTermScreen *screen;
3609
3610 if (term->tl_vterm == NULL)
3611 return;
3612 screen = vterm_obtain_screen(term->tl_vterm);
3613
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003614 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003615 while (term->tl_toprow > 0
3616 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3617 {
3618 int save_p_more = p_more;
3619
3620 p_more = FALSE;
3621 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003622 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003623 p_more = save_p_more;
3624 --term->tl_toprow;
3625 }
3626
3627 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3628 && pos.row < Rows; ++pos.row)
3629 {
3630 if (pos.row < term->tl_rows)
3631 {
3632 int max_col = MIN(Columns, term->tl_cols);
3633
Bram Moolenaar83d47902020-03-26 20:34:00 +01003634 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003635 }
3636 else
3637 pos.col = 0;
3638
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003639 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003640 }
3641
3642 term->tl_dirty_row_start = MAX_ROW;
3643 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003644}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003645#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003646
3647/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003648 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3649 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003650 * Terminal-Normal mode.
3651 */
3652 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003653term_do_update_window(win_T *wp)
3654{
3655 term_T *term = wp->w_buffer->b_term;
3656
3657 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3658}
3659
3660/*
3661 * Called to update a window that contains an active terminal.
3662 */
3663 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003664term_update_window(win_T *wp)
3665{
3666 term_T *term = wp->w_buffer->b_term;
3667 VTerm *vterm;
3668 VTermScreen *screen;
3669 VTermState *state;
3670 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003671 int rows, cols;
3672 int newrows, newcols;
3673 int minsize;
3674 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003675
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003676 vterm = term->tl_vterm;
3677 screen = vterm_obtain_screen(vterm);
3678 state = vterm_obtain_state(vterm);
3679
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003680 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3681 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003682 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003683 {
3684 term->tl_dirty_row_start = 0;
3685 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003686
3687 if (term->tl_postponed_scroll > 0
3688 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003689 // Scrolling is usually faster than redrawing, when there are only
3690 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003691 term_scroll_up(term, 0, term->tl_postponed_scroll);
3692 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003693 }
3694
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003695 /*
3696 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003697 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003698 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003699 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003700
Bram Moolenaar498c2562018-04-15 23:45:15 +02003701 newrows = 99999;
3702 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003703 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003704 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003705 // Always use curwin, it may be a popup window.
3706 win_T *wwp = twp == NULL ? curwin : twp;
3707
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003708 // When more than one window shows the same terminal, use the
3709 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003710 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003711 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003712 newrows = MIN(newrows, wwp->w_height);
3713 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003714 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003715 if (twp == NULL)
3716 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003717 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003718 if (newrows == 99999 || newcols == 99999)
3719 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003720 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3721 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3722
3723 if (term->tl_rows != newrows || term->tl_cols != newcols)
3724 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003725 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003726 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003727 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003728 newrows);
3729 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003730
3731 // Updating the terminal size will cause the snapshot to be cleared.
3732 // When not in terminal_loop() we need to restore it.
3733 if (term != in_terminal_loop)
3734 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003735 }
3736
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003737 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003738 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003739 position_cursor(wp, &pos, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003740
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003741 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3742 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003743 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003744 if (pos.row < term->tl_rows)
3745 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003746 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003747
Bram Moolenaar83d47902020-03-26 20:34:00 +01003748 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003749 }
3750 else
3751 pos.col = 0;
3752
Bram Moolenaarf118d482018-03-13 13:14:00 +01003753 screen_line(wp->w_winrow + pos.row
3754#ifdef FEAT_MENU
3755 + winbar_height(wp)
3756#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003757 , wp->w_wincol, pos.col, wp->w_width,
3758#ifdef FEAT_PROP_POPUP
3759 popup_is_popup(wp) ? SLF_POPUP :
3760#endif
3761 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003762 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003763 term->tl_dirty_row_start = MAX_ROW;
3764 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003765}
3766
3767/*
3768 * Return TRUE if "wp" is a terminal window where the job has finished.
3769 */
3770 int
3771term_is_finished(buf_T *buf)
3772{
3773 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3774}
3775
3776/*
3777 * Return TRUE if "wp" is a terminal window where the job has finished or we
3778 * are in Terminal-Normal mode, thus we show the buffer contents.
3779 */
3780 int
3781term_show_buffer(buf_T *buf)
3782{
3783 term_T *term = buf->b_term;
3784
3785 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3786}
3787
3788/*
3789 * The current buffer is going to be changed. If there is terminal
3790 * highlighting remove it now.
3791 */
3792 void
3793term_change_in_curbuf(void)
3794{
3795 term_T *term = curbuf->b_term;
3796
3797 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3798 {
3799 free_scrollback(term);
3800 redraw_buf_later(term->tl_buffer, NOT_VALID);
3801
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003802 // The buffer is now like a normal buffer, it cannot be easily
3803 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003804 set_string_option_direct((char_u *)"buftype", -1,
3805 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3806 }
3807}
3808
3809/*
3810 * Get the screen attribute for a position in the buffer.
3811 * Use a negative "col" to get the filler background color.
3812 */
3813 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003814term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003815{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003816 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003817 term_T *term = buf->b_term;
3818 sb_line_T *line;
3819 cellattr_T *cellattr;
3820
3821 if (lnum > term->tl_scrollback.ga_len)
3822 cellattr = &term->tl_default_color;
3823 else
3824 {
3825 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3826 if (col < 0 || col >= line->sb_cols)
3827 cellattr = &line->sb_fill_attr;
3828 else
3829 cellattr = line->sb_cells + col;
3830 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003831 return cell2attr(term, wp, cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003832}
3833
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003834/*
3835 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003836 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003837 */
3838 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003839cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003840{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003841 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->index);
3842 if (rgb->index == 0)
3843 rgb->type = VTERM_COLOR_RGB;
3844 else
3845 {
3846 rgb->type = VTERM_COLOR_INDEXED;
3847 --rgb->index;
3848 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003849}
3850
3851/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003852 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003853 */
3854 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003855init_default_colors(term_T *term, win_T *wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003856{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003857 VTermColor *fg, *bg;
3858 int fgval, bgval;
3859 int id;
3860
Bram Moolenaara80faa82020-04-12 19:37:17 +02003861 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003862 term->tl_default_color.width = 1;
3863 fg = &term->tl_default_color.fg;
3864 bg = &term->tl_default_color.bg;
3865
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003866 // Vterm uses a default black background. Set it to white when
3867 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003868 if (*p_bg == 'l')
3869 {
3870 fgval = 0;
3871 bgval = 255;
3872 }
3873 else
3874 {
3875 fgval = 255;
3876 bgval = 0;
3877 }
3878 fg->red = fg->green = fg->blue = fgval;
3879 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02003880 fg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_FG;
3881 bg->type = VTERM_COLOR_RGB | VTERM_COLOR_DEFAULT_BG;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003882
Bram Moolenaar83d47902020-03-26 20:34:00 +01003883 // The 'wincolor' or the highlight group overrules the defaults.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003884 if (wp != NULL && *wp->w_p_wcr != NUL)
3885 id = syn_name2id(wp->w_p_wcr);
3886 else
Bram Moolenaar83d47902020-03-26 20:34:00 +01003887 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003888
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003889 // Use the actual color for the GUI and when 'termguicolors' is set.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003890#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3891 if (0
3892# ifdef FEAT_GUI
3893 || gui.in_use
3894# endif
3895# ifdef FEAT_TERMGUICOLORS
3896 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003897# ifdef FEAT_VTP
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003898 // Finally get INVALCOLOR on this execution path
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003899 || (!p_tgc && t_colors >= 256)
3900# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003901# endif
3902 )
3903 {
3904 guicolor_T fg_rgb = INVALCOLOR;
3905 guicolor_T bg_rgb = INVALCOLOR;
3906
3907 if (id != 0)
3908 syn_id2colors(id, &fg_rgb, &bg_rgb);
3909
3910# ifdef FEAT_GUI
3911 if (gui.in_use)
3912 {
3913 if (fg_rgb == INVALCOLOR)
3914 fg_rgb = gui.norm_pixel;
3915 if (bg_rgb == INVALCOLOR)
3916 bg_rgb = gui.back_pixel;
3917 }
3918# ifdef FEAT_TERMGUICOLORS
3919 else
3920# endif
3921# endif
3922# ifdef FEAT_TERMGUICOLORS
3923 {
3924 if (fg_rgb == INVALCOLOR)
3925 fg_rgb = cterm_normal_fg_gui_color;
3926 if (bg_rgb == INVALCOLOR)
3927 bg_rgb = cterm_normal_bg_gui_color;
3928 }
3929# endif
3930 if (fg_rgb != INVALCOLOR)
3931 {
3932 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3933
3934 fg->red = (unsigned)(rgb >> 16);
3935 fg->green = (unsigned)(rgb >> 8) & 255;
3936 fg->blue = (unsigned)rgb & 255;
3937 }
3938 if (bg_rgb != INVALCOLOR)
3939 {
3940 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
3941
3942 bg->red = (unsigned)(rgb >> 16);
3943 bg->green = (unsigned)(rgb >> 8) & 255;
3944 bg->blue = (unsigned)rgb & 255;
3945 }
3946 }
3947 else
3948#endif
3949 if (id != 0 && t_colors >= 16)
3950 {
Bram Moolenaar83d47902020-03-26 20:34:00 +01003951 int cterm_fg = get_default_cterm_fg(term);
3952 int cterm_bg = get_default_cterm_bg(term);
3953
3954 if (cterm_fg >= 0)
3955 cterm_color2vterm(cterm_fg, fg);
3956 if (cterm_bg >= 0)
3957 cterm_color2vterm(cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003958 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003959 else
3960 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003961#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003962 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003963#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003964
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003965 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003966 if (cterm_normal_fg_color > 0)
3967 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003968 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003969# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3970# ifdef VIMDLL
3971 if (!gui.in_use)
3972# endif
3973 {
3974 tmp = fg->red;
3975 fg->red = fg->blue;
3976 fg->blue = tmp;
3977 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003978# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003979 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02003980# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003981 else
3982 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02003983# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003984
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003985 if (cterm_normal_bg_color > 0)
3986 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003987 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003988# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3989# ifdef VIMDLL
3990 if (!gui.in_use)
3991# endif
3992 {
3993 tmp = fg->red;
3994 fg->red = fg->blue;
3995 fg->blue = tmp;
3996 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003997# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003998 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02003999# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02004000 else
4001 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02004002# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004003 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01004004}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004005
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004006#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4007/*
4008 * Set the 16 ANSI colors from array of RGB values
4009 */
4010 static void
4011set_vterm_palette(VTerm *vterm, long_u *rgb)
4012{
4013 int index = 0;
4014 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004015
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004016 for (; index < 16; index++)
4017 {
4018 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004019
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004020 color.red = (unsigned)(rgb[index] >> 16);
4021 color.green = (unsigned)(rgb[index] >> 8) & 255;
4022 color.blue = (unsigned)rgb[index] & 255;
4023 vterm_state_set_palette_color(state, index, &color);
4024 }
4025}
4026
4027/*
4028 * Set the ANSI color palette from a list of colors
4029 */
4030 static int
4031set_ansi_colors_list(VTerm *vterm, list_T *list)
4032{
4033 int n = 0;
4034 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004035 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004036
Bram Moolenaarb0992022020-01-30 14:55:42 +01004037 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004038 {
4039 char_u *color_name;
4040 guicolor_T guicolor;
4041
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004042 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004043 if (color_name == NULL)
4044 return FAIL;
4045
4046 guicolor = GUI_GET_COLOR(color_name);
4047 if (guicolor == INVALCOLOR)
4048 return FAIL;
4049
4050 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4051 }
4052
4053 if (n != 16 || li != NULL)
4054 return FAIL;
4055
4056 set_vterm_palette(vterm, rgb);
4057
4058 return OK;
4059}
4060
4061/*
4062 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4063 */
4064 static void
4065init_vterm_ansi_colors(VTerm *vterm)
4066{
4067 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4068
4069 if (var != NULL
4070 && (var->di_tv.v_type != VAR_LIST
4071 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004072 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004073 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004074 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004075}
4076#endif
4077
Bram Moolenaar52acb112018-03-18 19:20:22 +01004078/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004079 * Handles a "drop" command from the job in the terminal.
4080 * "item" is the file name, "item->li_next" may have options.
4081 */
4082 static void
4083handle_drop_command(listitem_T *item)
4084{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004085 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004086 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004087 int bufnr;
4088 win_T *wp;
4089 tabpage_T *tp;
4090 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004091 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004092
4093 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4094 FOR_ALL_TAB_WINDOWS(tp, wp)
4095 {
4096 if (wp->w_buffer->b_fnum == bufnr)
4097 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004098 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004099 goto_tabpage_win(tp, wp);
4100 return;
4101 }
4102 }
4103
Bram Moolenaara80faa82020-04-12 19:37:17 +02004104 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004105
4106 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4107 && opt_item->li_tv.vval.v_dict != NULL)
4108 {
4109 dict_T *dict = opt_item->li_tv.vval.v_dict;
4110 char_u *p;
4111
Bram Moolenaar8f667172018-12-14 15:38:31 +01004112 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004113 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004114 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004115 if (p != NULL)
4116 {
4117 if (check_ff_value(p) == FAIL)
4118 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4119 else
4120 ea.force_ff = *p;
4121 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004122 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004123 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004124 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004125 if (p != NULL)
4126 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004127 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004128 if (ea.cmd != NULL)
4129 {
4130 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4131 ea.force_enc = 11;
4132 tofree = ea.cmd;
4133 }
4134 }
4135
Bram Moolenaar8f667172018-12-14 15:38:31 +01004136 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004137 if (p != NULL)
4138 get_bad_opt(p, &ea);
4139
4140 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4141 ea.force_bin = FORCE_BIN;
4142 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4143 ea.force_bin = FORCE_BIN;
4144 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4145 ea.force_bin = FORCE_NOBIN;
4146 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4147 ea.force_bin = FORCE_NOBIN;
4148 }
4149
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004150 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004151 if (ea.cmd == NULL)
4152 ea.cmd = (char_u *)"split";
4153 ea.arg = fname;
4154 ea.cmdidx = CMD_split;
4155 ex_splitview(&ea);
4156
4157 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004158}
4159
4160/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004161 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4162 */
4163 static int
4164is_permitted_term_api(char_u *func, char_u *pat)
4165{
4166 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4167}
4168
4169/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004170 * Handles a function call from the job running in a terminal.
4171 * "item" is the function name, "item->li_next" has the arguments.
4172 */
4173 static void
4174handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4175{
4176 char_u *func;
4177 typval_T argvars[2];
4178 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004179 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004180
4181 if (item->li_next == NULL)
4182 {
4183 ch_log(channel, "Missing function arguments for call");
4184 return;
4185 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004186 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004187
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004188 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004189 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004190 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004191 return;
4192 }
4193
4194 argvars[0].v_type = VAR_NUMBER;
4195 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4196 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004197 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004198 funcexe.firstline = 1L;
4199 funcexe.lastline = 1L;
4200 funcexe.evaluate = TRUE;
4201 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004202 {
4203 clear_tv(&rettv);
4204 ch_log(channel, "Function %s called", func);
4205 }
4206 else
4207 ch_log(channel, "Calling function %s failed", func);
4208}
4209
4210/*
4211 * Called by libvterm when it cannot recognize an OSC sequence.
4212 * We recognize a terminal API command.
4213 */
4214 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004215parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004216{
4217 term_T *term = (term_T *)user;
4218 js_read_T reader;
4219 typval_T tv;
4220 channel_T *channel = term->tl_job == NULL ? NULL
4221 : term->tl_job->jv_channel;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004222 garray_T *gap = &term->tl_osc_buf;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004223
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004224 // We recognize only OSC 5 1 ; {command}
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004225 if (command != 51)
4226 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004227
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004228 // Concatenate what was received until the final piece is found.
4229 if (ga_grow(gap, (int)frag.len + 1) == FAIL)
4230 {
4231 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004232 return 1;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004233 }
4234 mch_memmove((char *)gap->ga_data + gap->ga_len, frag.str, frag.len);
Bram Moolenaarf4b68e92020-05-27 21:22:14 +02004235 gap->ga_len += (int)frag.len;
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004236 if (!frag.final)
4237 return 1;
4238
4239 ((char *)gap->ga_data)[gap->ga_len] = 0;
4240 reader.js_buf = gap->ga_data;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004241 reader.js_fill = NULL;
4242 reader.js_used = 0;
4243 if (json_decode(&reader, &tv, 0) == OK
4244 && tv.v_type == VAR_LIST
4245 && tv.vval.v_list != NULL)
4246 {
4247 listitem_T *item = tv.vval.v_list->lv_first;
4248
4249 if (item == NULL)
4250 ch_log(channel, "Missing command");
4251 else
4252 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004253 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004254
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004255 // Make sure an invoked command doesn't delete the buffer (and the
4256 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004257 ++term->tl_buffer->b_locked;
4258
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004259 item = item->li_next;
4260 if (item == NULL)
4261 ch_log(channel, "Missing argument for %s", cmd);
4262 else if (STRCMP(cmd, "drop") == 0)
4263 handle_drop_command(item);
4264 else if (STRCMP(cmd, "call") == 0)
4265 handle_call_command(term, channel, item);
4266 else
4267 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004268 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004269 }
4270 }
4271 else
4272 ch_log(channel, "Invalid JSON received");
4273
Bram Moolenaareaa3e0d2020-05-19 23:11:00 +02004274 ga_clear(gap);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004275 clear_tv(&tv);
4276 return 1;
4277}
4278
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004279/*
4280 * Called by libvterm when it cannot recognize a CSI sequence.
4281 * We recognize the window position report.
4282 */
4283 static int
4284parse_csi(
4285 const char *leader UNUSED,
4286 const long args[],
4287 int argcount,
4288 const char *intermed UNUSED,
4289 char command,
4290 void *user)
4291{
4292 term_T *term = (term_T *)user;
4293 char buf[100];
4294 int len;
4295 int x = 0;
4296 int y = 0;
4297 win_T *wp;
4298
4299 // We recognize only CSI 13 t
4300 if (command != 't' || argcount != 1 || args[0] != 13)
4301 return 0; // not handled
4302
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004303 // When getting the window position is not possible or it fails it results
4304 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004305#if defined(FEAT_GUI) \
4306 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4307 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004308 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004309#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004310
4311 FOR_ALL_WINDOWS(wp)
4312 if (wp->w_buffer == term->tl_buffer)
4313 break;
4314 if (wp != NULL)
4315 {
4316#ifdef FEAT_GUI
4317 if (gui.in_use)
4318 {
4319 x += wp->w_wincol * gui.char_width;
4320 y += W_WINROW(wp) * gui.char_height;
4321 }
4322 else
4323#endif
4324 {
4325 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004326 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004327 x += wp->w_wincol * 7;
4328 y += W_WINROW(wp) * 10;
4329 }
4330 }
4331
4332 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4333 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4334 (char_u *)buf, len, NULL);
4335 return 1;
4336}
4337
Bram Moolenaard8637282020-05-20 18:41:41 +02004338static VTermStateFallbacks state_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004339 NULL, // control
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004340 parse_csi, // csi
4341 parse_osc, // osc
Bram Moolenaard8637282020-05-20 18:41:41 +02004342 NULL // dcs
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004343};
4344
4345/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004346 * Use Vim's allocation functions for vterm so profiling works.
4347 */
4348 static void *
4349vterm_malloc(size_t size, void *data UNUSED)
4350{
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004351 return alloc_clear(size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004352}
4353
4354 static void
4355vterm_memfree(void *ptr, void *data UNUSED)
4356{
4357 vim_free(ptr);
4358}
4359
4360static VTermAllocatorFunctions vterm_allocator = {
4361 &vterm_malloc,
4362 &vterm_memfree
4363};
4364
4365/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004366 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004367 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004368 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004369 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004370create_vterm(term_T *term, int rows, int cols)
4371{
4372 VTerm *vterm;
4373 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004374 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004375 VTermValue value;
4376
Bram Moolenaar756ef112018-04-10 12:04:27 +02004377 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004378 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004379 if (vterm == NULL)
4380 return FAIL;
4381
4382 // Allocate screen and state here, so we can bail out if that fails.
4383 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004384 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004385 if (state == NULL || screen == NULL)
4386 {
4387 vterm_free(vterm);
4388 return FAIL;
4389 }
4390
Bram Moolenaar52acb112018-03-18 19:20:22 +01004391 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004392 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004393 vterm_set_utf8(vterm, 1);
4394
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004395 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004396
4397 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004398 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004399 &term->tl_default_color.fg,
4400 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004401
Bram Moolenaar9e587872019-05-13 20:27:23 +02004402 if (t_colors < 16)
4403 // Less than 16 colors: assume that bold means using a bright color for
4404 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004405 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4406
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004407 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004408 vterm_screen_reset(screen, 1 /* hard */);
4409
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004410 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004411 vterm_screen_enable_altscreen(screen, 1);
4412
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004413 // For unix do not use a blinking cursor. In an xterm this causes the
4414 // cursor to blink if it's blinking in the xterm.
4415 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004416#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004417 if (GetCaretBlinkTime() == INFINITE)
4418 value.boolean = 0;
4419 else
4420 value.boolean = 1;
4421#else
4422 value.boolean = 0;
4423#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004424 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaard8637282020-05-20 18:41:41 +02004425 vterm_state_set_unrecognised_fallbacks(state, &state_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004426
4427 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004428}
4429
4430/*
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004431 * Called when 'wincolor' was set.
4432 */
4433 void
4434term_update_colors(void)
4435{
4436 term_T *term = curwin->w_buffer->b_term;
4437
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01004438 if (term->tl_vterm == NULL)
4439 return;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004440 init_default_colors(term, curwin);
4441 vterm_state_set_default_colors(
4442 vterm_obtain_state(term->tl_vterm),
4443 &term->tl_default_color.fg,
4444 &term->tl_default_color.bg);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01004445
4446 redraw_later(NOT_VALID);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004447}
4448
4449/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004450 * Return the text to show for the buffer name and status.
4451 */
4452 char_u *
4453term_get_status_text(term_T *term)
4454{
4455 if (term->tl_status_text == NULL)
4456 {
4457 char_u *txt;
4458 size_t len;
4459
4460 if (term->tl_normal_mode)
4461 {
4462 if (term_job_running(term))
4463 txt = (char_u *)_("Terminal");
4464 else
4465 txt = (char_u *)_("Terminal-finished");
4466 }
4467 else if (term->tl_title != NULL)
4468 txt = term->tl_title;
4469 else if (term_none_open(term))
4470 txt = (char_u *)_("active");
4471 else if (term_job_running(term))
4472 txt = (char_u *)_("running");
4473 else
4474 txt = (char_u *)_("finished");
4475 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004476 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004477 if (term->tl_status_text != NULL)
4478 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
4479 term->tl_buffer->b_fname, txt);
4480 }
4481 return term->tl_status_text;
4482}
4483
4484/*
4485 * Mark references in jobs of terminals.
4486 */
4487 int
4488set_ref_in_term(int copyID)
4489{
4490 int abort = FALSE;
4491 term_T *term;
4492 typval_T tv;
4493
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004494 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004495 if (term->tl_job != NULL)
4496 {
4497 tv.v_type = VAR_JOB;
4498 tv.vval.v_job = term->tl_job;
4499 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4500 }
4501 return abort;
4502}
4503
4504/*
4505 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004506 * Returns NULL when the buffer is not for a terminal window and logs a message
4507 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004508 */
4509 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004510term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004511{
4512 buf_T *buf;
4513
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004514 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004515 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004516 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004517 --emsg_off;
4518 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004519 {
4520 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004521 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004522 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004523 return buf;
4524}
4525
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004526 static void
4527clear_cell(VTermScreenCell *cell)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004528{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004529 CLEAR_FIELD(*cell);
4530 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4531 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004532}
4533
4534 static void
4535dump_term_color(FILE *fd, VTermColor *color)
4536{
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004537 int index;
4538
4539 if (VTERM_COLOR_IS_INDEXED(color))
4540 index = color->index + 1;
4541 else if (color->type == 0)
4542 // use RGB values
4543 index = 255;
4544 else
4545 // default color
4546 index = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004547 fprintf(fd, "%02x%02x%02x%d",
4548 (int)color->red, (int)color->green, (int)color->blue,
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004549 index);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004550}
4551
4552/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004553 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004554 *
4555 * Each screen cell in full is:
4556 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4557 * {characters} is a space for an empty cell
4558 * For a double-width character "+" is changed to "*" and the next cell is
4559 * skipped.
4560 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4561 * when "&" use the same as the previous cell.
4562 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4563 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4564 * {color-idx} is a number from 0 to 255
4565 *
4566 * Screen cell with same width, attributes and color as the previous one:
4567 * |{characters}
4568 *
4569 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4570 *
4571 * Repeating the previous screen cell:
4572 * @{count}
4573 */
4574 void
4575f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4576{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004577 buf_T *buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004578 term_T *term;
4579 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004580 int max_height = 0;
4581 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004582 stat_T st;
4583 FILE *fd;
4584 VTermPos pos;
4585 VTermScreen *screen;
4586 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004587 VTermState *state;
4588 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004589
4590 if (check_restricted() || check_secure())
4591 return;
4592 if (buf == NULL)
4593 return;
4594 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004595 if (term->tl_vterm == NULL)
4596 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004597 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004598 return;
4599 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004600
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004601 if (argvars[2].v_type != VAR_UNKNOWN)
4602 {
4603 dict_T *d;
4604
4605 if (argvars[2].v_type != VAR_DICT)
4606 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004607 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004608 return;
4609 }
4610 d = argvars[2].vval.v_dict;
4611 if (d != NULL)
4612 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004613 max_height = dict_get_number(d, (char_u *)"rows");
4614 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004615 }
4616 }
4617
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004618 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004619 if (fname == NULL)
4620 return;
4621 if (mch_stat((char *)fname, &st) >= 0)
4622 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004623 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004624 return;
4625 }
4626
Bram Moolenaard96ff162018-02-18 22:13:29 +01004627 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004629 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004630 return;
4631 }
4632
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004633 clear_cell(&prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004634
4635 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004636 state = vterm_obtain_state(term->tl_vterm);
4637 vterm_state_get_cursorpos(state, &cursor_pos);
4638
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004639 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4640 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004641 {
4642 int repeat = 0;
4643
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004644 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4645 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004646 {
4647 VTermScreenCell cell;
4648 int same_attr;
4649 int same_chars = TRUE;
4650 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004651 int is_cursor_pos = (pos.col == cursor_pos.col
4652 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004653
4654 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004655 clear_cell(&cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004656
4657 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4658 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004659 int c = cell.chars[i];
4660 int pc = prev_cell.chars[i];
4661
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004662 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004663 if (i == 0)
4664 {
4665 c = (c == NUL) ? ' ' : c;
4666 pc = (pc == NUL) ? ' ' : pc;
4667 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004668 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004669 same_chars = FALSE;
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004670 if (c == NUL || pc == NUL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004671 break;
4672 }
4673 same_attr = vtermAttr2hl(cell.attrs)
4674 == vtermAttr2hl(prev_cell.attrs)
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004675 && vterm_color_is_equal(&cell.fg, &prev_cell.fg)
4676 && vterm_color_is_equal(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004677 if (same_chars && cell.width == prev_cell.width && same_attr
4678 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004679 {
4680 ++repeat;
4681 }
4682 else
4683 {
4684 if (repeat > 0)
4685 {
4686 fprintf(fd, "@%d", repeat);
4687 repeat = 0;
4688 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004689 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004690
4691 if (cell.chars[0] == NUL)
4692 fputs(" ", fd);
4693 else
4694 {
4695 char_u charbuf[10];
4696 int len;
4697
4698 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4699 && cell.chars[i] != NUL; ++i)
4700 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004701 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004702 fwrite(charbuf, len, 1, fd);
4703 }
4704 }
4705
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004706 // When only the characters differ we don't write anything, the
4707 // following "|", "@" or NL will indicate using the same
4708 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004709 if (cell.width != prev_cell.width || !same_attr)
4710 {
4711 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004712 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004713 else
4714 fputs("+", fd);
4715
4716 if (same_attr)
4717 {
4718 fputs("&", fd);
4719 }
4720 else
4721 {
4722 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004723 if (vterm_color_is_equal(&cell.fg, &prev_cell.fg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004724 fputs("&", fd);
4725 else
4726 {
4727 fputs("#", fd);
4728 dump_term_color(fd, &cell.fg);
4729 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004730 if (vterm_color_is_equal(&cell.bg, &prev_cell.bg))
Bram Moolenaard96ff162018-02-18 22:13:29 +01004731 fputs("&", fd);
4732 else
4733 {
4734 fputs("#", fd);
4735 dump_term_color(fd, &cell.bg);
4736 }
4737 }
4738 }
4739
4740 prev_cell = cell;
4741 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004742
4743 if (cell.width == 2)
4744 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004745 }
4746 if (repeat > 0)
4747 fprintf(fd, "@%d", repeat);
4748 fputs("\n", fd);
4749 }
4750
4751 fclose(fd);
4752}
4753
4754/*
4755 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4756 */
4757 static void
4758dump_is_corrupt(garray_T *gap)
4759{
4760 ga_concat(gap, (char_u *)"CORRUPT");
4761}
4762
4763 static void
4764append_cell(garray_T *gap, cellattr_T *cell)
4765{
4766 if (ga_grow(gap, 1) == OK)
4767 {
4768 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4769 ++gap->ga_len;
4770 }
4771}
4772
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004773 static void
4774clear_cellattr(cellattr_T *cell)
4775{
4776 CLEAR_FIELD(*cell);
4777 cell->fg.type = VTERM_COLOR_DEFAULT_FG;
4778 cell->bg.type = VTERM_COLOR_DEFAULT_BG;
4779}
4780
Bram Moolenaard96ff162018-02-18 22:13:29 +01004781/*
4782 * Read the dump file from "fd" and append lines to the current buffer.
4783 * Return the cell width of the longest line.
4784 */
4785 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004786read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004787{
4788 int c;
4789 garray_T ga_text;
4790 garray_T ga_cell;
4791 char_u *prev_char = NULL;
4792 int attr = 0;
4793 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004794 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004795 term_T *term = curbuf->b_term;
4796 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004797 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004798
4799 ga_init2(&ga_text, 1, 90);
4800 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004801 clear_cellattr(&cell);
4802 clear_cellattr(&empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004803 cursor_pos->row = -1;
4804 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004805
4806 c = fgetc(fd);
4807 for (;;)
4808 {
4809 if (c == EOF)
4810 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004811 if (c == '\r')
4812 {
4813 // DOS line endings? Ignore.
4814 c = fgetc(fd);
4815 }
4816 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004817 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004818 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004819 if (ga_text.ga_data == NULL)
4820 dump_is_corrupt(&ga_text);
4821 if (ga_grow(&term->tl_scrollback, 1) == OK)
4822 {
4823 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
4824 + term->tl_scrollback.ga_len;
4825
4826 if (max_cells < ga_cell.ga_len)
4827 max_cells = ga_cell.ga_len;
4828 line->sb_cols = ga_cell.ga_len;
4829 line->sb_cells = ga_cell.ga_data;
4830 line->sb_fill_attr = term->tl_default_color;
4831 ++term->tl_scrollback.ga_len;
4832 ga_init(&ga_cell);
4833
4834 ga_append(&ga_text, NUL);
4835 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4836 ga_text.ga_len, FALSE);
4837 }
4838 else
4839 ga_clear(&ga_cell);
4840 ga_text.ga_len = 0;
4841
4842 c = fgetc(fd);
4843 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004844 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004845 {
4846 int prev_len = ga_text.ga_len;
4847
Bram Moolenaar9271d052018-02-25 21:39:46 +01004848 if (c == '>')
4849 {
4850 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004851 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01004852 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
4853 cursor_pos->col = ga_cell.ga_len;
4854 }
4855
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004856 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01004857 c = fgetc(fd);
4858 if (c != EOF)
4859 ga_append(&ga_text, c);
4860 for (;;)
4861 {
4862 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004863 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01004864 || c == EOF || c == '\n')
4865 break;
4866 ga_append(&ga_text, c);
4867 }
4868
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004869 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01004870 vim_free(prev_char);
4871 if (ga_text.ga_data != NULL)
4872 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
4873 ga_text.ga_len - prev_len);
4874
Bram Moolenaar9271d052018-02-25 21:39:46 +01004875 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004876 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004877 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01004878 }
4879 else if (c == '+' || c == '*')
4880 {
4881 int is_bg;
4882
4883 cell.width = c == '+' ? 1 : 2;
4884
4885 c = fgetc(fd);
4886 if (c == '&')
4887 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004888 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01004889 c = fgetc(fd);
4890 }
4891 else if (isdigit(c))
4892 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004893 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01004894 attr = 0;
4895 while (isdigit(c))
4896 {
4897 attr = attr * 10 + (c - '0');
4898 c = fgetc(fd);
4899 }
4900 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004901
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004902 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004903 for (is_bg = 0; is_bg <= 1; ++is_bg)
4904 {
4905 if (c == '&')
4906 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004907 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004908 c = fgetc(fd);
4909 }
4910 else if (c == '#')
4911 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004912 int red, green, blue, index = 0, type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004913
4914 c = fgetc(fd);
4915 red = hex2nr(c);
4916 c = fgetc(fd);
4917 red = (red << 4) + hex2nr(c);
4918 c = fgetc(fd);
4919 green = hex2nr(c);
4920 c = fgetc(fd);
4921 green = (green << 4) + hex2nr(c);
4922 c = fgetc(fd);
4923 blue = hex2nr(c);
4924 c = fgetc(fd);
4925 blue = (blue << 4) + hex2nr(c);
4926 c = fgetc(fd);
4927 if (!isdigit(c))
4928 dump_is_corrupt(&ga_text);
4929 while (isdigit(c))
4930 {
4931 index = index * 10 + (c - '0');
4932 c = fgetc(fd);
4933 }
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004934 if (index == 0 || index == 255)
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004935 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004936 type = VTERM_COLOR_RGB;
4937 if (index == 0)
4938 {
4939 if (is_bg)
4940 type |= VTERM_COLOR_DEFAULT_BG;
4941 else
4942 type |= VTERM_COLOR_DEFAULT_FG;
4943 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004944 }
4945 else
4946 {
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004947 type = VTERM_COLOR_INDEXED;
4948 index -= 1;
4949 }
4950 if (is_bg)
4951 {
4952 cell.bg.type = type;
4953 cell.bg.red = red;
4954 cell.bg.green = green;
4955 cell.bg.blue = blue;
4956 cell.bg.index = index;
4957 }
4958 else
4959 {
4960 cell.fg.type = type;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004961 cell.fg.red = red;
4962 cell.fg.green = green;
4963 cell.fg.blue = blue;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02004964 cell.fg.index = index;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004965 }
4966 }
4967 else
4968 dump_is_corrupt(&ga_text);
4969 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004970 }
4971 else
4972 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004973 }
4974 else
4975 dump_is_corrupt(&ga_text);
4976
4977 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004978 if (cell.width == 2)
4979 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004980 }
4981 else if (c == '@')
4982 {
4983 if (prev_char == NULL)
4984 dump_is_corrupt(&ga_text);
4985 else
4986 {
4987 int count = 0;
4988
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004989 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01004990 for (;;)
4991 {
4992 c = fgetc(fd);
4993 if (!isdigit(c))
4994 break;
4995 count = count * 10 + (c - '0');
4996 }
4997
4998 while (count-- > 0)
4999 {
5000 ga_concat(&ga_text, prev_char);
5001 append_cell(&ga_cell, &cell);
5002 }
5003 }
5004 }
5005 else
5006 {
5007 dump_is_corrupt(&ga_text);
5008 c = fgetc(fd);
5009 }
5010 }
5011
5012 if (ga_text.ga_len > 0)
5013 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005014 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01005015 dump_is_corrupt(&ga_text);
5016 ga_append(&ga_text, NUL);
5017 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
5018 ga_text.ga_len, FALSE);
5019 }
5020
5021 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02005022 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005023 vim_free(prev_char);
5024
5025 return max_cells;
5026}
5027
5028/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02005029 * Return an allocated string with at least "text_width" "=" characters and
5030 * "fname" inserted in the middle.
5031 */
5032 static char_u *
5033get_separator(int text_width, char_u *fname)
5034{
5035 int width = MAX(text_width, curwin->w_width);
5036 char_u *textline;
5037 int fname_size;
5038 char_u *p = fname;
5039 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005040 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005041
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02005042 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02005043 if (textline == NULL)
5044 return NULL;
5045
5046 fname_size = vim_strsize(fname);
5047 if (fname_size < width - 8)
5048 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005049 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02005050 width = MAX(text_width, fname_size + 8);
5051 }
5052 else if (fname_size > width - 8)
5053 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005054 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02005055 p = gettail(fname);
5056 fname_size = vim_strsize(p);
5057 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005058 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005059 while (fname_size > width - 8)
5060 {
5061 p += (*mb_ptr2len)(p);
5062 fname_size = vim_strsize(p);
5063 }
5064
5065 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5066 textline[i] = '=';
5067 textline[i++] = ' ';
5068
5069 STRCPY(textline + i, p);
5070 off = STRLEN(textline);
5071 textline[off] = ' ';
5072 for (i = 1; i < (width - fname_size) / 2; ++i)
5073 textline[off + i] = '=';
5074 textline[off + i] = NUL;
5075
5076 return textline;
5077}
5078
5079/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005080 * Common for "term_dumpdiff()" and "term_dumpload()".
5081 */
5082 static void
5083term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5084{
5085 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005086 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005087 char_u buf1[NUMBUFLEN];
5088 char_u buf2[NUMBUFLEN];
5089 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005090 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005091 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005092 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005093 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005094 char_u *textline = NULL;
5095
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005096 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005097 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005098 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005099 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005100 if (fname1 == NULL || (do_diff && fname2 == NULL))
5101 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005102 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005103 return;
5104 }
5105 fd1 = mch_fopen((char *)fname1, READBIN);
5106 if (fd1 == NULL)
5107 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005108 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005109 return;
5110 }
5111 if (do_diff)
5112 {
5113 fd2 = mch_fopen((char *)fname2, READBIN);
5114 if (fd2 == NULL)
5115 {
5116 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005117 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005118 return;
5119 }
5120 }
5121
5122 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005123 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5124 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5125 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5126 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5127 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005128
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005129 if (opt.jo_term_name == NULL)
5130 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005131 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005132
Bram Moolenaar51e14382019-05-25 20:21:28 +02005133 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005134 if (fname_tofree != NULL)
5135 {
5136 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5137 opt.jo_term_name = fname_tofree;
5138 }
5139 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005140
Bram Moolenaar87abab92019-06-03 21:14:59 +02005141 if (opt.jo_bufnr_buf != NULL)
5142 {
5143 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5144
5145 // With "bufnr" argument: enter the window with this buffer and make it
5146 // empty.
5147 if (wp == NULL)
5148 semsg(_(e_invarg2), "bufnr");
5149 else
5150 {
5151 buf = curbuf;
5152 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
Bram Moolenaarca70c072020-05-30 20:30:46 +02005153 ml_delete((linenr_T)1);
Bram Moolenaar86173482019-10-01 17:02:16 +02005154 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005155 redraw_later(NOT_VALID);
5156 }
5157 }
5158 else
5159 // Create a new terminal window.
5160 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5161
Bram Moolenaard96ff162018-02-18 22:13:29 +01005162 if (buf != NULL && buf->b_term != NULL)
5163 {
5164 int i;
5165 linenr_T bot_lnum;
5166 linenr_T lnum;
5167 term_T *term = buf->b_term;
5168 int width;
5169 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005170 VTermPos cursor_pos1;
5171 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005172
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005173 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005174
Bram Moolenaard96ff162018-02-18 22:13:29 +01005175 rettv->vval.v_number = buf->b_fnum;
5176
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005177 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005178 width = read_dump_file(fd1, &cursor_pos1);
5179
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005180 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005181 if (cursor_pos1.row >= 0)
5182 {
5183 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5184 coladvance(cursor_pos1.col);
5185 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005186
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005187 // Delete the empty line that was in the empty buffer.
Bram Moolenaarca70c072020-05-30 20:30:46 +02005188 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005189
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005190 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005191 if (!do_diff)
5192 goto theend;
5193
5194 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5195
Bram Moolenaar4a696342018-04-05 18:45:26 +02005196 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005197 if (textline == NULL)
5198 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005199 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5200 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5201 vim_free(textline);
5202
5203 textline = get_separator(width, fname2);
5204 if (textline == NULL)
5205 goto theend;
5206 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5207 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005208 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005209
5210 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005211 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005212 if (width2 > width)
5213 {
5214 vim_free(textline);
5215 textline = alloc(width2 + 1);
5216 if (textline == NULL)
5217 goto theend;
5218 width = width2;
5219 textline[width] = NUL;
5220 }
5221 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5222
5223 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5224 {
5225 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5226 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005227 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005228 for (i = 0; i < width; ++i)
5229 textline[i] = '-';
5230 }
5231 else
5232 {
5233 char_u *line1;
5234 char_u *line2;
5235 char_u *p1;
5236 char_u *p2;
5237 int col;
5238 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5239 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5240 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5241 ->sb_cells;
5242
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005243 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005244 line1 = vim_strsave(ml_get(lnum));
5245 if (line1 == NULL)
5246 break;
5247 p1 = line1;
5248
5249 line2 = ml_get(lnum + bot_lnum);
5250 p2 = line2;
5251 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5252 {
5253 int len1 = utfc_ptr2len(p1);
5254 int len2 = utfc_ptr2len(p2);
5255
5256 textline[col] = ' ';
5257 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005258 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005259 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005260 else if (lnum == cursor_pos1.row + 1
5261 && col == cursor_pos1.col
5262 && (cursor_pos1.row != cursor_pos2.row
5263 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005264 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005265 textline[col] = '>';
5266 else if (lnum == cursor_pos2.row + 1
5267 && col == cursor_pos2.col
5268 && (cursor_pos1.row != cursor_pos2.row
5269 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005270 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005271 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005272 else if (cellattr1 != NULL && cellattr2 != NULL)
5273 {
5274 if ((cellattr1 + col)->width
5275 != (cellattr2 + col)->width)
5276 textline[col] = 'w';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005277 else if (!vterm_color_is_equal(&(cellattr1 + col)->fg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005278 &(cellattr2 + col)->fg))
5279 textline[col] = 'f';
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005280 else if (!vterm_color_is_equal(&(cellattr1 + col)->bg,
Bram Moolenaard96ff162018-02-18 22:13:29 +01005281 &(cellattr2 + col)->bg))
5282 textline[col] = 'b';
5283 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5284 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5285 textline[col] = 'a';
5286 }
5287 p1 += len1;
5288 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005289 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005290 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005291
5292 while (col < width)
5293 {
5294 if (*p1 == NUL && *p2 == NUL)
5295 textline[col] = '?';
5296 else if (*p1 == NUL)
5297 {
5298 textline[col] = '+';
5299 p2 += utfc_ptr2len(p2);
5300 }
5301 else
5302 {
5303 textline[col] = '-';
5304 p1 += utfc_ptr2len(p1);
5305 }
5306 ++col;
5307 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005308
5309 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005310 }
5311 if (add_empty_scrollback(term, &term->tl_default_color,
5312 term->tl_top_diff_rows) == OK)
5313 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5314 ++bot_lnum;
5315 }
5316
5317 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5318 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005319 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005320 for (i = 0; i < width; ++i)
5321 textline[i] = '+';
5322 if (add_empty_scrollback(term, &term->tl_default_color,
5323 term->tl_top_diff_rows) == OK)
5324 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5325 ++lnum;
5326 ++bot_lnum;
5327 }
5328
5329 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005330
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005331 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005332 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005333 }
5334
5335theend:
5336 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005337 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005338 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005339 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005340 fclose(fd2);
5341}
5342
5343/*
5344 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5345 * bottom files.
5346 * Return FAIL when this is not possible.
5347 */
5348 int
5349term_swap_diff()
5350{
5351 term_T *term = curbuf->b_term;
5352 linenr_T line_count;
5353 linenr_T top_rows;
5354 linenr_T bot_rows;
5355 linenr_T bot_start;
5356 linenr_T lnum;
5357 char_u *p;
5358 sb_line_T *sb_line;
5359
5360 if (term == NULL
5361 || !term_is_finished(curbuf)
5362 || term->tl_top_diff_rows == 0
5363 || term->tl_scrollback.ga_len == 0)
5364 return FAIL;
5365
5366 line_count = curbuf->b_ml.ml_line_count;
5367 top_rows = term->tl_top_diff_rows;
5368 bot_rows = term->tl_bot_diff_rows;
5369 bot_start = line_count - bot_rows;
5370 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5371
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005372 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005373 for (lnum = 1; lnum <= top_rows; ++lnum)
5374 {
5375 p = vim_strsave(ml_get(1));
5376 if (p == NULL)
5377 return OK;
5378 ml_append(bot_start, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005379 ml_delete(1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005380 vim_free(p);
5381 }
5382
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005383 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005384 for (lnum = 1; lnum <= bot_rows; ++lnum)
5385 {
5386 p = vim_strsave(ml_get(bot_start + lnum));
5387 if (p == NULL)
5388 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005389 ml_delete(bot_start + lnum);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005390 ml_append(lnum - 1, p, 0, FALSE);
5391 vim_free(p);
5392 }
5393
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005394 // move top title to bottom
5395 p = vim_strsave(ml_get(bot_rows + 1));
5396 if (p == NULL)
5397 return OK;
5398 ml_append(line_count - top_rows - 1, p, 0, FALSE);
Bram Moolenaarca70c072020-05-30 20:30:46 +02005399 ml_delete(bot_rows + 1);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005400 vim_free(p);
5401
5402 // move bottom title to top
5403 p = vim_strsave(ml_get(line_count - top_rows));
5404 if (p == NULL)
5405 return OK;
Bram Moolenaarca70c072020-05-30 20:30:46 +02005406 ml_delete(line_count - top_rows);
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005407 ml_append(bot_rows, p, 0, FALSE);
5408 vim_free(p);
5409
Bram Moolenaard96ff162018-02-18 22:13:29 +01005410 if (top_rows == bot_rows)
5411 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005412 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005413 for (lnum = 0; lnum < top_rows; ++lnum)
5414 {
5415 sb_line_T temp;
5416
5417 temp = *(sb_line + lnum);
5418 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5419 *(sb_line + bot_start + lnum) = temp;
5420 }
5421 }
5422 else
5423 {
5424 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005425 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005426
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005427 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005428 if (temp != NULL)
5429 {
5430 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5431 mch_memmove(term->tl_scrollback.ga_data,
5432 temp + bot_start,
5433 sizeof(sb_line_T) * bot_rows);
5434 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5435 temp + top_rows,
5436 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5437 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5438 + line_count - top_rows,
5439 temp,
5440 sizeof(sb_line_T) * top_rows);
5441 vim_free(temp);
5442 }
5443 }
5444
5445 term->tl_top_diff_rows = bot_rows;
5446 term->tl_bot_diff_rows = top_rows;
5447
5448 update_screen(NOT_VALID);
5449 return OK;
5450}
5451
5452/*
5453 * "term_dumpdiff(filename, filename, options)" function
5454 */
5455 void
5456f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5457{
5458 term_load_dump(argvars, rettv, TRUE);
5459}
5460
5461/*
5462 * "term_dumpload(filename, options)" function
5463 */
5464 void
5465f_term_dumpload(typval_T *argvars, typval_T *rettv)
5466{
5467 term_load_dump(argvars, rettv, FALSE);
5468}
5469
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005470/*
5471 * "term_getaltscreen(buf)" function
5472 */
5473 void
5474f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5475{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005476 buf_T *buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005477
5478 if (buf == NULL)
5479 return;
5480 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5481}
5482
5483/*
5484 * "term_getattr(attr, name)" function
5485 */
5486 void
5487f_term_getattr(typval_T *argvars, typval_T *rettv)
5488{
5489 int attr;
5490 size_t i;
5491 char_u *name;
5492
5493 static struct {
5494 char *name;
5495 int attr;
5496 } attrs[] = {
5497 {"bold", HL_BOLD},
5498 {"italic", HL_ITALIC},
5499 {"underline", HL_UNDERLINE},
5500 {"strike", HL_STRIKETHROUGH},
5501 {"reverse", HL_INVERSE},
5502 };
5503
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005504 attr = tv_get_number(&argvars[0]);
5505 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005506 if (name == NULL)
5507 return;
5508
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005509 if (attr > HL_ALL)
5510 attr = syn_attr2attr(attr);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005511 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
5512 if (STRCMP(name, attrs[i].name) == 0)
5513 {
5514 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5515 break;
5516 }
5517}
5518
5519/*
5520 * "term_getcursor(buf)" function
5521 */
5522 void
5523f_term_getcursor(typval_T *argvars, typval_T *rettv)
5524{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005525 buf_T *buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005526 term_T *term;
5527 list_T *l;
5528 dict_T *d;
5529
5530 if (rettv_list_alloc(rettv) == FAIL)
5531 return;
5532 if (buf == NULL)
5533 return;
5534 term = buf->b_term;
5535
5536 l = rettv->vval.v_list;
5537 list_append_number(l, term->tl_cursor_pos.row + 1);
5538 list_append_number(l, term->tl_cursor_pos.col + 1);
5539
5540 d = dict_alloc();
5541 if (d != NULL)
5542 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005543 dict_add_number(d, "visible", term->tl_cursor_visible);
5544 dict_add_number(d, "blink", blink_state_is_inverted()
5545 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5546 dict_add_number(d, "shape", term->tl_cursor_shape);
5547 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005548 list_append_dict(l, d);
5549 }
5550}
5551
5552/*
5553 * "term_getjob(buf)" function
5554 */
5555 void
5556f_term_getjob(typval_T *argvars, typval_T *rettv)
5557{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005558 buf_T *buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005559
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005560 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005561 {
5562 rettv->v_type = VAR_SPECIAL;
5563 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005564 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005565 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005566
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005567 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005568 rettv->vval.v_job = buf->b_term->tl_job;
5569 if (rettv->vval.v_job != NULL)
5570 ++rettv->vval.v_job->jv_refcount;
5571}
5572
5573 static int
5574get_row_number(typval_T *tv, term_T *term)
5575{
5576 if (tv->v_type == VAR_STRING
5577 && tv->vval.v_string != NULL
5578 && STRCMP(tv->vval.v_string, ".") == 0)
5579 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005580 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005581}
5582
5583/*
5584 * "term_getline(buf, row)" function
5585 */
5586 void
5587f_term_getline(typval_T *argvars, typval_T *rettv)
5588{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005589 buf_T *buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005590 term_T *term;
5591 int row;
5592
5593 rettv->v_type = VAR_STRING;
5594 if (buf == NULL)
5595 return;
5596 term = buf->b_term;
5597 row = get_row_number(&argvars[1], term);
5598
5599 if (term->tl_vterm == NULL)
5600 {
5601 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5602
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005603 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005604 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5605 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5606 }
5607 else
5608 {
5609 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5610 VTermRect rect;
5611 int len;
5612 char_u *p;
5613
5614 if (row < 0 || row >= term->tl_rows)
5615 return;
5616 len = term->tl_cols * MB_MAXBYTES + 1;
5617 p = alloc(len);
5618 if (p == NULL)
5619 return;
5620 rettv->vval.v_string = p;
5621
5622 rect.start_col = 0;
5623 rect.end_col = term->tl_cols;
5624 rect.start_row = row;
5625 rect.end_row = row + 1;
5626 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5627 }
5628}
5629
5630/*
5631 * "term_getscrolled(buf)" function
5632 */
5633 void
5634f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5635{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005636 buf_T *buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005637
5638 if (buf == NULL)
5639 return;
5640 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5641}
5642
5643/*
5644 * "term_getsize(buf)" function
5645 */
5646 void
5647f_term_getsize(typval_T *argvars, typval_T *rettv)
5648{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005649 buf_T *buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005650 list_T *l;
5651
5652 if (rettv_list_alloc(rettv) == FAIL)
5653 return;
5654 if (buf == NULL)
5655 return;
5656
5657 l = rettv->vval.v_list;
5658 list_append_number(l, buf->b_term->tl_rows);
5659 list_append_number(l, buf->b_term->tl_cols);
5660}
5661
5662/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005663 * "term_setsize(buf, rows, cols)" function
5664 */
5665 void
5666f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5667{
5668 buf_T *buf = term_get_buf(argvars, "term_setsize()");
5669 term_T *term;
5670 varnumber_T rows, cols;
5671
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005672 if (buf == NULL)
5673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005674 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005675 return;
5676 }
5677 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005678 return;
5679 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005680 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005681 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005682 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005683 cols = cols <= 0 ? term->tl_cols : cols;
5684 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005685 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005686
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005687 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005688 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5689 term_report_winsize(term, term->tl_rows, term->tl_cols);
5690}
5691
5692/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005693 * "term_getstatus(buf)" function
5694 */
5695 void
5696f_term_getstatus(typval_T *argvars, typval_T *rettv)
5697{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005698 buf_T *buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005699 term_T *term;
5700 char_u val[100];
5701
5702 rettv->v_type = VAR_STRING;
5703 if (buf == NULL)
5704 return;
5705 term = buf->b_term;
5706
5707 if (term_job_running(term))
5708 STRCPY(val, "running");
5709 else
5710 STRCPY(val, "finished");
5711 if (term->tl_normal_mode)
5712 STRCAT(val, ",normal");
5713 rettv->vval.v_string = vim_strsave(val);
5714}
5715
5716/*
5717 * "term_gettitle(buf)" function
5718 */
5719 void
5720f_term_gettitle(typval_T *argvars, typval_T *rettv)
5721{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005722 buf_T *buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005723
5724 rettv->v_type = VAR_STRING;
5725 if (buf == NULL)
5726 return;
5727
5728 if (buf->b_term->tl_title != NULL)
5729 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5730}
5731
5732/*
5733 * "term_gettty(buf)" function
5734 */
5735 void
5736f_term_gettty(typval_T *argvars, typval_T *rettv)
5737{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005738 buf_T *buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005739 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005740 int num = 0;
5741
5742 rettv->v_type = VAR_STRING;
5743 if (buf == NULL)
5744 return;
5745 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005746 num = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005747
5748 switch (num)
5749 {
5750 case 0:
5751 if (buf->b_term->tl_job != NULL)
5752 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005753 break;
5754 case 1:
5755 if (buf->b_term->tl_job != NULL)
5756 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005757 break;
5758 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005759 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005760 return;
5761 }
5762 if (p != NULL)
5763 rettv->vval.v_string = vim_strsave(p);
5764}
5765
5766/*
5767 * "term_list()" function
5768 */
5769 void
5770f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
5771{
5772 term_T *tp;
5773 list_T *l;
5774
5775 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
5776 return;
5777
5778 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005779 FOR_ALL_TERMS(tp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005780 if (tp != NULL && tp->tl_buffer != NULL)
5781 if (list_append_number(l,
5782 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
5783 return;
5784}
5785
5786/*
5787 * "term_scrape(buf, row)" function
5788 */
5789 void
5790f_term_scrape(typval_T *argvars, typval_T *rettv)
5791{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005792 buf_T *buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005793 VTermScreen *screen = NULL;
5794 VTermPos pos;
5795 list_T *l;
5796 term_T *term;
5797 char_u *p;
5798 sb_line_T *line;
5799
5800 if (rettv_list_alloc(rettv) == FAIL)
5801 return;
5802 if (buf == NULL)
5803 return;
5804 term = buf->b_term;
5805
5806 l = rettv->vval.v_list;
5807 pos.row = get_row_number(&argvars[1], term);
5808
5809 if (term->tl_vterm != NULL)
5810 {
5811 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01005812 if (screen == NULL) // can't really happen
5813 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005814 p = NULL;
5815 line = NULL;
5816 }
5817 else
5818 {
5819 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
5820
5821 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
5822 return;
5823 p = ml_get_buf(buf, lnum + 1, FALSE);
5824 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
5825 }
5826
5827 for (pos.col = 0; pos.col < term->tl_cols; )
5828 {
5829 dict_T *dcell;
5830 int width;
5831 VTermScreenCellAttrs attrs;
5832 VTermColor fg, bg;
5833 char_u rgb[8];
5834 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
5835 int off = 0;
5836 int i;
5837
5838 if (screen == NULL)
5839 {
5840 cellattr_T *cellattr;
5841 int len;
5842
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005843 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005844 if (pos.col >= line->sb_cols)
5845 break;
5846 cellattr = line->sb_cells + pos.col;
5847 width = cellattr->width;
5848 attrs = cellattr->attrs;
5849 fg = cellattr->fg;
5850 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02005851 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005852 mch_memmove(mbs, p, len);
5853 mbs[len] = NUL;
5854 p += len;
5855 }
5856 else
5857 {
5858 VTermScreenCell cell;
Bram Moolenaare5886cc2020-05-21 20:10:04 +02005859
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005860 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
5861 break;
5862 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
5863 {
5864 if (cell.chars[i] == 0)
5865 break;
5866 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
5867 }
5868 mbs[off] = NUL;
5869 width = cell.width;
5870 attrs = cell.attrs;
5871 fg = cell.fg;
5872 bg = cell.bg;
5873 }
5874 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01005875 if (dcell == NULL)
5876 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005877 list_append_dict(l, dcell);
5878
Bram Moolenaare0be1672018-07-08 16:50:37 +02005879 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005880
5881 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5882 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005883 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005884 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5885 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005886 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005887
Bram Moolenaar83d47902020-03-26 20:34:00 +01005888 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02005889 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005890
5891 ++pos.col;
5892 if (width == 2)
5893 ++pos.col;
5894 }
5895}
5896
5897/*
5898 * "term_sendkeys(buf, keys)" function
5899 */
5900 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01005901f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005902{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005903 buf_T *buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005904 char_u *msg;
5905 term_T *term;
5906
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005907 if (buf == NULL)
5908 return;
5909
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005910 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005911 if (msg == NULL)
5912 return;
5913 term = buf->b_term;
5914 if (term->tl_vterm == NULL)
5915 return;
5916
5917 while (*msg != NUL)
5918 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02005919 int c;
5920
5921 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
5922 {
5923 c = TO_SPECIAL(msg[1], msg[2]);
5924 msg += 3;
5925 }
5926 else
5927 {
5928 c = PTR2CHAR(msg);
5929 msg += MB_CPTR2LEN(msg);
5930 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01005931 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005932 }
5933}
5934
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005935#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
5936/*
5937 * "term_getansicolors(buf)" function
5938 */
5939 void
5940f_term_getansicolors(typval_T *argvars, typval_T *rettv)
5941{
5942 buf_T *buf = term_get_buf(argvars, "term_getansicolors()");
5943 term_T *term;
5944 VTermState *state;
5945 VTermColor color;
5946 char_u hexbuf[10];
5947 int index;
5948 list_T *list;
5949
5950 if (rettv_list_alloc(rettv) == FAIL)
5951 return;
5952
5953 if (buf == NULL)
5954 return;
5955 term = buf->b_term;
5956 if (term->tl_vterm == NULL)
5957 return;
5958
5959 list = rettv->vval.v_list;
5960 state = vterm_obtain_state(term->tl_vterm);
5961 for (index = 0; index < 16; index++)
5962 {
5963 vterm_state_get_palette_color(state, index, &color);
5964 sprintf((char *)hexbuf, "#%02x%02x%02x",
5965 color.red, color.green, color.blue);
5966 if (list_append_string(list, hexbuf, 7) == FAIL)
5967 return;
5968 }
5969}
5970
5971/*
5972 * "term_setansicolors(buf, list)" function
5973 */
5974 void
5975f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
5976{
5977 buf_T *buf = term_get_buf(argvars, "term_setansicolors()");
5978 term_T *term;
5979
5980 if (buf == NULL)
5981 return;
5982 term = buf->b_term;
5983 if (term->tl_vterm == NULL)
5984 return;
5985
5986 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
5987 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005988 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005989 return;
5990 }
5991
5992 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005993 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005994}
5995#endif
5996
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005997/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02005998 * "term_setapi(buf, api)" function
5999 */
6000 void
6001f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
6002{
6003 buf_T *buf = term_get_buf(argvars, "term_setapi()");
6004 term_T *term;
6005 char_u *api;
6006
6007 if (buf == NULL)
6008 return;
6009 term = buf->b_term;
6010 vim_free(term->tl_api);
6011 api = tv_get_string_chk(&argvars[1]);
6012 if (api != NULL)
6013 term->tl_api = vim_strsave(api);
6014 else
6015 term->tl_api = NULL;
6016}
6017
6018/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006019 * "term_setrestore(buf, command)" function
6020 */
6021 void
6022f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6023{
6024#if defined(FEAT_SESSION)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006025 buf_T *buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006026 term_T *term;
6027 char_u *cmd;
6028
6029 if (buf == NULL)
6030 return;
6031 term = buf->b_term;
6032 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006033 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006034 if (cmd != NULL)
6035 term->tl_command = vim_strsave(cmd);
6036 else
6037 term->tl_command = NULL;
6038#endif
6039}
6040
6041/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006042 * "term_setkill(buf, how)" function
6043 */
6044 void
6045f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6046{
6047 buf_T *buf = term_get_buf(argvars, "term_setkill()");
6048 term_T *term;
6049 char_u *how;
6050
6051 if (buf == NULL)
6052 return;
6053 term = buf->b_term;
6054 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006055 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006056 if (how != NULL)
6057 term->tl_kill = vim_strsave(how);
6058 else
6059 term->tl_kill = NULL;
6060}
6061
6062/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006063 * "term_start(command, options)" function
6064 */
6065 void
6066f_term_start(typval_T *argvars, typval_T *rettv)
6067{
6068 jobopt_T opt;
6069 buf_T *buf;
6070
6071 init_job_options(&opt);
6072 if (argvars[1].v_type != VAR_UNKNOWN
6073 && get_job_options(&argvars[1], &opt,
6074 JO_TIMEOUT_ALL + JO_STOPONEXIT
6075 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6076 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6077 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6078 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006079 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006080 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006081 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006082 return;
6083
Bram Moolenaar13568252018-03-16 20:46:58 +01006084 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006085
6086 if (buf != NULL && buf->b_term != NULL)
6087 rettv->vval.v_number = buf->b_fnum;
6088}
6089
6090/*
6091 * "term_wait" function
6092 */
6093 void
6094f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6095{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006096 buf_T *buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006097
6098 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006099 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006100 if (buf->b_term->tl_job == NULL)
6101 {
6102 ch_log(NULL, "term_wait(): no job to wait for");
6103 return;
6104 }
6105 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006106 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006107 return;
6108
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006109 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006110 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006111 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6112 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006113 // The job is dead, keep reading channel I/O until the channel is
6114 // closed. buf->b_term may become NULL if the terminal was closed while
6115 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006116 ch_log(NULL, "term_wait(): waiting for channel to close");
6117 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6118 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006119 term_flush_messages();
6120
Bram Moolenaard45aa552018-05-21 22:50:29 +02006121 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006122 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006123 // If the terminal is closed when the channel is closed the
6124 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006125 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006126 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006127
6128 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006129 }
6130 else
6131 {
6132 long wait = 10L;
6133
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006134 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006135
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006136 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006137 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006138 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006139 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006140
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006141 // Flushing messages on channels is hopefully sufficient.
6142 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006143 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006144 }
6145}
6146
6147/*
6148 * Called when a channel has sent all the lines to a terminal.
6149 * Send a CTRL-D to mark the end of the text.
6150 */
6151 void
6152term_send_eof(channel_T *ch)
6153{
6154 term_T *term;
6155
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006156 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006157 if (term->tl_job == ch->ch_job)
6158 {
6159 if (term->tl_eof_chars != NULL)
6160 {
6161 channel_send(ch, PART_IN, term->tl_eof_chars,
6162 (int)STRLEN(term->tl_eof_chars), NULL);
6163 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6164 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006165# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006166 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006167 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006168 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6169# endif
6170 }
6171}
6172
Bram Moolenaar113e1072019-01-20 15:30:40 +01006173#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006174 job_T *
6175term_getjob(term_T *term)
6176{
6177 return term != NULL ? term->tl_job : NULL;
6178}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006179#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006180
Bram Moolenaar4f974752019-02-17 17:44:42 +01006181# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006182
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006183///////////////////////////////////////
6184// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006185#ifdef PROTO
6186typedef int COORD;
6187typedef int DWORD;
6188typedef int HANDLE;
6189typedef int *DWORD_PTR;
6190typedef int HPCON;
6191typedef int HRESULT;
6192typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006193typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006194typedef int PSIZE_T;
6195typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006196typedef int BOOL;
6197# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006198#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006199
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006200HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6201HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6202HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006203BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6204BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6205void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006206
6207 static int
6208dyn_conpty_init(int verbose)
6209{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006210 static HMODULE hKerneldll = NULL;
6211 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006212 static struct
6213 {
6214 char *name;
6215 FARPROC *ptr;
6216 } conpty_entry[] =
6217 {
6218 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6219 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6220 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6221 {"InitializeProcThreadAttributeList",
6222 (FARPROC*)&pInitializeProcThreadAttributeList},
6223 {"UpdateProcThreadAttribute",
6224 (FARPROC*)&pUpdateProcThreadAttribute},
6225 {"DeleteProcThreadAttributeList",
6226 (FARPROC*)&pDeleteProcThreadAttributeList},
6227 {NULL, NULL}
6228 };
6229
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006230 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006231 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006232 if (verbose)
6233 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006234 return FAIL;
6235 }
6236
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006237 // No need to initialize twice.
6238 if (hKerneldll)
6239 return OK;
6240
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006241 hKerneldll = vimLoadLib("kernel32.dll");
6242 for (i = 0; conpty_entry[i].name != NULL
6243 && conpty_entry[i].ptr != NULL; ++i)
6244 {
6245 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6246 conpty_entry[i].name)) == NULL)
6247 {
6248 if (verbose)
6249 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006250 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006251 return FAIL;
6252 }
6253 }
6254
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006255 return OK;
6256}
6257
6258 static int
6259conpty_term_and_job_init(
6260 term_T *term,
6261 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006262 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006263 jobopt_T *opt,
6264 jobopt_T *orig_opt)
6265{
6266 WCHAR *cmd_wchar = NULL;
6267 WCHAR *cmd_wchar_copy = NULL;
6268 WCHAR *cwd_wchar = NULL;
6269 WCHAR *env_wchar = NULL;
6270 channel_T *channel = NULL;
6271 job_T *job = NULL;
6272 HANDLE jo = NULL;
6273 garray_T ga_cmd, ga_env;
6274 char_u *cmd = NULL;
6275 HRESULT hr;
6276 COORD consize;
6277 SIZE_T breq;
6278 PROCESS_INFORMATION proc_info;
6279 HANDLE i_theirs = NULL;
6280 HANDLE o_theirs = NULL;
6281 HANDLE i_ours = NULL;
6282 HANDLE o_ours = NULL;
6283
6284 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6285 ga_init2(&ga_env, (int)sizeof(char*), 20);
6286
6287 if (argvar->v_type == VAR_STRING)
6288 {
6289 cmd = argvar->vval.v_string;
6290 }
6291 else if (argvar->v_type == VAR_LIST)
6292 {
6293 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6294 goto failed;
6295 cmd = ga_cmd.ga_data;
6296 }
6297 if (cmd == NULL || *cmd == NUL)
6298 {
6299 emsg(_(e_invarg));
6300 goto failed;
6301 }
6302
6303 term->tl_arg0_cmd = vim_strsave(cmd);
6304
6305 cmd_wchar = enc_to_utf16(cmd, NULL);
6306
6307 if (cmd_wchar != NULL)
6308 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006309 // Request by CreateProcessW
6310 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006311 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006312 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6313 }
6314
6315 ga_clear(&ga_cmd);
6316 if (cmd_wchar == NULL)
6317 goto failed;
6318 if (opt->jo_cwd != NULL)
6319 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6320
6321 win32_build_env(opt->jo_env, &ga_env, TRUE);
6322 env_wchar = ga_env.ga_data;
6323
6324 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6325 goto failed;
6326 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6327 goto failed;
6328
6329 consize.X = term->tl_cols;
6330 consize.Y = term->tl_rows;
6331 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6332 &term->tl_conpty);
6333 if (FAILED(hr))
6334 goto failed;
6335
6336 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6337
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006338 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006339 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006340 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006341 if (!term->tl_siex.lpAttributeList)
6342 goto failed;
6343 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6344 0, &breq))
6345 goto failed;
6346 if (!pUpdateProcThreadAttribute(
6347 term->tl_siex.lpAttributeList, 0,
6348 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6349 sizeof(HPCON), NULL, NULL))
6350 goto failed;
6351
6352 channel = add_channel();
6353 if (channel == NULL)
6354 goto failed;
6355
6356 job = job_alloc();
6357 if (job == NULL)
6358 goto failed;
6359 if (argvar->v_type == VAR_STRING)
6360 {
6361 int argc;
6362
6363 build_argv_from_string(cmd, &job->jv_argv, &argc);
6364 }
6365 else
6366 {
6367 int argc;
6368
6369 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6370 }
6371
6372 if (opt->jo_set & JO_IN_BUF)
6373 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6374
6375 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6376 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006377 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006378 env_wchar, cwd_wchar,
6379 &term->tl_siex.StartupInfo, &proc_info))
6380 goto failed;
6381
6382 CloseHandle(i_theirs);
6383 CloseHandle(o_theirs);
6384
6385 channel_set_pipes(channel,
6386 (sock_T)i_ours,
6387 (sock_T)o_ours,
6388 (sock_T)o_ours);
6389
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006390 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006391 channel->ch_write_text_mode = TRUE;
6392
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006393 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006394 channel->ch_anonymous_pipe = TRUE;
6395
6396 jo = CreateJobObject(NULL, NULL);
6397 if (jo == NULL)
6398 goto failed;
6399
6400 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6401 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006402 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006403 CloseHandle(jo);
6404 jo = NULL;
6405 }
6406
6407 ResumeThread(proc_info.hThread);
6408 CloseHandle(proc_info.hThread);
6409
6410 vim_free(cmd_wchar);
6411 vim_free(cmd_wchar_copy);
6412 vim_free(cwd_wchar);
6413 vim_free(env_wchar);
6414
6415 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6416 goto failed;
6417
6418#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6419 if (opt->jo_set2 & JO2_ANSI_COLORS)
6420 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6421 else
6422 init_vterm_ansi_colors(term->tl_vterm);
6423#endif
6424
6425 channel_set_job(channel, job, opt);
6426 job_set_options(job, opt);
6427
6428 job->jv_channel = channel;
6429 job->jv_proc_info = proc_info;
6430 job->jv_job_object = jo;
6431 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006432 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006433 ++job->jv_refcount;
6434 term->tl_job = job;
6435
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006436 // Redirecting stdout and stderr doesn't work at the job level. Instead
6437 // open the file here and handle it in. opt->jo_io was changed in
6438 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006439 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6440 {
6441 char_u *fname = opt->jo_io_name[PART_OUT];
6442
6443 ch_log(channel, "Opening output file %s", fname);
6444 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6445 if (term->tl_out_fd == NULL)
6446 semsg(_(e_notopen), fname);
6447 }
6448
6449 return OK;
6450
6451failed:
6452 ga_clear(&ga_cmd);
6453 ga_clear(&ga_env);
6454 vim_free(cmd_wchar);
6455 vim_free(cmd_wchar_copy);
6456 vim_free(cwd_wchar);
6457 if (channel != NULL)
6458 channel_clear(channel);
6459 if (job != NULL)
6460 {
6461 job->jv_channel = NULL;
6462 job_cleanup(job);
6463 }
6464 term->tl_job = NULL;
6465 if (jo != NULL)
6466 CloseHandle(jo);
6467
6468 if (term->tl_siex.lpAttributeList != NULL)
6469 {
6470 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6471 vim_free(term->tl_siex.lpAttributeList);
6472 }
6473 term->tl_siex.lpAttributeList = NULL;
6474 if (o_theirs != NULL)
6475 CloseHandle(o_theirs);
6476 if (o_ours != NULL)
6477 CloseHandle(o_ours);
6478 if (i_ours != NULL)
6479 CloseHandle(i_ours);
6480 if (i_theirs != NULL)
6481 CloseHandle(i_theirs);
6482 if (term->tl_conpty != NULL)
6483 pClosePseudoConsole(term->tl_conpty);
6484 term->tl_conpty = NULL;
6485 return FAIL;
6486}
6487
6488 static void
6489conpty_term_report_winsize(term_T *term, int rows, int cols)
6490{
6491 COORD consize;
6492
6493 consize.X = cols;
6494 consize.Y = rows;
6495 pResizePseudoConsole(term->tl_conpty, consize);
6496}
6497
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006498 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006499term_free_conpty(term_T *term)
6500{
6501 if (term->tl_siex.lpAttributeList != NULL)
6502 {
6503 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6504 vim_free(term->tl_siex.lpAttributeList);
6505 }
6506 term->tl_siex.lpAttributeList = NULL;
6507 if (term->tl_conpty != NULL)
6508 pClosePseudoConsole(term->tl_conpty);
6509 term->tl_conpty = NULL;
6510}
6511
6512 int
6513use_conpty(void)
6514{
6515 return has_conpty;
6516}
6517
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006518# ifndef PROTO
6519
6520#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6521#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006522#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006523
6524void* (*winpty_config_new)(UINT64, void*);
6525void* (*winpty_open)(void*, void*);
6526void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6527BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6528void (*winpty_config_set_mouse_mode)(void*, int);
6529void (*winpty_config_set_initial_size)(void*, int, int);
6530LPCWSTR (*winpty_conin_name)(void*);
6531LPCWSTR (*winpty_conout_name)(void*);
6532LPCWSTR (*winpty_conerr_name)(void*);
6533void (*winpty_free)(void*);
6534void (*winpty_config_free)(void*);
6535void (*winpty_spawn_config_free)(void*);
6536void (*winpty_error_free)(void*);
6537LPCWSTR (*winpty_error_msg)(void*);
6538BOOL (*winpty_set_size)(void*, int, int, void*);
6539HANDLE (*winpty_agent_process)(void*);
6540
6541#define WINPTY_DLL "winpty.dll"
6542
6543static HINSTANCE hWinPtyDLL = NULL;
6544# endif
6545
6546 static int
6547dyn_winpty_init(int verbose)
6548{
6549 int i;
6550 static struct
6551 {
6552 char *name;
6553 FARPROC *ptr;
6554 } winpty_entry[] =
6555 {
6556 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6557 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6558 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6559 {"winpty_config_set_mouse_mode",
6560 (FARPROC*)&winpty_config_set_mouse_mode},
6561 {"winpty_config_set_initial_size",
6562 (FARPROC*)&winpty_config_set_initial_size},
6563 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6564 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6565 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6566 {"winpty_free", (FARPROC*)&winpty_free},
6567 {"winpty_open", (FARPROC*)&winpty_open},
6568 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6569 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6570 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6571 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6572 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6573 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6574 {NULL, NULL}
6575 };
6576
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006577 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006578 if (hWinPtyDLL)
6579 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006580 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6581 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006582 if (*p_winptydll != NUL)
6583 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6584 if (!hWinPtyDLL)
6585 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6586 if (!hWinPtyDLL)
6587 {
6588 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006589 semsg(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006590 : (char_u *)WINPTY_DLL);
6591 return FAIL;
6592 }
6593 for (i = 0; winpty_entry[i].name != NULL
6594 && winpty_entry[i].ptr != NULL; ++i)
6595 {
6596 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6597 winpty_entry[i].name)) == NULL)
6598 {
6599 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006600 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006601 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006602 return FAIL;
6603 }
6604 }
6605
6606 return OK;
6607}
6608
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006609 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006610winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006611 term_T *term,
6612 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006613 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006614 jobopt_T *opt,
6615 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006616{
6617 WCHAR *cmd_wchar = NULL;
6618 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006619 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006620 channel_T *channel = NULL;
6621 job_T *job = NULL;
6622 DWORD error;
6623 HANDLE jo = NULL;
6624 HANDLE child_process_handle;
6625 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006626 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006627 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006628 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006629 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006630
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006631 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6632 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006633
6634 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006635 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006636 cmd = argvar->vval.v_string;
6637 }
6638 else if (argvar->v_type == VAR_LIST)
6639 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006640 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006641 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006642 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006643 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006644 if (cmd == NULL || *cmd == NUL)
6645 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006646 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006647 goto failed;
6648 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006649
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006650 term->tl_arg0_cmd = vim_strsave(cmd);
6651
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006652 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006653 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006654 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006655 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006656 if (opt->jo_cwd != NULL)
6657 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006658
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006659 win32_build_env(opt->jo_env, &ga_env, TRUE);
6660 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006661
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006662 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6663 if (term->tl_winpty_config == NULL)
6664 goto failed;
6665
6666 winpty_config_set_mouse_mode(term->tl_winpty_config,
6667 WINPTY_MOUSE_MODE_FORCE);
6668 winpty_config_set_initial_size(term->tl_winpty_config,
6669 term->tl_cols, term->tl_rows);
6670 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6671 if (term->tl_winpty == NULL)
6672 goto failed;
6673
6674 spawn_config = winpty_spawn_config_new(
6675 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6676 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6677 NULL,
6678 cmd_wchar,
6679 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006680 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006681 &winpty_err);
6682 if (spawn_config == NULL)
6683 goto failed;
6684
6685 channel = add_channel();
6686 if (channel == NULL)
6687 goto failed;
6688
6689 job = job_alloc();
6690 if (job == NULL)
6691 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006692 if (argvar->v_type == VAR_STRING)
6693 {
6694 int argc;
6695
6696 build_argv_from_string(cmd, &job->jv_argv, &argc);
6697 }
6698 else
6699 {
6700 int argc;
6701
6702 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6703 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006704
6705 if (opt->jo_set & JO_IN_BUF)
6706 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6707
6708 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
6709 &child_thread_handle, &error, &winpty_err))
6710 goto failed;
6711
6712 channel_set_pipes(channel,
6713 (sock_T)CreateFileW(
6714 winpty_conin_name(term->tl_winpty),
6715 GENERIC_WRITE, 0, NULL,
6716 OPEN_EXISTING, 0, NULL),
6717 (sock_T)CreateFileW(
6718 winpty_conout_name(term->tl_winpty),
6719 GENERIC_READ, 0, NULL,
6720 OPEN_EXISTING, 0, NULL),
6721 (sock_T)CreateFileW(
6722 winpty_conerr_name(term->tl_winpty),
6723 GENERIC_READ, 0, NULL,
6724 OPEN_EXISTING, 0, NULL));
6725
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006726 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006727 channel->ch_write_text_mode = TRUE;
6728
6729 jo = CreateJobObject(NULL, NULL);
6730 if (jo == NULL)
6731 goto failed;
6732
6733 if (!AssignProcessToJobObject(jo, child_process_handle))
6734 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006735 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006736 CloseHandle(jo);
6737 jo = NULL;
6738 }
6739
6740 winpty_spawn_config_free(spawn_config);
6741 vim_free(cmd_wchar);
6742 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006743 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006744
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006745 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6746 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006747
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006748#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6749 if (opt->jo_set2 & JO2_ANSI_COLORS)
6750 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6751 else
6752 init_vterm_ansi_colors(term->tl_vterm);
6753#endif
6754
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006755 channel_set_job(channel, job, opt);
6756 job_set_options(job, opt);
6757
6758 job->jv_channel = channel;
6759 job->jv_proc_info.hProcess = child_process_handle;
6760 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
6761 job->jv_job_object = jo;
6762 job->jv_status = JOB_STARTED;
6763 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006764 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006765 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006766 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006767 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006768 ++job->jv_refcount;
6769 term->tl_job = job;
6770
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006771 // Redirecting stdout and stderr doesn't work at the job level. Instead
6772 // open the file here and handle it in. opt->jo_io was changed in
6773 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006774 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6775 {
6776 char_u *fname = opt->jo_io_name[PART_OUT];
6777
6778 ch_log(channel, "Opening output file %s", fname);
6779 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6780 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006781 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006782 }
6783
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006784 return OK;
6785
6786failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006787 ga_clear(&ga_cmd);
6788 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006789 vim_free(cmd_wchar);
6790 vim_free(cwd_wchar);
6791 if (spawn_config != NULL)
6792 winpty_spawn_config_free(spawn_config);
6793 if (channel != NULL)
6794 channel_clear(channel);
6795 if (job != NULL)
6796 {
6797 job->jv_channel = NULL;
6798 job_cleanup(job);
6799 }
6800 term->tl_job = NULL;
6801 if (jo != NULL)
6802 CloseHandle(jo);
6803 if (term->tl_winpty != NULL)
6804 winpty_free(term->tl_winpty);
6805 term->tl_winpty = NULL;
6806 if (term->tl_winpty_config != NULL)
6807 winpty_config_free(term->tl_winpty_config);
6808 term->tl_winpty_config = NULL;
6809 if (winpty_err != NULL)
6810 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006811 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006812 (short_u *)winpty_error_msg(winpty_err), NULL);
6813
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006814 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006815 winpty_error_free(winpty_err);
6816 }
6817 return FAIL;
6818}
6819
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006820/*
6821 * Create a new terminal of "rows" by "cols" cells.
6822 * Store a reference in "term".
6823 * Return OK or FAIL.
6824 */
6825 static int
6826term_and_job_init(
6827 term_T *term,
6828 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01006829 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006830 jobopt_T *opt,
6831 jobopt_T *orig_opt)
6832{
6833 int use_winpty = FALSE;
6834 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006835 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006836
6837 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
6838 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
6839
6840 if (!has_winpty && !has_conpty)
6841 // If neither is available give the errors for winpty, since when
6842 // conpty is not available it can't be installed either.
6843 return dyn_winpty_init(TRUE);
6844
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006845 if (opt->jo_tty_type != NUL)
6846 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006847
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006848 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006849 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006850 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006851 use_conpty = TRUE;
6852 else if (has_winpty)
6853 use_winpty = TRUE;
6854 // else: error
6855 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006856 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006857 {
6858 if (has_winpty)
6859 use_winpty = TRUE;
6860 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006861 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006862 {
6863 if (has_conpty)
6864 use_conpty = TRUE;
6865 else
6866 return dyn_conpty_init(TRUE);
6867 }
6868
6869 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006870 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006871
6872 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006873 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006874
6875 // error
6876 return dyn_winpty_init(TRUE);
6877}
6878
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006879 static int
6880create_pty_only(term_T *term, jobopt_T *options)
6881{
6882 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
6883 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
6884 char in_name[80], out_name[80];
6885 channel_T *channel = NULL;
6886
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006887 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6888 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006889
6890 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
6891 GetCurrentProcessId(),
6892 curbuf->b_fnum);
6893 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
6894 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6895 PIPE_UNLIMITED_INSTANCES,
6896 0, 0, NMPWAIT_NOWAIT, NULL);
6897 if (hPipeIn == INVALID_HANDLE_VALUE)
6898 goto failed;
6899
6900 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
6901 GetCurrentProcessId(),
6902 curbuf->b_fnum);
6903 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
6904 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6905 PIPE_UNLIMITED_INSTANCES,
6906 0, 0, 0, NULL);
6907 if (hPipeOut == INVALID_HANDLE_VALUE)
6908 goto failed;
6909
6910 ConnectNamedPipe(hPipeIn, NULL);
6911 ConnectNamedPipe(hPipeOut, NULL);
6912
6913 term->tl_job = job_alloc();
6914 if (term->tl_job == NULL)
6915 goto failed;
6916 ++term->tl_job->jv_refcount;
6917
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006918 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006919 term->tl_job->jv_status = JOB_FINISHED;
6920
6921 channel = add_channel();
6922 if (channel == NULL)
6923 goto failed;
6924 term->tl_job->jv_channel = channel;
6925 channel->ch_keep_open = TRUE;
6926 channel->ch_named_pipe = TRUE;
6927
6928 channel_set_pipes(channel,
6929 (sock_T)hPipeIn,
6930 (sock_T)hPipeOut,
6931 (sock_T)hPipeOut);
6932 channel_set_job(channel, term->tl_job, options);
6933 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
6934 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
6935
6936 return OK;
6937
6938failed:
6939 if (hPipeIn != NULL)
6940 CloseHandle(hPipeIn);
6941 if (hPipeOut != NULL)
6942 CloseHandle(hPipeOut);
6943 return FAIL;
6944}
6945
6946/*
6947 * Free the terminal emulator part of "term".
6948 */
6949 static void
6950term_free_vterm(term_T *term)
6951{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006952 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006953 if (term->tl_winpty != NULL)
6954 winpty_free(term->tl_winpty);
6955 term->tl_winpty = NULL;
6956 if (term->tl_winpty_config != NULL)
6957 winpty_config_free(term->tl_winpty_config);
6958 term->tl_winpty_config = NULL;
6959 if (term->tl_vterm != NULL)
6960 vterm_free(term->tl_vterm);
6961 term->tl_vterm = NULL;
6962}
6963
6964/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02006965 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006966 */
6967 static void
6968term_report_winsize(term_T *term, int rows, int cols)
6969{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006970 if (term->tl_conpty)
6971 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006972 if (term->tl_winpty)
6973 winpty_set_size(term->tl_winpty, cols, rows, NULL);
6974}
6975
6976 int
6977terminal_enabled(void)
6978{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006979 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006980}
6981
6982# else
6983
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006984///////////////////////////////////////
6985// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006986
6987/*
6988 * Create a new terminal of "rows" by "cols" cells.
6989 * Start job for "cmd".
6990 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01006991 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006992 * Return OK or FAIL.
6993 */
6994 static int
6995term_and_job_init(
6996 term_T *term,
6997 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01006998 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006999 jobopt_T *opt,
7000 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007001{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007002 term->tl_arg0_cmd = NULL;
7003
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007004 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7005 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007006
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02007007#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
7008 if (opt->jo_set2 & JO2_ANSI_COLORS)
7009 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
7010 else
7011 init_vterm_ansi_colors(term->tl_vterm);
7012#endif
7013
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007014 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01007015 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007016 if (term->tl_job != NULL)
7017 ++term->tl_job->jv_refcount;
7018
7019 return term->tl_job != NULL
7020 && term->tl_job->jv_channel != NULL
7021 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
7022}
7023
7024 static int
7025create_pty_only(term_T *term, jobopt_T *opt)
7026{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01007027 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
7028 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007029
7030 term->tl_job = job_alloc();
7031 if (term->tl_job == NULL)
7032 return FAIL;
7033 ++term->tl_job->jv_refcount;
7034
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007035 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007036 term->tl_job->jv_status = JOB_FINISHED;
7037
7038 return mch_create_pty_channel(term->tl_job, opt);
7039}
7040
7041/*
7042 * Free the terminal emulator part of "term".
7043 */
7044 static void
7045term_free_vterm(term_T *term)
7046{
7047 if (term->tl_vterm != NULL)
7048 vterm_free(term->tl_vterm);
7049 term->tl_vterm = NULL;
7050}
7051
7052/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02007053 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007054 */
7055 static void
7056term_report_winsize(term_T *term, int rows, int cols)
7057{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007058 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007059 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7060 {
7061 int fd = -1;
7062 int part;
7063
7064 for (part = PART_OUT; part < PART_COUNT; ++part)
7065 {
7066 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007067 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007068 break;
7069 }
7070 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7071 mch_signal_job(term->tl_job, (char_u *)"winch");
7072 }
7073}
7074
7075# endif
7076
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007077#endif // FEAT_TERMINAL