blob: 5281ef31d2839971d7c438d62ecc62b8685a0dde [file] [log] [blame]
Bram Moolenaare4f25e42017-07-07 11:54:15 +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 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020013 * There are three parts:
14 * 1. Generic code for all systems.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020015 * Uses libvterm for the terminal emulator.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020016 * 2. The MS-Windows implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020017 * Uses winpty.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020018 * 3. The Unix-like implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020019 * Uses pseudo-tty's (pty's).
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020020 *
21 * For each terminal one VTerm is constructed. This uses libvterm. A copy of
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020022 * this library is in the libvterm directory.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020023 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020024 * When a terminal window is opened, a job is started that will be connected to
25 * the terminal emulator.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020026 *
27 * If the terminal window has keyboard focus, typed keys are converted to the
Bram Moolenaar8a773062017-07-24 22:29:21 +020028 * terminal encoding and writing to the job over a channel.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020029 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020030 * 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.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020034 *
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020035 * 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 *
Bram Moolenaare4f25e42017-07-07 11:54:15 +020038 * TODO:
Bram Moolenaar423802d2017-07-30 16:52:24 +020039 * - add option values to the command:
Bram Moolenaar82b9ca02017-08-08 23:06:46 +020040 * :term ++24x80 ++close vim notes.txt
Bram Moolenaardd693ce2017-08-10 23:15:19 +020041 * - When using term_finish "open" have a way to specify how the window is to
42 * be opened. E.g. term_opencmd "10split buffer %d".
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020043 * - support different cursor shapes, colors and attributes
44 * - make term_getcursor() return type (none/block/bar/underline) and
45 * attributes (color, blink, etc.)
Bram Moolenaardd693ce2017-08-10 23:15:19 +020046 * - Make argument list work on MS-Windows. #1954
Bram Moolenaar292d5692017-08-08 21:52:22 +020047 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaard85f2712017-07-28 21:51:57 +020048 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020049 * For the GUI fill termios with default values, perhaps like pangoterm:
50 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
51 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020052 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020053 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020054 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020055 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020056 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020057 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
58 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020059 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020060 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020061 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020062 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020063 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020064 * terminal. Allow:
65 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
66 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
67 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
68 * Check that something is connected to the terminal.
69 * Test: "cat" reading from a file or buffer
70 * "ls" writing stdout to a file or buffer
71 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020072 * - support ":term NONE" to open a terminal with a pty but not running a job
73 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020074 * - if the job in the terminal does not support the mouse, we can use the
75 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020076 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
77 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020078 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020079 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020080 * - Copy text in the vterm to the Vim buffer once in a while, so that
81 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020082 */
83
84#include "vim.h"
85
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020086#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020087
Bram Moolenaard5310982017-08-05 15:16:32 +020088#ifndef MIN
89# define MIN(x,y) ((x) < (y) ? (x) : (y))
90#endif
91#ifndef MAX
92# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020093#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020094
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020095#include "libvterm/include/vterm.h"
96
Bram Moolenaar33a43be2017-08-06 21:36:22 +020097/* This is VTermScreenCell without the characters, thus much smaller. */
98typedef struct {
99 VTermScreenCellAttrs attrs;
100 char width;
101 VTermColor fg, bg;
102} cellattr_T;
103
Bram Moolenaard85f2712017-07-28 21:51:57 +0200104typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200105 int sb_cols; /* can differ per line */
106 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200107} sb_line_T;
108
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200109/* typedef term_T in structs.h */
110struct terminal_S {
111 term_T *tl_next;
112
Bram Moolenaar423802d2017-07-30 16:52:24 +0200113 VTerm *tl_vterm;
114 job_T *tl_job;
115 buf_T *tl_buffer;
116
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200117 /* used when tl_job is NULL and only a pty was created */
118 int tl_tty_fd;
119 char_u *tl_tty_name;
120
Bram Moolenaar6d819742017-08-06 14:57:49 +0200121 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200122 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200123 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200124
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200125#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200126 void *tl_winpty_config;
127 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200128#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200129
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200130 /* last known vterm size */
131 int tl_rows;
132 int tl_cols;
133 /* vterm size does not follow window size */
134 int tl_rows_fixed;
135 int tl_cols_fixed;
136
Bram Moolenaar21554412017-07-24 21:44:43 +0200137 char_u *tl_title; /* NULL or allocated */
138 char_u *tl_status_text; /* NULL or allocated */
139
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200140 /* Range of screen rows to update. Zero based. */
141 int tl_dirty_row_start; /* -1 if nothing dirty */
142 int tl_dirty_row_end; /* row below last one to update */
143
Bram Moolenaard85f2712017-07-28 21:51:57 +0200144 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200145 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200146
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200147 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200148 int tl_cursor_visible;
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200149
150 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200151};
152
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200153#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
154#define TMODE_LOOP 2 /* CTRL-W N used */
155
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200156/*
157 * List of all active terminals.
158 */
159static term_T *first_term = NULL;
160
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200161
162#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
163#define KEY_BUF_LEN 200
164
165/*
166 * Functions with separate implementation for MS-Windows and Unix-like systems.
167 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200168static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200169static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200170static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200171
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200172/**************************************
173 * 1. Generic code for all systems.
174 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200175
176/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200177 * Determine the terminal size from 'termsize' and the current window.
178 * Assumes term->tl_rows and term->tl_cols are zero.
179 */
180 static void
181set_term_and_win_size(term_T *term)
182{
183 if (*curwin->w_p_tms != NUL)
184 {
185 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
186
187 term->tl_rows = atoi((char *)curwin->w_p_tms);
188 term->tl_cols = atoi((char *)p);
189 }
190 if (term->tl_rows == 0)
191 term->tl_rows = curwin->w_height;
192 else
193 {
194 win_setheight_win(term->tl_rows, curwin);
195 term->tl_rows_fixed = TRUE;
196 }
197 if (term->tl_cols == 0)
198 term->tl_cols = curwin->w_width;
199 else
200 {
201 win_setwidth_win(term->tl_cols, curwin);
202 term->tl_cols_fixed = TRUE;
203 }
204}
205
206/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200207 * Initialize job options for a terminal job.
208 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200209 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200210 static void
211init_job_options(jobopt_T *opt)
212{
213 clear_job_options(opt);
214
215 opt->jo_mode = MODE_RAW;
216 opt->jo_out_mode = MODE_RAW;
217 opt->jo_err_mode = MODE_RAW;
218 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
219
220 opt->jo_io[PART_OUT] = JIO_BUFFER;
221 opt->jo_io[PART_ERR] = JIO_BUFFER;
222 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
223
224 opt->jo_modifiable[PART_OUT] = 0;
225 opt->jo_modifiable[PART_ERR] = 0;
226 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
227
228 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
229}
230
231/*
232 * Set job options mandatory for a terminal job.
233 */
234 static void
235setup_job_options(jobopt_T *opt, int rows, int cols)
236{
237 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
238 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
239 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200240 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
241 opt->jo_term_rows = rows;
242 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
243 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200244}
245
246 static void
Bram Moolenaarda43b612017-08-11 22:27:50 +0200247term_start(char_u *cmd, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249 exarg_T split_ea;
250 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200252
253 if (check_restricted() || check_secure())
254 return;
255
256 term = (term_T *)alloc_clear(sizeof(term_T));
257 if (term == NULL)
258 return;
259 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200260 term->tl_cursor_visible = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200261 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200262 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200263
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200264 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200265 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200266 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200267 /* Create a new buffer in the current window. */
268 if (!can_abandon(curbuf, forceit))
269 {
270 EMSG(_(e_nowrtmsg));
271 return;
272 }
273 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
274 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
275 return;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200276 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200277 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200278 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200279 /* Open a new window or tab. */
280 split_ea.cmdidx = CMD_new;
281 split_ea.cmd = (char_u *)"new";
282 split_ea.arg = (char_u *)"";
283 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
284 {
285 split_ea.line2 = opt->jo_term_rows;
286 split_ea.addr_count = 1;
287 }
288 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
289 {
290 split_ea.line2 = opt->jo_term_cols;
291 split_ea.addr_count = 1;
292 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200293
Bram Moolenaarda43b612017-08-11 22:27:50 +0200294 ex_splitview(&split_ea);
295 if (curwin == old_curwin)
296 {
297 /* split failed */
298 vim_free(term);
299 return;
300 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200301 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200302 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200303 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200304
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200305 /* only one size was taken care of with :new, do the other one */
306 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
307 win_setheight(opt->jo_term_rows);
308 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
309 win_setwidth(opt->jo_term_cols);
310
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200311 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200312 term->tl_next = first_term;
313 first_term = term;
314
Bram Moolenaar293424c2017-07-26 23:11:01 +0200315 if (cmd == NULL || *cmd == NUL)
316 cmd = p_sh;
317
Bram Moolenaar78712a72017-08-05 14:50:12 +0200318 if (opt->jo_term_name != NULL)
319 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
320 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200321 {
322 int i;
323 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200324 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200325
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200326 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200327 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200328 /* Prepend a ! to the command name to avoid the buffer name equals
329 * the executable, otherwise ":w!" would overwrite it. */
330 if (i == 0)
331 vim_snprintf((char *)p, len, "!%s", cmd);
332 else
333 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200334 if (buflist_findname(p) == NULL)
335 {
336 curbuf->b_ffname = p;
337 break;
338 }
339 }
340 }
341 curbuf->b_fname = curbuf->b_ffname;
342
Bram Moolenaareb44a682017-08-03 22:44:55 +0200343 set_string_option_direct((char_u *)"buftype", -1,
344 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
345
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200346 /* Mark the buffer as not modifiable. It can only be made modifiable after
347 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200348 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200349
350 /* Set 'bufhidden' to "hide": allow closing the window. */
351 set_string_option_direct((char_u *)"bufhidden", -1,
352 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200353
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200354 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200355 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200356
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200357 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200358 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200359 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200360 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200361 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200362 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200363 }
364 else
365 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200366 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200367
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200368 /* Wiping out the buffer will also close the window and call
369 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200370 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200371 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200372}
373
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200374/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200375 * ":terminal": open a terminal window and execute a job in it.
376 */
377 void
378ex_terminal(exarg_T *eap)
379{
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200380 jobopt_T opt;
381 char_u *cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200382
383 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200384
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200385 cmd = eap->arg;
386 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
387 {
388 char_u *p;
389
390 cmd += 2;
391 p = skiptowhite(cmd);
392 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
393 opt.jo_term_finish = 'c';
394 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
395 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200396 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
397 opt.jo_curwin = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200398 else
399 {
400 if (*p)
401 *p = NUL;
402 EMSG2(_("E181: Invalid attribute: %s"), cmd);
403 return;
404 }
405 cmd = skipwhite(p);
406 }
407
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200408 if (eap->addr_count == 2)
409 {
410 opt.jo_term_rows = eap->line1;
411 opt.jo_term_cols = eap->line2;
412 }
413 else if (eap->addr_count == 1)
414 {
415 if (cmdmod.split & WSP_VERT)
416 opt.jo_term_cols = eap->line2;
417 else
418 opt.jo_term_rows = eap->line2;
419 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200420
Bram Moolenaarda43b612017-08-11 22:27:50 +0200421 term_start(cmd, &opt, eap->forceit);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200422}
423
424/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200425 * Free the scrollback buffer for "term".
426 */
427 static void
428free_scrollback(term_T *term)
429{
430 int i;
431
432 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
433 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
434 ga_clear(&term->tl_scrollback);
435}
436
437/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200438 * Free a terminal and everything it refers to.
439 * Kills the job if there is one.
440 * Called when wiping out a buffer.
441 */
442 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200443free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200444{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200445 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200446 term_T *tp;
447
448 if (term == NULL)
449 return;
450 if (first_term == term)
451 first_term = term->tl_next;
452 else
453 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
454 if (tp->tl_next == term)
455 {
456 tp->tl_next = term->tl_next;
457 break;
458 }
459
460 if (term->tl_job != NULL)
461 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200462 if (term->tl_job->jv_status != JOB_ENDED
463 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200464 job_stop(term->tl_job, NULL, "kill");
465 job_unref(term->tl_job);
466 }
467
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200468 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200469
470 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200471 vim_free(term->tl_title);
472 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200473 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200474 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200475}
476
477/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200478 * Write job output "msg[len]" to the vterm.
479 */
480 static void
481term_write_job_output(term_T *term, char_u *msg, size_t len)
482{
483 VTerm *vterm = term->tl_vterm;
484 char_u *p;
485 size_t done;
486 size_t len_now;
487
488 for (done = 0; done < len; done += len_now)
489 {
490 for (p = msg + done; p < msg + len; )
491 {
492 if (*p == NL)
493 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200494 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200495 }
496 len_now = p - msg - done;
497 vterm_input_write(vterm, (char *)msg + done, len_now);
498 if (p < msg + len && *p == NL)
499 {
500 /* Convert NL to CR-NL, that appears to work best. */
501 vterm_input_write(vterm, "\r\n", 2);
502 ++len_now;
503 }
504 }
505
506 /* this invokes the damage callbacks */
507 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
508}
509
Bram Moolenaar1c844932017-07-24 23:36:41 +0200510 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200511update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200512{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200513 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200514 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200515 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200516 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200517 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200518 if (term->tl_cursor_visible)
519 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200520 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200521#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200522 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200523 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200524#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200525 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200526}
527
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200528/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200529 * Invoked when "msg" output from a job was received. Write it to the terminal
530 * of "buffer".
531 */
532 void
533write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
534{
535 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200536 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200537
Bram Moolenaard85f2712017-07-28 21:51:57 +0200538 if (term->tl_vterm == NULL)
539 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200540 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200541 return;
542 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200543 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200544 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200545
Bram Moolenaar6d819742017-08-06 14:57:49 +0200546 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200547 {
548 /* TODO: only update once in a while. */
549 update_screen(0);
550 update_cursor(term, TRUE);
551 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200552}
553
554/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200555 * Send a mouse position and click to the vterm
556 */
557 static int
558term_send_mouse(VTerm *vterm, int button, int pressed)
559{
560 VTermModifier mod = VTERM_MOD_NONE;
561
562 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
563 mouse_col - W_WINCOL(curwin), mod);
564 vterm_mouse_button(vterm, button, pressed, mod);
565 return TRUE;
566}
567
568/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200569 * Convert typed key "c" into bytes to send to the job.
570 * Return the number of bytes in "buf".
571 */
572 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200573term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200574{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200575 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200576 VTermKey key = VTERM_KEY_NONE;
577 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200578 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200579
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200580 switch (c)
581 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200582 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200583 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200584 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
585 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200586 case K_DEL: key = VTERM_KEY_DEL; break;
587 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200588 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
589 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200590 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200591 case K_S_END: mod = VTERM_MOD_SHIFT;
592 key = VTERM_KEY_END; break;
593 case K_C_END: mod = VTERM_MOD_CTRL;
594 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200595 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
596 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
597 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
598 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
599 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
600 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
601 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
602 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
603 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
604 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
605 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
606 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
607 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200608 case K_S_HOME: mod = VTERM_MOD_SHIFT;
609 key = VTERM_KEY_HOME; break;
610 case K_C_HOME: mod = VTERM_MOD_CTRL;
611 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200612 case K_INS: key = VTERM_KEY_INS; break;
613 case K_K0: key = VTERM_KEY_KP_0; break;
614 case K_K1: key = VTERM_KEY_KP_1; break;
615 case K_K2: key = VTERM_KEY_KP_2; break;
616 case K_K3: key = VTERM_KEY_KP_3; break;
617 case K_K4: key = VTERM_KEY_KP_4; break;
618 case K_K5: key = VTERM_KEY_KP_5; break;
619 case K_K6: key = VTERM_KEY_KP_6; break;
620 case K_K7: key = VTERM_KEY_KP_7; break;
621 case K_K8: key = VTERM_KEY_KP_8; break;
622 case K_K9: key = VTERM_KEY_KP_9; break;
623 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
624 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
625 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
626 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
627 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
628 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
629 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
630 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
631 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
632 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
633 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
634 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
635 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200636 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
637 key = VTERM_KEY_LEFT; break;
638 case K_C_LEFT: mod = VTERM_MOD_CTRL;
639 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200640 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
641 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
642 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200643 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
644 key = VTERM_KEY_RIGHT; break;
645 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
646 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200647 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200648 case K_S_UP: mod = VTERM_MOD_SHIFT;
649 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200650 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200651
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200652 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
653 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
654 case K_MOUSELEFT: /* TODO */ return 0;
655 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200656
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200657 case K_LEFTMOUSE:
658 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
659 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
660 case K_LEFTRELEASE:
661 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
662 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
663 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
664 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
665 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
666 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
667 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
668 case K_X1MOUSE: /* TODO */ return 0;
669 case K_X1DRAG: /* TODO */ return 0;
670 case K_X1RELEASE: /* TODO */ return 0;
671 case K_X2MOUSE: /* TODO */ return 0;
672 case K_X2DRAG: /* TODO */ return 0;
673 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200674
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200675 case K_IGNORE: return 0;
676 case K_NOP: return 0;
677 case K_UNDO: return 0;
678 case K_HELP: return 0;
679 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
680 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
681 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
682 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
683 case K_SELECT: return 0;
684#ifdef FEAT_GUI
685 case K_VER_SCROLLBAR: return 0;
686 case K_HOR_SCROLLBAR: return 0;
687#endif
688#ifdef FEAT_GUI_TABLINE
689 case K_TABLINE: return 0;
690 case K_TABMENU: return 0;
691#endif
692#ifdef FEAT_NETBEANS_INTG
693 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
694#endif
695#ifdef FEAT_DND
696 case K_DROP: return 0;
697#endif
698#ifdef FEAT_AUTOCMD
699 case K_CURSORHOLD: return 0;
700#endif
701 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
702 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200703 }
704
705 /*
706 * Convert special keys to vterm keys:
707 * - Write keys to vterm: vterm_keyboard_key()
708 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200709 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200710 */
711 if (key != VTERM_KEY_NONE)
712 /* Special key, let vterm convert it. */
713 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200714 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200715 /* Normal character, let vterm convert it. */
716 vterm_keyboard_unichar(vterm, c, mod);
717
718 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200719 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200720}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200721
Bram Moolenaar938783d2017-07-16 20:13:26 +0200722/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200723 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200724 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200725 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200726term_job_running(term_T *term)
727{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200728 /* Also consider the job finished when the channel is closed, to avoid a
729 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200730 return term != NULL
731 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200732 && term->tl_job->jv_status == JOB_STARTED
733 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200734}
735
736/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200737 * Add the last line of the scrollback buffer to the buffer in the window.
738 */
739 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200740add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200741{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200742 buf_T *buf = term->tl_buffer;
743 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
744 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200745
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200746 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200747 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200748 {
749 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200750 curbuf = buf;
751 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200752 curbuf = curwin->w_buffer;
753 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200754}
755
756/*
757 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200758 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200759 */
760 static void
761move_terminal_to_buffer(term_T *term)
762{
763 win_T *wp;
764 int len;
765 int lines_skipped = 0;
766 VTermPos pos;
767 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200768 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200769 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200770
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200771 if (term->tl_vterm == NULL)
772 return;
773 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200774 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
775 {
776 len = 0;
777 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
778 if (vterm_screen_get_cell(screen, pos, &cell) != 0
779 && cell.chars[0] != NUL)
780 len = pos.col + 1;
781
782 if (len == 0)
783 ++lines_skipped;
784 else
785 {
786 while (lines_skipped > 0)
787 {
788 /* Line was skipped, add an empty line. */
789 --lines_skipped;
790 if (ga_grow(&term->tl_scrollback, 1) == OK)
791 {
792 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
793 + term->tl_scrollback.ga_len;
794
795 line->sb_cols = 0;
796 line->sb_cells = NULL;
797 ++term->tl_scrollback.ga_len;
798
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200799 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200800 }
801 }
802
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200803 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200804 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
805 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200806 garray_T ga;
807 int width;
808 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200809 + term->tl_scrollback.ga_len;
810
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200811 ga_init2(&ga, 1, 100);
812 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200813 {
814 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200815 {
816 width = 1;
817 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
818 if (ga_grow(&ga, 1) == OK)
819 ga.ga_len += mb_char2bytes(' ',
820 (char_u *)ga.ga_data + ga.ga_len);
821 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200822 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200823 {
824 width = cell.width;
825
826 p[pos.col].width = cell.width;
827 p[pos.col].attrs = cell.attrs;
828 p[pos.col].fg = cell.fg;
829 p[pos.col].bg = cell.bg;
830
831 if (ga_grow(&ga, MB_MAXBYTES) == OK)
832 {
833 int i;
834 int c;
835
836 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
837 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
838 (char_u *)ga.ga_data + ga.ga_len);
839 }
840 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200841 }
842 line->sb_cols = len;
843 line->sb_cells = p;
844 ++term->tl_scrollback.ga_len;
845
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200846 if (ga_grow(&ga, 1) == FAIL)
847 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
848 else
849 {
850 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
851 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
852 }
853 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200854 }
855 else
856 vim_free(p);
857 }
858 }
859
860 FOR_ALL_WINDOWS(wp)
861 {
862 if (wp->w_buffer == term->tl_buffer)
863 {
864 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
865 wp->w_cursor.col = 0;
866 wp->w_valid = 0;
867 redraw_win_later(wp, NOT_VALID);
868 }
869 }
870}
871
872 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200873set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200874{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200875 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200876 vim_free(term->tl_status_text);
877 term->tl_status_text = NULL;
878 if (term->tl_buffer == curbuf)
879 maketitle();
880}
881
882/*
883 * Called after the job if finished and Terminal mode is not active:
884 * Move the vterm contents into the scrollback buffer and free the vterm.
885 */
886 static void
887cleanup_vterm(term_T *term)
888{
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200889 if (term->tl_finish == 0)
890 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200891 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200892 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200893}
894
895/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200896 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200897 * Suspends updating the terminal window.
898 */
899 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200900term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200901{
902 term_T *term = curbuf->b_term;
903
904 /* Append the current terminal contents to the buffer. */
905 move_terminal_to_buffer(term);
906
Bram Moolenaar6d819742017-08-06 14:57:49 +0200907 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200908
Bram Moolenaar6d819742017-08-06 14:57:49 +0200909 /* Move the window cursor to the position of the cursor in the
910 * terminal. */
911 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
912 + term->tl_cursor_pos.row + 1;
913 check_cursor();
914 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200915
Bram Moolenaar6d819742017-08-06 14:57:49 +0200916 /* Display the same lines as in the terminal. */
917 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200918}
919
920/*
921 * Returns TRUE if the current window contains a terminal and we are in
922 * Terminal-Normal mode.
923 */
924 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200925term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200926{
927 term_T *term = curbuf->b_term;
928
Bram Moolenaar6d819742017-08-06 14:57:49 +0200929 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200930}
931
932/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200933 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200934 * Restores updating the terminal window.
935 */
936 void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200937term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +0200938{
939 term_T *term = curbuf->b_term;
940 sb_line_T *line;
941 garray_T *gap;
942
943 /* Remove the terminal contents from the scrollback and the buffer. */
944 gap = &term->tl_scrollback;
945 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
946 {
947 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
948 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
949 vim_free(line->sb_cells);
950 --gap->ga_len;
951 if (gap->ga_len == 0)
952 break;
953 }
954 check_cursor();
955
Bram Moolenaar6d819742017-08-06 14:57:49 +0200956 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200957
958 if (term->tl_channel_closed)
959 cleanup_vterm(term);
960 redraw_buf_and_status_later(curbuf, NOT_VALID);
961}
962
963/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200964 * Get a key from the user without mapping.
965 * TODO: use terminal mode mappings.
966 */
967 static int
968term_vgetc()
969{
970 int c;
971
972 ++no_mapping;
973 ++allow_keys;
974 got_int = FALSE;
975 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200976 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200977 --no_mapping;
978 --allow_keys;
979 return c;
980}
981
982/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200983 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200984 * Return FAIL when the key needs to be handled in Normal mode.
985 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200986 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200987 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200988send_keys_to_term(term_T *term, int c, int typed)
989{
990 char msg[KEY_BUF_LEN];
991 size_t len;
992 static int mouse_was_outside = FALSE;
993 int dragging_outside = FALSE;
994
995 /* Catch keys that need to be handled as in Normal mode. */
996 switch (c)
997 {
998 case NUL:
999 case K_ZERO:
1000 if (typed)
1001 stuffcharReadbuff(c);
1002 return FAIL;
1003
1004 case K_IGNORE:
1005 return FAIL;
1006
1007 case K_LEFTDRAG:
1008 case K_MIDDLEDRAG:
1009 case K_RIGHTDRAG:
1010 case K_X1DRAG:
1011 case K_X2DRAG:
1012 dragging_outside = mouse_was_outside;
1013 /* FALLTHROUGH */
1014 case K_LEFTMOUSE:
1015 case K_LEFTMOUSE_NM:
1016 case K_LEFTRELEASE:
1017 case K_LEFTRELEASE_NM:
1018 case K_MIDDLEMOUSE:
1019 case K_MIDDLERELEASE:
1020 case K_RIGHTMOUSE:
1021 case K_RIGHTRELEASE:
1022 case K_X1MOUSE:
1023 case K_X1RELEASE:
1024 case K_X2MOUSE:
1025 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001026
1027 case K_MOUSEUP:
1028 case K_MOUSEDOWN:
1029 case K_MOUSELEFT:
1030 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001031 if (mouse_row < W_WINROW(curwin)
1032 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1033 || mouse_col < W_WINCOL(curwin)
1034 || mouse_col >= W_ENDCOL(curwin)
1035 || dragging_outside)
1036 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001037 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001038 if (typed)
1039 {
1040 stuffcharReadbuff(c);
1041 mouse_was_outside = TRUE;
1042 }
1043 return FAIL;
1044 }
1045 }
1046 if (typed)
1047 mouse_was_outside = FALSE;
1048
1049 /* Convert the typed key to a sequence of bytes for the job. */
1050 len = term_convert_key(term, c, msg);
1051 if (len > 0)
1052 /* TODO: if FAIL is returned, stop? */
1053 channel_send(term->tl_job->jv_channel, PART_IN,
1054 (char_u *)msg, (int)len, NULL);
1055
1056 return OK;
1057}
1058
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001059 static void
1060position_cursor(win_T *wp, VTermPos *pos)
1061{
1062 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1063 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1064 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1065}
1066
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001067/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001068 * Handle CTRL-W "": send register contents to the job.
1069 */
1070 static void
1071term_paste_register(int prev_c UNUSED)
1072{
1073 int c;
1074 list_T *l;
1075 listitem_T *item;
1076 long reglen = 0;
1077 int type;
1078
1079#ifdef FEAT_CMDL_INFO
1080 if (add_to_showcmd(prev_c))
1081 if (add_to_showcmd('"'))
1082 out_flush();
1083#endif
1084 c = term_vgetc();
1085#ifdef FEAT_CMDL_INFO
1086 clear_showcmd();
1087#endif
1088
1089 /* CTRL-W "= prompt for expression to evaluate. */
1090 if (c == '=' && get_expr_register() != '=')
1091 return;
1092
1093 l = (list_T *)get_reg_contents(c, GREG_LIST);
1094 if (l != NULL)
1095 {
1096 type = get_reg_type(c, &reglen);
1097 for (item = l->lv_first; item != NULL; item = item->li_next)
1098 {
1099 char_u *s = get_tv_string(&item->li_tv);
1100
1101 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1102 s, STRLEN(s), NULL);
1103 if (item->li_next != NULL || type == MLINE)
1104 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1105 (char_u *)"\r", 1, NULL);
1106 }
1107 list_free(l);
1108 }
1109}
1110
1111/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001112 * Returns TRUE if the current window contains a terminal and we are sending
1113 * keys to the job.
1114 */
1115 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001116term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001117{
1118 term_T *term = curbuf->b_term;
1119
1120 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001121 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001122 && term->tl_vterm != NULL
1123 && term_job_running(term);
1124}
1125
1126/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001127 * Wait for input and send it to the job.
1128 * Return when the start of a CTRL-W command is typed or anything else that
1129 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001130 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1131 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001132 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001133 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001134terminal_loop(void)
1135{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001136 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001137 int termkey = 0;
1138
1139 if (*curwin->w_p_tk != NUL)
1140 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001141 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001142
1143 for (;;)
1144 {
1145 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001146 /* Repeat redrawing in case a message is received while redrawing. */
1147 while (curwin->w_redr_type != 0)
1148 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001149 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001150
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001151 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001152 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001153 /* job finished while waiting for a character */
1154 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001155
Bram Moolenaarfae42832017-08-01 22:24:26 +02001156#ifdef UNIX
1157 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1158#endif
1159#ifdef WIN3264
1160 if (c == Ctrl_C)
1161 /* We don't know if the job can handle CTRL-C itself or not, this
1162 * may kill the shell instead of killing the command running in the
1163 * shell. */
Bram Moolenaar2d33e902017-08-11 16:31:54 +02001164 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001165#endif
1166
Bram Moolenaar69198192017-08-05 14:10:48 +02001167 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001168 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001169 int prev_c = c;
1170
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001171#ifdef FEAT_CMDL_INFO
1172 if (add_to_showcmd(c))
1173 out_flush();
1174#endif
1175 c = term_vgetc();
1176#ifdef FEAT_CMDL_INFO
1177 clear_showcmd();
1178#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001179 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001180 /* job finished while waiting for a character */
1181 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001182
Bram Moolenaar69198192017-08-05 14:10:48 +02001183 if (prev_c == Ctrl_BSL)
1184 {
1185 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001186 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001187 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1188 term_enter_normal_mode();
1189 return FAIL;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001190 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001191 /* Send both keys to the terminal. */
1192 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1193 }
1194 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001195 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001196 /* "CTRL-W .": send CTRL-W to the job */
1197 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001198 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001199 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001200 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001201 /* CTRL-W N : go to Terminal-Normal mode. */
1202 term_enter_normal_mode();
Bram Moolenaar423802d2017-07-30 16:52:24 +02001203 return FAIL;
1204 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001205 else if (c == '"')
1206 {
1207 term_paste_register(prev_c);
1208 continue;
1209 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001210 else if (termkey == 0 || c != termkey)
1211 {
1212 stuffcharReadbuff(Ctrl_W);
1213 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001214 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001215 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001216 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001217 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1218 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001219 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001220 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001221}
1222
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001223/*
1224 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001225 * This updates the title and status, but does not close the vter, because
1226 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001227 */
1228 void
1229term_job_ended(job_T *job)
1230{
1231 term_T *term;
1232 int did_one = FALSE;
1233
1234 for (term = first_term; term != NULL; term = term->tl_next)
1235 if (term->tl_job == job)
1236 {
1237 vim_free(term->tl_title);
1238 term->tl_title = NULL;
1239 vim_free(term->tl_status_text);
1240 term->tl_status_text = NULL;
1241 redraw_buf_and_status_later(term->tl_buffer, VALID);
1242 did_one = TRUE;
1243 }
1244 if (did_one)
1245 redraw_statuslines();
1246 if (curbuf->b_term != NULL)
1247 {
1248 if (curbuf->b_term->tl_job == job)
1249 maketitle();
1250 update_cursor(curbuf->b_term, TRUE);
1251 }
1252}
1253
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001254 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001255may_toggle_cursor(term_T *term)
1256{
1257 if (curbuf == term->tl_buffer)
1258 {
1259 if (term->tl_cursor_visible)
1260 cursor_on();
1261 else
1262 cursor_off();
1263 }
1264}
1265
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001266 static int
1267handle_damage(VTermRect rect, void *user)
1268{
1269 term_T *term = (term_T *)user;
1270
1271 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1272 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1273 redraw_buf_later(term->tl_buffer, NOT_VALID);
1274 return 1;
1275}
1276
1277 static int
1278handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1279{
1280 term_T *term = (term_T *)user;
1281
1282 /* TODO */
1283 redraw_buf_later(term->tl_buffer, NOT_VALID);
1284 return 1;
1285}
1286
1287 static int
1288handle_movecursor(
1289 VTermPos pos,
1290 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001291 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001292 void *user)
1293{
1294 term_T *term = (term_T *)user;
1295 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001296
1297 term->tl_cursor_pos = pos;
1298 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001299
1300 FOR_ALL_WINDOWS(wp)
1301 {
1302 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001303 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001304 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001305 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001306 {
1307 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001308 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001309 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001310
1311 return 1;
1312}
1313
Bram Moolenaar21554412017-07-24 21:44:43 +02001314 static int
1315handle_settermprop(
1316 VTermProp prop,
1317 VTermValue *value,
1318 void *user)
1319{
1320 term_T *term = (term_T *)user;
1321
1322 switch (prop)
1323 {
1324 case VTERM_PROP_TITLE:
1325 vim_free(term->tl_title);
1326 term->tl_title = vim_strsave((char_u *)value->string);
1327 vim_free(term->tl_status_text);
1328 term->tl_status_text = NULL;
1329 if (term == curbuf->b_term)
1330 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001331 break;
1332
1333 case VTERM_PROP_CURSORVISIBLE:
1334 term->tl_cursor_visible = value->boolean;
1335 may_toggle_cursor(term);
1336 out_flush();
1337 break;
1338
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001339 case VTERM_PROP_ALTSCREEN:
1340 /* TODO: do anything else? */
1341 term->tl_using_altscreen = value->boolean;
1342 break;
1343
Bram Moolenaar21554412017-07-24 21:44:43 +02001344 default:
1345 break;
1346 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001347 /* Always return 1, otherwise vterm doesn't store the value internally. */
1348 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001349}
1350
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001351/*
1352 * The job running in the terminal resized the terminal.
1353 */
1354 static int
1355handle_resize(int rows, int cols, void *user)
1356{
1357 term_T *term = (term_T *)user;
1358 win_T *wp;
1359
1360 term->tl_rows = rows;
1361 term->tl_cols = cols;
1362 FOR_ALL_WINDOWS(wp)
1363 {
1364 if (wp->w_buffer == term->tl_buffer)
1365 {
1366 win_setheight_win(rows, wp);
1367 win_setwidth_win(cols, wp);
1368 }
1369 }
1370
1371 redraw_buf_later(term->tl_buffer, NOT_VALID);
1372 return 1;
1373}
1374
Bram Moolenaard85f2712017-07-28 21:51:57 +02001375/*
1376 * Handle a line that is pushed off the top of the screen.
1377 */
1378 static int
1379handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1380{
1381 term_T *term = (term_T *)user;
1382
1383 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001384 if (ga_grow(&term->tl_scrollback, 1) == OK)
1385 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001386 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001387 int len = 0;
1388 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001389 int c;
1390 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001391 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001392 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001393
1394 /* do not store empty cells at the end */
1395 for (i = 0; i < cols; ++i)
1396 if (cells[i].chars[0] != 0)
1397 len = i + 1;
1398
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001399 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001400 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001401 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001402 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001403 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001404 for (col = 0; col < len; col += cells[col].width)
1405 {
1406 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1407 {
1408 ga.ga_len = 0;
1409 break;
1410 }
1411 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1412 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1413 (char_u *)ga.ga_data + ga.ga_len);
1414 p[col].width = cells[col].width;
1415 p[col].attrs = cells[col].attrs;
1416 p[col].fg = cells[col].fg;
1417 p[col].bg = cells[col].bg;
1418 }
1419 }
1420 if (ga_grow(&ga, 1) == FAIL)
1421 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1422 else
1423 {
1424 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1425 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1426 }
1427 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001428
1429 line = (sb_line_T *)term->tl_scrollback.ga_data
1430 + term->tl_scrollback.ga_len;
1431 line->sb_cols = len;
1432 line->sb_cells = p;
1433 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001434 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001435 }
1436 return 0; /* ignored */
1437}
1438
Bram Moolenaar21554412017-07-24 21:44:43 +02001439static VTermScreenCallbacks screen_callbacks = {
1440 handle_damage, /* damage */
1441 handle_moverect, /* moverect */
1442 handle_movecursor, /* movecursor */
1443 handle_settermprop, /* settermprop */
1444 NULL, /* bell */
1445 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001446 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001447 NULL /* sb_popline */
1448};
1449
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001450/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001451 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001452 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001453 */
1454 void
1455term_channel_closed(channel_T *ch)
1456{
1457 term_T *term;
1458 int did_one = FALSE;
1459
1460 for (term = first_term; term != NULL; term = term->tl_next)
1461 if (term->tl_job == ch->ch_job)
1462 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001463 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001464 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001465
Bram Moolenaard85f2712017-07-28 21:51:57 +02001466 vim_free(term->tl_title);
1467 term->tl_title = NULL;
1468 vim_free(term->tl_status_text);
1469 term->tl_status_text = NULL;
1470
Bram Moolenaar423802d2017-07-30 16:52:24 +02001471 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001472 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001473 {
1474 int fnum = term->tl_buffer->b_fnum;
1475
Bram Moolenaar423802d2017-07-30 16:52:24 +02001476 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001477
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001478 if (term->tl_finish == 'c')
1479 {
1480 /* ++close or term_finish == "close" */
1481 curbuf = term->tl_buffer;
1482 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1483 break;
1484 }
1485 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1486 {
1487 char buf[50];
1488
1489 /* TODO: use term_opencmd */
1490 vim_snprintf(buf, sizeof(buf), "botright sbuf %d", fnum);
1491 do_cmdline_cmd((char_u *)buf);
1492 }
1493 }
1494
Bram Moolenaard85f2712017-07-28 21:51:57 +02001495 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001496 }
1497 if (did_one)
1498 {
1499 redraw_statuslines();
1500
1501 /* Need to break out of vgetc(). */
1502 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001503 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001504
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001505 term = curbuf->b_term;
1506 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001507 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001508 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001509 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001510 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001511 }
1512 }
1513}
1514
1515/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001516 * Reverse engineer the RGB value into a cterm color index.
1517 * First color is 1. Return 0 if no match found.
1518 */
1519 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001520color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001521{
1522 int red = color->red;
1523 int blue = color->blue;
1524 int green = color->green;
1525
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001526 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001527 if (red == 0)
1528 {
1529 if (green == 0)
1530 {
1531 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001532 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001533 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001534 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001535 }
1536 else if (green == 224)
1537 {
1538 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001539 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001540 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001541 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001542 }
1543 }
1544 else if (red == 224)
1545 {
1546 if (green == 0)
1547 {
1548 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001549 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001550 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001551 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001552 }
1553 else if (green == 224)
1554 {
1555 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001556 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001557 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001558 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001559 }
1560 }
1561 else if (red == 128)
1562 {
1563 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001564 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001565 }
1566 else if (red == 255)
1567 {
1568 if (green == 64)
1569 {
1570 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001571 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001572 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001573 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001574 }
1575 else if (green == 255)
1576 {
1577 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001578 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001579 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001580 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001581 }
1582 }
1583 else if (red == 64)
1584 {
1585 if (green == 64)
1586 {
1587 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001588 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001589 }
1590 else if (green == 255)
1591 {
1592 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001593 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001594 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001595 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001596 }
1597 }
1598 if (t_colors >= 256)
1599 {
1600 if (red == blue && red == green)
1601 {
1602 /* 24-color greyscale */
1603 static int cutoff[23] = {
1604 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1605 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1606 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1607 int i;
1608
1609 for (i = 0; i < 23; ++i)
1610 if (red < cutoff[i])
1611 return i + 233;
1612 return 256;
1613 }
1614
1615 /* 216-color cube */
1616 return 17 + ((red + 25) / 0x33) * 36
1617 + ((green + 25) / 0x33) * 6
1618 + (blue + 25) / 0x33;
1619 }
1620 return 0;
1621}
1622
1623/*
1624 * Convert the attributes of a vterm cell into an attribute index.
1625 */
1626 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001627cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001628{
1629 int attr = 0;
1630
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001631 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001632 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001633 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001634 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001635 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001636 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001637 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001638 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001639 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001640 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001641
1642#ifdef FEAT_GUI
1643 if (gui.in_use)
1644 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001645 guicolor_T fg, bg;
1646
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001647 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1648 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001649 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001650 }
1651 else
1652#endif
1653#ifdef FEAT_TERMGUICOLORS
1654 if (p_tgc)
1655 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001656 guicolor_T fg, bg;
1657
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001658 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1659 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001660
1661 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001662 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001663 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001664#endif
1665 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001666 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001667 int fg = color2index(&cellfg, TRUE, &bold);
1668 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001669
1670 /* with 8 colors set the bold attribute to get a bright foreground */
1671 if (bold == TRUE)
1672 attr |= HL_BOLD;
1673 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001674 }
1675 return 0;
1676}
1677
1678/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001679 * Called to update a window that contains an active terminal.
1680 * Returns FAIL when there is no terminal running in this window or in
1681 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001682 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001683 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001684term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001685{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001686 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001687 VTerm *vterm;
1688 VTermScreen *screen;
1689 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001690 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001691
Bram Moolenaar6d819742017-08-06 14:57:49 +02001692 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001693 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001694
Bram Moolenaard85f2712017-07-28 21:51:57 +02001695 vterm = term->tl_vterm;
1696 screen = vterm_obtain_screen(vterm);
1697 state = vterm_obtain_state(vterm);
1698
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001699 /*
1700 * If the window was resized a redraw will be triggered and we get here.
1701 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1702 */
1703 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1704 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001705 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001706 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1707 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1708 win_T *twp;
1709
1710 FOR_ALL_WINDOWS(twp)
1711 {
1712 /* When more than one window shows the same terminal, use the
1713 * smallest size. */
1714 if (twp->w_buffer == term->tl_buffer)
1715 {
1716 if (!term->tl_rows_fixed && rows > twp->w_height)
1717 rows = twp->w_height;
1718 if (!term->tl_cols_fixed && cols > twp->w_width)
1719 cols = twp->w_width;
1720 }
1721 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001722
1723 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001724 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001725 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001726 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001727 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001728
1729 /* The cursor may have been moved when resizing. */
1730 vterm_state_get_cursorpos(state, &pos);
1731 position_cursor(wp, &pos);
1732
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001733 /* TODO: Only redraw what changed. */
1734 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001735 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001736 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001737 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001738
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001739 if (pos.row < term->tl_rows)
1740 {
1741 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001742 {
1743 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001744 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001745
Bram Moolenaareeac6772017-07-23 15:48:37 +02001746 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1747 vim_memset(&cell, 0, sizeof(cell));
1748
1749 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001750 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001751 if (c == NUL)
1752 {
1753 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001754#if defined(FEAT_MBYTE)
1755 if (enc_utf8)
1756 ScreenLinesUC[off] = NUL;
1757#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001758 }
1759 else
1760 {
1761#if defined(FEAT_MBYTE)
1762 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001763 {
1764 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001765 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001766 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001767 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001768 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001769 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001770 if (enc_utf8)
1771 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001772 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001773#else
1774 ScreenLines[off] = c;
1775#endif
1776 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001777 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001778
1779 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001780 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001781 if (cell.width == 2)
1782 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001783 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001784#if defined(FEAT_MBYTE)
1785 if (enc_utf8)
1786 ScreenLinesUC[off] = NUL;
1787#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001788 ++pos.col;
1789 ++off;
1790 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001791 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001792 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001793 else
1794 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001795
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001796 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1797 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001798 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001799
1800 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001801}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001802
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001803/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001804 * Return TRUE if "wp" is a terminal window where the job has finished.
1805 */
1806 int
1807term_is_finished(buf_T *buf)
1808{
1809 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1810}
1811
1812/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001813 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02001814 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001815 */
1816 int
1817term_show_buffer(buf_T *buf)
1818{
1819 term_T *term = buf->b_term;
1820
Bram Moolenaar6d819742017-08-06 14:57:49 +02001821 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001822}
1823
1824/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001825 * The current buffer is going to be changed. If there is terminal
1826 * highlighting remove it now.
1827 */
1828 void
1829term_change_in_curbuf(void)
1830{
1831 term_T *term = curbuf->b_term;
1832
1833 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1834 {
1835 free_scrollback(term);
1836 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001837
1838 /* The buffer is now like a normal buffer, it cannot be easily
1839 * abandoned when changed. */
1840 set_string_option_direct((char_u *)"buftype", -1,
1841 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001842 }
1843}
1844
1845/*
1846 * Get the screen attribute for a position in the buffer.
1847 */
1848 int
1849term_get_attr(buf_T *buf, linenr_T lnum, int col)
1850{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001851 term_T *term = buf->b_term;
1852 sb_line_T *line;
1853 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001854
Bram Moolenaar70229f92017-07-29 16:01:53 +02001855 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001856 return 0;
1857 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1858 if (col >= line->sb_cols)
1859 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001860 cellattr = line->sb_cells + col;
1861 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001862}
1863
1864/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001865 * Create a new vterm and initialize it.
1866 */
1867 static void
1868create_vterm(term_T *term, int rows, int cols)
1869{
1870 VTerm *vterm;
1871 VTermScreen *screen;
1872
1873 vterm = vterm_new(rows, cols);
1874 term->tl_vterm = vterm;
1875 screen = vterm_obtain_screen(vterm);
1876 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1877 /* TODO: depends on 'encoding'. */
1878 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001879
1880 /* Vterm uses a default black background. Set it to white when
1881 * 'background' is "light". */
1882 if (*p_bg == 'l')
1883 {
1884 VTermColor fg, bg;
1885
1886 fg.red = fg.green = fg.blue = 0;
1887 bg.red = bg.green = bg.blue = 255;
1888 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1889 }
1890
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001891 /* Required to initialize most things. */
1892 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001893
1894 /* Allow using alternate screen. */
1895 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001896}
1897
Bram Moolenaar21554412017-07-24 21:44:43 +02001898/*
1899 * Return the text to show for the buffer name and status.
1900 */
1901 char_u *
1902term_get_status_text(term_T *term)
1903{
1904 if (term->tl_status_text == NULL)
1905 {
1906 char_u *txt;
1907 size_t len;
1908
Bram Moolenaar6d819742017-08-06 14:57:49 +02001909 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001910 {
1911 if (term_job_running(term))
1912 txt = (char_u *)_("Terminal");
1913 else
1914 txt = (char_u *)_("Terminal-finished");
1915 }
1916 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001917 txt = term->tl_title;
1918 else if (term_job_running(term))
1919 txt = (char_u *)_("running");
1920 else
1921 txt = (char_u *)_("finished");
1922 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001923 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001924 if (term->tl_status_text != NULL)
1925 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1926 term->tl_buffer->b_fname, txt);
1927 }
1928 return term->tl_status_text;
1929}
1930
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001931/*
1932 * Mark references in jobs of terminals.
1933 */
1934 int
1935set_ref_in_term(int copyID)
1936{
1937 int abort = FALSE;
1938 term_T *term;
1939 typval_T tv;
1940
1941 for (term = first_term; term != NULL; term = term->tl_next)
1942 if (term->tl_job != NULL)
1943 {
1944 tv.v_type = VAR_JOB;
1945 tv.vval.v_job = term->tl_job;
1946 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1947 }
1948 return abort;
1949}
1950
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001951/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001952 * Get the buffer from the first argument in "argvars".
1953 * Returns NULL when the buffer is not for a terminal window.
1954 */
1955 static buf_T *
1956term_get_buf(typval_T *argvars)
1957{
1958 buf_T *buf;
1959
1960 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1961 ++emsg_off;
1962 buf = get_buf_tv(&argvars[0], FALSE);
1963 --emsg_off;
1964 if (buf == NULL || buf->b_term == NULL)
1965 return NULL;
1966 return buf;
1967}
1968
1969/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001970 * "term_getaltscreen(buf)" function
1971 */
1972 void
1973f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
1974{
1975 buf_T *buf = term_get_buf(argvars);
1976
1977 if (buf == NULL)
1978 return;
1979 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
1980}
1981
1982/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001983 * "term_getattr(attr, name)" function
1984 */
1985 void
1986f_term_getattr(typval_T *argvars, typval_T *rettv)
1987{
1988 int attr;
1989 size_t i;
1990 char_u *name;
1991
1992 static struct {
1993 char *name;
1994 int attr;
1995 } attrs[] = {
1996 {"bold", HL_BOLD},
1997 {"italic", HL_ITALIC},
1998 {"underline", HL_UNDERLINE},
1999 {"strike", HL_STANDOUT},
2000 {"reverse", HL_INVERSE},
2001 };
2002
2003 attr = get_tv_number(&argvars[0]);
2004 name = get_tv_string_chk(&argvars[1]);
2005 if (name == NULL)
2006 return;
2007
2008 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2009 if (STRCMP(name, attrs[i].name) == 0)
2010 {
2011 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2012 break;
2013 }
2014}
2015
2016/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002017 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002018 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002019 void
2020f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002021{
Bram Moolenaar97870002017-07-30 18:28:38 +02002022 buf_T *buf = term_get_buf(argvars);
2023 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002024
Bram Moolenaar97870002017-07-30 18:28:38 +02002025 if (rettv_list_alloc(rettv) == FAIL)
2026 return;
2027 if (buf == NULL)
2028 return;
2029
2030 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002031 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
2032 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02002033 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002034}
2035
2036/*
2037 * "term_getjob(buf)" function
2038 */
2039 void
2040f_term_getjob(typval_T *argvars, typval_T *rettv)
2041{
2042 buf_T *buf = term_get_buf(argvars);
2043
2044 rettv->v_type = VAR_JOB;
2045 rettv->vval.v_job = NULL;
2046 if (buf == NULL)
2047 return;
2048
2049 rettv->vval.v_job = buf->b_term->tl_job;
2050 if (rettv->vval.v_job != NULL)
2051 ++rettv->vval.v_job->jv_refcount;
2052}
2053
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002054 static int
2055get_row_number(typval_T *tv, term_T *term)
2056{
2057 if (tv->v_type == VAR_STRING
2058 && tv->vval.v_string != NULL
2059 && STRCMP(tv->vval.v_string, ".") == 0)
2060 return term->tl_cursor_pos.row;
2061 return (int)get_tv_number(tv) - 1;
2062}
2063
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002064/*
2065 * "term_getline(buf, row)" function
2066 */
2067 void
2068f_term_getline(typval_T *argvars, typval_T *rettv)
2069{
2070 buf_T *buf = term_get_buf(argvars);
2071 term_T *term;
2072 int row;
2073
2074 rettv->v_type = VAR_STRING;
2075 if (buf == NULL)
2076 return;
2077 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002078 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002079
2080 if (term->tl_vterm == NULL)
2081 {
2082 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2083
2084 /* vterm is finished, get the text from the buffer */
2085 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2086 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2087 }
2088 else
2089 {
2090 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2091 VTermRect rect;
2092 int len;
2093 char_u *p;
2094
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002095 if (row < 0 || row >= term->tl_rows)
2096 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002097 len = term->tl_cols * MB_MAXBYTES + 1;
2098 p = alloc(len);
2099 if (p == NULL)
2100 return;
2101 rettv->vval.v_string = p;
2102
2103 rect.start_col = 0;
2104 rect.end_col = term->tl_cols;
2105 rect.start_row = row;
2106 rect.end_row = row + 1;
2107 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2108 }
2109}
2110
2111/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002112 * "term_getscrolled(buf)" function
2113 */
2114 void
2115f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2116{
2117 buf_T *buf = term_get_buf(argvars);
2118
2119 if (buf == NULL)
2120 return;
2121 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2122}
2123
2124/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002125 * "term_getsize(buf)" function
2126 */
2127 void
2128f_term_getsize(typval_T *argvars, typval_T *rettv)
2129{
2130 buf_T *buf = term_get_buf(argvars);
2131 list_T *l;
2132
2133 if (rettv_list_alloc(rettv) == FAIL)
2134 return;
2135 if (buf == NULL)
2136 return;
2137
2138 l = rettv->vval.v_list;
2139 list_append_number(l, buf->b_term->tl_rows);
2140 list_append_number(l, buf->b_term->tl_cols);
2141}
2142
2143/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002144 * "term_getstatus(buf)" function
2145 */
2146 void
2147f_term_getstatus(typval_T *argvars, typval_T *rettv)
2148{
2149 buf_T *buf = term_get_buf(argvars);
2150 term_T *term;
2151 char_u val[100];
2152
2153 rettv->v_type = VAR_STRING;
2154 if (buf == NULL)
2155 return;
2156 term = buf->b_term;
2157
2158 if (term_job_running(term))
2159 STRCPY(val, "running");
2160 else
2161 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002162 if (term->tl_normal_mode)
2163 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002164 rettv->vval.v_string = vim_strsave(val);
2165}
2166
2167/*
2168 * "term_gettitle(buf)" function
2169 */
2170 void
2171f_term_gettitle(typval_T *argvars, typval_T *rettv)
2172{
2173 buf_T *buf = term_get_buf(argvars);
2174
2175 rettv->v_type = VAR_STRING;
2176 if (buf == NULL)
2177 return;
2178
2179 if (buf->b_term->tl_title != NULL)
2180 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2181}
2182
2183/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002184 * "term_gettty(buf)" function
2185 */
2186 void
2187f_term_gettty(typval_T *argvars, typval_T *rettv)
2188{
2189 buf_T *buf = term_get_buf(argvars);
2190 char_u *p;
2191
2192 rettv->v_type = VAR_STRING;
2193 if (buf == NULL)
2194 return;
2195 if (buf->b_term->tl_job != NULL)
2196 p = buf->b_term->tl_job->jv_tty_name;
2197 else
2198 p = buf->b_term->tl_tty_name;
2199 if (p != NULL)
2200 rettv->vval.v_string = vim_strsave(p);
2201}
2202
2203/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002204 * "term_list()" function
2205 */
2206 void
2207f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2208{
2209 term_T *tp;
2210 list_T *l;
2211
2212 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2213 return;
2214
2215 l = rettv->vval.v_list;
2216 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2217 if (tp != NULL && tp->tl_buffer != NULL)
2218 if (list_append_number(l,
2219 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2220 return;
2221}
2222
2223/*
2224 * "term_scrape(buf, row)" function
2225 */
2226 void
2227f_term_scrape(typval_T *argvars, typval_T *rettv)
2228{
2229 buf_T *buf = term_get_buf(argvars);
2230 VTermScreen *screen = NULL;
2231 VTermPos pos;
2232 list_T *l;
2233 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002234 char_u *p;
2235 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002236
2237 if (rettv_list_alloc(rettv) == FAIL)
2238 return;
2239 if (buf == NULL)
2240 return;
2241 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002242
2243 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002244 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002245
2246 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002247 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002248 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002249 p = NULL;
2250 line = NULL;
2251 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002252 else
2253 {
2254 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2255
2256 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2257 return;
2258 p = ml_get_buf(buf, lnum + 1, FALSE);
2259 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2260 }
2261
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002262 for (pos.col = 0; pos.col < term->tl_cols; )
2263 {
2264 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002265 int width;
2266 VTermScreenCellAttrs attrs;
2267 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002268 char_u rgb[8];
2269 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2270 int off = 0;
2271 int i;
2272
2273 if (screen == NULL)
2274 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002275 cellattr_T *cellattr;
2276 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002277
2278 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002279 if (pos.col >= line->sb_cols)
2280 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002281 cellattr = line->sb_cells + pos.col;
2282 width = cellattr->width;
2283 attrs = cellattr->attrs;
2284 fg = cellattr->fg;
2285 bg = cellattr->bg;
2286 len = MB_PTR2LEN(p);
2287 mch_memmove(mbs, p, len);
2288 mbs[len] = NUL;
2289 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002290 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002291 else
2292 {
2293 VTermScreenCell cell;
2294 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2295 break;
2296 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2297 {
2298 if (cell.chars[i] == 0)
2299 break;
2300 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2301 }
2302 mbs[off] = NUL;
2303 width = cell.width;
2304 attrs = cell.attrs;
2305 fg = cell.fg;
2306 bg = cell.bg;
2307 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002308 dcell = dict_alloc();
2309 list_append_dict(l, dcell);
2310
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002311 dict_add_nr_str(dcell, "chars", 0, mbs);
2312
2313 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002314 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002315 dict_add_nr_str(dcell, "fg", 0, rgb);
2316 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002317 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002318 dict_add_nr_str(dcell, "bg", 0, rgb);
2319
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002320 dict_add_nr_str(dcell, "attr",
2321 cell2attr(attrs, fg, bg), NULL);
2322 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002323
2324 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002325 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002326 ++pos.col;
2327 }
2328}
2329
2330/*
2331 * "term_sendkeys(buf, keys)" function
2332 */
2333 void
2334f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2335{
2336 buf_T *buf = term_get_buf(argvars);
2337 char_u *msg;
2338 term_T *term;
2339
2340 rettv->v_type = VAR_UNKNOWN;
2341 if (buf == NULL)
2342 return;
2343
2344 msg = get_tv_string_chk(&argvars[1]);
2345 if (msg == NULL)
2346 return;
2347 term = buf->b_term;
2348 if (term->tl_vterm == NULL)
2349 return;
2350
2351 while (*msg != NUL)
2352 {
2353 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2354 msg += MB_PTR2LEN(msg);
2355 }
2356
Bram Moolenaar6d819742017-08-06 14:57:49 +02002357 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002358 {
2359 /* TODO: only update once in a while. */
2360 update_screen(0);
2361 if (buf == curbuf)
2362 update_cursor(term, TRUE);
2363 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002364}
2365
2366/*
2367 * "term_start(command, options)" function
2368 */
2369 void
2370f_term_start(typval_T *argvars, typval_T *rettv)
2371{
2372 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002373 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002374
2375 if (cmd == NULL)
2376 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002377 init_job_options(&opt);
2378 /* TODO: allow more job options */
2379 if (argvars[1].v_type != VAR_UNKNOWN
2380 && get_job_options(&argvars[1], &opt,
2381 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002382 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
2383 JO2_TERM_NAME + JO2_TERM_FINISH
Bram Moolenaarda43b612017-08-11 22:27:50 +02002384 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002385 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002386 return;
2387
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002388 if (opt.jo_vertical)
2389 cmdmod.split = WSP_VERT;
Bram Moolenaarda43b612017-08-11 22:27:50 +02002390 term_start(cmd, &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002391
2392 if (curbuf->b_term != NULL)
2393 rettv->vval.v_number = curbuf->b_fnum;
2394}
2395
2396/*
2397 * "term_wait" function
2398 */
2399 void
2400f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2401{
2402 buf_T *buf = term_get_buf(argvars);
2403
2404 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002405 {
2406 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002407 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002408 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002409 if (buf->b_term->tl_job == NULL)
2410 {
2411 ch_log(NULL, "term_wait(): no job to wait for");
2412 return;
2413 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002414
2415 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002416 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002417 {
2418 /* The job is dead, keep reading channel I/O until the channel is
2419 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002420 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002421 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2422 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002423 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002424 parse_queued_messages();
2425 ui_delay(10L, FALSE);
2426 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002427 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002428 parse_queued_messages();
2429 }
2430 else
2431 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002432 long wait = 10L;
2433
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002434 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002435 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002436
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002437 /* Wait for some time for any channel I/O. */
2438 if (argvars[1].v_type != VAR_UNKNOWN)
2439 wait = get_tv_number(&argvars[1]);
2440 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002441 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002442
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002443 /* Flushing messages on channels is hopefully sufficient.
2444 * TODO: is there a better way? */
2445 parse_queued_messages();
2446 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002447}
2448
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002449# ifdef WIN3264
2450
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002451/**************************************
2452 * 2. MS-Windows implementation.
2453 */
2454
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002455#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2456#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2457
Bram Moolenaar8a773062017-07-24 22:29:21 +02002458void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002459void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002460void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002461BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2462void (*winpty_config_set_initial_size)(void*, int, int);
2463LPCWSTR (*winpty_conin_name)(void*);
2464LPCWSTR (*winpty_conout_name)(void*);
2465LPCWSTR (*winpty_conerr_name)(void*);
2466void (*winpty_free)(void*);
2467void (*winpty_config_free)(void*);
2468void (*winpty_spawn_config_free)(void*);
2469void (*winpty_error_free)(void*);
2470LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002471BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002472HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002473
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002474#define WINPTY_DLL "winpty.dll"
2475
2476static HINSTANCE hWinPtyDLL = NULL;
2477
2478 int
2479dyn_winpty_init(void)
2480{
2481 int i;
2482 static struct
2483 {
2484 char *name;
2485 FARPROC *ptr;
2486 } winpty_entry[] =
2487 {
2488 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2489 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2490 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2491 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2492 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2493 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2494 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2495 {"winpty_free", (FARPROC*)&winpty_free},
2496 {"winpty_open", (FARPROC*)&winpty_open},
2497 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2498 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2499 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2500 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002501 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002502 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002503 {NULL, NULL}
2504 };
2505
2506 /* No need to initialize twice. */
2507 if (hWinPtyDLL)
2508 return 1;
2509 /* Load winpty.dll */
2510 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2511 if (!hWinPtyDLL)
2512 {
2513 EMSG2(_(e_loadlib), WINPTY_DLL);
2514 return 0;
2515 }
2516 for (i = 0; winpty_entry[i].name != NULL
2517 && winpty_entry[i].ptr != NULL; ++i)
2518 {
2519 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2520 winpty_entry[i].name)) == NULL)
2521 {
2522 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2523 return 0;
2524 }
2525 }
2526
2527 return 1;
2528}
2529
2530/*
2531 * Create a new terminal of "rows" by "cols" cells.
2532 * Store a reference in "term".
2533 * Return OK or FAIL.
2534 */
2535 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002536term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002537{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002538 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002539 channel_T *channel = NULL;
2540 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002541 DWORD error;
2542 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2543 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002544 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002545 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002546
2547 if (!dyn_winpty_init())
2548 return FAIL;
2549
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002550 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002551 if (p == NULL)
2552 return FAIL;
2553
2554 job = job_alloc();
2555 if (job == NULL)
2556 goto failed;
2557
2558 channel = add_channel();
2559 if (channel == NULL)
2560 goto failed;
2561
2562 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2563 if (term->tl_winpty_config == NULL)
2564 goto failed;
2565
2566 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2567 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2568 if (term->tl_winpty == NULL)
2569 goto failed;
2570
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002571 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002572 spawn_config = winpty_spawn_config_new(
2573 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2574 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2575 NULL,
2576 p,
2577 NULL,
2578 NULL,
2579 &winpty_err);
2580 if (spawn_config == NULL)
2581 goto failed;
2582
2583 channel = add_channel();
2584 if (channel == NULL)
2585 goto failed;
2586
2587 job = job_alloc();
2588 if (job == NULL)
2589 goto failed;
2590
2591 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2592 &child_thread_handle, &error, &winpty_err))
2593 goto failed;
2594
2595 channel_set_pipes(channel,
2596 (sock_T) CreateFileW(
2597 winpty_conin_name(term->tl_winpty),
2598 GENERIC_WRITE, 0, NULL,
2599 OPEN_EXISTING, 0, NULL),
2600 (sock_T) CreateFileW(
2601 winpty_conout_name(term->tl_winpty),
2602 GENERIC_READ, 0, NULL,
2603 OPEN_EXISTING, 0, NULL),
2604 (sock_T) CreateFileW(
2605 winpty_conerr_name(term->tl_winpty),
2606 GENERIC_READ, 0, NULL,
2607 OPEN_EXISTING, 0, NULL));
2608
2609 jo = CreateJobObject(NULL, NULL);
2610 if (jo == NULL)
2611 goto failed;
2612
2613 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002614 {
2615 /* Failed, switch the way to terminate process with TerminateProcess. */
2616 CloseHandle(jo);
2617 jo = NULL;
2618 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002619
2620 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002621 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002622
2623 create_vterm(term, rows, cols);
2624
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002625 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002626 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002627
2628 job->jv_channel = channel;
2629 job->jv_proc_info.hProcess = child_process_handle;
2630 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2631 job->jv_job_object = jo;
2632 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002633 sprintf(buf, "winpty://%lu",
2634 GetProcessId(winpty_agent_process(term->tl_winpty)));
2635 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002636 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002637 term->tl_job = job;
2638
2639 return OK;
2640
2641failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002642 if (spawn_config != NULL)
2643 winpty_spawn_config_free(spawn_config);
2644 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002645 if (channel != NULL)
2646 channel_clear(channel);
2647 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002648 {
2649 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002650 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002651 }
2652 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002653 if (jo != NULL)
2654 CloseHandle(jo);
2655 if (term->tl_winpty != NULL)
2656 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002657 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002658 if (term->tl_winpty_config != NULL)
2659 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002660 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002661 if (winpty_err != NULL)
2662 {
2663 char_u *msg = utf16_to_enc(
2664 (short_u *)winpty_error_msg(winpty_err), NULL);
2665
2666 EMSG(msg);
2667 winpty_error_free(winpty_err);
2668 }
2669 return FAIL;
2670}
2671
2672/*
2673 * Free the terminal emulator part of "term".
2674 */
2675 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002676term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002677{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002678 if (term->tl_winpty != NULL)
2679 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002680 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002681 if (term->tl_winpty_config != NULL)
2682 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002683 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002684 if (term->tl_vterm != NULL)
2685 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002686 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002687}
2688
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002689/*
2690 * Request size to terminal.
2691 */
2692 static void
2693term_report_winsize(term_T *term, int rows, int cols)
2694{
2695 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2696}
2697
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002698# else
2699
2700/**************************************
2701 * 3. Unix-like implementation.
2702 */
2703
2704/*
2705 * Create a new terminal of "rows" by "cols" cells.
2706 * Start job for "cmd".
2707 * Store the pointers in "term".
2708 * Return OK or FAIL.
2709 */
2710 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002711term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002712{
2713 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002714
2715 create_vterm(term, rows, cols);
2716
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002717 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002718 argvars[0].v_type = VAR_STRING;
2719 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002720
2721 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002722 if (term->tl_job != NULL)
2723 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002724
Bram Moolenaar61a66052017-07-22 18:39:00 +02002725 return term->tl_job != NULL
2726 && term->tl_job->jv_channel != NULL
2727 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002728}
2729
2730/*
2731 * Free the terminal emulator part of "term".
2732 */
2733 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002734term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002735{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002736 if (term->tl_vterm != NULL)
2737 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002738 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002739}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002740
2741/*
2742 * Request size to terminal.
2743 */
2744 static void
2745term_report_winsize(term_T *term, int rows, int cols)
2746{
2747 /* Use an ioctl() to report the new window size to the job. */
2748 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2749 {
2750 int fd = -1;
2751 int part;
2752
2753 for (part = PART_OUT; part < PART_COUNT; ++part)
2754 {
2755 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2756 if (isatty(fd))
2757 break;
2758 }
2759 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02002760 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002761 }
2762}
2763
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002764# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002765
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002766#endif /* FEAT_TERMINAL */