blob: b22cc1a39acab3c6853ef586151ef626dbe9baf2 [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;
240 opt->jo_term_rows = rows;
241 opt->jo_term_cols = cols;
242}
243
244 static void
245term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200246{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247 exarg_T split_ea;
248 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250
251 if (check_restricted() || check_secure())
252 return;
253
254 term = (term_T *)alloc_clear(sizeof(term_T));
255 if (term == NULL)
256 return;
257 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200258 term->tl_cursor_visible = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200259 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200260 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200261
262 /* Open a new window or tab. */
263 vim_memset(&split_ea, 0, sizeof(split_ea));
264 split_ea.cmdidx = CMD_new;
265 split_ea.cmd = (char_u *)"new";
266 split_ea.arg = (char_u *)"";
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200267 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
268 {
269 split_ea.line2 = opt->jo_term_rows;
270 split_ea.addr_count = 1;
271 }
272 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
273 {
274 split_ea.line2 = opt->jo_term_cols;
275 split_ea.addr_count = 1;
276 }
277
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200278 ex_splitview(&split_ea);
279 if (curwin == old_curwin)
280 {
281 /* split failed */
282 vim_free(term);
283 return;
284 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200285 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200286 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200287
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200288 /* only one size was taken care of with :new, do the other one */
289 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
290 win_setheight(opt->jo_term_rows);
291 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
292 win_setwidth(opt->jo_term_cols);
293
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200294 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200295 term->tl_next = first_term;
296 first_term = term;
297
Bram Moolenaar293424c2017-07-26 23:11:01 +0200298 if (cmd == NULL || *cmd == NUL)
299 cmd = p_sh;
300
Bram Moolenaar78712a72017-08-05 14:50:12 +0200301 if (opt->jo_term_name != NULL)
302 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
303 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200304 {
305 int i;
306 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200307 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200308
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200309 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200310 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200311 /* Prepend a ! to the command name to avoid the buffer name equals
312 * the executable, otherwise ":w!" would overwrite it. */
313 if (i == 0)
314 vim_snprintf((char *)p, len, "!%s", cmd);
315 else
316 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200317 if (buflist_findname(p) == NULL)
318 {
319 curbuf->b_ffname = p;
320 break;
321 }
322 }
323 }
324 curbuf->b_fname = curbuf->b_ffname;
325
Bram Moolenaareb44a682017-08-03 22:44:55 +0200326 set_string_option_direct((char_u *)"buftype", -1,
327 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
328
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200329 /* Mark the buffer as not modifiable. It can only be made modifiable after
330 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200331 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200332
333 /* Set 'bufhidden' to "hide": allow closing the window. */
334 set_string_option_direct((char_u *)"bufhidden", -1,
335 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200336
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200337 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200338 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200339
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200340 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200341 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200342 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200343 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200344 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200345 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200346 }
347 else
348 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200349 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200350
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200351 /* Wiping out the buffer will also close the window and call
352 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200353 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200354 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200355}
356
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200357/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200358 * ":terminal": open a terminal window and execute a job in it.
359 */
360 void
361ex_terminal(exarg_T *eap)
362{
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200363 jobopt_T opt;
364 char_u *cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200365
366 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200367
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200368 cmd = eap->arg;
369 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
370 {
371 char_u *p;
372
373 cmd += 2;
374 p = skiptowhite(cmd);
375 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
376 opt.jo_term_finish = 'c';
377 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
378 opt.jo_term_finish = 'o';
379 else
380 {
381 if (*p)
382 *p = NUL;
383 EMSG2(_("E181: Invalid attribute: %s"), cmd);
384 return;
385 }
386 cmd = skipwhite(p);
387 }
388
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200389 if (eap->addr_count == 2)
390 {
391 opt.jo_term_rows = eap->line1;
392 opt.jo_term_cols = eap->line2;
393 }
394 else if (eap->addr_count == 1)
395 {
396 if (cmdmod.split & WSP_VERT)
397 opt.jo_term_cols = eap->line2;
398 else
399 opt.jo_term_rows = eap->line2;
400 }
401 /* TODO: get more options from before the command */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200402
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200403 term_start(cmd, &opt);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200404}
405
406/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200407 * Free the scrollback buffer for "term".
408 */
409 static void
410free_scrollback(term_T *term)
411{
412 int i;
413
414 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
415 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
416 ga_clear(&term->tl_scrollback);
417}
418
419/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200420 * Free a terminal and everything it refers to.
421 * Kills the job if there is one.
422 * Called when wiping out a buffer.
423 */
424 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200425free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200426{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200427 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200428 term_T *tp;
429
430 if (term == NULL)
431 return;
432 if (first_term == term)
433 first_term = term->tl_next;
434 else
435 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
436 if (tp->tl_next == term)
437 {
438 tp->tl_next = term->tl_next;
439 break;
440 }
441
442 if (term->tl_job != NULL)
443 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200444 if (term->tl_job->jv_status != JOB_ENDED
445 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200446 job_stop(term->tl_job, NULL, "kill");
447 job_unref(term->tl_job);
448 }
449
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200450 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200451
452 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200453 vim_free(term->tl_title);
454 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200455 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200456 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200457}
458
459/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200460 * Write job output "msg[len]" to the vterm.
461 */
462 static void
463term_write_job_output(term_T *term, char_u *msg, size_t len)
464{
465 VTerm *vterm = term->tl_vterm;
466 char_u *p;
467 size_t done;
468 size_t len_now;
469
470 for (done = 0; done < len; done += len_now)
471 {
472 for (p = msg + done; p < msg + len; )
473 {
474 if (*p == NL)
475 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200476 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200477 }
478 len_now = p - msg - done;
479 vterm_input_write(vterm, (char *)msg + done, len_now);
480 if (p < msg + len && *p == NL)
481 {
482 /* Convert NL to CR-NL, that appears to work best. */
483 vterm_input_write(vterm, "\r\n", 2);
484 ++len_now;
485 }
486 }
487
488 /* this invokes the damage callbacks */
489 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
490}
491
Bram Moolenaar1c844932017-07-24 23:36:41 +0200492 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200493update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200494{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200495 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200496 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200497 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200498 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200499 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200500 if (term->tl_cursor_visible)
501 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200502 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200503#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200504 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200505 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200506#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200507 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200508}
509
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200510/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200511 * Invoked when "msg" output from a job was received. Write it to the terminal
512 * of "buffer".
513 */
514 void
515write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
516{
517 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200518 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200519
Bram Moolenaard85f2712017-07-28 21:51:57 +0200520 if (term->tl_vterm == NULL)
521 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200522 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200523 return;
524 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200525 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200526 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200527
Bram Moolenaar6d819742017-08-06 14:57:49 +0200528 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200529 {
530 /* TODO: only update once in a while. */
531 update_screen(0);
532 update_cursor(term, TRUE);
533 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200534}
535
536/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200537 * Send a mouse position and click to the vterm
538 */
539 static int
540term_send_mouse(VTerm *vterm, int button, int pressed)
541{
542 VTermModifier mod = VTERM_MOD_NONE;
543
544 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
545 mouse_col - W_WINCOL(curwin), mod);
546 vterm_mouse_button(vterm, button, pressed, mod);
547 return TRUE;
548}
549
550/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200551 * Convert typed key "c" into bytes to send to the job.
552 * Return the number of bytes in "buf".
553 */
554 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200555term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200556{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200557 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200558 VTermKey key = VTERM_KEY_NONE;
559 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200560 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200561
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200562 switch (c)
563 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200564 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200565 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200566 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
567 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200568 case K_DEL: key = VTERM_KEY_DEL; break;
569 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200570 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
571 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200572 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200573 case K_S_END: mod = VTERM_MOD_SHIFT;
574 key = VTERM_KEY_END; break;
575 case K_C_END: mod = VTERM_MOD_CTRL;
576 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200577 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
578 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
579 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
580 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
581 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
582 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
583 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
584 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
585 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
586 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
587 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
588 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
589 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200590 case K_S_HOME: mod = VTERM_MOD_SHIFT;
591 key = VTERM_KEY_HOME; break;
592 case K_C_HOME: mod = VTERM_MOD_CTRL;
593 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200594 case K_INS: key = VTERM_KEY_INS; break;
595 case K_K0: key = VTERM_KEY_KP_0; break;
596 case K_K1: key = VTERM_KEY_KP_1; break;
597 case K_K2: key = VTERM_KEY_KP_2; break;
598 case K_K3: key = VTERM_KEY_KP_3; break;
599 case K_K4: key = VTERM_KEY_KP_4; break;
600 case K_K5: key = VTERM_KEY_KP_5; break;
601 case K_K6: key = VTERM_KEY_KP_6; break;
602 case K_K7: key = VTERM_KEY_KP_7; break;
603 case K_K8: key = VTERM_KEY_KP_8; break;
604 case K_K9: key = VTERM_KEY_KP_9; break;
605 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
606 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
607 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
608 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
609 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
610 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
611 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
612 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
613 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
614 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
615 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
616 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
617 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200618 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
619 key = VTERM_KEY_LEFT; break;
620 case K_C_LEFT: mod = VTERM_MOD_CTRL;
621 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200622 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
623 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
624 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200625 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
626 key = VTERM_KEY_RIGHT; break;
627 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
628 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200629 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200630 case K_S_UP: mod = VTERM_MOD_SHIFT;
631 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200632 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200633
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200634 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
635 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
636 case K_MOUSELEFT: /* TODO */ return 0;
637 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200638
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200639 case K_LEFTMOUSE:
640 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
641 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
642 case K_LEFTRELEASE:
643 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
644 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
645 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
646 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
647 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
648 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
649 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
650 case K_X1MOUSE: /* TODO */ return 0;
651 case K_X1DRAG: /* TODO */ return 0;
652 case K_X1RELEASE: /* TODO */ return 0;
653 case K_X2MOUSE: /* TODO */ return 0;
654 case K_X2DRAG: /* TODO */ return 0;
655 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200656
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200657 case K_IGNORE: return 0;
658 case K_NOP: return 0;
659 case K_UNDO: return 0;
660 case K_HELP: return 0;
661 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
662 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
663 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
664 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
665 case K_SELECT: return 0;
666#ifdef FEAT_GUI
667 case K_VER_SCROLLBAR: return 0;
668 case K_HOR_SCROLLBAR: return 0;
669#endif
670#ifdef FEAT_GUI_TABLINE
671 case K_TABLINE: return 0;
672 case K_TABMENU: return 0;
673#endif
674#ifdef FEAT_NETBEANS_INTG
675 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
676#endif
677#ifdef FEAT_DND
678 case K_DROP: return 0;
679#endif
680#ifdef FEAT_AUTOCMD
681 case K_CURSORHOLD: return 0;
682#endif
683 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
684 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200685 }
686
687 /*
688 * Convert special keys to vterm keys:
689 * - Write keys to vterm: vterm_keyboard_key()
690 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200691 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200692 */
693 if (key != VTERM_KEY_NONE)
694 /* Special key, let vterm convert it. */
695 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200696 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200697 /* Normal character, let vterm convert it. */
698 vterm_keyboard_unichar(vterm, c, mod);
699
700 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200701 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200702}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200703
Bram Moolenaar938783d2017-07-16 20:13:26 +0200704/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200705 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200706 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200707 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200708term_job_running(term_T *term)
709{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200710 /* Also consider the job finished when the channel is closed, to avoid a
711 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200712 return term != NULL
713 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200714 && term->tl_job->jv_status == JOB_STARTED
715 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200716}
717
718/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200719 * Add the last line of the scrollback buffer to the buffer in the window.
720 */
721 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200722add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200723{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200724 buf_T *buf = term->tl_buffer;
725 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
726 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200727
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200728 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200729 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200730 {
731 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200732 curbuf = buf;
733 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200734 curbuf = curwin->w_buffer;
735 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200736}
737
738/*
739 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200740 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200741 */
742 static void
743move_terminal_to_buffer(term_T *term)
744{
745 win_T *wp;
746 int len;
747 int lines_skipped = 0;
748 VTermPos pos;
749 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200750 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200751 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200752
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200753 if (term->tl_vterm == NULL)
754 return;
755 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200756 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
757 {
758 len = 0;
759 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
760 if (vterm_screen_get_cell(screen, pos, &cell) != 0
761 && cell.chars[0] != NUL)
762 len = pos.col + 1;
763
764 if (len == 0)
765 ++lines_skipped;
766 else
767 {
768 while (lines_skipped > 0)
769 {
770 /* Line was skipped, add an empty line. */
771 --lines_skipped;
772 if (ga_grow(&term->tl_scrollback, 1) == OK)
773 {
774 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
775 + term->tl_scrollback.ga_len;
776
777 line->sb_cols = 0;
778 line->sb_cells = NULL;
779 ++term->tl_scrollback.ga_len;
780
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200781 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200782 }
783 }
784
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200785 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200786 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
787 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200788 garray_T ga;
789 int width;
790 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200791 + term->tl_scrollback.ga_len;
792
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200793 ga_init2(&ga, 1, 100);
794 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200795 {
796 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200797 {
798 width = 1;
799 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
800 if (ga_grow(&ga, 1) == OK)
801 ga.ga_len += mb_char2bytes(' ',
802 (char_u *)ga.ga_data + ga.ga_len);
803 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200804 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200805 {
806 width = cell.width;
807
808 p[pos.col].width = cell.width;
809 p[pos.col].attrs = cell.attrs;
810 p[pos.col].fg = cell.fg;
811 p[pos.col].bg = cell.bg;
812
813 if (ga_grow(&ga, MB_MAXBYTES) == OK)
814 {
815 int i;
816 int c;
817
818 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
819 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
820 (char_u *)ga.ga_data + ga.ga_len);
821 }
822 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200823 }
824 line->sb_cols = len;
825 line->sb_cells = p;
826 ++term->tl_scrollback.ga_len;
827
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200828 if (ga_grow(&ga, 1) == FAIL)
829 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
830 else
831 {
832 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
833 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
834 }
835 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200836 }
837 else
838 vim_free(p);
839 }
840 }
841
842 FOR_ALL_WINDOWS(wp)
843 {
844 if (wp->w_buffer == term->tl_buffer)
845 {
846 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
847 wp->w_cursor.col = 0;
848 wp->w_valid = 0;
849 redraw_win_later(wp, NOT_VALID);
850 }
851 }
852}
853
854 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200855set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200856{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200857 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200858 vim_free(term->tl_status_text);
859 term->tl_status_text = NULL;
860 if (term->tl_buffer == curbuf)
861 maketitle();
862}
863
864/*
865 * Called after the job if finished and Terminal mode is not active:
866 * Move the vterm contents into the scrollback buffer and free the vterm.
867 */
868 static void
869cleanup_vterm(term_T *term)
870{
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200871 if (term->tl_finish == 0)
872 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200873 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200874 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200875}
876
877/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200878 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200879 * Suspends updating the terminal window.
880 */
881 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200882term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200883{
884 term_T *term = curbuf->b_term;
885
886 /* Append the current terminal contents to the buffer. */
887 move_terminal_to_buffer(term);
888
Bram Moolenaar6d819742017-08-06 14:57:49 +0200889 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200890
Bram Moolenaar6d819742017-08-06 14:57:49 +0200891 /* Move the window cursor to the position of the cursor in the
892 * terminal. */
893 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
894 + term->tl_cursor_pos.row + 1;
895 check_cursor();
896 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200897
Bram Moolenaar6d819742017-08-06 14:57:49 +0200898 /* Display the same lines as in the terminal. */
899 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200900}
901
902/*
903 * Returns TRUE if the current window contains a terminal and we are in
904 * Terminal-Normal mode.
905 */
906 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200907term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200908{
909 term_T *term = curbuf->b_term;
910
Bram Moolenaar6d819742017-08-06 14:57:49 +0200911 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200912}
913
914/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200915 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200916 * Restores updating the terminal window.
917 */
918 void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200919term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +0200920{
921 term_T *term = curbuf->b_term;
922 sb_line_T *line;
923 garray_T *gap;
924
925 /* Remove the terminal contents from the scrollback and the buffer. */
926 gap = &term->tl_scrollback;
927 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
928 {
929 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
930 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
931 vim_free(line->sb_cells);
932 --gap->ga_len;
933 if (gap->ga_len == 0)
934 break;
935 }
936 check_cursor();
937
Bram Moolenaar6d819742017-08-06 14:57:49 +0200938 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200939
940 if (term->tl_channel_closed)
941 cleanup_vterm(term);
942 redraw_buf_and_status_later(curbuf, NOT_VALID);
943}
944
945/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200946 * Get a key from the user without mapping.
947 * TODO: use terminal mode mappings.
948 */
949 static int
950term_vgetc()
951{
952 int c;
953
954 ++no_mapping;
955 ++allow_keys;
956 got_int = FALSE;
957 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200958 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200959 --no_mapping;
960 --allow_keys;
961 return c;
962}
963
964/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200965 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200966 * Return FAIL when the key needs to be handled in Normal mode.
967 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200968 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200969 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200970send_keys_to_term(term_T *term, int c, int typed)
971{
972 char msg[KEY_BUF_LEN];
973 size_t len;
974 static int mouse_was_outside = FALSE;
975 int dragging_outside = FALSE;
976
977 /* Catch keys that need to be handled as in Normal mode. */
978 switch (c)
979 {
980 case NUL:
981 case K_ZERO:
982 if (typed)
983 stuffcharReadbuff(c);
984 return FAIL;
985
986 case K_IGNORE:
987 return FAIL;
988
989 case K_LEFTDRAG:
990 case K_MIDDLEDRAG:
991 case K_RIGHTDRAG:
992 case K_X1DRAG:
993 case K_X2DRAG:
994 dragging_outside = mouse_was_outside;
995 /* FALLTHROUGH */
996 case K_LEFTMOUSE:
997 case K_LEFTMOUSE_NM:
998 case K_LEFTRELEASE:
999 case K_LEFTRELEASE_NM:
1000 case K_MIDDLEMOUSE:
1001 case K_MIDDLERELEASE:
1002 case K_RIGHTMOUSE:
1003 case K_RIGHTRELEASE:
1004 case K_X1MOUSE:
1005 case K_X1RELEASE:
1006 case K_X2MOUSE:
1007 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001008
1009 case K_MOUSEUP:
1010 case K_MOUSEDOWN:
1011 case K_MOUSELEFT:
1012 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001013 if (mouse_row < W_WINROW(curwin)
1014 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1015 || mouse_col < W_WINCOL(curwin)
1016 || mouse_col >= W_ENDCOL(curwin)
1017 || dragging_outside)
1018 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001019 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001020 if (typed)
1021 {
1022 stuffcharReadbuff(c);
1023 mouse_was_outside = TRUE;
1024 }
1025 return FAIL;
1026 }
1027 }
1028 if (typed)
1029 mouse_was_outside = FALSE;
1030
1031 /* Convert the typed key to a sequence of bytes for the job. */
1032 len = term_convert_key(term, c, msg);
1033 if (len > 0)
1034 /* TODO: if FAIL is returned, stop? */
1035 channel_send(term->tl_job->jv_channel, PART_IN,
1036 (char_u *)msg, (int)len, NULL);
1037
1038 return OK;
1039}
1040
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001041 static void
1042position_cursor(win_T *wp, VTermPos *pos)
1043{
1044 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1045 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1046 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1047}
1048
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001049/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001050 * Handle CTRL-W "": send register contents to the job.
1051 */
1052 static void
1053term_paste_register(int prev_c UNUSED)
1054{
1055 int c;
1056 list_T *l;
1057 listitem_T *item;
1058 long reglen = 0;
1059 int type;
1060
1061#ifdef FEAT_CMDL_INFO
1062 if (add_to_showcmd(prev_c))
1063 if (add_to_showcmd('"'))
1064 out_flush();
1065#endif
1066 c = term_vgetc();
1067#ifdef FEAT_CMDL_INFO
1068 clear_showcmd();
1069#endif
1070
1071 /* CTRL-W "= prompt for expression to evaluate. */
1072 if (c == '=' && get_expr_register() != '=')
1073 return;
1074
1075 l = (list_T *)get_reg_contents(c, GREG_LIST);
1076 if (l != NULL)
1077 {
1078 type = get_reg_type(c, &reglen);
1079 for (item = l->lv_first; item != NULL; item = item->li_next)
1080 {
1081 char_u *s = get_tv_string(&item->li_tv);
1082
1083 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1084 s, STRLEN(s), NULL);
1085 if (item->li_next != NULL || type == MLINE)
1086 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1087 (char_u *)"\r", 1, NULL);
1088 }
1089 list_free(l);
1090 }
1091}
1092
1093/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001094 * Returns TRUE if the current window contains a terminal and we are sending
1095 * keys to the job.
1096 */
1097 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001098term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001099{
1100 term_T *term = curbuf->b_term;
1101
1102 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001103 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001104 && term->tl_vterm != NULL
1105 && term_job_running(term);
1106}
1107
1108/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001109 * Wait for input and send it to the job.
1110 * Return when the start of a CTRL-W command is typed or anything else that
1111 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001112 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1113 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001114 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001115 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001116terminal_loop(void)
1117{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001118 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001119 int termkey = 0;
1120
1121 if (*curwin->w_p_tk != NUL)
1122 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001123 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001124
1125 for (;;)
1126 {
1127 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001128 /* Repeat redrawing in case a message is received while redrawing. */
1129 while (curwin->w_redr_type != 0)
1130 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001131 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001132
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001133 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001134 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001135 /* job finished while waiting for a character */
1136 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001137
Bram Moolenaarfae42832017-08-01 22:24:26 +02001138#ifdef UNIX
1139 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1140#endif
1141#ifdef WIN3264
1142 if (c == Ctrl_C)
1143 /* We don't know if the job can handle CTRL-C itself or not, this
1144 * may kill the shell instead of killing the command running in the
1145 * shell. */
Bram Moolenaar2d33e902017-08-11 16:31:54 +02001146 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001147#endif
1148
Bram Moolenaar69198192017-08-05 14:10:48 +02001149 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001150 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001151 int prev_c = c;
1152
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001153#ifdef FEAT_CMDL_INFO
1154 if (add_to_showcmd(c))
1155 out_flush();
1156#endif
1157 c = term_vgetc();
1158#ifdef FEAT_CMDL_INFO
1159 clear_showcmd();
1160#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001161 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001162 /* job finished while waiting for a character */
1163 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001164
Bram Moolenaar69198192017-08-05 14:10:48 +02001165 if (prev_c == Ctrl_BSL)
1166 {
1167 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001168 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001169 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1170 term_enter_normal_mode();
1171 return FAIL;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001172 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001173 /* Send both keys to the terminal. */
1174 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1175 }
1176 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001177 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001178 /* "CTRL-W .": send CTRL-W to the job */
1179 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001180 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001181 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001182 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001183 /* CTRL-W N : go to Terminal-Normal mode. */
1184 term_enter_normal_mode();
Bram Moolenaar423802d2017-07-30 16:52:24 +02001185 return FAIL;
1186 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001187 else if (c == '"')
1188 {
1189 term_paste_register(prev_c);
1190 continue;
1191 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001192 else if (termkey == 0 || c != termkey)
1193 {
1194 stuffcharReadbuff(Ctrl_W);
1195 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001196 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001197 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001198 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001199 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1200 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001201 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001202 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001203}
1204
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001205/*
1206 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001207 * This updates the title and status, but does not close the vter, because
1208 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001209 */
1210 void
1211term_job_ended(job_T *job)
1212{
1213 term_T *term;
1214 int did_one = FALSE;
1215
1216 for (term = first_term; term != NULL; term = term->tl_next)
1217 if (term->tl_job == job)
1218 {
1219 vim_free(term->tl_title);
1220 term->tl_title = NULL;
1221 vim_free(term->tl_status_text);
1222 term->tl_status_text = NULL;
1223 redraw_buf_and_status_later(term->tl_buffer, VALID);
1224 did_one = TRUE;
1225 }
1226 if (did_one)
1227 redraw_statuslines();
1228 if (curbuf->b_term != NULL)
1229 {
1230 if (curbuf->b_term->tl_job == job)
1231 maketitle();
1232 update_cursor(curbuf->b_term, TRUE);
1233 }
1234}
1235
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001236 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001237may_toggle_cursor(term_T *term)
1238{
1239 if (curbuf == term->tl_buffer)
1240 {
1241 if (term->tl_cursor_visible)
1242 cursor_on();
1243 else
1244 cursor_off();
1245 }
1246}
1247
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001248 static int
1249handle_damage(VTermRect rect, void *user)
1250{
1251 term_T *term = (term_T *)user;
1252
1253 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1254 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1255 redraw_buf_later(term->tl_buffer, NOT_VALID);
1256 return 1;
1257}
1258
1259 static int
1260handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1261{
1262 term_T *term = (term_T *)user;
1263
1264 /* TODO */
1265 redraw_buf_later(term->tl_buffer, NOT_VALID);
1266 return 1;
1267}
1268
1269 static int
1270handle_movecursor(
1271 VTermPos pos,
1272 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001273 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001274 void *user)
1275{
1276 term_T *term = (term_T *)user;
1277 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001278
1279 term->tl_cursor_pos = pos;
1280 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001281
1282 FOR_ALL_WINDOWS(wp)
1283 {
1284 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001285 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001286 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001287 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001288 {
1289 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001290 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001291 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001292
1293 return 1;
1294}
1295
Bram Moolenaar21554412017-07-24 21:44:43 +02001296 static int
1297handle_settermprop(
1298 VTermProp prop,
1299 VTermValue *value,
1300 void *user)
1301{
1302 term_T *term = (term_T *)user;
1303
1304 switch (prop)
1305 {
1306 case VTERM_PROP_TITLE:
1307 vim_free(term->tl_title);
1308 term->tl_title = vim_strsave((char_u *)value->string);
1309 vim_free(term->tl_status_text);
1310 term->tl_status_text = NULL;
1311 if (term == curbuf->b_term)
1312 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001313 break;
1314
1315 case VTERM_PROP_CURSORVISIBLE:
1316 term->tl_cursor_visible = value->boolean;
1317 may_toggle_cursor(term);
1318 out_flush();
1319 break;
1320
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001321 case VTERM_PROP_ALTSCREEN:
1322 /* TODO: do anything else? */
1323 term->tl_using_altscreen = value->boolean;
1324 break;
1325
Bram Moolenaar21554412017-07-24 21:44:43 +02001326 default:
1327 break;
1328 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001329 /* Always return 1, otherwise vterm doesn't store the value internally. */
1330 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001331}
1332
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001333/*
1334 * The job running in the terminal resized the terminal.
1335 */
1336 static int
1337handle_resize(int rows, int cols, void *user)
1338{
1339 term_T *term = (term_T *)user;
1340 win_T *wp;
1341
1342 term->tl_rows = rows;
1343 term->tl_cols = cols;
1344 FOR_ALL_WINDOWS(wp)
1345 {
1346 if (wp->w_buffer == term->tl_buffer)
1347 {
1348 win_setheight_win(rows, wp);
1349 win_setwidth_win(cols, wp);
1350 }
1351 }
1352
1353 redraw_buf_later(term->tl_buffer, NOT_VALID);
1354 return 1;
1355}
1356
Bram Moolenaard85f2712017-07-28 21:51:57 +02001357/*
1358 * Handle a line that is pushed off the top of the screen.
1359 */
1360 static int
1361handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1362{
1363 term_T *term = (term_T *)user;
1364
1365 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001366 if (ga_grow(&term->tl_scrollback, 1) == OK)
1367 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001368 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001369 int len = 0;
1370 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001371 int c;
1372 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001373 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001374 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001375
1376 /* do not store empty cells at the end */
1377 for (i = 0; i < cols; ++i)
1378 if (cells[i].chars[0] != 0)
1379 len = i + 1;
1380
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001381 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001382 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001383 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001384 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001385 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001386 for (col = 0; col < len; col += cells[col].width)
1387 {
1388 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1389 {
1390 ga.ga_len = 0;
1391 break;
1392 }
1393 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1394 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1395 (char_u *)ga.ga_data + ga.ga_len);
1396 p[col].width = cells[col].width;
1397 p[col].attrs = cells[col].attrs;
1398 p[col].fg = cells[col].fg;
1399 p[col].bg = cells[col].bg;
1400 }
1401 }
1402 if (ga_grow(&ga, 1) == FAIL)
1403 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1404 else
1405 {
1406 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1407 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1408 }
1409 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001410
1411 line = (sb_line_T *)term->tl_scrollback.ga_data
1412 + term->tl_scrollback.ga_len;
1413 line->sb_cols = len;
1414 line->sb_cells = p;
1415 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001416 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001417 }
1418 return 0; /* ignored */
1419}
1420
Bram Moolenaar21554412017-07-24 21:44:43 +02001421static VTermScreenCallbacks screen_callbacks = {
1422 handle_damage, /* damage */
1423 handle_moverect, /* moverect */
1424 handle_movecursor, /* movecursor */
1425 handle_settermprop, /* settermprop */
1426 NULL, /* bell */
1427 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001428 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001429 NULL /* sb_popline */
1430};
1431
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001432/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001433 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001434 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001435 */
1436 void
1437term_channel_closed(channel_T *ch)
1438{
1439 term_T *term;
1440 int did_one = FALSE;
1441
1442 for (term = first_term; term != NULL; term = term->tl_next)
1443 if (term->tl_job == ch->ch_job)
1444 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001445 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001446 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001447
Bram Moolenaard85f2712017-07-28 21:51:57 +02001448 vim_free(term->tl_title);
1449 term->tl_title = NULL;
1450 vim_free(term->tl_status_text);
1451 term->tl_status_text = NULL;
1452
Bram Moolenaar423802d2017-07-30 16:52:24 +02001453 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001454 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001455 {
1456 int fnum = term->tl_buffer->b_fnum;
1457
Bram Moolenaar423802d2017-07-30 16:52:24 +02001458 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001459
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001460 if (term->tl_finish == 'c')
1461 {
1462 /* ++close or term_finish == "close" */
1463 curbuf = term->tl_buffer;
1464 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1465 break;
1466 }
1467 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1468 {
1469 char buf[50];
1470
1471 /* TODO: use term_opencmd */
1472 vim_snprintf(buf, sizeof(buf), "botright sbuf %d", fnum);
1473 do_cmdline_cmd((char_u *)buf);
1474 }
1475 }
1476
Bram Moolenaard85f2712017-07-28 21:51:57 +02001477 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001478 }
1479 if (did_one)
1480 {
1481 redraw_statuslines();
1482
1483 /* Need to break out of vgetc(). */
1484 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001485 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001486
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001487 term = curbuf->b_term;
1488 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001489 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001490 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001491 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001492 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001493 }
1494 }
1495}
1496
1497/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001498 * Reverse engineer the RGB value into a cterm color index.
1499 * First color is 1. Return 0 if no match found.
1500 */
1501 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001502color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001503{
1504 int red = color->red;
1505 int blue = color->blue;
1506 int green = color->green;
1507
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001508 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001509 if (red == 0)
1510 {
1511 if (green == 0)
1512 {
1513 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001514 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001515 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001516 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001517 }
1518 else if (green == 224)
1519 {
1520 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001521 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001522 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001523 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001524 }
1525 }
1526 else if (red == 224)
1527 {
1528 if (green == 0)
1529 {
1530 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001531 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001532 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001533 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001534 }
1535 else if (green == 224)
1536 {
1537 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001538 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001539 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001540 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001541 }
1542 }
1543 else if (red == 128)
1544 {
1545 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001546 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001547 }
1548 else if (red == 255)
1549 {
1550 if (green == 64)
1551 {
1552 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001553 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001554 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001555 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001556 }
1557 else if (green == 255)
1558 {
1559 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001560 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001561 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001562 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001563 }
1564 }
1565 else if (red == 64)
1566 {
1567 if (green == 64)
1568 {
1569 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001570 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001571 }
1572 else if (green == 255)
1573 {
1574 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001575 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001576 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001577 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001578 }
1579 }
1580 if (t_colors >= 256)
1581 {
1582 if (red == blue && red == green)
1583 {
1584 /* 24-color greyscale */
1585 static int cutoff[23] = {
1586 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1587 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1588 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1589 int i;
1590
1591 for (i = 0; i < 23; ++i)
1592 if (red < cutoff[i])
1593 return i + 233;
1594 return 256;
1595 }
1596
1597 /* 216-color cube */
1598 return 17 + ((red + 25) / 0x33) * 36
1599 + ((green + 25) / 0x33) * 6
1600 + (blue + 25) / 0x33;
1601 }
1602 return 0;
1603}
1604
1605/*
1606 * Convert the attributes of a vterm cell into an attribute index.
1607 */
1608 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001609cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001610{
1611 int attr = 0;
1612
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001613 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001614 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001615 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001616 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001617 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001618 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001619 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001620 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001621 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001622 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001623
1624#ifdef FEAT_GUI
1625 if (gui.in_use)
1626 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001627 guicolor_T fg, bg;
1628
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001629 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1630 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001631 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001632 }
1633 else
1634#endif
1635#ifdef FEAT_TERMGUICOLORS
1636 if (p_tgc)
1637 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001638 guicolor_T fg, bg;
1639
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001640 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1641 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001642
1643 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001644 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001645 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001646#endif
1647 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001648 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001649 int fg = color2index(&cellfg, TRUE, &bold);
1650 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001651
1652 /* with 8 colors set the bold attribute to get a bright foreground */
1653 if (bold == TRUE)
1654 attr |= HL_BOLD;
1655 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001656 }
1657 return 0;
1658}
1659
1660/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001661 * Called to update a window that contains an active terminal.
1662 * Returns FAIL when there is no terminal running in this window or in
1663 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001664 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001665 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001666term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001667{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001668 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001669 VTerm *vterm;
1670 VTermScreen *screen;
1671 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001672 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001673
Bram Moolenaar6d819742017-08-06 14:57:49 +02001674 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001675 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001676
Bram Moolenaard85f2712017-07-28 21:51:57 +02001677 vterm = term->tl_vterm;
1678 screen = vterm_obtain_screen(vterm);
1679 state = vterm_obtain_state(vterm);
1680
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001681 /*
1682 * If the window was resized a redraw will be triggered and we get here.
1683 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1684 */
1685 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1686 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001687 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001688 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1689 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1690 win_T *twp;
1691
1692 FOR_ALL_WINDOWS(twp)
1693 {
1694 /* When more than one window shows the same terminal, use the
1695 * smallest size. */
1696 if (twp->w_buffer == term->tl_buffer)
1697 {
1698 if (!term->tl_rows_fixed && rows > twp->w_height)
1699 rows = twp->w_height;
1700 if (!term->tl_cols_fixed && cols > twp->w_width)
1701 cols = twp->w_width;
1702 }
1703 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001704
1705 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001706 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001707 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001708 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001709 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001710
1711 /* The cursor may have been moved when resizing. */
1712 vterm_state_get_cursorpos(state, &pos);
1713 position_cursor(wp, &pos);
1714
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001715 /* TODO: Only redraw what changed. */
1716 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001717 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001718 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001719 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001720
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001721 if (pos.row < term->tl_rows)
1722 {
1723 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001724 {
1725 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001726 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001727
Bram Moolenaareeac6772017-07-23 15:48:37 +02001728 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1729 vim_memset(&cell, 0, sizeof(cell));
1730
1731 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001732 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001733 if (c == NUL)
1734 {
1735 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001736#if defined(FEAT_MBYTE)
1737 if (enc_utf8)
1738 ScreenLinesUC[off] = NUL;
1739#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001740 }
1741 else
1742 {
1743#if defined(FEAT_MBYTE)
1744 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001745 {
1746 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001747 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001748 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001749 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001750 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001751 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001752 if (enc_utf8)
1753 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001754 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001755#else
1756 ScreenLines[off] = c;
1757#endif
1758 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001759 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001760
1761 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001762 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001763 if (cell.width == 2)
1764 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001765 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001766#if defined(FEAT_MBYTE)
1767 if (enc_utf8)
1768 ScreenLinesUC[off] = NUL;
1769#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001770 ++pos.col;
1771 ++off;
1772 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001773 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001774 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001775 else
1776 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001777
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001778 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1779 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001780 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001781
1782 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001783}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001784
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001785/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001786 * Return TRUE if "wp" is a terminal window where the job has finished.
1787 */
1788 int
1789term_is_finished(buf_T *buf)
1790{
1791 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1792}
1793
1794/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001795 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02001796 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001797 */
1798 int
1799term_show_buffer(buf_T *buf)
1800{
1801 term_T *term = buf->b_term;
1802
Bram Moolenaar6d819742017-08-06 14:57:49 +02001803 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001804}
1805
1806/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001807 * The current buffer is going to be changed. If there is terminal
1808 * highlighting remove it now.
1809 */
1810 void
1811term_change_in_curbuf(void)
1812{
1813 term_T *term = curbuf->b_term;
1814
1815 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1816 {
1817 free_scrollback(term);
1818 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001819
1820 /* The buffer is now like a normal buffer, it cannot be easily
1821 * abandoned when changed. */
1822 set_string_option_direct((char_u *)"buftype", -1,
1823 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001824 }
1825}
1826
1827/*
1828 * Get the screen attribute for a position in the buffer.
1829 */
1830 int
1831term_get_attr(buf_T *buf, linenr_T lnum, int col)
1832{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001833 term_T *term = buf->b_term;
1834 sb_line_T *line;
1835 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001836
Bram Moolenaar70229f92017-07-29 16:01:53 +02001837 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001838 return 0;
1839 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1840 if (col >= line->sb_cols)
1841 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001842 cellattr = line->sb_cells + col;
1843 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001844}
1845
1846/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001847 * Create a new vterm and initialize it.
1848 */
1849 static void
1850create_vterm(term_T *term, int rows, int cols)
1851{
1852 VTerm *vterm;
1853 VTermScreen *screen;
1854
1855 vterm = vterm_new(rows, cols);
1856 term->tl_vterm = vterm;
1857 screen = vterm_obtain_screen(vterm);
1858 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1859 /* TODO: depends on 'encoding'. */
1860 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001861
1862 /* Vterm uses a default black background. Set it to white when
1863 * 'background' is "light". */
1864 if (*p_bg == 'l')
1865 {
1866 VTermColor fg, bg;
1867
1868 fg.red = fg.green = fg.blue = 0;
1869 bg.red = bg.green = bg.blue = 255;
1870 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1871 }
1872
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001873 /* Required to initialize most things. */
1874 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001875
1876 /* Allow using alternate screen. */
1877 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001878}
1879
Bram Moolenaar21554412017-07-24 21:44:43 +02001880/*
1881 * Return the text to show for the buffer name and status.
1882 */
1883 char_u *
1884term_get_status_text(term_T *term)
1885{
1886 if (term->tl_status_text == NULL)
1887 {
1888 char_u *txt;
1889 size_t len;
1890
Bram Moolenaar6d819742017-08-06 14:57:49 +02001891 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001892 {
1893 if (term_job_running(term))
1894 txt = (char_u *)_("Terminal");
1895 else
1896 txt = (char_u *)_("Terminal-finished");
1897 }
1898 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001899 txt = term->tl_title;
1900 else if (term_job_running(term))
1901 txt = (char_u *)_("running");
1902 else
1903 txt = (char_u *)_("finished");
1904 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001905 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001906 if (term->tl_status_text != NULL)
1907 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1908 term->tl_buffer->b_fname, txt);
1909 }
1910 return term->tl_status_text;
1911}
1912
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001913/*
1914 * Mark references in jobs of terminals.
1915 */
1916 int
1917set_ref_in_term(int copyID)
1918{
1919 int abort = FALSE;
1920 term_T *term;
1921 typval_T tv;
1922
1923 for (term = first_term; term != NULL; term = term->tl_next)
1924 if (term->tl_job != NULL)
1925 {
1926 tv.v_type = VAR_JOB;
1927 tv.vval.v_job = term->tl_job;
1928 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1929 }
1930 return abort;
1931}
1932
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001933/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001934 * Get the buffer from the first argument in "argvars".
1935 * Returns NULL when the buffer is not for a terminal window.
1936 */
1937 static buf_T *
1938term_get_buf(typval_T *argvars)
1939{
1940 buf_T *buf;
1941
1942 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1943 ++emsg_off;
1944 buf = get_buf_tv(&argvars[0], FALSE);
1945 --emsg_off;
1946 if (buf == NULL || buf->b_term == NULL)
1947 return NULL;
1948 return buf;
1949}
1950
1951/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001952 * "term_getaltscreen(buf)" function
1953 */
1954 void
1955f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
1956{
1957 buf_T *buf = term_get_buf(argvars);
1958
1959 if (buf == NULL)
1960 return;
1961 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
1962}
1963
1964/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001965 * "term_getattr(attr, name)" function
1966 */
1967 void
1968f_term_getattr(typval_T *argvars, typval_T *rettv)
1969{
1970 int attr;
1971 size_t i;
1972 char_u *name;
1973
1974 static struct {
1975 char *name;
1976 int attr;
1977 } attrs[] = {
1978 {"bold", HL_BOLD},
1979 {"italic", HL_ITALIC},
1980 {"underline", HL_UNDERLINE},
1981 {"strike", HL_STANDOUT},
1982 {"reverse", HL_INVERSE},
1983 };
1984
1985 attr = get_tv_number(&argvars[0]);
1986 name = get_tv_string_chk(&argvars[1]);
1987 if (name == NULL)
1988 return;
1989
1990 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1991 if (STRCMP(name, attrs[i].name) == 0)
1992 {
1993 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1994 break;
1995 }
1996}
1997
1998/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001999 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002000 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002001 void
2002f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002003{
Bram Moolenaar97870002017-07-30 18:28:38 +02002004 buf_T *buf = term_get_buf(argvars);
2005 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002006
Bram Moolenaar97870002017-07-30 18:28:38 +02002007 if (rettv_list_alloc(rettv) == FAIL)
2008 return;
2009 if (buf == NULL)
2010 return;
2011
2012 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002013 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
2014 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02002015 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002016}
2017
2018/*
2019 * "term_getjob(buf)" function
2020 */
2021 void
2022f_term_getjob(typval_T *argvars, typval_T *rettv)
2023{
2024 buf_T *buf = term_get_buf(argvars);
2025
2026 rettv->v_type = VAR_JOB;
2027 rettv->vval.v_job = NULL;
2028 if (buf == NULL)
2029 return;
2030
2031 rettv->vval.v_job = buf->b_term->tl_job;
2032 if (rettv->vval.v_job != NULL)
2033 ++rettv->vval.v_job->jv_refcount;
2034}
2035
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002036 static int
2037get_row_number(typval_T *tv, term_T *term)
2038{
2039 if (tv->v_type == VAR_STRING
2040 && tv->vval.v_string != NULL
2041 && STRCMP(tv->vval.v_string, ".") == 0)
2042 return term->tl_cursor_pos.row;
2043 return (int)get_tv_number(tv) - 1;
2044}
2045
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002046/*
2047 * "term_getline(buf, row)" function
2048 */
2049 void
2050f_term_getline(typval_T *argvars, typval_T *rettv)
2051{
2052 buf_T *buf = term_get_buf(argvars);
2053 term_T *term;
2054 int row;
2055
2056 rettv->v_type = VAR_STRING;
2057 if (buf == NULL)
2058 return;
2059 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002060 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002061
2062 if (term->tl_vterm == NULL)
2063 {
2064 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2065
2066 /* vterm is finished, get the text from the buffer */
2067 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2068 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2069 }
2070 else
2071 {
2072 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2073 VTermRect rect;
2074 int len;
2075 char_u *p;
2076
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002077 if (row < 0 || row >= term->tl_rows)
2078 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002079 len = term->tl_cols * MB_MAXBYTES + 1;
2080 p = alloc(len);
2081 if (p == NULL)
2082 return;
2083 rettv->vval.v_string = p;
2084
2085 rect.start_col = 0;
2086 rect.end_col = term->tl_cols;
2087 rect.start_row = row;
2088 rect.end_row = row + 1;
2089 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2090 }
2091}
2092
2093/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002094 * "term_getscrolled(buf)" function
2095 */
2096 void
2097f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2098{
2099 buf_T *buf = term_get_buf(argvars);
2100
2101 if (buf == NULL)
2102 return;
2103 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2104}
2105
2106/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002107 * "term_getsize(buf)" function
2108 */
2109 void
2110f_term_getsize(typval_T *argvars, typval_T *rettv)
2111{
2112 buf_T *buf = term_get_buf(argvars);
2113 list_T *l;
2114
2115 if (rettv_list_alloc(rettv) == FAIL)
2116 return;
2117 if (buf == NULL)
2118 return;
2119
2120 l = rettv->vval.v_list;
2121 list_append_number(l, buf->b_term->tl_rows);
2122 list_append_number(l, buf->b_term->tl_cols);
2123}
2124
2125/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002126 * "term_getstatus(buf)" function
2127 */
2128 void
2129f_term_getstatus(typval_T *argvars, typval_T *rettv)
2130{
2131 buf_T *buf = term_get_buf(argvars);
2132 term_T *term;
2133 char_u val[100];
2134
2135 rettv->v_type = VAR_STRING;
2136 if (buf == NULL)
2137 return;
2138 term = buf->b_term;
2139
2140 if (term_job_running(term))
2141 STRCPY(val, "running");
2142 else
2143 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002144 if (term->tl_normal_mode)
2145 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002146 rettv->vval.v_string = vim_strsave(val);
2147}
2148
2149/*
2150 * "term_gettitle(buf)" function
2151 */
2152 void
2153f_term_gettitle(typval_T *argvars, typval_T *rettv)
2154{
2155 buf_T *buf = term_get_buf(argvars);
2156
2157 rettv->v_type = VAR_STRING;
2158 if (buf == NULL)
2159 return;
2160
2161 if (buf->b_term->tl_title != NULL)
2162 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2163}
2164
2165/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002166 * "term_gettty(buf)" function
2167 */
2168 void
2169f_term_gettty(typval_T *argvars, typval_T *rettv)
2170{
2171 buf_T *buf = term_get_buf(argvars);
2172 char_u *p;
2173
2174 rettv->v_type = VAR_STRING;
2175 if (buf == NULL)
2176 return;
2177 if (buf->b_term->tl_job != NULL)
2178 p = buf->b_term->tl_job->jv_tty_name;
2179 else
2180 p = buf->b_term->tl_tty_name;
2181 if (p != NULL)
2182 rettv->vval.v_string = vim_strsave(p);
2183}
2184
2185/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002186 * "term_list()" function
2187 */
2188 void
2189f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2190{
2191 term_T *tp;
2192 list_T *l;
2193
2194 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2195 return;
2196
2197 l = rettv->vval.v_list;
2198 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2199 if (tp != NULL && tp->tl_buffer != NULL)
2200 if (list_append_number(l,
2201 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2202 return;
2203}
2204
2205/*
2206 * "term_scrape(buf, row)" function
2207 */
2208 void
2209f_term_scrape(typval_T *argvars, typval_T *rettv)
2210{
2211 buf_T *buf = term_get_buf(argvars);
2212 VTermScreen *screen = NULL;
2213 VTermPos pos;
2214 list_T *l;
2215 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002216 char_u *p;
2217 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002218
2219 if (rettv_list_alloc(rettv) == FAIL)
2220 return;
2221 if (buf == NULL)
2222 return;
2223 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002224
2225 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002226 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002227
2228 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002229 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002230 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002231 p = NULL;
2232 line = NULL;
2233 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002234 else
2235 {
2236 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2237
2238 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2239 return;
2240 p = ml_get_buf(buf, lnum + 1, FALSE);
2241 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2242 }
2243
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002244 for (pos.col = 0; pos.col < term->tl_cols; )
2245 {
2246 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002247 int width;
2248 VTermScreenCellAttrs attrs;
2249 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002250 char_u rgb[8];
2251 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2252 int off = 0;
2253 int i;
2254
2255 if (screen == NULL)
2256 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002257 cellattr_T *cellattr;
2258 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002259
2260 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002261 if (pos.col >= line->sb_cols)
2262 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002263 cellattr = line->sb_cells + pos.col;
2264 width = cellattr->width;
2265 attrs = cellattr->attrs;
2266 fg = cellattr->fg;
2267 bg = cellattr->bg;
2268 len = MB_PTR2LEN(p);
2269 mch_memmove(mbs, p, len);
2270 mbs[len] = NUL;
2271 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002272 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002273 else
2274 {
2275 VTermScreenCell cell;
2276 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2277 break;
2278 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2279 {
2280 if (cell.chars[i] == 0)
2281 break;
2282 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2283 }
2284 mbs[off] = NUL;
2285 width = cell.width;
2286 attrs = cell.attrs;
2287 fg = cell.fg;
2288 bg = cell.bg;
2289 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002290 dcell = dict_alloc();
2291 list_append_dict(l, dcell);
2292
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002293 dict_add_nr_str(dcell, "chars", 0, mbs);
2294
2295 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002296 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002297 dict_add_nr_str(dcell, "fg", 0, rgb);
2298 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002299 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002300 dict_add_nr_str(dcell, "bg", 0, rgb);
2301
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002302 dict_add_nr_str(dcell, "attr",
2303 cell2attr(attrs, fg, bg), NULL);
2304 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002305
2306 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002307 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002308 ++pos.col;
2309 }
2310}
2311
2312/*
2313 * "term_sendkeys(buf, keys)" function
2314 */
2315 void
2316f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2317{
2318 buf_T *buf = term_get_buf(argvars);
2319 char_u *msg;
2320 term_T *term;
2321
2322 rettv->v_type = VAR_UNKNOWN;
2323 if (buf == NULL)
2324 return;
2325
2326 msg = get_tv_string_chk(&argvars[1]);
2327 if (msg == NULL)
2328 return;
2329 term = buf->b_term;
2330 if (term->tl_vterm == NULL)
2331 return;
2332
2333 while (*msg != NUL)
2334 {
2335 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2336 msg += MB_PTR2LEN(msg);
2337 }
2338
Bram Moolenaar6d819742017-08-06 14:57:49 +02002339 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002340 {
2341 /* TODO: only update once in a while. */
2342 update_screen(0);
2343 if (buf == curbuf)
2344 update_cursor(term, TRUE);
2345 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002346}
2347
2348/*
2349 * "term_start(command, options)" function
2350 */
2351 void
2352f_term_start(typval_T *argvars, typval_T *rettv)
2353{
2354 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002355 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002356
2357 if (cmd == NULL)
2358 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002359 init_job_options(&opt);
2360 /* TODO: allow more job options */
2361 if (argvars[1].v_type != VAR_UNKNOWN
2362 && get_job_options(&argvars[1], &opt,
2363 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar78712a72017-08-05 14:50:12 +02002364 + JO_EXIT_CB + JO_CLOSE_CALLBACK
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002365 + JO2_TERM_NAME + JO2_TERM_FINISH) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002366 return;
2367
2368 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002369
2370 if (curbuf->b_term != NULL)
2371 rettv->vval.v_number = curbuf->b_fnum;
2372}
2373
2374/*
2375 * "term_wait" function
2376 */
2377 void
2378f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2379{
2380 buf_T *buf = term_get_buf(argvars);
2381
2382 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002383 {
2384 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002385 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002386 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002387 if (buf->b_term->tl_job == NULL)
2388 {
2389 ch_log(NULL, "term_wait(): no job to wait for");
2390 return;
2391 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002392
2393 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002394 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002395 {
2396 /* The job is dead, keep reading channel I/O until the channel is
2397 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002398 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002399 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2400 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002401 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002402 parse_queued_messages();
2403 ui_delay(10L, FALSE);
2404 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002405 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002406 parse_queued_messages();
2407 }
2408 else
2409 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002410 long wait = 10L;
2411
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002412 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002413 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002414
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002415 /* Wait for some time for any channel I/O. */
2416 if (argvars[1].v_type != VAR_UNKNOWN)
2417 wait = get_tv_number(&argvars[1]);
2418 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002419 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002420
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002421 /* Flushing messages on channels is hopefully sufficient.
2422 * TODO: is there a better way? */
2423 parse_queued_messages();
2424 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002425}
2426
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002427# ifdef WIN3264
2428
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002429/**************************************
2430 * 2. MS-Windows implementation.
2431 */
2432
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002433#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2434#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2435
Bram Moolenaar8a773062017-07-24 22:29:21 +02002436void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002437void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002438void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002439BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2440void (*winpty_config_set_initial_size)(void*, int, int);
2441LPCWSTR (*winpty_conin_name)(void*);
2442LPCWSTR (*winpty_conout_name)(void*);
2443LPCWSTR (*winpty_conerr_name)(void*);
2444void (*winpty_free)(void*);
2445void (*winpty_config_free)(void*);
2446void (*winpty_spawn_config_free)(void*);
2447void (*winpty_error_free)(void*);
2448LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002449BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002450HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002451
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002452#define WINPTY_DLL "winpty.dll"
2453
2454static HINSTANCE hWinPtyDLL = NULL;
2455
2456 int
2457dyn_winpty_init(void)
2458{
2459 int i;
2460 static struct
2461 {
2462 char *name;
2463 FARPROC *ptr;
2464 } winpty_entry[] =
2465 {
2466 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2467 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2468 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2469 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2470 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2471 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2472 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2473 {"winpty_free", (FARPROC*)&winpty_free},
2474 {"winpty_open", (FARPROC*)&winpty_open},
2475 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2476 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2477 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2478 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002479 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002480 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002481 {NULL, NULL}
2482 };
2483
2484 /* No need to initialize twice. */
2485 if (hWinPtyDLL)
2486 return 1;
2487 /* Load winpty.dll */
2488 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2489 if (!hWinPtyDLL)
2490 {
2491 EMSG2(_(e_loadlib), WINPTY_DLL);
2492 return 0;
2493 }
2494 for (i = 0; winpty_entry[i].name != NULL
2495 && winpty_entry[i].ptr != NULL; ++i)
2496 {
2497 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2498 winpty_entry[i].name)) == NULL)
2499 {
2500 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2501 return 0;
2502 }
2503 }
2504
2505 return 1;
2506}
2507
2508/*
2509 * Create a new terminal of "rows" by "cols" cells.
2510 * Store a reference in "term".
2511 * Return OK or FAIL.
2512 */
2513 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002514term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002515{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002516 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002517 channel_T *channel = NULL;
2518 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002519 DWORD error;
2520 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2521 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002522 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002523 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002524
2525 if (!dyn_winpty_init())
2526 return FAIL;
2527
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002528 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002529 if (p == NULL)
2530 return FAIL;
2531
2532 job = job_alloc();
2533 if (job == NULL)
2534 goto failed;
2535
2536 channel = add_channel();
2537 if (channel == NULL)
2538 goto failed;
2539
2540 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2541 if (term->tl_winpty_config == NULL)
2542 goto failed;
2543
2544 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2545 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2546 if (term->tl_winpty == NULL)
2547 goto failed;
2548
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002549 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002550 spawn_config = winpty_spawn_config_new(
2551 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2552 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2553 NULL,
2554 p,
2555 NULL,
2556 NULL,
2557 &winpty_err);
2558 if (spawn_config == NULL)
2559 goto failed;
2560
2561 channel = add_channel();
2562 if (channel == NULL)
2563 goto failed;
2564
2565 job = job_alloc();
2566 if (job == NULL)
2567 goto failed;
2568
2569 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2570 &child_thread_handle, &error, &winpty_err))
2571 goto failed;
2572
2573 channel_set_pipes(channel,
2574 (sock_T) CreateFileW(
2575 winpty_conin_name(term->tl_winpty),
2576 GENERIC_WRITE, 0, NULL,
2577 OPEN_EXISTING, 0, NULL),
2578 (sock_T) CreateFileW(
2579 winpty_conout_name(term->tl_winpty),
2580 GENERIC_READ, 0, NULL,
2581 OPEN_EXISTING, 0, NULL),
2582 (sock_T) CreateFileW(
2583 winpty_conerr_name(term->tl_winpty),
2584 GENERIC_READ, 0, NULL,
2585 OPEN_EXISTING, 0, NULL));
2586
2587 jo = CreateJobObject(NULL, NULL);
2588 if (jo == NULL)
2589 goto failed;
2590
2591 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002592 {
2593 /* Failed, switch the way to terminate process with TerminateProcess. */
2594 CloseHandle(jo);
2595 jo = NULL;
2596 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002597
2598 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002599 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002600
2601 create_vterm(term, rows, cols);
2602
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002603 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002604 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002605
2606 job->jv_channel = channel;
2607 job->jv_proc_info.hProcess = child_process_handle;
2608 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2609 job->jv_job_object = jo;
2610 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002611 sprintf(buf, "winpty://%lu",
2612 GetProcessId(winpty_agent_process(term->tl_winpty)));
2613 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002614 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002615 term->tl_job = job;
2616
2617 return OK;
2618
2619failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002620 if (spawn_config != NULL)
2621 winpty_spawn_config_free(spawn_config);
2622 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002623 if (channel != NULL)
2624 channel_clear(channel);
2625 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002626 {
2627 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002628 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002629 }
2630 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002631 if (jo != NULL)
2632 CloseHandle(jo);
2633 if (term->tl_winpty != NULL)
2634 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002635 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002636 if (term->tl_winpty_config != NULL)
2637 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002638 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002639 if (winpty_err != NULL)
2640 {
2641 char_u *msg = utf16_to_enc(
2642 (short_u *)winpty_error_msg(winpty_err), NULL);
2643
2644 EMSG(msg);
2645 winpty_error_free(winpty_err);
2646 }
2647 return FAIL;
2648}
2649
2650/*
2651 * Free the terminal emulator part of "term".
2652 */
2653 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002654term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002655{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002656 if (term->tl_winpty != NULL)
2657 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002658 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002659 if (term->tl_winpty_config != NULL)
2660 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002661 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002662 if (term->tl_vterm != NULL)
2663 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002664 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002665}
2666
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002667/*
2668 * Request size to terminal.
2669 */
2670 static void
2671term_report_winsize(term_T *term, int rows, int cols)
2672{
2673 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2674}
2675
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002676# else
2677
2678/**************************************
2679 * 3. Unix-like implementation.
2680 */
2681
2682/*
2683 * Create a new terminal of "rows" by "cols" cells.
2684 * Start job for "cmd".
2685 * Store the pointers in "term".
2686 * Return OK or FAIL.
2687 */
2688 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002689term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002690{
2691 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002692
2693 create_vterm(term, rows, cols);
2694
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002695 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002696 argvars[0].v_type = VAR_STRING;
2697 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002698
2699 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002700 if (term->tl_job != NULL)
2701 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002702
Bram Moolenaar61a66052017-07-22 18:39:00 +02002703 return term->tl_job != NULL
2704 && term->tl_job->jv_channel != NULL
2705 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002706}
2707
2708/*
2709 * Free the terminal emulator part of "term".
2710 */
2711 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002712term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002713{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002714 if (term->tl_vterm != NULL)
2715 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002716 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002717}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002718
2719/*
2720 * Request size to terminal.
2721 */
2722 static void
2723term_report_winsize(term_T *term, int rows, int cols)
2724{
2725 /* Use an ioctl() to report the new window size to the job. */
2726 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2727 {
2728 int fd = -1;
2729 int part;
2730
2731 for (part = PART_OUT; part < PART_COUNT; ++part)
2732 {
2733 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2734 if (isatty(fd))
2735 break;
2736 }
2737 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02002738 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002739 }
2740}
2741
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002742# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002743
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002744#endif /* FEAT_TERMINAL */