blob: 99b82b2f510958706c35ea198a4a2cd91d2a4aae [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{
698 linenr_T lnum = term->tl_scrollback.ga_len - 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200699
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200700 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200701 if (lnum == 0)
702 {
703 /* Delete the empty line that was in the empty buffer. */
704 curbuf = term->tl_buffer;
705 ml_delete(2, FALSE);
706 curbuf = curwin->w_buffer;
707 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200708}
709
710/*
711 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200712 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200713 */
714 static void
715move_terminal_to_buffer(term_T *term)
716{
717 win_T *wp;
718 int len;
719 int lines_skipped = 0;
720 VTermPos pos;
721 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200722 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200723 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200724
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200725 if (term->tl_vterm == NULL)
726 return;
727 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200728 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
729 {
730 len = 0;
731 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
732 if (vterm_screen_get_cell(screen, pos, &cell) != 0
733 && cell.chars[0] != NUL)
734 len = pos.col + 1;
735
736 if (len == 0)
737 ++lines_skipped;
738 else
739 {
740 while (lines_skipped > 0)
741 {
742 /* Line was skipped, add an empty line. */
743 --lines_skipped;
744 if (ga_grow(&term->tl_scrollback, 1) == OK)
745 {
746 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
747 + term->tl_scrollback.ga_len;
748
749 line->sb_cols = 0;
750 line->sb_cells = NULL;
751 ++term->tl_scrollback.ga_len;
752
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200753 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200754 }
755 }
756
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200757 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200758 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
759 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200760 garray_T ga;
761 int width;
762 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200763 + term->tl_scrollback.ga_len;
764
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200765 ga_init2(&ga, 1, 100);
766 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200767 {
768 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200769 {
770 width = 1;
771 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
772 if (ga_grow(&ga, 1) == OK)
773 ga.ga_len += mb_char2bytes(' ',
774 (char_u *)ga.ga_data + ga.ga_len);
775 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200776 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200777 {
778 width = cell.width;
779
780 p[pos.col].width = cell.width;
781 p[pos.col].attrs = cell.attrs;
782 p[pos.col].fg = cell.fg;
783 p[pos.col].bg = cell.bg;
784
785 if (ga_grow(&ga, MB_MAXBYTES) == OK)
786 {
787 int i;
788 int c;
789
790 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
791 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
792 (char_u *)ga.ga_data + ga.ga_len);
793 }
794 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200795 }
796 line->sb_cols = len;
797 line->sb_cells = p;
798 ++term->tl_scrollback.ga_len;
799
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200800 if (ga_grow(&ga, 1) == FAIL)
801 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
802 else
803 {
804 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
805 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
806 }
807 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200808 }
809 else
810 vim_free(p);
811 }
812 }
813
814 FOR_ALL_WINDOWS(wp)
815 {
816 if (wp->w_buffer == term->tl_buffer)
817 {
818 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
819 wp->w_cursor.col = 0;
820 wp->w_valid = 0;
821 redraw_win_later(wp, NOT_VALID);
822 }
823 }
824}
825
826 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200827set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200828{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200829 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200830 vim_free(term->tl_status_text);
831 term->tl_status_text = NULL;
832 if (term->tl_buffer == curbuf)
833 maketitle();
834}
835
836/*
837 * Called after the job if finished and Terminal mode is not active:
838 * Move the vterm contents into the scrollback buffer and free the vterm.
839 */
840 static void
841cleanup_vterm(term_T *term)
842{
843 move_terminal_to_buffer(term);
844 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200845 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200846}
847
848/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200849 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200850 * Suspends updating the terminal window.
851 */
852 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200853term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200854{
855 term_T *term = curbuf->b_term;
856
857 /* Append the current terminal contents to the buffer. */
858 move_terminal_to_buffer(term);
859
Bram Moolenaar6d819742017-08-06 14:57:49 +0200860 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200861
Bram Moolenaar6d819742017-08-06 14:57:49 +0200862 /* Move the window cursor to the position of the cursor in the
863 * terminal. */
864 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
865 + term->tl_cursor_pos.row + 1;
866 check_cursor();
867 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200868
Bram Moolenaar6d819742017-08-06 14:57:49 +0200869 /* Display the same lines as in the terminal. */
870 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200871}
872
873/*
874 * Returns TRUE if the current window contains a terminal and we are in
875 * Terminal-Normal mode.
876 */
877 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200878term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200879{
880 term_T *term = curbuf->b_term;
881
Bram Moolenaar6d819742017-08-06 14:57:49 +0200882 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200883}
884
885/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200886 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200887 * Restores updating the terminal window.
888 */
889 void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200890term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +0200891{
892 term_T *term = curbuf->b_term;
893 sb_line_T *line;
894 garray_T *gap;
895
896 /* Remove the terminal contents from the scrollback and the buffer. */
897 gap = &term->tl_scrollback;
898 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
899 {
900 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
901 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
902 vim_free(line->sb_cells);
903 --gap->ga_len;
904 if (gap->ga_len == 0)
905 break;
906 }
907 check_cursor();
908
Bram Moolenaar6d819742017-08-06 14:57:49 +0200909 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200910
911 if (term->tl_channel_closed)
912 cleanup_vterm(term);
913 redraw_buf_and_status_later(curbuf, NOT_VALID);
914}
915
916/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200917 * Get a key from the user without mapping.
918 * TODO: use terminal mode mappings.
919 */
920 static int
921term_vgetc()
922{
923 int c;
924
925 ++no_mapping;
926 ++allow_keys;
927 got_int = FALSE;
928 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200929 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200930 --no_mapping;
931 --allow_keys;
932 return c;
933}
934
935/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200936 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200937 * Return FAIL when the key needs to be handled in Normal mode.
938 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200939 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200940 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200941send_keys_to_term(term_T *term, int c, int typed)
942{
943 char msg[KEY_BUF_LEN];
944 size_t len;
945 static int mouse_was_outside = FALSE;
946 int dragging_outside = FALSE;
947
948 /* Catch keys that need to be handled as in Normal mode. */
949 switch (c)
950 {
951 case NUL:
952 case K_ZERO:
953 if (typed)
954 stuffcharReadbuff(c);
955 return FAIL;
956
957 case K_IGNORE:
958 return FAIL;
959
960 case K_LEFTDRAG:
961 case K_MIDDLEDRAG:
962 case K_RIGHTDRAG:
963 case K_X1DRAG:
964 case K_X2DRAG:
965 dragging_outside = mouse_was_outside;
966 /* FALLTHROUGH */
967 case K_LEFTMOUSE:
968 case K_LEFTMOUSE_NM:
969 case K_LEFTRELEASE:
970 case K_LEFTRELEASE_NM:
971 case K_MIDDLEMOUSE:
972 case K_MIDDLERELEASE:
973 case K_RIGHTMOUSE:
974 case K_RIGHTRELEASE:
975 case K_X1MOUSE:
976 case K_X1RELEASE:
977 case K_X2MOUSE:
978 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200979
980 case K_MOUSEUP:
981 case K_MOUSEDOWN:
982 case K_MOUSELEFT:
983 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200984 if (mouse_row < W_WINROW(curwin)
985 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
986 || mouse_col < W_WINCOL(curwin)
987 || mouse_col >= W_ENDCOL(curwin)
988 || dragging_outside)
989 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200990 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200991 if (typed)
992 {
993 stuffcharReadbuff(c);
994 mouse_was_outside = TRUE;
995 }
996 return FAIL;
997 }
998 }
999 if (typed)
1000 mouse_was_outside = FALSE;
1001
1002 /* Convert the typed key to a sequence of bytes for the job. */
1003 len = term_convert_key(term, c, msg);
1004 if (len > 0)
1005 /* TODO: if FAIL is returned, stop? */
1006 channel_send(term->tl_job->jv_channel, PART_IN,
1007 (char_u *)msg, (int)len, NULL);
1008
1009 return OK;
1010}
1011
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001012 static void
1013position_cursor(win_T *wp, VTermPos *pos)
1014{
1015 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1016 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1017 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1018}
1019
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001020/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001021 * Handle CTRL-W "": send register contents to the job.
1022 */
1023 static void
1024term_paste_register(int prev_c UNUSED)
1025{
1026 int c;
1027 list_T *l;
1028 listitem_T *item;
1029 long reglen = 0;
1030 int type;
1031
1032#ifdef FEAT_CMDL_INFO
1033 if (add_to_showcmd(prev_c))
1034 if (add_to_showcmd('"'))
1035 out_flush();
1036#endif
1037 c = term_vgetc();
1038#ifdef FEAT_CMDL_INFO
1039 clear_showcmd();
1040#endif
1041
1042 /* CTRL-W "= prompt for expression to evaluate. */
1043 if (c == '=' && get_expr_register() != '=')
1044 return;
1045
1046 l = (list_T *)get_reg_contents(c, GREG_LIST);
1047 if (l != NULL)
1048 {
1049 type = get_reg_type(c, &reglen);
1050 for (item = l->lv_first; item != NULL; item = item->li_next)
1051 {
1052 char_u *s = get_tv_string(&item->li_tv);
1053
1054 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1055 s, STRLEN(s), NULL);
1056 if (item->li_next != NULL || type == MLINE)
1057 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1058 (char_u *)"\r", 1, NULL);
1059 }
1060 list_free(l);
1061 }
1062}
1063
1064/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001065 * Returns TRUE if the current window contains a terminal and we are sending
1066 * keys to the job.
1067 */
1068 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001069term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001070{
1071 term_T *term = curbuf->b_term;
1072
1073 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001074 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001075 && term->tl_vterm != NULL
1076 && term_job_running(term);
1077}
1078
1079/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001080 * Wait for input and send it to the job.
1081 * Return when the start of a CTRL-W command is typed or anything else that
1082 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001083 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1084 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001085 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001086 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001087terminal_loop(void)
1088{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001089 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001090 int termkey = 0;
1091
1092 if (*curwin->w_p_tk != NUL)
1093 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001094 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001095
1096 for (;;)
1097 {
1098 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001099 /* Repeat redrawing in case a message is received while redrawing. */
1100 while (curwin->w_redr_type != 0)
1101 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001102 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001103
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001104 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001105 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001106 /* job finished while waiting for a character */
1107 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001108
Bram Moolenaarfae42832017-08-01 22:24:26 +02001109#ifdef UNIX
1110 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1111#endif
1112#ifdef WIN3264
1113 if (c == Ctrl_C)
1114 /* We don't know if the job can handle CTRL-C itself or not, this
1115 * may kill the shell instead of killing the command running in the
1116 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001117 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001118#endif
1119
Bram Moolenaar69198192017-08-05 14:10:48 +02001120 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001121 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001122 int prev_c = c;
1123
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001124#ifdef FEAT_CMDL_INFO
1125 if (add_to_showcmd(c))
1126 out_flush();
1127#endif
1128 c = term_vgetc();
1129#ifdef FEAT_CMDL_INFO
1130 clear_showcmd();
1131#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001132 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001133 /* job finished while waiting for a character */
1134 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001135
Bram Moolenaar69198192017-08-05 14:10:48 +02001136 if (prev_c == Ctrl_BSL)
1137 {
1138 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001139 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001140 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1141 term_enter_normal_mode();
1142 return FAIL;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001143 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001144 /* Send both keys to the terminal. */
1145 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1146 }
1147 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001148 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001149 /* "CTRL-W .": send CTRL-W to the job */
1150 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001151 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001152 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001153 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001154 /* CTRL-W N : go to Terminal-Normal mode. */
1155 term_enter_normal_mode();
Bram Moolenaar423802d2017-07-30 16:52:24 +02001156 return FAIL;
1157 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001158 else if (c == '"')
1159 {
1160 term_paste_register(prev_c);
1161 continue;
1162 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001163 else if (termkey == 0 || c != termkey)
1164 {
1165 stuffcharReadbuff(Ctrl_W);
1166 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001167 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001168 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001169 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001170 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1171 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001172 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001173 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001174}
1175
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001176/*
1177 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001178 * This updates the title and status, but does not close the vter, because
1179 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001180 */
1181 void
1182term_job_ended(job_T *job)
1183{
1184 term_T *term;
1185 int did_one = FALSE;
1186
1187 for (term = first_term; term != NULL; term = term->tl_next)
1188 if (term->tl_job == job)
1189 {
1190 vim_free(term->tl_title);
1191 term->tl_title = NULL;
1192 vim_free(term->tl_status_text);
1193 term->tl_status_text = NULL;
1194 redraw_buf_and_status_later(term->tl_buffer, VALID);
1195 did_one = TRUE;
1196 }
1197 if (did_one)
1198 redraw_statuslines();
1199 if (curbuf->b_term != NULL)
1200 {
1201 if (curbuf->b_term->tl_job == job)
1202 maketitle();
1203 update_cursor(curbuf->b_term, TRUE);
1204 }
1205}
1206
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001207 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001208may_toggle_cursor(term_T *term)
1209{
1210 if (curbuf == term->tl_buffer)
1211 {
1212 if (term->tl_cursor_visible)
1213 cursor_on();
1214 else
1215 cursor_off();
1216 }
1217}
1218
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001219 static int
1220handle_damage(VTermRect rect, void *user)
1221{
1222 term_T *term = (term_T *)user;
1223
1224 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1225 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1226 redraw_buf_later(term->tl_buffer, NOT_VALID);
1227 return 1;
1228}
1229
1230 static int
1231handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1232{
1233 term_T *term = (term_T *)user;
1234
1235 /* TODO */
1236 redraw_buf_later(term->tl_buffer, NOT_VALID);
1237 return 1;
1238}
1239
1240 static int
1241handle_movecursor(
1242 VTermPos pos,
1243 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001244 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001245 void *user)
1246{
1247 term_T *term = (term_T *)user;
1248 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001249
1250 term->tl_cursor_pos = pos;
1251 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001252
1253 FOR_ALL_WINDOWS(wp)
1254 {
1255 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001256 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001257 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001258 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001259 {
1260 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001261 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001262 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001263
1264 return 1;
1265}
1266
Bram Moolenaar21554412017-07-24 21:44:43 +02001267 static int
1268handle_settermprop(
1269 VTermProp prop,
1270 VTermValue *value,
1271 void *user)
1272{
1273 term_T *term = (term_T *)user;
1274
1275 switch (prop)
1276 {
1277 case VTERM_PROP_TITLE:
1278 vim_free(term->tl_title);
1279 term->tl_title = vim_strsave((char_u *)value->string);
1280 vim_free(term->tl_status_text);
1281 term->tl_status_text = NULL;
1282 if (term == curbuf->b_term)
1283 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001284 break;
1285
1286 case VTERM_PROP_CURSORVISIBLE:
1287 term->tl_cursor_visible = value->boolean;
1288 may_toggle_cursor(term);
1289 out_flush();
1290 break;
1291
Bram Moolenaar21554412017-07-24 21:44:43 +02001292 default:
1293 break;
1294 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001295 /* Always return 1, otherwise vterm doesn't store the value internally. */
1296 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001297}
1298
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001299/*
1300 * The job running in the terminal resized the terminal.
1301 */
1302 static int
1303handle_resize(int rows, int cols, void *user)
1304{
1305 term_T *term = (term_T *)user;
1306 win_T *wp;
1307
1308 term->tl_rows = rows;
1309 term->tl_cols = cols;
1310 FOR_ALL_WINDOWS(wp)
1311 {
1312 if (wp->w_buffer == term->tl_buffer)
1313 {
1314 win_setheight_win(rows, wp);
1315 win_setwidth_win(cols, wp);
1316 }
1317 }
1318
1319 redraw_buf_later(term->tl_buffer, NOT_VALID);
1320 return 1;
1321}
1322
Bram Moolenaard85f2712017-07-28 21:51:57 +02001323/*
1324 * Handle a line that is pushed off the top of the screen.
1325 */
1326 static int
1327handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1328{
1329 term_T *term = (term_T *)user;
1330
1331 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001332 if (ga_grow(&term->tl_scrollback, 1) == OK)
1333 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001334 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001335 int len = 0;
1336 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001337 int c;
1338 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001339 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001340 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001341
1342 /* do not store empty cells at the end */
1343 for (i = 0; i < cols; ++i)
1344 if (cells[i].chars[0] != 0)
1345 len = i + 1;
1346
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001347 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001348 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001349 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001350 {
1351 ga_init2(&ga, 1, 100);
1352 for (col = 0; col < len; col += cells[col].width)
1353 {
1354 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1355 {
1356 ga.ga_len = 0;
1357 break;
1358 }
1359 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1360 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1361 (char_u *)ga.ga_data + ga.ga_len);
1362 p[col].width = cells[col].width;
1363 p[col].attrs = cells[col].attrs;
1364 p[col].fg = cells[col].fg;
1365 p[col].bg = cells[col].bg;
1366 }
1367 }
1368 if (ga_grow(&ga, 1) == FAIL)
1369 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1370 else
1371 {
1372 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1373 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1374 }
1375 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001376
1377 line = (sb_line_T *)term->tl_scrollback.ga_data
1378 + term->tl_scrollback.ga_len;
1379 line->sb_cols = len;
1380 line->sb_cells = p;
1381 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001382 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001383 }
1384 return 0; /* ignored */
1385}
1386
Bram Moolenaar21554412017-07-24 21:44:43 +02001387static VTermScreenCallbacks screen_callbacks = {
1388 handle_damage, /* damage */
1389 handle_moverect, /* moverect */
1390 handle_movecursor, /* movecursor */
1391 handle_settermprop, /* settermprop */
1392 NULL, /* bell */
1393 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001394 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001395 NULL /* sb_popline */
1396};
1397
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001398/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001399 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001400 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001401 */
1402 void
1403term_channel_closed(channel_T *ch)
1404{
1405 term_T *term;
1406 int did_one = FALSE;
1407
1408 for (term = first_term; term != NULL; term = term->tl_next)
1409 if (term->tl_job == ch->ch_job)
1410 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001411 term->tl_channel_closed = TRUE;
1412
Bram Moolenaard85f2712017-07-28 21:51:57 +02001413 vim_free(term->tl_title);
1414 term->tl_title = NULL;
1415 vim_free(term->tl_status_text);
1416 term->tl_status_text = NULL;
1417
Bram Moolenaar423802d2017-07-30 16:52:24 +02001418 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001419 if (!term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001420 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001421
1422 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1423 did_one = TRUE;
1424 }
1425 if (did_one)
1426 {
1427 redraw_statuslines();
1428
1429 /* Need to break out of vgetc(). */
1430 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001431 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001432
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001433 term = curbuf->b_term;
1434 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001435 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001436 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001437 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001438 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001439 }
1440 }
1441}
1442
1443/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001444 * Reverse engineer the RGB value into a cterm color index.
1445 * First color is 1. Return 0 if no match found.
1446 */
1447 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001448color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001449{
1450 int red = color->red;
1451 int blue = color->blue;
1452 int green = color->green;
1453
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001454 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001455 if (red == 0)
1456 {
1457 if (green == 0)
1458 {
1459 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001460 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001461 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001462 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001463 }
1464 else if (green == 224)
1465 {
1466 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001467 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001468 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001469 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001470 }
1471 }
1472 else if (red == 224)
1473 {
1474 if (green == 0)
1475 {
1476 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001477 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001478 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001479 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001480 }
1481 else if (green == 224)
1482 {
1483 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001484 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001485 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001486 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001487 }
1488 }
1489 else if (red == 128)
1490 {
1491 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001492 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001493 }
1494 else if (red == 255)
1495 {
1496 if (green == 64)
1497 {
1498 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001499 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001500 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001501 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001502 }
1503 else if (green == 255)
1504 {
1505 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001506 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001507 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001508 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001509 }
1510 }
1511 else if (red == 64)
1512 {
1513 if (green == 64)
1514 {
1515 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001516 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001517 }
1518 else if (green == 255)
1519 {
1520 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001521 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001522 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001523 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001524 }
1525 }
1526 if (t_colors >= 256)
1527 {
1528 if (red == blue && red == green)
1529 {
1530 /* 24-color greyscale */
1531 static int cutoff[23] = {
1532 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1533 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1534 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1535 int i;
1536
1537 for (i = 0; i < 23; ++i)
1538 if (red < cutoff[i])
1539 return i + 233;
1540 return 256;
1541 }
1542
1543 /* 216-color cube */
1544 return 17 + ((red + 25) / 0x33) * 36
1545 + ((green + 25) / 0x33) * 6
1546 + (blue + 25) / 0x33;
1547 }
1548 return 0;
1549}
1550
1551/*
1552 * Convert the attributes of a vterm cell into an attribute index.
1553 */
1554 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001555cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001556{
1557 int attr = 0;
1558
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001559 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001560 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001561 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001562 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001563 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001564 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001565 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001566 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001567 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001568 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001569
1570#ifdef FEAT_GUI
1571 if (gui.in_use)
1572 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001573 guicolor_T fg, bg;
1574
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001575 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1576 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001577 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001578 }
1579 else
1580#endif
1581#ifdef FEAT_TERMGUICOLORS
1582 if (p_tgc)
1583 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001584 guicolor_T fg, bg;
1585
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001586 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1587 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001588
1589 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001590 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001591 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001592#endif
1593 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001594 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001595 int fg = color2index(&cellfg, TRUE, &bold);
1596 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001597
1598 /* with 8 colors set the bold attribute to get a bright foreground */
1599 if (bold == TRUE)
1600 attr |= HL_BOLD;
1601 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001602 }
1603 return 0;
1604}
1605
1606/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001607 * Called to update a window that contains an active terminal.
1608 * Returns FAIL when there is no terminal running in this window or in
1609 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001610 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001611 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001612term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001613{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001614 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001615 VTerm *vterm;
1616 VTermScreen *screen;
1617 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001618 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001619
Bram Moolenaar6d819742017-08-06 14:57:49 +02001620 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001621 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001622
Bram Moolenaard85f2712017-07-28 21:51:57 +02001623 vterm = term->tl_vterm;
1624 screen = vterm_obtain_screen(vterm);
1625 state = vterm_obtain_state(vterm);
1626
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001627 /*
1628 * If the window was resized a redraw will be triggered and we get here.
1629 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1630 */
1631 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1632 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001633 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001634 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1635 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1636 win_T *twp;
1637
1638 FOR_ALL_WINDOWS(twp)
1639 {
1640 /* When more than one window shows the same terminal, use the
1641 * smallest size. */
1642 if (twp->w_buffer == term->tl_buffer)
1643 {
1644 if (!term->tl_rows_fixed && rows > twp->w_height)
1645 rows = twp->w_height;
1646 if (!term->tl_cols_fixed && cols > twp->w_width)
1647 cols = twp->w_width;
1648 }
1649 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001650
1651 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001652 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001653 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001654 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001655 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001656
1657 /* The cursor may have been moved when resizing. */
1658 vterm_state_get_cursorpos(state, &pos);
1659 position_cursor(wp, &pos);
1660
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001661 /* TODO: Only redraw what changed. */
1662 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001663 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001664 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001665 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001666
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001667 if (pos.row < term->tl_rows)
1668 {
1669 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001670 {
1671 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001672 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001673
Bram Moolenaareeac6772017-07-23 15:48:37 +02001674 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1675 vim_memset(&cell, 0, sizeof(cell));
1676
1677 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001678 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001679 if (c == NUL)
1680 {
1681 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001682#if defined(FEAT_MBYTE)
1683 if (enc_utf8)
1684 ScreenLinesUC[off] = NUL;
1685#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001686 }
1687 else
1688 {
1689#if defined(FEAT_MBYTE)
1690 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001691 {
1692 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001693 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001694 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001695 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001696 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001697 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001698 if (enc_utf8)
1699 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001700 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001701#else
1702 ScreenLines[off] = c;
1703#endif
1704 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001705 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001706
1707 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001708 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001709 if (cell.width == 2)
1710 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001711 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001712#if defined(FEAT_MBYTE)
1713 if (enc_utf8)
1714 ScreenLinesUC[off] = NUL;
1715#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001716 ++pos.col;
1717 ++off;
1718 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001719 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001720 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001721 else
1722 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001723
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001724 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1725 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001726 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001727
1728 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001729}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001730
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001731/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001732 * Return TRUE if "wp" is a terminal window where the job has finished.
1733 */
1734 int
1735term_is_finished(buf_T *buf)
1736{
1737 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1738}
1739
1740/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001741 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02001742 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001743 */
1744 int
1745term_show_buffer(buf_T *buf)
1746{
1747 term_T *term = buf->b_term;
1748
Bram Moolenaar6d819742017-08-06 14:57:49 +02001749 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001750}
1751
1752/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001753 * The current buffer is going to be changed. If there is terminal
1754 * highlighting remove it now.
1755 */
1756 void
1757term_change_in_curbuf(void)
1758{
1759 term_T *term = curbuf->b_term;
1760
1761 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1762 {
1763 free_scrollback(term);
1764 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001765
1766 /* The buffer is now like a normal buffer, it cannot be easily
1767 * abandoned when changed. */
1768 set_string_option_direct((char_u *)"buftype", -1,
1769 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001770 }
1771}
1772
1773/*
1774 * Get the screen attribute for a position in the buffer.
1775 */
1776 int
1777term_get_attr(buf_T *buf, linenr_T lnum, int col)
1778{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001779 term_T *term = buf->b_term;
1780 sb_line_T *line;
1781 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001782
Bram Moolenaar70229f92017-07-29 16:01:53 +02001783 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001784 return 0;
1785 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1786 if (col >= line->sb_cols)
1787 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001788 cellattr = line->sb_cells + col;
1789 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001790}
1791
1792/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001793 * Create a new vterm and initialize it.
1794 */
1795 static void
1796create_vterm(term_T *term, int rows, int cols)
1797{
1798 VTerm *vterm;
1799 VTermScreen *screen;
1800
1801 vterm = vterm_new(rows, cols);
1802 term->tl_vterm = vterm;
1803 screen = vterm_obtain_screen(vterm);
1804 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1805 /* TODO: depends on 'encoding'. */
1806 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001807
1808 /* Vterm uses a default black background. Set it to white when
1809 * 'background' is "light". */
1810 if (*p_bg == 'l')
1811 {
1812 VTermColor fg, bg;
1813
1814 fg.red = fg.green = fg.blue = 0;
1815 bg.red = bg.green = bg.blue = 255;
1816 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1817 }
1818
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001819 /* Required to initialize most things. */
1820 vterm_screen_reset(screen, 1 /* hard */);
1821}
1822
Bram Moolenaar21554412017-07-24 21:44:43 +02001823/*
1824 * Return the text to show for the buffer name and status.
1825 */
1826 char_u *
1827term_get_status_text(term_T *term)
1828{
1829 if (term->tl_status_text == NULL)
1830 {
1831 char_u *txt;
1832 size_t len;
1833
Bram Moolenaar6d819742017-08-06 14:57:49 +02001834 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001835 {
1836 if (term_job_running(term))
1837 txt = (char_u *)_("Terminal");
1838 else
1839 txt = (char_u *)_("Terminal-finished");
1840 }
1841 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001842 txt = term->tl_title;
1843 else if (term_job_running(term))
1844 txt = (char_u *)_("running");
1845 else
1846 txt = (char_u *)_("finished");
1847 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001848 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001849 if (term->tl_status_text != NULL)
1850 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1851 term->tl_buffer->b_fname, txt);
1852 }
1853 return term->tl_status_text;
1854}
1855
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001856/*
1857 * Mark references in jobs of terminals.
1858 */
1859 int
1860set_ref_in_term(int copyID)
1861{
1862 int abort = FALSE;
1863 term_T *term;
1864 typval_T tv;
1865
1866 for (term = first_term; term != NULL; term = term->tl_next)
1867 if (term->tl_job != NULL)
1868 {
1869 tv.v_type = VAR_JOB;
1870 tv.vval.v_job = term->tl_job;
1871 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1872 }
1873 return abort;
1874}
1875
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001876/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001877 * Get the buffer from the first argument in "argvars".
1878 * Returns NULL when the buffer is not for a terminal window.
1879 */
1880 static buf_T *
1881term_get_buf(typval_T *argvars)
1882{
1883 buf_T *buf;
1884
1885 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1886 ++emsg_off;
1887 buf = get_buf_tv(&argvars[0], FALSE);
1888 --emsg_off;
1889 if (buf == NULL || buf->b_term == NULL)
1890 return NULL;
1891 return buf;
1892}
1893
1894/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001895 * "term_getattr(attr, name)" function
1896 */
1897 void
1898f_term_getattr(typval_T *argvars, typval_T *rettv)
1899{
1900 int attr;
1901 size_t i;
1902 char_u *name;
1903
1904 static struct {
1905 char *name;
1906 int attr;
1907 } attrs[] = {
1908 {"bold", HL_BOLD},
1909 {"italic", HL_ITALIC},
1910 {"underline", HL_UNDERLINE},
1911 {"strike", HL_STANDOUT},
1912 {"reverse", HL_INVERSE},
1913 };
1914
1915 attr = get_tv_number(&argvars[0]);
1916 name = get_tv_string_chk(&argvars[1]);
1917 if (name == NULL)
1918 return;
1919
1920 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1921 if (STRCMP(name, attrs[i].name) == 0)
1922 {
1923 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1924 break;
1925 }
1926}
1927
1928/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001929 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001930 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001931 void
1932f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001933{
Bram Moolenaar97870002017-07-30 18:28:38 +02001934 buf_T *buf = term_get_buf(argvars);
1935 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001936
Bram Moolenaar97870002017-07-30 18:28:38 +02001937 if (rettv_list_alloc(rettv) == FAIL)
1938 return;
1939 if (buf == NULL)
1940 return;
1941
1942 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001943 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1944 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001945 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001946}
1947
1948/*
1949 * "term_getjob(buf)" function
1950 */
1951 void
1952f_term_getjob(typval_T *argvars, typval_T *rettv)
1953{
1954 buf_T *buf = term_get_buf(argvars);
1955
1956 rettv->v_type = VAR_JOB;
1957 rettv->vval.v_job = NULL;
1958 if (buf == NULL)
1959 return;
1960
1961 rettv->vval.v_job = buf->b_term->tl_job;
1962 if (rettv->vval.v_job != NULL)
1963 ++rettv->vval.v_job->jv_refcount;
1964}
1965
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001966 static int
1967get_row_number(typval_T *tv, term_T *term)
1968{
1969 if (tv->v_type == VAR_STRING
1970 && tv->vval.v_string != NULL
1971 && STRCMP(tv->vval.v_string, ".") == 0)
1972 return term->tl_cursor_pos.row;
1973 return (int)get_tv_number(tv) - 1;
1974}
1975
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001976/*
1977 * "term_getline(buf, row)" function
1978 */
1979 void
1980f_term_getline(typval_T *argvars, typval_T *rettv)
1981{
1982 buf_T *buf = term_get_buf(argvars);
1983 term_T *term;
1984 int row;
1985
1986 rettv->v_type = VAR_STRING;
1987 if (buf == NULL)
1988 return;
1989 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001990 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001991
1992 if (term->tl_vterm == NULL)
1993 {
1994 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1995
1996 /* vterm is finished, get the text from the buffer */
1997 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1998 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1999 }
2000 else
2001 {
2002 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2003 VTermRect rect;
2004 int len;
2005 char_u *p;
2006
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002007 if (row < 0 || row >= term->tl_rows)
2008 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002009 len = term->tl_cols * MB_MAXBYTES + 1;
2010 p = alloc(len);
2011 if (p == NULL)
2012 return;
2013 rettv->vval.v_string = p;
2014
2015 rect.start_col = 0;
2016 rect.end_col = term->tl_cols;
2017 rect.start_row = row;
2018 rect.end_row = row + 1;
2019 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2020 }
2021}
2022
2023/*
2024 * "term_getsize(buf)" function
2025 */
2026 void
2027f_term_getsize(typval_T *argvars, typval_T *rettv)
2028{
2029 buf_T *buf = term_get_buf(argvars);
2030 list_T *l;
2031
2032 if (rettv_list_alloc(rettv) == FAIL)
2033 return;
2034 if (buf == NULL)
2035 return;
2036
2037 l = rettv->vval.v_list;
2038 list_append_number(l, buf->b_term->tl_rows);
2039 list_append_number(l, buf->b_term->tl_cols);
2040}
2041
2042/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002043 * "term_getstatus(buf)" function
2044 */
2045 void
2046f_term_getstatus(typval_T *argvars, typval_T *rettv)
2047{
2048 buf_T *buf = term_get_buf(argvars);
2049 term_T *term;
2050 char_u val[100];
2051
2052 rettv->v_type = VAR_STRING;
2053 if (buf == NULL)
2054 return;
2055 term = buf->b_term;
2056
2057 if (term_job_running(term))
2058 STRCPY(val, "running");
2059 else
2060 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002061 if (term->tl_normal_mode)
2062 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002063 rettv->vval.v_string = vim_strsave(val);
2064}
2065
2066/*
2067 * "term_gettitle(buf)" function
2068 */
2069 void
2070f_term_gettitle(typval_T *argvars, typval_T *rettv)
2071{
2072 buf_T *buf = term_get_buf(argvars);
2073
2074 rettv->v_type = VAR_STRING;
2075 if (buf == NULL)
2076 return;
2077
2078 if (buf->b_term->tl_title != NULL)
2079 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2080}
2081
2082/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002083 * "term_gettty(buf)" function
2084 */
2085 void
2086f_term_gettty(typval_T *argvars, typval_T *rettv)
2087{
2088 buf_T *buf = term_get_buf(argvars);
2089 char_u *p;
2090
2091 rettv->v_type = VAR_STRING;
2092 if (buf == NULL)
2093 return;
2094 if (buf->b_term->tl_job != NULL)
2095 p = buf->b_term->tl_job->jv_tty_name;
2096 else
2097 p = buf->b_term->tl_tty_name;
2098 if (p != NULL)
2099 rettv->vval.v_string = vim_strsave(p);
2100}
2101
2102/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002103 * "term_list()" function
2104 */
2105 void
2106f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2107{
2108 term_T *tp;
2109 list_T *l;
2110
2111 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2112 return;
2113
2114 l = rettv->vval.v_list;
2115 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2116 if (tp != NULL && tp->tl_buffer != NULL)
2117 if (list_append_number(l,
2118 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2119 return;
2120}
2121
2122/*
2123 * "term_scrape(buf, row)" function
2124 */
2125 void
2126f_term_scrape(typval_T *argvars, typval_T *rettv)
2127{
2128 buf_T *buf = term_get_buf(argvars);
2129 VTermScreen *screen = NULL;
2130 VTermPos pos;
2131 list_T *l;
2132 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002133 char_u *p;
2134 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002135
2136 if (rettv_list_alloc(rettv) == FAIL)
2137 return;
2138 if (buf == NULL)
2139 return;
2140 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002141
2142 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002143 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002144
2145 if (term->tl_vterm != NULL)
2146 screen = vterm_obtain_screen(term->tl_vterm);
2147 else
2148 {
2149 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2150
2151 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2152 return;
2153 p = ml_get_buf(buf, lnum + 1, FALSE);
2154 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2155 }
2156
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002157 for (pos.col = 0; pos.col < term->tl_cols; )
2158 {
2159 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002160 int width;
2161 VTermScreenCellAttrs attrs;
2162 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002163 char_u rgb[8];
2164 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2165 int off = 0;
2166 int i;
2167
2168 if (screen == NULL)
2169 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002170 cellattr_T *cellattr;
2171 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002172
2173 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002174 if (pos.col >= line->sb_cols)
2175 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002176 cellattr = line->sb_cells + pos.col;
2177 width = cellattr->width;
2178 attrs = cellattr->attrs;
2179 fg = cellattr->fg;
2180 bg = cellattr->bg;
2181 len = MB_PTR2LEN(p);
2182 mch_memmove(mbs, p, len);
2183 mbs[len] = NUL;
2184 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002185 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002186 else
2187 {
2188 VTermScreenCell cell;
2189 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2190 break;
2191 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2192 {
2193 if (cell.chars[i] == 0)
2194 break;
2195 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2196 }
2197 mbs[off] = NUL;
2198 width = cell.width;
2199 attrs = cell.attrs;
2200 fg = cell.fg;
2201 bg = cell.bg;
2202 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002203 dcell = dict_alloc();
2204 list_append_dict(l, dcell);
2205
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002206 dict_add_nr_str(dcell, "chars", 0, mbs);
2207
2208 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002209 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002210 dict_add_nr_str(dcell, "fg", 0, rgb);
2211 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002212 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002213 dict_add_nr_str(dcell, "bg", 0, rgb);
2214
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002215 dict_add_nr_str(dcell, "attr",
2216 cell2attr(attrs, fg, bg), NULL);
2217 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002218
2219 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002220 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002221 ++pos.col;
2222 }
2223}
2224
2225/*
2226 * "term_sendkeys(buf, keys)" function
2227 */
2228 void
2229f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2230{
2231 buf_T *buf = term_get_buf(argvars);
2232 char_u *msg;
2233 term_T *term;
2234
2235 rettv->v_type = VAR_UNKNOWN;
2236 if (buf == NULL)
2237 return;
2238
2239 msg = get_tv_string_chk(&argvars[1]);
2240 if (msg == NULL)
2241 return;
2242 term = buf->b_term;
2243 if (term->tl_vterm == NULL)
2244 return;
2245
2246 while (*msg != NUL)
2247 {
2248 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2249 msg += MB_PTR2LEN(msg);
2250 }
2251
Bram Moolenaar6d819742017-08-06 14:57:49 +02002252 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002253 {
2254 /* TODO: only update once in a while. */
2255 update_screen(0);
2256 if (buf == curbuf)
2257 update_cursor(term, TRUE);
2258 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002259}
2260
2261/*
2262 * "term_start(command, options)" function
2263 */
2264 void
2265f_term_start(typval_T *argvars, typval_T *rettv)
2266{
2267 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002268 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002269
2270 if (cmd == NULL)
2271 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002272 init_job_options(&opt);
2273 /* TODO: allow more job options */
2274 if (argvars[1].v_type != VAR_UNKNOWN
2275 && get_job_options(&argvars[1], &opt,
2276 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar78712a72017-08-05 14:50:12 +02002277 + JO_EXIT_CB + JO_CLOSE_CALLBACK
2278 + JO2_TERM_NAME) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002279 return;
2280
2281 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002282
2283 if (curbuf->b_term != NULL)
2284 rettv->vval.v_number = curbuf->b_fnum;
2285}
2286
2287/*
2288 * "term_wait" function
2289 */
2290 void
2291f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2292{
2293 buf_T *buf = term_get_buf(argvars);
2294
2295 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002296 {
2297 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002298 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002299 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002300 if (buf->b_term->tl_job == NULL)
2301 {
2302 ch_log(NULL, "term_wait(): no job to wait for");
2303 return;
2304 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002305
2306 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002307 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002308 {
2309 /* The job is dead, keep reading channel I/O until the channel is
2310 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002311 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002312 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2313 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002314 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002315 parse_queued_messages();
2316 ui_delay(10L, FALSE);
2317 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002318 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002319 parse_queued_messages();
2320 }
2321 else
2322 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002323 long wait = 10L;
2324
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002325 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002326 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002327
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002328 /* Wait for some time for any channel I/O. */
2329 if (argvars[1].v_type != VAR_UNKNOWN)
2330 wait = get_tv_number(&argvars[1]);
2331 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002332 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002333
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002334 /* Flushing messages on channels is hopefully sufficient.
2335 * TODO: is there a better way? */
2336 parse_queued_messages();
2337 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002338}
2339
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002340# ifdef WIN3264
2341
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002342/**************************************
2343 * 2. MS-Windows implementation.
2344 */
2345
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002346#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2347#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2348
Bram Moolenaar8a773062017-07-24 22:29:21 +02002349void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002350void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002351void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002352BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2353void (*winpty_config_set_initial_size)(void*, int, int);
2354LPCWSTR (*winpty_conin_name)(void*);
2355LPCWSTR (*winpty_conout_name)(void*);
2356LPCWSTR (*winpty_conerr_name)(void*);
2357void (*winpty_free)(void*);
2358void (*winpty_config_free)(void*);
2359void (*winpty_spawn_config_free)(void*);
2360void (*winpty_error_free)(void*);
2361LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002362BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002363HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002364
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002365#define WINPTY_DLL "winpty.dll"
2366
2367static HINSTANCE hWinPtyDLL = NULL;
2368
2369 int
2370dyn_winpty_init(void)
2371{
2372 int i;
2373 static struct
2374 {
2375 char *name;
2376 FARPROC *ptr;
2377 } winpty_entry[] =
2378 {
2379 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2380 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2381 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2382 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2383 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2384 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2385 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2386 {"winpty_free", (FARPROC*)&winpty_free},
2387 {"winpty_open", (FARPROC*)&winpty_open},
2388 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2389 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2390 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2391 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002392 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002393 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002394 {NULL, NULL}
2395 };
2396
2397 /* No need to initialize twice. */
2398 if (hWinPtyDLL)
2399 return 1;
2400 /* Load winpty.dll */
2401 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2402 if (!hWinPtyDLL)
2403 {
2404 EMSG2(_(e_loadlib), WINPTY_DLL);
2405 return 0;
2406 }
2407 for (i = 0; winpty_entry[i].name != NULL
2408 && winpty_entry[i].ptr != NULL; ++i)
2409 {
2410 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2411 winpty_entry[i].name)) == NULL)
2412 {
2413 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2414 return 0;
2415 }
2416 }
2417
2418 return 1;
2419}
2420
2421/*
2422 * Create a new terminal of "rows" by "cols" cells.
2423 * Store a reference in "term".
2424 * Return OK or FAIL.
2425 */
2426 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002427term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002428{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002429 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002430 channel_T *channel = NULL;
2431 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002432 DWORD error;
2433 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2434 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002435 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002436 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002437
2438 if (!dyn_winpty_init())
2439 return FAIL;
2440
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002441 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002442 if (p == NULL)
2443 return FAIL;
2444
2445 job = job_alloc();
2446 if (job == NULL)
2447 goto failed;
2448
2449 channel = add_channel();
2450 if (channel == NULL)
2451 goto failed;
2452
2453 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2454 if (term->tl_winpty_config == NULL)
2455 goto failed;
2456
2457 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2458 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2459 if (term->tl_winpty == NULL)
2460 goto failed;
2461
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002462 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002463 spawn_config = winpty_spawn_config_new(
2464 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2465 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2466 NULL,
2467 p,
2468 NULL,
2469 NULL,
2470 &winpty_err);
2471 if (spawn_config == NULL)
2472 goto failed;
2473
2474 channel = add_channel();
2475 if (channel == NULL)
2476 goto failed;
2477
2478 job = job_alloc();
2479 if (job == NULL)
2480 goto failed;
2481
2482 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2483 &child_thread_handle, &error, &winpty_err))
2484 goto failed;
2485
2486 channel_set_pipes(channel,
2487 (sock_T) CreateFileW(
2488 winpty_conin_name(term->tl_winpty),
2489 GENERIC_WRITE, 0, NULL,
2490 OPEN_EXISTING, 0, NULL),
2491 (sock_T) CreateFileW(
2492 winpty_conout_name(term->tl_winpty),
2493 GENERIC_READ, 0, NULL,
2494 OPEN_EXISTING, 0, NULL),
2495 (sock_T) CreateFileW(
2496 winpty_conerr_name(term->tl_winpty),
2497 GENERIC_READ, 0, NULL,
2498 OPEN_EXISTING, 0, NULL));
2499
2500 jo = CreateJobObject(NULL, NULL);
2501 if (jo == NULL)
2502 goto failed;
2503
2504 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002505 {
2506 /* Failed, switch the way to terminate process with TerminateProcess. */
2507 CloseHandle(jo);
2508 jo = NULL;
2509 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002510
2511 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002512 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002513
2514 create_vterm(term, rows, cols);
2515
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002516 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002517 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002518
2519 job->jv_channel = channel;
2520 job->jv_proc_info.hProcess = child_process_handle;
2521 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2522 job->jv_job_object = jo;
2523 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002524 sprintf(buf, "winpty://%lu",
2525 GetProcessId(winpty_agent_process(term->tl_winpty)));
2526 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002527 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002528 term->tl_job = job;
2529
2530 return OK;
2531
2532failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002533 if (spawn_config != NULL)
2534 winpty_spawn_config_free(spawn_config);
2535 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002536 if (channel != NULL)
2537 channel_clear(channel);
2538 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002539 {
2540 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002541 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002542 }
2543 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002544 if (jo != NULL)
2545 CloseHandle(jo);
2546 if (term->tl_winpty != NULL)
2547 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002548 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002549 if (term->tl_winpty_config != NULL)
2550 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002551 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002552 if (winpty_err != NULL)
2553 {
2554 char_u *msg = utf16_to_enc(
2555 (short_u *)winpty_error_msg(winpty_err), NULL);
2556
2557 EMSG(msg);
2558 winpty_error_free(winpty_err);
2559 }
2560 return FAIL;
2561}
2562
2563/*
2564 * Free the terminal emulator part of "term".
2565 */
2566 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002567term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002568{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002569 if (term->tl_winpty != NULL)
2570 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002571 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002572 if (term->tl_winpty_config != NULL)
2573 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002574 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002575 if (term->tl_vterm != NULL)
2576 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002577 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002578}
2579
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002580/*
2581 * Request size to terminal.
2582 */
2583 static void
2584term_report_winsize(term_T *term, int rows, int cols)
2585{
2586 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2587}
2588
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002589# else
2590
2591/**************************************
2592 * 3. Unix-like implementation.
2593 */
2594
2595/*
2596 * Create a new terminal of "rows" by "cols" cells.
2597 * Start job for "cmd".
2598 * Store the pointers in "term".
2599 * Return OK or FAIL.
2600 */
2601 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002602term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002603{
2604 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002605
2606 create_vterm(term, rows, cols);
2607
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002608 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002609 argvars[0].v_type = VAR_STRING;
2610 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002611
2612 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002613 if (term->tl_job != NULL)
2614 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002615
Bram Moolenaar61a66052017-07-22 18:39:00 +02002616 return term->tl_job != NULL
2617 && term->tl_job->jv_channel != NULL
2618 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002619}
2620
2621/*
2622 * Free the terminal emulator part of "term".
2623 */
2624 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002625term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002626{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002627 if (term->tl_vterm != NULL)
2628 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002629 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002630}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002631
2632/*
2633 * Request size to terminal.
2634 */
2635 static void
2636term_report_winsize(term_T *term, int rows, int cols)
2637{
2638 /* Use an ioctl() to report the new window size to the job. */
2639 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2640 {
2641 int fd = -1;
2642 int part;
2643
2644 for (part = PART_OUT; part < PART_COUNT; ++part)
2645 {
2646 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2647 if (isatty(fd))
2648 break;
2649 }
2650 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2651 mch_stop_job(term->tl_job, (char_u *)"winch");
2652 }
2653}
2654
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002655# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002656
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002657#endif /* FEAT_TERMINAL */