blob: 32f2266a787660a2fa6f6549413aeb37a272b74e [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
46 * - make term_getcursor() return type (none/block/bar/underline) and
47 * attributes (color, blink, etc.)
Bram Moolenaar292d5692017-08-08 21:52:22 +020048 * - MS-Windows: no redraw for 'updatetime' #1915
49 * - term_getline() and term_scrape() don't work once the job exited. Use the
50 * buffer and scrollback, remembering the topline from when the job exited.
Bram Moolenaard85f2712017-07-28 21:51:57 +020051 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020052 * For the GUI fill termios with default values, perhaps like pangoterm:
53 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
54 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020055 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020056 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020057 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020058 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020059 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020060 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
61 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020062 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020063 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020064 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020065 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020066 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020067 * terminal. Allow:
68 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
69 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
70 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
71 * Check that something is connected to the terminal.
72 * Test: "cat" reading from a file or buffer
73 * "ls" writing stdout to a file or buffer
74 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020075 * - support ":term NONE" to open a terminal with a pty but not running a job
76 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020077 * - if the job in the terminal does not support the mouse, we can use the
78 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020079 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
80 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020081 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020082 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020083 * - Copy text in the vterm to the Vim buffer once in a while, so that
84 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020085 */
86
87#include "vim.h"
88
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020089#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020090
Bram Moolenaard5310982017-08-05 15:16:32 +020091#ifndef MIN
92# define MIN(x,y) ((x) < (y) ? (x) : (y))
93#endif
94#ifndef MAX
95# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020096#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020097
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020098#include "libvterm/include/vterm.h"
99
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200100/* This is VTermScreenCell without the characters, thus much smaller. */
101typedef struct {
102 VTermScreenCellAttrs attrs;
103 char width;
104 VTermColor fg, bg;
105} cellattr_T;
106
Bram Moolenaard85f2712017-07-28 21:51:57 +0200107typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200108 int sb_cols; /* can differ per line */
109 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200110} sb_line_T;
111
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200112/* typedef term_T in structs.h */
113struct terminal_S {
114 term_T *tl_next;
115
Bram Moolenaar423802d2017-07-30 16:52:24 +0200116 VTerm *tl_vterm;
117 job_T *tl_job;
118 buf_T *tl_buffer;
119
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200120 /* used when tl_job is NULL and only a pty was created */
121 int tl_tty_fd;
122 char_u *tl_tty_name;
123
Bram Moolenaar6d819742017-08-06 14:57:49 +0200124 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200125 int tl_channel_closed;
126
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200127#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200128 void *tl_winpty_config;
129 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200130#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200131
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200132 /* last known vterm size */
133 int tl_rows;
134 int tl_cols;
135 /* vterm size does not follow window size */
136 int tl_rows_fixed;
137 int tl_cols_fixed;
138
Bram Moolenaar21554412017-07-24 21:44:43 +0200139 char_u *tl_title; /* NULL or allocated */
140 char_u *tl_status_text; /* NULL or allocated */
141
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200142 /* Range of screen rows to update. Zero based. */
143 int tl_dirty_row_start; /* -1 if nothing dirty */
144 int tl_dirty_row_end; /* row below last one to update */
145
Bram Moolenaard85f2712017-07-28 21:51:57 +0200146 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200147 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200148
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200149 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200150 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200151};
152
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200153#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
154#define TMODE_LOOP 2 /* CTRL-W N used */
155
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200156/*
157 * List of all active terminals.
158 */
159static term_T *first_term = NULL;
160
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200161
162#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
163#define KEY_BUF_LEN 200
164
165/*
166 * Functions with separate implementation for MS-Windows and Unix-like systems.
167 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200168static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200169static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200170static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200171
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200172/**************************************
173 * 1. Generic code for all systems.
174 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200175
176/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200177 * Determine the terminal size from 'termsize' and the current window.
178 * Assumes term->tl_rows and term->tl_cols are zero.
179 */
180 static void
181set_term_and_win_size(term_T *term)
182{
183 if (*curwin->w_p_tms != NUL)
184 {
185 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
186
187 term->tl_rows = atoi((char *)curwin->w_p_tms);
188 term->tl_cols = atoi((char *)p);
189 }
190 if (term->tl_rows == 0)
191 term->tl_rows = curwin->w_height;
192 else
193 {
194 win_setheight_win(term->tl_rows, curwin);
195 term->tl_rows_fixed = TRUE;
196 }
197 if (term->tl_cols == 0)
198 term->tl_cols = curwin->w_width;
199 else
200 {
201 win_setwidth_win(term->tl_cols, curwin);
202 term->tl_cols_fixed = TRUE;
203 }
204}
205
206/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200207 * Initialize job options for a terminal job.
208 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200209 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200210 static void
211init_job_options(jobopt_T *opt)
212{
213 clear_job_options(opt);
214
215 opt->jo_mode = MODE_RAW;
216 opt->jo_out_mode = MODE_RAW;
217 opt->jo_err_mode = MODE_RAW;
218 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
219
220 opt->jo_io[PART_OUT] = JIO_BUFFER;
221 opt->jo_io[PART_ERR] = JIO_BUFFER;
222 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
223
224 opt->jo_modifiable[PART_OUT] = 0;
225 opt->jo_modifiable[PART_ERR] = 0;
226 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
227
228 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
229}
230
231/*
232 * Set job options mandatory for a terminal job.
233 */
234 static void
235setup_job_options(jobopt_T *opt, int rows, int cols)
236{
237 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
238 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
239 opt->jo_pty = TRUE;
240 opt->jo_term_rows = rows;
241 opt->jo_term_cols = cols;
242}
243
244 static void
245term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200246{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247 exarg_T split_ea;
248 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250
251 if (check_restricted() || check_secure())
252 return;
253
254 term = (term_T *)alloc_clear(sizeof(term_T));
255 if (term == NULL)
256 return;
257 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200258 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200259 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200260
261 /* Open a new window or tab. */
262 vim_memset(&split_ea, 0, sizeof(split_ea));
263 split_ea.cmdidx = CMD_new;
264 split_ea.cmd = (char_u *)"new";
265 split_ea.arg = (char_u *)"";
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200266 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
267 {
268 split_ea.line2 = opt->jo_term_rows;
269 split_ea.addr_count = 1;
270 }
271 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
272 {
273 split_ea.line2 = opt->jo_term_cols;
274 split_ea.addr_count = 1;
275 }
276
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200277 ex_splitview(&split_ea);
278 if (curwin == old_curwin)
279 {
280 /* split failed */
281 vim_free(term);
282 return;
283 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200284 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200285 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200286
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200287 /* only one size was taken care of with :new, do the other one */
288 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
289 win_setheight(opt->jo_term_rows);
290 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
291 win_setwidth(opt->jo_term_cols);
292
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200293 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200294 term->tl_next = first_term;
295 first_term = term;
296
Bram Moolenaar293424c2017-07-26 23:11:01 +0200297 if (cmd == NULL || *cmd == NUL)
298 cmd = p_sh;
299
Bram Moolenaar78712a72017-08-05 14:50:12 +0200300 if (opt->jo_term_name != NULL)
301 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
302 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200303 {
304 int i;
305 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200306 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200307
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200308 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200309 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200310 /* Prepend a ! to the command name to avoid the buffer name equals
311 * the executable, otherwise ":w!" would overwrite it. */
312 if (i == 0)
313 vim_snprintf((char *)p, len, "!%s", cmd);
314 else
315 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200316 if (buflist_findname(p) == NULL)
317 {
318 curbuf->b_ffname = p;
319 break;
320 }
321 }
322 }
323 curbuf->b_fname = curbuf->b_ffname;
324
Bram Moolenaareb44a682017-08-03 22:44:55 +0200325 set_string_option_direct((char_u *)"buftype", -1,
326 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
327
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200328 /* Mark the buffer as not modifiable. It can only be made modifiable after
329 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200330 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200331
332 /* Set 'bufhidden' to "hide": allow closing the window. */
333 set_string_option_direct((char_u *)"bufhidden", -1,
334 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200335
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200336 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200337 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200338
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200339 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200340 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200341 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200342 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200343 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200344 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200345 }
346 else
347 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200348 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200349
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200350 /* Wiping out the buffer will also close the window and call
351 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200352 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200353 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200354}
355
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200356/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200357 * ":terminal": open a terminal window and execute a job in it.
358 */
359 void
360ex_terminal(exarg_T *eap)
361{
362 jobopt_T opt;
363
364 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200365
366 if (eap->addr_count == 2)
367 {
368 opt.jo_term_rows = eap->line1;
369 opt.jo_term_cols = eap->line2;
370 }
371 else if (eap->addr_count == 1)
372 {
373 if (cmdmod.split & WSP_VERT)
374 opt.jo_term_cols = eap->line2;
375 else
376 opt.jo_term_rows = eap->line2;
377 }
378 /* TODO: get more options from before the command */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200379
380 term_start(eap->arg, &opt);
381}
382
383/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200384 * Free the scrollback buffer for "term".
385 */
386 static void
387free_scrollback(term_T *term)
388{
389 int i;
390
391 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
392 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
393 ga_clear(&term->tl_scrollback);
394}
395
396/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200397 * Free a terminal and everything it refers to.
398 * Kills the job if there is one.
399 * Called when wiping out a buffer.
400 */
401 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200402free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200403{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200404 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200405 term_T *tp;
406
407 if (term == NULL)
408 return;
409 if (first_term == term)
410 first_term = term->tl_next;
411 else
412 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
413 if (tp->tl_next == term)
414 {
415 tp->tl_next = term->tl_next;
416 break;
417 }
418
419 if (term->tl_job != NULL)
420 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200421 if (term->tl_job->jv_status != JOB_ENDED
422 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200423 job_stop(term->tl_job, NULL, "kill");
424 job_unref(term->tl_job);
425 }
426
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200427 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200428
429 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200430 vim_free(term->tl_title);
431 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200432 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200433 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200434}
435
436/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200437 * Write job output "msg[len]" to the vterm.
438 */
439 static void
440term_write_job_output(term_T *term, char_u *msg, size_t len)
441{
442 VTerm *vterm = term->tl_vterm;
443 char_u *p;
444 size_t done;
445 size_t len_now;
446
447 for (done = 0; done < len; done += len_now)
448 {
449 for (p = msg + done; p < msg + len; )
450 {
451 if (*p == NL)
452 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200453 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200454 }
455 len_now = p - msg - done;
456 vterm_input_write(vterm, (char *)msg + done, len_now);
457 if (p < msg + len && *p == NL)
458 {
459 /* Convert NL to CR-NL, that appears to work best. */
460 vterm_input_write(vterm, "\r\n", 2);
461 ++len_now;
462 }
463 }
464
465 /* this invokes the damage callbacks */
466 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
467}
468
Bram Moolenaar1c844932017-07-24 23:36:41 +0200469 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200470update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200471{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200472 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200473 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200474 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200475 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200476 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200477 if (term->tl_cursor_visible)
478 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200479 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200480#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200481 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200482 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200483#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200484 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200485}
486
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200487/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200488 * Invoked when "msg" output from a job was received. Write it to the terminal
489 * of "buffer".
490 */
491 void
492write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
493{
494 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200495 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200496
Bram Moolenaard85f2712017-07-28 21:51:57 +0200497 if (term->tl_vterm == NULL)
498 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200499 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200500 return;
501 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200502 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200503 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200504
Bram Moolenaar6d819742017-08-06 14:57:49 +0200505 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200506 {
507 /* TODO: only update once in a while. */
508 update_screen(0);
509 update_cursor(term, TRUE);
510 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200511}
512
513/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200514 * Send a mouse position and click to the vterm
515 */
516 static int
517term_send_mouse(VTerm *vterm, int button, int pressed)
518{
519 VTermModifier mod = VTERM_MOD_NONE;
520
521 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
522 mouse_col - W_WINCOL(curwin), mod);
523 vterm_mouse_button(vterm, button, pressed, mod);
524 return TRUE;
525}
526
527/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200528 * Convert typed key "c" into bytes to send to the job.
529 * Return the number of bytes in "buf".
530 */
531 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200532term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200533{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200534 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200535 VTermKey key = VTERM_KEY_NONE;
536 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200537 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200538
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200539 switch (c)
540 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200541 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200542 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200543 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
544 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200545 case K_DEL: key = VTERM_KEY_DEL; break;
546 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200547 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
548 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200549 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200550 case K_S_END: mod = VTERM_MOD_SHIFT;
551 key = VTERM_KEY_END; break;
552 case K_C_END: mod = VTERM_MOD_CTRL;
553 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200554 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
555 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
556 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
557 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
558 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
559 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
560 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
561 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
562 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
563 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
564 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
565 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
566 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200567 case K_S_HOME: mod = VTERM_MOD_SHIFT;
568 key = VTERM_KEY_HOME; break;
569 case K_C_HOME: mod = VTERM_MOD_CTRL;
570 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200571 case K_INS: key = VTERM_KEY_INS; break;
572 case K_K0: key = VTERM_KEY_KP_0; break;
573 case K_K1: key = VTERM_KEY_KP_1; break;
574 case K_K2: key = VTERM_KEY_KP_2; break;
575 case K_K3: key = VTERM_KEY_KP_3; break;
576 case K_K4: key = VTERM_KEY_KP_4; break;
577 case K_K5: key = VTERM_KEY_KP_5; break;
578 case K_K6: key = VTERM_KEY_KP_6; break;
579 case K_K7: key = VTERM_KEY_KP_7; break;
580 case K_K8: key = VTERM_KEY_KP_8; break;
581 case K_K9: key = VTERM_KEY_KP_9; break;
582 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
583 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
584 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
585 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
586 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
587 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
588 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
589 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
590 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
591 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
592 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
593 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
594 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200595 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
596 key = VTERM_KEY_LEFT; break;
597 case K_C_LEFT: mod = VTERM_MOD_CTRL;
598 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200599 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
600 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
601 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200602 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
603 key = VTERM_KEY_RIGHT; break;
604 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
605 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200606 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200607 case K_S_UP: mod = VTERM_MOD_SHIFT;
608 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200609 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200610
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200611 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
612 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
613 case K_MOUSELEFT: /* TODO */ return 0;
614 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200615
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200616 case K_LEFTMOUSE:
617 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
618 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
619 case K_LEFTRELEASE:
620 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
621 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
622 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
623 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
624 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
625 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
626 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
627 case K_X1MOUSE: /* TODO */ return 0;
628 case K_X1DRAG: /* TODO */ return 0;
629 case K_X1RELEASE: /* TODO */ return 0;
630 case K_X2MOUSE: /* TODO */ return 0;
631 case K_X2DRAG: /* TODO */ return 0;
632 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200633
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200634 case K_IGNORE: return 0;
635 case K_NOP: return 0;
636 case K_UNDO: return 0;
637 case K_HELP: return 0;
638 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
639 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
640 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
641 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
642 case K_SELECT: return 0;
643#ifdef FEAT_GUI
644 case K_VER_SCROLLBAR: return 0;
645 case K_HOR_SCROLLBAR: return 0;
646#endif
647#ifdef FEAT_GUI_TABLINE
648 case K_TABLINE: return 0;
649 case K_TABMENU: return 0;
650#endif
651#ifdef FEAT_NETBEANS_INTG
652 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
653#endif
654#ifdef FEAT_DND
655 case K_DROP: return 0;
656#endif
657#ifdef FEAT_AUTOCMD
658 case K_CURSORHOLD: return 0;
659#endif
660 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
661 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200662 }
663
664 /*
665 * Convert special keys to vterm keys:
666 * - Write keys to vterm: vterm_keyboard_key()
667 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200668 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200669 */
670 if (key != VTERM_KEY_NONE)
671 /* Special key, let vterm convert it. */
672 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200673 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200674 /* Normal character, let vterm convert it. */
675 vterm_keyboard_unichar(vterm, c, mod);
676
677 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200678 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200679}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200680
Bram Moolenaar938783d2017-07-16 20:13:26 +0200681/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200682 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200683 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200684 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200685term_job_running(term_T *term)
686{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200687 /* Also consider the job finished when the channel is closed, to avoid a
688 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200689 return term != NULL
690 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200691 && term->tl_job->jv_status == JOB_STARTED
692 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200693}
694
695/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200696 * Add the last line of the scrollback buffer to the buffer in the window.
697 */
698 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200699add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200700{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200701 buf_T *buf = term->tl_buffer;
702 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
703 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200704
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200705 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200706 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200707 {
708 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200709 curbuf = buf;
710 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200711 curbuf = curwin->w_buffer;
712 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200713}
714
715/*
716 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200717 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200718 */
719 static void
720move_terminal_to_buffer(term_T *term)
721{
722 win_T *wp;
723 int len;
724 int lines_skipped = 0;
725 VTermPos pos;
726 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200727 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200728 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200729
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200730 if (term->tl_vterm == NULL)
731 return;
732 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200733 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
734 {
735 len = 0;
736 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
737 if (vterm_screen_get_cell(screen, pos, &cell) != 0
738 && cell.chars[0] != NUL)
739 len = pos.col + 1;
740
741 if (len == 0)
742 ++lines_skipped;
743 else
744 {
745 while (lines_skipped > 0)
746 {
747 /* Line was skipped, add an empty line. */
748 --lines_skipped;
749 if (ga_grow(&term->tl_scrollback, 1) == OK)
750 {
751 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
752 + term->tl_scrollback.ga_len;
753
754 line->sb_cols = 0;
755 line->sb_cells = NULL;
756 ++term->tl_scrollback.ga_len;
757
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200758 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200759 }
760 }
761
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200762 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200763 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
764 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200765 garray_T ga;
766 int width;
767 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200768 + term->tl_scrollback.ga_len;
769
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200770 ga_init2(&ga, 1, 100);
771 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200772 {
773 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200774 {
775 width = 1;
776 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
777 if (ga_grow(&ga, 1) == OK)
778 ga.ga_len += mb_char2bytes(' ',
779 (char_u *)ga.ga_data + ga.ga_len);
780 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200781 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200782 {
783 width = cell.width;
784
785 p[pos.col].width = cell.width;
786 p[pos.col].attrs = cell.attrs;
787 p[pos.col].fg = cell.fg;
788 p[pos.col].bg = cell.bg;
789
790 if (ga_grow(&ga, MB_MAXBYTES) == OK)
791 {
792 int i;
793 int c;
794
795 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
796 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
797 (char_u *)ga.ga_data + ga.ga_len);
798 }
799 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200800 }
801 line->sb_cols = len;
802 line->sb_cells = p;
803 ++term->tl_scrollback.ga_len;
804
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200805 if (ga_grow(&ga, 1) == FAIL)
806 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
807 else
808 {
809 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
810 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
811 }
812 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200813 }
814 else
815 vim_free(p);
816 }
817 }
818
819 FOR_ALL_WINDOWS(wp)
820 {
821 if (wp->w_buffer == term->tl_buffer)
822 {
823 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
824 wp->w_cursor.col = 0;
825 wp->w_valid = 0;
826 redraw_win_later(wp, NOT_VALID);
827 }
828 }
829}
830
831 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200832set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200833{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200834 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200835 vim_free(term->tl_status_text);
836 term->tl_status_text = NULL;
837 if (term->tl_buffer == curbuf)
838 maketitle();
839}
840
841/*
842 * Called after the job if finished and Terminal mode is not active:
843 * Move the vterm contents into the scrollback buffer and free the vterm.
844 */
845 static void
846cleanup_vterm(term_T *term)
847{
848 move_terminal_to_buffer(term);
849 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200850 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200851}
852
853/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200854 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200855 * Suspends updating the terminal window.
856 */
857 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200858term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200859{
860 term_T *term = curbuf->b_term;
861
862 /* Append the current terminal contents to the buffer. */
863 move_terminal_to_buffer(term);
864
Bram Moolenaar6d819742017-08-06 14:57:49 +0200865 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200866
Bram Moolenaar6d819742017-08-06 14:57:49 +0200867 /* Move the window cursor to the position of the cursor in the
868 * terminal. */
869 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
870 + term->tl_cursor_pos.row + 1;
871 check_cursor();
872 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200873
Bram Moolenaar6d819742017-08-06 14:57:49 +0200874 /* Display the same lines as in the terminal. */
875 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200876}
877
878/*
879 * Returns TRUE if the current window contains a terminal and we are in
880 * Terminal-Normal mode.
881 */
882 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200883term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200884{
885 term_T *term = curbuf->b_term;
886
Bram Moolenaar6d819742017-08-06 14:57:49 +0200887 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200888}
889
890/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200891 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200892 * Restores updating the terminal window.
893 */
894 void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200895term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +0200896{
897 term_T *term = curbuf->b_term;
898 sb_line_T *line;
899 garray_T *gap;
900
901 /* Remove the terminal contents from the scrollback and the buffer. */
902 gap = &term->tl_scrollback;
903 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
904 {
905 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
906 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
907 vim_free(line->sb_cells);
908 --gap->ga_len;
909 if (gap->ga_len == 0)
910 break;
911 }
912 check_cursor();
913
Bram Moolenaar6d819742017-08-06 14:57:49 +0200914 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200915
916 if (term->tl_channel_closed)
917 cleanup_vterm(term);
918 redraw_buf_and_status_later(curbuf, NOT_VALID);
919}
920
921/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200922 * Get a key from the user without mapping.
923 * TODO: use terminal mode mappings.
924 */
925 static int
926term_vgetc()
927{
928 int c;
929
930 ++no_mapping;
931 ++allow_keys;
932 got_int = FALSE;
933 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200934 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200935 --no_mapping;
936 --allow_keys;
937 return c;
938}
939
940/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200941 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200942 * Return FAIL when the key needs to be handled in Normal mode.
943 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200944 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200945 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200946send_keys_to_term(term_T *term, int c, int typed)
947{
948 char msg[KEY_BUF_LEN];
949 size_t len;
950 static int mouse_was_outside = FALSE;
951 int dragging_outside = FALSE;
952
953 /* Catch keys that need to be handled as in Normal mode. */
954 switch (c)
955 {
956 case NUL:
957 case K_ZERO:
958 if (typed)
959 stuffcharReadbuff(c);
960 return FAIL;
961
962 case K_IGNORE:
963 return FAIL;
964
965 case K_LEFTDRAG:
966 case K_MIDDLEDRAG:
967 case K_RIGHTDRAG:
968 case K_X1DRAG:
969 case K_X2DRAG:
970 dragging_outside = mouse_was_outside;
971 /* FALLTHROUGH */
972 case K_LEFTMOUSE:
973 case K_LEFTMOUSE_NM:
974 case K_LEFTRELEASE:
975 case K_LEFTRELEASE_NM:
976 case K_MIDDLEMOUSE:
977 case K_MIDDLERELEASE:
978 case K_RIGHTMOUSE:
979 case K_RIGHTRELEASE:
980 case K_X1MOUSE:
981 case K_X1RELEASE:
982 case K_X2MOUSE:
983 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200984
985 case K_MOUSEUP:
986 case K_MOUSEDOWN:
987 case K_MOUSELEFT:
988 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200989 if (mouse_row < W_WINROW(curwin)
990 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
991 || mouse_col < W_WINCOL(curwin)
992 || mouse_col >= W_ENDCOL(curwin)
993 || dragging_outside)
994 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200995 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200996 if (typed)
997 {
998 stuffcharReadbuff(c);
999 mouse_was_outside = TRUE;
1000 }
1001 return FAIL;
1002 }
1003 }
1004 if (typed)
1005 mouse_was_outside = FALSE;
1006
1007 /* Convert the typed key to a sequence of bytes for the job. */
1008 len = term_convert_key(term, c, msg);
1009 if (len > 0)
1010 /* TODO: if FAIL is returned, stop? */
1011 channel_send(term->tl_job->jv_channel, PART_IN,
1012 (char_u *)msg, (int)len, NULL);
1013
1014 return OK;
1015}
1016
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001017 static void
1018position_cursor(win_T *wp, VTermPos *pos)
1019{
1020 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1021 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1022 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1023}
1024
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001025/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001026 * Handle CTRL-W "": send register contents to the job.
1027 */
1028 static void
1029term_paste_register(int prev_c UNUSED)
1030{
1031 int c;
1032 list_T *l;
1033 listitem_T *item;
1034 long reglen = 0;
1035 int type;
1036
1037#ifdef FEAT_CMDL_INFO
1038 if (add_to_showcmd(prev_c))
1039 if (add_to_showcmd('"'))
1040 out_flush();
1041#endif
1042 c = term_vgetc();
1043#ifdef FEAT_CMDL_INFO
1044 clear_showcmd();
1045#endif
1046
1047 /* CTRL-W "= prompt for expression to evaluate. */
1048 if (c == '=' && get_expr_register() != '=')
1049 return;
1050
1051 l = (list_T *)get_reg_contents(c, GREG_LIST);
1052 if (l != NULL)
1053 {
1054 type = get_reg_type(c, &reglen);
1055 for (item = l->lv_first; item != NULL; item = item->li_next)
1056 {
1057 char_u *s = get_tv_string(&item->li_tv);
1058
1059 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1060 s, STRLEN(s), NULL);
1061 if (item->li_next != NULL || type == MLINE)
1062 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1063 (char_u *)"\r", 1, NULL);
1064 }
1065 list_free(l);
1066 }
1067}
1068
1069/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001070 * Returns TRUE if the current window contains a terminal and we are sending
1071 * keys to the job.
1072 */
1073 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001074term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001075{
1076 term_T *term = curbuf->b_term;
1077
1078 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001079 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001080 && term->tl_vterm != NULL
1081 && term_job_running(term);
1082}
1083
1084/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001085 * Wait for input and send it to the job.
1086 * Return when the start of a CTRL-W command is typed or anything else that
1087 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001088 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1089 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001090 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001091 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001092terminal_loop(void)
1093{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001094 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001095 int termkey = 0;
1096
1097 if (*curwin->w_p_tk != NUL)
1098 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001099 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001100
1101 for (;;)
1102 {
1103 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001104 /* Repeat redrawing in case a message is received while redrawing. */
1105 while (curwin->w_redr_type != 0)
1106 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001107 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001108
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001109 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001110 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001111 /* job finished while waiting for a character */
1112 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001113
Bram Moolenaarfae42832017-08-01 22:24:26 +02001114#ifdef UNIX
1115 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1116#endif
1117#ifdef WIN3264
1118 if (c == Ctrl_C)
1119 /* We don't know if the job can handle CTRL-C itself or not, this
1120 * may kill the shell instead of killing the command running in the
1121 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001122 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001123#endif
1124
Bram Moolenaar69198192017-08-05 14:10:48 +02001125 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001126 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001127 int prev_c = c;
1128
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001129#ifdef FEAT_CMDL_INFO
1130 if (add_to_showcmd(c))
1131 out_flush();
1132#endif
1133 c = term_vgetc();
1134#ifdef FEAT_CMDL_INFO
1135 clear_showcmd();
1136#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001137 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001138 /* job finished while waiting for a character */
1139 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001140
Bram Moolenaar69198192017-08-05 14:10:48 +02001141 if (prev_c == Ctrl_BSL)
1142 {
1143 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001144 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001145 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1146 term_enter_normal_mode();
1147 return FAIL;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001148 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001149 /* Send both keys to the terminal. */
1150 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1151 }
1152 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001153 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001154 /* "CTRL-W .": send CTRL-W to the job */
1155 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001156 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001157 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001158 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001159 /* CTRL-W N : go to Terminal-Normal mode. */
1160 term_enter_normal_mode();
Bram Moolenaar423802d2017-07-30 16:52:24 +02001161 return FAIL;
1162 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001163 else if (c == '"')
1164 {
1165 term_paste_register(prev_c);
1166 continue;
1167 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001168 else if (termkey == 0 || c != termkey)
1169 {
1170 stuffcharReadbuff(Ctrl_W);
1171 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001172 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001173 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001174 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001175 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1176 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001177 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001178 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001179}
1180
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001181/*
1182 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001183 * This updates the title and status, but does not close the vter, because
1184 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001185 */
1186 void
1187term_job_ended(job_T *job)
1188{
1189 term_T *term;
1190 int did_one = FALSE;
1191
1192 for (term = first_term; term != NULL; term = term->tl_next)
1193 if (term->tl_job == job)
1194 {
1195 vim_free(term->tl_title);
1196 term->tl_title = NULL;
1197 vim_free(term->tl_status_text);
1198 term->tl_status_text = NULL;
1199 redraw_buf_and_status_later(term->tl_buffer, VALID);
1200 did_one = TRUE;
1201 }
1202 if (did_one)
1203 redraw_statuslines();
1204 if (curbuf->b_term != NULL)
1205 {
1206 if (curbuf->b_term->tl_job == job)
1207 maketitle();
1208 update_cursor(curbuf->b_term, TRUE);
1209 }
1210}
1211
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001212 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001213may_toggle_cursor(term_T *term)
1214{
1215 if (curbuf == term->tl_buffer)
1216 {
1217 if (term->tl_cursor_visible)
1218 cursor_on();
1219 else
1220 cursor_off();
1221 }
1222}
1223
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001224 static int
1225handle_damage(VTermRect rect, void *user)
1226{
1227 term_T *term = (term_T *)user;
1228
1229 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1230 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1231 redraw_buf_later(term->tl_buffer, NOT_VALID);
1232 return 1;
1233}
1234
1235 static int
1236handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1237{
1238 term_T *term = (term_T *)user;
1239
1240 /* TODO */
1241 redraw_buf_later(term->tl_buffer, NOT_VALID);
1242 return 1;
1243}
1244
1245 static int
1246handle_movecursor(
1247 VTermPos pos,
1248 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001249 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001250 void *user)
1251{
1252 term_T *term = (term_T *)user;
1253 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001254
1255 term->tl_cursor_pos = pos;
1256 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001257
1258 FOR_ALL_WINDOWS(wp)
1259 {
1260 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001261 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001262 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001263 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001264 {
1265 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001266 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001267 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001268
1269 return 1;
1270}
1271
Bram Moolenaar21554412017-07-24 21:44:43 +02001272 static int
1273handle_settermprop(
1274 VTermProp prop,
1275 VTermValue *value,
1276 void *user)
1277{
1278 term_T *term = (term_T *)user;
1279
1280 switch (prop)
1281 {
1282 case VTERM_PROP_TITLE:
1283 vim_free(term->tl_title);
1284 term->tl_title = vim_strsave((char_u *)value->string);
1285 vim_free(term->tl_status_text);
1286 term->tl_status_text = NULL;
1287 if (term == curbuf->b_term)
1288 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001289 break;
1290
1291 case VTERM_PROP_CURSORVISIBLE:
1292 term->tl_cursor_visible = value->boolean;
1293 may_toggle_cursor(term);
1294 out_flush();
1295 break;
1296
Bram Moolenaar21554412017-07-24 21:44:43 +02001297 default:
1298 break;
1299 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001300 /* Always return 1, otherwise vterm doesn't store the value internally. */
1301 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001302}
1303
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001304/*
1305 * The job running in the terminal resized the terminal.
1306 */
1307 static int
1308handle_resize(int rows, int cols, void *user)
1309{
1310 term_T *term = (term_T *)user;
1311 win_T *wp;
1312
1313 term->tl_rows = rows;
1314 term->tl_cols = cols;
1315 FOR_ALL_WINDOWS(wp)
1316 {
1317 if (wp->w_buffer == term->tl_buffer)
1318 {
1319 win_setheight_win(rows, wp);
1320 win_setwidth_win(cols, wp);
1321 }
1322 }
1323
1324 redraw_buf_later(term->tl_buffer, NOT_VALID);
1325 return 1;
1326}
1327
Bram Moolenaard85f2712017-07-28 21:51:57 +02001328/*
1329 * Handle a line that is pushed off the top of the screen.
1330 */
1331 static int
1332handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1333{
1334 term_T *term = (term_T *)user;
1335
1336 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001337 if (ga_grow(&term->tl_scrollback, 1) == OK)
1338 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001339 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001340 int len = 0;
1341 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001342 int c;
1343 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001344 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001345 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001346
1347 /* do not store empty cells at the end */
1348 for (i = 0; i < cols; ++i)
1349 if (cells[i].chars[0] != 0)
1350 len = i + 1;
1351
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001352 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001353 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001354 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001355 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001356 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001357 for (col = 0; col < len; col += cells[col].width)
1358 {
1359 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1360 {
1361 ga.ga_len = 0;
1362 break;
1363 }
1364 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1365 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1366 (char_u *)ga.ga_data + ga.ga_len);
1367 p[col].width = cells[col].width;
1368 p[col].attrs = cells[col].attrs;
1369 p[col].fg = cells[col].fg;
1370 p[col].bg = cells[col].bg;
1371 }
1372 }
1373 if (ga_grow(&ga, 1) == FAIL)
1374 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1375 else
1376 {
1377 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1378 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1379 }
1380 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001381
1382 line = (sb_line_T *)term->tl_scrollback.ga_data
1383 + term->tl_scrollback.ga_len;
1384 line->sb_cols = len;
1385 line->sb_cells = p;
1386 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001387 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001388 }
1389 return 0; /* ignored */
1390}
1391
Bram Moolenaar21554412017-07-24 21:44:43 +02001392static VTermScreenCallbacks screen_callbacks = {
1393 handle_damage, /* damage */
1394 handle_moverect, /* moverect */
1395 handle_movecursor, /* movecursor */
1396 handle_settermprop, /* settermprop */
1397 NULL, /* bell */
1398 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001399 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001400 NULL /* sb_popline */
1401};
1402
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001403/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001404 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001405 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001406 */
1407 void
1408term_channel_closed(channel_T *ch)
1409{
1410 term_T *term;
1411 int did_one = FALSE;
1412
1413 for (term = first_term; term != NULL; term = term->tl_next)
1414 if (term->tl_job == ch->ch_job)
1415 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001416 term->tl_channel_closed = TRUE;
1417
Bram Moolenaard85f2712017-07-28 21:51:57 +02001418 vim_free(term->tl_title);
1419 term->tl_title = NULL;
1420 vim_free(term->tl_status_text);
1421 term->tl_status_text = NULL;
1422
Bram Moolenaar423802d2017-07-30 16:52:24 +02001423 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001424 if (!term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001425 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001426
1427 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1428 did_one = TRUE;
1429 }
1430 if (did_one)
1431 {
1432 redraw_statuslines();
1433
1434 /* Need to break out of vgetc(). */
1435 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001436 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001437
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001438 term = curbuf->b_term;
1439 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001440 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001441 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001442 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001443 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001444 }
1445 }
1446}
1447
1448/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001449 * Reverse engineer the RGB value into a cterm color index.
1450 * First color is 1. Return 0 if no match found.
1451 */
1452 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001453color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001454{
1455 int red = color->red;
1456 int blue = color->blue;
1457 int green = color->green;
1458
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001459 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001460 if (red == 0)
1461 {
1462 if (green == 0)
1463 {
1464 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001465 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001466 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001467 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001468 }
1469 else if (green == 224)
1470 {
1471 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001472 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001473 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001474 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001475 }
1476 }
1477 else if (red == 224)
1478 {
1479 if (green == 0)
1480 {
1481 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001482 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001483 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001484 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001485 }
1486 else if (green == 224)
1487 {
1488 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001489 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001490 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001491 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001492 }
1493 }
1494 else if (red == 128)
1495 {
1496 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001497 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001498 }
1499 else if (red == 255)
1500 {
1501 if (green == 64)
1502 {
1503 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001504 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001505 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001506 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001507 }
1508 else if (green == 255)
1509 {
1510 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001511 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001512 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001513 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001514 }
1515 }
1516 else if (red == 64)
1517 {
1518 if (green == 64)
1519 {
1520 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001521 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001522 }
1523 else if (green == 255)
1524 {
1525 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001526 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001527 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001528 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001529 }
1530 }
1531 if (t_colors >= 256)
1532 {
1533 if (red == blue && red == green)
1534 {
1535 /* 24-color greyscale */
1536 static int cutoff[23] = {
1537 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1538 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1539 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1540 int i;
1541
1542 for (i = 0; i < 23; ++i)
1543 if (red < cutoff[i])
1544 return i + 233;
1545 return 256;
1546 }
1547
1548 /* 216-color cube */
1549 return 17 + ((red + 25) / 0x33) * 36
1550 + ((green + 25) / 0x33) * 6
1551 + (blue + 25) / 0x33;
1552 }
1553 return 0;
1554}
1555
1556/*
1557 * Convert the attributes of a vterm cell into an attribute index.
1558 */
1559 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001560cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001561{
1562 int attr = 0;
1563
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001564 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001565 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001566 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001567 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001568 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001569 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001570 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001571 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001572 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001573 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001574
1575#ifdef FEAT_GUI
1576 if (gui.in_use)
1577 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001578 guicolor_T fg, bg;
1579
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001580 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1581 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001582 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001583 }
1584 else
1585#endif
1586#ifdef FEAT_TERMGUICOLORS
1587 if (p_tgc)
1588 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001589 guicolor_T fg, bg;
1590
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001591 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1592 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001593
1594 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001595 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001596 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001597#endif
1598 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001599 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001600 int fg = color2index(&cellfg, TRUE, &bold);
1601 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001602
1603 /* with 8 colors set the bold attribute to get a bright foreground */
1604 if (bold == TRUE)
1605 attr |= HL_BOLD;
1606 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001607 }
1608 return 0;
1609}
1610
1611/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001612 * Called to update a window that contains an active terminal.
1613 * Returns FAIL when there is no terminal running in this window or in
1614 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001615 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001616 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001617term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001618{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001619 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001620 VTerm *vterm;
1621 VTermScreen *screen;
1622 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001623 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001624
Bram Moolenaar6d819742017-08-06 14:57:49 +02001625 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001626 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001627
Bram Moolenaard85f2712017-07-28 21:51:57 +02001628 vterm = term->tl_vterm;
1629 screen = vterm_obtain_screen(vterm);
1630 state = vterm_obtain_state(vterm);
1631
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001632 /*
1633 * If the window was resized a redraw will be triggered and we get here.
1634 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1635 */
1636 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1637 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001638 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001639 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1640 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1641 win_T *twp;
1642
1643 FOR_ALL_WINDOWS(twp)
1644 {
1645 /* When more than one window shows the same terminal, use the
1646 * smallest size. */
1647 if (twp->w_buffer == term->tl_buffer)
1648 {
1649 if (!term->tl_rows_fixed && rows > twp->w_height)
1650 rows = twp->w_height;
1651 if (!term->tl_cols_fixed && cols > twp->w_width)
1652 cols = twp->w_width;
1653 }
1654 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001655
1656 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001657 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001658 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001659 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001660 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001661
1662 /* The cursor may have been moved when resizing. */
1663 vterm_state_get_cursorpos(state, &pos);
1664 position_cursor(wp, &pos);
1665
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001666 /* TODO: Only redraw what changed. */
1667 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001668 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001669 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001670 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001671
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001672 if (pos.row < term->tl_rows)
1673 {
1674 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001675 {
1676 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001677 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001678
Bram Moolenaareeac6772017-07-23 15:48:37 +02001679 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1680 vim_memset(&cell, 0, sizeof(cell));
1681
1682 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001683 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001684 if (c == NUL)
1685 {
1686 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001687#if defined(FEAT_MBYTE)
1688 if (enc_utf8)
1689 ScreenLinesUC[off] = NUL;
1690#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001691 }
1692 else
1693 {
1694#if defined(FEAT_MBYTE)
1695 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001696 {
1697 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001698 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001699 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001700 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001701 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001702 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001703 if (enc_utf8)
1704 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001705 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001706#else
1707 ScreenLines[off] = c;
1708#endif
1709 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001710 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001711
1712 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001713 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001714 if (cell.width == 2)
1715 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001716 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001717#if defined(FEAT_MBYTE)
1718 if (enc_utf8)
1719 ScreenLinesUC[off] = NUL;
1720#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001721 ++pos.col;
1722 ++off;
1723 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001724 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001725 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001726 else
1727 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001728
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001729 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1730 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001731 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001732
1733 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001734}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001735
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001736/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001737 * Return TRUE if "wp" is a terminal window where the job has finished.
1738 */
1739 int
1740term_is_finished(buf_T *buf)
1741{
1742 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1743}
1744
1745/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001746 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02001747 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001748 */
1749 int
1750term_show_buffer(buf_T *buf)
1751{
1752 term_T *term = buf->b_term;
1753
Bram Moolenaar6d819742017-08-06 14:57:49 +02001754 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001755}
1756
1757/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001758 * The current buffer is going to be changed. If there is terminal
1759 * highlighting remove it now.
1760 */
1761 void
1762term_change_in_curbuf(void)
1763{
1764 term_T *term = curbuf->b_term;
1765
1766 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1767 {
1768 free_scrollback(term);
1769 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001770
1771 /* The buffer is now like a normal buffer, it cannot be easily
1772 * abandoned when changed. */
1773 set_string_option_direct((char_u *)"buftype", -1,
1774 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001775 }
1776}
1777
1778/*
1779 * Get the screen attribute for a position in the buffer.
1780 */
1781 int
1782term_get_attr(buf_T *buf, linenr_T lnum, int col)
1783{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001784 term_T *term = buf->b_term;
1785 sb_line_T *line;
1786 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001787
Bram Moolenaar70229f92017-07-29 16:01:53 +02001788 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001789 return 0;
1790 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1791 if (col >= line->sb_cols)
1792 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001793 cellattr = line->sb_cells + col;
1794 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001795}
1796
1797/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001798 * Create a new vterm and initialize it.
1799 */
1800 static void
1801create_vterm(term_T *term, int rows, int cols)
1802{
1803 VTerm *vterm;
1804 VTermScreen *screen;
1805
1806 vterm = vterm_new(rows, cols);
1807 term->tl_vterm = vterm;
1808 screen = vterm_obtain_screen(vterm);
1809 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1810 /* TODO: depends on 'encoding'. */
1811 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001812
1813 /* Vterm uses a default black background. Set it to white when
1814 * 'background' is "light". */
1815 if (*p_bg == 'l')
1816 {
1817 VTermColor fg, bg;
1818
1819 fg.red = fg.green = fg.blue = 0;
1820 bg.red = bg.green = bg.blue = 255;
1821 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1822 }
1823
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001824 /* Required to initialize most things. */
1825 vterm_screen_reset(screen, 1 /* hard */);
1826}
1827
Bram Moolenaar21554412017-07-24 21:44:43 +02001828/*
1829 * Return the text to show for the buffer name and status.
1830 */
1831 char_u *
1832term_get_status_text(term_T *term)
1833{
1834 if (term->tl_status_text == NULL)
1835 {
1836 char_u *txt;
1837 size_t len;
1838
Bram Moolenaar6d819742017-08-06 14:57:49 +02001839 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001840 {
1841 if (term_job_running(term))
1842 txt = (char_u *)_("Terminal");
1843 else
1844 txt = (char_u *)_("Terminal-finished");
1845 }
1846 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001847 txt = term->tl_title;
1848 else if (term_job_running(term))
1849 txt = (char_u *)_("running");
1850 else
1851 txt = (char_u *)_("finished");
1852 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001853 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001854 if (term->tl_status_text != NULL)
1855 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1856 term->tl_buffer->b_fname, txt);
1857 }
1858 return term->tl_status_text;
1859}
1860
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001861/*
1862 * Mark references in jobs of terminals.
1863 */
1864 int
1865set_ref_in_term(int copyID)
1866{
1867 int abort = FALSE;
1868 term_T *term;
1869 typval_T tv;
1870
1871 for (term = first_term; term != NULL; term = term->tl_next)
1872 if (term->tl_job != NULL)
1873 {
1874 tv.v_type = VAR_JOB;
1875 tv.vval.v_job = term->tl_job;
1876 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1877 }
1878 return abort;
1879}
1880
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001881/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001882 * Get the buffer from the first argument in "argvars".
1883 * Returns NULL when the buffer is not for a terminal window.
1884 */
1885 static buf_T *
1886term_get_buf(typval_T *argvars)
1887{
1888 buf_T *buf;
1889
1890 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1891 ++emsg_off;
1892 buf = get_buf_tv(&argvars[0], FALSE);
1893 --emsg_off;
1894 if (buf == NULL || buf->b_term == NULL)
1895 return NULL;
1896 return buf;
1897}
1898
1899/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001900 * "term_getattr(attr, name)" function
1901 */
1902 void
1903f_term_getattr(typval_T *argvars, typval_T *rettv)
1904{
1905 int attr;
1906 size_t i;
1907 char_u *name;
1908
1909 static struct {
1910 char *name;
1911 int attr;
1912 } attrs[] = {
1913 {"bold", HL_BOLD},
1914 {"italic", HL_ITALIC},
1915 {"underline", HL_UNDERLINE},
1916 {"strike", HL_STANDOUT},
1917 {"reverse", HL_INVERSE},
1918 };
1919
1920 attr = get_tv_number(&argvars[0]);
1921 name = get_tv_string_chk(&argvars[1]);
1922 if (name == NULL)
1923 return;
1924
1925 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1926 if (STRCMP(name, attrs[i].name) == 0)
1927 {
1928 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1929 break;
1930 }
1931}
1932
1933/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001934 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001935 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001936 void
1937f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001938{
Bram Moolenaar97870002017-07-30 18:28:38 +02001939 buf_T *buf = term_get_buf(argvars);
1940 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001941
Bram Moolenaar97870002017-07-30 18:28:38 +02001942 if (rettv_list_alloc(rettv) == FAIL)
1943 return;
1944 if (buf == NULL)
1945 return;
1946
1947 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001948 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1949 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001950 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001951}
1952
1953/*
1954 * "term_getjob(buf)" function
1955 */
1956 void
1957f_term_getjob(typval_T *argvars, typval_T *rettv)
1958{
1959 buf_T *buf = term_get_buf(argvars);
1960
1961 rettv->v_type = VAR_JOB;
1962 rettv->vval.v_job = NULL;
1963 if (buf == NULL)
1964 return;
1965
1966 rettv->vval.v_job = buf->b_term->tl_job;
1967 if (rettv->vval.v_job != NULL)
1968 ++rettv->vval.v_job->jv_refcount;
1969}
1970
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001971 static int
1972get_row_number(typval_T *tv, term_T *term)
1973{
1974 if (tv->v_type == VAR_STRING
1975 && tv->vval.v_string != NULL
1976 && STRCMP(tv->vval.v_string, ".") == 0)
1977 return term->tl_cursor_pos.row;
1978 return (int)get_tv_number(tv) - 1;
1979}
1980
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001981/*
1982 * "term_getline(buf, row)" function
1983 */
1984 void
1985f_term_getline(typval_T *argvars, typval_T *rettv)
1986{
1987 buf_T *buf = term_get_buf(argvars);
1988 term_T *term;
1989 int row;
1990
1991 rettv->v_type = VAR_STRING;
1992 if (buf == NULL)
1993 return;
1994 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001995 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001996
1997 if (term->tl_vterm == NULL)
1998 {
1999 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2000
2001 /* vterm is finished, get the text from the buffer */
2002 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2003 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2004 }
2005 else
2006 {
2007 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2008 VTermRect rect;
2009 int len;
2010 char_u *p;
2011
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002012 if (row < 0 || row >= term->tl_rows)
2013 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002014 len = term->tl_cols * MB_MAXBYTES + 1;
2015 p = alloc(len);
2016 if (p == NULL)
2017 return;
2018 rettv->vval.v_string = p;
2019
2020 rect.start_col = 0;
2021 rect.end_col = term->tl_cols;
2022 rect.start_row = row;
2023 rect.end_row = row + 1;
2024 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2025 }
2026}
2027
2028/*
2029 * "term_getsize(buf)" function
2030 */
2031 void
2032f_term_getsize(typval_T *argvars, typval_T *rettv)
2033{
2034 buf_T *buf = term_get_buf(argvars);
2035 list_T *l;
2036
2037 if (rettv_list_alloc(rettv) == FAIL)
2038 return;
2039 if (buf == NULL)
2040 return;
2041
2042 l = rettv->vval.v_list;
2043 list_append_number(l, buf->b_term->tl_rows);
2044 list_append_number(l, buf->b_term->tl_cols);
2045}
2046
2047/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002048 * "term_getstatus(buf)" function
2049 */
2050 void
2051f_term_getstatus(typval_T *argvars, typval_T *rettv)
2052{
2053 buf_T *buf = term_get_buf(argvars);
2054 term_T *term;
2055 char_u val[100];
2056
2057 rettv->v_type = VAR_STRING;
2058 if (buf == NULL)
2059 return;
2060 term = buf->b_term;
2061
2062 if (term_job_running(term))
2063 STRCPY(val, "running");
2064 else
2065 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002066 if (term->tl_normal_mode)
2067 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002068 rettv->vval.v_string = vim_strsave(val);
2069}
2070
2071/*
2072 * "term_gettitle(buf)" function
2073 */
2074 void
2075f_term_gettitle(typval_T *argvars, typval_T *rettv)
2076{
2077 buf_T *buf = term_get_buf(argvars);
2078
2079 rettv->v_type = VAR_STRING;
2080 if (buf == NULL)
2081 return;
2082
2083 if (buf->b_term->tl_title != NULL)
2084 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2085}
2086
2087/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002088 * "term_gettty(buf)" function
2089 */
2090 void
2091f_term_gettty(typval_T *argvars, typval_T *rettv)
2092{
2093 buf_T *buf = term_get_buf(argvars);
2094 char_u *p;
2095
2096 rettv->v_type = VAR_STRING;
2097 if (buf == NULL)
2098 return;
2099 if (buf->b_term->tl_job != NULL)
2100 p = buf->b_term->tl_job->jv_tty_name;
2101 else
2102 p = buf->b_term->tl_tty_name;
2103 if (p != NULL)
2104 rettv->vval.v_string = vim_strsave(p);
2105}
2106
2107/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002108 * "term_list()" function
2109 */
2110 void
2111f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2112{
2113 term_T *tp;
2114 list_T *l;
2115
2116 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2117 return;
2118
2119 l = rettv->vval.v_list;
2120 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2121 if (tp != NULL && tp->tl_buffer != NULL)
2122 if (list_append_number(l,
2123 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2124 return;
2125}
2126
2127/*
2128 * "term_scrape(buf, row)" function
2129 */
2130 void
2131f_term_scrape(typval_T *argvars, typval_T *rettv)
2132{
2133 buf_T *buf = term_get_buf(argvars);
2134 VTermScreen *screen = NULL;
2135 VTermPos pos;
2136 list_T *l;
2137 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002138 char_u *p;
2139 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002140
2141 if (rettv_list_alloc(rettv) == FAIL)
2142 return;
2143 if (buf == NULL)
2144 return;
2145 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002146
2147 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002148 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002149
2150 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002151 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002152 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002153 p = NULL;
2154 line = NULL;
2155 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002156 else
2157 {
2158 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2159
2160 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2161 return;
2162 p = ml_get_buf(buf, lnum + 1, FALSE);
2163 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2164 }
2165
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002166 for (pos.col = 0; pos.col < term->tl_cols; )
2167 {
2168 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002169 int width;
2170 VTermScreenCellAttrs attrs;
2171 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002172 char_u rgb[8];
2173 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2174 int off = 0;
2175 int i;
2176
2177 if (screen == NULL)
2178 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002179 cellattr_T *cellattr;
2180 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002181
2182 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002183 if (pos.col >= line->sb_cols)
2184 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002185 cellattr = line->sb_cells + pos.col;
2186 width = cellattr->width;
2187 attrs = cellattr->attrs;
2188 fg = cellattr->fg;
2189 bg = cellattr->bg;
2190 len = MB_PTR2LEN(p);
2191 mch_memmove(mbs, p, len);
2192 mbs[len] = NUL;
2193 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002194 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002195 else
2196 {
2197 VTermScreenCell cell;
2198 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2199 break;
2200 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2201 {
2202 if (cell.chars[i] == 0)
2203 break;
2204 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2205 }
2206 mbs[off] = NUL;
2207 width = cell.width;
2208 attrs = cell.attrs;
2209 fg = cell.fg;
2210 bg = cell.bg;
2211 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002212 dcell = dict_alloc();
2213 list_append_dict(l, dcell);
2214
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002215 dict_add_nr_str(dcell, "chars", 0, mbs);
2216
2217 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002218 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002219 dict_add_nr_str(dcell, "fg", 0, rgb);
2220 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002221 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002222 dict_add_nr_str(dcell, "bg", 0, rgb);
2223
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002224 dict_add_nr_str(dcell, "attr",
2225 cell2attr(attrs, fg, bg), NULL);
2226 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002227
2228 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002229 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002230 ++pos.col;
2231 }
2232}
2233
2234/*
2235 * "term_sendkeys(buf, keys)" function
2236 */
2237 void
2238f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2239{
2240 buf_T *buf = term_get_buf(argvars);
2241 char_u *msg;
2242 term_T *term;
2243
2244 rettv->v_type = VAR_UNKNOWN;
2245 if (buf == NULL)
2246 return;
2247
2248 msg = get_tv_string_chk(&argvars[1]);
2249 if (msg == NULL)
2250 return;
2251 term = buf->b_term;
2252 if (term->tl_vterm == NULL)
2253 return;
2254
2255 while (*msg != NUL)
2256 {
2257 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2258 msg += MB_PTR2LEN(msg);
2259 }
2260
Bram Moolenaar6d819742017-08-06 14:57:49 +02002261 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002262 {
2263 /* TODO: only update once in a while. */
2264 update_screen(0);
2265 if (buf == curbuf)
2266 update_cursor(term, TRUE);
2267 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002268}
2269
2270/*
2271 * "term_start(command, options)" function
2272 */
2273 void
2274f_term_start(typval_T *argvars, typval_T *rettv)
2275{
2276 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002277 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002278
2279 if (cmd == NULL)
2280 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002281 init_job_options(&opt);
2282 /* TODO: allow more job options */
2283 if (argvars[1].v_type != VAR_UNKNOWN
2284 && get_job_options(&argvars[1], &opt,
2285 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar78712a72017-08-05 14:50:12 +02002286 + JO_EXIT_CB + JO_CLOSE_CALLBACK
2287 + JO2_TERM_NAME) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002288 return;
2289
2290 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002291
2292 if (curbuf->b_term != NULL)
2293 rettv->vval.v_number = curbuf->b_fnum;
2294}
2295
2296/*
2297 * "term_wait" function
2298 */
2299 void
2300f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2301{
2302 buf_T *buf = term_get_buf(argvars);
2303
2304 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002305 {
2306 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002307 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002308 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002309 if (buf->b_term->tl_job == NULL)
2310 {
2311 ch_log(NULL, "term_wait(): no job to wait for");
2312 return;
2313 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002314
2315 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002316 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002317 {
2318 /* The job is dead, keep reading channel I/O until the channel is
2319 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002320 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002321 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2322 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002323 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002324 parse_queued_messages();
2325 ui_delay(10L, FALSE);
2326 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002327 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002328 parse_queued_messages();
2329 }
2330 else
2331 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002332 long wait = 10L;
2333
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002334 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002335 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002336
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002337 /* Wait for some time for any channel I/O. */
2338 if (argvars[1].v_type != VAR_UNKNOWN)
2339 wait = get_tv_number(&argvars[1]);
2340 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002341 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002342
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002343 /* Flushing messages on channels is hopefully sufficient.
2344 * TODO: is there a better way? */
2345 parse_queued_messages();
2346 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002347}
2348
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002349# ifdef WIN3264
2350
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002351/**************************************
2352 * 2. MS-Windows implementation.
2353 */
2354
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002355#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2356#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2357
Bram Moolenaar8a773062017-07-24 22:29:21 +02002358void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002359void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002360void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002361BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2362void (*winpty_config_set_initial_size)(void*, int, int);
2363LPCWSTR (*winpty_conin_name)(void*);
2364LPCWSTR (*winpty_conout_name)(void*);
2365LPCWSTR (*winpty_conerr_name)(void*);
2366void (*winpty_free)(void*);
2367void (*winpty_config_free)(void*);
2368void (*winpty_spawn_config_free)(void*);
2369void (*winpty_error_free)(void*);
2370LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002371BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002372HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002373
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002374#define WINPTY_DLL "winpty.dll"
2375
2376static HINSTANCE hWinPtyDLL = NULL;
2377
2378 int
2379dyn_winpty_init(void)
2380{
2381 int i;
2382 static struct
2383 {
2384 char *name;
2385 FARPROC *ptr;
2386 } winpty_entry[] =
2387 {
2388 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2389 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2390 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2391 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2392 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2393 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2394 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2395 {"winpty_free", (FARPROC*)&winpty_free},
2396 {"winpty_open", (FARPROC*)&winpty_open},
2397 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2398 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2399 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2400 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002401 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002402 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002403 {NULL, NULL}
2404 };
2405
2406 /* No need to initialize twice. */
2407 if (hWinPtyDLL)
2408 return 1;
2409 /* Load winpty.dll */
2410 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2411 if (!hWinPtyDLL)
2412 {
2413 EMSG2(_(e_loadlib), WINPTY_DLL);
2414 return 0;
2415 }
2416 for (i = 0; winpty_entry[i].name != NULL
2417 && winpty_entry[i].ptr != NULL; ++i)
2418 {
2419 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2420 winpty_entry[i].name)) == NULL)
2421 {
2422 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2423 return 0;
2424 }
2425 }
2426
2427 return 1;
2428}
2429
2430/*
2431 * Create a new terminal of "rows" by "cols" cells.
2432 * Store a reference in "term".
2433 * Return OK or FAIL.
2434 */
2435 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002436term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002437{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002438 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002439 channel_T *channel = NULL;
2440 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002441 DWORD error;
2442 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2443 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002444 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002445 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002446
2447 if (!dyn_winpty_init())
2448 return FAIL;
2449
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002450 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002451 if (p == NULL)
2452 return FAIL;
2453
2454 job = job_alloc();
2455 if (job == NULL)
2456 goto failed;
2457
2458 channel = add_channel();
2459 if (channel == NULL)
2460 goto failed;
2461
2462 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2463 if (term->tl_winpty_config == NULL)
2464 goto failed;
2465
2466 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2467 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2468 if (term->tl_winpty == NULL)
2469 goto failed;
2470
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002471 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002472 spawn_config = winpty_spawn_config_new(
2473 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2474 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2475 NULL,
2476 p,
2477 NULL,
2478 NULL,
2479 &winpty_err);
2480 if (spawn_config == NULL)
2481 goto failed;
2482
2483 channel = add_channel();
2484 if (channel == NULL)
2485 goto failed;
2486
2487 job = job_alloc();
2488 if (job == NULL)
2489 goto failed;
2490
2491 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2492 &child_thread_handle, &error, &winpty_err))
2493 goto failed;
2494
2495 channel_set_pipes(channel,
2496 (sock_T) CreateFileW(
2497 winpty_conin_name(term->tl_winpty),
2498 GENERIC_WRITE, 0, NULL,
2499 OPEN_EXISTING, 0, NULL),
2500 (sock_T) CreateFileW(
2501 winpty_conout_name(term->tl_winpty),
2502 GENERIC_READ, 0, NULL,
2503 OPEN_EXISTING, 0, NULL),
2504 (sock_T) CreateFileW(
2505 winpty_conerr_name(term->tl_winpty),
2506 GENERIC_READ, 0, NULL,
2507 OPEN_EXISTING, 0, NULL));
2508
2509 jo = CreateJobObject(NULL, NULL);
2510 if (jo == NULL)
2511 goto failed;
2512
2513 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002514 {
2515 /* Failed, switch the way to terminate process with TerminateProcess. */
2516 CloseHandle(jo);
2517 jo = NULL;
2518 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002519
2520 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002521 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002522
2523 create_vterm(term, rows, cols);
2524
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002525 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002526 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002527
2528 job->jv_channel = channel;
2529 job->jv_proc_info.hProcess = child_process_handle;
2530 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2531 job->jv_job_object = jo;
2532 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002533 sprintf(buf, "winpty://%lu",
2534 GetProcessId(winpty_agent_process(term->tl_winpty)));
2535 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002536 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002537 term->tl_job = job;
2538
2539 return OK;
2540
2541failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002542 if (spawn_config != NULL)
2543 winpty_spawn_config_free(spawn_config);
2544 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002545 if (channel != NULL)
2546 channel_clear(channel);
2547 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002548 {
2549 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002550 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002551 }
2552 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002553 if (jo != NULL)
2554 CloseHandle(jo);
2555 if (term->tl_winpty != NULL)
2556 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002557 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002558 if (term->tl_winpty_config != NULL)
2559 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002560 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002561 if (winpty_err != NULL)
2562 {
2563 char_u *msg = utf16_to_enc(
2564 (short_u *)winpty_error_msg(winpty_err), NULL);
2565
2566 EMSG(msg);
2567 winpty_error_free(winpty_err);
2568 }
2569 return FAIL;
2570}
2571
2572/*
2573 * Free the terminal emulator part of "term".
2574 */
2575 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002576term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002577{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002578 if (term->tl_winpty != NULL)
2579 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002580 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002581 if (term->tl_winpty_config != NULL)
2582 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002583 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002584 if (term->tl_vterm != NULL)
2585 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002586 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002587}
2588
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002589/*
2590 * Request size to terminal.
2591 */
2592 static void
2593term_report_winsize(term_T *term, int rows, int cols)
2594{
2595 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2596}
2597
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002598# else
2599
2600/**************************************
2601 * 3. Unix-like implementation.
2602 */
2603
2604/*
2605 * Create a new terminal of "rows" by "cols" cells.
2606 * Start job for "cmd".
2607 * Store the pointers in "term".
2608 * Return OK or FAIL.
2609 */
2610 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002611term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002612{
2613 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002614
2615 create_vterm(term, rows, cols);
2616
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002617 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002618 argvars[0].v_type = VAR_STRING;
2619 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002620
2621 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002622 if (term->tl_job != NULL)
2623 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002624
Bram Moolenaar61a66052017-07-22 18:39:00 +02002625 return term->tl_job != NULL
2626 && term->tl_job->jv_channel != NULL
2627 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002628}
2629
2630/*
2631 * Free the terminal emulator part of "term".
2632 */
2633 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002634term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002635{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002636 if (term->tl_vterm != NULL)
2637 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002638 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002639}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002640
2641/*
2642 * Request size to terminal.
2643 */
2644 static void
2645term_report_winsize(term_T *term, int rows, int cols)
2646{
2647 /* Use an ioctl() to report the new window size to the job. */
2648 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2649 {
2650 int fd = -1;
2651 int part;
2652
2653 for (part = PART_OUT; part < PART_COUNT; ++part)
2654 {
2655 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2656 if (isatty(fd))
2657 break;
2658 }
2659 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2660 mch_stop_job(term->tl_job, (char_u *)"winch");
2661 }
2662}
2663
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002664# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002665
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002666#endif /* FEAT_TERMINAL */