blob: 5774dba9a2e0dab1e78f9198f69c1be55ed08350 [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;
165};
166
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100167#define TMODE_ONCE 1 // CTRL-\ CTRL-N used
168#define TMODE_LOOP 2 // CTRL-W N used
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200169
170/*
171 * List of all active terminals.
172 */
173static term_T *first_term = NULL;
174
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100175// Terminal active in terminal_loop().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200176static term_T *in_terminal_loop = NULL;
177
Bram Moolenaar4f974752019-02-17 17:44:42 +0100178#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100179static BOOL has_winpty = FALSE;
180static BOOL has_conpty = FALSE;
181#endif
182
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100183#define MAX_ROW 999999 // used for tl_dirty_row_end to update all rows
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200184#define KEY_BUF_LEN 200
185
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200186#define FOR_ALL_TERMS(term) \
187 for ((term) = first_term; (term) != NULL; (term) = (term)->tl_next)
188
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200189/*
190 * Functions with separate implementation for MS-Windows and Unix-like systems.
191 */
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200192static 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 +0200193static int create_pty_only(term_T *term, jobopt_T *opt);
194static void term_report_winsize(term_T *term, int rows, int cols);
195static void term_free_vterm(term_T *term);
Bram Moolenaar13568252018-03-16 20:46:58 +0100196#ifdef FEAT_GUI
197static void update_system_term(term_T *term);
198#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200199
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100200static void handle_postponed_scrollback(term_T *term);
201
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100202// The character that we know (or assume) that the terminal expects for the
203// backspace key.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200204static int term_backspace_char = BS;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200205
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100206// "Terminal" highlight group colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +0100207static int term_default_cterm_fg = -1;
208static int term_default_cterm_bg = -1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200209
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100210// Store the last set and the desired cursor properties, so that we only update
211// them when needed. Doing it unnecessary may result in flicker.
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200212static char_u *last_set_cursor_color = NULL;
213static char_u *desired_cursor_color = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +0100214static int last_set_cursor_shape = -1;
215static int desired_cursor_shape = -1;
216static int last_set_cursor_blink = -1;
217static int desired_cursor_blink = -1;
218
219
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100220///////////////////////////////////////
221// 1. Generic code for all systems.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200222
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200223 static int
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200224cursor_color_equal(char_u *lhs_color, char_u *rhs_color)
225{
226 if (lhs_color != NULL && rhs_color != NULL)
227 return STRCMP(lhs_color, rhs_color) == 0;
228 return lhs_color == NULL && rhs_color == NULL;
229}
230
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200231 static void
232cursor_color_copy(char_u **to_color, char_u *from_color)
233{
234 // Avoid a free & alloc if the value is already right.
235 if (cursor_color_equal(*to_color, from_color))
236 return;
237 vim_free(*to_color);
238 *to_color = (from_color == NULL) ? NULL : vim_strsave(from_color);
239}
240
241 static char_u *
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200242cursor_color_get(char_u *color)
243{
244 return (color == NULL) ? (char_u *)"" : color;
245}
246
247
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200248/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200249 * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
Bram Moolenaar498c2562018-04-15 23:45:15 +0200250 * current window.
251 * Sets "rows" and/or "cols" to zero when it should follow the window size.
252 * Return TRUE if the size is the minimum size: "24*80".
253 */
254 static int
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200255parse_termwinsize(win_T *wp, int *rows, int *cols)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200256{
257 int minsize = FALSE;
258
259 *rows = 0;
260 *cols = 0;
261
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200262 if (*wp->w_p_tws != NUL)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200263 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200264 char_u *p = vim_strchr(wp->w_p_tws, 'x');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200265
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100266 // Syntax of value was already checked when it's set.
Bram Moolenaar498c2562018-04-15 23:45:15 +0200267 if (p == NULL)
268 {
269 minsize = TRUE;
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200270 p = vim_strchr(wp->w_p_tws, '*');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200271 }
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200272 *rows = atoi((char *)wp->w_p_tws);
Bram Moolenaar498c2562018-04-15 23:45:15 +0200273 *cols = atoi((char *)p + 1);
274 }
275 return minsize;
276}
277
278/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200279 * Determine the terminal size from 'termwinsize' and the current window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200280 */
281 static void
282set_term_and_win_size(term_T *term)
283{
Bram Moolenaar13568252018-03-16 20:46:58 +0100284#ifdef FEAT_GUI
285 if (term->tl_system)
286 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100287 // Use the whole screen for the system command. However, it will start
288 // at the command line and scroll up as needed, using tl_toprow.
Bram Moolenaar13568252018-03-16 20:46:58 +0100289 term->tl_rows = Rows;
290 term->tl_cols = Columns;
Bram Moolenaar07b46af2018-04-10 14:56:18 +0200291 return;
Bram Moolenaar13568252018-03-16 20:46:58 +0100292 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100293#endif
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200294 if (parse_termwinsize(curwin, &term->tl_rows, &term->tl_cols))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200295 {
Bram Moolenaar498c2562018-04-15 23:45:15 +0200296 if (term->tl_rows != 0)
297 term->tl_rows = MAX(term->tl_rows, curwin->w_height);
298 if (term->tl_cols != 0)
299 term->tl_cols = MAX(term->tl_cols, curwin->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200300 }
301 if (term->tl_rows == 0)
302 term->tl_rows = curwin->w_height;
303 else
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200304 win_setheight_win(term->tl_rows, curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200305 if (term->tl_cols == 0)
306 term->tl_cols = curwin->w_width;
307 else
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200308 win_setwidth_win(term->tl_cols, curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200309}
310
311/*
312 * Initialize job options for a terminal job.
313 * Caller may overrule some of them.
314 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100315 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200316init_job_options(jobopt_T *opt)
317{
318 clear_job_options(opt);
319
320 opt->jo_mode = MODE_RAW;
321 opt->jo_out_mode = MODE_RAW;
322 opt->jo_err_mode = MODE_RAW;
323 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
324}
325
326/*
327 * Set job options mandatory for a terminal job.
328 */
329 static void
330setup_job_options(jobopt_T *opt, int rows, int cols)
331{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100332#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100333 // Win32: Redirecting the job output won't work, thus always connect stdout
334 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200335 if (!(opt->jo_set & JO_OUT_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200336#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200337 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100338 // Connect stdout to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200339 opt->jo_io[PART_OUT] = JIO_BUFFER;
340 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
341 opt->jo_modifiable[PART_OUT] = 0;
342 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
343 }
344
Bram Moolenaar4f974752019-02-17 17:44:42 +0100345#ifndef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100346 // Win32: Redirecting the job output won't work, thus always connect stderr
347 // here.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200348 if (!(opt->jo_set & JO_ERR_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200349#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200350 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100351 // Connect stderr to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200352 opt->jo_io[PART_ERR] = JIO_BUFFER;
353 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
354 opt->jo_modifiable[PART_ERR] = 0;
355 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
356 }
357
358 opt->jo_pty = TRUE;
359 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
360 opt->jo_term_rows = rows;
361 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
362 opt->jo_term_cols = cols;
363}
364
365/*
Bram Moolenaar5c381eb2019-06-25 06:50:31 +0200366 * Flush messages on channels.
367 */
368 static void
369term_flush_messages()
370{
371 mch_check_messages();
372 parse_queued_messages();
373}
374
375/*
Bram Moolenaard96ff162018-02-18 22:13:29 +0100376 * Close a terminal buffer (and its window). Used when creating the terminal
377 * fails.
378 */
379 static void
380term_close_buffer(buf_T *buf, buf_T *old_curbuf)
381{
382 free_terminal(buf);
383 if (old_curbuf != NULL)
384 {
385 --curbuf->b_nwindows;
386 curbuf = old_curbuf;
387 curwin->w_buffer = curbuf;
388 ++curbuf->b_nwindows;
389 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100390 CHECK_CURBUF;
Bram Moolenaard96ff162018-02-18 22:13:29 +0100391
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100392 // Wiping out the buffer will also close the window and call
393 // free_terminal().
Bram Moolenaard96ff162018-02-18 22:13:29 +0100394 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
395}
396
397/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200398 * Start a terminal window and return its buffer.
Bram Moolenaar13568252018-03-16 20:46:58 +0100399 * Use either "argvar" or "argv", the other must be NULL.
400 * When "flags" has TERM_START_NOJOB only create the buffer, b_term and open
401 * the window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200402 * Returns NULL when failed.
403 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100404 buf_T *
405term_start(
406 typval_T *argvar,
407 char **argv,
408 jobopt_T *opt,
409 int flags)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200410{
411 exarg_T split_ea;
412 win_T *old_curwin = curwin;
413 term_T *term;
414 buf_T *old_curbuf = NULL;
415 int res;
416 buf_T *newbuf;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100417 int vertical = opt->jo_vertical || (cmdmod.split & WSP_VERT);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200418 jobopt_T orig_opt; // only partly filled
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200419
420 if (check_restricted() || check_secure())
421 return NULL;
422
423 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
424 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
425 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
Bram Moolenaarb0992022020-01-30 14:55:42 +0100426 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF))
427 || (argvar != NULL
428 && argvar->v_type == VAR_LIST
429 && argvar->vval.v_list != NULL
430 && argvar->vval.v_list->lv_first == &range_list_item))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200431 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100432 emsg(_(e_invarg));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200433 return NULL;
434 }
435
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200436 term = ALLOC_CLEAR_ONE(term_T);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200437 if (term == NULL)
438 return NULL;
439 term->tl_dirty_row_end = MAX_ROW;
440 term->tl_cursor_visible = TRUE;
441 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
442 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100443#ifdef FEAT_GUI
444 term->tl_system = (flags & TERM_START_SYSTEM);
445#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200446 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100447 ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200448
Bram Moolenaara80faa82020-04-12 19:37:17 +0200449 CLEAR_FIELD(split_ea);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200450 if (opt->jo_curwin)
451 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100452 // Create a new buffer in the current window.
Bram Moolenaar13568252018-03-16 20:46:58 +0100453 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200454 {
455 no_write_message();
456 vim_free(term);
457 return NULL;
458 }
459 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaar13568252018-03-16 20:46:58 +0100460 ECMD_HIDE
461 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
462 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200463 {
464 vim_free(term);
465 return NULL;
466 }
467 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100468 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200469 {
470 buf_T *buf;
471
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100472 // Create a new buffer without a window. Make it the current buffer for
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100473 // a moment to be able to do the initializations.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200474 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
475 BLN_NEW | BLN_LISTED);
476 if (buf == NULL || ml_open(buf) == FAIL)
477 {
478 vim_free(term);
479 return NULL;
480 }
481 old_curbuf = curbuf;
482 --curbuf->b_nwindows;
483 curbuf = buf;
484 curwin->w_buffer = buf;
485 ++curbuf->b_nwindows;
486 }
487 else
488 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100489 // Open a new window or tab.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200490 split_ea.cmdidx = CMD_new;
491 split_ea.cmd = (char_u *)"new";
492 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100493 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200494 {
495 split_ea.line2 = opt->jo_term_rows;
496 split_ea.addr_count = 1;
497 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100498 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200499 {
500 split_ea.line2 = opt->jo_term_cols;
501 split_ea.addr_count = 1;
502 }
503
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100504 if (vertical)
505 cmdmod.split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200506 ex_splitview(&split_ea);
507 if (curwin == old_curwin)
508 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100509 // split failed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200510 vim_free(term);
511 return NULL;
512 }
513 }
514 term->tl_buffer = curbuf;
515 curbuf->b_term = term;
516
517 if (!opt->jo_hidden)
518 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100519 // Only one size was taken care of with :new, do the other one. With
520 // "curwin" both need to be done.
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100521 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200522 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100523 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200524 win_setwidth(opt->jo_term_cols);
525 }
526
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100527 // Link the new terminal in the list of active terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200528 term->tl_next = first_term;
529 first_term = term;
530
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100531 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
532
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200533 if (opt->jo_term_name != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100534 {
535 vim_free(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200536 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100537 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100538 else if (argv != NULL)
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100539 {
540 vim_free(curbuf->b_ffname);
Bram Moolenaar13568252018-03-16 20:46:58 +0100541 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaard5bc32d2020-03-22 19:25:50 +0100542 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200543 else
544 {
545 int i;
546 size_t len;
547 char_u *cmd, *p;
548
549 if (argvar->v_type == VAR_STRING)
550 {
551 cmd = argvar->vval.v_string;
552 if (cmd == NULL)
553 cmd = (char_u *)"";
554 else if (STRCMP(cmd, "NONE") == 0)
555 cmd = (char_u *)"pty";
556 }
557 else if (argvar->v_type != VAR_LIST
558 || argvar->vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +0100559 || argvar->vval.v_list->lv_len == 0
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100560 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200561 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
562 cmd = (char_u*)"";
563
564 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200565 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200566
567 for (i = 0; p != NULL; ++i)
568 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100569 // Prepend a ! to the command name to avoid the buffer name equals
570 // the executable, otherwise ":w!" would overwrite it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200571 if (i == 0)
572 vim_snprintf((char *)p, len, "!%s", cmd);
573 else
574 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
575 if (buflist_findname(p) == NULL)
576 {
577 vim_free(curbuf->b_ffname);
578 curbuf->b_ffname = p;
579 break;
580 }
581 }
582 }
Bram Moolenaare010c722020-02-24 21:37:54 +0100583 vim_free(curbuf->b_sfname);
584 curbuf->b_sfname = vim_strsave(curbuf->b_ffname);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200585 curbuf->b_fname = curbuf->b_ffname;
586
Bram Moolenaar5e94a292020-03-19 18:46:57 +0100587 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
588
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200589 if (opt->jo_term_opencmd != NULL)
590 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
591
592 if (opt->jo_eof_chars != NULL)
593 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
594
595 set_string_option_direct((char_u *)"buftype", -1,
596 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200597 // Avoid that 'buftype' is reset when this buffer is entered.
598 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200599
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100600 // Mark the buffer as not modifiable. It can only be made modifiable after
601 // the job finished.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200602 curbuf->b_p_ma = FALSE;
603
604 set_term_and_win_size(term);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100605#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200606 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
607#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200608 setup_job_options(opt, term->tl_rows, term->tl_cols);
609
Bram Moolenaar13568252018-03-16 20:46:58 +0100610 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100611 return curbuf;
612
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100613#if defined(FEAT_SESSION)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100614 // Remember the command for the session file.
Bram Moolenaar13568252018-03-16 20:46:58 +0100615 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100616 term->tl_command = vim_strsave((char_u *)"NONE");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100617 else if (argvar->v_type == VAR_STRING)
618 {
619 char_u *cmd = argvar->vval.v_string;
620
621 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
622 term->tl_command = vim_strsave(cmd);
623 }
624 else if (argvar->v_type == VAR_LIST
625 && argvar->vval.v_list != NULL
626 && argvar->vval.v_list->lv_len > 0)
627 {
628 garray_T ga;
629 listitem_T *item;
630
631 ga_init2(&ga, 1, 100);
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200632 FOR_ALL_LIST_ITEMS(argvar->vval.v_list, item)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100633 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100634 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100635 char_u *p;
636
637 if (s == NULL)
638 break;
639 p = vim_strsave_fnameescape(s, FALSE);
640 if (p == NULL)
641 break;
642 ga_concat(&ga, p);
643 vim_free(p);
644 ga_append(&ga, ' ');
645 }
646 if (item == NULL)
647 {
648 ga_append(&ga, NUL);
649 term->tl_command = ga.ga_data;
650 }
651 else
652 ga_clear(&ga);
653 }
654#endif
655
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100656 if (opt->jo_term_kill != NULL)
657 {
658 char_u *p = skiptowhite(opt->jo_term_kill);
659
660 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
661 }
662
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200663 if (opt->jo_term_api != NULL)
Bram Moolenaar21109272020-01-30 16:27:20 +0100664 {
665 char_u *p = skiptowhite(opt->jo_term_api);
666
667 term->tl_api = vim_strnsave(opt->jo_term_api, p - opt->jo_term_api);
668 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200669 else
670 term->tl_api = vim_strsave((char_u *)"Tapi_");
671
Bram Moolenaar83d47902020-03-26 20:34:00 +0100672 if (opt->jo_set2 & JO2_TERM_HIGHLIGHT)
673 term->tl_highlight_name = vim_strsave(opt->jo_term_highlight);
674
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100675 // System dependent: setup the vterm and maybe start the job in it.
Bram Moolenaar13568252018-03-16 20:46:58 +0100676 if (argv == NULL
677 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200678 && argvar->vval.v_string != NULL
679 && STRCMP(argvar->vval.v_string, "NONE") == 0)
680 res = create_pty_only(term, opt);
681 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200682 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200683
684 newbuf = curbuf;
685 if (res == OK)
686 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100687 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200688 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
689 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100690#ifdef FEAT_GUI
691 if (term->tl_system)
692 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100693 // display first line below typed command
Bram Moolenaar13568252018-03-16 20:46:58 +0100694 term->tl_toprow = msg_row + 1;
695 term->tl_dirty_row_end = 0;
696 }
697#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200698
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100699 // Make sure we don't get stuck on sending keys to the job, it leads to
700 // a deadlock if the job is waiting for Vim to read.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200701 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
702
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200703 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200704 {
705 --curbuf->b_nwindows;
706 curbuf = old_curbuf;
707 curwin->w_buffer = curbuf;
708 ++curbuf->b_nwindows;
709 }
710 }
711 else
712 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100713 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200714 return NULL;
715 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100716
Bram Moolenaar13568252018-03-16 20:46:58 +0100717 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar28ed4df2019-10-26 16:21:40 +0200718 if (!opt->jo_hidden && !(flags & TERM_START_SYSTEM))
719 apply_autocmds(EVENT_TERMINALWINOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200720 return newbuf;
721}
722
723/*
724 * ":terminal": open a terminal window and execute a job in it.
725 */
726 void
727ex_terminal(exarg_T *eap)
728{
729 typval_T argvar[2];
730 jobopt_T opt;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100731 int opt_shell = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200732 char_u *cmd;
733 char_u *tofree = NULL;
734
735 init_job_options(&opt);
736
737 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100738 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200739 {
740 char_u *p, *ep;
741
742 cmd += 2;
743 p = skiptowhite(cmd);
744 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200745 if (ep != NULL)
746 {
747 if (ep < p)
748 p = ep;
749 else
750 ep = NULL;
751 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200752
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200753# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
754 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
755 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200756 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200757 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100758 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200759 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200760 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200761 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200762 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200763 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200764 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200765 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100766 opt.jo_term_norestore = 1;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100767 else if (OPTARG_HAS("shell"))
768 opt_shell = TRUE;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200769 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100770 {
771 opt.jo_set2 |= JO2_TERM_KILL;
772 opt.jo_term_kill = ep + 1;
773 p = skiptowhite(cmd);
774 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200775 else if (OPTARG_HAS("api"))
776 {
777 opt.jo_set2 |= JO2_TERM_API;
778 if (ep != NULL)
779 {
780 opt.jo_term_api = ep + 1;
781 p = skiptowhite(cmd);
782 }
783 else
784 opt.jo_term_api = NULL;
785 }
786 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200787 {
788 opt.jo_set2 |= JO2_TERM_ROWS;
789 opt.jo_term_rows = atoi((char *)ep + 1);
790 p = skiptowhite(cmd);
791 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200792 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200793 {
794 opt.jo_set2 |= JO2_TERM_COLS;
795 opt.jo_term_cols = atoi((char *)ep + 1);
796 p = skiptowhite(cmd);
797 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200798 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200799 {
800 char_u *buf = NULL;
801 char_u *keys;
802
Bram Moolenaar21109272020-01-30 16:27:20 +0100803 vim_free(opt.jo_eof_chars);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200804 p = skiptowhite(cmd);
805 *p = NUL;
Bram Moolenaar459fd782019-10-13 16:43:39 +0200806 keys = replace_termcodes(ep + 1, &buf,
807 REPTERM_FROM_PART | REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200808 opt.jo_set2 |= JO2_EOF_CHARS;
809 opt.jo_eof_chars = vim_strsave(keys);
810 vim_free(buf);
811 *p = ' ';
812 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100813#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100814 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
815 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100816 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100817 int tty_type = NUL;
818
819 p = skiptowhite(cmd);
820 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
821 tty_type = 'w';
822 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
823 tty_type = 'c';
824 else
825 {
826 semsg(e_invargval, "type");
827 goto theend;
828 }
829 opt.jo_set2 |= JO2_TTY_TYPE;
830 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100831 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100832#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200833 else
834 {
835 if (*p)
836 *p = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100837 semsg(_("E181: Invalid attribute: %s"), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100838 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200839 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200840# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200841 cmd = skipwhite(p);
842 }
843 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100844 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100845 // Make a copy of 'shell', an autocommand may change the option.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200846 tofree = cmd = vim_strsave(p_sh);
847
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100848 // default to close when the shell exits
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100849 if (opt.jo_term_finish == NUL)
Bram Moolenaare2978022020-04-26 14:47:44 +0200850 opt.jo_term_finish = TL_FINISH_CLOSE;
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100851 }
852
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200853 if (eap->addr_count > 0)
854 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100855 // Write lines from current buffer to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200856 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
857 opt.jo_io[PART_IN] = JIO_BUFFER;
858 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
859 opt.jo_in_top = eap->line1;
860 opt.jo_in_bot = eap->line2;
861 }
862
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100863 if (opt_shell && tofree == NULL)
864 {
865#ifdef UNIX
866 char **argv = NULL;
867 char_u *tofree1 = NULL;
868 char_u *tofree2 = NULL;
869
870 // :term ++shell command
871 if (unix_build_argv(cmd, &argv, &tofree1, &tofree2) == OK)
872 term_start(NULL, argv, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaaradf4aa22019-11-10 22:36:44 +0100873 vim_free(argv);
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100874 vim_free(tofree1);
875 vim_free(tofree2);
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100876 goto theend;
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100877#else
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100878# ifdef MSWIN
879 long_u cmdlen = STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10;
880 char_u *newcmd;
881
882 newcmd = alloc(cmdlen);
883 if (newcmd == NULL)
884 goto theend;
885 tofree = newcmd;
886 vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", p_sh, p_shcf, cmd);
887 cmd = newcmd;
888# else
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100889 emsg(_("E279: Sorry, ++shell is not supported on this system"));
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100890 goto theend;
891# endif
Bram Moolenaar197c6b72019-11-03 23:37:12 +0100892#endif
893 }
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100894 argvar[0].v_type = VAR_STRING;
895 argvar[0].vval.v_string = cmd;
896 argvar[1].v_type = VAR_UNKNOWN;
897 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100898
899theend:
Bram Moolenaar2d6d76f2019-11-04 23:18:35 +0100900 vim_free(tofree);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200901 vim_free(opt.jo_eof_chars);
902}
903
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100904#if defined(FEAT_SESSION) || defined(PROTO)
905/*
906 * Write a :terminal command to the session file to restore the terminal in
907 * window "wp".
908 * Return FAIL if writing fails.
909 */
910 int
911term_write_session(FILE *fd, win_T *wp)
912{
913 term_T *term = wp->w_buffer->b_term;
914
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +0100915 // Create the terminal and run the command. This is not without
916 // risk, but let's assume the user only creates a session when this
917 // will be OK.
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100918 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
919 term->tl_cols, term->tl_rows) < 0)
920 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100921#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100922 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
923 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100924#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100925 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
926 return FAIL;
927
928 return put_eol(fd);
929}
930
931/*
932 * Return TRUE if "buf" has a terminal that should be restored.
933 */
934 int
935term_should_restore(buf_T *buf)
936{
937 term_T *term = buf->b_term;
938
939 return term != NULL && (term->tl_command == NULL
940 || STRCMP(term->tl_command, "NONE") != 0);
941}
942#endif
943
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200944/*
945 * Free the scrollback buffer for "term".
946 */
947 static void
948free_scrollback(term_T *term)
949{
950 int i;
951
952 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
953 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
954 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100955 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
956 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
957 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200958}
959
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100960
961// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +0200962static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100963
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200964/*
965 * Free a terminal and everything it refers to.
966 * Kills the job if there is one.
967 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100968 * The actual terminal structure is freed later in free_unused_terminals(),
969 * because callbacks may wipe out a buffer while the terminal is still
970 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200971 */
972 void
973free_terminal(buf_T *buf)
974{
975 term_T *term = buf->b_term;
976 term_T *tp;
977
978 if (term == NULL)
979 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100980
981 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200982 if (first_term == term)
983 first_term = term->tl_next;
984 else
985 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
986 if (tp->tl_next == term)
987 {
988 tp->tl_next = term->tl_next;
989 break;
990 }
991
992 if (term->tl_job != NULL)
993 {
994 if (term->tl_job->jv_status != JOB_ENDED
995 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +0100996 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200997 job_stop(term->tl_job, NULL, "kill");
998 job_unref(term->tl_job);
999 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001000 term->tl_next = terminals_to_free;
1001 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001002
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001003 buf->b_term = NULL;
1004 if (in_terminal_loop == term)
1005 in_terminal_loop = NULL;
1006}
1007
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001008 void
1009free_unused_terminals()
1010{
1011 while (terminals_to_free != NULL)
1012 {
1013 term_T *term = terminals_to_free;
1014
1015 terminals_to_free = term->tl_next;
1016
1017 free_scrollback(term);
1018
1019 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001020 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001021 vim_free(term->tl_title);
1022#ifdef FEAT_SESSION
1023 vim_free(term->tl_command);
1024#endif
1025 vim_free(term->tl_kill);
1026 vim_free(term->tl_status_text);
1027 vim_free(term->tl_opencmd);
1028 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001029 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +01001030#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001031 if (term->tl_out_fd != NULL)
1032 fclose(term->tl_out_fd);
1033#endif
Bram Moolenaar83d47902020-03-26 20:34:00 +01001034 vim_free(term->tl_highlight_name);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001035 vim_free(term->tl_cursor_color);
1036 vim_free(term);
1037 }
1038}
1039
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001040/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001041 * Get the part that is connected to the tty. Normally this is PART_IN, but
1042 * when writing buffer lines to the job it can be another. This makes it
1043 * possible to do "1,5term vim -".
1044 */
1045 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001046get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001047{
1048#ifdef UNIX
1049 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1050 int i;
1051
1052 for (i = 0; i < 3; ++i)
1053 {
1054 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1055
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01001056 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001057 return parts[i];
1058 }
1059#endif
1060 return PART_IN;
1061}
1062
1063/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001064 * Write job output "msg[len]" to the vterm.
1065 */
1066 static void
1067term_write_job_output(term_T *term, char_u *msg, size_t len)
1068{
1069 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001070 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001071
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001072 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001073
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001074 // flush vterm buffer when vterm responded to control sequence
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001075 if (prevlen != vterm_output_get_buffer_current(vterm))
1076 {
1077 char buf[KEY_BUF_LEN];
1078 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1079
1080 if (curlen > 0)
1081 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1082 (char_u *)buf, (int)curlen, NULL);
1083 }
1084
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001085 // this invokes the damage callbacks
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001086 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1087}
1088
1089 static void
1090update_cursor(term_T *term, int redraw)
1091{
1092 if (term->tl_normal_mode)
1093 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001094#ifdef FEAT_GUI
1095 if (term->tl_system)
1096 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1097 term->tl_cursor_pos.col);
1098 else
1099#endif
1100 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001101 if (redraw)
1102 {
1103 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1104 cursor_on();
1105 out_flush();
1106#ifdef FEAT_GUI
1107 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001108 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001109 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001110 gui_mch_flush();
1111 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001112#endif
1113 }
1114}
1115
1116/*
1117 * Invoked when "msg" output from a job was received. Write it to the terminal
1118 * of "buffer".
1119 */
1120 void
1121write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1122{
1123 size_t len = STRLEN(msg);
1124 term_T *term = buffer->b_term;
1125
Bram Moolenaar4f974752019-02-17 17:44:42 +01001126#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001127 // Win32: Cannot redirect output of the job, intercept it here and write to
1128 // the file.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001129 if (term->tl_out_fd != NULL)
1130 {
1131 ch_log(channel, "Writing %d bytes to output file", (int)len);
1132 fwrite(msg, len, 1, term->tl_out_fd);
1133 return;
1134 }
1135#endif
1136
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001137 if (term->tl_vterm == NULL)
1138 {
1139 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1140 return;
1141 }
1142 ch_log(channel, "writing %d bytes to terminal", (int)len);
1143 term_write_job_output(term, msg, len);
1144
Bram Moolenaar13568252018-03-16 20:46:58 +01001145#ifdef FEAT_GUI
1146 if (term->tl_system)
1147 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001148 // show system output, scrolling up the screen as needed
Bram Moolenaar13568252018-03-16 20:46:58 +01001149 update_system_term(term);
1150 update_cursor(term, TRUE);
1151 }
1152 else
1153#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001154 // In Terminal-Normal mode we are displaying the buffer, not the terminal
1155 // contents, thus no screen update is needed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001156 if (!term->tl_normal_mode)
1157 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001158 // Don't use update_screen() when editing the command line, it gets
1159 // cleared.
1160 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001161 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001162 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001163 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001164 update_screen(VALID_NO_UPDATE);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001165 // update_screen() can be slow, check the terminal wasn't closed
1166 // already
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001167 if (buffer == curbuf && curbuf->b_term != NULL)
1168 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001169 }
1170 else
1171 redraw_after_callback(TRUE);
1172 }
1173}
1174
1175/*
1176 * Send a mouse position and click to the vterm
1177 */
1178 static int
1179term_send_mouse(VTerm *vterm, int button, int pressed)
1180{
1181 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001182 int row = mouse_row - W_WINROW(curwin);
1183 int col = mouse_col - curwin->w_wincol;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001184
Bram Moolenaar219c7d02020-02-01 21:57:29 +01001185#ifdef FEAT_PROP_POPUP
1186 if (popup_is_popup(curwin))
1187 {
1188 row -= popup_top_extra(curwin);
1189 col -= popup_left_extra(curwin);
1190 }
1191#endif
1192 vterm_mouse_move(vterm, row, col, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001193 if (button != 0)
1194 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001195 return TRUE;
1196}
1197
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001198static int enter_mouse_col = -1;
1199static int enter_mouse_row = -1;
1200
1201/*
1202 * Handle a mouse click, drag or release.
1203 * Return TRUE when a mouse event is sent to the terminal.
1204 */
1205 static int
1206term_mouse_click(VTerm *vterm, int key)
1207{
1208#if defined(FEAT_CLIPBOARD)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001209 // For modeless selection mouse drag and release events are ignored, unless
1210 // they are preceded with a mouse down event
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001211 static int ignore_drag_release = TRUE;
1212 VTermMouseState mouse_state;
1213
1214 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1215 if (mouse_state.flags == 0)
1216 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001217 // Terminal is not using the mouse, use modeless selection.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001218 switch (key)
1219 {
1220 case K_LEFTDRAG:
1221 case K_LEFTRELEASE:
1222 case K_RIGHTDRAG:
1223 case K_RIGHTRELEASE:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001224 // Ignore drag and release events when the button-down wasn't
1225 // seen before.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001226 if (ignore_drag_release)
1227 {
1228 int save_mouse_col, save_mouse_row;
1229
1230 if (enter_mouse_col < 0)
1231 break;
1232
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001233 // mouse click in the window gave us focus, handle that
1234 // click now
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001235 save_mouse_col = mouse_col;
1236 save_mouse_row = mouse_row;
1237 mouse_col = enter_mouse_col;
1238 mouse_row = enter_mouse_row;
1239 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1240 mouse_col = save_mouse_col;
1241 mouse_row = save_mouse_row;
1242 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001243 // FALLTHROUGH
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001244 case K_LEFTMOUSE:
1245 case K_RIGHTMOUSE:
1246 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1247 ignore_drag_release = TRUE;
1248 else
1249 ignore_drag_release = FALSE;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001250 // Should we call mouse_has() here?
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001251 if (clip_star.available)
1252 {
1253 int button, is_click, is_drag;
1254
1255 button = get_mouse_button(KEY2TERMCAP1(key),
1256 &is_click, &is_drag);
1257 if (mouse_model_popup() && button == MOUSE_LEFT
1258 && (mod_mask & MOD_MASK_SHIFT))
1259 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001260 // Translate shift-left to right button.
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001261 button = MOUSE_RIGHT;
1262 mod_mask &= ~MOD_MASK_SHIFT;
1263 }
1264 clip_modeless(button, is_click, is_drag);
1265 }
1266 break;
1267
1268 case K_MIDDLEMOUSE:
1269 if (clip_star.available)
1270 insert_reg('*', TRUE);
1271 break;
1272 }
1273 enter_mouse_col = -1;
1274 return FALSE;
1275 }
1276#endif
1277 enter_mouse_col = -1;
1278
1279 switch (key)
1280 {
1281 case K_LEFTMOUSE:
1282 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1283 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1284 case K_LEFTRELEASE:
1285 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1286 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1287 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1288 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1289 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1290 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1291 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1292 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1293 }
1294 return TRUE;
1295}
1296
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001297/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001298 * Convert typed key "c" with modifiers "modmask" into bytes to send to the
1299 * job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001300 * Return the number of bytes in "buf".
1301 */
1302 static int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001303term_convert_key(term_T *term, int c, int modmask, char *buf)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001304{
1305 VTerm *vterm = term->tl_vterm;
1306 VTermKey key = VTERM_KEY_NONE;
1307 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001308 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001309
1310 switch (c)
1311 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001312 // don't use VTERM_KEY_ENTER, it may do an unwanted conversion
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001313
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001314 // don't use VTERM_KEY_BACKSPACE, it always
1315 // becomes 0x7f DEL
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001316 case K_BS: c = term_backspace_char; break;
1317
1318 case ESC: key = VTERM_KEY_ESCAPE; break;
1319 case K_DEL: key = VTERM_KEY_DEL; break;
1320 case K_DOWN: key = VTERM_KEY_DOWN; break;
1321 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1322 key = VTERM_KEY_DOWN; break;
1323 case K_END: key = VTERM_KEY_END; break;
1324 case K_S_END: mod = VTERM_MOD_SHIFT;
1325 key = VTERM_KEY_END; break;
1326 case K_C_END: mod = VTERM_MOD_CTRL;
1327 key = VTERM_KEY_END; break;
1328 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1329 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1330 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1331 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1332 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1333 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1334 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1335 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1336 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1337 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1338 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1339 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1340 case K_HOME: key = VTERM_KEY_HOME; break;
1341 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1342 key = VTERM_KEY_HOME; break;
1343 case K_C_HOME: mod = VTERM_MOD_CTRL;
1344 key = VTERM_KEY_HOME; break;
1345 case K_INS: key = VTERM_KEY_INS; break;
1346 case K_K0: key = VTERM_KEY_KP_0; break;
1347 case K_K1: key = VTERM_KEY_KP_1; break;
1348 case K_K2: key = VTERM_KEY_KP_2; break;
1349 case K_K3: key = VTERM_KEY_KP_3; break;
1350 case K_K4: key = VTERM_KEY_KP_4; break;
1351 case K_K5: key = VTERM_KEY_KP_5; break;
1352 case K_K6: key = VTERM_KEY_KP_6; break;
1353 case K_K7: key = VTERM_KEY_KP_7; break;
1354 case K_K8: key = VTERM_KEY_KP_8; break;
1355 case K_K9: key = VTERM_KEY_KP_9; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001356 case K_KDEL: key = VTERM_KEY_DEL; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001357 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001358 case K_KEND: key = VTERM_KEY_KP_1; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001359 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001360 case K_KHOME: key = VTERM_KEY_KP_7; break; // TODO
1361 case K_KINS: key = VTERM_KEY_KP_0; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001362 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1363 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001364 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; // TODO
1365 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; // TODO
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001366 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1367 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1368 case K_LEFT: key = VTERM_KEY_LEFT; break;
1369 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1370 key = VTERM_KEY_LEFT; break;
1371 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1372 key = VTERM_KEY_LEFT; break;
1373 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1374 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1375 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1376 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1377 key = VTERM_KEY_RIGHT; break;
1378 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1379 key = VTERM_KEY_RIGHT; break;
1380 case K_UP: key = VTERM_KEY_UP; break;
1381 case K_S_UP: mod = VTERM_MOD_SHIFT;
1382 key = VTERM_KEY_UP; break;
1383 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001384 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1385 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001386
Bram Moolenaara42ad572017-11-16 13:08:04 +01001387 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1388 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001389 case K_MOUSELEFT: /* TODO */ return 0;
1390 case K_MOUSERIGHT: /* TODO */ return 0;
1391
1392 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001393 case K_LEFTMOUSE_NM:
1394 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001395 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001396 case K_LEFTRELEASE_NM:
1397 case K_MOUSEMOVE:
1398 case K_MIDDLEMOUSE:
1399 case K_MIDDLEDRAG:
1400 case K_MIDDLERELEASE:
1401 case K_RIGHTMOUSE:
1402 case K_RIGHTDRAG:
1403 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1404 return 0;
1405 other = TRUE;
1406 break;
1407
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001408 case K_X1MOUSE: /* TODO */ return 0;
1409 case K_X1DRAG: /* TODO */ return 0;
1410 case K_X1RELEASE: /* TODO */ return 0;
1411 case K_X2MOUSE: /* TODO */ return 0;
1412 case K_X2DRAG: /* TODO */ return 0;
1413 case K_X2RELEASE: /* TODO */ return 0;
1414
1415 case K_IGNORE: return 0;
1416 case K_NOP: return 0;
1417 case K_UNDO: return 0;
1418 case K_HELP: return 0;
1419 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1420 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1421 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1422 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1423 case K_SELECT: return 0;
1424#ifdef FEAT_GUI
1425 case K_VER_SCROLLBAR: return 0;
1426 case K_HOR_SCROLLBAR: return 0;
1427#endif
1428#ifdef FEAT_GUI_TABLINE
1429 case K_TABLINE: return 0;
1430 case K_TABMENU: return 0;
1431#endif
1432#ifdef FEAT_NETBEANS_INTG
1433 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1434#endif
1435#ifdef FEAT_DND
1436 case K_DROP: return 0;
1437#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001438 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001439 case K_PS: vterm_keyboard_start_paste(vterm);
1440 other = TRUE;
1441 break;
1442 case K_PE: vterm_keyboard_end_paste(vterm);
1443 other = TRUE;
1444 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001445 }
1446
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001447 // add modifiers for the typed key
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001448 if (modmask & MOD_MASK_SHIFT)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001449 mod |= VTERM_MOD_SHIFT;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001450 if (modmask & MOD_MASK_CTRL)
Bram Moolenaar459fd782019-10-13 16:43:39 +02001451 mod |= VTERM_MOD_CTRL;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01001452 if (modmask & (MOD_MASK_ALT | MOD_MASK_META))
Bram Moolenaar459fd782019-10-13 16:43:39 +02001453 mod |= VTERM_MOD_ALT;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001454
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001455 /*
1456 * Convert special keys to vterm keys:
1457 * - Write keys to vterm: vterm_keyboard_key()
1458 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001459 */
1460 if (key != VTERM_KEY_NONE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001461 // Special key, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001462 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001463 else if (!other)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001464 // Normal character, let vterm convert it.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001465 vterm_keyboard_unichar(vterm, c, mod);
1466
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001467 // Read back the converted escape sequence.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001468 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1469}
1470
1471/*
1472 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001473 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001474 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001475 */
1476 static int
1477term_job_running_check(term_T *term, int check_job_status)
1478{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001479 // Also consider the job finished when the channel is closed, to avoid a
1480 // race condition when updating the title.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001481 if (term != NULL
1482 && term->tl_job != NULL
1483 && channel_is_open(term->tl_job->jv_channel))
1484 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001485 job_T *job = term->tl_job;
1486
1487 // Careful: Checking the job status may invoked callbacks, which close
1488 // the buffer and terminate "term". However, "job" will not be freed
1489 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001490 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001491 job_status(job);
1492 return (job->jv_status == JOB_STARTED
1493 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001494 }
1495 return FALSE;
1496}
1497
1498/*
1499 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001500 */
1501 int
1502term_job_running(term_T *term)
1503{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001504 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001505}
1506
1507/*
1508 * Return TRUE if "term" has an active channel and used ":term NONE".
1509 */
1510 int
1511term_none_open(term_T *term)
1512{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001513 // Also consider the job finished when the channel is closed, to avoid a
1514 // race condition when updating the title.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001515 return term != NULL
1516 && term->tl_job != NULL
1517 && channel_is_open(term->tl_job->jv_channel)
1518 && term->tl_job->jv_channel->ch_keep_open;
1519}
1520
1521/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001522 * Used when exiting: kill the job in "buf" if so desired.
1523 * Return OK when the job finished.
1524 * Return FAIL when the job is still running.
1525 */
1526 int
1527term_try_stop_job(buf_T *buf)
1528{
1529 int count;
1530 char *how = (char *)buf->b_term->tl_kill;
1531
1532#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1533 if ((how == NULL || *how == NUL) && (p_confirm || cmdmod.confirm))
1534 {
1535 char_u buff[DIALOG_MSG_SIZE];
1536 int ret;
1537
1538 dialog_msg(buff, _("Kill job in \"%s\"?"), buf->b_fname);
1539 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1540 if (ret == VIM_YES)
1541 how = "kill";
1542 else if (ret == VIM_CANCEL)
1543 return FAIL;
1544 }
1545#endif
1546 if (how == NULL || *how == NUL)
1547 return FAIL;
1548
1549 job_stop(buf->b_term->tl_job, NULL, how);
1550
Bram Moolenaar9172d232019-01-29 23:06:54 +01001551 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001552 for (count = 0; count < 100; ++count)
1553 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001554 job_T *job;
1555
1556 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001557 if (!buf_valid(buf)
1558 || buf->b_term == NULL
1559 || buf->b_term->tl_job == NULL)
1560 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001561 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001562
Bram Moolenaar9172d232019-01-29 23:06:54 +01001563 // Call job_status() to update jv_status. It may cause the job to be
1564 // cleaned up but it won't be freed.
1565 job_status(job);
1566 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001567 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001568
Bram Moolenaar8f7ab4b2019-10-23 23:16:45 +02001569 ui_delay(10L, TRUE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001570 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001571 }
1572 return FAIL;
1573}
1574
1575/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001576 * Add the last line of the scrollback buffer to the buffer in the window.
1577 */
1578 static void
1579add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1580{
1581 buf_T *buf = term->tl_buffer;
1582 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1583 linenr_T lnum = buf->b_ml.ml_line_count;
1584
Bram Moolenaar4f974752019-02-17 17:44:42 +01001585#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001586 if (!enc_utf8 && enc_codepage > 0)
1587 {
1588 WCHAR *ret = NULL;
1589 int length = 0;
1590
1591 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1592 &ret, &length);
1593 if (ret != NULL)
1594 {
1595 WideCharToMultiByte_alloc(enc_codepage, 0,
1596 ret, length, (char **)&text, &len, 0, 0);
1597 vim_free(ret);
1598 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1599 vim_free(text);
1600 }
1601 }
1602 else
1603#endif
1604 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1605 if (empty)
1606 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001607 // Delete the empty line that was in the empty buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001608 curbuf = buf;
1609 ml_delete(1, FALSE);
1610 curbuf = curwin->w_buffer;
1611 }
1612}
1613
1614 static void
1615cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1616{
1617 attr->width = cell->width;
1618 attr->attrs = cell->attrs;
1619 attr->fg = cell->fg;
1620 attr->bg = cell->bg;
1621}
1622
1623 static int
1624equal_celattr(cellattr_T *a, cellattr_T *b)
1625{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001626 // Comparing the colors should be sufficient.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001627 return a->fg.red == b->fg.red
1628 && a->fg.green == b->fg.green
1629 && a->fg.blue == b->fg.blue
1630 && a->bg.red == b->bg.red
1631 && a->bg.green == b->bg.green
1632 && a->bg.blue == b->bg.blue;
1633}
1634
Bram Moolenaard96ff162018-02-18 22:13:29 +01001635/*
1636 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1637 * line at this position. Otherwise at the end.
1638 */
1639 static int
1640add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1641{
1642 if (ga_grow(&term->tl_scrollback, 1) == OK)
1643 {
1644 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1645 + term->tl_scrollback.ga_len;
1646
1647 if (lnum > 0)
1648 {
1649 int i;
1650
1651 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1652 {
1653 *line = *(line - 1);
1654 --line;
1655 }
1656 }
1657 line->sb_cols = 0;
1658 line->sb_cells = NULL;
1659 line->sb_fill_attr = *fill_attr;
1660 ++term->tl_scrollback.ga_len;
1661 return OK;
1662 }
1663 return FALSE;
1664}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001665
1666/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001667 * Remove the terminal contents from the scrollback and the buffer.
1668 * Used before adding a new scrollback line or updating the buffer for lines
1669 * displayed in the terminal.
1670 */
1671 static void
1672cleanup_scrollback(term_T *term)
1673{
1674 sb_line_T *line;
1675 garray_T *gap;
1676
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001677 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001678 gap = &term->tl_scrollback;
1679 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1680 && gap->ga_len > 0)
1681 {
1682 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1683 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1684 vim_free(line->sb_cells);
1685 --gap->ga_len;
1686 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001687 curbuf = curwin->w_buffer;
1688 if (curbuf == term->tl_buffer)
1689 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001690}
1691
1692/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001693 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001694 */
1695 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001696update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001697{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001698 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001699 int len;
1700 int lines_skipped = 0;
1701 VTermPos pos;
1702 VTermScreenCell cell;
1703 cellattr_T fill_attr, new_fill_attr;
1704 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001705
1706 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1707 "Adding terminal window snapshot to buffer");
1708
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001709 // First remove the lines that were appended before, they might be
1710 // outdated.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001711 cleanup_scrollback(term);
1712
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001713 screen = vterm_obtain_screen(term->tl_vterm);
1714 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001715 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1716 {
1717 len = 0;
1718 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1719 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1720 && cell.chars[0] != NUL)
1721 {
1722 len = pos.col + 1;
1723 new_fill_attr = term->tl_default_color;
1724 }
1725 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001726 // Assume the last attr is the filler attr.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001727 cell2cellattr(&cell, &new_fill_attr);
1728
1729 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1730 ++lines_skipped;
1731 else
1732 {
1733 while (lines_skipped > 0)
1734 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001735 // Line was skipped, add an empty line.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001736 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001737 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001738 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001739 }
1740
1741 if (len == 0)
1742 p = NULL;
1743 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001744 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001745 if ((p != NULL || len == 0)
1746 && ga_grow(&term->tl_scrollback, 1) == OK)
1747 {
1748 garray_T ga;
1749 int width;
1750 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1751 + term->tl_scrollback.ga_len;
1752
1753 ga_init2(&ga, 1, 100);
1754 for (pos.col = 0; pos.col < len; pos.col += width)
1755 {
1756 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1757 {
1758 width = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001759 CLEAR_POINTER(p + pos.col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001760 if (ga_grow(&ga, 1) == OK)
1761 ga.ga_len += utf_char2bytes(' ',
1762 (char_u *)ga.ga_data + ga.ga_len);
1763 }
1764 else
1765 {
1766 width = cell.width;
1767
1768 cell2cellattr(&cell, &p[pos.col]);
1769
Bram Moolenaara79fd562018-12-20 20:47:32 +01001770 // Each character can be up to 6 bytes.
1771 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001772 {
1773 int i;
1774 int c;
1775
1776 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1777 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1778 (char_u *)ga.ga_data + ga.ga_len);
1779 }
1780 }
1781 }
1782 line->sb_cols = len;
1783 line->sb_cells = p;
1784 line->sb_fill_attr = new_fill_attr;
1785 fill_attr = new_fill_attr;
1786 ++term->tl_scrollback.ga_len;
1787
1788 if (ga_grow(&ga, 1) == FAIL)
1789 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1790 else
1791 {
1792 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1793 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1794 }
1795 ga_clear(&ga);
1796 }
1797 else
1798 vim_free(p);
1799 }
1800 }
1801
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001802 // Add trailing empty lines.
1803 for (pos.row = term->tl_scrollback.ga_len;
1804 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1805 ++pos.row)
1806 {
1807 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1808 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1809 }
1810
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001811 term->tl_dirty_snapshot = FALSE;
1812#ifdef FEAT_TIMERS
1813 term->tl_timer_set = FALSE;
1814#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001815}
1816
1817/*
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001818 * Loop over all windows in the current tab, and also curwin, which is not
1819 * encountered when using a terminal in a popup window.
1820 * Return TRUE if "*wp" was set to the next window.
1821 */
1822 static int
1823for_all_windows_and_curwin(win_T **wp, int *did_curwin)
1824{
1825 if (*wp == NULL)
1826 *wp = firstwin;
1827 else if ((*wp)->w_next != NULL)
1828 *wp = (*wp)->w_next;
1829 else if (!*did_curwin)
1830 *wp = curwin;
1831 else
1832 return FALSE;
1833 if (*wp == curwin)
1834 *did_curwin = TRUE;
1835 return TRUE;
1836}
1837
1838/*
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001839 * If needed, add the current lines of the terminal to scrollback and to the
1840 * buffer. Called after the job has ended and when switching to
1841 * Terminal-Normal mode.
1842 * When "redraw" is TRUE redraw the windows that show the terminal.
1843 */
1844 static void
1845may_move_terminal_to_buffer(term_T *term, int redraw)
1846{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001847 if (term->tl_vterm == NULL)
1848 return;
1849
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001850 // Update the snapshot only if something changes or the buffer does not
1851 // have all the lines.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001852 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1853 <= term->tl_scrollback_scrolled)
1854 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001855
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001856 // Obtain the current background color.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001857 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1858 &term->tl_default_color.fg, &term->tl_default_color.bg);
1859
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001860 if (redraw)
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001861 {
1862 win_T *wp = NULL;
1863 int did_curwin = FALSE;
1864
1865 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001866 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001867 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001868 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001869 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1870 wp->w_cursor.col = 0;
1871 wp->w_valid = 0;
1872 if (wp->w_cursor.lnum >= wp->w_height)
1873 {
1874 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001875
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001876 if (wp->w_topline < min_topline)
1877 wp->w_topline = min_topline;
1878 }
1879 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001880 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001881 }
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001882 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001883}
1884
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001885#if defined(FEAT_TIMERS) || defined(PROTO)
1886/*
1887 * Check if any terminal timer expired. If so, copy text from the terminal to
1888 * the buffer.
1889 * Return the time until the next timer will expire.
1890 */
1891 int
1892term_check_timers(int next_due_arg, proftime_T *now)
1893{
1894 term_T *term;
1895 int next_due = next_due_arg;
1896
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001897 FOR_ALL_TERMS(term)
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001898 {
1899 if (term->tl_timer_set && !term->tl_normal_mode)
1900 {
1901 long this_due = proftime_time_left(&term->tl_timer_due, now);
1902
1903 if (this_due <= 1)
1904 {
1905 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001906 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001907 }
1908 else if (next_due == -1 || next_due > this_due)
1909 next_due = this_due;
1910 }
1911 }
1912
1913 return next_due;
1914}
1915#endif
1916
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001917/*
1918 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
1919 * otherwise end it.
1920 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001921 static void
1922set_terminal_mode(term_T *term, int normal_mode)
1923{
1924 term->tl_normal_mode = normal_mode;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001925 if (!normal_mode)
1926 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001927 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001928 if (term->tl_buffer == curbuf)
1929 maketitle();
1930}
1931
1932/*
Bram Moolenaare2978022020-04-26 14:47:44 +02001933 * Called after the job is finished and Terminal mode is not active:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001934 * Move the vterm contents into the scrollback buffer and free the vterm.
1935 */
1936 static void
1937cleanup_vterm(term_T *term)
1938{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001939 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01001940 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001941 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001942 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001943}
1944
1945/*
1946 * Switch from Terminal-Job mode to Terminal-Normal mode.
1947 * Suspends updating the terminal window.
1948 */
1949 static void
1950term_enter_normal_mode(void)
1951{
1952 term_T *term = curbuf->b_term;
1953
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001954 set_terminal_mode(term, TRUE);
1955
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001956 // Append the current terminal contents to the buffer.
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001957 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001958
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001959 // Move the window cursor to the position of the cursor in the
1960 // terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001961 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1962 + term->tl_cursor_pos.row + 1;
1963 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02001964 if (coladvance(term->tl_cursor_pos.col) == FAIL)
1965 coladvance(MAXCOL);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001966 curwin->w_set_curswant = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001967
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01001968 // Display the same lines as in the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001969 curwin->w_topline = term->tl_scrollback_scrolled + 1;
1970}
1971
1972/*
1973 * Returns TRUE if the current window contains a terminal and we are in
1974 * Terminal-Normal mode.
1975 */
1976 int
1977term_in_normal_mode(void)
1978{
1979 term_T *term = curbuf->b_term;
1980
1981 return term != NULL && term->tl_normal_mode;
1982}
1983
1984/*
1985 * Switch from Terminal-Normal mode to Terminal-Job mode.
1986 * Restores updating the terminal window.
1987 */
1988 void
1989term_enter_job_mode()
1990{
1991 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001992
1993 set_terminal_mode(term, FALSE);
1994
1995 if (term->tl_channel_closed)
1996 cleanup_vterm(term);
1997 redraw_buf_and_status_later(curbuf, NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01001998#ifdef FEAT_PROP_POPUP
1999 if (WIN_IS_POPUP(curwin))
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01002000 redraw_later(NOT_VALID);
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002001#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002002}
2003
2004/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002005 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002006 * Note: while waiting a terminal may be closed and freed if the channel is
2007 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002008 */
2009 static int
2010term_vgetc()
2011{
2012 int c;
2013 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002014 int modify_other_keys =
2015 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002016
2017 State = TERMINAL;
2018 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002019#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002020 ctrl_break_was_pressed = FALSE;
2021#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002022 if (modify_other_keys)
2023 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002024 c = vgetc();
2025 got_int = FALSE;
2026 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002027 if (modify_other_keys)
2028 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002029 return c;
2030}
2031
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002032static int mouse_was_outside = FALSE;
2033
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002034/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002035 * Send key "c" with modifiers "modmask" to terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002036 * Return FAIL when the key needs to be handled in Normal mode.
2037 * Return OK when the key was dropped or sent to the terminal.
2038 */
2039 int
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002040send_keys_to_term(term_T *term, int c, int modmask, int typed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002041{
2042 char msg[KEY_BUF_LEN];
2043 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002044 int dragging_outside = FALSE;
2045
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002046 // Catch keys that need to be handled as in Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002047 switch (c)
2048 {
2049 case NUL:
2050 case K_ZERO:
2051 if (typed)
2052 stuffcharReadbuff(c);
2053 return FAIL;
2054
Bram Moolenaar231a2db2018-05-06 13:53:50 +02002055 case K_TABLINE:
2056 stuffcharReadbuff(c);
2057 return FAIL;
2058
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002059 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002060 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002061 return FAIL;
2062
2063 case K_LEFTDRAG:
2064 case K_MIDDLEDRAG:
2065 case K_RIGHTDRAG:
2066 case K_X1DRAG:
2067 case K_X2DRAG:
2068 dragging_outside = mouse_was_outside;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002069 // FALLTHROUGH
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002070 case K_LEFTMOUSE:
2071 case K_LEFTMOUSE_NM:
2072 case K_LEFTRELEASE:
2073 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002074 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002075 case K_MIDDLEMOUSE:
2076 case K_MIDDLERELEASE:
2077 case K_RIGHTMOUSE:
2078 case K_RIGHTRELEASE:
2079 case K_X1MOUSE:
2080 case K_X1RELEASE:
2081 case K_X2MOUSE:
2082 case K_X2RELEASE:
2083
2084 case K_MOUSEUP:
2085 case K_MOUSEDOWN:
2086 case K_MOUSELEFT:
2087 case K_MOUSERIGHT:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002088 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002089 int row = mouse_row;
2090 int col = mouse_col;
2091
2092#ifdef FEAT_PROP_POPUP
2093 if (popup_is_popup(curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002094 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002095 row -= popup_top_extra(curwin);
2096 col -= popup_left_extra(curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002097 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002098#endif
2099 if (row < W_WINROW(curwin)
2100 || row >= (W_WINROW(curwin) + curwin->w_height)
2101 || col < curwin->w_wincol
2102 || col >= W_ENDCOL(curwin)
2103 || dragging_outside)
2104 {
2105 // click or scroll outside the current window or on status
2106 // line or vertical separator
2107 if (typed)
2108 {
2109 stuffcharReadbuff(c);
2110 mouse_was_outside = TRUE;
2111 }
2112 return FAIL;
2113 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002114 }
2115 }
2116 if (typed)
2117 mouse_was_outside = FALSE;
2118
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002119 // Convert the typed key to a sequence of bytes for the job.
2120 len = term_convert_key(term, c, modmask, msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002121 if (len > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002122 // TODO: if FAIL is returned, stop?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002123 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2124 (char_u *)msg, (int)len, NULL);
2125
2126 return OK;
2127}
2128
2129 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002130position_cursor(win_T *wp, VTermPos *pos, int add_off UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002131{
2132 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2133 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002134#ifdef FEAT_PROP_POPUP
2135 if (add_off && popup_is_popup(curwin))
2136 {
2137 wp->w_wrow += popup_top_extra(curwin);
2138 wp->w_wcol += popup_left_extra(curwin);
2139 }
2140#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002141 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2142}
2143
2144/*
2145 * Handle CTRL-W "": send register contents to the job.
2146 */
2147 static void
2148term_paste_register(int prev_c UNUSED)
2149{
2150 int c;
2151 list_T *l;
2152 listitem_T *item;
2153 long reglen = 0;
2154 int type;
2155
2156#ifdef FEAT_CMDL_INFO
2157 if (add_to_showcmd(prev_c))
2158 if (add_to_showcmd('"'))
2159 out_flush();
2160#endif
2161 c = term_vgetc();
2162#ifdef FEAT_CMDL_INFO
2163 clear_showcmd();
2164#endif
2165 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002166 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002167 return;
2168
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002169 // CTRL-W "= prompt for expression to evaluate.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002170 if (c == '=' && get_expr_register() != '=')
2171 return;
2172 if (!term_use_loop())
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002173 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002174 return;
2175
2176 l = (list_T *)get_reg_contents(c, GREG_LIST);
2177 if (l != NULL)
2178 {
2179 type = get_reg_type(c, &reglen);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002180 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002181 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002182 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002183#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002184 char_u *tmp = s;
2185
2186 if (!enc_utf8 && enc_codepage > 0)
2187 {
2188 WCHAR *ret = NULL;
2189 int length = 0;
2190
2191 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2192 (int)STRLEN(s), &ret, &length);
2193 if (ret != NULL)
2194 {
2195 WideCharToMultiByte_alloc(CP_UTF8, 0,
2196 ret, length, (char **)&s, &length, 0, 0);
2197 vim_free(ret);
2198 }
2199 }
2200#endif
2201 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2202 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002203#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002204 if (tmp != s)
2205 vim_free(s);
2206#endif
2207
2208 if (item->li_next != NULL || type == MLINE)
2209 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2210 (char_u *)"\r", 1, NULL);
2211 }
2212 list_free(l);
2213 }
2214}
2215
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002216/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002217 * Return TRUE when waiting for a character in the terminal, the cursor of the
2218 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002219 */
2220 int
2221terminal_is_active()
2222{
2223 return in_terminal_loop != NULL;
2224}
2225
Bram Moolenaar83d47902020-03-26 20:34:00 +01002226/*
2227 * Return the highight group name for the terminal; "Terminal" if not set.
2228 */
2229 static char_u *
2230term_get_highlight_name(term_T *term)
2231{
2232 if (term->tl_highlight_name == NULL)
2233 return (char_u *)"Terminal";
2234 return term->tl_highlight_name;
2235}
2236
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002237#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002238 cursorentry_T *
2239term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2240{
2241 term_T *term = in_terminal_loop;
2242 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002243 int id;
2244 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002245
Bram Moolenaara80faa82020-04-12 19:37:17 +02002246 CLEAR_FIELD(entry);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002247 entry.shape = entry.mshape =
2248 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2249 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2250 SHAPE_BLOCK;
2251 entry.percentage = 20;
2252 if (term->tl_cursor_blink)
2253 {
2254 entry.blinkwait = 700;
2255 entry.blinkon = 400;
2256 entry.blinkoff = 250;
2257 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002258
Bram Moolenaar83d47902020-03-26 20:34:00 +01002259 // The highlight group overrules the defaults.
2260 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002261 if (id != 0)
2262 {
2263 syn_id2colors(id, &term_fg, &term_bg);
2264 *fg = term_bg;
2265 }
2266 else
2267 *fg = gui.back_pixel;
2268
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002269 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002270 {
2271 if (id != 0)
2272 *bg = term_fg;
2273 else
2274 *bg = gui.norm_pixel;
2275 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002276 else
2277 *bg = color_name2handle(term->tl_cursor_color);
2278 entry.name = "n";
2279 entry.used_for = SHAPE_CURSOR;
2280
2281 return &entry;
2282}
2283#endif
2284
Bram Moolenaard317b382018-02-08 22:33:31 +01002285 static void
2286may_output_cursor_props(void)
2287{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002288 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002289 || last_set_cursor_shape != desired_cursor_shape
2290 || last_set_cursor_blink != desired_cursor_blink)
2291 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002292 cursor_color_copy(&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;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002295 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002296 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002297 // this will restore the initial cursor style, if possible
Bram Moolenaard317b382018-02-08 22:33:31 +01002298 ui_cursor_shape_forced(TRUE);
2299 else
2300 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2301 }
2302}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002303
Bram Moolenaard317b382018-02-08 22:33:31 +01002304/*
2305 * Set the cursor color and shape, if not last set to these.
2306 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002307 static void
2308may_set_cursor_props(term_T *term)
2309{
2310#ifdef FEAT_GUI
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002311 // For the GUI the cursor properties are obtained with
2312 // term_get_cursor_shape().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002313 if (gui.in_use)
2314 return;
2315#endif
2316 if (in_terminal_loop == term)
2317 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002318 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002319 desired_cursor_shape = term->tl_cursor_shape;
2320 desired_cursor_blink = term->tl_cursor_blink;
2321 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002322 }
2323}
2324
Bram Moolenaard317b382018-02-08 22:33:31 +01002325/*
2326 * Reset the desired cursor properties and restore them when needed.
2327 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002328 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002329prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002330{
2331#ifdef FEAT_GUI
2332 if (gui.in_use)
2333 return;
2334#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002335 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002336 desired_cursor_shape = -1;
2337 desired_cursor_blink = -1;
2338 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002339}
2340
2341/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002342 * Returns TRUE if the current window contains a terminal and we are sending
2343 * keys to the job.
2344 * If "check_job_status" is TRUE update the job status.
2345 */
2346 static int
2347term_use_loop_check(int check_job_status)
2348{
2349 term_T *term = curbuf->b_term;
2350
2351 return term != NULL
2352 && !term->tl_normal_mode
2353 && term->tl_vterm != NULL
2354 && term_job_running_check(term, check_job_status);
2355}
2356
2357/*
2358 * Returns TRUE if the current window contains a terminal and we are sending
2359 * keys to the job.
2360 */
2361 int
2362term_use_loop(void)
2363{
2364 return term_use_loop_check(FALSE);
2365}
2366
2367/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002368 * Called when entering a window with the mouse. If this is a terminal window
2369 * we may want to change state.
2370 */
2371 void
2372term_win_entered()
2373{
2374 term_T *term = curbuf->b_term;
2375
2376 if (term != NULL)
2377 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002378 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002379 {
2380 reset_VIsual_and_resel();
2381 if (State & INSERT)
2382 stop_insert_mode = TRUE;
2383 }
2384 mouse_was_outside = FALSE;
2385 enter_mouse_col = mouse_col;
2386 enter_mouse_row = mouse_row;
2387 }
2388}
2389
2390/*
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002391 * vgetc() may not include CTRL in the key when modify_other_keys is set.
2392 * Return the Ctrl-key value in that case.
2393 */
2394 static int
2395raw_c_to_ctrl(int c)
2396{
2397 if ((mod_mask & MOD_MASK_CTRL)
2398 && ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')))
2399 return c & 0x1f;
2400 return c;
2401}
2402
2403/*
2404 * When modify_other_keys is set then do the reverse of raw_c_to_ctrl().
2405 * May set "mod_mask".
2406 */
2407 static int
2408ctrl_to_raw_c(int c)
2409{
2410 if (c < 0x20 && vterm_is_modify_other_keys(curbuf->b_term->tl_vterm))
2411 {
2412 mod_mask |= MOD_MASK_CTRL;
2413 return c + '@';
2414 }
2415 return c;
2416}
2417
2418/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002419 * Wait for input and send it to the job.
2420 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2421 * when there is no more typahead.
2422 * Return when the start of a CTRL-W command is typed or anything else that
2423 * should be handled as a Normal mode command.
2424 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2425 * the terminal was closed.
2426 */
2427 int
2428terminal_loop(int blocking)
2429{
2430 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002431 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002432 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002433 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002434#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002435 int tty_fd = curbuf->b_term->tl_job->jv_channel
2436 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002437#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002438 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002439
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002440 // Remember the terminal we are sending keys to. However, the terminal
2441 // might be closed while waiting for a character, e.g. typing "exit" in a
2442 // shell and ++close was used. Therefore use curbuf->b_term instead of a
2443 // stored reference.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002444 in_terminal_loop = curbuf->b_term;
2445
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002446 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002447 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002448 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002449 if (termwinkey == Ctrl_W)
2450 termwinkey = 0;
2451 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002452 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002453 may_set_cursor_props(curbuf->b_term);
2454
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002455 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002456 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002457#ifdef FEAT_GUI
2458 if (!curbuf->b_term->tl_system)
2459#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002460 // TODO: skip screen update when handling a sequence of keys.
2461 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002462 while (must_redraw != 0)
2463 if (update_screen(0) == FAIL)
2464 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002465 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002466 // job finished while redrawing
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002467 break;
2468
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002469 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002470 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002471
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002472 raw_c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002473 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002474 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002475 // Job finished while waiting for a character. Push back the
2476 // received character.
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002477 if (raw_c != K_IGNORE)
2478 vungetc(raw_c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002479 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002480 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002481 if (raw_c == K_IGNORE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002482 continue;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002483 c = raw_c_to_ctrl(raw_c);
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002484
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002485#ifdef UNIX
2486 /*
2487 * The shell or another program may change the tty settings. Getting
2488 * them for every typed character is a bit of overhead, but it's needed
2489 * for the first character typed, e.g. when Vim starts in a shell.
2490 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002491 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002492 {
2493 ttyinfo_T info;
2494
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002495 // Get the current backspace character of the pty.
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002496 if (get_tty_info(tty_fd, &info) == OK)
2497 term_backspace_char = info.backspace;
2498 }
2499#endif
2500
Bram Moolenaar4f974752019-02-17 17:44:42 +01002501#ifdef MSWIN
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002502 // On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2503 // Use CTRL-BREAK to kill the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002504 if (ctrl_break_was_pressed)
2505 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2506#endif
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002507 // Was either CTRL-W (termwinkey) or CTRL-\ pressed?
2508 // Not in a system terminal.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002509 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002510#ifdef FEAT_GUI
2511 && !curbuf->b_term->tl_system
2512#endif
2513 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002514 {
2515 int prev_c = c;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002516 int prev_raw_c = raw_c;
2517 int prev_mod_mask = mod_mask;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002518
2519#ifdef FEAT_CMDL_INFO
2520 if (add_to_showcmd(c))
2521 out_flush();
2522#endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002523 raw_c = term_vgetc();
2524 c = raw_c_to_ctrl(raw_c);
2525
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002526#ifdef FEAT_CMDL_INFO
2527 clear_showcmd();
2528#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002529 if (!term_use_loop_check(TRUE)
2530 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002531 // job finished while waiting for a character
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002532 break;
2533
2534 if (prev_c == Ctrl_BSL)
2535 {
2536 if (c == Ctrl_N)
2537 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002538 // CTRL-\ CTRL-N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002539 term_enter_normal_mode();
2540 ret = FAIL;
2541 goto theend;
2542 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002543 // Send both keys to the terminal, first one here, second one
2544 // below.
2545 send_keys_to_term(curbuf->b_term, prev_raw_c, prev_mod_mask,
2546 TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002547 }
2548 else if (c == Ctrl_C)
2549 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002550 // "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002551 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2552 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002553 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002554 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002555 // "CTRL-W .": send CTRL-W to the job
2556 // "'termwinkey' .": send 'termwinkey' to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002557 raw_c = ctrl_to_raw_c(termwinkey == 0 ? Ctrl_W : termwinkey);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002558 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002559 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002560 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002561 // "CTRL-W CTRL-\": send CTRL-\ to the job
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002562 raw_c = ctrl_to_raw_c(Ctrl_BSL);
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002563 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002564 else if (c == 'N')
2565 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002566 // CTRL-W N : go to Terminal-Normal mode.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002567 term_enter_normal_mode();
2568 ret = FAIL;
2569 goto theend;
2570 }
2571 else if (c == '"')
2572 {
2573 term_paste_register(prev_c);
2574 continue;
2575 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002576 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002577 {
Bram Moolenaara4b26992019-08-15 20:58:54 +02002578 char_u buf[MB_MAXBYTES + 2];
2579
2580 // Put the command into the typeahead buffer, when using the
2581 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2582 buf[0] = Ctrl_W;
2583 buf[(*mb_char2bytes)(c, buf + 1) + 1] = NUL;
2584 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002585 ret = OK;
2586 goto theend;
2587 }
2588 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002589# ifdef MSWIN
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002590 if (!enc_utf8 && has_mbyte && raw_c >= 0x80)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002591 {
2592 WCHAR wc;
2593 char_u mb[3];
2594
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002595 mb[0] = (unsigned)raw_c >> 8;
2596 mb[1] = raw_c;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002597 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002598 raw_c = wc;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002599 }
2600# endif
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002601 if (send_keys_to_term(curbuf->b_term, raw_c, mod_mask, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002602 {
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01002603 if (raw_c == K_MOUSEMOVE)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002604 // We are sure to come back here, don't reset the cursor color
2605 // and shape to avoid flickering.
Bram Moolenaard317b382018-02-08 22:33:31 +01002606 restore_cursor = FALSE;
2607
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002608 ret = OK;
2609 goto theend;
2610 }
2611 }
2612 ret = FAIL;
2613
2614theend:
2615 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002616 if (restore_cursor)
2617 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002618
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002619 // Move a snapshot of the screen contents to the buffer, so that completion
2620 // works in other buffers.
Bram Moolenaar620020e2018-05-13 19:06:12 +02002621 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2622 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002623
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002624 return ret;
2625}
2626
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002627 static void
2628may_toggle_cursor(term_T *term)
2629{
2630 if (in_terminal_loop == term)
2631 {
2632 if (term->tl_cursor_visible)
2633 cursor_on();
2634 else
2635 cursor_off();
2636 }
2637}
2638
2639/*
Bram Moolenaar83d47902020-03-26 20:34:00 +01002640 * Cache "Terminal" highlight group colors.
2641 */
2642 void
2643set_terminal_default_colors(int cterm_fg, int cterm_bg)
2644{
2645 term_default_cterm_fg = cterm_fg - 1;
2646 term_default_cterm_bg = cterm_bg - 1;
2647}
2648
2649 static int
2650get_default_cterm_fg(term_T *term)
2651{
2652 if (term->tl_highlight_name != NULL)
2653 {
2654 int id = syn_name2id(term->tl_highlight_name);
2655 int fg = -1;
2656 int bg = -1;
2657
2658 if (id > 0)
2659 syn_id2cterm_bg(id, &fg, &bg);
2660 return fg;
2661 }
2662 return term_default_cterm_fg;
2663}
2664
2665 static int
2666get_default_cterm_bg(term_T *term)
2667{
2668 if (term->tl_highlight_name != NULL)
2669 {
2670 int id = syn_name2id(term->tl_highlight_name);
2671 int fg = -1;
2672 int bg = -1;
2673
2674 if (id > 0)
2675 syn_id2cterm_bg(id, &fg, &bg);
2676 return bg;
2677 }
2678 return term_default_cterm_bg;
2679}
2680
2681/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002682 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002683 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002684 */
2685 static int
2686color2index(VTermColor *color, int fg, int *boldp)
2687{
2688 int red = color->red;
2689 int blue = color->blue;
2690 int green = color->green;
2691
Bram Moolenaar46359e12017-11-29 22:33:38 +01002692 if (color->ansi_index != VTERM_ANSI_INDEX_NONE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002693 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002694 // The first 16 colors and default: use the ANSI index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002695 switch (color->ansi_index)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002696 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002697 case 0: return 0;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002698 case 1: return lookup_color( 0, fg, boldp) + 1; // black
2699 case 2: return lookup_color( 4, fg, boldp) + 1; // dark red
2700 case 3: return lookup_color( 2, fg, boldp) + 1; // dark green
Bram Moolenaare2978022020-04-26 14:47:44 +02002701 case 4: return lookup_color( 7, fg, boldp) + 1; // dark yellow
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002702 case 5: return lookup_color( 1, fg, boldp) + 1; // dark blue
2703 case 6: return lookup_color( 5, fg, boldp) + 1; // dark magenta
2704 case 7: return lookup_color( 3, fg, boldp) + 1; // dark cyan
2705 case 8: return lookup_color( 8, fg, boldp) + 1; // light grey
2706 case 9: return lookup_color(12, fg, boldp) + 1; // dark grey
2707 case 10: return lookup_color(20, fg, boldp) + 1; // red
2708 case 11: return lookup_color(16, fg, boldp) + 1; // green
2709 case 12: return lookup_color(24, fg, boldp) + 1; // yellow
2710 case 13: return lookup_color(14, fg, boldp) + 1; // blue
2711 case 14: return lookup_color(22, fg, boldp) + 1; // magenta
2712 case 15: return lookup_color(18, fg, boldp) + 1; // cyan
2713 case 16: return lookup_color(26, fg, boldp) + 1; // white
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002714 }
2715 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002716
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002717 if (t_colors >= 256)
2718 {
2719 if (red == blue && red == green)
2720 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002721 // 24-color greyscale plus white and black
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002722 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002723 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2724 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2725 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002726 int i;
2727
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002728 if (red < 5)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002729 return 17; // 00/00/00
2730 if (red > 245) // ff/ff/ff
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002731 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002732 for (i = 0; i < 23; ++i)
2733 if (red < cutoff[i])
2734 return i + 233;
2735 return 256;
2736 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002737 {
2738 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2739 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002740
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002741 // 216-color cube
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002742 for (ri = 0; ri < 5; ++ri)
2743 if (red < cutoff[ri])
2744 break;
2745 for (gi = 0; gi < 5; ++gi)
2746 if (green < cutoff[gi])
2747 break;
2748 for (bi = 0; bi < 5; ++bi)
2749 if (blue < cutoff[bi])
2750 break;
2751 return 17 + ri * 36 + gi * 6 + bi;
2752 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002753 }
2754 return 0;
2755}
2756
2757/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002758 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002759 */
2760 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002761vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002762{
2763 int attr = 0;
2764
2765 if (cellattrs.bold)
2766 attr |= HL_BOLD;
2767 if (cellattrs.underline)
2768 attr |= HL_UNDERLINE;
2769 if (cellattrs.italic)
2770 attr |= HL_ITALIC;
2771 if (cellattrs.strike)
2772 attr |= HL_STRIKETHROUGH;
2773 if (cellattrs.reverse)
2774 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002775 return attr;
2776}
2777
2778/*
2779 * Store Vterm attributes in "cell" from highlight flags.
2780 */
2781 static void
2782hl2vtermAttr(int attr, cellattr_T *cell)
2783{
Bram Moolenaara80faa82020-04-12 19:37:17 +02002784 CLEAR_FIELD(cell->attrs);
Bram Moolenaard96ff162018-02-18 22:13:29 +01002785 if (attr & HL_BOLD)
2786 cell->attrs.bold = 1;
2787 if (attr & HL_UNDERLINE)
2788 cell->attrs.underline = 1;
2789 if (attr & HL_ITALIC)
2790 cell->attrs.italic = 1;
2791 if (attr & HL_STRIKETHROUGH)
2792 cell->attrs.strike = 1;
2793 if (attr & HL_INVERSE)
2794 cell->attrs.reverse = 1;
2795}
2796
2797/*
2798 * Convert the attributes of a vterm cell into an attribute index.
2799 */
2800 static int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002801cell2attr(
Bram Moolenaar83d47902020-03-26 20:34:00 +01002802 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002803 win_T *wp,
2804 VTermScreenCellAttrs cellattrs,
2805 VTermColor cellfg,
2806 VTermColor cellbg)
Bram Moolenaard96ff162018-02-18 22:13:29 +01002807{
2808 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002809
2810#ifdef FEAT_GUI
2811 if (gui.in_use)
2812 {
2813 guicolor_T fg, bg;
2814
2815 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2816 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2817 return get_gui_attr_idx(attr, fg, bg);
2818 }
2819 else
2820#endif
2821#ifdef FEAT_TERMGUICOLORS
2822 if (p_tgc)
2823 {
2824 guicolor_T fg, bg;
2825
2826 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2827 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2828
2829 return get_tgc_attr_idx(attr, fg, bg);
2830 }
2831 else
2832#endif
2833 {
2834 int bold = MAYBE;
2835 int fg = color2index(&cellfg, TRUE, &bold);
2836 int bg = color2index(&cellbg, FALSE, &bold);
2837
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002838 // Use the 'wincolor' or "Terminal" highlighting for the default
2839 // colors.
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002840 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002841 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002842 int wincolor_fg = -1;
2843 int wincolor_bg = -1;
2844
2845 if (wp != NULL && *wp->w_p_wcr != NUL)
2846 {
2847 int id = syn_name2id(curwin->w_p_wcr);
2848
2849 // Get the 'wincolor' group colors.
2850 if (id > 0)
2851 syn_id2cterm_bg(id, &wincolor_fg, &wincolor_bg);
2852 }
2853 if (fg == 0)
2854 {
2855 if (wincolor_fg >= 0)
2856 fg = wincolor_fg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002857 else
2858 {
2859 int cterm_fg = get_default_cterm_fg(term);
2860
2861 if (cterm_fg >= 0)
2862 fg = cterm_fg + 1;
2863 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002864 }
2865 if (bg == 0)
2866 {
2867 if (wincolor_bg >= 0)
2868 bg = wincolor_bg + 1;
Bram Moolenaar83d47902020-03-26 20:34:00 +01002869 else
2870 {
2871 int cterm_bg = get_default_cterm_bg(term);
2872
2873 if (cterm_bg >= 0)
2874 bg = cterm_bg + 1;
2875 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002876 }
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002877 }
2878
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002879 // with 8 colors set the bold attribute to get a bright foreground
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002880 if (bold == TRUE)
2881 attr |= HL_BOLD;
2882 return get_cterm_attr_idx(attr, fg, bg);
2883 }
2884 return 0;
2885}
2886
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002887 static void
2888set_dirty_snapshot(term_T *term)
2889{
2890 term->tl_dirty_snapshot = TRUE;
2891#ifdef FEAT_TIMERS
2892 if (!term->tl_normal_mode)
2893 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002894 // Update the snapshot after 100 msec of not getting updates.
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002895 profile_setlimit(100L, &term->tl_timer_due);
2896 term->tl_timer_set = TRUE;
2897 }
2898#endif
2899}
2900
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002901 static int
2902handle_damage(VTermRect rect, void *user)
2903{
2904 term_T *term = (term_T *)user;
2905
2906 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2907 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002908 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002909 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002910 return 1;
2911}
2912
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002913 static void
2914term_scroll_up(term_T *term, int start_row, int count)
2915{
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002916 win_T *wp = NULL;
2917 int did_curwin = FALSE;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002918 VTermColor fg, bg;
2919 VTermScreenCellAttrs attr;
2920 int clear_attr;
2921
Bram Moolenaara80faa82020-04-12 19:37:17 +02002922 CLEAR_FIELD(attr);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002923
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002924 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002925 {
2926 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002927 {
2928 // Set the color to clear lines with.
2929 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2930 &fg, &bg);
Bram Moolenaar83d47902020-03-26 20:34:00 +01002931 clear_attr = cell2attr(term, wp, attr, fg, bg);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002932 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002933 }
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002934 }
2935}
2936
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002937 static int
2938handle_moverect(VTermRect dest, VTermRect src, void *user)
2939{
2940 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002941 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002942
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002943 // Scrolling up is done much more efficiently by deleting lines instead of
2944 // redrawing the text. But avoid doing this multiple times, postpone until
2945 // the redraw happens.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002946 if (dest.start_col == src.start_col
2947 && dest.end_col == src.end_col
2948 && dest.start_row < src.start_row)
2949 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002950 if (dest.start_row == 0)
2951 term->tl_postponed_scroll += count;
2952 else
2953 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002954 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02002955
2956 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
2957 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002958 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02002959
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01002960 // Note sure if the scrolling will work correctly, let's do a complete
2961 // redraw later.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002962 redraw_buf_later(term->tl_buffer, NOT_VALID);
2963 return 1;
2964}
2965
2966 static int
2967handle_movecursor(
2968 VTermPos pos,
2969 VTermPos oldpos UNUSED,
2970 int visible,
2971 void *user)
2972{
2973 term_T *term = (term_T *)user;
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002974 win_T *wp = NULL;
2975 int did_curwin = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002976
2977 term->tl_cursor_pos = pos;
2978 term->tl_cursor_visible = visible;
2979
Bram Moolenaare52e0c82020-02-28 22:20:10 +01002980 while (for_all_windows_and_curwin(&wp, &did_curwin))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002981 {
2982 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar219c7d02020-02-01 21:57:29 +01002983 position_cursor(wp, &pos, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002984 }
2985 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
2986 {
2987 may_toggle_cursor(term);
2988 update_cursor(term, term->tl_cursor_visible);
2989 }
2990
2991 return 1;
2992}
2993
2994 static int
2995handle_settermprop(
2996 VTermProp prop,
2997 VTermValue *value,
2998 void *user)
2999{
3000 term_T *term = (term_T *)user;
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003001 char_u *strval = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003002
3003 switch (prop)
3004 {
3005 case VTERM_PROP_TITLE:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003006 strval = vim_strnsave((char_u *)value->string.str,
3007 (int)value->string.len);
3008 if (strval == NULL)
3009 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003010 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003011 // a blank title isn't useful, make it empty, so that "running" is
3012 // displayed
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003013 if (*skipwhite(strval) == NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003014 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003015 // Same as blank
3016 else if (term->tl_arg0_cmd != NULL
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003017 && STRNCMP(term->tl_arg0_cmd, strval,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003018 (int)STRLEN(term->tl_arg0_cmd)) == 0)
3019 term->tl_title = NULL;
3020 // Empty corrupted data of winpty
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003021 else if (STRNCMP(" - ", strval, 4) == 0)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01003022 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003023#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003024 else if (!enc_utf8 && enc_codepage > 0)
3025 {
3026 WCHAR *ret = NULL;
3027 int length = 0;
3028
3029 MultiByteToWideChar_alloc(CP_UTF8, 0,
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003030 (char*)value->string.str,
3031 (int)value->string.len, &ret, &length);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003032 if (ret != NULL)
3033 {
3034 WideCharToMultiByte_alloc(enc_codepage, 0,
3035 ret, length, (char**)&term->tl_title,
3036 &length, 0, 0);
3037 vim_free(ret);
3038 }
3039 }
3040#endif
3041 else
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003042 {
3043 term->tl_title = vim_strsave(strval);
3044 strval = NULL;
3045 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003046 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003047 if (term == curbuf->b_term)
3048 maketitle();
3049 break;
3050
3051 case VTERM_PROP_CURSORVISIBLE:
3052 term->tl_cursor_visible = value->boolean;
3053 may_toggle_cursor(term);
3054 out_flush();
3055 break;
3056
3057 case VTERM_PROP_CURSORBLINK:
3058 term->tl_cursor_blink = value->boolean;
3059 may_set_cursor_props(term);
3060 break;
3061
3062 case VTERM_PROP_CURSORSHAPE:
3063 term->tl_cursor_shape = value->number;
3064 may_set_cursor_props(term);
3065 break;
3066
3067 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003068 strval = vim_strnsave((char_u *)value->string.str,
3069 (int)value->string.len);
3070 if (strval == NULL)
3071 break;
3072 cursor_color_copy(&term->tl_cursor_color, strval);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003073 may_set_cursor_props(term);
3074 break;
3075
3076 case VTERM_PROP_ALTSCREEN:
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003077 // TODO: do anything else?
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003078 term->tl_using_altscreen = value->boolean;
3079 break;
3080
3081 default:
3082 break;
3083 }
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02003084 vim_free(strval);
3085
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003086 // Always return 1, otherwise vterm doesn't store the value internally.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003087 return 1;
3088}
3089
3090/*
3091 * The job running in the terminal resized the terminal.
3092 */
3093 static int
3094handle_resize(int rows, int cols, void *user)
3095{
3096 term_T *term = (term_T *)user;
3097 win_T *wp;
3098
3099 term->tl_rows = rows;
3100 term->tl_cols = cols;
3101 if (term->tl_vterm_size_changed)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003102 // Size was set by vterm_set_size(), don't set the window size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003103 term->tl_vterm_size_changed = FALSE;
3104 else
3105 {
3106 FOR_ALL_WINDOWS(wp)
3107 {
3108 if (wp->w_buffer == term->tl_buffer)
3109 {
3110 win_setheight_win(rows, wp);
3111 win_setwidth_win(cols, wp);
3112 }
3113 }
3114 redraw_buf_later(term->tl_buffer, NOT_VALID);
3115 }
3116 return 1;
3117}
3118
3119/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003120 * If the number of lines that are stored goes over 'termscrollback' then
3121 * delete the first 10%.
3122 * "gap" points to tl_scrollback or tl_scrollback_postponed.
3123 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003124 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003125 static void
3126limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003127{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003128 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003129 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02003130 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003131 int i;
3132
3133 curbuf = term->tl_buffer;
3134 for (i = 0; i < todo; ++i)
3135 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003136 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
3137 if (update_buffer)
3138 ml_delete(1, FALSE);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003139 }
3140 curbuf = curwin->w_buffer;
3141
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003142 gap->ga_len -= todo;
3143 mch_memmove(gap->ga_data,
3144 (sb_line_T *)gap->ga_data + todo,
3145 sizeof(sb_line_T) * gap->ga_len);
3146 if (update_buffer)
3147 term->tl_scrollback_scrolled -= todo;
3148 }
3149}
3150
3151/*
3152 * Handle a line that is pushed off the top of the screen.
3153 */
3154 static int
3155handle_pushline(int cols, const VTermScreenCell *cells, void *user)
3156{
3157 term_T *term = (term_T *)user;
3158 garray_T *gap;
3159 int update_buffer;
3160
3161 if (term->tl_normal_mode)
3162 {
3163 // In Terminal-Normal mode the user interacts with the buffer, thus we
3164 // must not change it. Postpone adding the scrollback lines.
3165 gap = &term->tl_scrollback_postponed;
3166 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003167 }
3168 else
3169 {
3170 // First remove the lines that were appended before, the pushed line
3171 // goes above it.
3172 cleanup_scrollback(term);
3173 gap = &term->tl_scrollback;
3174 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02003175 }
3176
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003177 limit_scrollback(term, gap, update_buffer);
3178
3179 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003180 {
3181 cellattr_T *p = NULL;
3182 int len = 0;
3183 int i;
3184 int c;
3185 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003186 int text_len;
3187 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003188 sb_line_T *line;
3189 garray_T ga;
3190 cellattr_T fill_attr = term->tl_default_color;
3191
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003192 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003193 for (i = 0; i < cols; ++i)
3194 if (cells[i].chars[0] != 0)
3195 len = i + 1;
3196 else
3197 cell2cellattr(&cells[i], &fill_attr);
3198
3199 ga_init2(&ga, 1, 100);
3200 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003201 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003202 if (p != NULL)
3203 {
3204 for (col = 0; col < len; col += cells[col].width)
3205 {
3206 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
3207 {
3208 ga.ga_len = 0;
3209 break;
3210 }
3211 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
3212 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
3213 (char_u *)ga.ga_data + ga.ga_len);
3214 cell2cellattr(&cells[col], &p[col]);
3215 }
3216 }
3217 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003218 {
3219 if (update_buffer)
3220 text = (char_u *)"";
3221 else
3222 text = vim_strsave((char_u *)"");
3223 text_len = 0;
3224 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003225 else
3226 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003227 text = ga.ga_data;
3228 text_len = ga.ga_len;
3229 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003230 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003231 if (update_buffer)
3232 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003233
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003234 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003235 line->sb_cols = len;
3236 line->sb_cells = p;
3237 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003238 if (update_buffer)
3239 {
3240 line->sb_text = NULL;
3241 ++term->tl_scrollback_scrolled;
3242 ga_clear(&ga); // free the text
3243 }
3244 else
3245 {
3246 line->sb_text = text;
3247 ga_init(&ga); // text is kept in tl_scrollback_postponed
3248 }
3249 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003250 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003251 return 0; // ignored
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003252}
3253
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003254/*
3255 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
3256 * received and stored in tl_scrollback_postponed.
3257 */
3258 static void
3259handle_postponed_scrollback(term_T *term)
3260{
3261 int i;
3262
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003263 if (term->tl_scrollback_postponed.ga_len == 0)
3264 return;
3265 ch_log(NULL, "Moving postponed scrollback to scrollback");
3266
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003267 // First remove the lines that were appended before, the pushed lines go
3268 // above it.
3269 cleanup_scrollback(term);
3270
3271 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3272 {
3273 char_u *text;
3274 sb_line_T *pp_line;
3275 sb_line_T *line;
3276
3277 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3278 break;
3279 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3280
3281 text = pp_line->sb_text;
3282 if (text == NULL)
3283 text = (char_u *)"";
3284 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3285 vim_free(pp_line->sb_text);
3286
3287 line = (sb_line_T *)term->tl_scrollback.ga_data
3288 + term->tl_scrollback.ga_len;
3289 line->sb_cols = pp_line->sb_cols;
3290 line->sb_cells = pp_line->sb_cells;
3291 line->sb_fill_attr = pp_line->sb_fill_attr;
3292 line->sb_text = NULL;
3293 ++term->tl_scrollback_scrolled;
3294 ++term->tl_scrollback.ga_len;
3295 }
3296
3297 ga_clear(&term->tl_scrollback_postponed);
3298 limit_scrollback(term, &term->tl_scrollback, TRUE);
3299}
3300
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003301static VTermScreenCallbacks screen_callbacks = {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003302 handle_damage, // damage
3303 handle_moverect, // moverect
3304 handle_movecursor, // movecursor
3305 handle_settermprop, // settermprop
3306 NULL, // bell
3307 handle_resize, // resize
3308 handle_pushline, // sb_pushline
3309 NULL // sb_popline
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003310};
3311
3312/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003313 * Do the work after the channel of a terminal was closed.
3314 * Must be called only when updating_screen is FALSE.
3315 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3316 */
3317 static int
3318term_after_channel_closed(term_T *term)
3319{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003320 // Unless in Terminal-Normal mode: clear the vterm.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003321 if (!term->tl_normal_mode)
3322 {
3323 int fnum = term->tl_buffer->b_fnum;
3324
3325 cleanup_vterm(term);
3326
3327 if (term->tl_finish == TL_FINISH_CLOSE)
3328 {
3329 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003330 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003331#ifdef FEAT_PROP_POPUP
3332 win_T *pwin = NULL;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003333
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003334 // If this was a terminal in a popup window, go back to the
3335 // previous window.
3336 if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
3337 {
3338 pwin = curwin;
3339 if (win_valid(prevwin))
3340 win_enter(prevwin, FALSE);
3341 }
3342 else
3343#endif
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003344 // If this is the last normal window: exit Vim.
3345 if (term->tl_buffer->b_nwindows > 0 && only_one_window())
3346 {
3347 exarg_T ea;
3348
Bram Moolenaara80faa82020-04-12 19:37:17 +02003349 CLEAR_FIELD(ea);
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02003350 ex_quit(&ea);
3351 return TRUE;
3352 }
3353
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003354 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003355 ch_log(NULL, "terminal job finished, closing window");
3356 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003357 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003358 if (curwin == aucmd_win)
3359 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003360 if (do_set_w_closing)
3361 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003362 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003363 if (do_set_w_closing)
3364 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003365 aucmd_restbuf(&aco);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003366#ifdef FEAT_PROP_POPUP
3367 if (pwin != NULL)
3368 popup_close_with_retval(pwin, 0);
3369#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003370 return TRUE;
3371 }
3372 if (term->tl_finish == TL_FINISH_OPEN
3373 && term->tl_buffer->b_nwindows == 0)
3374 {
3375 char buf[50];
3376
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003377 // TODO: use term_opencmd
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003378 ch_log(NULL, "terminal job finished, opening window");
3379 vim_snprintf(buf, sizeof(buf),
3380 term->tl_opencmd == NULL
3381 ? "botright sbuf %d"
3382 : (char *)term->tl_opencmd, fnum);
3383 do_cmdline_cmd((char_u *)buf);
3384 }
3385 else
3386 ch_log(NULL, "terminal job finished");
3387 }
3388
3389 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3390 return FALSE;
3391}
3392
Bram Moolenaard98c0b62020-02-02 15:25:16 +01003393#if defined(FEAT_PROP_POPUP) || defined(PROTO)
3394/*
3395 * If the current window is a terminal in a popup window and the job has
3396 * finished, close the popup window and to back to the previous window.
3397 * Otherwise return FAIL.
3398 */
3399 int
3400may_close_term_popup(void)
3401{
3402 if (popup_is_popup(curwin) && curbuf->b_term != NULL
3403 && !term_job_running(curbuf->b_term))
3404 {
3405 win_T *pwin = curwin;
3406
3407 if (win_valid(prevwin))
3408 win_enter(prevwin, FALSE);
3409 popup_close_with_retval(pwin, 0);
3410 return OK;
3411 }
3412 return FAIL;
3413}
3414#endif
3415
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003416/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003417 * Called when a channel has been closed.
3418 * If this was a channel for a terminal window then finish it up.
3419 */
3420 void
3421term_channel_closed(channel_T *ch)
3422{
3423 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003424 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003425 int did_one = FALSE;
3426
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003427 for (term = first_term; term != NULL; term = next_term)
3428 {
3429 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003430 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003431 {
3432 term->tl_channel_closed = TRUE;
3433 did_one = TRUE;
3434
Bram Moolenaard23a8232018-02-10 18:45:26 +01003435 VIM_CLEAR(term->tl_title);
3436 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003437#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003438 if (term->tl_out_fd != NULL)
3439 {
3440 fclose(term->tl_out_fd);
3441 term->tl_out_fd = NULL;
3442 }
3443#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003444
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003445 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003446 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003447 // Cannot open or close windows now. Can happen when
3448 // 'lazyredraw' is set.
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003449 term->tl_channel_recently_closed = TRUE;
3450 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003451 }
3452
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003453 if (term_after_channel_closed(term))
3454 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003455 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003456 }
3457
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003458 if (did_one)
3459 {
3460 redraw_statuslines();
3461
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003462 // Need to break out of vgetc().
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003463 ins_char_typebuf(K_IGNORE);
3464 typebuf_was_filled = TRUE;
3465
3466 term = curbuf->b_term;
3467 if (term != NULL)
3468 {
3469 if (term->tl_job == ch->ch_job)
3470 maketitle();
3471 update_cursor(term, term->tl_cursor_visible);
3472 }
3473 }
3474}
3475
3476/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003477 * To be called after resetting updating_screen: handle any terminal where the
3478 * channel was closed.
3479 */
3480 void
3481term_check_channel_closed_recently()
3482{
3483 term_T *term;
3484 term_T *next_term;
3485
3486 for (term = first_term; term != NULL; term = next_term)
3487 {
3488 next_term = term->tl_next;
3489 if (term->tl_channel_recently_closed)
3490 {
3491 term->tl_channel_recently_closed = FALSE;
3492 if (term_after_channel_closed(term))
3493 // start over, the list may have changed
3494 next_term = first_term;
3495 }
3496 }
3497}
3498
3499/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003500 * Fill one screen line from a line of the terminal.
3501 * Advances "pos" to past the last column.
3502 */
3503 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003504term_line2screenline(
Bram Moolenaar83d47902020-03-26 20:34:00 +01003505 term_T *term,
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003506 win_T *wp,
3507 VTermScreen *screen,
3508 VTermPos *pos,
3509 int max_col)
Bram Moolenaar13568252018-03-16 20:46:58 +01003510{
3511 int off = screen_get_current_line_off();
3512
3513 for (pos->col = 0; pos->col < max_col; )
3514 {
3515 VTermScreenCell cell;
3516 int c;
3517
3518 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003519 CLEAR_FIELD(cell);
Bram Moolenaar13568252018-03-16 20:46:58 +01003520
3521 c = cell.chars[0];
3522 if (c == NUL)
3523 {
3524 ScreenLines[off] = ' ';
3525 if (enc_utf8)
3526 ScreenLinesUC[off] = NUL;
3527 }
3528 else
3529 {
3530 if (enc_utf8)
3531 {
3532 int i;
3533
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003534 // composing chars
Bram Moolenaar13568252018-03-16 20:46:58 +01003535 for (i = 0; i < Screen_mco
3536 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3537 {
3538 ScreenLinesC[i][off] = cell.chars[i + 1];
3539 if (cell.chars[i + 1] == 0)
3540 break;
3541 }
3542 if (c >= 0x80 || (Screen_mco > 0
3543 && ScreenLinesC[0][off] != 0))
3544 {
3545 ScreenLines[off] = ' ';
3546 ScreenLinesUC[off] = c;
3547 }
3548 else
3549 {
3550 ScreenLines[off] = c;
3551 ScreenLinesUC[off] = NUL;
3552 }
3553 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003554#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003555 else if (has_mbyte && c >= 0x80)
3556 {
3557 char_u mb[MB_MAXBYTES+1];
3558 WCHAR wc = c;
3559
3560 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3561 (char*)mb, 2, 0, 0) > 1)
3562 {
3563 ScreenLines[off] = mb[0];
3564 ScreenLines[off + 1] = mb[1];
3565 cell.width = mb_ptr2cells(mb);
3566 }
3567 else
3568 ScreenLines[off] = c;
3569 }
3570#endif
3571 else
3572 ScreenLines[off] = c;
3573 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003574 ScreenAttrs[off] = cell2attr(term, wp, cell.attrs, cell.fg, cell.bg);
Bram Moolenaar13568252018-03-16 20:46:58 +01003575
3576 ++pos->col;
3577 ++off;
3578 if (cell.width == 2)
3579 {
3580 if (enc_utf8)
3581 ScreenLinesUC[off] = NUL;
3582
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003583 // don't set the second byte to NUL for a DBCS encoding, it
3584 // has been set above
Bram Moolenaar13568252018-03-16 20:46:58 +01003585 if (enc_utf8 || !has_mbyte)
3586 ScreenLines[off] = NUL;
3587
3588 ++pos->col;
3589 ++off;
3590 }
3591 }
3592}
3593
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003594#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003595 static void
3596update_system_term(term_T *term)
3597{
3598 VTermPos pos;
3599 VTermScreen *screen;
3600
3601 if (term->tl_vterm == NULL)
3602 return;
3603 screen = vterm_obtain_screen(term->tl_vterm);
3604
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003605 // Scroll up to make more room for terminal lines if needed.
Bram Moolenaar13568252018-03-16 20:46:58 +01003606 while (term->tl_toprow > 0
3607 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3608 {
3609 int save_p_more = p_more;
3610
3611 p_more = FALSE;
3612 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003613 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003614 p_more = save_p_more;
3615 --term->tl_toprow;
3616 }
3617
3618 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3619 && pos.row < Rows; ++pos.row)
3620 {
3621 if (pos.row < term->tl_rows)
3622 {
3623 int max_col = MIN(Columns, term->tl_cols);
3624
Bram Moolenaar83d47902020-03-26 20:34:00 +01003625 term_line2screenline(term, NULL, screen, &pos, max_col);
Bram Moolenaar13568252018-03-16 20:46:58 +01003626 }
3627 else
3628 pos.col = 0;
3629
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003630 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003631 }
3632
3633 term->tl_dirty_row_start = MAX_ROW;
3634 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003635}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003636#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003637
3638/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003639 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3640 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003641 * Terminal-Normal mode.
3642 */
3643 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003644term_do_update_window(win_T *wp)
3645{
3646 term_T *term = wp->w_buffer->b_term;
3647
3648 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3649}
3650
3651/*
3652 * Called to update a window that contains an active terminal.
3653 */
3654 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003655term_update_window(win_T *wp)
3656{
3657 term_T *term = wp->w_buffer->b_term;
3658 VTerm *vterm;
3659 VTermScreen *screen;
3660 VTermState *state;
3661 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003662 int rows, cols;
3663 int newrows, newcols;
3664 int minsize;
3665 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003666
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003667 vterm = term->tl_vterm;
3668 screen = vterm_obtain_screen(vterm);
3669 state = vterm_obtain_state(vterm);
3670
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003671 // We use NOT_VALID on a resize or scroll, redraw everything then. With
3672 // SOME_VALID only redraw what was marked dirty.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003673 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003674 {
3675 term->tl_dirty_row_start = 0;
3676 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003677
3678 if (term->tl_postponed_scroll > 0
3679 && term->tl_postponed_scroll < term->tl_rows / 3)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003680 // Scrolling is usually faster than redrawing, when there are only
3681 // a few lines to scroll.
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003682 term_scroll_up(term, 0, term->tl_postponed_scroll);
3683 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003684 }
3685
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003686 /*
3687 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003688 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003689 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003690 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003691
Bram Moolenaar498c2562018-04-15 23:45:15 +02003692 newrows = 99999;
3693 newcols = 99999;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003694 for (twp = firstwin; ; twp = twp->w_next)
Bram Moolenaar498c2562018-04-15 23:45:15 +02003695 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003696 // Always use curwin, it may be a popup window.
3697 win_T *wwp = twp == NULL ? curwin : twp;
3698
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003699 // When more than one window shows the same terminal, use the
3700 // smallest size.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003701 if (wwp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003702 {
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003703 newrows = MIN(newrows, wwp->w_height);
3704 newcols = MIN(newcols, wwp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003705 }
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003706 if (twp == NULL)
3707 break;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003708 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003709 if (newrows == 99999 || newcols == 99999)
3710 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003711 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3712 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3713
3714 if (term->tl_rows != newrows || term->tl_cols != newcols)
3715 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003716 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003717 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003718 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003719 newrows);
3720 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003721
3722 // Updating the terminal size will cause the snapshot to be cleared.
3723 // When not in terminal_loop() we need to restore it.
3724 if (term != in_terminal_loop)
3725 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003726 }
3727
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003728 // The cursor may have been moved when resizing.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003729 vterm_state_get_cursorpos(state, &pos);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003730 position_cursor(wp, &pos, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003731
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003732 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3733 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003734 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003735 if (pos.row < term->tl_rows)
3736 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003737 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003738
Bram Moolenaar83d47902020-03-26 20:34:00 +01003739 term_line2screenline(term, wp, screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003740 }
3741 else
3742 pos.col = 0;
3743
Bram Moolenaarf118d482018-03-13 13:14:00 +01003744 screen_line(wp->w_winrow + pos.row
3745#ifdef FEAT_MENU
3746 + winbar_height(wp)
3747#endif
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003748 , wp->w_wincol, pos.col, wp->w_width,
3749#ifdef FEAT_PROP_POPUP
3750 popup_is_popup(wp) ? SLF_POPUP :
3751#endif
3752 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003753 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003754 term->tl_dirty_row_start = MAX_ROW;
3755 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003756}
3757
3758/*
3759 * Return TRUE if "wp" is a terminal window where the job has finished.
3760 */
3761 int
3762term_is_finished(buf_T *buf)
3763{
3764 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3765}
3766
3767/*
3768 * Return TRUE if "wp" is a terminal window where the job has finished or we
3769 * are in Terminal-Normal mode, thus we show the buffer contents.
3770 */
3771 int
3772term_show_buffer(buf_T *buf)
3773{
3774 term_T *term = buf->b_term;
3775
3776 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3777}
3778
3779/*
3780 * The current buffer is going to be changed. If there is terminal
3781 * highlighting remove it now.
3782 */
3783 void
3784term_change_in_curbuf(void)
3785{
3786 term_T *term = curbuf->b_term;
3787
3788 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3789 {
3790 free_scrollback(term);
3791 redraw_buf_later(term->tl_buffer, NOT_VALID);
3792
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003793 // The buffer is now like a normal buffer, it cannot be easily
3794 // abandoned when changed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003795 set_string_option_direct((char_u *)"buftype", -1,
3796 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3797 }
3798}
3799
3800/*
3801 * Get the screen attribute for a position in the buffer.
3802 * Use a negative "col" to get the filler background color.
3803 */
3804 int
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003805term_get_attr(win_T *wp, linenr_T lnum, int col)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003806{
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003807 buf_T *buf = wp->w_buffer;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003808 term_T *term = buf->b_term;
3809 sb_line_T *line;
3810 cellattr_T *cellattr;
3811
3812 if (lnum > term->tl_scrollback.ga_len)
3813 cellattr = &term->tl_default_color;
3814 else
3815 {
3816 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3817 if (col < 0 || col >= line->sb_cols)
3818 cellattr = &line->sb_fill_attr;
3819 else
3820 cellattr = line->sb_cells + col;
3821 }
Bram Moolenaar83d47902020-03-26 20:34:00 +01003822 return cell2attr(term, wp, cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003823}
3824
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003825/*
3826 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003827 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003828 */
3829 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003830cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003831{
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003832 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->ansi_index);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003833}
3834
3835/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003836 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003837 */
3838 static void
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003839init_default_colors(term_T *term, win_T *wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003840{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003841 VTermColor *fg, *bg;
3842 int fgval, bgval;
3843 int id;
3844
Bram Moolenaara80faa82020-04-12 19:37:17 +02003845 CLEAR_FIELD(term->tl_default_color.attrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003846 term->tl_default_color.width = 1;
3847 fg = &term->tl_default_color.fg;
3848 bg = &term->tl_default_color.bg;
3849
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003850 // Vterm uses a default black background. Set it to white when
3851 // 'background' is "light".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003852 if (*p_bg == 'l')
3853 {
3854 fgval = 0;
3855 bgval = 255;
3856 }
3857 else
3858 {
3859 fgval = 255;
3860 bgval = 0;
3861 }
3862 fg->red = fg->green = fg->blue = fgval;
3863 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01003864 fg->ansi_index = bg->ansi_index = VTERM_ANSI_INDEX_DEFAULT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003865
Bram Moolenaar83d47902020-03-26 20:34:00 +01003866 // The 'wincolor' or the highlight group overrules the defaults.
Bram Moolenaar219c7d02020-02-01 21:57:29 +01003867 if (wp != NULL && *wp->w_p_wcr != NUL)
3868 id = syn_name2id(wp->w_p_wcr);
3869 else
Bram Moolenaar83d47902020-03-26 20:34:00 +01003870 id = syn_name2id(term_get_highlight_name(term));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003871
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003872 // Use the actual color for the GUI and when 'termguicolors' is set.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003873#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3874 if (0
3875# ifdef FEAT_GUI
3876 || gui.in_use
3877# endif
3878# ifdef FEAT_TERMGUICOLORS
3879 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003880# ifdef FEAT_VTP
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003881 // Finally get INVALCOLOR on this execution path
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003882 || (!p_tgc && t_colors >= 256)
3883# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003884# endif
3885 )
3886 {
3887 guicolor_T fg_rgb = INVALCOLOR;
3888 guicolor_T bg_rgb = INVALCOLOR;
3889
3890 if (id != 0)
3891 syn_id2colors(id, &fg_rgb, &bg_rgb);
3892
3893# ifdef FEAT_GUI
3894 if (gui.in_use)
3895 {
3896 if (fg_rgb == INVALCOLOR)
3897 fg_rgb = gui.norm_pixel;
3898 if (bg_rgb == INVALCOLOR)
3899 bg_rgb = gui.back_pixel;
3900 }
3901# ifdef FEAT_TERMGUICOLORS
3902 else
3903# endif
3904# endif
3905# ifdef FEAT_TERMGUICOLORS
3906 {
3907 if (fg_rgb == INVALCOLOR)
3908 fg_rgb = cterm_normal_fg_gui_color;
3909 if (bg_rgb == INVALCOLOR)
3910 bg_rgb = cterm_normal_bg_gui_color;
3911 }
3912# endif
3913 if (fg_rgb != INVALCOLOR)
3914 {
3915 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3916
3917 fg->red = (unsigned)(rgb >> 16);
3918 fg->green = (unsigned)(rgb >> 8) & 255;
3919 fg->blue = (unsigned)rgb & 255;
3920 }
3921 if (bg_rgb != INVALCOLOR)
3922 {
3923 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
3924
3925 bg->red = (unsigned)(rgb >> 16);
3926 bg->green = (unsigned)(rgb >> 8) & 255;
3927 bg->blue = (unsigned)rgb & 255;
3928 }
3929 }
3930 else
3931#endif
3932 if (id != 0 && t_colors >= 16)
3933 {
Bram Moolenaar83d47902020-03-26 20:34:00 +01003934 int cterm_fg = get_default_cterm_fg(term);
3935 int cterm_bg = get_default_cterm_bg(term);
3936
3937 if (cterm_fg >= 0)
3938 cterm_color2vterm(cterm_fg, fg);
3939 if (cterm_bg >= 0)
3940 cterm_color2vterm(cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003941 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003942 else
3943 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003944#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003945 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003946#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003947
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01003948 // In an MS-Windows console we know the normal colors.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003949 if (cterm_normal_fg_color > 0)
3950 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003951 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003952# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3953# ifdef VIMDLL
3954 if (!gui.in_use)
3955# endif
3956 {
3957 tmp = fg->red;
3958 fg->red = fg->blue;
3959 fg->blue = tmp;
3960 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003961# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003962 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02003963# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003964 else
3965 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02003966# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003967
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003968 if (cterm_normal_bg_color > 0)
3969 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003970 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003971# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3972# ifdef VIMDLL
3973 if (!gui.in_use)
3974# endif
3975 {
3976 tmp = fg->red;
3977 fg->red = fg->blue;
3978 fg->blue = tmp;
3979 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003980# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003981 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02003982# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003983 else
3984 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02003985# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003986 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01003987}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003988
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02003989#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3990/*
3991 * Set the 16 ANSI colors from array of RGB values
3992 */
3993 static void
3994set_vterm_palette(VTerm *vterm, long_u *rgb)
3995{
3996 int index = 0;
3997 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01003998
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02003999 for (; index < 16; index++)
4000 {
4001 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02004002
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004003 color.red = (unsigned)(rgb[index] >> 16);
4004 color.green = (unsigned)(rgb[index] >> 8) & 255;
4005 color.blue = (unsigned)rgb[index] & 255;
4006 vterm_state_set_palette_color(state, index, &color);
4007 }
4008}
4009
4010/*
4011 * Set the ANSI color palette from a list of colors
4012 */
4013 static int
4014set_ansi_colors_list(VTerm *vterm, list_T *list)
4015{
4016 int n = 0;
4017 long_u rgb[16];
Bram Moolenaarb0992022020-01-30 14:55:42 +01004018 listitem_T *li;
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004019
Bram Moolenaarb0992022020-01-30 14:55:42 +01004020 for (li = list->lv_first; li != NULL && n < 16; li = li->li_next, n++)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004021 {
4022 char_u *color_name;
4023 guicolor_T guicolor;
4024
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004025 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004026 if (color_name == NULL)
4027 return FAIL;
4028
4029 guicolor = GUI_GET_COLOR(color_name);
4030 if (guicolor == INVALCOLOR)
4031 return FAIL;
4032
4033 rgb[n] = GUI_MCH_GET_RGB(guicolor);
4034 }
4035
4036 if (n != 16 || li != NULL)
4037 return FAIL;
4038
4039 set_vterm_palette(vterm, rgb);
4040
4041 return OK;
4042}
4043
4044/*
4045 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
4046 */
4047 static void
4048init_vterm_ansi_colors(VTerm *vterm)
4049{
4050 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
4051
4052 if (var != NULL
4053 && (var->di_tv.v_type != VAR_LIST
4054 || var->di_tv.vval.v_list == NULL
Bram Moolenaarb0992022020-01-30 14:55:42 +01004055 || var->di_tv.vval.v_list->lv_first == &range_list_item
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004056 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004057 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004058}
4059#endif
4060
Bram Moolenaar52acb112018-03-18 19:20:22 +01004061/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004062 * Handles a "drop" command from the job in the terminal.
4063 * "item" is the file name, "item->li_next" may have options.
4064 */
4065 static void
4066handle_drop_command(listitem_T *item)
4067{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004068 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004069 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004070 int bufnr;
4071 win_T *wp;
4072 tabpage_T *tp;
4073 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004074 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004075
4076 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
4077 FOR_ALL_TAB_WINDOWS(tp, wp)
4078 {
4079 if (wp->w_buffer->b_fnum == bufnr)
4080 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004081 // buffer is in a window already, go there
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004082 goto_tabpage_win(tp, wp);
4083 return;
4084 }
4085 }
4086
Bram Moolenaara80faa82020-04-12 19:37:17 +02004087 CLEAR_FIELD(ea);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004088
4089 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
4090 && opt_item->li_tv.vval.v_dict != NULL)
4091 {
4092 dict_T *dict = opt_item->li_tv.vval.v_dict;
4093 char_u *p;
4094
Bram Moolenaar8f667172018-12-14 15:38:31 +01004095 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004096 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004097 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004098 if (p != NULL)
4099 {
4100 if (check_ff_value(p) == FAIL)
4101 ch_log(NULL, "Invalid ff argument to drop: %s", p);
4102 else
4103 ea.force_ff = *p;
4104 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01004105 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004106 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004107 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004108 if (p != NULL)
4109 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004110 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004111 if (ea.cmd != NULL)
4112 {
4113 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
4114 ea.force_enc = 11;
4115 tofree = ea.cmd;
4116 }
4117 }
4118
Bram Moolenaar8f667172018-12-14 15:38:31 +01004119 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004120 if (p != NULL)
4121 get_bad_opt(p, &ea);
4122
4123 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
4124 ea.force_bin = FORCE_BIN;
4125 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
4126 ea.force_bin = FORCE_BIN;
4127 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
4128 ea.force_bin = FORCE_NOBIN;
4129 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
4130 ea.force_bin = FORCE_NOBIN;
4131 }
4132
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004133 // open in new window, like ":split fname"
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004134 if (ea.cmd == NULL)
4135 ea.cmd = (char_u *)"split";
4136 ea.arg = fname;
4137 ea.cmdidx = CMD_split;
4138 ex_splitview(&ea);
4139
4140 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004141}
4142
4143/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004144 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
4145 */
4146 static int
4147is_permitted_term_api(char_u *func, char_u *pat)
4148{
4149 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
4150}
4151
4152/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004153 * Handles a function call from the job running in a terminal.
4154 * "item" is the function name, "item->li_next" has the arguments.
4155 */
4156 static void
4157handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
4158{
4159 char_u *func;
4160 typval_T argvars[2];
4161 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004162 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004163
4164 if (item->li_next == NULL)
4165 {
4166 ch_log(channel, "Missing function arguments for call");
4167 return;
4168 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004169 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004170
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004171 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004172 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02004173 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004174 return;
4175 }
4176
4177 argvars[0].v_type = VAR_NUMBER;
4178 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
4179 argvars[1] = item->li_next->li_tv;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004180 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004181 funcexe.firstline = 1L;
4182 funcexe.lastline = 1L;
4183 funcexe.evaluate = TRUE;
4184 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004185 {
4186 clear_tv(&rettv);
4187 ch_log(channel, "Function %s called", func);
4188 }
4189 else
4190 ch_log(channel, "Calling function %s failed", func);
4191}
4192
4193/*
4194 * Called by libvterm when it cannot recognize an OSC sequence.
4195 * We recognize a terminal API command.
4196 */
4197 static int
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004198parse_osc(int command, VTermStringFragment frag, void *user)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004199{
4200 term_T *term = (term_T *)user;
4201 js_read_T reader;
4202 typval_T tv;
4203 channel_T *channel = term->tl_job == NULL ? NULL
4204 : term->tl_job->jv_channel;
4205
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004206 // We recognize only OSC 5 1 ; {command}
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004207 if (command != 51)
4208 return 0;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004209
Bram Moolenaarbe593bf2020-05-19 21:20:04 +02004210 reader.js_buf = vim_strnsave((char_u *)frag.str, (int)(frag.len));
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004211 if (reader.js_buf == NULL)
4212 return 1;
4213 reader.js_fill = NULL;
4214 reader.js_used = 0;
4215 if (json_decode(&reader, &tv, 0) == OK
4216 && tv.v_type == VAR_LIST
4217 && tv.vval.v_list != NULL)
4218 {
4219 listitem_T *item = tv.vval.v_list->lv_first;
4220
4221 if (item == NULL)
4222 ch_log(channel, "Missing command");
4223 else
4224 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004225 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004226
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004227 // Make sure an invoked command doesn't delete the buffer (and the
4228 // terminal) under our fingers.
Bram Moolenaara997b452018-04-17 23:24:06 +02004229 ++term->tl_buffer->b_locked;
4230
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004231 item = item->li_next;
4232 if (item == NULL)
4233 ch_log(channel, "Missing argument for %s", cmd);
4234 else if (STRCMP(cmd, "drop") == 0)
4235 handle_drop_command(item);
4236 else if (STRCMP(cmd, "call") == 0)
4237 handle_call_command(term, channel, item);
4238 else
4239 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02004240 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004241 }
4242 }
4243 else
4244 ch_log(channel, "Invalid JSON received");
4245
4246 vim_free(reader.js_buf);
4247 clear_tv(&tv);
4248 return 1;
4249}
4250
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004251/*
4252 * Called by libvterm when it cannot recognize a CSI sequence.
4253 * We recognize the window position report.
4254 */
4255 static int
4256parse_csi(
4257 const char *leader UNUSED,
4258 const long args[],
4259 int argcount,
4260 const char *intermed UNUSED,
4261 char command,
4262 void *user)
4263{
4264 term_T *term = (term_T *)user;
4265 char buf[100];
4266 int len;
4267 int x = 0;
4268 int y = 0;
4269 win_T *wp;
4270
4271 // We recognize only CSI 13 t
4272 if (command != 't' || argcount != 1 || args[0] != 13)
4273 return 0; // not handled
4274
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004275 // When getting the window position is not possible or it fails it results
4276 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02004277#if defined(FEAT_GUI) \
4278 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
4279 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004280 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02004281#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004282
4283 FOR_ALL_WINDOWS(wp)
4284 if (wp->w_buffer == term->tl_buffer)
4285 break;
4286 if (wp != NULL)
4287 {
4288#ifdef FEAT_GUI
4289 if (gui.in_use)
4290 {
4291 x += wp->w_wincol * gui.char_width;
4292 y += W_WINROW(wp) * gui.char_height;
4293 }
4294 else
4295#endif
4296 {
4297 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004298 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004299 x += wp->w_wincol * 7;
4300 y += W_WINROW(wp) * 10;
4301 }
4302 }
4303
4304 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
4305 channel_send(term->tl_job->jv_channel, get_tty_part(term),
4306 (char_u *)buf, len, NULL);
4307 return 1;
4308}
4309
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004310static VTermParserCallbacks parser_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02004311 NULL, // text
4312 NULL, // control
4313 NULL, // escape
4314 parse_csi, // csi
4315 parse_osc, // osc
4316 NULL, // dcs
4317 NULL // resize
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004318};
4319
4320/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02004321 * Use Vim's allocation functions for vterm so profiling works.
4322 */
4323 static void *
4324vterm_malloc(size_t size, void *data UNUSED)
4325{
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004326 return alloc_clear(size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02004327}
4328
4329 static void
4330vterm_memfree(void *ptr, void *data UNUSED)
4331{
4332 vim_free(ptr);
4333}
4334
4335static VTermAllocatorFunctions vterm_allocator = {
4336 &vterm_malloc,
4337 &vterm_memfree
4338};
4339
4340/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004341 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004342 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004343 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004344 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004345create_vterm(term_T *term, int rows, int cols)
4346{
4347 VTerm *vterm;
4348 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004349 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004350 VTermValue value;
4351
Bram Moolenaar756ef112018-04-10 12:04:27 +02004352 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004353 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004354 if (vterm == NULL)
4355 return FAIL;
4356
4357 // Allocate screen and state here, so we can bail out if that fails.
4358 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004359 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004360 if (state == NULL || screen == NULL)
4361 {
4362 vterm_free(vterm);
4363 return FAIL;
4364 }
4365
Bram Moolenaar52acb112018-03-18 19:20:22 +01004366 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004367 // TODO: depends on 'encoding'.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004368 vterm_set_utf8(vterm, 1);
4369
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004370 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004371
4372 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004373 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004374 &term->tl_default_color.fg,
4375 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004376
Bram Moolenaar9e587872019-05-13 20:27:23 +02004377 if (t_colors < 16)
4378 // Less than 16 colors: assume that bold means using a bright color for
4379 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004380 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4381
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004382 // Required to initialize most things.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004383 vterm_screen_reset(screen, 1 /* hard */);
4384
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004385 // Allow using alternate screen.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004386 vterm_screen_enable_altscreen(screen, 1);
4387
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004388 // For unix do not use a blinking cursor. In an xterm this causes the
4389 // cursor to blink if it's blinking in the xterm.
4390 // For Windows we respect the system wide setting.
Bram Moolenaar4f974752019-02-17 17:44:42 +01004391#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004392 if (GetCaretBlinkTime() == INFINITE)
4393 value.boolean = 0;
4394 else
4395 value.boolean = 1;
4396#else
4397 value.boolean = 0;
4398#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004399 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
4400 vterm_state_set_unrecognised_fallbacks(state, &parser_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004401
4402 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004403}
4404
4405/*
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004406 * Called when 'wincolor' was set.
4407 */
4408 void
4409term_update_colors(void)
4410{
4411 term_T *term = curwin->w_buffer->b_term;
4412
Bram Moolenaar7ba3b912020-02-10 20:34:04 +01004413 if (term->tl_vterm == NULL)
4414 return;
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004415 init_default_colors(term, curwin);
4416 vterm_state_set_default_colors(
4417 vterm_obtain_state(term->tl_vterm),
4418 &term->tl_default_color.fg,
4419 &term->tl_default_color.bg);
Bram Moolenaard5bc32d2020-03-22 19:25:50 +01004420
4421 redraw_later(NOT_VALID);
Bram Moolenaar219c7d02020-02-01 21:57:29 +01004422}
4423
4424/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004425 * Return the text to show for the buffer name and status.
4426 */
4427 char_u *
4428term_get_status_text(term_T *term)
4429{
4430 if (term->tl_status_text == NULL)
4431 {
4432 char_u *txt;
4433 size_t len;
4434
4435 if (term->tl_normal_mode)
4436 {
4437 if (term_job_running(term))
4438 txt = (char_u *)_("Terminal");
4439 else
4440 txt = (char_u *)_("Terminal-finished");
4441 }
4442 else if (term->tl_title != NULL)
4443 txt = term->tl_title;
4444 else if (term_none_open(term))
4445 txt = (char_u *)_("active");
4446 else if (term_job_running(term))
4447 txt = (char_u *)_("running");
4448 else
4449 txt = (char_u *)_("finished");
4450 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004451 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004452 if (term->tl_status_text != NULL)
4453 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
4454 term->tl_buffer->b_fname, txt);
4455 }
4456 return term->tl_status_text;
4457}
4458
4459/*
4460 * Mark references in jobs of terminals.
4461 */
4462 int
4463set_ref_in_term(int copyID)
4464{
4465 int abort = FALSE;
4466 term_T *term;
4467 typval_T tv;
4468
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004469 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004470 if (term->tl_job != NULL)
4471 {
4472 tv.v_type = VAR_JOB;
4473 tv.vval.v_job = term->tl_job;
4474 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4475 }
4476 return abort;
4477}
4478
4479/*
4480 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004481 * Returns NULL when the buffer is not for a terminal window and logs a message
4482 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004483 */
4484 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004485term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004486{
4487 buf_T *buf;
4488
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004489 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004490 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004491 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004492 --emsg_off;
4493 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004494 {
4495 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004496 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004497 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004498 return buf;
4499}
4500
Bram Moolenaard96ff162018-02-18 22:13:29 +01004501 static int
4502same_color(VTermColor *a, VTermColor *b)
4503{
4504 return a->red == b->red
4505 && a->green == b->green
4506 && a->blue == b->blue
4507 && a->ansi_index == b->ansi_index;
4508}
4509
4510 static void
4511dump_term_color(FILE *fd, VTermColor *color)
4512{
4513 fprintf(fd, "%02x%02x%02x%d",
4514 (int)color->red, (int)color->green, (int)color->blue,
4515 (int)color->ansi_index);
4516}
4517
4518/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004519 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004520 *
4521 * Each screen cell in full is:
4522 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4523 * {characters} is a space for an empty cell
4524 * For a double-width character "+" is changed to "*" and the next cell is
4525 * skipped.
4526 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4527 * when "&" use the same as the previous cell.
4528 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4529 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4530 * {color-idx} is a number from 0 to 255
4531 *
4532 * Screen cell with same width, attributes and color as the previous one:
4533 * |{characters}
4534 *
4535 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4536 *
4537 * Repeating the previous screen cell:
4538 * @{count}
4539 */
4540 void
4541f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4542{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004543 buf_T *buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004544 term_T *term;
4545 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004546 int max_height = 0;
4547 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004548 stat_T st;
4549 FILE *fd;
4550 VTermPos pos;
4551 VTermScreen *screen;
4552 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004553 VTermState *state;
4554 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004555
4556 if (check_restricted() || check_secure())
4557 return;
4558 if (buf == NULL)
4559 return;
4560 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004561 if (term->tl_vterm == NULL)
4562 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004563 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004564 return;
4565 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004566
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004567 if (argvars[2].v_type != VAR_UNKNOWN)
4568 {
4569 dict_T *d;
4570
4571 if (argvars[2].v_type != VAR_DICT)
4572 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004573 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004574 return;
4575 }
4576 d = argvars[2].vval.v_dict;
4577 if (d != NULL)
4578 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004579 max_height = dict_get_number(d, (char_u *)"rows");
4580 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004581 }
4582 }
4583
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004584 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004585 if (fname == NULL)
4586 return;
4587 if (mch_stat((char *)fname, &st) >= 0)
4588 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004589 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004590 return;
4591 }
4592
Bram Moolenaard96ff162018-02-18 22:13:29 +01004593 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4594 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004595 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004596 return;
4597 }
4598
Bram Moolenaara80faa82020-04-12 19:37:17 +02004599 CLEAR_FIELD(prev_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004600
4601 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004602 state = vterm_obtain_state(term->tl_vterm);
4603 vterm_state_get_cursorpos(state, &cursor_pos);
4604
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004605 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4606 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004607 {
4608 int repeat = 0;
4609
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004610 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4611 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004612 {
4613 VTermScreenCell cell;
4614 int same_attr;
4615 int same_chars = TRUE;
4616 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004617 int is_cursor_pos = (pos.col == cursor_pos.col
4618 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004619
4620 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004621 CLEAR_FIELD(cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004622
4623 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4624 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004625 int c = cell.chars[i];
4626 int pc = prev_cell.chars[i];
4627
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004628 // For the first character NUL is the same as space.
Bram Moolenaar47015b82018-03-23 22:10:34 +01004629 if (i == 0)
4630 {
4631 c = (c == NUL) ? ' ' : c;
4632 pc = (pc == NUL) ? ' ' : pc;
4633 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004634 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004635 same_chars = FALSE;
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004636 if (c == NUL || pc == NUL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004637 break;
4638 }
4639 same_attr = vtermAttr2hl(cell.attrs)
4640 == vtermAttr2hl(prev_cell.attrs)
4641 && same_color(&cell.fg, &prev_cell.fg)
4642 && same_color(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004643 if (same_chars && cell.width == prev_cell.width && same_attr
4644 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004645 {
4646 ++repeat;
4647 }
4648 else
4649 {
4650 if (repeat > 0)
4651 {
4652 fprintf(fd, "@%d", repeat);
4653 repeat = 0;
4654 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004655 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004656
4657 if (cell.chars[0] == NUL)
4658 fputs(" ", fd);
4659 else
4660 {
4661 char_u charbuf[10];
4662 int len;
4663
4664 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4665 && cell.chars[i] != NUL; ++i)
4666 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004667 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004668 fwrite(charbuf, len, 1, fd);
4669 }
4670 }
4671
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004672 // When only the characters differ we don't write anything, the
4673 // following "|", "@" or NL will indicate using the same
4674 // attributes.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004675 if (cell.width != prev_cell.width || !same_attr)
4676 {
4677 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004678 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004679 else
4680 fputs("+", fd);
4681
4682 if (same_attr)
4683 {
4684 fputs("&", fd);
4685 }
4686 else
4687 {
4688 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
4689 if (same_color(&cell.fg, &prev_cell.fg))
4690 fputs("&", fd);
4691 else
4692 {
4693 fputs("#", fd);
4694 dump_term_color(fd, &cell.fg);
4695 }
4696 if (same_color(&cell.bg, &prev_cell.bg))
4697 fputs("&", fd);
4698 else
4699 {
4700 fputs("#", fd);
4701 dump_term_color(fd, &cell.bg);
4702 }
4703 }
4704 }
4705
4706 prev_cell = cell;
4707 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004708
4709 if (cell.width == 2)
4710 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004711 }
4712 if (repeat > 0)
4713 fprintf(fd, "@%d", repeat);
4714 fputs("\n", fd);
4715 }
4716
4717 fclose(fd);
4718}
4719
4720/*
4721 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4722 */
4723 static void
4724dump_is_corrupt(garray_T *gap)
4725{
4726 ga_concat(gap, (char_u *)"CORRUPT");
4727}
4728
4729 static void
4730append_cell(garray_T *gap, cellattr_T *cell)
4731{
4732 if (ga_grow(gap, 1) == OK)
4733 {
4734 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4735 ++gap->ga_len;
4736 }
4737}
4738
4739/*
4740 * Read the dump file from "fd" and append lines to the current buffer.
4741 * Return the cell width of the longest line.
4742 */
4743 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004744read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004745{
4746 int c;
4747 garray_T ga_text;
4748 garray_T ga_cell;
4749 char_u *prev_char = NULL;
4750 int attr = 0;
4751 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004752 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004753 term_T *term = curbuf->b_term;
4754 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004755 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004756
4757 ga_init2(&ga_text, 1, 90);
4758 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
Bram Moolenaara80faa82020-04-12 19:37:17 +02004759 CLEAR_FIELD(cell);
4760 CLEAR_FIELD(empty_cell);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004761 cursor_pos->row = -1;
4762 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004763
4764 c = fgetc(fd);
4765 for (;;)
4766 {
4767 if (c == EOF)
4768 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004769 if (c == '\r')
4770 {
4771 // DOS line endings? Ignore.
4772 c = fgetc(fd);
4773 }
4774 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004775 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004776 // End of a line: append it to the buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01004777 if (ga_text.ga_data == NULL)
4778 dump_is_corrupt(&ga_text);
4779 if (ga_grow(&term->tl_scrollback, 1) == OK)
4780 {
4781 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
4782 + term->tl_scrollback.ga_len;
4783
4784 if (max_cells < ga_cell.ga_len)
4785 max_cells = ga_cell.ga_len;
4786 line->sb_cols = ga_cell.ga_len;
4787 line->sb_cells = ga_cell.ga_data;
4788 line->sb_fill_attr = term->tl_default_color;
4789 ++term->tl_scrollback.ga_len;
4790 ga_init(&ga_cell);
4791
4792 ga_append(&ga_text, NUL);
4793 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4794 ga_text.ga_len, FALSE);
4795 }
4796 else
4797 ga_clear(&ga_cell);
4798 ga_text.ga_len = 0;
4799
4800 c = fgetc(fd);
4801 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004802 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004803 {
4804 int prev_len = ga_text.ga_len;
4805
Bram Moolenaar9271d052018-02-25 21:39:46 +01004806 if (c == '>')
4807 {
4808 if (cursor_pos->row != -1)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004809 dump_is_corrupt(&ga_text); // duplicate cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01004810 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
4811 cursor_pos->col = ga_cell.ga_len;
4812 }
4813
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004814 // normal character(s) followed by "+", "*", "|", "@" or NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01004815 c = fgetc(fd);
4816 if (c != EOF)
4817 ga_append(&ga_text, c);
4818 for (;;)
4819 {
4820 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004821 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01004822 || c == EOF || c == '\n')
4823 break;
4824 ga_append(&ga_text, c);
4825 }
4826
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004827 // save the character for repeating it
Bram Moolenaard96ff162018-02-18 22:13:29 +01004828 vim_free(prev_char);
4829 if (ga_text.ga_data != NULL)
4830 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
4831 ga_text.ga_len - prev_len);
4832
Bram Moolenaar9271d052018-02-25 21:39:46 +01004833 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004834 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004835 // use all attributes from previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01004836 }
4837 else if (c == '+' || c == '*')
4838 {
4839 int is_bg;
4840
4841 cell.width = c == '+' ? 1 : 2;
4842
4843 c = fgetc(fd);
4844 if (c == '&')
4845 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004846 // use same attr as previous cell
Bram Moolenaard96ff162018-02-18 22:13:29 +01004847 c = fgetc(fd);
4848 }
4849 else if (isdigit(c))
4850 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004851 // get the decimal attribute
Bram Moolenaard96ff162018-02-18 22:13:29 +01004852 attr = 0;
4853 while (isdigit(c))
4854 {
4855 attr = attr * 10 + (c - '0');
4856 c = fgetc(fd);
4857 }
4858 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004859
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004860 // is_bg == 0: fg, is_bg == 1: bg
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004861 for (is_bg = 0; is_bg <= 1; ++is_bg)
4862 {
4863 if (c == '&')
4864 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004865 // use same color as previous cell
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004866 c = fgetc(fd);
4867 }
4868 else if (c == '#')
4869 {
4870 int red, green, blue, index = 0;
4871
4872 c = fgetc(fd);
4873 red = hex2nr(c);
4874 c = fgetc(fd);
4875 red = (red << 4) + hex2nr(c);
4876 c = fgetc(fd);
4877 green = hex2nr(c);
4878 c = fgetc(fd);
4879 green = (green << 4) + hex2nr(c);
4880 c = fgetc(fd);
4881 blue = hex2nr(c);
4882 c = fgetc(fd);
4883 blue = (blue << 4) + hex2nr(c);
4884 c = fgetc(fd);
4885 if (!isdigit(c))
4886 dump_is_corrupt(&ga_text);
4887 while (isdigit(c))
4888 {
4889 index = index * 10 + (c - '0');
4890 c = fgetc(fd);
4891 }
4892
4893 if (is_bg)
4894 {
4895 cell.bg.red = red;
4896 cell.bg.green = green;
4897 cell.bg.blue = blue;
4898 cell.bg.ansi_index = index;
4899 }
4900 else
4901 {
4902 cell.fg.red = red;
4903 cell.fg.green = green;
4904 cell.fg.blue = blue;
4905 cell.fg.ansi_index = index;
4906 }
4907 }
4908 else
4909 dump_is_corrupt(&ga_text);
4910 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004911 }
4912 else
4913 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004914 }
4915 else
4916 dump_is_corrupt(&ga_text);
4917
4918 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004919 if (cell.width == 2)
4920 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004921 }
4922 else if (c == '@')
4923 {
4924 if (prev_char == NULL)
4925 dump_is_corrupt(&ga_text);
4926 else
4927 {
4928 int count = 0;
4929
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004930 // repeat previous character, get the count
Bram Moolenaard96ff162018-02-18 22:13:29 +01004931 for (;;)
4932 {
4933 c = fgetc(fd);
4934 if (!isdigit(c))
4935 break;
4936 count = count * 10 + (c - '0');
4937 }
4938
4939 while (count-- > 0)
4940 {
4941 ga_concat(&ga_text, prev_char);
4942 append_cell(&ga_cell, &cell);
4943 }
4944 }
4945 }
4946 else
4947 {
4948 dump_is_corrupt(&ga_text);
4949 c = fgetc(fd);
4950 }
4951 }
4952
4953 if (ga_text.ga_len > 0)
4954 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004955 // trailing characters after last NL
Bram Moolenaard96ff162018-02-18 22:13:29 +01004956 dump_is_corrupt(&ga_text);
4957 ga_append(&ga_text, NUL);
4958 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4959 ga_text.ga_len, FALSE);
4960 }
4961
4962 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02004963 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004964 vim_free(prev_char);
4965
4966 return max_cells;
4967}
4968
4969/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02004970 * Return an allocated string with at least "text_width" "=" characters and
4971 * "fname" inserted in the middle.
4972 */
4973 static char_u *
4974get_separator(int text_width, char_u *fname)
4975{
4976 int width = MAX(text_width, curwin->w_width);
4977 char_u *textline;
4978 int fname_size;
4979 char_u *p = fname;
4980 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02004981 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02004982
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02004983 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02004984 if (textline == NULL)
4985 return NULL;
4986
4987 fname_size = vim_strsize(fname);
4988 if (fname_size < width - 8)
4989 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004990 // enough room, don't use the full window width
Bram Moolenaar4a696342018-04-05 18:45:26 +02004991 width = MAX(text_width, fname_size + 8);
4992 }
4993 else if (fname_size > width - 8)
4994 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004995 // full name doesn't fit, use only the tail
Bram Moolenaar4a696342018-04-05 18:45:26 +02004996 p = gettail(fname);
4997 fname_size = vim_strsize(p);
4998 }
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01004999 // skip characters until the name fits
Bram Moolenaar4a696342018-04-05 18:45:26 +02005000 while (fname_size > width - 8)
5001 {
5002 p += (*mb_ptr2len)(p);
5003 fname_size = vim_strsize(p);
5004 }
5005
5006 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
5007 textline[i] = '=';
5008 textline[i++] = ' ';
5009
5010 STRCPY(textline + i, p);
5011 off = STRLEN(textline);
5012 textline[off] = ' ';
5013 for (i = 1; i < (width - fname_size) / 2; ++i)
5014 textline[off + i] = '=';
5015 textline[off + i] = NUL;
5016
5017 return textline;
5018}
5019
5020/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01005021 * Common for "term_dumpdiff()" and "term_dumpload()".
5022 */
5023 static void
5024term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
5025{
5026 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02005027 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005028 char_u buf1[NUMBUFLEN];
5029 char_u buf2[NUMBUFLEN];
5030 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005031 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005032 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005033 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005034 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005035 char_u *textline = NULL;
5036
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005037 // First open the files. If this fails bail out.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005038 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005039 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005040 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005041 if (fname1 == NULL || (do_diff && fname2 == NULL))
5042 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005043 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01005044 return;
5045 }
5046 fd1 = mch_fopen((char *)fname1, READBIN);
5047 if (fd1 == NULL)
5048 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005049 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005050 return;
5051 }
5052 if (do_diff)
5053 {
5054 fd2 = mch_fopen((char *)fname2, READBIN);
5055 if (fd2 == NULL)
5056 {
5057 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005058 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005059 return;
5060 }
5061 }
5062
5063 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005064 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
5065 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
5066 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
5067 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
5068 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005069
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005070 if (opt.jo_term_name == NULL)
5071 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01005072 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005073
Bram Moolenaar51e14382019-05-25 20:21:28 +02005074 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005075 if (fname_tofree != NULL)
5076 {
5077 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
5078 opt.jo_term_name = fname_tofree;
5079 }
5080 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005081
Bram Moolenaar87abab92019-06-03 21:14:59 +02005082 if (opt.jo_bufnr_buf != NULL)
5083 {
5084 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
5085
5086 // With "bufnr" argument: enter the window with this buffer and make it
5087 // empty.
5088 if (wp == NULL)
5089 semsg(_(e_invarg2), "bufnr");
5090 else
5091 {
5092 buf = curbuf;
5093 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
5094 ml_delete((linenr_T)1, FALSE);
Bram Moolenaar86173482019-10-01 17:02:16 +02005095 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02005096 redraw_later(NOT_VALID);
5097 }
5098 }
5099 else
5100 // Create a new terminal window.
5101 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
5102
Bram Moolenaard96ff162018-02-18 22:13:29 +01005103 if (buf != NULL && buf->b_term != NULL)
5104 {
5105 int i;
5106 linenr_T bot_lnum;
5107 linenr_T lnum;
5108 term_T *term = buf->b_term;
5109 int width;
5110 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005111 VTermPos cursor_pos1;
5112 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005113
Bram Moolenaar219c7d02020-02-01 21:57:29 +01005114 init_default_colors(term, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01005115
Bram Moolenaard96ff162018-02-18 22:13:29 +01005116 rettv->vval.v_number = buf->b_fnum;
5117
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005118 // read the files, fill the buffer with the diff
Bram Moolenaar9271d052018-02-25 21:39:46 +01005119 width = read_dump_file(fd1, &cursor_pos1);
5120
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005121 // position the cursor
Bram Moolenaar9271d052018-02-25 21:39:46 +01005122 if (cursor_pos1.row >= 0)
5123 {
5124 curwin->w_cursor.lnum = cursor_pos1.row + 1;
5125 coladvance(cursor_pos1.col);
5126 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005127
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005128 // Delete the empty line that was in the empty buffer.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005129 ml_delete(1, FALSE);
5130
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005131 // For term_dumpload() we are done here.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005132 if (!do_diff)
5133 goto theend;
5134
5135 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
5136
Bram Moolenaar4a696342018-04-05 18:45:26 +02005137 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005138 if (textline == NULL)
5139 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005140 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5141 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
5142 vim_free(textline);
5143
5144 textline = get_separator(width, fname2);
5145 if (textline == NULL)
5146 goto theend;
5147 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
5148 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005149 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005150
5151 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01005152 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005153 if (width2 > width)
5154 {
5155 vim_free(textline);
5156 textline = alloc(width2 + 1);
5157 if (textline == NULL)
5158 goto theend;
5159 width = width2;
5160 textline[width] = NUL;
5161 }
5162 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
5163
5164 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
5165 {
5166 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
5167 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005168 // bottom part has fewer rows, fill with "-"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005169 for (i = 0; i < width; ++i)
5170 textline[i] = '-';
5171 }
5172 else
5173 {
5174 char_u *line1;
5175 char_u *line2;
5176 char_u *p1;
5177 char_u *p2;
5178 int col;
5179 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5180 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
5181 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
5182 ->sb_cells;
5183
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005184 // Make a copy, getting the second line will invalidate it.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005185 line1 = vim_strsave(ml_get(lnum));
5186 if (line1 == NULL)
5187 break;
5188 p1 = line1;
5189
5190 line2 = ml_get(lnum + bot_lnum);
5191 p2 = line2;
5192 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
5193 {
5194 int len1 = utfc_ptr2len(p1);
5195 int len2 = utfc_ptr2len(p2);
5196
5197 textline[col] = ' ';
5198 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005199 // text differs
Bram Moolenaard96ff162018-02-18 22:13:29 +01005200 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01005201 else if (lnum == cursor_pos1.row + 1
5202 && col == cursor_pos1.col
5203 && (cursor_pos1.row != cursor_pos2.row
5204 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005205 // cursor in first but not in second
Bram Moolenaar9271d052018-02-25 21:39:46 +01005206 textline[col] = '>';
5207 else if (lnum == cursor_pos2.row + 1
5208 && col == cursor_pos2.col
5209 && (cursor_pos1.row != cursor_pos2.row
5210 || cursor_pos1.col != cursor_pos2.col))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005211 // cursor in second but not in first
Bram Moolenaar9271d052018-02-25 21:39:46 +01005212 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01005213 else if (cellattr1 != NULL && cellattr2 != NULL)
5214 {
5215 if ((cellattr1 + col)->width
5216 != (cellattr2 + col)->width)
5217 textline[col] = 'w';
5218 else if (!same_color(&(cellattr1 + col)->fg,
5219 &(cellattr2 + col)->fg))
5220 textline[col] = 'f';
5221 else if (!same_color(&(cellattr1 + col)->bg,
5222 &(cellattr2 + col)->bg))
5223 textline[col] = 'b';
5224 else if (vtermAttr2hl((cellattr1 + col)->attrs)
5225 != vtermAttr2hl(((cellattr2 + col)->attrs)))
5226 textline[col] = 'a';
5227 }
5228 p1 += len1;
5229 p2 += len2;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005230 // TODO: handle different width
Bram Moolenaard96ff162018-02-18 22:13:29 +01005231 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01005232
5233 while (col < width)
5234 {
5235 if (*p1 == NUL && *p2 == NUL)
5236 textline[col] = '?';
5237 else if (*p1 == NUL)
5238 {
5239 textline[col] = '+';
5240 p2 += utfc_ptr2len(p2);
5241 }
5242 else
5243 {
5244 textline[col] = '-';
5245 p1 += utfc_ptr2len(p1);
5246 }
5247 ++col;
5248 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01005249
5250 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005251 }
5252 if (add_empty_scrollback(term, &term->tl_default_color,
5253 term->tl_top_diff_rows) == OK)
5254 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5255 ++bot_lnum;
5256 }
5257
5258 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
5259 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005260 // bottom part has more rows, fill with "+"
Bram Moolenaard96ff162018-02-18 22:13:29 +01005261 for (i = 0; i < width; ++i)
5262 textline[i] = '+';
5263 if (add_empty_scrollback(term, &term->tl_default_color,
5264 term->tl_top_diff_rows) == OK)
5265 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
5266 ++lnum;
5267 ++bot_lnum;
5268 }
5269
5270 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02005271
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005272 // looks better without wrapping
Bram Moolenaar4a696342018-04-05 18:45:26 +02005273 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01005274 }
5275
5276theend:
5277 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01005278 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005279 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01005280 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01005281 fclose(fd2);
5282}
5283
5284/*
5285 * If the current buffer shows the output of term_dumpdiff(), swap the top and
5286 * bottom files.
5287 * Return FAIL when this is not possible.
5288 */
5289 int
5290term_swap_diff()
5291{
5292 term_T *term = curbuf->b_term;
5293 linenr_T line_count;
5294 linenr_T top_rows;
5295 linenr_T bot_rows;
5296 linenr_T bot_start;
5297 linenr_T lnum;
5298 char_u *p;
5299 sb_line_T *sb_line;
5300
5301 if (term == NULL
5302 || !term_is_finished(curbuf)
5303 || term->tl_top_diff_rows == 0
5304 || term->tl_scrollback.ga_len == 0)
5305 return FAIL;
5306
5307 line_count = curbuf->b_ml.ml_line_count;
5308 top_rows = term->tl_top_diff_rows;
5309 bot_rows = term->tl_bot_diff_rows;
5310 bot_start = line_count - bot_rows;
5311 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
5312
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005313 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01005314 for (lnum = 1; lnum <= top_rows; ++lnum)
5315 {
5316 p = vim_strsave(ml_get(1));
5317 if (p == NULL)
5318 return OK;
5319 ml_append(bot_start, p, 0, FALSE);
5320 ml_delete(1, FALSE);
5321 vim_free(p);
5322 }
5323
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005324 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01005325 for (lnum = 1; lnum <= bot_rows; ++lnum)
5326 {
5327 p = vim_strsave(ml_get(bot_start + lnum));
5328 if (p == NULL)
5329 return OK;
5330 ml_delete(bot_start + lnum, FALSE);
5331 ml_append(lnum - 1, p, 0, FALSE);
5332 vim_free(p);
5333 }
5334
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01005335 // move top title to bottom
5336 p = vim_strsave(ml_get(bot_rows + 1));
5337 if (p == NULL)
5338 return OK;
5339 ml_append(line_count - top_rows - 1, p, 0, FALSE);
5340 ml_delete(bot_rows + 1, FALSE);
5341 vim_free(p);
5342
5343 // move bottom title to top
5344 p = vim_strsave(ml_get(line_count - top_rows));
5345 if (p == NULL)
5346 return OK;
5347 ml_delete(line_count - top_rows, FALSE);
5348 ml_append(bot_rows, p, 0, FALSE);
5349 vim_free(p);
5350
Bram Moolenaard96ff162018-02-18 22:13:29 +01005351 if (top_rows == bot_rows)
5352 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005353 // rows counts are equal, can swap cell properties
Bram Moolenaard96ff162018-02-18 22:13:29 +01005354 for (lnum = 0; lnum < top_rows; ++lnum)
5355 {
5356 sb_line_T temp;
5357
5358 temp = *(sb_line + lnum);
5359 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5360 *(sb_line + bot_start + lnum) = temp;
5361 }
5362 }
5363 else
5364 {
5365 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005366 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005367
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005368 // need to copy cell properties into temp memory
Bram Moolenaard96ff162018-02-18 22:13:29 +01005369 if (temp != NULL)
5370 {
5371 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5372 mch_memmove(term->tl_scrollback.ga_data,
5373 temp + bot_start,
5374 sizeof(sb_line_T) * bot_rows);
5375 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5376 temp + top_rows,
5377 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5378 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5379 + line_count - top_rows,
5380 temp,
5381 sizeof(sb_line_T) * top_rows);
5382 vim_free(temp);
5383 }
5384 }
5385
5386 term->tl_top_diff_rows = bot_rows;
5387 term->tl_bot_diff_rows = top_rows;
5388
5389 update_screen(NOT_VALID);
5390 return OK;
5391}
5392
5393/*
5394 * "term_dumpdiff(filename, filename, options)" function
5395 */
5396 void
5397f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5398{
5399 term_load_dump(argvars, rettv, TRUE);
5400}
5401
5402/*
5403 * "term_dumpload(filename, options)" function
5404 */
5405 void
5406f_term_dumpload(typval_T *argvars, typval_T *rettv)
5407{
5408 term_load_dump(argvars, rettv, FALSE);
5409}
5410
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005411/*
5412 * "term_getaltscreen(buf)" function
5413 */
5414 void
5415f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5416{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005417 buf_T *buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005418
5419 if (buf == NULL)
5420 return;
5421 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5422}
5423
5424/*
5425 * "term_getattr(attr, name)" function
5426 */
5427 void
5428f_term_getattr(typval_T *argvars, typval_T *rettv)
5429{
5430 int attr;
5431 size_t i;
5432 char_u *name;
5433
5434 static struct {
5435 char *name;
5436 int attr;
5437 } attrs[] = {
5438 {"bold", HL_BOLD},
5439 {"italic", HL_ITALIC},
5440 {"underline", HL_UNDERLINE},
5441 {"strike", HL_STRIKETHROUGH},
5442 {"reverse", HL_INVERSE},
5443 };
5444
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005445 attr = tv_get_number(&argvars[0]);
5446 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005447 if (name == NULL)
5448 return;
5449
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005450 if (attr > HL_ALL)
5451 attr = syn_attr2attr(attr);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005452 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
5453 if (STRCMP(name, attrs[i].name) == 0)
5454 {
5455 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5456 break;
5457 }
5458}
5459
5460/*
5461 * "term_getcursor(buf)" function
5462 */
5463 void
5464f_term_getcursor(typval_T *argvars, typval_T *rettv)
5465{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005466 buf_T *buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005467 term_T *term;
5468 list_T *l;
5469 dict_T *d;
5470
5471 if (rettv_list_alloc(rettv) == FAIL)
5472 return;
5473 if (buf == NULL)
5474 return;
5475 term = buf->b_term;
5476
5477 l = rettv->vval.v_list;
5478 list_append_number(l, term->tl_cursor_pos.row + 1);
5479 list_append_number(l, term->tl_cursor_pos.col + 1);
5480
5481 d = dict_alloc();
5482 if (d != NULL)
5483 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005484 dict_add_number(d, "visible", term->tl_cursor_visible);
5485 dict_add_number(d, "blink", blink_state_is_inverted()
5486 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5487 dict_add_number(d, "shape", term->tl_cursor_shape);
5488 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005489 list_append_dict(l, d);
5490 }
5491}
5492
5493/*
5494 * "term_getjob(buf)" function
5495 */
5496 void
5497f_term_getjob(typval_T *argvars, typval_T *rettv)
5498{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005499 buf_T *buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005500
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005501 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005502 {
5503 rettv->v_type = VAR_SPECIAL;
5504 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005505 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005506 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005507
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005508 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005509 rettv->vval.v_job = buf->b_term->tl_job;
5510 if (rettv->vval.v_job != NULL)
5511 ++rettv->vval.v_job->jv_refcount;
5512}
5513
5514 static int
5515get_row_number(typval_T *tv, term_T *term)
5516{
5517 if (tv->v_type == VAR_STRING
5518 && tv->vval.v_string != NULL
5519 && STRCMP(tv->vval.v_string, ".") == 0)
5520 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005521 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005522}
5523
5524/*
5525 * "term_getline(buf, row)" function
5526 */
5527 void
5528f_term_getline(typval_T *argvars, typval_T *rettv)
5529{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005530 buf_T *buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005531 term_T *term;
5532 int row;
5533
5534 rettv->v_type = VAR_STRING;
5535 if (buf == NULL)
5536 return;
5537 term = buf->b_term;
5538 row = get_row_number(&argvars[1], term);
5539
5540 if (term->tl_vterm == NULL)
5541 {
5542 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5543
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005544 // vterm is finished, get the text from the buffer
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005545 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5546 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5547 }
5548 else
5549 {
5550 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5551 VTermRect rect;
5552 int len;
5553 char_u *p;
5554
5555 if (row < 0 || row >= term->tl_rows)
5556 return;
5557 len = term->tl_cols * MB_MAXBYTES + 1;
5558 p = alloc(len);
5559 if (p == NULL)
5560 return;
5561 rettv->vval.v_string = p;
5562
5563 rect.start_col = 0;
5564 rect.end_col = term->tl_cols;
5565 rect.start_row = row;
5566 rect.end_row = row + 1;
5567 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5568 }
5569}
5570
5571/*
5572 * "term_getscrolled(buf)" function
5573 */
5574 void
5575f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5576{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005577 buf_T *buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005578
5579 if (buf == NULL)
5580 return;
5581 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5582}
5583
5584/*
5585 * "term_getsize(buf)" function
5586 */
5587 void
5588f_term_getsize(typval_T *argvars, typval_T *rettv)
5589{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005590 buf_T *buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005591 list_T *l;
5592
5593 if (rettv_list_alloc(rettv) == FAIL)
5594 return;
5595 if (buf == NULL)
5596 return;
5597
5598 l = rettv->vval.v_list;
5599 list_append_number(l, buf->b_term->tl_rows);
5600 list_append_number(l, buf->b_term->tl_cols);
5601}
5602
5603/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005604 * "term_setsize(buf, rows, cols)" function
5605 */
5606 void
5607f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5608{
5609 buf_T *buf = term_get_buf(argvars, "term_setsize()");
5610 term_T *term;
5611 varnumber_T rows, cols;
5612
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005613 if (buf == NULL)
5614 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005615 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005616 return;
5617 }
5618 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005619 return;
5620 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005621 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005622 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005623 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005624 cols = cols <= 0 ? term->tl_cols : cols;
5625 vterm_set_size(term->tl_vterm, rows, cols);
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005626 // handle_resize() will resize the windows
Bram Moolenaara42d3632018-04-14 17:05:38 +02005627
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005628 // Get and remember the size we ended up with. Update the pty.
Bram Moolenaara42d3632018-04-14 17:05:38 +02005629 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5630 term_report_winsize(term, term->tl_rows, term->tl_cols);
5631}
5632
5633/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005634 * "term_getstatus(buf)" function
5635 */
5636 void
5637f_term_getstatus(typval_T *argvars, typval_T *rettv)
5638{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005639 buf_T *buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005640 term_T *term;
5641 char_u val[100];
5642
5643 rettv->v_type = VAR_STRING;
5644 if (buf == NULL)
5645 return;
5646 term = buf->b_term;
5647
5648 if (term_job_running(term))
5649 STRCPY(val, "running");
5650 else
5651 STRCPY(val, "finished");
5652 if (term->tl_normal_mode)
5653 STRCAT(val, ",normal");
5654 rettv->vval.v_string = vim_strsave(val);
5655}
5656
5657/*
5658 * "term_gettitle(buf)" function
5659 */
5660 void
5661f_term_gettitle(typval_T *argvars, typval_T *rettv)
5662{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005663 buf_T *buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005664
5665 rettv->v_type = VAR_STRING;
5666 if (buf == NULL)
5667 return;
5668
5669 if (buf->b_term->tl_title != NULL)
5670 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5671}
5672
5673/*
5674 * "term_gettty(buf)" function
5675 */
5676 void
5677f_term_gettty(typval_T *argvars, typval_T *rettv)
5678{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005679 buf_T *buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005680 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005681 int num = 0;
5682
5683 rettv->v_type = VAR_STRING;
5684 if (buf == NULL)
5685 return;
5686 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005687 num = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005688
5689 switch (num)
5690 {
5691 case 0:
5692 if (buf->b_term->tl_job != NULL)
5693 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005694 break;
5695 case 1:
5696 if (buf->b_term->tl_job != NULL)
5697 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005698 break;
5699 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005700 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005701 return;
5702 }
5703 if (p != NULL)
5704 rettv->vval.v_string = vim_strsave(p);
5705}
5706
5707/*
5708 * "term_list()" function
5709 */
5710 void
5711f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
5712{
5713 term_T *tp;
5714 list_T *l;
5715
5716 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
5717 return;
5718
5719 l = rettv->vval.v_list;
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005720 FOR_ALL_TERMS(tp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005721 if (tp != NULL && tp->tl_buffer != NULL)
5722 if (list_append_number(l,
5723 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
5724 return;
5725}
5726
5727/*
5728 * "term_scrape(buf, row)" function
5729 */
5730 void
5731f_term_scrape(typval_T *argvars, typval_T *rettv)
5732{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005733 buf_T *buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005734 VTermScreen *screen = NULL;
5735 VTermPos pos;
5736 list_T *l;
5737 term_T *term;
5738 char_u *p;
5739 sb_line_T *line;
5740
5741 if (rettv_list_alloc(rettv) == FAIL)
5742 return;
5743 if (buf == NULL)
5744 return;
5745 term = buf->b_term;
5746
5747 l = rettv->vval.v_list;
5748 pos.row = get_row_number(&argvars[1], term);
5749
5750 if (term->tl_vterm != NULL)
5751 {
5752 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01005753 if (screen == NULL) // can't really happen
5754 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005755 p = NULL;
5756 line = NULL;
5757 }
5758 else
5759 {
5760 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
5761
5762 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
5763 return;
5764 p = ml_get_buf(buf, lnum + 1, FALSE);
5765 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
5766 }
5767
5768 for (pos.col = 0; pos.col < term->tl_cols; )
5769 {
5770 dict_T *dcell;
5771 int width;
5772 VTermScreenCellAttrs attrs;
5773 VTermColor fg, bg;
5774 char_u rgb[8];
5775 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
5776 int off = 0;
5777 int i;
5778
5779 if (screen == NULL)
5780 {
5781 cellattr_T *cellattr;
5782 int len;
5783
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01005784 // vterm has finished, get the cell from scrollback
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005785 if (pos.col >= line->sb_cols)
5786 break;
5787 cellattr = line->sb_cells + pos.col;
5788 width = cellattr->width;
5789 attrs = cellattr->attrs;
5790 fg = cellattr->fg;
5791 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02005792 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005793 mch_memmove(mbs, p, len);
5794 mbs[len] = NUL;
5795 p += len;
5796 }
5797 else
5798 {
5799 VTermScreenCell cell;
5800 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
5801 break;
5802 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
5803 {
5804 if (cell.chars[i] == 0)
5805 break;
5806 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
5807 }
5808 mbs[off] = NUL;
5809 width = cell.width;
5810 attrs = cell.attrs;
5811 fg = cell.fg;
5812 bg = cell.bg;
5813 }
5814 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01005815 if (dcell == NULL)
5816 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005817 list_append_dict(l, dcell);
5818
Bram Moolenaare0be1672018-07-08 16:50:37 +02005819 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005820
5821 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5822 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005823 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005824 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5825 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005826 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005827
Bram Moolenaar83d47902020-03-26 20:34:00 +01005828 dict_add_number(dcell, "attr", cell2attr(term, NULL, attrs, fg, bg));
Bram Moolenaare0be1672018-07-08 16:50:37 +02005829 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005830
5831 ++pos.col;
5832 if (width == 2)
5833 ++pos.col;
5834 }
5835}
5836
5837/*
5838 * "term_sendkeys(buf, keys)" function
5839 */
5840 void
Bram Moolenaar3a05ce62020-03-11 19:30:01 +01005841f_term_sendkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005842{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005843 buf_T *buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005844 char_u *msg;
5845 term_T *term;
5846
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005847 if (buf == NULL)
5848 return;
5849
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005850 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005851 if (msg == NULL)
5852 return;
5853 term = buf->b_term;
5854 if (term->tl_vterm == NULL)
5855 return;
5856
5857 while (*msg != NUL)
5858 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02005859 int c;
5860
5861 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
5862 {
5863 c = TO_SPECIAL(msg[1], msg[2]);
5864 msg += 3;
5865 }
5866 else
5867 {
5868 c = PTR2CHAR(msg);
5869 msg += MB_CPTR2LEN(msg);
5870 }
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01005871 send_keys_to_term(term, c, 0, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005872 }
5873}
5874
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005875#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
5876/*
5877 * "term_getansicolors(buf)" function
5878 */
5879 void
5880f_term_getansicolors(typval_T *argvars, typval_T *rettv)
5881{
5882 buf_T *buf = term_get_buf(argvars, "term_getansicolors()");
5883 term_T *term;
5884 VTermState *state;
5885 VTermColor color;
5886 char_u hexbuf[10];
5887 int index;
5888 list_T *list;
5889
5890 if (rettv_list_alloc(rettv) == FAIL)
5891 return;
5892
5893 if (buf == NULL)
5894 return;
5895 term = buf->b_term;
5896 if (term->tl_vterm == NULL)
5897 return;
5898
5899 list = rettv->vval.v_list;
5900 state = vterm_obtain_state(term->tl_vterm);
5901 for (index = 0; index < 16; index++)
5902 {
5903 vterm_state_get_palette_color(state, index, &color);
5904 sprintf((char *)hexbuf, "#%02x%02x%02x",
5905 color.red, color.green, color.blue);
5906 if (list_append_string(list, hexbuf, 7) == FAIL)
5907 return;
5908 }
5909}
5910
5911/*
5912 * "term_setansicolors(buf, list)" function
5913 */
5914 void
5915f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
5916{
5917 buf_T *buf = term_get_buf(argvars, "term_setansicolors()");
5918 term_T *term;
5919
5920 if (buf == NULL)
5921 return;
5922 term = buf->b_term;
5923 if (term->tl_vterm == NULL)
5924 return;
5925
5926 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
5927 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005928 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005929 return;
5930 }
5931
5932 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005933 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005934}
5935#endif
5936
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005937/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02005938 * "term_setapi(buf, api)" function
5939 */
5940 void
5941f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
5942{
5943 buf_T *buf = term_get_buf(argvars, "term_setapi()");
5944 term_T *term;
5945 char_u *api;
5946
5947 if (buf == NULL)
5948 return;
5949 term = buf->b_term;
5950 vim_free(term->tl_api);
5951 api = tv_get_string_chk(&argvars[1]);
5952 if (api != NULL)
5953 term->tl_api = vim_strsave(api);
5954 else
5955 term->tl_api = NULL;
5956}
5957
5958/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005959 * "term_setrestore(buf, command)" function
5960 */
5961 void
5962f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5963{
5964#if defined(FEAT_SESSION)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005965 buf_T *buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005966 term_T *term;
5967 char_u *cmd;
5968
5969 if (buf == NULL)
5970 return;
5971 term = buf->b_term;
5972 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005973 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005974 if (cmd != NULL)
5975 term->tl_command = vim_strsave(cmd);
5976 else
5977 term->tl_command = NULL;
5978#endif
5979}
5980
5981/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005982 * "term_setkill(buf, how)" function
5983 */
5984 void
5985f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5986{
5987 buf_T *buf = term_get_buf(argvars, "term_setkill()");
5988 term_T *term;
5989 char_u *how;
5990
5991 if (buf == NULL)
5992 return;
5993 term = buf->b_term;
5994 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005995 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005996 if (how != NULL)
5997 term->tl_kill = vim_strsave(how);
5998 else
5999 term->tl_kill = NULL;
6000}
6001
6002/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006003 * "term_start(command, options)" function
6004 */
6005 void
6006f_term_start(typval_T *argvars, typval_T *rettv)
6007{
6008 jobopt_T opt;
6009 buf_T *buf;
6010
6011 init_job_options(&opt);
6012 if (argvars[1].v_type != VAR_UNKNOWN
6013 && get_job_options(&argvars[1], &opt,
6014 JO_TIMEOUT_ALL + JO_STOPONEXIT
6015 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
6016 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
6017 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
6018 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01006019 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaar83d47902020-03-26 20:34:00 +01006020 + JO2_NORESTORE + JO2_TERM_KILL + JO2_TERM_HIGHLIGHT
Bram Moolenaard2842ea2019-09-26 23:08:54 +02006021 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006022 return;
6023
Bram Moolenaar13568252018-03-16 20:46:58 +01006024 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006025
6026 if (buf != NULL && buf->b_term != NULL)
6027 rettv->vval.v_number = buf->b_fnum;
6028}
6029
6030/*
6031 * "term_wait" function
6032 */
6033 void
6034f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
6035{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01006036 buf_T *buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006037
6038 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006039 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006040 if (buf->b_term->tl_job == NULL)
6041 {
6042 ch_log(NULL, "term_wait(): no job to wait for");
6043 return;
6044 }
6045 if (buf->b_term->tl_job->jv_channel == NULL)
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006046 // channel is closed, nothing to do
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006047 return;
6048
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006049 // Get the job status, this will detect a job that finished.
Bram Moolenaara15ef452018-02-09 16:46:00 +01006050 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006051 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
6052 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006053 // The job is dead, keep reading channel I/O until the channel is
6054 // closed. buf->b_term may become NULL if the terminal was closed while
6055 // waiting.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006056 ch_log(NULL, "term_wait(): waiting for channel to close");
6057 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
6058 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006059 term_flush_messages();
6060
Bram Moolenaard45aa552018-05-21 22:50:29 +02006061 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01006062 if (!buf_valid(buf))
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006063 // If the terminal is closed when the channel is closed the
6064 // buffer disappears.
Bram Moolenaare5182262017-11-19 15:05:44 +01006065 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006066 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006067
6068 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006069 }
6070 else
6071 {
6072 long wait = 10L;
6073
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006074 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006075
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006076 // Wait for some time for any channel I/O.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006077 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006078 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006079 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006080
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006081 // Flushing messages on channels is hopefully sufficient.
6082 // TODO: is there a better way?
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02006083 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006084 }
6085}
6086
6087/*
6088 * Called when a channel has sent all the lines to a terminal.
6089 * Send a CTRL-D to mark the end of the text.
6090 */
6091 void
6092term_send_eof(channel_T *ch)
6093{
6094 term_T *term;
6095
Bram Moolenaaraeea7212020-04-02 18:50:46 +02006096 FOR_ALL_TERMS(term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006097 if (term->tl_job == ch->ch_job)
6098 {
6099 if (term->tl_eof_chars != NULL)
6100 {
6101 channel_send(ch, PART_IN, term->tl_eof_chars,
6102 (int)STRLEN(term->tl_eof_chars), NULL);
6103 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
6104 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01006105# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006106 else
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006107 // Default: CTRL-D
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006108 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
6109# endif
6110 }
6111}
6112
Bram Moolenaar113e1072019-01-20 15:30:40 +01006113#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006114 job_T *
6115term_getjob(term_T *term)
6116{
6117 return term != NULL ? term->tl_job : NULL;
6118}
Bram Moolenaar113e1072019-01-20 15:30:40 +01006119#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02006120
Bram Moolenaar4f974752019-02-17 17:44:42 +01006121# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006122
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006123///////////////////////////////////////
6124// 2. MS-Windows implementation.
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006125#ifdef PROTO
6126typedef int COORD;
6127typedef int DWORD;
6128typedef int HANDLE;
6129typedef int *DWORD_PTR;
6130typedef int HPCON;
6131typedef int HRESULT;
6132typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02006133typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006134typedef int PSIZE_T;
6135typedef int PVOID;
Bram Moolenaar1e814bc2019-11-03 21:19:41 +01006136typedef int BOOL;
6137# define WINAPI
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02006138#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006139
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006140HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
6141HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
6142HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01006143BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
6144BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
6145void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006146
6147 static int
6148dyn_conpty_init(int verbose)
6149{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006150 static HMODULE hKerneldll = NULL;
6151 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006152 static struct
6153 {
6154 char *name;
6155 FARPROC *ptr;
6156 } conpty_entry[] =
6157 {
6158 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
6159 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
6160 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
6161 {"InitializeProcThreadAttributeList",
6162 (FARPROC*)&pInitializeProcThreadAttributeList},
6163 {"UpdateProcThreadAttribute",
6164 (FARPROC*)&pUpdateProcThreadAttribute},
6165 {"DeleteProcThreadAttributeList",
6166 (FARPROC*)&pDeleteProcThreadAttributeList},
6167 {NULL, NULL}
6168 };
6169
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006170 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006171 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006172 if (verbose)
6173 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006174 return FAIL;
6175 }
6176
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006177 // No need to initialize twice.
6178 if (hKerneldll)
6179 return OK;
6180
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006181 hKerneldll = vimLoadLib("kernel32.dll");
6182 for (i = 0; conpty_entry[i].name != NULL
6183 && conpty_entry[i].ptr != NULL; ++i)
6184 {
6185 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
6186 conpty_entry[i].name)) == NULL)
6187 {
6188 if (verbose)
6189 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006190 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006191 return FAIL;
6192 }
6193 }
6194
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006195 return OK;
6196}
6197
6198 static int
6199conpty_term_and_job_init(
6200 term_T *term,
6201 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006202 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006203 jobopt_T *opt,
6204 jobopt_T *orig_opt)
6205{
6206 WCHAR *cmd_wchar = NULL;
6207 WCHAR *cmd_wchar_copy = NULL;
6208 WCHAR *cwd_wchar = NULL;
6209 WCHAR *env_wchar = NULL;
6210 channel_T *channel = NULL;
6211 job_T *job = NULL;
6212 HANDLE jo = NULL;
6213 garray_T ga_cmd, ga_env;
6214 char_u *cmd = NULL;
6215 HRESULT hr;
6216 COORD consize;
6217 SIZE_T breq;
6218 PROCESS_INFORMATION proc_info;
6219 HANDLE i_theirs = NULL;
6220 HANDLE o_theirs = NULL;
6221 HANDLE i_ours = NULL;
6222 HANDLE o_ours = NULL;
6223
6224 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6225 ga_init2(&ga_env, (int)sizeof(char*), 20);
6226
6227 if (argvar->v_type == VAR_STRING)
6228 {
6229 cmd = argvar->vval.v_string;
6230 }
6231 else if (argvar->v_type == VAR_LIST)
6232 {
6233 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
6234 goto failed;
6235 cmd = ga_cmd.ga_data;
6236 }
6237 if (cmd == NULL || *cmd == NUL)
6238 {
6239 emsg(_(e_invarg));
6240 goto failed;
6241 }
6242
6243 term->tl_arg0_cmd = vim_strsave(cmd);
6244
6245 cmd_wchar = enc_to_utf16(cmd, NULL);
6246
6247 if (cmd_wchar != NULL)
6248 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006249 // Request by CreateProcessW
6250 breq = wcslen(cmd_wchar) + 1 + 1; // Addition of NUL by API
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006251 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006252 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
6253 }
6254
6255 ga_clear(&ga_cmd);
6256 if (cmd_wchar == NULL)
6257 goto failed;
6258 if (opt->jo_cwd != NULL)
6259 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
6260
6261 win32_build_env(opt->jo_env, &ga_env, TRUE);
6262 env_wchar = ga_env.ga_data;
6263
6264 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
6265 goto failed;
6266 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
6267 goto failed;
6268
6269 consize.X = term->tl_cols;
6270 consize.Y = term->tl_rows;
6271 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
6272 &term->tl_conpty);
6273 if (FAILED(hr))
6274 goto failed;
6275
6276 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
6277
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006278 // Set up pipe inheritance safely: Vista or later.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006279 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006280 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006281 if (!term->tl_siex.lpAttributeList)
6282 goto failed;
6283 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
6284 0, &breq))
6285 goto failed;
6286 if (!pUpdateProcThreadAttribute(
6287 term->tl_siex.lpAttributeList, 0,
6288 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
6289 sizeof(HPCON), NULL, NULL))
6290 goto failed;
6291
6292 channel = add_channel();
6293 if (channel == NULL)
6294 goto failed;
6295
6296 job = job_alloc();
6297 if (job == NULL)
6298 goto failed;
6299 if (argvar->v_type == VAR_STRING)
6300 {
6301 int argc;
6302
6303 build_argv_from_string(cmd, &job->jv_argv, &argc);
6304 }
6305 else
6306 {
6307 int argc;
6308
6309 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6310 }
6311
6312 if (opt->jo_set & JO_IN_BUF)
6313 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6314
6315 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
6316 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
Bram Moolenaar07b761a2020-04-26 16:06:01 +02006317 | CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006318 env_wchar, cwd_wchar,
6319 &term->tl_siex.StartupInfo, &proc_info))
6320 goto failed;
6321
6322 CloseHandle(i_theirs);
6323 CloseHandle(o_theirs);
6324
6325 channel_set_pipes(channel,
6326 (sock_T)i_ours,
6327 (sock_T)o_ours,
6328 (sock_T)o_ours);
6329
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006330 // Write lines with CR instead of NL.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006331 channel->ch_write_text_mode = TRUE;
6332
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006333 // Use to explicitly delete anonymous pipe handle.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006334 channel->ch_anonymous_pipe = TRUE;
6335
6336 jo = CreateJobObject(NULL, NULL);
6337 if (jo == NULL)
6338 goto failed;
6339
6340 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6341 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006342 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006343 CloseHandle(jo);
6344 jo = NULL;
6345 }
6346
6347 ResumeThread(proc_info.hThread);
6348 CloseHandle(proc_info.hThread);
6349
6350 vim_free(cmd_wchar);
6351 vim_free(cmd_wchar_copy);
6352 vim_free(cwd_wchar);
6353 vim_free(env_wchar);
6354
6355 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6356 goto failed;
6357
6358#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6359 if (opt->jo_set2 & JO2_ANSI_COLORS)
6360 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6361 else
6362 init_vterm_ansi_colors(term->tl_vterm);
6363#endif
6364
6365 channel_set_job(channel, job, opt);
6366 job_set_options(job, opt);
6367
6368 job->jv_channel = channel;
6369 job->jv_proc_info = proc_info;
6370 job->jv_job_object = jo;
6371 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006372 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006373 ++job->jv_refcount;
6374 term->tl_job = job;
6375
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006376 // Redirecting stdout and stderr doesn't work at the job level. Instead
6377 // open the file here and handle it in. opt->jo_io was changed in
6378 // setup_job_options(), use the original flags here.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006379 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6380 {
6381 char_u *fname = opt->jo_io_name[PART_OUT];
6382
6383 ch_log(channel, "Opening output file %s", fname);
6384 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6385 if (term->tl_out_fd == NULL)
6386 semsg(_(e_notopen), fname);
6387 }
6388
6389 return OK;
6390
6391failed:
6392 ga_clear(&ga_cmd);
6393 ga_clear(&ga_env);
6394 vim_free(cmd_wchar);
6395 vim_free(cmd_wchar_copy);
6396 vim_free(cwd_wchar);
6397 if (channel != NULL)
6398 channel_clear(channel);
6399 if (job != NULL)
6400 {
6401 job->jv_channel = NULL;
6402 job_cleanup(job);
6403 }
6404 term->tl_job = NULL;
6405 if (jo != NULL)
6406 CloseHandle(jo);
6407
6408 if (term->tl_siex.lpAttributeList != NULL)
6409 {
6410 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6411 vim_free(term->tl_siex.lpAttributeList);
6412 }
6413 term->tl_siex.lpAttributeList = NULL;
6414 if (o_theirs != NULL)
6415 CloseHandle(o_theirs);
6416 if (o_ours != NULL)
6417 CloseHandle(o_ours);
6418 if (i_ours != NULL)
6419 CloseHandle(i_ours);
6420 if (i_theirs != NULL)
6421 CloseHandle(i_theirs);
6422 if (term->tl_conpty != NULL)
6423 pClosePseudoConsole(term->tl_conpty);
6424 term->tl_conpty = NULL;
6425 return FAIL;
6426}
6427
6428 static void
6429conpty_term_report_winsize(term_T *term, int rows, int cols)
6430{
6431 COORD consize;
6432
6433 consize.X = cols;
6434 consize.Y = rows;
6435 pResizePseudoConsole(term->tl_conpty, consize);
6436}
6437
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006438 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006439term_free_conpty(term_T *term)
6440{
6441 if (term->tl_siex.lpAttributeList != NULL)
6442 {
6443 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6444 vim_free(term->tl_siex.lpAttributeList);
6445 }
6446 term->tl_siex.lpAttributeList = NULL;
6447 if (term->tl_conpty != NULL)
6448 pClosePseudoConsole(term->tl_conpty);
6449 term->tl_conpty = NULL;
6450}
6451
6452 int
6453use_conpty(void)
6454{
6455 return has_conpty;
6456}
6457
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006458# ifndef PROTO
6459
6460#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6461#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006462#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006463
6464void* (*winpty_config_new)(UINT64, void*);
6465void* (*winpty_open)(void*, void*);
6466void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6467BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6468void (*winpty_config_set_mouse_mode)(void*, int);
6469void (*winpty_config_set_initial_size)(void*, int, int);
6470LPCWSTR (*winpty_conin_name)(void*);
6471LPCWSTR (*winpty_conout_name)(void*);
6472LPCWSTR (*winpty_conerr_name)(void*);
6473void (*winpty_free)(void*);
6474void (*winpty_config_free)(void*);
6475void (*winpty_spawn_config_free)(void*);
6476void (*winpty_error_free)(void*);
6477LPCWSTR (*winpty_error_msg)(void*);
6478BOOL (*winpty_set_size)(void*, int, int, void*);
6479HANDLE (*winpty_agent_process)(void*);
6480
6481#define WINPTY_DLL "winpty.dll"
6482
6483static HINSTANCE hWinPtyDLL = NULL;
6484# endif
6485
6486 static int
6487dyn_winpty_init(int verbose)
6488{
6489 int i;
6490 static struct
6491 {
6492 char *name;
6493 FARPROC *ptr;
6494 } winpty_entry[] =
6495 {
6496 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6497 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6498 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6499 {"winpty_config_set_mouse_mode",
6500 (FARPROC*)&winpty_config_set_mouse_mode},
6501 {"winpty_config_set_initial_size",
6502 (FARPROC*)&winpty_config_set_initial_size},
6503 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6504 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6505 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6506 {"winpty_free", (FARPROC*)&winpty_free},
6507 {"winpty_open", (FARPROC*)&winpty_open},
6508 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6509 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6510 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6511 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6512 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6513 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6514 {NULL, NULL}
6515 };
6516
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006517 // No need to initialize twice.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006518 if (hWinPtyDLL)
6519 return OK;
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006520 // Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6521 // winpty.dll.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006522 if (*p_winptydll != NUL)
6523 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6524 if (!hWinPtyDLL)
6525 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6526 if (!hWinPtyDLL)
6527 {
6528 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006529 semsg(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006530 : (char_u *)WINPTY_DLL);
6531 return FAIL;
6532 }
6533 for (i = 0; winpty_entry[i].name != NULL
6534 && winpty_entry[i].ptr != NULL; ++i)
6535 {
6536 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6537 winpty_entry[i].name)) == NULL)
6538 {
6539 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006540 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006541 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006542 return FAIL;
6543 }
6544 }
6545
6546 return OK;
6547}
6548
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006549 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006550winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006551 term_T *term,
6552 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006553 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006554 jobopt_T *opt,
6555 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006556{
6557 WCHAR *cmd_wchar = NULL;
6558 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006559 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006560 channel_T *channel = NULL;
6561 job_T *job = NULL;
6562 DWORD error;
6563 HANDLE jo = NULL;
6564 HANDLE child_process_handle;
6565 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006566 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006567 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006568 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006569 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006570
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006571 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6572 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006573
6574 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006575 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006576 cmd = argvar->vval.v_string;
6577 }
6578 else if (argvar->v_type == VAR_LIST)
6579 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006580 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006581 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006582 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006583 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006584 if (cmd == NULL || *cmd == NUL)
6585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006586 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006587 goto failed;
6588 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006589
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006590 term->tl_arg0_cmd = vim_strsave(cmd);
6591
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006592 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006593 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006594 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006595 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006596 if (opt->jo_cwd != NULL)
6597 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006598
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006599 win32_build_env(opt->jo_env, &ga_env, TRUE);
6600 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006601
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006602 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6603 if (term->tl_winpty_config == NULL)
6604 goto failed;
6605
6606 winpty_config_set_mouse_mode(term->tl_winpty_config,
6607 WINPTY_MOUSE_MODE_FORCE);
6608 winpty_config_set_initial_size(term->tl_winpty_config,
6609 term->tl_cols, term->tl_rows);
6610 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6611 if (term->tl_winpty == NULL)
6612 goto failed;
6613
6614 spawn_config = winpty_spawn_config_new(
6615 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6616 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6617 NULL,
6618 cmd_wchar,
6619 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006620 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006621 &winpty_err);
6622 if (spawn_config == NULL)
6623 goto failed;
6624
6625 channel = add_channel();
6626 if (channel == NULL)
6627 goto failed;
6628
6629 job = job_alloc();
6630 if (job == NULL)
6631 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006632 if (argvar->v_type == VAR_STRING)
6633 {
6634 int argc;
6635
6636 build_argv_from_string(cmd, &job->jv_argv, &argc);
6637 }
6638 else
6639 {
6640 int argc;
6641
6642 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6643 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006644
6645 if (opt->jo_set & JO_IN_BUF)
6646 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6647
6648 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
6649 &child_thread_handle, &error, &winpty_err))
6650 goto failed;
6651
6652 channel_set_pipes(channel,
6653 (sock_T)CreateFileW(
6654 winpty_conin_name(term->tl_winpty),
6655 GENERIC_WRITE, 0, NULL,
6656 OPEN_EXISTING, 0, NULL),
6657 (sock_T)CreateFileW(
6658 winpty_conout_name(term->tl_winpty),
6659 GENERIC_READ, 0, NULL,
6660 OPEN_EXISTING, 0, NULL),
6661 (sock_T)CreateFileW(
6662 winpty_conerr_name(term->tl_winpty),
6663 GENERIC_READ, 0, NULL,
6664 OPEN_EXISTING, 0, NULL));
6665
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006666 // Write lines with CR instead of NL.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006667 channel->ch_write_text_mode = TRUE;
6668
6669 jo = CreateJobObject(NULL, NULL);
6670 if (jo == NULL)
6671 goto failed;
6672
6673 if (!AssignProcessToJobObject(jo, child_process_handle))
6674 {
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006675 // Failed, switch the way to terminate process with TerminateProcess.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006676 CloseHandle(jo);
6677 jo = NULL;
6678 }
6679
6680 winpty_spawn_config_free(spawn_config);
6681 vim_free(cmd_wchar);
6682 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006683 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006684
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006685 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6686 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006687
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006688#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6689 if (opt->jo_set2 & JO2_ANSI_COLORS)
6690 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6691 else
6692 init_vterm_ansi_colors(term->tl_vterm);
6693#endif
6694
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006695 channel_set_job(channel, job, opt);
6696 job_set_options(job, opt);
6697
6698 job->jv_channel = channel;
6699 job->jv_proc_info.hProcess = child_process_handle;
6700 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
6701 job->jv_job_object = jo;
6702 job->jv_status = JOB_STARTED;
6703 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006704 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006705 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006706 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006707 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006708 ++job->jv_refcount;
6709 term->tl_job = job;
6710
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006711 // Redirecting stdout and stderr doesn't work at the job level. Instead
6712 // open the file here and handle it in. opt->jo_io was changed in
6713 // setup_job_options(), use the original flags here.
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006714 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6715 {
6716 char_u *fname = opt->jo_io_name[PART_OUT];
6717
6718 ch_log(channel, "Opening output file %s", fname);
6719 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6720 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006721 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006722 }
6723
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006724 return OK;
6725
6726failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006727 ga_clear(&ga_cmd);
6728 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006729 vim_free(cmd_wchar);
6730 vim_free(cwd_wchar);
6731 if (spawn_config != NULL)
6732 winpty_spawn_config_free(spawn_config);
6733 if (channel != NULL)
6734 channel_clear(channel);
6735 if (job != NULL)
6736 {
6737 job->jv_channel = NULL;
6738 job_cleanup(job);
6739 }
6740 term->tl_job = NULL;
6741 if (jo != NULL)
6742 CloseHandle(jo);
6743 if (term->tl_winpty != NULL)
6744 winpty_free(term->tl_winpty);
6745 term->tl_winpty = NULL;
6746 if (term->tl_winpty_config != NULL)
6747 winpty_config_free(term->tl_winpty_config);
6748 term->tl_winpty_config = NULL;
6749 if (winpty_err != NULL)
6750 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006751 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006752 (short_u *)winpty_error_msg(winpty_err), NULL);
6753
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006754 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006755 winpty_error_free(winpty_err);
6756 }
6757 return FAIL;
6758}
6759
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006760/*
6761 * Create a new terminal of "rows" by "cols" cells.
6762 * Store a reference in "term".
6763 * Return OK or FAIL.
6764 */
6765 static int
6766term_and_job_init(
6767 term_T *term,
6768 typval_T *argvar,
Bram Moolenaar197c6b72019-11-03 23:37:12 +01006769 char **argv,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006770 jobopt_T *opt,
6771 jobopt_T *orig_opt)
6772{
6773 int use_winpty = FALSE;
6774 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006775 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006776
6777 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
6778 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
6779
6780 if (!has_winpty && !has_conpty)
6781 // If neither is available give the errors for winpty, since when
6782 // conpty is not available it can't be installed either.
6783 return dyn_winpty_init(TRUE);
6784
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006785 if (opt->jo_tty_type != NUL)
6786 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006787
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006788 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006789 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006790 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006791 use_conpty = TRUE;
6792 else if (has_winpty)
6793 use_winpty = TRUE;
6794 // else: error
6795 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006796 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006797 {
6798 if (has_winpty)
6799 use_winpty = TRUE;
6800 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006801 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006802 {
6803 if (has_conpty)
6804 use_conpty = TRUE;
6805 else
6806 return dyn_conpty_init(TRUE);
6807 }
6808
6809 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006810 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006811
6812 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006813 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006814
6815 // error
6816 return dyn_winpty_init(TRUE);
6817}
6818
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006819 static int
6820create_pty_only(term_T *term, jobopt_T *options)
6821{
6822 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
6823 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
6824 char in_name[80], out_name[80];
6825 channel_T *channel = NULL;
6826
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006827 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6828 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006829
6830 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
6831 GetCurrentProcessId(),
6832 curbuf->b_fnum);
6833 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
6834 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6835 PIPE_UNLIMITED_INSTANCES,
6836 0, 0, NMPWAIT_NOWAIT, NULL);
6837 if (hPipeIn == INVALID_HANDLE_VALUE)
6838 goto failed;
6839
6840 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
6841 GetCurrentProcessId(),
6842 curbuf->b_fnum);
6843 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
6844 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6845 PIPE_UNLIMITED_INSTANCES,
6846 0, 0, 0, NULL);
6847 if (hPipeOut == INVALID_HANDLE_VALUE)
6848 goto failed;
6849
6850 ConnectNamedPipe(hPipeIn, NULL);
6851 ConnectNamedPipe(hPipeOut, NULL);
6852
6853 term->tl_job = job_alloc();
6854 if (term->tl_job == NULL)
6855 goto failed;
6856 ++term->tl_job->jv_refcount;
6857
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006858 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006859 term->tl_job->jv_status = JOB_FINISHED;
6860
6861 channel = add_channel();
6862 if (channel == NULL)
6863 goto failed;
6864 term->tl_job->jv_channel = channel;
6865 channel->ch_keep_open = TRUE;
6866 channel->ch_named_pipe = TRUE;
6867
6868 channel_set_pipes(channel,
6869 (sock_T)hPipeIn,
6870 (sock_T)hPipeOut,
6871 (sock_T)hPipeOut);
6872 channel_set_job(channel, term->tl_job, options);
6873 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
6874 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
6875
6876 return OK;
6877
6878failed:
6879 if (hPipeIn != NULL)
6880 CloseHandle(hPipeIn);
6881 if (hPipeOut != NULL)
6882 CloseHandle(hPipeOut);
6883 return FAIL;
6884}
6885
6886/*
6887 * Free the terminal emulator part of "term".
6888 */
6889 static void
6890term_free_vterm(term_T *term)
6891{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006892 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006893 if (term->tl_winpty != NULL)
6894 winpty_free(term->tl_winpty);
6895 term->tl_winpty = NULL;
6896 if (term->tl_winpty_config != NULL)
6897 winpty_config_free(term->tl_winpty_config);
6898 term->tl_winpty_config = NULL;
6899 if (term->tl_vterm != NULL)
6900 vterm_free(term->tl_vterm);
6901 term->tl_vterm = NULL;
6902}
6903
6904/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02006905 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006906 */
6907 static void
6908term_report_winsize(term_T *term, int rows, int cols)
6909{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006910 if (term->tl_conpty)
6911 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006912 if (term->tl_winpty)
6913 winpty_set_size(term->tl_winpty, cols, rows, NULL);
6914}
6915
6916 int
6917terminal_enabled(void)
6918{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006919 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006920}
6921
6922# else
6923
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006924///////////////////////////////////////
6925// 3. Unix-like implementation.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006926
6927/*
6928 * Create a new terminal of "rows" by "cols" cells.
6929 * Start job for "cmd".
6930 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01006931 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006932 * Return OK or FAIL.
6933 */
6934 static int
6935term_and_job_init(
6936 term_T *term,
6937 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01006938 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006939 jobopt_T *opt,
6940 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006941{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006942 term->tl_arg0_cmd = NULL;
6943
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006944 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6945 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006946
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006947#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6948 if (opt->jo_set2 & JO2_ANSI_COLORS)
6949 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6950 else
6951 init_vterm_ansi_colors(term->tl_vterm);
6952#endif
6953
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006954 // This may change a string in "argvar".
Bram Moolenaar21109272020-01-30 16:27:20 +01006955 term->tl_job = job_start(argvar, argv, opt, &term->tl_job);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006956 if (term->tl_job != NULL)
6957 ++term->tl_job->jv_refcount;
6958
6959 return term->tl_job != NULL
6960 && term->tl_job->jv_channel != NULL
6961 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
6962}
6963
6964 static int
6965create_pty_only(term_T *term, jobopt_T *opt)
6966{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006967 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6968 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006969
6970 term->tl_job = job_alloc();
6971 if (term->tl_job == NULL)
6972 return FAIL;
6973 ++term->tl_job->jv_refcount;
6974
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006975 // behave like the job is already finished
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006976 term->tl_job->jv_status = JOB_FINISHED;
6977
6978 return mch_create_pty_channel(term->tl_job, opt);
6979}
6980
6981/*
6982 * Free the terminal emulator part of "term".
6983 */
6984 static void
6985term_free_vterm(term_T *term)
6986{
6987 if (term->tl_vterm != NULL)
6988 vterm_free(term->tl_vterm);
6989 term->tl_vterm = NULL;
6990}
6991
6992/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02006993 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006994 */
6995 static void
6996term_report_winsize(term_T *term, int rows, int cols)
6997{
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01006998 // Use an ioctl() to report the new window size to the job.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006999 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
7000 {
7001 int fd = -1;
7002 int part;
7003
7004 for (part = PART_OUT; part < PART_COUNT; ++part)
7005 {
7006 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01007007 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02007008 break;
7009 }
7010 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
7011 mch_signal_job(term->tl_job, (char_u *)"winch");
7012 }
7013}
7014
7015# endif
7016
Bram Moolenaar0d6f5d92019-12-05 21:33:15 +01007017#endif // FEAT_TERMINAL