blob: 901bb9bed11083db13babaaed53320c3fda44442 [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 Moolenaar8c0095c2017-07-18 22:53:21 +020039 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020040 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020041 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
42 * job finishes).
43 * - add option values to the command:
44 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020045 * - support different cursor shapes, colors and attributes
Bram Moolenaar6d819742017-08-06 14:57:49 +020046 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020047 * - make term_getcursor() return type (none/block/bar/underline) and
48 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020049 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020050 * For the GUI fill termios with default values, perhaps like pangoterm:
51 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
52 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020053 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020054 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020055 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020056 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020057 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020058 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
59 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020060 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020061 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020062 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020063 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020064 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020065 * terminal. Allow:
66 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
67 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
68 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
69 * Check that something is connected to the terminal.
70 * Test: "cat" reading from a file or buffer
71 * "ls" writing stdout to a file or buffer
72 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020073 * - support ":term NONE" to open a terminal with a pty but not running a job
74 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020075 * - if the job in the terminal does not support the mouse, we can use the
76 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020077 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
78 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020079 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020080 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020081 * - Copy text in the vterm to the Vim buffer once in a while, so that
82 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020083 */
84
85#include "vim.h"
86
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020087#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020088
Bram Moolenaard5310982017-08-05 15:16:32 +020089#ifndef MIN
90# define MIN(x,y) ((x) < (y) ? (x) : (y))
91#endif
92#ifndef MAX
93# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020094#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020095
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020096#include "libvterm/include/vterm.h"
97
Bram Moolenaar33a43be2017-08-06 21:36:22 +020098/* This is VTermScreenCell without the characters, thus much smaller. */
99typedef struct {
100 VTermScreenCellAttrs attrs;
101 char width;
102 VTermColor fg, bg;
103} cellattr_T;
104
Bram Moolenaard85f2712017-07-28 21:51:57 +0200105typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200106 int sb_cols; /* can differ per line */
107 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200108} sb_line_T;
109
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200110/* typedef term_T in structs.h */
111struct terminal_S {
112 term_T *tl_next;
113
Bram Moolenaar423802d2017-07-30 16:52:24 +0200114 VTerm *tl_vterm;
115 job_T *tl_job;
116 buf_T *tl_buffer;
117
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200118 /* used when tl_job is NULL and only a pty was created */
119 int tl_tty_fd;
120 char_u *tl_tty_name;
121
Bram Moolenaar6d819742017-08-06 14:57:49 +0200122 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200123 int tl_channel_closed;
124
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 Moolenaare4f25e42017-07-07 11:54:15 +0200149};
150
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200151#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
152#define TMODE_LOOP 2 /* CTRL-W N used */
153
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200154/*
155 * List of all active terminals.
156 */
157static term_T *first_term = NULL;
158
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200159
160#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
161#define KEY_BUF_LEN 200
162
163/*
164 * Functions with separate implementation for MS-Windows and Unix-like systems.
165 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200166static 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 +0200167static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200168static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200169
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200170/**************************************
171 * 1. Generic code for all systems.
172 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200173
174/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200175 * Determine the terminal size from 'termsize' and the current window.
176 * Assumes term->tl_rows and term->tl_cols are zero.
177 */
178 static void
179set_term_and_win_size(term_T *term)
180{
181 if (*curwin->w_p_tms != NUL)
182 {
183 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
184
185 term->tl_rows = atoi((char *)curwin->w_p_tms);
186 term->tl_cols = atoi((char *)p);
187 }
188 if (term->tl_rows == 0)
189 term->tl_rows = curwin->w_height;
190 else
191 {
192 win_setheight_win(term->tl_rows, curwin);
193 term->tl_rows_fixed = TRUE;
194 }
195 if (term->tl_cols == 0)
196 term->tl_cols = curwin->w_width;
197 else
198 {
199 win_setwidth_win(term->tl_cols, curwin);
200 term->tl_cols_fixed = TRUE;
201 }
202}
203
204/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200205 * Initialize job options for a terminal job.
206 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200207 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200208 static void
209init_job_options(jobopt_T *opt)
210{
211 clear_job_options(opt);
212
213 opt->jo_mode = MODE_RAW;
214 opt->jo_out_mode = MODE_RAW;
215 opt->jo_err_mode = MODE_RAW;
216 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
217
218 opt->jo_io[PART_OUT] = JIO_BUFFER;
219 opt->jo_io[PART_ERR] = JIO_BUFFER;
220 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
221
222 opt->jo_modifiable[PART_OUT] = 0;
223 opt->jo_modifiable[PART_ERR] = 0;
224 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
225
226 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
227}
228
229/*
230 * Set job options mandatory for a terminal job.
231 */
232 static void
233setup_job_options(jobopt_T *opt, int rows, int cols)
234{
235 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
236 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
237 opt->jo_pty = TRUE;
238 opt->jo_term_rows = rows;
239 opt->jo_term_cols = cols;
240}
241
242 static void
243term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200244{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200245 exarg_T split_ea;
246 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248
249 if (check_restricted() || check_secure())
250 return;
251
252 term = (term_T *)alloc_clear(sizeof(term_T));
253 if (term == NULL)
254 return;
255 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200256 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200257 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200258
259 /* Open a new window or tab. */
260 vim_memset(&split_ea, 0, sizeof(split_ea));
261 split_ea.cmdidx = CMD_new;
262 split_ea.cmd = (char_u *)"new";
263 split_ea.arg = (char_u *)"";
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200264 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
265 {
266 split_ea.line2 = opt->jo_term_rows;
267 split_ea.addr_count = 1;
268 }
269 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
270 {
271 split_ea.line2 = opt->jo_term_cols;
272 split_ea.addr_count = 1;
273 }
274
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200275 ex_splitview(&split_ea);
276 if (curwin == old_curwin)
277 {
278 /* split failed */
279 vim_free(term);
280 return;
281 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200282 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200283 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200284
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200285 /* only one size was taken care of with :new, do the other one */
286 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
287 win_setheight(opt->jo_term_rows);
288 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
289 win_setwidth(opt->jo_term_cols);
290
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200291 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200292 term->tl_next = first_term;
293 first_term = term;
294
Bram Moolenaar293424c2017-07-26 23:11:01 +0200295 if (cmd == NULL || *cmd == NUL)
296 cmd = p_sh;
297
Bram Moolenaar78712a72017-08-05 14:50:12 +0200298 if (opt->jo_term_name != NULL)
299 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
300 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200301 {
302 int i;
303 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200304 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200305
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200306 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200307 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200308 /* Prepend a ! to the command name to avoid the buffer name equals
309 * the executable, otherwise ":w!" would overwrite it. */
310 if (i == 0)
311 vim_snprintf((char *)p, len, "!%s", cmd);
312 else
313 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200314 if (buflist_findname(p) == NULL)
315 {
316 curbuf->b_ffname = p;
317 break;
318 }
319 }
320 }
321 curbuf->b_fname = curbuf->b_ffname;
322
Bram Moolenaareb44a682017-08-03 22:44:55 +0200323 set_string_option_direct((char_u *)"buftype", -1,
324 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
325
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200326 /* Mark the buffer as not modifiable. It can only be made modifiable after
327 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200328 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200329
330 /* Set 'bufhidden' to "hide": allow closing the window. */
331 set_string_option_direct((char_u *)"bufhidden", -1,
332 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200333
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200334 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200335 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200336
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200337 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200338 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200339 {
340 /* store the size we ended up with */
341 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200342 }
343 else
344 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200345 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200346
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200347 /* Wiping out the buffer will also close the window and call
348 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200349 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200350 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200351}
352
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200353/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200354 * ":terminal": open a terminal window and execute a job in it.
355 */
356 void
357ex_terminal(exarg_T *eap)
358{
359 jobopt_T opt;
360
361 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200362
363 if (eap->addr_count == 2)
364 {
365 opt.jo_term_rows = eap->line1;
366 opt.jo_term_cols = eap->line2;
367 }
368 else if (eap->addr_count == 1)
369 {
370 if (cmdmod.split & WSP_VERT)
371 opt.jo_term_cols = eap->line2;
372 else
373 opt.jo_term_rows = eap->line2;
374 }
375 /* TODO: get more options from before the command */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200376
377 term_start(eap->arg, &opt);
378}
379
380/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200381 * Free the scrollback buffer for "term".
382 */
383 static void
384free_scrollback(term_T *term)
385{
386 int i;
387
388 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
389 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
390 ga_clear(&term->tl_scrollback);
391}
392
393/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200394 * Free a terminal and everything it refers to.
395 * Kills the job if there is one.
396 * Called when wiping out a buffer.
397 */
398 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200399free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200400{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200401 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200402 term_T *tp;
403
404 if (term == NULL)
405 return;
406 if (first_term == term)
407 first_term = term->tl_next;
408 else
409 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
410 if (tp->tl_next == term)
411 {
412 tp->tl_next = term->tl_next;
413 break;
414 }
415
416 if (term->tl_job != NULL)
417 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200418 if (term->tl_job->jv_status != JOB_ENDED
419 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200420 job_stop(term->tl_job, NULL, "kill");
421 job_unref(term->tl_job);
422 }
423
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200424 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200425
426 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200427 vim_free(term->tl_title);
428 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200429 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200430 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200431}
432
433/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200434 * Write job output "msg[len]" to the vterm.
435 */
436 static void
437term_write_job_output(term_T *term, char_u *msg, size_t len)
438{
439 VTerm *vterm = term->tl_vterm;
440 char_u *p;
441 size_t done;
442 size_t len_now;
443
444 for (done = 0; done < len; done += len_now)
445 {
446 for (p = msg + done; p < msg + len; )
447 {
448 if (*p == NL)
449 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200450 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200451 }
452 len_now = p - msg - done;
453 vterm_input_write(vterm, (char *)msg + done, len_now);
454 if (p < msg + len && *p == NL)
455 {
456 /* Convert NL to CR-NL, that appears to work best. */
457 vterm_input_write(vterm, "\r\n", 2);
458 ++len_now;
459 }
460 }
461
462 /* this invokes the damage callbacks */
463 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
464}
465
Bram Moolenaar1c844932017-07-24 23:36:41 +0200466 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200467update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200468{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200469 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200470 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200471 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200472 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200473 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200474 if (term->tl_cursor_visible)
475 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200476 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200477#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200478 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200479 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200480#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200481 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200482}
483
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200484/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200485 * Invoked when "msg" output from a job was received. Write it to the terminal
486 * of "buffer".
487 */
488 void
489write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
490{
491 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200492 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200493
Bram Moolenaard85f2712017-07-28 21:51:57 +0200494 if (term->tl_vterm == NULL)
495 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200496 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200497 return;
498 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200499 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200500 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200501
Bram Moolenaar6d819742017-08-06 14:57:49 +0200502 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200503 {
504 /* TODO: only update once in a while. */
505 update_screen(0);
506 update_cursor(term, TRUE);
507 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200508}
509
510/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200511 * Send a mouse position and click to the vterm
512 */
513 static int
514term_send_mouse(VTerm *vterm, int button, int pressed)
515{
516 VTermModifier mod = VTERM_MOD_NONE;
517
518 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
519 mouse_col - W_WINCOL(curwin), mod);
520 vterm_mouse_button(vterm, button, pressed, mod);
521 return TRUE;
522}
523
524/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200525 * Convert typed key "c" into bytes to send to the job.
526 * Return the number of bytes in "buf".
527 */
528 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200529term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200530{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200531 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200532 VTermKey key = VTERM_KEY_NONE;
533 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200534 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200535
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200536 switch (c)
537 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200538 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200539 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200540 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
541 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200542 case K_DEL: key = VTERM_KEY_DEL; break;
543 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200544 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
545 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200546 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200547 case K_S_END: mod = VTERM_MOD_SHIFT;
548 key = VTERM_KEY_END; break;
549 case K_C_END: mod = VTERM_MOD_CTRL;
550 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200551 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
552 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
553 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
554 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
555 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
556 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
557 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
558 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
559 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
560 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
561 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
562 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
563 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200564 case K_S_HOME: mod = VTERM_MOD_SHIFT;
565 key = VTERM_KEY_HOME; break;
566 case K_C_HOME: mod = VTERM_MOD_CTRL;
567 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200568 case K_INS: key = VTERM_KEY_INS; break;
569 case K_K0: key = VTERM_KEY_KP_0; break;
570 case K_K1: key = VTERM_KEY_KP_1; break;
571 case K_K2: key = VTERM_KEY_KP_2; break;
572 case K_K3: key = VTERM_KEY_KP_3; break;
573 case K_K4: key = VTERM_KEY_KP_4; break;
574 case K_K5: key = VTERM_KEY_KP_5; break;
575 case K_K6: key = VTERM_KEY_KP_6; break;
576 case K_K7: key = VTERM_KEY_KP_7; break;
577 case K_K8: key = VTERM_KEY_KP_8; break;
578 case K_K9: key = VTERM_KEY_KP_9; break;
579 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
580 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
581 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
582 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
583 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
584 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
585 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
586 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
587 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
588 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
589 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
590 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
591 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200592 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
593 key = VTERM_KEY_LEFT; break;
594 case K_C_LEFT: mod = VTERM_MOD_CTRL;
595 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200596 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
597 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
598 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200599 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
600 key = VTERM_KEY_RIGHT; break;
601 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
602 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200603 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200604 case K_S_UP: mod = VTERM_MOD_SHIFT;
605 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200606 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200607
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200608 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
609 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
610 case K_MOUSELEFT: /* TODO */ return 0;
611 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200612
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200613 case K_LEFTMOUSE:
614 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
615 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
616 case K_LEFTRELEASE:
617 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
618 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
619 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
620 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
621 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
622 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
623 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
624 case K_X1MOUSE: /* TODO */ return 0;
625 case K_X1DRAG: /* TODO */ return 0;
626 case K_X1RELEASE: /* TODO */ return 0;
627 case K_X2MOUSE: /* TODO */ return 0;
628 case K_X2DRAG: /* TODO */ return 0;
629 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200630
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200631 case K_IGNORE: return 0;
632 case K_NOP: return 0;
633 case K_UNDO: return 0;
634 case K_HELP: return 0;
635 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
636 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
637 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
638 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
639 case K_SELECT: return 0;
640#ifdef FEAT_GUI
641 case K_VER_SCROLLBAR: return 0;
642 case K_HOR_SCROLLBAR: return 0;
643#endif
644#ifdef FEAT_GUI_TABLINE
645 case K_TABLINE: return 0;
646 case K_TABMENU: return 0;
647#endif
648#ifdef FEAT_NETBEANS_INTG
649 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
650#endif
651#ifdef FEAT_DND
652 case K_DROP: return 0;
653#endif
654#ifdef FEAT_AUTOCMD
655 case K_CURSORHOLD: return 0;
656#endif
657 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
658 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200659 }
660
661 /*
662 * Convert special keys to vterm keys:
663 * - Write keys to vterm: vterm_keyboard_key()
664 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200665 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200666 */
667 if (key != VTERM_KEY_NONE)
668 /* Special key, let vterm convert it. */
669 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200670 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200671 /* Normal character, let vterm convert it. */
672 vterm_keyboard_unichar(vterm, c, mod);
673
674 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200675 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200676}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200677
Bram Moolenaar938783d2017-07-16 20:13:26 +0200678/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200679 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200680 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200681 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200682term_job_running(term_T *term)
683{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200684 /* Also consider the job finished when the channel is closed, to avoid a
685 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200686 return term != NULL
687 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200688 && term->tl_job->jv_status == JOB_STARTED
689 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200690}
691
692/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200693 * Add the last line of the scrollback buffer to the buffer in the window.
694 */
695 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200696add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200697{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200698 buf_T *buf = term->tl_buffer;
699 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
700 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200701
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200702 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200703 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200704 {
705 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200706 curbuf = buf;
707 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200708 curbuf = curwin->w_buffer;
709 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200710}
711
712/*
713 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200714 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200715 */
716 static void
717move_terminal_to_buffer(term_T *term)
718{
719 win_T *wp;
720 int len;
721 int lines_skipped = 0;
722 VTermPos pos;
723 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200724 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200725 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200726
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200727 if (term->tl_vterm == NULL)
728 return;
729 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200730 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
731 {
732 len = 0;
733 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
734 if (vterm_screen_get_cell(screen, pos, &cell) != 0
735 && cell.chars[0] != NUL)
736 len = pos.col + 1;
737
738 if (len == 0)
739 ++lines_skipped;
740 else
741 {
742 while (lines_skipped > 0)
743 {
744 /* Line was skipped, add an empty line. */
745 --lines_skipped;
746 if (ga_grow(&term->tl_scrollback, 1) == OK)
747 {
748 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
749 + term->tl_scrollback.ga_len;
750
751 line->sb_cols = 0;
752 line->sb_cells = NULL;
753 ++term->tl_scrollback.ga_len;
754
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200755 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200756 }
757 }
758
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200759 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200760 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
761 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200762 garray_T ga;
763 int width;
764 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200765 + term->tl_scrollback.ga_len;
766
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200767 ga_init2(&ga, 1, 100);
768 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200769 {
770 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200771 {
772 width = 1;
773 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
774 if (ga_grow(&ga, 1) == OK)
775 ga.ga_len += mb_char2bytes(' ',
776 (char_u *)ga.ga_data + ga.ga_len);
777 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200778 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200779 {
780 width = cell.width;
781
782 p[pos.col].width = cell.width;
783 p[pos.col].attrs = cell.attrs;
784 p[pos.col].fg = cell.fg;
785 p[pos.col].bg = cell.bg;
786
787 if (ga_grow(&ga, MB_MAXBYTES) == OK)
788 {
789 int i;
790 int c;
791
792 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
793 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
794 (char_u *)ga.ga_data + ga.ga_len);
795 }
796 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200797 }
798 line->sb_cols = len;
799 line->sb_cells = p;
800 ++term->tl_scrollback.ga_len;
801
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200802 if (ga_grow(&ga, 1) == FAIL)
803 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
804 else
805 {
806 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
807 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
808 }
809 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200810 }
811 else
812 vim_free(p);
813 }
814 }
815
816 FOR_ALL_WINDOWS(wp)
817 {
818 if (wp->w_buffer == term->tl_buffer)
819 {
820 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
821 wp->w_cursor.col = 0;
822 wp->w_valid = 0;
823 redraw_win_later(wp, NOT_VALID);
824 }
825 }
826}
827
828 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200829set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200830{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200831 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200832 vim_free(term->tl_status_text);
833 term->tl_status_text = NULL;
834 if (term->tl_buffer == curbuf)
835 maketitle();
836}
837
838/*
839 * Called after the job if finished and Terminal mode is not active:
840 * Move the vterm contents into the scrollback buffer and free the vterm.
841 */
842 static void
843cleanup_vterm(term_T *term)
844{
845 move_terminal_to_buffer(term);
846 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200847 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200848}
849
850/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200851 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200852 * Suspends updating the terminal window.
853 */
854 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200855term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200856{
857 term_T *term = curbuf->b_term;
858
859 /* Append the current terminal contents to the buffer. */
860 move_terminal_to_buffer(term);
861
Bram Moolenaar6d819742017-08-06 14:57:49 +0200862 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200863
Bram Moolenaar6d819742017-08-06 14:57:49 +0200864 /* Move the window cursor to the position of the cursor in the
865 * terminal. */
866 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
867 + term->tl_cursor_pos.row + 1;
868 check_cursor();
869 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200870
Bram Moolenaar6d819742017-08-06 14:57:49 +0200871 /* Display the same lines as in the terminal. */
872 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200873}
874
875/*
876 * Returns TRUE if the current window contains a terminal and we are in
877 * Terminal-Normal mode.
878 */
879 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200880term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200881{
882 term_T *term = curbuf->b_term;
883
Bram Moolenaar6d819742017-08-06 14:57:49 +0200884 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200885}
886
887/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200888 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200889 * Restores updating the terminal window.
890 */
891 void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200892term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +0200893{
894 term_T *term = curbuf->b_term;
895 sb_line_T *line;
896 garray_T *gap;
897
898 /* Remove the terminal contents from the scrollback and the buffer. */
899 gap = &term->tl_scrollback;
900 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
901 {
902 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
903 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
904 vim_free(line->sb_cells);
905 --gap->ga_len;
906 if (gap->ga_len == 0)
907 break;
908 }
909 check_cursor();
910
Bram Moolenaar6d819742017-08-06 14:57:49 +0200911 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200912
913 if (term->tl_channel_closed)
914 cleanup_vterm(term);
915 redraw_buf_and_status_later(curbuf, NOT_VALID);
916}
917
918/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200919 * Get a key from the user without mapping.
920 * TODO: use terminal mode mappings.
921 */
922 static int
923term_vgetc()
924{
925 int c;
926
927 ++no_mapping;
928 ++allow_keys;
929 got_int = FALSE;
930 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200931 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200932 --no_mapping;
933 --allow_keys;
934 return c;
935}
936
937/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200938 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200939 * Return FAIL when the key needs to be handled in Normal mode.
940 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200941 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200942 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200943send_keys_to_term(term_T *term, int c, int typed)
944{
945 char msg[KEY_BUF_LEN];
946 size_t len;
947 static int mouse_was_outside = FALSE;
948 int dragging_outside = FALSE;
949
950 /* Catch keys that need to be handled as in Normal mode. */
951 switch (c)
952 {
953 case NUL:
954 case K_ZERO:
955 if (typed)
956 stuffcharReadbuff(c);
957 return FAIL;
958
959 case K_IGNORE:
960 return FAIL;
961
962 case K_LEFTDRAG:
963 case K_MIDDLEDRAG:
964 case K_RIGHTDRAG:
965 case K_X1DRAG:
966 case K_X2DRAG:
967 dragging_outside = mouse_was_outside;
968 /* FALLTHROUGH */
969 case K_LEFTMOUSE:
970 case K_LEFTMOUSE_NM:
971 case K_LEFTRELEASE:
972 case K_LEFTRELEASE_NM:
973 case K_MIDDLEMOUSE:
974 case K_MIDDLERELEASE:
975 case K_RIGHTMOUSE:
976 case K_RIGHTRELEASE:
977 case K_X1MOUSE:
978 case K_X1RELEASE:
979 case K_X2MOUSE:
980 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200981
982 case K_MOUSEUP:
983 case K_MOUSEDOWN:
984 case K_MOUSELEFT:
985 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200986 if (mouse_row < W_WINROW(curwin)
987 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
988 || mouse_col < W_WINCOL(curwin)
989 || mouse_col >= W_ENDCOL(curwin)
990 || dragging_outside)
991 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200992 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200993 if (typed)
994 {
995 stuffcharReadbuff(c);
996 mouse_was_outside = TRUE;
997 }
998 return FAIL;
999 }
1000 }
1001 if (typed)
1002 mouse_was_outside = FALSE;
1003
1004 /* Convert the typed key to a sequence of bytes for the job. */
1005 len = term_convert_key(term, c, msg);
1006 if (len > 0)
1007 /* TODO: if FAIL is returned, stop? */
1008 channel_send(term->tl_job->jv_channel, PART_IN,
1009 (char_u *)msg, (int)len, NULL);
1010
1011 return OK;
1012}
1013
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001014 static void
1015position_cursor(win_T *wp, VTermPos *pos)
1016{
1017 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1018 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1019 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1020}
1021
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001022/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001023 * Handle CTRL-W "": send register contents to the job.
1024 */
1025 static void
1026term_paste_register(int prev_c UNUSED)
1027{
1028 int c;
1029 list_T *l;
1030 listitem_T *item;
1031 long reglen = 0;
1032 int type;
1033
1034#ifdef FEAT_CMDL_INFO
1035 if (add_to_showcmd(prev_c))
1036 if (add_to_showcmd('"'))
1037 out_flush();
1038#endif
1039 c = term_vgetc();
1040#ifdef FEAT_CMDL_INFO
1041 clear_showcmd();
1042#endif
1043
1044 /* CTRL-W "= prompt for expression to evaluate. */
1045 if (c == '=' && get_expr_register() != '=')
1046 return;
1047
1048 l = (list_T *)get_reg_contents(c, GREG_LIST);
1049 if (l != NULL)
1050 {
1051 type = get_reg_type(c, &reglen);
1052 for (item = l->lv_first; item != NULL; item = item->li_next)
1053 {
1054 char_u *s = get_tv_string(&item->li_tv);
1055
1056 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1057 s, STRLEN(s), NULL);
1058 if (item->li_next != NULL || type == MLINE)
1059 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1060 (char_u *)"\r", 1, NULL);
1061 }
1062 list_free(l);
1063 }
1064}
1065
1066/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001067 * Returns TRUE if the current window contains a terminal and we are sending
1068 * keys to the job.
1069 */
1070 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001071term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001072{
1073 term_T *term = curbuf->b_term;
1074
1075 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001076 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001077 && term->tl_vterm != NULL
1078 && term_job_running(term);
1079}
1080
1081/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001082 * Wait for input and send it to the job.
1083 * Return when the start of a CTRL-W command is typed or anything else that
1084 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001085 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1086 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001087 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001088 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001089terminal_loop(void)
1090{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001091 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001092 int termkey = 0;
1093
1094 if (*curwin->w_p_tk != NUL)
1095 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001096 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001097
1098 for (;;)
1099 {
1100 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001101 /* Repeat redrawing in case a message is received while redrawing. */
1102 while (curwin->w_redr_type != 0)
1103 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001104 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001105
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001106 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001107 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001108 /* job finished while waiting for a character */
1109 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001110
Bram Moolenaarfae42832017-08-01 22:24:26 +02001111#ifdef UNIX
1112 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1113#endif
1114#ifdef WIN3264
1115 if (c == Ctrl_C)
1116 /* We don't know if the job can handle CTRL-C itself or not, this
1117 * may kill the shell instead of killing the command running in the
1118 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001119 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001120#endif
1121
Bram Moolenaar69198192017-08-05 14:10:48 +02001122 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001123 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001124 int prev_c = c;
1125
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001126#ifdef FEAT_CMDL_INFO
1127 if (add_to_showcmd(c))
1128 out_flush();
1129#endif
1130 c = term_vgetc();
1131#ifdef FEAT_CMDL_INFO
1132 clear_showcmd();
1133#endif
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 Moolenaar1f28b4c2017-07-28 13:48:34 +02001137
Bram Moolenaar69198192017-08-05 14:10:48 +02001138 if (prev_c == Ctrl_BSL)
1139 {
1140 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001141 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001142 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1143 term_enter_normal_mode();
1144 return FAIL;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001145 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001146 /* Send both keys to the terminal. */
1147 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1148 }
1149 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001150 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001151 /* "CTRL-W .": send CTRL-W to the job */
1152 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001153 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001154 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001155 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001156 /* CTRL-W N : go to Terminal-Normal mode. */
1157 term_enter_normal_mode();
Bram Moolenaar423802d2017-07-30 16:52:24 +02001158 return FAIL;
1159 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001160 else if (c == '"')
1161 {
1162 term_paste_register(prev_c);
1163 continue;
1164 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001165 else if (termkey == 0 || c != termkey)
1166 {
1167 stuffcharReadbuff(Ctrl_W);
1168 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001169 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001170 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001171 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001172 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1173 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001174 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001175 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001176}
1177
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001178/*
1179 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001180 * This updates the title and status, but does not close the vter, because
1181 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001182 */
1183 void
1184term_job_ended(job_T *job)
1185{
1186 term_T *term;
1187 int did_one = FALSE;
1188
1189 for (term = first_term; term != NULL; term = term->tl_next)
1190 if (term->tl_job == job)
1191 {
1192 vim_free(term->tl_title);
1193 term->tl_title = NULL;
1194 vim_free(term->tl_status_text);
1195 term->tl_status_text = NULL;
1196 redraw_buf_and_status_later(term->tl_buffer, VALID);
1197 did_one = TRUE;
1198 }
1199 if (did_one)
1200 redraw_statuslines();
1201 if (curbuf->b_term != NULL)
1202 {
1203 if (curbuf->b_term->tl_job == job)
1204 maketitle();
1205 update_cursor(curbuf->b_term, TRUE);
1206 }
1207}
1208
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001209 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001210may_toggle_cursor(term_T *term)
1211{
1212 if (curbuf == term->tl_buffer)
1213 {
1214 if (term->tl_cursor_visible)
1215 cursor_on();
1216 else
1217 cursor_off();
1218 }
1219}
1220
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001221 static int
1222handle_damage(VTermRect rect, void *user)
1223{
1224 term_T *term = (term_T *)user;
1225
1226 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1227 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1228 redraw_buf_later(term->tl_buffer, NOT_VALID);
1229 return 1;
1230}
1231
1232 static int
1233handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1234{
1235 term_T *term = (term_T *)user;
1236
1237 /* TODO */
1238 redraw_buf_later(term->tl_buffer, NOT_VALID);
1239 return 1;
1240}
1241
1242 static int
1243handle_movecursor(
1244 VTermPos pos,
1245 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001246 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001247 void *user)
1248{
1249 term_T *term = (term_T *)user;
1250 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001251
1252 term->tl_cursor_pos = pos;
1253 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001254
1255 FOR_ALL_WINDOWS(wp)
1256 {
1257 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001258 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001259 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001260 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001261 {
1262 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001263 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001264 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001265
1266 return 1;
1267}
1268
Bram Moolenaar21554412017-07-24 21:44:43 +02001269 static int
1270handle_settermprop(
1271 VTermProp prop,
1272 VTermValue *value,
1273 void *user)
1274{
1275 term_T *term = (term_T *)user;
1276
1277 switch (prop)
1278 {
1279 case VTERM_PROP_TITLE:
1280 vim_free(term->tl_title);
1281 term->tl_title = vim_strsave((char_u *)value->string);
1282 vim_free(term->tl_status_text);
1283 term->tl_status_text = NULL;
1284 if (term == curbuf->b_term)
1285 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001286 break;
1287
1288 case VTERM_PROP_CURSORVISIBLE:
1289 term->tl_cursor_visible = value->boolean;
1290 may_toggle_cursor(term);
1291 out_flush();
1292 break;
1293
Bram Moolenaar21554412017-07-24 21:44:43 +02001294 default:
1295 break;
1296 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001297 /* Always return 1, otherwise vterm doesn't store the value internally. */
1298 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001299}
1300
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001301/*
1302 * The job running in the terminal resized the terminal.
1303 */
1304 static int
1305handle_resize(int rows, int cols, void *user)
1306{
1307 term_T *term = (term_T *)user;
1308 win_T *wp;
1309
1310 term->tl_rows = rows;
1311 term->tl_cols = cols;
1312 FOR_ALL_WINDOWS(wp)
1313 {
1314 if (wp->w_buffer == term->tl_buffer)
1315 {
1316 win_setheight_win(rows, wp);
1317 win_setwidth_win(cols, wp);
1318 }
1319 }
1320
1321 redraw_buf_later(term->tl_buffer, NOT_VALID);
1322 return 1;
1323}
1324
Bram Moolenaard85f2712017-07-28 21:51:57 +02001325/*
1326 * Handle a line that is pushed off the top of the screen.
1327 */
1328 static int
1329handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1330{
1331 term_T *term = (term_T *)user;
1332
1333 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001334 if (ga_grow(&term->tl_scrollback, 1) == OK)
1335 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001336 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001337 int len = 0;
1338 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001339 int c;
1340 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001341 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001342 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001343
1344 /* do not store empty cells at the end */
1345 for (i = 0; i < cols; ++i)
1346 if (cells[i].chars[0] != 0)
1347 len = i + 1;
1348
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001349 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001350 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001351 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001352 {
1353 ga_init2(&ga, 1, 100);
1354 for (col = 0; col < len; col += cells[col].width)
1355 {
1356 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1357 {
1358 ga.ga_len = 0;
1359 break;
1360 }
1361 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1362 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1363 (char_u *)ga.ga_data + ga.ga_len);
1364 p[col].width = cells[col].width;
1365 p[col].attrs = cells[col].attrs;
1366 p[col].fg = cells[col].fg;
1367 p[col].bg = cells[col].bg;
1368 }
1369 }
1370 if (ga_grow(&ga, 1) == FAIL)
1371 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1372 else
1373 {
1374 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1375 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1376 }
1377 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001378
1379 line = (sb_line_T *)term->tl_scrollback.ga_data
1380 + term->tl_scrollback.ga_len;
1381 line->sb_cols = len;
1382 line->sb_cells = p;
1383 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001384 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001385 }
1386 return 0; /* ignored */
1387}
1388
Bram Moolenaar21554412017-07-24 21:44:43 +02001389static VTermScreenCallbacks screen_callbacks = {
1390 handle_damage, /* damage */
1391 handle_moverect, /* moverect */
1392 handle_movecursor, /* movecursor */
1393 handle_settermprop, /* settermprop */
1394 NULL, /* bell */
1395 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001396 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001397 NULL /* sb_popline */
1398};
1399
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001400/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001401 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001402 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001403 */
1404 void
1405term_channel_closed(channel_T *ch)
1406{
1407 term_T *term;
1408 int did_one = FALSE;
1409
1410 for (term = first_term; term != NULL; term = term->tl_next)
1411 if (term->tl_job == ch->ch_job)
1412 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001413 term->tl_channel_closed = TRUE;
1414
Bram Moolenaard85f2712017-07-28 21:51:57 +02001415 vim_free(term->tl_title);
1416 term->tl_title = NULL;
1417 vim_free(term->tl_status_text);
1418 term->tl_status_text = NULL;
1419
Bram Moolenaar423802d2017-07-30 16:52:24 +02001420 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001421 if (!term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001422 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001423
1424 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1425 did_one = TRUE;
1426 }
1427 if (did_one)
1428 {
1429 redraw_statuslines();
1430
1431 /* Need to break out of vgetc(). */
1432 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001433 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001434
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001435 term = curbuf->b_term;
1436 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001437 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001438 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001439 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001440 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001441 }
1442 }
1443}
1444
1445/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001446 * Reverse engineer the RGB value into a cterm color index.
1447 * First color is 1. Return 0 if no match found.
1448 */
1449 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001450color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001451{
1452 int red = color->red;
1453 int blue = color->blue;
1454 int green = color->green;
1455
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001456 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001457 if (red == 0)
1458 {
1459 if (green == 0)
1460 {
1461 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001462 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001463 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001464 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001465 }
1466 else if (green == 224)
1467 {
1468 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001469 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001470 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001471 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001472 }
1473 }
1474 else if (red == 224)
1475 {
1476 if (green == 0)
1477 {
1478 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001479 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001480 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001481 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001482 }
1483 else if (green == 224)
1484 {
1485 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001486 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001487 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001488 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001489 }
1490 }
1491 else if (red == 128)
1492 {
1493 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001494 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001495 }
1496 else if (red == 255)
1497 {
1498 if (green == 64)
1499 {
1500 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001501 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001502 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001503 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001504 }
1505 else if (green == 255)
1506 {
1507 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001508 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001509 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001510 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001511 }
1512 }
1513 else if (red == 64)
1514 {
1515 if (green == 64)
1516 {
1517 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001518 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001519 }
1520 else if (green == 255)
1521 {
1522 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001523 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001524 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001525 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001526 }
1527 }
1528 if (t_colors >= 256)
1529 {
1530 if (red == blue && red == green)
1531 {
1532 /* 24-color greyscale */
1533 static int cutoff[23] = {
1534 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1535 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1536 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1537 int i;
1538
1539 for (i = 0; i < 23; ++i)
1540 if (red < cutoff[i])
1541 return i + 233;
1542 return 256;
1543 }
1544
1545 /* 216-color cube */
1546 return 17 + ((red + 25) / 0x33) * 36
1547 + ((green + 25) / 0x33) * 6
1548 + (blue + 25) / 0x33;
1549 }
1550 return 0;
1551}
1552
1553/*
1554 * Convert the attributes of a vterm cell into an attribute index.
1555 */
1556 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001557cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001558{
1559 int attr = 0;
1560
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001561 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001562 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001563 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001564 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001565 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001566 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001567 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001568 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001569 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001570 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001571
1572#ifdef FEAT_GUI
1573 if (gui.in_use)
1574 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001575 guicolor_T fg, bg;
1576
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001577 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1578 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001579 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001580 }
1581 else
1582#endif
1583#ifdef FEAT_TERMGUICOLORS
1584 if (p_tgc)
1585 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001586 guicolor_T fg, bg;
1587
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001588 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1589 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001590
1591 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001592 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001593 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001594#endif
1595 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001596 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001597 int fg = color2index(&cellfg, TRUE, &bold);
1598 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001599
1600 /* with 8 colors set the bold attribute to get a bright foreground */
1601 if (bold == TRUE)
1602 attr |= HL_BOLD;
1603 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001604 }
1605 return 0;
1606}
1607
1608/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001609 * Called to update a window that contains an active terminal.
1610 * Returns FAIL when there is no terminal running in this window or in
1611 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001612 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001613 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001614term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001615{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001616 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001617 VTerm *vterm;
1618 VTermScreen *screen;
1619 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001620 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001621
Bram Moolenaar6d819742017-08-06 14:57:49 +02001622 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001623 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001624
Bram Moolenaard85f2712017-07-28 21:51:57 +02001625 vterm = term->tl_vterm;
1626 screen = vterm_obtain_screen(vterm);
1627 state = vterm_obtain_state(vterm);
1628
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001629 /*
1630 * If the window was resized a redraw will be triggered and we get here.
1631 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1632 */
1633 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1634 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001635 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001636 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1637 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1638 win_T *twp;
1639
1640 FOR_ALL_WINDOWS(twp)
1641 {
1642 /* When more than one window shows the same terminal, use the
1643 * smallest size. */
1644 if (twp->w_buffer == term->tl_buffer)
1645 {
1646 if (!term->tl_rows_fixed && rows > twp->w_height)
1647 rows = twp->w_height;
1648 if (!term->tl_cols_fixed && cols > twp->w_width)
1649 cols = twp->w_width;
1650 }
1651 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001652
1653 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001654 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001655 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001656 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001657 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001658
1659 /* The cursor may have been moved when resizing. */
1660 vterm_state_get_cursorpos(state, &pos);
1661 position_cursor(wp, &pos);
1662
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001663 /* TODO: Only redraw what changed. */
1664 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001665 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001666 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001667 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001668
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001669 if (pos.row < term->tl_rows)
1670 {
1671 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001672 {
1673 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001674 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001675
Bram Moolenaareeac6772017-07-23 15:48:37 +02001676 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1677 vim_memset(&cell, 0, sizeof(cell));
1678
1679 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001680 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001681 if (c == NUL)
1682 {
1683 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001684#if defined(FEAT_MBYTE)
1685 if (enc_utf8)
1686 ScreenLinesUC[off] = NUL;
1687#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001688 }
1689 else
1690 {
1691#if defined(FEAT_MBYTE)
1692 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001693 {
1694 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001695 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001696 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001697 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001698 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001699 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001700 if (enc_utf8)
1701 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001702 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001703#else
1704 ScreenLines[off] = c;
1705#endif
1706 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001707 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001708
1709 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001710 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001711 if (cell.width == 2)
1712 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001713 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001714#if defined(FEAT_MBYTE)
1715 if (enc_utf8)
1716 ScreenLinesUC[off] = NUL;
1717#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001718 ++pos.col;
1719 ++off;
1720 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001721 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001722 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001723 else
1724 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001725
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001726 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1727 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001728 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001729
1730 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001731}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001732
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001733/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001734 * Return TRUE if "wp" is a terminal window where the job has finished.
1735 */
1736 int
1737term_is_finished(buf_T *buf)
1738{
1739 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1740}
1741
1742/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001743 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02001744 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001745 */
1746 int
1747term_show_buffer(buf_T *buf)
1748{
1749 term_T *term = buf->b_term;
1750
Bram Moolenaar6d819742017-08-06 14:57:49 +02001751 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001752}
1753
1754/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001755 * The current buffer is going to be changed. If there is terminal
1756 * highlighting remove it now.
1757 */
1758 void
1759term_change_in_curbuf(void)
1760{
1761 term_T *term = curbuf->b_term;
1762
1763 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1764 {
1765 free_scrollback(term);
1766 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001767
1768 /* The buffer is now like a normal buffer, it cannot be easily
1769 * abandoned when changed. */
1770 set_string_option_direct((char_u *)"buftype", -1,
1771 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001772 }
1773}
1774
1775/*
1776 * Get the screen attribute for a position in the buffer.
1777 */
1778 int
1779term_get_attr(buf_T *buf, linenr_T lnum, int col)
1780{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001781 term_T *term = buf->b_term;
1782 sb_line_T *line;
1783 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001784
Bram Moolenaar70229f92017-07-29 16:01:53 +02001785 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001786 return 0;
1787 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1788 if (col >= line->sb_cols)
1789 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001790 cellattr = line->sb_cells + col;
1791 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001792}
1793
1794/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001795 * Create a new vterm and initialize it.
1796 */
1797 static void
1798create_vterm(term_T *term, int rows, int cols)
1799{
1800 VTerm *vterm;
1801 VTermScreen *screen;
1802
1803 vterm = vterm_new(rows, cols);
1804 term->tl_vterm = vterm;
1805 screen = vterm_obtain_screen(vterm);
1806 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1807 /* TODO: depends on 'encoding'. */
1808 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001809
1810 /* Vterm uses a default black background. Set it to white when
1811 * 'background' is "light". */
1812 if (*p_bg == 'l')
1813 {
1814 VTermColor fg, bg;
1815
1816 fg.red = fg.green = fg.blue = 0;
1817 bg.red = bg.green = bg.blue = 255;
1818 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1819 }
1820
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001821 /* Required to initialize most things. */
1822 vterm_screen_reset(screen, 1 /* hard */);
1823}
1824
Bram Moolenaar21554412017-07-24 21:44:43 +02001825/*
1826 * Return the text to show for the buffer name and status.
1827 */
1828 char_u *
1829term_get_status_text(term_T *term)
1830{
1831 if (term->tl_status_text == NULL)
1832 {
1833 char_u *txt;
1834 size_t len;
1835
Bram Moolenaar6d819742017-08-06 14:57:49 +02001836 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001837 {
1838 if (term_job_running(term))
1839 txt = (char_u *)_("Terminal");
1840 else
1841 txt = (char_u *)_("Terminal-finished");
1842 }
1843 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001844 txt = term->tl_title;
1845 else if (term_job_running(term))
1846 txt = (char_u *)_("running");
1847 else
1848 txt = (char_u *)_("finished");
1849 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001850 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001851 if (term->tl_status_text != NULL)
1852 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1853 term->tl_buffer->b_fname, txt);
1854 }
1855 return term->tl_status_text;
1856}
1857
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001858/*
1859 * Mark references in jobs of terminals.
1860 */
1861 int
1862set_ref_in_term(int copyID)
1863{
1864 int abort = FALSE;
1865 term_T *term;
1866 typval_T tv;
1867
1868 for (term = first_term; term != NULL; term = term->tl_next)
1869 if (term->tl_job != NULL)
1870 {
1871 tv.v_type = VAR_JOB;
1872 tv.vval.v_job = term->tl_job;
1873 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1874 }
1875 return abort;
1876}
1877
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001878/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001879 * Get the buffer from the first argument in "argvars".
1880 * Returns NULL when the buffer is not for a terminal window.
1881 */
1882 static buf_T *
1883term_get_buf(typval_T *argvars)
1884{
1885 buf_T *buf;
1886
1887 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1888 ++emsg_off;
1889 buf = get_buf_tv(&argvars[0], FALSE);
1890 --emsg_off;
1891 if (buf == NULL || buf->b_term == NULL)
1892 return NULL;
1893 return buf;
1894}
1895
1896/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001897 * "term_getattr(attr, name)" function
1898 */
1899 void
1900f_term_getattr(typval_T *argvars, typval_T *rettv)
1901{
1902 int attr;
1903 size_t i;
1904 char_u *name;
1905
1906 static struct {
1907 char *name;
1908 int attr;
1909 } attrs[] = {
1910 {"bold", HL_BOLD},
1911 {"italic", HL_ITALIC},
1912 {"underline", HL_UNDERLINE},
1913 {"strike", HL_STANDOUT},
1914 {"reverse", HL_INVERSE},
1915 };
1916
1917 attr = get_tv_number(&argvars[0]);
1918 name = get_tv_string_chk(&argvars[1]);
1919 if (name == NULL)
1920 return;
1921
1922 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1923 if (STRCMP(name, attrs[i].name) == 0)
1924 {
1925 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1926 break;
1927 }
1928}
1929
1930/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001931 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001932 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001933 void
1934f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001935{
Bram Moolenaar97870002017-07-30 18:28:38 +02001936 buf_T *buf = term_get_buf(argvars);
1937 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001938
Bram Moolenaar97870002017-07-30 18:28:38 +02001939 if (rettv_list_alloc(rettv) == FAIL)
1940 return;
1941 if (buf == NULL)
1942 return;
1943
1944 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001945 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1946 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001947 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001948}
1949
1950/*
1951 * "term_getjob(buf)" function
1952 */
1953 void
1954f_term_getjob(typval_T *argvars, typval_T *rettv)
1955{
1956 buf_T *buf = term_get_buf(argvars);
1957
1958 rettv->v_type = VAR_JOB;
1959 rettv->vval.v_job = NULL;
1960 if (buf == NULL)
1961 return;
1962
1963 rettv->vval.v_job = buf->b_term->tl_job;
1964 if (rettv->vval.v_job != NULL)
1965 ++rettv->vval.v_job->jv_refcount;
1966}
1967
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001968 static int
1969get_row_number(typval_T *tv, term_T *term)
1970{
1971 if (tv->v_type == VAR_STRING
1972 && tv->vval.v_string != NULL
1973 && STRCMP(tv->vval.v_string, ".") == 0)
1974 return term->tl_cursor_pos.row;
1975 return (int)get_tv_number(tv) - 1;
1976}
1977
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001978/*
1979 * "term_getline(buf, row)" function
1980 */
1981 void
1982f_term_getline(typval_T *argvars, typval_T *rettv)
1983{
1984 buf_T *buf = term_get_buf(argvars);
1985 term_T *term;
1986 int row;
1987
1988 rettv->v_type = VAR_STRING;
1989 if (buf == NULL)
1990 return;
1991 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001992 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001993
1994 if (term->tl_vterm == NULL)
1995 {
1996 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1997
1998 /* vterm is finished, get the text from the buffer */
1999 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2000 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2001 }
2002 else
2003 {
2004 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2005 VTermRect rect;
2006 int len;
2007 char_u *p;
2008
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002009 if (row < 0 || row >= term->tl_rows)
2010 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002011 len = term->tl_cols * MB_MAXBYTES + 1;
2012 p = alloc(len);
2013 if (p == NULL)
2014 return;
2015 rettv->vval.v_string = p;
2016
2017 rect.start_col = 0;
2018 rect.end_col = term->tl_cols;
2019 rect.start_row = row;
2020 rect.end_row = row + 1;
2021 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2022 }
2023}
2024
2025/*
2026 * "term_getsize(buf)" function
2027 */
2028 void
2029f_term_getsize(typval_T *argvars, typval_T *rettv)
2030{
2031 buf_T *buf = term_get_buf(argvars);
2032 list_T *l;
2033
2034 if (rettv_list_alloc(rettv) == FAIL)
2035 return;
2036 if (buf == NULL)
2037 return;
2038
2039 l = rettv->vval.v_list;
2040 list_append_number(l, buf->b_term->tl_rows);
2041 list_append_number(l, buf->b_term->tl_cols);
2042}
2043
2044/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002045 * "term_getstatus(buf)" function
2046 */
2047 void
2048f_term_getstatus(typval_T *argvars, typval_T *rettv)
2049{
2050 buf_T *buf = term_get_buf(argvars);
2051 term_T *term;
2052 char_u val[100];
2053
2054 rettv->v_type = VAR_STRING;
2055 if (buf == NULL)
2056 return;
2057 term = buf->b_term;
2058
2059 if (term_job_running(term))
2060 STRCPY(val, "running");
2061 else
2062 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002063 if (term->tl_normal_mode)
2064 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002065 rettv->vval.v_string = vim_strsave(val);
2066}
2067
2068/*
2069 * "term_gettitle(buf)" function
2070 */
2071 void
2072f_term_gettitle(typval_T *argvars, typval_T *rettv)
2073{
2074 buf_T *buf = term_get_buf(argvars);
2075
2076 rettv->v_type = VAR_STRING;
2077 if (buf == NULL)
2078 return;
2079
2080 if (buf->b_term->tl_title != NULL)
2081 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2082}
2083
2084/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002085 * "term_gettty(buf)" function
2086 */
2087 void
2088f_term_gettty(typval_T *argvars, typval_T *rettv)
2089{
2090 buf_T *buf = term_get_buf(argvars);
2091 char_u *p;
2092
2093 rettv->v_type = VAR_STRING;
2094 if (buf == NULL)
2095 return;
2096 if (buf->b_term->tl_job != NULL)
2097 p = buf->b_term->tl_job->jv_tty_name;
2098 else
2099 p = buf->b_term->tl_tty_name;
2100 if (p != NULL)
2101 rettv->vval.v_string = vim_strsave(p);
2102}
2103
2104/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002105 * "term_list()" function
2106 */
2107 void
2108f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2109{
2110 term_T *tp;
2111 list_T *l;
2112
2113 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2114 return;
2115
2116 l = rettv->vval.v_list;
2117 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2118 if (tp != NULL && tp->tl_buffer != NULL)
2119 if (list_append_number(l,
2120 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2121 return;
2122}
2123
2124/*
2125 * "term_scrape(buf, row)" function
2126 */
2127 void
2128f_term_scrape(typval_T *argvars, typval_T *rettv)
2129{
2130 buf_T *buf = term_get_buf(argvars);
2131 VTermScreen *screen = NULL;
2132 VTermPos pos;
2133 list_T *l;
2134 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002135 char_u *p;
2136 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002137
2138 if (rettv_list_alloc(rettv) == FAIL)
2139 return;
2140 if (buf == NULL)
2141 return;
2142 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002143
2144 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002145 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002146
2147 if (term->tl_vterm != NULL)
2148 screen = vterm_obtain_screen(term->tl_vterm);
2149 else
2150 {
2151 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2152
2153 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2154 return;
2155 p = ml_get_buf(buf, lnum + 1, FALSE);
2156 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2157 }
2158
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002159 for (pos.col = 0; pos.col < term->tl_cols; )
2160 {
2161 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002162 int width;
2163 VTermScreenCellAttrs attrs;
2164 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002165 char_u rgb[8];
2166 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2167 int off = 0;
2168 int i;
2169
2170 if (screen == NULL)
2171 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002172 cellattr_T *cellattr;
2173 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002174
2175 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002176 if (pos.col >= line->sb_cols)
2177 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002178 cellattr = line->sb_cells + pos.col;
2179 width = cellattr->width;
2180 attrs = cellattr->attrs;
2181 fg = cellattr->fg;
2182 bg = cellattr->bg;
2183 len = MB_PTR2LEN(p);
2184 mch_memmove(mbs, p, len);
2185 mbs[len] = NUL;
2186 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002187 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002188 else
2189 {
2190 VTermScreenCell cell;
2191 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2192 break;
2193 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2194 {
2195 if (cell.chars[i] == 0)
2196 break;
2197 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2198 }
2199 mbs[off] = NUL;
2200 width = cell.width;
2201 attrs = cell.attrs;
2202 fg = cell.fg;
2203 bg = cell.bg;
2204 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002205 dcell = dict_alloc();
2206 list_append_dict(l, dcell);
2207
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002208 dict_add_nr_str(dcell, "chars", 0, mbs);
2209
2210 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002211 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002212 dict_add_nr_str(dcell, "fg", 0, rgb);
2213 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002214 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002215 dict_add_nr_str(dcell, "bg", 0, rgb);
2216
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002217 dict_add_nr_str(dcell, "attr",
2218 cell2attr(attrs, fg, bg), NULL);
2219 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002220
2221 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002222 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002223 ++pos.col;
2224 }
2225}
2226
2227/*
2228 * "term_sendkeys(buf, keys)" function
2229 */
2230 void
2231f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2232{
2233 buf_T *buf = term_get_buf(argvars);
2234 char_u *msg;
2235 term_T *term;
2236
2237 rettv->v_type = VAR_UNKNOWN;
2238 if (buf == NULL)
2239 return;
2240
2241 msg = get_tv_string_chk(&argvars[1]);
2242 if (msg == NULL)
2243 return;
2244 term = buf->b_term;
2245 if (term->tl_vterm == NULL)
2246 return;
2247
2248 while (*msg != NUL)
2249 {
2250 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2251 msg += MB_PTR2LEN(msg);
2252 }
2253
Bram Moolenaar6d819742017-08-06 14:57:49 +02002254 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002255 {
2256 /* TODO: only update once in a while. */
2257 update_screen(0);
2258 if (buf == curbuf)
2259 update_cursor(term, TRUE);
2260 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002261}
2262
2263/*
2264 * "term_start(command, options)" function
2265 */
2266 void
2267f_term_start(typval_T *argvars, typval_T *rettv)
2268{
2269 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002270 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002271
2272 if (cmd == NULL)
2273 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002274 init_job_options(&opt);
2275 /* TODO: allow more job options */
2276 if (argvars[1].v_type != VAR_UNKNOWN
2277 && get_job_options(&argvars[1], &opt,
2278 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar78712a72017-08-05 14:50:12 +02002279 + JO_EXIT_CB + JO_CLOSE_CALLBACK
2280 + JO2_TERM_NAME) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002281 return;
2282
2283 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002284
2285 if (curbuf->b_term != NULL)
2286 rettv->vval.v_number = curbuf->b_fnum;
2287}
2288
2289/*
2290 * "term_wait" function
2291 */
2292 void
2293f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2294{
2295 buf_T *buf = term_get_buf(argvars);
2296
2297 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002298 {
2299 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002300 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002301 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002302 if (buf->b_term->tl_job == NULL)
2303 {
2304 ch_log(NULL, "term_wait(): no job to wait for");
2305 return;
2306 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002307
2308 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002309 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002310 {
2311 /* The job is dead, keep reading channel I/O until the channel is
2312 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002313 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002314 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2315 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002316 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002317 parse_queued_messages();
2318 ui_delay(10L, FALSE);
2319 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002320 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002321 parse_queued_messages();
2322 }
2323 else
2324 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002325 long wait = 10L;
2326
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002327 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002328 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002329
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002330 /* Wait for some time for any channel I/O. */
2331 if (argvars[1].v_type != VAR_UNKNOWN)
2332 wait = get_tv_number(&argvars[1]);
2333 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002334 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002335
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002336 /* Flushing messages on channels is hopefully sufficient.
2337 * TODO: is there a better way? */
2338 parse_queued_messages();
2339 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002340}
2341
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002342# ifdef WIN3264
2343
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002344/**************************************
2345 * 2. MS-Windows implementation.
2346 */
2347
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002348#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2349#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2350
Bram Moolenaar8a773062017-07-24 22:29:21 +02002351void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002352void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002353void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002354BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2355void (*winpty_config_set_initial_size)(void*, int, int);
2356LPCWSTR (*winpty_conin_name)(void*);
2357LPCWSTR (*winpty_conout_name)(void*);
2358LPCWSTR (*winpty_conerr_name)(void*);
2359void (*winpty_free)(void*);
2360void (*winpty_config_free)(void*);
2361void (*winpty_spawn_config_free)(void*);
2362void (*winpty_error_free)(void*);
2363LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002364BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002365HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002366
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002367#define WINPTY_DLL "winpty.dll"
2368
2369static HINSTANCE hWinPtyDLL = NULL;
2370
2371 int
2372dyn_winpty_init(void)
2373{
2374 int i;
2375 static struct
2376 {
2377 char *name;
2378 FARPROC *ptr;
2379 } winpty_entry[] =
2380 {
2381 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2382 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2383 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2384 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2385 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2386 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2387 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2388 {"winpty_free", (FARPROC*)&winpty_free},
2389 {"winpty_open", (FARPROC*)&winpty_open},
2390 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2391 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2392 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2393 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002394 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002395 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002396 {NULL, NULL}
2397 };
2398
2399 /* No need to initialize twice. */
2400 if (hWinPtyDLL)
2401 return 1;
2402 /* Load winpty.dll */
2403 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2404 if (!hWinPtyDLL)
2405 {
2406 EMSG2(_(e_loadlib), WINPTY_DLL);
2407 return 0;
2408 }
2409 for (i = 0; winpty_entry[i].name != NULL
2410 && winpty_entry[i].ptr != NULL; ++i)
2411 {
2412 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2413 winpty_entry[i].name)) == NULL)
2414 {
2415 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2416 return 0;
2417 }
2418 }
2419
2420 return 1;
2421}
2422
2423/*
2424 * Create a new terminal of "rows" by "cols" cells.
2425 * Store a reference in "term".
2426 * Return OK or FAIL.
2427 */
2428 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002429term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002430{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002431 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002432 channel_T *channel = NULL;
2433 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002434 DWORD error;
2435 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2436 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002437 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002438 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002439
2440 if (!dyn_winpty_init())
2441 return FAIL;
2442
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002443 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002444 if (p == NULL)
2445 return FAIL;
2446
2447 job = job_alloc();
2448 if (job == NULL)
2449 goto failed;
2450
2451 channel = add_channel();
2452 if (channel == NULL)
2453 goto failed;
2454
2455 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2456 if (term->tl_winpty_config == NULL)
2457 goto failed;
2458
2459 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2460 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2461 if (term->tl_winpty == NULL)
2462 goto failed;
2463
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002464 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002465 spawn_config = winpty_spawn_config_new(
2466 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2467 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2468 NULL,
2469 p,
2470 NULL,
2471 NULL,
2472 &winpty_err);
2473 if (spawn_config == NULL)
2474 goto failed;
2475
2476 channel = add_channel();
2477 if (channel == NULL)
2478 goto failed;
2479
2480 job = job_alloc();
2481 if (job == NULL)
2482 goto failed;
2483
2484 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2485 &child_thread_handle, &error, &winpty_err))
2486 goto failed;
2487
2488 channel_set_pipes(channel,
2489 (sock_T) CreateFileW(
2490 winpty_conin_name(term->tl_winpty),
2491 GENERIC_WRITE, 0, NULL,
2492 OPEN_EXISTING, 0, NULL),
2493 (sock_T) CreateFileW(
2494 winpty_conout_name(term->tl_winpty),
2495 GENERIC_READ, 0, NULL,
2496 OPEN_EXISTING, 0, NULL),
2497 (sock_T) CreateFileW(
2498 winpty_conerr_name(term->tl_winpty),
2499 GENERIC_READ, 0, NULL,
2500 OPEN_EXISTING, 0, NULL));
2501
2502 jo = CreateJobObject(NULL, NULL);
2503 if (jo == NULL)
2504 goto failed;
2505
2506 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002507 {
2508 /* Failed, switch the way to terminate process with TerminateProcess. */
2509 CloseHandle(jo);
2510 jo = NULL;
2511 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002512
2513 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002514 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002515
2516 create_vterm(term, rows, cols);
2517
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002518 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002519 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002520
2521 job->jv_channel = channel;
2522 job->jv_proc_info.hProcess = child_process_handle;
2523 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2524 job->jv_job_object = jo;
2525 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002526 sprintf(buf, "winpty://%lu",
2527 GetProcessId(winpty_agent_process(term->tl_winpty)));
2528 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002529 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002530 term->tl_job = job;
2531
2532 return OK;
2533
2534failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002535 if (spawn_config != NULL)
2536 winpty_spawn_config_free(spawn_config);
2537 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002538 if (channel != NULL)
2539 channel_clear(channel);
2540 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002541 {
2542 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002543 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002544 }
2545 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002546 if (jo != NULL)
2547 CloseHandle(jo);
2548 if (term->tl_winpty != NULL)
2549 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002550 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002551 if (term->tl_winpty_config != NULL)
2552 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002553 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002554 if (winpty_err != NULL)
2555 {
2556 char_u *msg = utf16_to_enc(
2557 (short_u *)winpty_error_msg(winpty_err), NULL);
2558
2559 EMSG(msg);
2560 winpty_error_free(winpty_err);
2561 }
2562 return FAIL;
2563}
2564
2565/*
2566 * Free the terminal emulator part of "term".
2567 */
2568 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002569term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002570{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002571 if (term->tl_winpty != NULL)
2572 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002573 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002574 if (term->tl_winpty_config != NULL)
2575 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002576 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002577 if (term->tl_vterm != NULL)
2578 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002579 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002580}
2581
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002582/*
2583 * Request size to terminal.
2584 */
2585 static void
2586term_report_winsize(term_T *term, int rows, int cols)
2587{
2588 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2589}
2590
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002591# else
2592
2593/**************************************
2594 * 3. Unix-like implementation.
2595 */
2596
2597/*
2598 * Create a new terminal of "rows" by "cols" cells.
2599 * Start job for "cmd".
2600 * Store the pointers in "term".
2601 * Return OK or FAIL.
2602 */
2603 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002604term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002605{
2606 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002607
2608 create_vterm(term, rows, cols);
2609
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002610 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002611 argvars[0].v_type = VAR_STRING;
2612 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002613
2614 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002615 if (term->tl_job != NULL)
2616 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002617
Bram Moolenaar61a66052017-07-22 18:39:00 +02002618 return term->tl_job != NULL
2619 && term->tl_job->jv_channel != NULL
2620 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002621}
2622
2623/*
2624 * Free the terminal emulator part of "term".
2625 */
2626 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002627term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002628{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002629 if (term->tl_vterm != NULL)
2630 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002631 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002632}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002633
2634/*
2635 * Request size to terminal.
2636 */
2637 static void
2638term_report_winsize(term_T *term, int rows, int cols)
2639{
2640 /* Use an ioctl() to report the new window size to the job. */
2641 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2642 {
2643 int fd = -1;
2644 int part;
2645
2646 for (part = PART_OUT; part < PART_COUNT; ++part)
2647 {
2648 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2649 if (isatty(fd))
2650 break;
2651 }
2652 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2653 mch_stop_job(term->tl_job, (char_u *)"winch");
2654 }
2655}
2656
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002657# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002658
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002659#endif /* FEAT_TERMINAL */