blob: 36d5cf15d14a35633cf7b65d5fd719ba80556b8b [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).
Bram Moolenaar82b9ca02017-08-08 23:06:46 +020043 * patch by Yasuhiro: #1950
Bram Moolenaar423802d2017-07-30 16:52:24 +020044 * - add option values to the command:
45 * :term <24x80> <close> vim notes.txt
Bram Moolenaar82b9ca02017-08-08 23:06:46 +020046 * or use:
47 * :term ++24x80 ++close vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020048 * - support different cursor shapes, colors and attributes
49 * - make term_getcursor() return type (none/block/bar/underline) and
50 * attributes (color, blink, etc.)
Bram Moolenaar292d5692017-08-08 21:52:22 +020051 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaard85f2712017-07-28 21:51:57 +020052 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020053 * For the GUI fill termios with default values, perhaps like pangoterm:
54 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
55 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020056 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020057 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020058 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020059 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020060 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020061 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
62 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020063 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020064 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020065 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020066 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020067 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020068 * terminal. Allow:
69 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
70 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
71 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
72 * Check that something is connected to the terminal.
73 * Test: "cat" reading from a file or buffer
74 * "ls" writing stdout to a file or buffer
75 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020076 * - support ":term NONE" to open a terminal with a pty but not running a job
77 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020078 * - if the job in the terminal does not support the mouse, we can use the
79 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020080 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
81 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020082 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020083 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020084 * - Copy text in the vterm to the Vim buffer once in a while, so that
85 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020086 */
87
88#include "vim.h"
89
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020090#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020091
Bram Moolenaard5310982017-08-05 15:16:32 +020092#ifndef MIN
93# define MIN(x,y) ((x) < (y) ? (x) : (y))
94#endif
95#ifndef MAX
96# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020097#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020098
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020099#include "libvterm/include/vterm.h"
100
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200101/* This is VTermScreenCell without the characters, thus much smaller. */
102typedef struct {
103 VTermScreenCellAttrs attrs;
104 char width;
105 VTermColor fg, bg;
106} cellattr_T;
107
Bram Moolenaard85f2712017-07-28 21:51:57 +0200108typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200109 int sb_cols; /* can differ per line */
110 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200111} sb_line_T;
112
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200113/* typedef term_T in structs.h */
114struct terminal_S {
115 term_T *tl_next;
116
Bram Moolenaar423802d2017-07-30 16:52:24 +0200117 VTerm *tl_vterm;
118 job_T *tl_job;
119 buf_T *tl_buffer;
120
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200121 /* used when tl_job is NULL and only a pty was created */
122 int tl_tty_fd;
123 char_u *tl_tty_name;
124
Bram Moolenaar6d819742017-08-06 14:57:49 +0200125 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200126 int tl_channel_closed;
127
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200128#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200129 void *tl_winpty_config;
130 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200131#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200132
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200133 /* last known vterm size */
134 int tl_rows;
135 int tl_cols;
136 /* vterm size does not follow window size */
137 int tl_rows_fixed;
138 int tl_cols_fixed;
139
Bram Moolenaar21554412017-07-24 21:44:43 +0200140 char_u *tl_title; /* NULL or allocated */
141 char_u *tl_status_text; /* NULL or allocated */
142
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200143 /* Range of screen rows to update. Zero based. */
144 int tl_dirty_row_start; /* -1 if nothing dirty */
145 int tl_dirty_row_end; /* row below last one to update */
146
Bram Moolenaard85f2712017-07-28 21:51:57 +0200147 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200148 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200149
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200150 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200151 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200152};
153
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200154#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
155#define TMODE_LOOP 2 /* CTRL-W N used */
156
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200157/*
158 * List of all active terminals.
159 */
160static term_T *first_term = NULL;
161
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200162
163#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
164#define KEY_BUF_LEN 200
165
166/*
167 * Functions with separate implementation for MS-Windows and Unix-like systems.
168 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200169static 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 +0200170static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200171static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200172
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200173/**************************************
174 * 1. Generic code for all systems.
175 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200176
177/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200178 * Determine the terminal size from 'termsize' and the current window.
179 * Assumes term->tl_rows and term->tl_cols are zero.
180 */
181 static void
182set_term_and_win_size(term_T *term)
183{
184 if (*curwin->w_p_tms != NUL)
185 {
186 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
187
188 term->tl_rows = atoi((char *)curwin->w_p_tms);
189 term->tl_cols = atoi((char *)p);
190 }
191 if (term->tl_rows == 0)
192 term->tl_rows = curwin->w_height;
193 else
194 {
195 win_setheight_win(term->tl_rows, curwin);
196 term->tl_rows_fixed = TRUE;
197 }
198 if (term->tl_cols == 0)
199 term->tl_cols = curwin->w_width;
200 else
201 {
202 win_setwidth_win(term->tl_cols, curwin);
203 term->tl_cols_fixed = TRUE;
204 }
205}
206
207/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200208 * Initialize job options for a terminal job.
209 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200210 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200211 static void
212init_job_options(jobopt_T *opt)
213{
214 clear_job_options(opt);
215
216 opt->jo_mode = MODE_RAW;
217 opt->jo_out_mode = MODE_RAW;
218 opt->jo_err_mode = MODE_RAW;
219 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
220
221 opt->jo_io[PART_OUT] = JIO_BUFFER;
222 opt->jo_io[PART_ERR] = JIO_BUFFER;
223 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
224
225 opt->jo_modifiable[PART_OUT] = 0;
226 opt->jo_modifiable[PART_ERR] = 0;
227 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
228
229 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
230}
231
232/*
233 * Set job options mandatory for a terminal job.
234 */
235 static void
236setup_job_options(jobopt_T *opt, int rows, int cols)
237{
238 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
239 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
240 opt->jo_pty = TRUE;
241 opt->jo_term_rows = rows;
242 opt->jo_term_cols = cols;
243}
244
245 static void
246term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248 exarg_T split_ea;
249 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251
252 if (check_restricted() || check_secure())
253 return;
254
255 term = (term_T *)alloc_clear(sizeof(term_T));
256 if (term == NULL)
257 return;
258 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200259 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200260 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200261
262 /* Open a new window or tab. */
263 vim_memset(&split_ea, 0, sizeof(split_ea));
264 split_ea.cmdidx = CMD_new;
265 split_ea.cmd = (char_u *)"new";
266 split_ea.arg = (char_u *)"";
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200267 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
268 {
269 split_ea.line2 = opt->jo_term_rows;
270 split_ea.addr_count = 1;
271 }
272 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
273 {
274 split_ea.line2 = opt->jo_term_cols;
275 split_ea.addr_count = 1;
276 }
277
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200278 ex_splitview(&split_ea);
279 if (curwin == old_curwin)
280 {
281 /* split failed */
282 vim_free(term);
283 return;
284 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200285 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200286 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200287
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200288 /* only one size was taken care of with :new, do the other one */
289 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
290 win_setheight(opt->jo_term_rows);
291 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
292 win_setwidth(opt->jo_term_cols);
293
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200294 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200295 term->tl_next = first_term;
296 first_term = term;
297
Bram Moolenaar293424c2017-07-26 23:11:01 +0200298 if (cmd == NULL || *cmd == NUL)
299 cmd = p_sh;
300
Bram Moolenaar78712a72017-08-05 14:50:12 +0200301 if (opt->jo_term_name != NULL)
302 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
303 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200304 {
305 int i;
306 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200307 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200308
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200309 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200310 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200311 /* Prepend a ! to the command name to avoid the buffer name equals
312 * the executable, otherwise ":w!" would overwrite it. */
313 if (i == 0)
314 vim_snprintf((char *)p, len, "!%s", cmd);
315 else
316 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200317 if (buflist_findname(p) == NULL)
318 {
319 curbuf->b_ffname = p;
320 break;
321 }
322 }
323 }
324 curbuf->b_fname = curbuf->b_ffname;
325
Bram Moolenaareb44a682017-08-03 22:44:55 +0200326 set_string_option_direct((char_u *)"buftype", -1,
327 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
328
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200329 /* Mark the buffer as not modifiable. It can only be made modifiable after
330 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200331 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200332
333 /* Set 'bufhidden' to "hide": allow closing the window. */
334 set_string_option_direct((char_u *)"bufhidden", -1,
335 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200336
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200337 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200338 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200339
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200340 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200341 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200342 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200343 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200344 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200345 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200346 }
347 else
348 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200349 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200350
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200351 /* Wiping out the buffer will also close the window and call
352 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200353 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200354 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200355}
356
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200357/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200358 * ":terminal": open a terminal window and execute a job in it.
359 */
360 void
361ex_terminal(exarg_T *eap)
362{
363 jobopt_T opt;
364
365 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200366
367 if (eap->addr_count == 2)
368 {
369 opt.jo_term_rows = eap->line1;
370 opt.jo_term_cols = eap->line2;
371 }
372 else if (eap->addr_count == 1)
373 {
374 if (cmdmod.split & WSP_VERT)
375 opt.jo_term_cols = eap->line2;
376 else
377 opt.jo_term_rows = eap->line2;
378 }
379 /* TODO: get more options from before the command */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200380
381 term_start(eap->arg, &opt);
382}
383
384/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200385 * Free the scrollback buffer for "term".
386 */
387 static void
388free_scrollback(term_T *term)
389{
390 int i;
391
392 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
393 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
394 ga_clear(&term->tl_scrollback);
395}
396
397/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200398 * Free a terminal and everything it refers to.
399 * Kills the job if there is one.
400 * Called when wiping out a buffer.
401 */
402 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200403free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200404{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200405 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200406 term_T *tp;
407
408 if (term == NULL)
409 return;
410 if (first_term == term)
411 first_term = term->tl_next;
412 else
413 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
414 if (tp->tl_next == term)
415 {
416 tp->tl_next = term->tl_next;
417 break;
418 }
419
420 if (term->tl_job != NULL)
421 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200422 if (term->tl_job->jv_status != JOB_ENDED
423 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200424 job_stop(term->tl_job, NULL, "kill");
425 job_unref(term->tl_job);
426 }
427
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200428 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200429
430 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200431 vim_free(term->tl_title);
432 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200433 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200434 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200435}
436
437/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200438 * Write job output "msg[len]" to the vterm.
439 */
440 static void
441term_write_job_output(term_T *term, char_u *msg, size_t len)
442{
443 VTerm *vterm = term->tl_vterm;
444 char_u *p;
445 size_t done;
446 size_t len_now;
447
448 for (done = 0; done < len; done += len_now)
449 {
450 for (p = msg + done; p < msg + len; )
451 {
452 if (*p == NL)
453 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200454 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200455 }
456 len_now = p - msg - done;
457 vterm_input_write(vterm, (char *)msg + done, len_now);
458 if (p < msg + len && *p == NL)
459 {
460 /* Convert NL to CR-NL, that appears to work best. */
461 vterm_input_write(vterm, "\r\n", 2);
462 ++len_now;
463 }
464 }
465
466 /* this invokes the damage callbacks */
467 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
468}
469
Bram Moolenaar1c844932017-07-24 23:36:41 +0200470 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200471update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200472{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200473 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200474 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200475 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200476 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200477 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200478 if (term->tl_cursor_visible)
479 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200480 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200481#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200482 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200483 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200484#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200485 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200486}
487
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200488/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200489 * Invoked when "msg" output from a job was received. Write it to the terminal
490 * of "buffer".
491 */
492 void
493write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
494{
495 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200496 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200497
Bram Moolenaard85f2712017-07-28 21:51:57 +0200498 if (term->tl_vterm == NULL)
499 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200500 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200501 return;
502 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200503 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200504 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200505
Bram Moolenaar6d819742017-08-06 14:57:49 +0200506 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200507 {
508 /* TODO: only update once in a while. */
509 update_screen(0);
510 update_cursor(term, TRUE);
511 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200512}
513
514/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200515 * Send a mouse position and click to the vterm
516 */
517 static int
518term_send_mouse(VTerm *vterm, int button, int pressed)
519{
520 VTermModifier mod = VTERM_MOD_NONE;
521
522 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
523 mouse_col - W_WINCOL(curwin), mod);
524 vterm_mouse_button(vterm, button, pressed, mod);
525 return TRUE;
526}
527
528/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200529 * Convert typed key "c" into bytes to send to the job.
530 * Return the number of bytes in "buf".
531 */
532 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200533term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200534{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200535 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200536 VTermKey key = VTERM_KEY_NONE;
537 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200538 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200539
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200540 switch (c)
541 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200542 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200543 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200544 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
545 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200546 case K_DEL: key = VTERM_KEY_DEL; break;
547 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200548 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
549 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200550 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200551 case K_S_END: mod = VTERM_MOD_SHIFT;
552 key = VTERM_KEY_END; break;
553 case K_C_END: mod = VTERM_MOD_CTRL;
554 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200555 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
556 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
557 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
558 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
559 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
560 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
561 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
562 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
563 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
564 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
565 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
566 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
567 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200568 case K_S_HOME: mod = VTERM_MOD_SHIFT;
569 key = VTERM_KEY_HOME; break;
570 case K_C_HOME: mod = VTERM_MOD_CTRL;
571 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200572 case K_INS: key = VTERM_KEY_INS; break;
573 case K_K0: key = VTERM_KEY_KP_0; break;
574 case K_K1: key = VTERM_KEY_KP_1; break;
575 case K_K2: key = VTERM_KEY_KP_2; break;
576 case K_K3: key = VTERM_KEY_KP_3; break;
577 case K_K4: key = VTERM_KEY_KP_4; break;
578 case K_K5: key = VTERM_KEY_KP_5; break;
579 case K_K6: key = VTERM_KEY_KP_6; break;
580 case K_K7: key = VTERM_KEY_KP_7; break;
581 case K_K8: key = VTERM_KEY_KP_8; break;
582 case K_K9: key = VTERM_KEY_KP_9; break;
583 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
584 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
585 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
586 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
587 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
588 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
589 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
590 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
591 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
592 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
593 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
594 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
595 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200596 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
597 key = VTERM_KEY_LEFT; break;
598 case K_C_LEFT: mod = VTERM_MOD_CTRL;
599 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200600 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
601 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
602 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200603 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
604 key = VTERM_KEY_RIGHT; break;
605 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
606 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200607 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200608 case K_S_UP: mod = VTERM_MOD_SHIFT;
609 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200610 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200611
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200612 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
613 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
614 case K_MOUSELEFT: /* TODO */ return 0;
615 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200616
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200617 case K_LEFTMOUSE:
618 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
619 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
620 case K_LEFTRELEASE:
621 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
622 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
623 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
624 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
625 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
626 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
627 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
628 case K_X1MOUSE: /* TODO */ return 0;
629 case K_X1DRAG: /* TODO */ return 0;
630 case K_X1RELEASE: /* TODO */ return 0;
631 case K_X2MOUSE: /* TODO */ return 0;
632 case K_X2DRAG: /* TODO */ return 0;
633 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200634
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200635 case K_IGNORE: return 0;
636 case K_NOP: return 0;
637 case K_UNDO: return 0;
638 case K_HELP: return 0;
639 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
640 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
641 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
642 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
643 case K_SELECT: return 0;
644#ifdef FEAT_GUI
645 case K_VER_SCROLLBAR: return 0;
646 case K_HOR_SCROLLBAR: return 0;
647#endif
648#ifdef FEAT_GUI_TABLINE
649 case K_TABLINE: return 0;
650 case K_TABMENU: return 0;
651#endif
652#ifdef FEAT_NETBEANS_INTG
653 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
654#endif
655#ifdef FEAT_DND
656 case K_DROP: return 0;
657#endif
658#ifdef FEAT_AUTOCMD
659 case K_CURSORHOLD: return 0;
660#endif
661 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
662 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200663 }
664
665 /*
666 * Convert special keys to vterm keys:
667 * - Write keys to vterm: vterm_keyboard_key()
668 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200669 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200670 */
671 if (key != VTERM_KEY_NONE)
672 /* Special key, let vterm convert it. */
673 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200674 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200675 /* Normal character, let vterm convert it. */
676 vterm_keyboard_unichar(vterm, c, mod);
677
678 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200679 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200680}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200681
Bram Moolenaar938783d2017-07-16 20:13:26 +0200682/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200683 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200684 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200685 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200686term_job_running(term_T *term)
687{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200688 /* Also consider the job finished when the channel is closed, to avoid a
689 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200690 return term != NULL
691 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200692 && term->tl_job->jv_status == JOB_STARTED
693 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200694}
695
696/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200697 * Add the last line of the scrollback buffer to the buffer in the window.
698 */
699 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200700add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200701{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200702 buf_T *buf = term->tl_buffer;
703 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
704 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200705
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200706 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200707 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200708 {
709 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200710 curbuf = buf;
711 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200712 curbuf = curwin->w_buffer;
713 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200714}
715
716/*
717 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200718 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200719 */
720 static void
721move_terminal_to_buffer(term_T *term)
722{
723 win_T *wp;
724 int len;
725 int lines_skipped = 0;
726 VTermPos pos;
727 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200728 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200729 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200730
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200731 if (term->tl_vterm == NULL)
732 return;
733 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200734 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
735 {
736 len = 0;
737 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
738 if (vterm_screen_get_cell(screen, pos, &cell) != 0
739 && cell.chars[0] != NUL)
740 len = pos.col + 1;
741
742 if (len == 0)
743 ++lines_skipped;
744 else
745 {
746 while (lines_skipped > 0)
747 {
748 /* Line was skipped, add an empty line. */
749 --lines_skipped;
750 if (ga_grow(&term->tl_scrollback, 1) == OK)
751 {
752 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
753 + term->tl_scrollback.ga_len;
754
755 line->sb_cols = 0;
756 line->sb_cells = NULL;
757 ++term->tl_scrollback.ga_len;
758
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200759 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200760 }
761 }
762
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200763 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200764 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
765 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200766 garray_T ga;
767 int width;
768 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200769 + term->tl_scrollback.ga_len;
770
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200771 ga_init2(&ga, 1, 100);
772 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200773 {
774 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200775 {
776 width = 1;
777 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
778 if (ga_grow(&ga, 1) == OK)
779 ga.ga_len += mb_char2bytes(' ',
780 (char_u *)ga.ga_data + ga.ga_len);
781 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200782 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200783 {
784 width = cell.width;
785
786 p[pos.col].width = cell.width;
787 p[pos.col].attrs = cell.attrs;
788 p[pos.col].fg = cell.fg;
789 p[pos.col].bg = cell.bg;
790
791 if (ga_grow(&ga, MB_MAXBYTES) == OK)
792 {
793 int i;
794 int c;
795
796 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
797 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
798 (char_u *)ga.ga_data + ga.ga_len);
799 }
800 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200801 }
802 line->sb_cols = len;
803 line->sb_cells = p;
804 ++term->tl_scrollback.ga_len;
805
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200806 if (ga_grow(&ga, 1) == FAIL)
807 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
808 else
809 {
810 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
811 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
812 }
813 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200814 }
815 else
816 vim_free(p);
817 }
818 }
819
820 FOR_ALL_WINDOWS(wp)
821 {
822 if (wp->w_buffer == term->tl_buffer)
823 {
824 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
825 wp->w_cursor.col = 0;
826 wp->w_valid = 0;
827 redraw_win_later(wp, NOT_VALID);
828 }
829 }
830}
831
832 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200833set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200834{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200835 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200836 vim_free(term->tl_status_text);
837 term->tl_status_text = NULL;
838 if (term->tl_buffer == curbuf)
839 maketitle();
840}
841
842/*
843 * Called after the job if finished and Terminal mode is not active:
844 * Move the vterm contents into the scrollback buffer and free the vterm.
845 */
846 static void
847cleanup_vterm(term_T *term)
848{
849 move_terminal_to_buffer(term);
850 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200851 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200852}
853
854/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200855 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200856 * Suspends updating the terminal window.
857 */
858 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200859term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200860{
861 term_T *term = curbuf->b_term;
862
863 /* Append the current terminal contents to the buffer. */
864 move_terminal_to_buffer(term);
865
Bram Moolenaar6d819742017-08-06 14:57:49 +0200866 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200867
Bram Moolenaar6d819742017-08-06 14:57:49 +0200868 /* Move the window cursor to the position of the cursor in the
869 * terminal. */
870 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
871 + term->tl_cursor_pos.row + 1;
872 check_cursor();
873 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200874
Bram Moolenaar6d819742017-08-06 14:57:49 +0200875 /* Display the same lines as in the terminal. */
876 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200877}
878
879/*
880 * Returns TRUE if the current window contains a terminal and we are in
881 * Terminal-Normal mode.
882 */
883 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200884term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200885{
886 term_T *term = curbuf->b_term;
887
Bram Moolenaar6d819742017-08-06 14:57:49 +0200888 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200889}
890
891/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200892 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200893 * Restores updating the terminal window.
894 */
895 void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200896term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +0200897{
898 term_T *term = curbuf->b_term;
899 sb_line_T *line;
900 garray_T *gap;
901
902 /* Remove the terminal contents from the scrollback and the buffer. */
903 gap = &term->tl_scrollback;
904 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
905 {
906 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
907 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
908 vim_free(line->sb_cells);
909 --gap->ga_len;
910 if (gap->ga_len == 0)
911 break;
912 }
913 check_cursor();
914
Bram Moolenaar6d819742017-08-06 14:57:49 +0200915 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200916
917 if (term->tl_channel_closed)
918 cleanup_vterm(term);
919 redraw_buf_and_status_later(curbuf, NOT_VALID);
920}
921
922/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200923 * Get a key from the user without mapping.
924 * TODO: use terminal mode mappings.
925 */
926 static int
927term_vgetc()
928{
929 int c;
930
931 ++no_mapping;
932 ++allow_keys;
933 got_int = FALSE;
934 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200935 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200936 --no_mapping;
937 --allow_keys;
938 return c;
939}
940
941/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200942 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200943 * Return FAIL when the key needs to be handled in Normal mode.
944 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200945 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200946 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200947send_keys_to_term(term_T *term, int c, int typed)
948{
949 char msg[KEY_BUF_LEN];
950 size_t len;
951 static int mouse_was_outside = FALSE;
952 int dragging_outside = FALSE;
953
954 /* Catch keys that need to be handled as in Normal mode. */
955 switch (c)
956 {
957 case NUL:
958 case K_ZERO:
959 if (typed)
960 stuffcharReadbuff(c);
961 return FAIL;
962
963 case K_IGNORE:
964 return FAIL;
965
966 case K_LEFTDRAG:
967 case K_MIDDLEDRAG:
968 case K_RIGHTDRAG:
969 case K_X1DRAG:
970 case K_X2DRAG:
971 dragging_outside = mouse_was_outside;
972 /* FALLTHROUGH */
973 case K_LEFTMOUSE:
974 case K_LEFTMOUSE_NM:
975 case K_LEFTRELEASE:
976 case K_LEFTRELEASE_NM:
977 case K_MIDDLEMOUSE:
978 case K_MIDDLERELEASE:
979 case K_RIGHTMOUSE:
980 case K_RIGHTRELEASE:
981 case K_X1MOUSE:
982 case K_X1RELEASE:
983 case K_X2MOUSE:
984 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200985
986 case K_MOUSEUP:
987 case K_MOUSEDOWN:
988 case K_MOUSELEFT:
989 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200990 if (mouse_row < W_WINROW(curwin)
991 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
992 || mouse_col < W_WINCOL(curwin)
993 || mouse_col >= W_ENDCOL(curwin)
994 || dragging_outside)
995 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200996 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200997 if (typed)
998 {
999 stuffcharReadbuff(c);
1000 mouse_was_outside = TRUE;
1001 }
1002 return FAIL;
1003 }
1004 }
1005 if (typed)
1006 mouse_was_outside = FALSE;
1007
1008 /* Convert the typed key to a sequence of bytes for the job. */
1009 len = term_convert_key(term, c, msg);
1010 if (len > 0)
1011 /* TODO: if FAIL is returned, stop? */
1012 channel_send(term->tl_job->jv_channel, PART_IN,
1013 (char_u *)msg, (int)len, NULL);
1014
1015 return OK;
1016}
1017
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001018 static void
1019position_cursor(win_T *wp, VTermPos *pos)
1020{
1021 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1022 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1023 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1024}
1025
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001026/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001027 * Handle CTRL-W "": send register contents to the job.
1028 */
1029 static void
1030term_paste_register(int prev_c UNUSED)
1031{
1032 int c;
1033 list_T *l;
1034 listitem_T *item;
1035 long reglen = 0;
1036 int type;
1037
1038#ifdef FEAT_CMDL_INFO
1039 if (add_to_showcmd(prev_c))
1040 if (add_to_showcmd('"'))
1041 out_flush();
1042#endif
1043 c = term_vgetc();
1044#ifdef FEAT_CMDL_INFO
1045 clear_showcmd();
1046#endif
1047
1048 /* CTRL-W "= prompt for expression to evaluate. */
1049 if (c == '=' && get_expr_register() != '=')
1050 return;
1051
1052 l = (list_T *)get_reg_contents(c, GREG_LIST);
1053 if (l != NULL)
1054 {
1055 type = get_reg_type(c, &reglen);
1056 for (item = l->lv_first; item != NULL; item = item->li_next)
1057 {
1058 char_u *s = get_tv_string(&item->li_tv);
1059
1060 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1061 s, STRLEN(s), NULL);
1062 if (item->li_next != NULL || type == MLINE)
1063 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1064 (char_u *)"\r", 1, NULL);
1065 }
1066 list_free(l);
1067 }
1068}
1069
1070/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001071 * Returns TRUE if the current window contains a terminal and we are sending
1072 * keys to the job.
1073 */
1074 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001075term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001076{
1077 term_T *term = curbuf->b_term;
1078
1079 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001080 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001081 && term->tl_vterm != NULL
1082 && term_job_running(term);
1083}
1084
1085/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001086 * Wait for input and send it to the job.
1087 * Return when the start of a CTRL-W command is typed or anything else that
1088 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001089 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1090 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001091 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001092 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001093terminal_loop(void)
1094{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001095 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001096 int termkey = 0;
1097
1098 if (*curwin->w_p_tk != NUL)
1099 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001100 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001101
1102 for (;;)
1103 {
1104 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001105 /* Repeat redrawing in case a message is received while redrawing. */
1106 while (curwin->w_redr_type != 0)
1107 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001108 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001109
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001110 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001111 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001112 /* job finished while waiting for a character */
1113 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001114
Bram Moolenaarfae42832017-08-01 22:24:26 +02001115#ifdef UNIX
1116 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1117#endif
1118#ifdef WIN3264
1119 if (c == Ctrl_C)
1120 /* We don't know if the job can handle CTRL-C itself or not, this
1121 * may kill the shell instead of killing the command running in the
1122 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001123 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001124#endif
1125
Bram Moolenaar69198192017-08-05 14:10:48 +02001126 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001127 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001128 int prev_c = c;
1129
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001130#ifdef FEAT_CMDL_INFO
1131 if (add_to_showcmd(c))
1132 out_flush();
1133#endif
1134 c = term_vgetc();
1135#ifdef FEAT_CMDL_INFO
1136 clear_showcmd();
1137#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001138 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001139 /* job finished while waiting for a character */
1140 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001141
Bram Moolenaar69198192017-08-05 14:10:48 +02001142 if (prev_c == Ctrl_BSL)
1143 {
1144 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001145 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001146 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1147 term_enter_normal_mode();
1148 return FAIL;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001149 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001150 /* Send both keys to the terminal. */
1151 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1152 }
1153 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001154 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001155 /* "CTRL-W .": send CTRL-W to the job */
1156 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001157 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001158 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001159 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001160 /* CTRL-W N : go to Terminal-Normal mode. */
1161 term_enter_normal_mode();
Bram Moolenaar423802d2017-07-30 16:52:24 +02001162 return FAIL;
1163 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001164 else if (c == '"')
1165 {
1166 term_paste_register(prev_c);
1167 continue;
1168 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001169 else if (termkey == 0 || c != termkey)
1170 {
1171 stuffcharReadbuff(Ctrl_W);
1172 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001173 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001174 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001175 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001176 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1177 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001178 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001179 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001180}
1181
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001182/*
1183 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001184 * This updates the title and status, but does not close the vter, because
1185 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001186 */
1187 void
1188term_job_ended(job_T *job)
1189{
1190 term_T *term;
1191 int did_one = FALSE;
1192
1193 for (term = first_term; term != NULL; term = term->tl_next)
1194 if (term->tl_job == job)
1195 {
1196 vim_free(term->tl_title);
1197 term->tl_title = NULL;
1198 vim_free(term->tl_status_text);
1199 term->tl_status_text = NULL;
1200 redraw_buf_and_status_later(term->tl_buffer, VALID);
1201 did_one = TRUE;
1202 }
1203 if (did_one)
1204 redraw_statuslines();
1205 if (curbuf->b_term != NULL)
1206 {
1207 if (curbuf->b_term->tl_job == job)
1208 maketitle();
1209 update_cursor(curbuf->b_term, TRUE);
1210 }
1211}
1212
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001213 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001214may_toggle_cursor(term_T *term)
1215{
1216 if (curbuf == term->tl_buffer)
1217 {
1218 if (term->tl_cursor_visible)
1219 cursor_on();
1220 else
1221 cursor_off();
1222 }
1223}
1224
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001225 static int
1226handle_damage(VTermRect rect, void *user)
1227{
1228 term_T *term = (term_T *)user;
1229
1230 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1231 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1232 redraw_buf_later(term->tl_buffer, NOT_VALID);
1233 return 1;
1234}
1235
1236 static int
1237handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1238{
1239 term_T *term = (term_T *)user;
1240
1241 /* TODO */
1242 redraw_buf_later(term->tl_buffer, NOT_VALID);
1243 return 1;
1244}
1245
1246 static int
1247handle_movecursor(
1248 VTermPos pos,
1249 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001250 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001251 void *user)
1252{
1253 term_T *term = (term_T *)user;
1254 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001255
1256 term->tl_cursor_pos = pos;
1257 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001258
1259 FOR_ALL_WINDOWS(wp)
1260 {
1261 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001262 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001263 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001264 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001265 {
1266 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001267 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001268 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001269
1270 return 1;
1271}
1272
Bram Moolenaar21554412017-07-24 21:44:43 +02001273 static int
1274handle_settermprop(
1275 VTermProp prop,
1276 VTermValue *value,
1277 void *user)
1278{
1279 term_T *term = (term_T *)user;
1280
1281 switch (prop)
1282 {
1283 case VTERM_PROP_TITLE:
1284 vim_free(term->tl_title);
1285 term->tl_title = vim_strsave((char_u *)value->string);
1286 vim_free(term->tl_status_text);
1287 term->tl_status_text = NULL;
1288 if (term == curbuf->b_term)
1289 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001290 break;
1291
1292 case VTERM_PROP_CURSORVISIBLE:
1293 term->tl_cursor_visible = value->boolean;
1294 may_toggle_cursor(term);
1295 out_flush();
1296 break;
1297
Bram Moolenaar21554412017-07-24 21:44:43 +02001298 default:
1299 break;
1300 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001301 /* Always return 1, otherwise vterm doesn't store the value internally. */
1302 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001303}
1304
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001305/*
1306 * The job running in the terminal resized the terminal.
1307 */
1308 static int
1309handle_resize(int rows, int cols, void *user)
1310{
1311 term_T *term = (term_T *)user;
1312 win_T *wp;
1313
1314 term->tl_rows = rows;
1315 term->tl_cols = cols;
1316 FOR_ALL_WINDOWS(wp)
1317 {
1318 if (wp->w_buffer == term->tl_buffer)
1319 {
1320 win_setheight_win(rows, wp);
1321 win_setwidth_win(cols, wp);
1322 }
1323 }
1324
1325 redraw_buf_later(term->tl_buffer, NOT_VALID);
1326 return 1;
1327}
1328
Bram Moolenaard85f2712017-07-28 21:51:57 +02001329/*
1330 * Handle a line that is pushed off the top of the screen.
1331 */
1332 static int
1333handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1334{
1335 term_T *term = (term_T *)user;
1336
1337 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001338 if (ga_grow(&term->tl_scrollback, 1) == OK)
1339 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001340 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001341 int len = 0;
1342 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001343 int c;
1344 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001345 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001346 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001347
1348 /* do not store empty cells at the end */
1349 for (i = 0; i < cols; ++i)
1350 if (cells[i].chars[0] != 0)
1351 len = i + 1;
1352
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001353 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001354 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001355 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001356 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001357 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001358 for (col = 0; col < len; col += cells[col].width)
1359 {
1360 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1361 {
1362 ga.ga_len = 0;
1363 break;
1364 }
1365 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1366 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1367 (char_u *)ga.ga_data + ga.ga_len);
1368 p[col].width = cells[col].width;
1369 p[col].attrs = cells[col].attrs;
1370 p[col].fg = cells[col].fg;
1371 p[col].bg = cells[col].bg;
1372 }
1373 }
1374 if (ga_grow(&ga, 1) == FAIL)
1375 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1376 else
1377 {
1378 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1379 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1380 }
1381 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001382
1383 line = (sb_line_T *)term->tl_scrollback.ga_data
1384 + term->tl_scrollback.ga_len;
1385 line->sb_cols = len;
1386 line->sb_cells = p;
1387 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001388 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001389 }
1390 return 0; /* ignored */
1391}
1392
Bram Moolenaar21554412017-07-24 21:44:43 +02001393static VTermScreenCallbacks screen_callbacks = {
1394 handle_damage, /* damage */
1395 handle_moverect, /* moverect */
1396 handle_movecursor, /* movecursor */
1397 handle_settermprop, /* settermprop */
1398 NULL, /* bell */
1399 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001400 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001401 NULL /* sb_popline */
1402};
1403
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001404/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001405 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001406 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001407 */
1408 void
1409term_channel_closed(channel_T *ch)
1410{
1411 term_T *term;
1412 int did_one = FALSE;
1413
1414 for (term = first_term; term != NULL; term = term->tl_next)
1415 if (term->tl_job == ch->ch_job)
1416 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001417 term->tl_channel_closed = TRUE;
1418
Bram Moolenaard85f2712017-07-28 21:51:57 +02001419 vim_free(term->tl_title);
1420 term->tl_title = NULL;
1421 vim_free(term->tl_status_text);
1422 term->tl_status_text = NULL;
1423
Bram Moolenaar423802d2017-07-30 16:52:24 +02001424 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001425 if (!term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001426 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001427
1428 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1429 did_one = TRUE;
1430 }
1431 if (did_one)
1432 {
1433 redraw_statuslines();
1434
1435 /* Need to break out of vgetc(). */
1436 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001437 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001438
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001439 term = curbuf->b_term;
1440 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001441 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001442 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001443 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001444 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001445 }
1446 }
1447}
1448
1449/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001450 * Reverse engineer the RGB value into a cterm color index.
1451 * First color is 1. Return 0 if no match found.
1452 */
1453 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001454color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001455{
1456 int red = color->red;
1457 int blue = color->blue;
1458 int green = color->green;
1459
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001460 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001461 if (red == 0)
1462 {
1463 if (green == 0)
1464 {
1465 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001466 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001467 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001468 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001469 }
1470 else if (green == 224)
1471 {
1472 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001473 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001474 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001475 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001476 }
1477 }
1478 else if (red == 224)
1479 {
1480 if (green == 0)
1481 {
1482 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001483 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001484 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001485 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001486 }
1487 else if (green == 224)
1488 {
1489 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001490 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001491 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001492 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001493 }
1494 }
1495 else if (red == 128)
1496 {
1497 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001498 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001499 }
1500 else if (red == 255)
1501 {
1502 if (green == 64)
1503 {
1504 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001505 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001506 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001507 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001508 }
1509 else if (green == 255)
1510 {
1511 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001512 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001513 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001514 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001515 }
1516 }
1517 else if (red == 64)
1518 {
1519 if (green == 64)
1520 {
1521 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001522 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001523 }
1524 else if (green == 255)
1525 {
1526 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001527 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001528 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001529 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001530 }
1531 }
1532 if (t_colors >= 256)
1533 {
1534 if (red == blue && red == green)
1535 {
1536 /* 24-color greyscale */
1537 static int cutoff[23] = {
1538 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1539 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1540 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1541 int i;
1542
1543 for (i = 0; i < 23; ++i)
1544 if (red < cutoff[i])
1545 return i + 233;
1546 return 256;
1547 }
1548
1549 /* 216-color cube */
1550 return 17 + ((red + 25) / 0x33) * 36
1551 + ((green + 25) / 0x33) * 6
1552 + (blue + 25) / 0x33;
1553 }
1554 return 0;
1555}
1556
1557/*
1558 * Convert the attributes of a vterm cell into an attribute index.
1559 */
1560 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001561cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001562{
1563 int attr = 0;
1564
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001565 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001566 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001567 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001568 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001569 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001570 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001571 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001572 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001573 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001574 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001575
1576#ifdef FEAT_GUI
1577 if (gui.in_use)
1578 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001579 guicolor_T fg, bg;
1580
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001581 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1582 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001583 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001584 }
1585 else
1586#endif
1587#ifdef FEAT_TERMGUICOLORS
1588 if (p_tgc)
1589 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001590 guicolor_T fg, bg;
1591
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001592 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1593 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001594
1595 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001596 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001597 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001598#endif
1599 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001600 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001601 int fg = color2index(&cellfg, TRUE, &bold);
1602 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001603
1604 /* with 8 colors set the bold attribute to get a bright foreground */
1605 if (bold == TRUE)
1606 attr |= HL_BOLD;
1607 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001608 }
1609 return 0;
1610}
1611
1612/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001613 * Called to update a window that contains an active terminal.
1614 * Returns FAIL when there is no terminal running in this window or in
1615 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001616 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001617 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001618term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001619{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001620 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001621 VTerm *vterm;
1622 VTermScreen *screen;
1623 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001624 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001625
Bram Moolenaar6d819742017-08-06 14:57:49 +02001626 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001627 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001628
Bram Moolenaard85f2712017-07-28 21:51:57 +02001629 vterm = term->tl_vterm;
1630 screen = vterm_obtain_screen(vterm);
1631 state = vterm_obtain_state(vterm);
1632
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001633 /*
1634 * If the window was resized a redraw will be triggered and we get here.
1635 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1636 */
1637 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1638 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001639 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001640 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1641 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1642 win_T *twp;
1643
1644 FOR_ALL_WINDOWS(twp)
1645 {
1646 /* When more than one window shows the same terminal, use the
1647 * smallest size. */
1648 if (twp->w_buffer == term->tl_buffer)
1649 {
1650 if (!term->tl_rows_fixed && rows > twp->w_height)
1651 rows = twp->w_height;
1652 if (!term->tl_cols_fixed && cols > twp->w_width)
1653 cols = twp->w_width;
1654 }
1655 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001656
1657 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001658 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001659 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001660 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001661 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001662
1663 /* The cursor may have been moved when resizing. */
1664 vterm_state_get_cursorpos(state, &pos);
1665 position_cursor(wp, &pos);
1666
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001667 /* TODO: Only redraw what changed. */
1668 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001669 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001670 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001671 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001672
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001673 if (pos.row < term->tl_rows)
1674 {
1675 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001676 {
1677 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001678 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001679
Bram Moolenaareeac6772017-07-23 15:48:37 +02001680 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1681 vim_memset(&cell, 0, sizeof(cell));
1682
1683 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001684 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001685 if (c == NUL)
1686 {
1687 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001688#if defined(FEAT_MBYTE)
1689 if (enc_utf8)
1690 ScreenLinesUC[off] = NUL;
1691#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001692 }
1693 else
1694 {
1695#if defined(FEAT_MBYTE)
1696 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001697 {
1698 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001699 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001700 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001701 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001702 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001703 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001704 if (enc_utf8)
1705 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001706 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001707#else
1708 ScreenLines[off] = c;
1709#endif
1710 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001711 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001712
1713 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001714 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001715 if (cell.width == 2)
1716 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001717 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001718#if defined(FEAT_MBYTE)
1719 if (enc_utf8)
1720 ScreenLinesUC[off] = NUL;
1721#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001722 ++pos.col;
1723 ++off;
1724 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001725 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001726 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001727 else
1728 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001729
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001730 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1731 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001732 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001733
1734 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001735}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001736
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001737/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001738 * Return TRUE if "wp" is a terminal window where the job has finished.
1739 */
1740 int
1741term_is_finished(buf_T *buf)
1742{
1743 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1744}
1745
1746/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001747 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02001748 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001749 */
1750 int
1751term_show_buffer(buf_T *buf)
1752{
1753 term_T *term = buf->b_term;
1754
Bram Moolenaar6d819742017-08-06 14:57:49 +02001755 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001756}
1757
1758/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001759 * The current buffer is going to be changed. If there is terminal
1760 * highlighting remove it now.
1761 */
1762 void
1763term_change_in_curbuf(void)
1764{
1765 term_T *term = curbuf->b_term;
1766
1767 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1768 {
1769 free_scrollback(term);
1770 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001771
1772 /* The buffer is now like a normal buffer, it cannot be easily
1773 * abandoned when changed. */
1774 set_string_option_direct((char_u *)"buftype", -1,
1775 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001776 }
1777}
1778
1779/*
1780 * Get the screen attribute for a position in the buffer.
1781 */
1782 int
1783term_get_attr(buf_T *buf, linenr_T lnum, int col)
1784{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001785 term_T *term = buf->b_term;
1786 sb_line_T *line;
1787 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001788
Bram Moolenaar70229f92017-07-29 16:01:53 +02001789 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001790 return 0;
1791 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1792 if (col >= line->sb_cols)
1793 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001794 cellattr = line->sb_cells + col;
1795 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001796}
1797
1798/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001799 * Create a new vterm and initialize it.
1800 */
1801 static void
1802create_vterm(term_T *term, int rows, int cols)
1803{
1804 VTerm *vterm;
1805 VTermScreen *screen;
1806
1807 vterm = vterm_new(rows, cols);
1808 term->tl_vterm = vterm;
1809 screen = vterm_obtain_screen(vterm);
1810 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1811 /* TODO: depends on 'encoding'. */
1812 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001813
1814 /* Vterm uses a default black background. Set it to white when
1815 * 'background' is "light". */
1816 if (*p_bg == 'l')
1817 {
1818 VTermColor fg, bg;
1819
1820 fg.red = fg.green = fg.blue = 0;
1821 bg.red = bg.green = bg.blue = 255;
1822 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1823 }
1824
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001825 /* Required to initialize most things. */
1826 vterm_screen_reset(screen, 1 /* hard */);
1827}
1828
Bram Moolenaar21554412017-07-24 21:44:43 +02001829/*
1830 * Return the text to show for the buffer name and status.
1831 */
1832 char_u *
1833term_get_status_text(term_T *term)
1834{
1835 if (term->tl_status_text == NULL)
1836 {
1837 char_u *txt;
1838 size_t len;
1839
Bram Moolenaar6d819742017-08-06 14:57:49 +02001840 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001841 {
1842 if (term_job_running(term))
1843 txt = (char_u *)_("Terminal");
1844 else
1845 txt = (char_u *)_("Terminal-finished");
1846 }
1847 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001848 txt = term->tl_title;
1849 else if (term_job_running(term))
1850 txt = (char_u *)_("running");
1851 else
1852 txt = (char_u *)_("finished");
1853 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001854 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001855 if (term->tl_status_text != NULL)
1856 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1857 term->tl_buffer->b_fname, txt);
1858 }
1859 return term->tl_status_text;
1860}
1861
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001862/*
1863 * Mark references in jobs of terminals.
1864 */
1865 int
1866set_ref_in_term(int copyID)
1867{
1868 int abort = FALSE;
1869 term_T *term;
1870 typval_T tv;
1871
1872 for (term = first_term; term != NULL; term = term->tl_next)
1873 if (term->tl_job != NULL)
1874 {
1875 tv.v_type = VAR_JOB;
1876 tv.vval.v_job = term->tl_job;
1877 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1878 }
1879 return abort;
1880}
1881
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001882/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001883 * Get the buffer from the first argument in "argvars".
1884 * Returns NULL when the buffer is not for a terminal window.
1885 */
1886 static buf_T *
1887term_get_buf(typval_T *argvars)
1888{
1889 buf_T *buf;
1890
1891 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1892 ++emsg_off;
1893 buf = get_buf_tv(&argvars[0], FALSE);
1894 --emsg_off;
1895 if (buf == NULL || buf->b_term == NULL)
1896 return NULL;
1897 return buf;
1898}
1899
1900/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001901 * "term_getattr(attr, name)" function
1902 */
1903 void
1904f_term_getattr(typval_T *argvars, typval_T *rettv)
1905{
1906 int attr;
1907 size_t i;
1908 char_u *name;
1909
1910 static struct {
1911 char *name;
1912 int attr;
1913 } attrs[] = {
1914 {"bold", HL_BOLD},
1915 {"italic", HL_ITALIC},
1916 {"underline", HL_UNDERLINE},
1917 {"strike", HL_STANDOUT},
1918 {"reverse", HL_INVERSE},
1919 };
1920
1921 attr = get_tv_number(&argvars[0]);
1922 name = get_tv_string_chk(&argvars[1]);
1923 if (name == NULL)
1924 return;
1925
1926 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1927 if (STRCMP(name, attrs[i].name) == 0)
1928 {
1929 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1930 break;
1931 }
1932}
1933
1934/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001935 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001936 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001937 void
1938f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001939{
Bram Moolenaar97870002017-07-30 18:28:38 +02001940 buf_T *buf = term_get_buf(argvars);
1941 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001942
Bram Moolenaar97870002017-07-30 18:28:38 +02001943 if (rettv_list_alloc(rettv) == FAIL)
1944 return;
1945 if (buf == NULL)
1946 return;
1947
1948 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001949 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1950 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001951 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001952}
1953
1954/*
1955 * "term_getjob(buf)" function
1956 */
1957 void
1958f_term_getjob(typval_T *argvars, typval_T *rettv)
1959{
1960 buf_T *buf = term_get_buf(argvars);
1961
1962 rettv->v_type = VAR_JOB;
1963 rettv->vval.v_job = NULL;
1964 if (buf == NULL)
1965 return;
1966
1967 rettv->vval.v_job = buf->b_term->tl_job;
1968 if (rettv->vval.v_job != NULL)
1969 ++rettv->vval.v_job->jv_refcount;
1970}
1971
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001972 static int
1973get_row_number(typval_T *tv, term_T *term)
1974{
1975 if (tv->v_type == VAR_STRING
1976 && tv->vval.v_string != NULL
1977 && STRCMP(tv->vval.v_string, ".") == 0)
1978 return term->tl_cursor_pos.row;
1979 return (int)get_tv_number(tv) - 1;
1980}
1981
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001982/*
1983 * "term_getline(buf, row)" function
1984 */
1985 void
1986f_term_getline(typval_T *argvars, typval_T *rettv)
1987{
1988 buf_T *buf = term_get_buf(argvars);
1989 term_T *term;
1990 int row;
1991
1992 rettv->v_type = VAR_STRING;
1993 if (buf == NULL)
1994 return;
1995 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001996 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001997
1998 if (term->tl_vterm == NULL)
1999 {
2000 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2001
2002 /* vterm is finished, get the text from the buffer */
2003 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2004 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2005 }
2006 else
2007 {
2008 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2009 VTermRect rect;
2010 int len;
2011 char_u *p;
2012
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002013 if (row < 0 || row >= term->tl_rows)
2014 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002015 len = term->tl_cols * MB_MAXBYTES + 1;
2016 p = alloc(len);
2017 if (p == NULL)
2018 return;
2019 rettv->vval.v_string = p;
2020
2021 rect.start_col = 0;
2022 rect.end_col = term->tl_cols;
2023 rect.start_row = row;
2024 rect.end_row = row + 1;
2025 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2026 }
2027}
2028
2029/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002030 * "term_getscrolled(buf)" function
2031 */
2032 void
2033f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2034{
2035 buf_T *buf = term_get_buf(argvars);
2036
2037 if (buf == NULL)
2038 return;
2039 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2040}
2041
2042/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002043 * "term_getsize(buf)" function
2044 */
2045 void
2046f_term_getsize(typval_T *argvars, typval_T *rettv)
2047{
2048 buf_T *buf = term_get_buf(argvars);
2049 list_T *l;
2050
2051 if (rettv_list_alloc(rettv) == FAIL)
2052 return;
2053 if (buf == NULL)
2054 return;
2055
2056 l = rettv->vval.v_list;
2057 list_append_number(l, buf->b_term->tl_rows);
2058 list_append_number(l, buf->b_term->tl_cols);
2059}
2060
2061/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002062 * "term_getstatus(buf)" function
2063 */
2064 void
2065f_term_getstatus(typval_T *argvars, typval_T *rettv)
2066{
2067 buf_T *buf = term_get_buf(argvars);
2068 term_T *term;
2069 char_u val[100];
2070
2071 rettv->v_type = VAR_STRING;
2072 if (buf == NULL)
2073 return;
2074 term = buf->b_term;
2075
2076 if (term_job_running(term))
2077 STRCPY(val, "running");
2078 else
2079 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002080 if (term->tl_normal_mode)
2081 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002082 rettv->vval.v_string = vim_strsave(val);
2083}
2084
2085/*
2086 * "term_gettitle(buf)" function
2087 */
2088 void
2089f_term_gettitle(typval_T *argvars, typval_T *rettv)
2090{
2091 buf_T *buf = term_get_buf(argvars);
2092
2093 rettv->v_type = VAR_STRING;
2094 if (buf == NULL)
2095 return;
2096
2097 if (buf->b_term->tl_title != NULL)
2098 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2099}
2100
2101/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002102 * "term_gettty(buf)" function
2103 */
2104 void
2105f_term_gettty(typval_T *argvars, typval_T *rettv)
2106{
2107 buf_T *buf = term_get_buf(argvars);
2108 char_u *p;
2109
2110 rettv->v_type = VAR_STRING;
2111 if (buf == NULL)
2112 return;
2113 if (buf->b_term->tl_job != NULL)
2114 p = buf->b_term->tl_job->jv_tty_name;
2115 else
2116 p = buf->b_term->tl_tty_name;
2117 if (p != NULL)
2118 rettv->vval.v_string = vim_strsave(p);
2119}
2120
2121/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002122 * "term_list()" function
2123 */
2124 void
2125f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2126{
2127 term_T *tp;
2128 list_T *l;
2129
2130 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2131 return;
2132
2133 l = rettv->vval.v_list;
2134 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2135 if (tp != NULL && tp->tl_buffer != NULL)
2136 if (list_append_number(l,
2137 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2138 return;
2139}
2140
2141/*
2142 * "term_scrape(buf, row)" function
2143 */
2144 void
2145f_term_scrape(typval_T *argvars, typval_T *rettv)
2146{
2147 buf_T *buf = term_get_buf(argvars);
2148 VTermScreen *screen = NULL;
2149 VTermPos pos;
2150 list_T *l;
2151 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002152 char_u *p;
2153 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002154
2155 if (rettv_list_alloc(rettv) == FAIL)
2156 return;
2157 if (buf == NULL)
2158 return;
2159 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002160
2161 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002162 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002163
2164 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002165 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002166 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002167 p = NULL;
2168 line = NULL;
2169 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002170 else
2171 {
2172 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2173
2174 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2175 return;
2176 p = ml_get_buf(buf, lnum + 1, FALSE);
2177 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2178 }
2179
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002180 for (pos.col = 0; pos.col < term->tl_cols; )
2181 {
2182 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002183 int width;
2184 VTermScreenCellAttrs attrs;
2185 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002186 char_u rgb[8];
2187 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2188 int off = 0;
2189 int i;
2190
2191 if (screen == NULL)
2192 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002193 cellattr_T *cellattr;
2194 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002195
2196 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002197 if (pos.col >= line->sb_cols)
2198 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002199 cellattr = line->sb_cells + pos.col;
2200 width = cellattr->width;
2201 attrs = cellattr->attrs;
2202 fg = cellattr->fg;
2203 bg = cellattr->bg;
2204 len = MB_PTR2LEN(p);
2205 mch_memmove(mbs, p, len);
2206 mbs[len] = NUL;
2207 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002208 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002209 else
2210 {
2211 VTermScreenCell cell;
2212 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2213 break;
2214 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2215 {
2216 if (cell.chars[i] == 0)
2217 break;
2218 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2219 }
2220 mbs[off] = NUL;
2221 width = cell.width;
2222 attrs = cell.attrs;
2223 fg = cell.fg;
2224 bg = cell.bg;
2225 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002226 dcell = dict_alloc();
2227 list_append_dict(l, dcell);
2228
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002229 dict_add_nr_str(dcell, "chars", 0, mbs);
2230
2231 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002232 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002233 dict_add_nr_str(dcell, "fg", 0, rgb);
2234 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002235 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002236 dict_add_nr_str(dcell, "bg", 0, rgb);
2237
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002238 dict_add_nr_str(dcell, "attr",
2239 cell2attr(attrs, fg, bg), NULL);
2240 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002241
2242 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002243 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002244 ++pos.col;
2245 }
2246}
2247
2248/*
2249 * "term_sendkeys(buf, keys)" function
2250 */
2251 void
2252f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2253{
2254 buf_T *buf = term_get_buf(argvars);
2255 char_u *msg;
2256 term_T *term;
2257
2258 rettv->v_type = VAR_UNKNOWN;
2259 if (buf == NULL)
2260 return;
2261
2262 msg = get_tv_string_chk(&argvars[1]);
2263 if (msg == NULL)
2264 return;
2265 term = buf->b_term;
2266 if (term->tl_vterm == NULL)
2267 return;
2268
2269 while (*msg != NUL)
2270 {
2271 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2272 msg += MB_PTR2LEN(msg);
2273 }
2274
Bram Moolenaar6d819742017-08-06 14:57:49 +02002275 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002276 {
2277 /* TODO: only update once in a while. */
2278 update_screen(0);
2279 if (buf == curbuf)
2280 update_cursor(term, TRUE);
2281 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002282}
2283
2284/*
2285 * "term_start(command, options)" function
2286 */
2287 void
2288f_term_start(typval_T *argvars, typval_T *rettv)
2289{
2290 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002291 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002292
2293 if (cmd == NULL)
2294 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002295 init_job_options(&opt);
2296 /* TODO: allow more job options */
2297 if (argvars[1].v_type != VAR_UNKNOWN
2298 && get_job_options(&argvars[1], &opt,
2299 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar78712a72017-08-05 14:50:12 +02002300 + JO_EXIT_CB + JO_CLOSE_CALLBACK
2301 + JO2_TERM_NAME) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002302 return;
2303
2304 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002305
2306 if (curbuf->b_term != NULL)
2307 rettv->vval.v_number = curbuf->b_fnum;
2308}
2309
2310/*
2311 * "term_wait" function
2312 */
2313 void
2314f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2315{
2316 buf_T *buf = term_get_buf(argvars);
2317
2318 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002319 {
2320 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002321 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002322 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002323 if (buf->b_term->tl_job == NULL)
2324 {
2325 ch_log(NULL, "term_wait(): no job to wait for");
2326 return;
2327 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002328
2329 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002330 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002331 {
2332 /* The job is dead, keep reading channel I/O until the channel is
2333 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002334 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002335 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2336 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002337 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002338 parse_queued_messages();
2339 ui_delay(10L, FALSE);
2340 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002341 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002342 parse_queued_messages();
2343 }
2344 else
2345 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002346 long wait = 10L;
2347
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002348 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002349 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002350
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002351 /* Wait for some time for any channel I/O. */
2352 if (argvars[1].v_type != VAR_UNKNOWN)
2353 wait = get_tv_number(&argvars[1]);
2354 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002355 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002356
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002357 /* Flushing messages on channels is hopefully sufficient.
2358 * TODO: is there a better way? */
2359 parse_queued_messages();
2360 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002361}
2362
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002363# ifdef WIN3264
2364
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002365/**************************************
2366 * 2. MS-Windows implementation.
2367 */
2368
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002369#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2370#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2371
Bram Moolenaar8a773062017-07-24 22:29:21 +02002372void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002373void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002374void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002375BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2376void (*winpty_config_set_initial_size)(void*, int, int);
2377LPCWSTR (*winpty_conin_name)(void*);
2378LPCWSTR (*winpty_conout_name)(void*);
2379LPCWSTR (*winpty_conerr_name)(void*);
2380void (*winpty_free)(void*);
2381void (*winpty_config_free)(void*);
2382void (*winpty_spawn_config_free)(void*);
2383void (*winpty_error_free)(void*);
2384LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002385BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002386HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002387
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002388#define WINPTY_DLL "winpty.dll"
2389
2390static HINSTANCE hWinPtyDLL = NULL;
2391
2392 int
2393dyn_winpty_init(void)
2394{
2395 int i;
2396 static struct
2397 {
2398 char *name;
2399 FARPROC *ptr;
2400 } winpty_entry[] =
2401 {
2402 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2403 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2404 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2405 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2406 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2407 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2408 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2409 {"winpty_free", (FARPROC*)&winpty_free},
2410 {"winpty_open", (FARPROC*)&winpty_open},
2411 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2412 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2413 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2414 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002415 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002416 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002417 {NULL, NULL}
2418 };
2419
2420 /* No need to initialize twice. */
2421 if (hWinPtyDLL)
2422 return 1;
2423 /* Load winpty.dll */
2424 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2425 if (!hWinPtyDLL)
2426 {
2427 EMSG2(_(e_loadlib), WINPTY_DLL);
2428 return 0;
2429 }
2430 for (i = 0; winpty_entry[i].name != NULL
2431 && winpty_entry[i].ptr != NULL; ++i)
2432 {
2433 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2434 winpty_entry[i].name)) == NULL)
2435 {
2436 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2437 return 0;
2438 }
2439 }
2440
2441 return 1;
2442}
2443
2444/*
2445 * Create a new terminal of "rows" by "cols" cells.
2446 * Store a reference in "term".
2447 * Return OK or FAIL.
2448 */
2449 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002450term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002451{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002452 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002453 channel_T *channel = NULL;
2454 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002455 DWORD error;
2456 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2457 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002458 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002459 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002460
2461 if (!dyn_winpty_init())
2462 return FAIL;
2463
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002464 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002465 if (p == NULL)
2466 return FAIL;
2467
2468 job = job_alloc();
2469 if (job == NULL)
2470 goto failed;
2471
2472 channel = add_channel();
2473 if (channel == NULL)
2474 goto failed;
2475
2476 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2477 if (term->tl_winpty_config == NULL)
2478 goto failed;
2479
2480 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2481 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2482 if (term->tl_winpty == NULL)
2483 goto failed;
2484
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002485 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002486 spawn_config = winpty_spawn_config_new(
2487 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2488 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2489 NULL,
2490 p,
2491 NULL,
2492 NULL,
2493 &winpty_err);
2494 if (spawn_config == NULL)
2495 goto failed;
2496
2497 channel = add_channel();
2498 if (channel == NULL)
2499 goto failed;
2500
2501 job = job_alloc();
2502 if (job == NULL)
2503 goto failed;
2504
2505 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2506 &child_thread_handle, &error, &winpty_err))
2507 goto failed;
2508
2509 channel_set_pipes(channel,
2510 (sock_T) CreateFileW(
2511 winpty_conin_name(term->tl_winpty),
2512 GENERIC_WRITE, 0, NULL,
2513 OPEN_EXISTING, 0, NULL),
2514 (sock_T) CreateFileW(
2515 winpty_conout_name(term->tl_winpty),
2516 GENERIC_READ, 0, NULL,
2517 OPEN_EXISTING, 0, NULL),
2518 (sock_T) CreateFileW(
2519 winpty_conerr_name(term->tl_winpty),
2520 GENERIC_READ, 0, NULL,
2521 OPEN_EXISTING, 0, NULL));
2522
2523 jo = CreateJobObject(NULL, NULL);
2524 if (jo == NULL)
2525 goto failed;
2526
2527 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002528 {
2529 /* Failed, switch the way to terminate process with TerminateProcess. */
2530 CloseHandle(jo);
2531 jo = NULL;
2532 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002533
2534 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002535 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002536
2537 create_vterm(term, rows, cols);
2538
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002539 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002540 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002541
2542 job->jv_channel = channel;
2543 job->jv_proc_info.hProcess = child_process_handle;
2544 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2545 job->jv_job_object = jo;
2546 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002547 sprintf(buf, "winpty://%lu",
2548 GetProcessId(winpty_agent_process(term->tl_winpty)));
2549 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002550 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002551 term->tl_job = job;
2552
2553 return OK;
2554
2555failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002556 if (spawn_config != NULL)
2557 winpty_spawn_config_free(spawn_config);
2558 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002559 if (channel != NULL)
2560 channel_clear(channel);
2561 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002562 {
2563 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002564 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002565 }
2566 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002567 if (jo != NULL)
2568 CloseHandle(jo);
2569 if (term->tl_winpty != NULL)
2570 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002571 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002572 if (term->tl_winpty_config != NULL)
2573 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002574 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002575 if (winpty_err != NULL)
2576 {
2577 char_u *msg = utf16_to_enc(
2578 (short_u *)winpty_error_msg(winpty_err), NULL);
2579
2580 EMSG(msg);
2581 winpty_error_free(winpty_err);
2582 }
2583 return FAIL;
2584}
2585
2586/*
2587 * Free the terminal emulator part of "term".
2588 */
2589 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002590term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002591{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002592 if (term->tl_winpty != NULL)
2593 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002594 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002595 if (term->tl_winpty_config != NULL)
2596 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002597 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002598 if (term->tl_vterm != NULL)
2599 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002600 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002601}
2602
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002603/*
2604 * Request size to terminal.
2605 */
2606 static void
2607term_report_winsize(term_T *term, int rows, int cols)
2608{
2609 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2610}
2611
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002612# else
2613
2614/**************************************
2615 * 3. Unix-like implementation.
2616 */
2617
2618/*
2619 * Create a new terminal of "rows" by "cols" cells.
2620 * Start job for "cmd".
2621 * Store the pointers in "term".
2622 * Return OK or FAIL.
2623 */
2624 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002625term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002626{
2627 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002628
2629 create_vterm(term, rows, cols);
2630
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002631 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002632 argvars[0].v_type = VAR_STRING;
2633 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002634
2635 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002636 if (term->tl_job != NULL)
2637 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002638
Bram Moolenaar61a66052017-07-22 18:39:00 +02002639 return term->tl_job != NULL
2640 && term->tl_job->jv_channel != NULL
2641 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002642}
2643
2644/*
2645 * Free the terminal emulator part of "term".
2646 */
2647 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002648term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002649{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002650 if (term->tl_vterm != NULL)
2651 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002652 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002653}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002654
2655/*
2656 * Request size to terminal.
2657 */
2658 static void
2659term_report_winsize(term_T *term, int rows, int cols)
2660{
2661 /* Use an ioctl() to report the new window size to the job. */
2662 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2663 {
2664 int fd = -1;
2665 int part;
2666
2667 for (part = PART_OUT; part < PART_COUNT; ++part)
2668 {
2669 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2670 if (isatty(fd))
2671 break;
2672 }
2673 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2674 mch_stop_job(term->tl_job, (char_u *)"winch");
2675 }
2676}
2677
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002678# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002679
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002680#endif /* FEAT_TERMINAL */